(x41,x42,x51,x52,x55,x56,x76,x77,x47,x74,x14) ; Main refinement group loop
;
; grploop.pam -- Main refinement group loop       al Nov 2000
;                              stat in operation  al Feb 2005
;                              stack alignment    al Mar 2005
; MASTER COPY: /net/bali/usr1/spider/docs/techs/recon/newprogs/
; 
; PURPOSE:    Runs for each defocus group on each iteration
;
; CALLED FROM: pub_starter.pam or  
;              refine.pam  
;
; INPUT REGISTERS:
;    x41                     Angular steps           (Varies with iter)
;    x42                     Restrict angular search (Varies with iter)
;    x51                     Radius of structure
;    x52                     Image size
;    x55                     Max. shift allowed is +-x55
;    x56                     Radius of structure used in projection
;    x74                     Starting alignment iteration    
;    x76                     Alignment step iteration counter  (Varies with iter)
;    x77                     Defocus group                     (Varies with group)
; OUTPUT REGISTERS:
;    x47                     % of images whose proj. angle moved by > 1.5*x41
;
; INPUT FILES (SET IN: refine_settings  refine_settings.pam.pam):
;    input/order_select                   [order_select]        Image ID doc. fil
;    work/select_{***group}               [group_select]       (From prepare)
;    final/vft{**iter}                    [iter_vft]           (From refine)
;    work/ctf{***grp}                     [temp_ctf_file]      (From prepare)
;    input/dala{**iter}_{***group}@       [aligned_images]      Aligned stacked image files
;    final/align{**iter}_{***group}       [next_group_align]
;    input/data{***grp}@                  [unaligned_images]    Unaligned stacked image file  
;    final/angvoea_{**iter}               [iter_ang_voea]        
;    final/selvoea_{**iter}               [iter_select_voea] 
;
; OUTPUT FILES(SET IN: refine_settings refine_settings.pam.pam):
;    work/dala{**iter+1}_{***grp}@        [next_aligned_images]
;    final/align{**iter+1}_{***group}     [next_group_align]
;    work/vol{**iter}_{***group}          [next_group_vol]
;    work/vol_{**iter+1}_odd_{***group}   [next_group_vol]_odd  (Deleted in mergegroups)
;    work/vol_{**iter+1}_even_{***group}  [next_group_vol]_even (Deleted in mergegroups)
;    local/refproj{**iter}_{***grp}@      [temp_ref_projs]      (Created & deleted)
;    local/refproj{**iter+1}_{***group}   [temp_ref_projs]      (Created & deleted)
;    final/dres{**iter+1}_{***group}      [next_group_dres]       
; 
; PROCEDURES CALLED: saveresp
;
; INLINE BUFFERS USED: _1, _4

 VM
 echo " In grploop, Iteration: {**x76}  Group: {***x77}"
 VM
 date '+ Time: %x  %X'

 x54=5.0                   ; First radius for AP (Can alter this)
 x17=1.5*x41               ; Convergence criterion angular distance limit.
 
 x58=int(x52/2)+1          ; Image center
 x86=x76+1                 ; Next iteration

 UD N,x53                  ; Get number of ref. projections in use
 [iter_select_voea]
 MY FL                     ; Flush results file

 UD IC,x77,x79             ; Sets x79 to number of images in this group
 [order_select]
 UD ICE
 [order_select]

 ; Multiply Fourier of current volume by CTF file for this group
 MU                        ; Multiply
 [iter_vft]                ; Fourier of current volume (final/vft{iter}
 [temp_ctf_file]           ; CTF  file     (work/ctf{grp})
 _1                        ; _1 created here
 *
 
 FT                        ; Reverse Fourier transform
 _1                        ; Fourier of CTF corrected current volume
 _4                        ; CTF corrected current volume created here 

 DE                        ; _1 can be deleted here 
 _1                        ; Fourier of CTF corrected current volume

 ; Create stack holding angular reference projections from input volume.
 PJ 3Q                     ; Create reference projections
 _4                        ; CTF corrected current volume     (input)
 x56                       ; Radius of object
 [iter_select_voea]        ; Ref. angles selection doc. file  (input) - selvoea
 [iter_ang_voea]           ; Ref. angles doc. file            (input)
 [temp_ref_projs]@******   ; Template for ref. projections    (output) - refproj

 DE                        ; _4 can be deleted here
 _4                        ; CTF corrected current volume

 DE                        ; Remove existing next alignment doc output file  
 [next_group_align]        ; Alignment doc file

 DE                        ; Remove existing scratch file  
 work/SCR_{**x76}_{***x77} ; Reference rings scratch file 

 MY FL                     ; Flush results file

 ; Find reference projection matching current aligned image
 AP REF 
 [temp_ref_projs]@******   ; Template of existing ref. projections 
 (1-x53)                   ; Ref. projections file numbers
 x55                       ; Shift search range
 (x54,x51,1)               ; First, last ring & skip
 [iter_ang_voea]           ; Ref. angles file  (input)
 work/SCR_{**x76}_{***x77} ; No scratch file if fits in-core
 [aligned_images]*****     ; Template for current exp. aligned images  
 (1-x79)                   ; Current exp. images file numbers
 [group_align]             ; Exp. images alignment doc. file < < < .....
 (x42,x17)                 ; Angular proj search restriction & stop threshold
 (1)                       ; Check mirrored projections
 [next_group_align]        ; Alignment doc file (output)

 ; Align experimental images 
 RT SQ                     ; Rotate & shift (stack operation)
 [unaligned_images]        ; Unaligned original images (stack)
 [next_aligned_images]     ; Current aligned images (output)
 (6,0,7,8)                 ; Rot., scale, & shift registers
 [next_group_align]        ; Alignment doc file from 'AP REF'
      
 ; '%BIG-ANGDIF, AVG-ANGDIF, AVG-CCROT, %WORSE, AVG-WORSE,  AVG-BETTER'
 UD -2, x47,      x92,        x93,       x94,    x95,        x96
 [next_group_align]         ; Next align. doc file
 UD E

 ; Echo size of change in proj. angle, etc.
 VM
 echo " Iteration: {**x76} Group: {***x77}  Excessive changes: {***x47}%"
 VM
 echo " Degred.%: {%f5.1%x94}  Avg. degred.: {%f10.2%x95}  Avg. improve: {%f10.2%x96}"

 VM                             ; Echo this for isolating stack overflow
 echo " Commencing back projection"

 MY FL

 ; Calculate new, refined volume using centered projections  and 
 ; the angles from align doc. file
 BP 32F                         ; Back Projection - 3D Fourier
 [next_aligned_images]******    ; Template for aligned images (stacked)
 [group_select]                 ; Selection doc. file   - select
 [next_group_align]             ; Alignment doc file    - align
 *                              ; No symmetries  
 [next_group_vol]tmp            ; Reconstructed volume - voltmp
 [next_group_vol]_odd           ; Reconstructed volume - vol_odd
 [next_group_vol]_even          ; Reconstructed volume - vol_even

 MY FL

 CG PH,x11,x11,x11,x21,x22,x23  ; Find center of gravity of new vol.
 [next_group_vol]tmp
 MY FL

 ; Center reconstructed volume
 SH F                           ; Shift new vol.
 [next_group_vol]tmp            ; Input  volume
 [next_group_vol]               ; Output voume
 -x21,-x22                      ; Shifts
 -x23

 DE                             ; Delete temp file - vol{iter}tmp
 [next_group_vol]tmp

 ; Find resolution metric
 RF 3 x69,x68                   ; Phase Residual &  shell correl.
 [next_group_vol]_odd           ; First  volume input file  
 [next_group_vol]_even          ; Second volume input file
 (0.5)
 (0.2,2.0)
 C
 (90.0)
 (3)
 [next_group_dres]              ; Output document file - dres

 ; Record group resolution in resolutiongs doc file
 @saveresp(x14,x76,x77,x68)     
 [grp_resol]                    ; Output file

 VM                             ; Remove - refproj (no longer needed)
 \rm -f [temp_ref_projs]*.$DATEXT 

 IF (x76 .GT. x74) THEN         ; Not first iteration
    DE                          ; Remove previous group vol. 
    [group_vol]                 ;   work/vol_{**x76}_{***x77}      
    DE                          ; Remove previous odd group vol.
    [group_vol]_odd             ;   work/vol_{**x76}_{***x77}_odd
    DE                          ; Remove previous even group vol.
    [group_vol]_even            ;   work/vol_{**x76}_{***x77}_even
    DE                          ; Remove previous current aligned images
    [aligned_images]            ;   work/dala{**x76}_{***x77}
 ENDIF

 MY FL
RE
;