; Controls refinement of a reconstruction
;
; SOURCE: spider/docs/techs/recon/newprogs/refine.pam    
;         New                              ArDean Leith Nov 2000
;         Rewritten                        ArDean Leith Mar 2004
;         [] variables                     ArDean Leith Dec 2005
;         Ampenhance fixed                 ArDean Leith May 2006
;         More stacks                      ArDean Leith Dec 2006
;         [temp_local_dir] def.            ArDean Leith Oct 2009
;         File locations changed           ArDean Leith Oct 2009
;         Small angles limits              ArDean Leith Oct 2010
;
; PURPOSE: Controls serial refinement 
;
; I/O Registers and files are set in: refine settings.pam
;
; INPUT REGISTERS:
;   [sp_maxspfreq]           Maximum spatial freq. (used in prepare)
;   [radius]                 Radius of structure
;   [alignsh]                Shift allowed is +- this value
;   [prj-radius]             Radius of structure for projection
;   [iter1]                  Starting iteration
;   [iter-end]               Ending   iteration
;   [sp_lambda]              Lambda                (used in prepare)
;   [small-ang]              Small angle refinement flag (1 is true) 
;   [converg]                Convergence criterion percentage
;
; INPUT FILES:
;   [params]                ../params                             Input params file  
;   [vol_orig]              ../Reconstruction/vol01               Input volume file  
;   [sel_group_orig]        ../Reconstruction/sel_group_cclim     Group selection file
;   [sel_particles_orig]    ../Reconstruction/sel_particles_***   Particle selection files 
;   [group_align_orig]      ../Alignment/align_01_***             Alignment parameter files 
;   [unaligned_images]      ../Alignment/data***                  Original unaligned images stack
;
; PROCEDURES CALLED:
;    refine_settings          refine_settings.pam
;    prepare                  prepare.pam
;
;    grploop                  grploop.pam    OR           
;    smangloop                smangloop.pam            
;
;    mergegroups              mergegroups.pam
;    ... enhance (optional)   enhance.pam
;    ... saveresp             saveresp.pam
;    endmerge                 endmerge.pam
; 
;    endrefine                endrefine.pam  
;    ....softmask             softmask.pam            
;    ... saveresp             saveresp.pam
;
; ---------------------------------------------------------------------

MD
TR OFF                       ; Loop info turned off
MD
VB OFF                       ; File info turned off
MD
SET MP                       ; Use all available OMP processors
0

; Input initial parameters & file names
@refine_settings([sp_maxspfreq],[radius],[alignsh],[prj-radius],[iter1],[iter-end],[sp_lambda],[small-ang],[sp_winsize],[converg])

; Redefine [temp_local_dir] location to current work directory for non-PubSub
GLO [temp_local_dir] = '[temp_work_dir]'

IF ([iter1].LE.1) THEN
   ; Prepare input files (only needs to be done once) 
   @prepare([sp_maxspfreq],[sp_lambda],[iter1])
ENDIF

MY FL

UD N [num-grps]            ; Get number of defocus groups
[sel_group]

DO [iter]=[iter1],[iter-end] ; Loop over all iterations ----------------------------------

   RR S [ampenhance]       ; Amplitude enhancement flag (varies with iteration)
   ([amp-enhance-flags])   ; Global string variable
   [iter]

   FT                      ; Fourier on current volume 
   [current_vol]           ; Volume from previous iter. or reconstruction  (input) 
   [iter_vft]              ; Fourier of volume (used by all groups on this iter. in grploop) 

   DE
   [iter_refangs]          ; Reference angles doc file         (removed)

   ; Create reference angle doc file for this iteration 

   IF([small-ang].EQ.0) THEN  

      ; For normal angle refinement
      RR S [ang-step]
      ([ang-steps])        ; Angular step for projection angle  (varies with iteration)
      [iter]

      VO EA [num-angles]   ; Sets [num-angles] to number of reference projections
      [ang-step]           ; Theta angular step          (varies with iteration)
      (0,90.0)             ; Theta range, 90 is for use with 'Check Mirrored Positons:1'
      (0,359.9)            ; Phi range
      [iter_refangs]       ; Reference angles doc file       (output)

      RR S [ang-limit]     ; Restriction on angular search      (varies with iteration)
      ([ang-limits])
      [iter]
    ELSE                    ; Small angle refinement

      ; For Small angle refinement
      VO EA [num-angles]   ; Sets [num-angles] to number of reference projections
      [ang-step-sm]        ; Theta angular step     
      (0,[theta-range])    ; Theta range 
      (0,359.9)            ; Phi range
      [iter_refangs]       ; Reference angles doc file       (output)
 
   ENDIF

   ; Process all defocus groups one by one
   [num-big] = 0           ; Number of large proj. angle displacements
   DO [i]=1,[num-grps]
      UD S [i],[grp]       ; Get defocus group number from unsorted list  
      [sel_group]          ; Group selection file            (input)

      VM
      echo -n " Processing group: {***[grp]}   " ; date  '+ TIME: %x  %X'
      MY FL                ; Flush results file
 
      ; grploop/smangloop returns [stop] 
      IF([small-ang].EQ.0)THEN  ; Runs main refinement grploop tasks
         @grploop([ang-step],[ang-limit],[radius],[alignsh],[prj-radius],[iter],[grp],[stop],[sp_maxspfreq])
      ELSE
         @smangloop([radius],[alignsh],[prj-radius],[iter],[grp],[stop])
      ENDIF

      ; Check size of projection angular changes
      ; [stop] returns % of images whose proj. angle moved by >1.5*[ang-step]
      IF([stop].GT.[converg])  [num-big]=[num-big]+1  ; Excessive ang. change for this group  

      MY FL                  ; Flush results file
   ENDDO
   UD E                      ; Finished with incore doc file

   VM
   echo " Iteration: {**[iter]} Excessive proj. ang. changes in: {***[num-big]} groups"
 
   ; If projection angular changes small in all groups, can stop now
   IF([num-big].EQ.0) EXIT  

   ; Consolidate data for CTF corrections
   @mergegroups([sp_maxspfreq],[iter],[ampenhance])

   DE                       ; Delete vft file.
   [iter_vft]               ; Current iteration Fourier vol      (removed)

   MY FL                    ; Flush results file
ENDDO

VM
echo ' ' ; echo -n " Alignment halting after iteration: {**[iter]}  " ; date '+ TIME: %x  %X' ; echo ' '

DO [i]=1,[num-grps]     
  UD IC [i], [grp]         ; Get defocus group number from list
  [sel_group]

  @endmerge([prj-radius],[grp],[iter])
ENDDO

UD ICE                     ; End doc file 'UD IC' use
[sel_group]

; Prepare final resolution files
@endrefine([radius],[sp_maxspfreq],[iter])    

VM
echo " " ; echo -n  " SUCCESS, FINISHED REFINEMENT    " ; date '+ TIME: %x  %X'

EN
;