([ang-step],[radius],[alignsh],[prj-radius],[iter],[grp],[toobig],[maxspfreq]) ; Small angle refinement group loop
;
; SOURCE: smangloop.pam                                     ArDean Leith  Nov. 2002
;                          rewrite                          ArDean Leith  Oct. 2004
;                          exp images name bug              ArDean Leith  Jan. 2005
;                          BP align bug                     ArDean Leith  Feb. 2005
;                          avg CCROT degredations           ArDean Leith  Feb. 2005
;                          []                               ArDean Leith  Dec. 2005
;                          more stacks                      ArDean Leith  Dec. 2006
;                          removed iter1 parameter          ArDean Leith  Feb. 2007
;
; MASTER COPY: /net/bali/usr1/spider/docs/techs/recon/programs/
; 
; PURPOSE: Small angle refinement group loop.  Runs for each defocus 
;          group on each iteration
;
; CALLED FROM: pub_refine_start or   
;              refine.pam  
;
; REGISTERS AND FILES ARE SET IN: refine settings refine settings.pam.pam); INPUT REGISTERS:
;
; INPUT REGISTERS:
;   [ang-step]                Angular difference stopping limit   
;   [toobig]                  Iteration ending flag   (returned)
;   [radius]                  Radius (pixels) of the structure
;   [alignsh]                 Shift allowed is +-[alignsh]
;   [prj-radius]              Radius (pixels) of structure for projection
;   [iter]                    Alignment step iteration counter  
;   [grp]                     Current defocus group                      
;
;  '##' denotes iteration,  '##+' denotes next iteration, and '***' denotes group
; INPUT FILES (SET IN: refine_settings  refine_settings.pam.pam):
;   [order_select]            input/order_select           Image ID doc. file
;   [sorted_order_select]     input/order_select_sort      Image ID doc. fil
;   [sel_particles]           input/select_***             Particle selection doc. file
;   [iter_vft]                final/vft##                  Current filtered volume (from refine)
;   [temp_ctf_file]           work/ctf***                  CTF corrected volume (from prepare)
;   [aligned_images]          input/dala##_***@            Aligned stacked image files
;   [group_align]             final/align##+_***           Alignment parameter doc file
;   [unaligned_images]        input/data***@               Unaligned stacked image file  
;   [iter_ang_voea]           final/angvoea_##             Ref. angles doc. file        
;   [iter_select_voea]        final/selvoea_##             Ref. angles selection doc. file    
;
; OUTPUT FILES(SET IN: refine_settings refine_settings.pam.pam):
;   [next_aligned_images]     work/dala##+_***@            Aligned stacked image file
;   [next_group_align]        final/align##+_***           Alignment parameter doc file
;   [next_group_vol]          work/defgrp***/vol##+        Reconstructed volume
;   [next_group_vol]_odd      work/defgrp***/vol_##+_odd   (created & deleted)  
;   [next_group_vol]_even     work/defgrp***/vol_##+_even  (created & deleted) 
;   [next_group_dres]         final/defgrp***/dres##+      Resolution curve doc. file
;   [img_ang_vora]            final/angvora_##_***                 
;   [temp_ref_projs]_5@                                    (created & deleted)
; 
; PROCEDURES CALLED: 
;
; INLINE BUFFERS USED: _1,_4

 VM
 echo " In smangloop, Iteration: {**[iter]}  Group: {***[grp]}"
 VM
 date '+ Time: %x  %X'

 [radius1] = 5.0                           ; First radius for 'AP REF' (Can alter this)
 [converg] = 1.5*[ang-step]                ; Angular distance limit for stopping iters.
 [next-iter]=[iter]+1
 
 UD N [num-refs]                           ; Get number of ref. imges 
 [iter_select_voea]                        ; Reference image selection file   (input)

 ; Get number of images in current group
 UD N,[numgrps]                            ; Find number of defocus groups
 [order_select]                            ;                                  (input)

 DO LB1 [i]=1,[numgrps]                    ; Loop over defocus groups  --------
    ;         GROUP, PART.               
    UD IC [i], [grpt], [num-imgs]         ; Get current number of particles 
    [order_select]                        ; Doc file listing groups          (input)    
    IF ([grpt].EQ.[grp])GOTO LB2
 LB1
 LB2                                       ; Got number of particles in group
 UD ICE
 [order_select]

 MY FL                                     ; Flush results file

 ; Multiply Fourier of current volume by CTF file for this group
 MU                                        ; Multiply
 [iter_vft]                                ; Fourier of current volume         (input}
 [temp_ctf_file]                           ; CTF  file     (work/ctf{grp})     (input)
 _1                                        ; _1 created here                   (output)
 *                                         ; No more multiplications
 
 FT                                        ; Fourier transform
 _1                                        ; Fourier of CTF corrected volume   (input)
 _4                                        ; CTF corrected current volume      (output) 

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

 FI [nsam]                                 ; Find image size
 _4                                        ; CTF corrected current volume      (input) 
 (2)                                       ; Location of nsam in header

 MS                                        ; Make MT inline stack for ref. projections
 [temp_ref_projs]                          ; MT stack                          (output)
 ([nsam],[nsam],1)                         ; Image size
 [num-refs]                                ; Number of images allowed in stack

 DE                                        ; Remove existing align parameter doc file 
 [next_group_align]

 ; Make header for next group align doc file 
 SD /PSI, THE, PHI, REF, EXP, ANG, SX, SY, NPROJ, DIFF, CCROT, ANG, SX, SY, MIR-CC
 [next_group_align]                  ; (output)

 [num-big]    = 0                    ; Number of angular changes above iteration stop limit
 [toobig]     = 0                    ; Not OK to stop iterating
 [sum-ccrot]  = 0.0                  ; Sum of CCROT correlation coefficients
 [num-degred] = 0.0                  ; Number of CCROT degredations
 [sum-degred] = 0.0                  ; Sum of CCROT degredations
 [num-impr]   = 0.0                  ; Number of CCROT improvements
 [sum-impr]   = 0.0                  ; Sum of CCROT improvements
 [sum-diff]   = 0.0

 DO LB3 [numpart]=1,[num-imgs]       ; Loop over all particles
    UD [numpart], [img]              ; Get current particle number 
    [sel_particles]                  ; Group particle selection file      (input) 

    ; Format of alignment parameters doc. file is:
    ;         PSI,THE,PHI,           REF#,EXP#, ANG,SX,SY,   NPROJ,DIFF,CCROT
    UD IC [img], [psi],[the],[phi],  [d],[exp], [d],[d],[d], [d],[d],[old-ccrot]
    [group_align]                    ; Input alignment parameters doc. file

    DE                               ; Remove existing angvora doc. file  
    [img_ang_vora]                   ; angvora

    VO RAS                      
    [iter_ang_voea]                  ; Relative angles file            (input)
    -[phi],-[the],-[psi]             ; Offset
    (1,0)                            ; Psi set to zero
    [img_ang_vora]                   ; Doc. file for angles to search  (output)

    ; Create stack holding set of reference projections from input volume.
    PJ 3Q                            ; Create ref. projections
    _4                               ; CTF corrected current volume     (input)              
    [prj-radius]                     ; Radius of computed object
    (1-[num-refs])                   ; Ref. projection file numbers 
    [img_ang_vora]                   ; Angles in search area doc. file  (input)
    [temp_ref_projs]******           ; Template for ref. projections    (output)

    MY FL                            ; Flush results file

    ; Find reference image matching experimental image.  Output to registers not doc. file
    ;       PSI,THE,PHI,       REF#,EXP#, ANG,  SX, SY,    NPROJ,DIFF,     CCROT,  CURRENT_ALIGN
    AP REF  [psi],[the],[phi], [ref],[exp], [inp],[sx],[sy], [nproj],[diff], [ccrot], x70,x71,x72,x73
    [temp_ref_projs]******           ; Template of existing ref. projections (input) 
    (1-[num-refs])                   ; Ref. projection file numbers
    [alignsh]                        ; Shift search range
    ([radius1],[radius],1)           ; First, last ring, & skip
    [img_ang_vora]                   ; Ref. angles file                  (input)
    SCR.{******[grp]}                ; No scratch file (usually will fit in-core)
    [aligned_images]******           ; Aligned original images           (input)
    [img]                            ; Current exp. image file number
    [group_align]                    ; Alignment parameters doc. file     (input)
    (0)                              ; Angular projection search restriction
    (0)                              ; Mirrored projection check flag 

    ;         PSI,THE,PHI,         REF#,EXP#, ANG,  SX,  SY,   NPROJ,DIFF,CCROT
    SD [img], [psi],[the],[phi],  [ref],[img],[inp],[sx],[sy], [num-refs],[diff],[ccrot], x70,x71,x72,x73
    [next_group_align]                                 ; Next align. doc file (output)

    IF ([ccrot] .LT. [old-ccrot]) THEN
       [sum-degred]=[sum-degred]+([old-ccrot]-[ccrot]) ; Sum average CCROT degredation
       [num-degred]=[num-degred]+1                     ; Increment   CCROT degredations counter
    ELSE
       [sum-impr]=[sum-impr]+([ccrot]-[old-ccrot])     ; Sum average CCROT improvement
       [num-impr]=[num-impr]+1                         ; Increment   CCROT improvements counter
    ENDIF

    IF ([diff] .GT.[converg]) [num-big]=[num-big]+1    ; Increment large displacement counter
    [sum-ccrot]=[sum-ccrot]+[ccrot]                    ; Sum rotational CCC (for average).
    [sum-diff]=[sum-diff]+[diff]                        ; Sum of angular differences
      
    ; Apply combined transformation to original exp. sample image
    RT SQ                                              ; Rotate & shift
    [unaligned_images]@{******[img]}                   ; Unaligned original images stack  (input)
    [next_aligned_images]{******[img]}                 ; New aligned exp. images stack    (output)
    [inp]                                              ; Inplane rotation angle
    [sx],[sy]                                          ; X & Y shift
 LB3

 UD ICE                                                ; Close this file here
 [group_align]                  

 ; Calculate refined 3D structure using centered projections and new angles. 
 BP 32F                         ; Back projection, 3D, Fourier space
 [next_aligned_images]******    ; Template for aligned images      (input)
 [sel_particles]                ; Particle selection doc. file     (input)
 [next_group_align]             ; Align parameters doc file        (input)
 *                              ; No symmetries file  
 [next_group_vol]tmp            ; Reconstructed 3D file            (output)
 [next_group_vol]_odd           ; Reconstructed 3D file            (output)
 [next_group_vol]_even          ; Reconstructed 3D file            (output)

 CG PH,x11,x11,x11,[xcg],[ycg],[zcg]  ; Find center of gravity of new vol.
 [next_group_vol]tmp

 SH F                           ; Shift new vol. to center of gravity
 [next_group_vol]tmp            ; volume file - voltmp            (input)
 [next_group_vol]               ; volume file - vol               (output)
 -[xcg],-[ycg],-[zcg]           ; Shift amounts

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

 DE                             ; Delete  file
 [next_group_dres]              ; FSC document file - dres 

 RF 3                           ; Phase Residual &  shell correl.
 [next_group_vol]_odd           ; First  volume file             (input) 
 [next_group_vol]_even          ; Second volume file             (input) 
 (0.5)                          ; Ring width
 (0.2,2.0)                      ; Lower and upper scale factor
 C                              ; Missing cone
 (90.0)                         ; Maximum tilt angle 
 (3)                            ; Factor for noise comparison
 [next_group_dres]              ; Resolution document file      (output)

 DE                             ; Remove - refproj (no longer needed)
 [temp_ref_projs] 

 IF ([iter] .GT. 1) THEN        ; Not first iteration
    DE                          ; Remove previous group vol. 
    [group_vol]              
    DE                          ; Remove previous odd group vol.
    [group_vol]_odd
    DE                          ; Remove previous even group vol.
    [group_vol]_even
 ENDIF

 ; Find % of images showing excessive change in angular displacement
 [toobig]=100*[num-big]/[num-imgs]  ; % of images with excessive change in ang. displacement

 VM
 echo "Iteration: {**[iter]} Group: {***[grp]}   Excessive changes: {***[toobig]}%"

 [avg-ccrot] = [sum-ccrot]/[num-imgs]                            ; Average CCROT 
 [avg-degred]=0.0                                                ; Average CCROT degredation
 IF ([num-degred] .GT. 0)[avg-degred]=[sum-degred] /[num-degred] ; Average CCROT degredation
 [avg-impr]=0.0                                                  ; Average CCROT improvement
 IF ([num-impr] .GT. 0) [avg-impr]=[sum-impr]/[num-impr]         ; Average CCROT improvement
 [per-degred] = 100*[num-degred]/[num-imgs]                      ; Percent images with CCROT degredation
 [avg-diff]=[sum-diff]/[num-imgs]                                ; Average angular difference

 VM
 echo " CCROT degred.: {***[per-degred]}%  Avg degred.: {%F8.2%[avg-degred]}  Avg improve: {%F8.2%[avg-impr]}"

 SD / %Large angles, Avg. Ang-diff,  Avg. CCROT, % degred.,  Avg degred.,  Avg improv.
 [next_group_align]                   ; Next align. doc file  (output)

 SD -2, [toobig],[avg-diff],[avg-ccrot],[per-degred],[avg-degred],[avg-impr]
 [next_group_align]                   ; Next align. doc file  (output)

 SD E                                 ; Close doc file  
 [next_group_align]                   ; Next align. doc file
 
 MY FL
RE