([prj-radius],[grp],[iter]) ; Merge volumes at end of defocus group loop
; SOURCE: spider/docs/techs/recon/newprogs/endmerge.pam
;         new                                Nov 2000  ArDean Leith
;         []                                 Dec 2005  ArDean Leith
;         maxim                              Dec 2006  ArDean Leith
;         group sel. filename                Dec 2009  ArDean Leith
;         echo format                        Aug 2010  ArDean Leith  
;         'BP CG 3'                          Jan 2011  ArDean Leith
;
; INPUT REGISTERS: (Read from caller)
;   [prj-radius]           Radius of restored object 
;   [grp]                  Group number
;   [next-iter]            Iteration number
;
;  '##' denotes iteration,  '##+' denotes next iteration,  '***' denotes group
; INPUT FILES:
;   [sel_particles]        input/select_***        Group particle selection doc file
;   [next_group_align]     final/align##+***       Alignment parameter doc file 
;
; OUTPUT FILES:
;   [next_group_bpr]       final/bpr##+_***        Reconstructed group volume
;   [next_group_bpr]_sub1  final/bpr##+_***_sub1   Group volume-sub1 
;   [next_group_bpr]_sub2  final/bpr##+_***_sub2   Group volume-sub2 
;   [next_group_dbpr]      final/dbpr##+           Overall resolution doc file 
;
; Uses inline stack file: _8
;......................................................................

[next-iter]=[iter]+1

VM
echo -n " Starting endmerge, Group: {***[grp]} ---  " ; date '+ %x  %X'

DE
 _8@                       ; If not deleted may overflow existing stack

FI H [maxim]               ; Find total number of images (not same as in group selection)
  [unaligned_images]@      ; Aligned images                    (input)
  MAXIM                    ; Number of images in file 

; Rarely the aligned projections may not fit in _8@, if not, use disk stack.
CP                         ; Load unaligned images into inline stack #8
  [unaligned_images]@      ; Aligned images                    (input)
  _8@                      ; Inline stack                      (output)
  [maxim]                  ; Number of images (Not all in use)

; Apply new transforms to original particle images 
RT SQ                      ; Rotate & shift operation
  _8@******                ; Unaligned original stacked images 
  [sel_particles]          ; Particle selection file            (input) 
  6,0,7,8                  ; Reg. #s for angle, scale, & shift
  [next_group_align]       ; Alignment parameter doc file       (input)
  _8@******                ; Current aligned images             (output)

; Calculate volumes using 'BP CG 3' (Gives better resolution than 'BP 32F').
BP CG 3                    ; Back Projection - CG
  _8@******                ; Template for image files           (input)
  [sel_particles]          ; Group particle selection doc. file (input)
  [prj-radius]             ; Radius of restored object
  [next_group_align]       ; Alignment parameters doc file      (input)
  L                        ; Linear interpolation 
  [next_group_bpr]         ; Reconstructed group volume         (output)
  [next_group_bpr]_sub1    ; Reconstructed group volume 1       (output)
  [next_group_bpr]_sub2    ; Reconstructed group volume 2       (output)
  1.0E-5,0.0               ; Error limit, chi^2 limit
  25, 1                    ; Iteration limit, mode
  2000                     ; Lambda

MY FL

; Assess group reconstruction resolution
RF 3                       ; Phase Residual & FSC
  [next_group_bpr]_sub1    ; Group subset volume 1              (input)
  [next_group_bpr]_sub2    ; Group subset volume 2              (input)
  0.5                      ; Ring width
  0.2, 2.0                 ; Scale factor
  C                        ; Missing cone or wedge
  90.0                     ; Max. tilt angle
  3.0                      ; Factor for noise comparison
  [next_group_dbpr]        ; Group resolution doc file          (output)

DE
_8@

MY FL

RE
;