; Reconstructs a volume by back-projection of an image series
 ;
 ; SOURCE: spider/docs/techs/recon1/Procs/pub-recon.spi
 ;
 ;         New                                 ArDean Leith  Nov 2000
 ;         [rn] for endmerge                   ArDean Leith  May 2005
 ;         [] from publish                     ArDean Leith  Feb 2006
 ;         More stacks                         ArDean Leith  Dec 2006
 ;         COG centering                       ArDean Leith  Sep 2010
 ;         Dala files removed                  ArDean Leith  Jan 2012
 ;         pixsiz, resol headers,...           ArDean Leith  Aug 2012
 ;         For CTF corrected images            ArDean Leith  Oct 2013
 ;         For gold standard reconstruction    ArDean Leith  May 2014
 ;         Inlined params, changed dirs        ArDean Leith  Mar 2016
 ;
 ; PURPOSE: Runs on master node to control parallel reconstruction. 
 ;          Reconstructs a volume by back-projection of an image series.
 ;
 ; Parameters & files are set in: recon-settings.spi
 ;
 ; INPUT FILES: ([win_dir] denotes input directory, '***' denotes group,  '%' denotes subset)
 ;   [sel_group]        [win_dir]/sel_group       Group selection file           (one)
 ;   [sel_parts]        [win_dir]/sel_part_***    Group particle selection files (one/group)
 ;   [group_align]      [win_dir]/align_01_***    Alignment parameter files      (one/group)
 ;   [unaligned_images] [win_dir]/data_***        Unaligned image stacks         (one/group)
 ;
 ; PROCEDURES CALLED:
 ;   recon-settings       recon-settings.spi
 ;   pub_submit               pub_submit.spi
 ;   .. qsub/publish                qsub.pbs
 ;   .... recon-settings  recon-settings.spi            
 ;   .... recon-loop          recon-loop          
 ;   merge-fsc-filt       merge-fsc-filt.spi
 ;
 ; -------------------------------- END BATCH HEADER ----------------------------
 
 MD
   TR OFF                    ; Loop info turned off
 MD
   VB OFF                    ; File info turned off
 MD
   SET MP                    ; Use only a few processors if using master node!!
   4 

 ; Get reconstruction parameters & file names
 [steptype]   = 1            ; Reconstruction step
 @recon-settings([steptype],[num-grps],[pixsiz],[ang-step],[r2],[alignsh],[prj-radius],[winsiz],[incore-yn],[bp-type],[qsub])
 [ampenhance] = 0
 [sphdecon]   = 0
 [iter]       = 0
 [gold-std]   = 1

 SYS
   echo '  Projection radius: {%I0%[prj-radius]}' ; echo

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

 ; Backproject ('BP 3F') aligned experimental images in parallel by groups of images
 ; to create two subset volumes. Wait for all parallel groups to finish.

 [task]   = 0                                         ; Unused
 [script] = './spider $PRJEXT/$DATEXT @recon-loop '   ; Script starts recon-loop 
 @pub-submit([iter],[num-grps],[task],[qsub])
   [script]                                           ; Runs recon-loop


 ; 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 current time 
   echo -n '  Reconstruction finished' ; date '+ TIME: %x  %X' ; echo  

 EN
 ;