; Controls serial reconstruction refinement
 ;
 ; SOURCE: spider/docs/techs/recon1/Procs/refine.spi    
 ;         New                               ArDean Leith Nov 2000
 ;         Rewritten                         ArDean Leith Mar 2004
 ;         [] variables                      ArDean Leith Dec 2005
 ;         Ampenhance                        ArDean Leith May 2006
 ;         More stacks                       ArDean Leith Dec 2006
 ;         Small angles limits               ArDean Leith Oct 2010
 ;         pixsiz, resol headers,r2,...      ArDean Leith Aug 2012
 ;         For CTF corrected images          ArDean Leith Oct 2013
 ;         For gold standard reconstruction  ArDean Leith May 2014
 ;
 ; PURPOSE: Controls serial reconstruction refinement.  This is master procedure for
 ;          serial refinement over several iterations. Data may be in one or more groups.
 ;
 ; USAGE:   clean ; ./spider spi/dat @refine
 ;
 ; I/O Registers and files are set in: refine-settings.spi
 ;
 ; VARIABLE REGISTERS:
 ;   [pixsiz]                Pixel size 
 ;   [r2]                    Radius of object
 ;   [alignsh]               Shift allowed is +- this value
 ;   [prj-radius]            Radius of object for projection
 ;   [iter1]                 Starting iteration
 ;   [iter-end]              Ending   iteration
 ;   [smallang-yn]           Small angle refinement flag (1 == true) 
 ;
 ; INPUT FILES: ('***' denotes group here)
 ;   [vol_orig]              ../Reconstruction/vol_01                 Initial volume file   (one)
 ;   [sel_group_orig]        ../Reconstruction/input/bp_sel_group     Group selection file  (one)
 ;   [sel_parts_orig]        ../Reconstruction/input/bp_sel_part_***  Group particle selection files (one/group)
 ;   [unaligned_images_orig] ../Reconstruction/input/bp_data_ctfd_*** Unaligned image stacks         (one/group)
 ;
 ; PROCEDURES CALLED:
 ;    refine-settings             refine-settings.spi
 ;    refine-show-r2               refine-show-r2.spi
 ;    refine-setrefangles     refine-setrefangles.spi            
 ;    pub-prjrefs                     pub-prjrefs.spi            
 ;    refine-loop                     refine-loop.spi            
 ;    refine-smangloop           refine-smangloop.spi            
 ;    refine-bp                         refine-bp.spi            
 ;    merge-fsc-filt               merge-fsc-filt.spi
 ;    .. sphdecon                        sphdecon.spi            
 ;    .. enhance (optional)               enhance.spi
 ;
 ; --------------------------------- END BATCH HEADER --------------------------

 MD
   TR OFF                    ; Loop info turned off
 MD
   VB OFF                    ; File info turned off
 MD
   SET MP                    ; Use only one or two processors if using master node!!
   0 

 ; Input initial parameters & file names but not angular steps
 @refine-settings([pixsiz],[r2],[alignsh],[prj-radius],[iter1],[iter-end],[sphdecon],[smallang-yn],[qsub],[incore-yn],[gold-std],[bp-type])

 SYS
   echo -e "  Alignment shift:   {%I0%[alignsh]}   Alignment radius: {%I0%[r2]}"
 SYS
   echo    "  Projection radius: {%I0%[prj-radius]}" ; echo

 ; Show alignment and projection radii on center slice of reference volume
 !@refine-show-r2([r2],[alignsh],[prj-radius])
 !  final/vol_01_s1            ; Reference volume

 SD /     ITERATION     MASKED-RES    RESOLUTION
   [iter_resol]               ; Resolution doc file              (output)
 SD E     
   [iter_resol]               ; Resolution doc file              (finished)

 UD N [num-grps]              ; Find number of groups
   [sel_group]                ; Group selection file      (input)

 IF ( [smallang-yn] == 1 ) THEN
   ; List desired offset angles for reference projections in a doc file.
   @refine-setrefangles([iter],[smallang-yn],[ampenhance],[ang-step],[ang-limit],[num-angs])      
 ENDIF

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

   IF ( [smallang-yn] == 0 ) THEN
     ; List desired angles ('VO EA') for reference projections in a doc file.
     @refine-setrefangles([iter],[smallang-yn],[ampenhance],[ang-step],[ang-limit],[num-angs])      

     ; Create reference projection stacks ('PJ 3F') from both subset reference volumes. 
     @pub-prjrefs([iter],1,[prj-radius],[qsub])

   ENDIF

   DO [s] = 1,2               ; Loop over resolution subsets ------------------

     SYS
       echo  " Iteration: {%I0%[iter]}  Projecting: [vol_s]   From: {%I0%[num-ang]} angles" 

     ; Create stacks holding angular reference projections from both current reference volumes.
     PJ 3F                    ; Projection operation
       [vol_s]                ; Vol from previous iteration      (input) 
       [prj-radius]           ; Radius of object
       1-[num-ang]            ; Number of ref. angles used    
       [iter_refangs]         ; Ref. angles doc file             (input)
       [ref_projs_s]@*****    ; Template for ref. projections    (output) 
   ENDDO

   ; Process all groups serially one-by-one

   DO [grp] = 1,[num-grps]    ; Loop over all  groups  -----------------

      SYS
        echo ; echo -n " Iteration: {%I0%[iter]}  Refining group: {%I4%[grp]}   " ; date  '+ TIME: %x  %X' ; echo

      MY FL                   ; Flush results file

      IF ( [smallang-yn] == 0 ) THEN
        ; For regular angle alignment
        @refine-loop([ang-step],[ang-limit],[r2],[alignsh],[prj-radius],[iter],[grp],[pixsiz],[incore-yn],[bp-type],[iter-end])
      ELSE
        ; For small angle alignment around a determined position
        @refine-smangloop([ang-step],[r2],[alignsh],[prj-radius],[iter],[grp]],[pixsiz],[bp-type],[iter-end])
      ENDIF
     
      MY FL                   ; Flush results file

   ENDDO                      ; End of: Loop over all groups -------------------

   UD E                       ; Finished with incore doc file

   ; Merge ('AS S') the group volumes into two subset output volumes,
   ; find resolution ('FSC') by comparing FSC of the two subsets,
   ; filter ('FD C')  the combined volumes using FSC curve to limit resolution, 
   ; merge ('AS S') the two subset output volumes into single overall output volume.

   @merge-fsc-filt([pixsiz],[iter],[ampenhance],[r2],[sphdecon],[gold-std],[qsub])

   SYS
     echo " Iteration: {%I0%[iter]} Finished" ; echo
   MY FL                      ; Flush results
 ENDDO                        ; End of loop over all iterations ----------------------------

 SYS                          ; Echo current time 
   echo -n "  Refinement finished after iteration: {**[iter]}   " ; date '+ TIME: %x  %X' ; echo  

 EN
 ;