([freq],[iter],[ampenhance]) ; ; Merges groups at end of each refinement iteration
;
; SOURCE: spider/docs/techs/recon/newprogs/mergegroups.pam
;         New                                  ArDean Leith  Nov 2000
;         saveresp                             ArDean Leith  Jan 2005
;         Enhance                              ArDean Leith  Apr 2005
;         []                                   ArDean Leith  Dec 2005
;         Filenames                            ArDean Leith  Dec 2009
;         Echo format                          ArDean Leith  Aug 2010
;         CofG centering now done here         ArDean Leith  Sep 2010
;         Ampl. enhancement moved out          ArDean Leith  Oct 2010
;
; PURPOSE: Consolidates goup volumes with CTF correction at end of each 
;          iteration into overeall volumes. Filters to limit resolution.
;          (This is not done in parallel).
;
; CALLED FROM: pub_refine  pub_refine.pam
;
; INPUT REGISTERS:
;    [freq]                                 Nyquist frequency for filtering        
;    [iter]                                 Refinement step iteration counter  
;    [ampenhance]                           Flag to use amplitude enhancement  
;
;  '##' denotes iteration,  '##+' denotes next iteration,  '***' denotes group
; INPUT FILES:
;    [sel_group_sorted]              input/sel_group_sorted         Sorted list of groups                
;    [temp_ctf_file_template]        work/ctf***                    3D CTF files
;    [next_group_vol_template]       work/defgrp***/vol_##          Group volume    
;    [next_group_vol_template]_sub1  work/defgrp***/vol_##_sub1     Group volume, deleted at end   
;    [next_group_vol_template]_sub2  work/defgrp***/vol_##_sub2     Group volume, deleted at end 
;    [mask]                          input/mask  (Optional)         Mask from original input volume 

; OUTPUT FILES:
;    [next_val]                      final/val##                    Unfiltered volume
;    [next_vol]_filtered             final/vol##_filtered           Filtered volume
;    [next_vol]_sub1                 final/vol##_sub1               Filtered volume - subset 1
;    [next_vol]_sub2                 final/vol##_sub2               Filtered volume - subset 2
;    [next_vol]                      final/vol##                    Shifted, filtered volume
;    [next_dres]                     final/dres##                   Resolution doc.
;    [enhance_doc]                   work/enhance_doc_##+  (Optional) Ampl filter doc. file
;    [next_val]_amps                 final/val##+_amps     (Optional) Ampl enhanced vol., unfiltered
;
;.......................................................................

   ; All defocus groups must be present here for "TF CTS" use!

   [next-iter]=[iter] + 1                   ; Next iteration

   TF CTS                                   ; Transfer Function - CTF Correction
   [next_group_vol_template]                ; Overall group volumes             (input)
   [sel_group_sorted]                       ; Group selection file              (input)
   [temp_ctf_file_template]                 ; 3D CTF files                      (input)
   (3)                                      ; SNR
   [next_val]                               ; Overall CTF'd volume              (output)

   TF CTS                                   ; Transfer Function - CTF Correction
   [next_group_vol_template]_sub1           ; Subset 1 group volumes            (input)
   [sel_group_sorted]                       ; Group selection file              (input)
   [temp_ctf_file_template]                 ; 3D CTF files                      (input)
   (3)                                      ; SNR
   [next_vol]_sub1                          ; CTF'd volume - subset 1           (output)

   TF CTS                                   ; Transfer Function - CTF Correction
   [next_group_vol_template]_sub2           ; Subset 2 group volumes            (input)
   [sel_group_sorted]                       ; Group Selection File              (input)
   [temp_ctf_file_template]                 ; 3D CTF files                      (input)
   (3)                                      ; SNR
   [next_vol]_sub2                          ; CTF'd volume - subset 2           (output)

   DE
   [next_dres]                              ; FSC doc file                      (removed) 

   ; Resolution calculation
   RF 3 [p69],[spfreq]                      ; Phase Residual & FSC
   [next_vol]_sub1                          ; Volume - subset 1                  (input)
   [next_vol]_sub2                          ; Volume - subset 2                 (input)
   (0.5)                                    ; Ring width
   (0.2,2.0)                                ; Scale factor
   C                                        ; Missing cone/wedge angle 
   (90.0)                                   ; Maximum tilt angle
   (3)                                      ; Factor for noise comparison
   [next_dres]                              ; FSC doc file                      (output)

   [grp] = 0                                ; Defocus group is zero now (none)
   @saveresp([freq],[iter],[grp],[spfreq])  ; Record resolution in doc file
   [dbpr_resol]                             ; Resolution doc file               (output)

   IF ([ampenhance] .EQ. 0) THEN            ; No amplitude enhancement (Usual case)
      ; Filter to limit resolution

      IF([spfreq] .GT. 0.35)[spfreq] = 0.4  ; Limits pass-band
      [stop-band] = [spfreq]+0.15           ; Stop band frequency

      ; Filter output volume to limit resolution
      FQ                                    ; Quick filter          
      [next_val]                            ; CTF'd volume                     (input)
      [next_vol]_filtered                   ; Filtered volume                  (output)
      (7)                                   ; Butterworth Low pass filter
      [spfreq],[stop-band]                  ; Pass-band and stop-band freq.

   ELSE                                     ; Use amplitude enhancement
      ; Apply amplitude enhancement filter and filter to limit resolution
      @enhance([p69],[freq],[next-iter],[spfreq])  
      [next_val]                            ; Reconstructed volume             (input)
      [next_vol]_filtered                   ; Filtered volume                  (output)

   ENDIF

   ; Find center of gravity of overall filtered volume
   CG PH [unused],[unused],[unused],[xcg],[ycg],[zcg]  
   [next_vol]_filtered                      ; Filtered volume                  (input)

   ; Shift filtered vol to its CofG
   SH F                                     ; Shift filtered vol 
   [next_vol]_filtered                      ; Filtered volume                  (input)
   [next_vol]                               ; Shifted filtered volume          (output)
   -[xcg],-[ycg],-[zcg]                     ; Shift distances for CofG
 
   VM                                       ; Delete files to save space
   \rm [next_group_vol_template]_sub1* [next_group_vol_template]_sub2*   

   VM
   echo -n " Iter: {**[iter]}"   Shift: -{%F4.1%[xcg]},-{%F4.1%[ycg]},-{%F4.1%[zcg]} ; date '+ Time: %x  %X' 

   MY FL
   RE

;