([freq],[iter],[ampenhance]) ; ; Merges groups at end of each refinement iteration
;
; SOURCE:  mergegroups.pam       ArDean Leith   Nov 2000
;                                saveresp       Jan 2005
;                                enhance        Apr 2005
;                                []             Dec 2005
; MASTER COPY: /net/bali/usr1/spider/docs/techs/recon/programs/
; 
; PURPOSE: Consolidates CTF data at end of each iteration, not in ||
;
; CALLED FROM: pub_refine  pub_refine.pam
;
; INPUT REGISTERS:
;    [freq]                     Nyquist frequency        
;    [iter]                     Refinement step iteration counter  
;    [ampenhance]               Flag to use amplitude enhancement  
;
; INPUT FILES:
;    input/order_select_sort               [sorted_order_select]
;    work/ctf***                           [temp_ctf_file_template] Template for 3D CTF files
;    work/defgrp{***grp}/vol{**iter}       [next_group_vol]
;    work/defgrp{***grp}/vol_{**iter}      [next_group_vol_template]      (deleted at end)  
;    work/defgrp{***grp}/vol_{**iter}_odd  [next_group_vol_template]_odd  (deleted at end)  
;    work/defgrp{***grp}/vol_{**iter}_even [next_group_vol_template]_even (deleted at end)
;    input/mask                            [mask]  (Mask created from input file)

; OUTPUT FILES:
;    final/val{**iter}                     [next_val]      (Unfiltered volume)
;    final/vol{**iter}                     [next_vol]      (Filtered volume)
;    final/vol{**iter}_odd                 [next_vol]_odd  (Filtered volume)
;    final/vol{**iter}_even                [next_vol]_even (Filtered volume)
;    final/dres{**iter}                    [next_dres]     (Resolution doc)
;    work/enhance_doc     ???              [enhance_doc]   (enhancement document)
;                         ???              [next_val]_amps (amplitude-enhanced volume, unfiltered)
;
;.......................................................................

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

   [ring] = 0.5                  ; FSC ring width
   [next-iter]=[iter] + 1        ; Next iteration

   TF CTS                        ; Transfer Function - CTF correction
   [next_group_vol_template]     ; Template for 3D volumes               (input)
   [sorted_order_select]         ; Group Selection File                  (input)
   [temp_ctf_file_template]      ; Template for 3D ctf file - work/ctf   (input)
   (3)                           ; SNR
   [next_val]                    ; Unfiltered CTF'd volume               (output)

   TF CTS                        ; Transfer Function - CTF correction
   [next_group_vol_template]_odd ; Template for 3D odd volumes           (input)
   [sorted_order_select]         ; Group Selection File                  (input)
   [temp_ctf_file_template]      ; Template for 3D ctf files - work/ctf  (input)
   (3)                           ; SNR
   [next_vol]_odd                ; Odd volume                           (output)

   TF CTS                        ; Transfer Function - CTF correction
   [next_group_vol_template]_even ; Template for 3D even volumes         (input)
   [sorted_order_select]         ; Group Selection File                  (input)
   [temp_ctf_file_template]      ; Template for 3D ctf files - work/ctf  (input)
   (3)                           ; SNR
   [next_vol]_even               ; Even output volume                    (output)

   DE
   [next_dres]                   ; Resolution output doc file 

   ; Resolution calculation
   RF 3 [p69],[spfreq]           ; Phase Residual & Fourier shell correlation
   [next_vol]_odd                ; Odd volume                           (input)
   [next_vol]_even               ; Even volume                          (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]                   ; Resolution doc file - dres_iter     (output)

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

   IF ([spfreq] .GT. 0.35) [spfreq] = 0.4  ; Limits pass-band
   [stop-band]= [spfreq]+0.15              ; Stop band frequency
   [filter-limit]=int([p69]*[ring])        ; Filter limit
   [filter-limit]                          ; diagnostic

   IF ([ampenhance] .EQ. 1) THEN           ; Use amplitude enhancement

      DE                            ; Delete 
      [enhance_doc]                 ; Output enhancement document file

      ; Generate amplitude enhancement curve 
      @enhance([filter-limit],[freq],[next-iter]) ; Filter limit, Max. spatial frequency, Next iter.
      [next_val]                    ; Input volume

      FD                            ; Filter using enhancement doc. file
      [next_val]                    ; Input volume
      [next_val]_amps               ; Output amplitude-enhanced volume, unfiltered
      [enhance_doc]                 ; Input enhancement document file

      FQ                            ; Quick Filter          
      [next_val]_amps               ; Input file (Created above)   - val
      [next_vol]                    ; Output volume, ampl enhanced and filtered
      (7)                           ; Butterworth low pass filter
      [spfreq],[stop-band]          ; Pass-band and stop-band frequency
      
      IQ FI [masked]                ; See if mask file exists
      [mask]                        ; Mask created from ribosome-translocon

      IF ([masked] .GE. 1) THEN     ; Use mask file
         MM C                       ; Mask multiply to eliminate high freq. noise
         [mask]                     ; Mask created from ribosome-translocon
         [next_vol]                 ; Volume for next iteration
      ENDIF

   ELSE
      ; Filter output volume to limit the resolution
      FQ                            ; Quick Filter          
      [next_val]                    ; Input file (Created above)   - val
      [next_vol]                    ; Creates filtered volume, used on next iter.
      (7)                           ; Butterworth Low pass filter
      [spfreq],[stop-band]          ; Pass-band and stop-band frequency
   ENDIF

   VM                               ; Saves space for tests - grp/vol!.
   \rm [next_group_vol_template]_odd* [next_group_vol_template]_even*   

   VM
   echo " Merged groups for iteration: {**[iter]}"
   VM
   date '+ Time: %x  %X'

   MY FL
   RE
;