; Controls serial alignment
 ;
 ; SOURCE:  spider/docs/techs/recon1/Procs/align.spi   
 ;
 ; PURPOSE: Multi-reference alignment of an image series. 
 ;          Experimental images are aligned with reference projections via
 ;          shifts (translations) and rotations.  
 ;
 ; USAGE:   clean ; ./spider spi/dat @align 0
 ;
 ; I/O Registers & files are set in: recon-settings.spi
 ;
 ; VARIABLE REGISTERS:
 ;   [r2]                  Alignment radius of object 
 ;   [alignsh]             Translation shift allowed is +-[alignsh]
 ;   [incore-yn]           Incore images used
 ;
 ; 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)
 ;   [unaligned_images]    [win_dir]/data_***         CTF corrected, unaligned image stacks  (one/group)        (one/group)
 ;   [ref_projs]           [rec_dir]/projs_00         Reference projections          (one)
 ;
 ; OUTPUT FILES: ('[rec_dir] denotes output directory and '%' denotes subset)
 ;   [next_group_align_s]  [rec_dir]/align_01_***_s%  Alignment parameter doc files        (two/group)
 ;   [aligned_images]      [rec_dir]/dala_***@        Rotationally aligned particle stacks (two/group)
 ;
 ; PROCEDURES CALLED:
 ;    recon-settings        recon-settings.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!!
   10 

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

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

 [iter]      = 0
 [next-iter] = 1

 GLO [r2]        = [r2]
 GLO [alignsh]   = [alignsh]
 GLO [incore-yn] = 1

 ; Processes experimental images in series by groups of images.
 ; Find alignment parameters ('AP SHC') which best align experimental images with 
 ; optimal projection image. 
 ; Rotate ('RT SF') original particle images using these rotational alignment parameters.

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

   @align-loop
     [grp]

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

 SYS
   echo -en '  Alignment completed' ; date '+ TIME: %x %X' ; echo

 EN