; Controls and synchronizes refinement
 ;
 ; SOURCE: spider/docs/techs/recon1/Procs/pub-refine.spi
 ;
 ;         New                              ArDean Leith  Nov 2000
 ;         [rn] for endmerge                ArDean Leith  May 2005
 ;         [] from publish                  ArDean Leith  Feb 2006
 ;         Ampenhance                       ArDean Leith  May 2006
 ;         More stacks                      ArDean Leith  Dec 2006
 ;         Existing iter_refangs bug        ArDean Leith  Aug 2010
 ;         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
 ;
 ; PURPOSE: Runs on master node to control PubSub refinement.  Master procedure for
 ;          multiple iteration refinement on parallel cluster using: PubSub to control
 ;          distribution of parallel jobs. Each data group uses one parallel process.
 ;
 ; I/O Registers & files are set in: refine-settings.spi
 ;
 ; INPUT REGISTERS:
 ;   [maxspfreq]              Maximum spatial freq (1/A) (used in prepare)
 ;   [r2]                     Radius of object 
 ;   [alignsh]                Translation shift allowed is +-[alignsh]
 ;   [iter-end]               Ending iteration
 ;
 ; 
 ; INPUT FILES: ('***' denotes group here)
 ;   [params]                ../params                                Params file           (one)
 ;   [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-prepare                refine-prepare.spi
 ;    refine-refangles            refine-refangles.spi
 ;
 ;    publish                             publish.perl
 ;    ... pub-refine-start        pub-refine-start          
 ;    ....... refine-settings      refine-settings.spi            
 ;    ....... refine-loop              refine-loop.spi            
 ;    ....... refine-smangloop    refine-smangloop.spi            
 ;    ....... refine-bp                  refine-bp.spi            
 ;
 ;    refine-merge                    refine-merge.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 on master node!!
  1 

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

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

 [iter] = 2

 @refine-merge([pixsiz],[iter],[ampenhance],[r2])

 SYS
   echo "  Iteration: {%I0%[iter]} Finished" ; echo

 EN
 ;