; Controls and synchronizes alignment
; 
; SOURCE pub_apsh.bat                 ArDean Leith    Nov. 2004
;
; PURPOSE: Multireference alignment of an image series. 
;          Sample images are aligned with reference projections via
;          shifts (translations) and rotations.  
;
;          FOR PROJECTS WITH MULTIPLE DEFOCUS GROUPs. IF YOU HAVE ONLY
;          SINGLE GROUP USE: 
;
; MASTER COPY: /net/bali/usr1/spider/docs/techs/recon/programs/
;
; INSTRUCTIONS:  pub_apsh.html
;
; I/O PARAMETERS AND FILES ARE SET HERE:
;
;------------ Edit next 4 Alignment parameters --------------------- 

x20 = 366.47    ; Diameter of object (Angstroms) used in alignment search.
                ;   (EDIT as needed!)
                ;   Diameter is used to find radius of last alignment ring.

x31 = 8 	; Translation search range (must be divisible by step size)
x32 = 2		; Translation step size

x21 = 5		; First rotation alignment ring, only radii > this will be analyzed

; ----------------- Input files --------------------------------------------

FR G
[params]../params                                  ; Parameter setting file

FR G                                     
[order]../Power_Spectra/order_defgrps              ; Defocus groups selection file

FR G
[ref_images_template]prj/prj****                   ; Reference image name template

FR G
[ref_images_selection]projlist                     ; List of reference image numbers 
              
FR G
[ref_images_angles]refangles                       ; Ref. angles doc file 

FR G
[sample_images_template]../Particles/win/ser****** ; Particle images

FR G
[sample_images_selection]part_by_group_{***x77}    ; Particle selection doc file

; ----------------- Output files  -----------------------------------------

FR G
[align_parameters]align_{***x77}                   ; Alignment parameters doc. file 

;
; -------------------------------------------------------------------------

MD
TR OFF                           ; Decreases results file output
MD
VB OFF                           ; Decreases results file output

IF (x14 .NE. 1) THEN             ; Primary PubSub run

   UD N,x70                      ; Get number of defocus groups 
   [order]                       ; Defocus groups selection file  (input)

   DO LB1 x11=1,x70              ;  Loop over all groups 

      UD x11,x77                 ; Get current defocus group number 
      [order]                    ; Defocus group selection file   (input)
   
      VM                         ; Use PubSub to create parallel processes
      publish './spider bat/$DATEXT @pub_apsh {***x77} x14=1 x77={***x77}'

      MY FL                      ; Flush results file
   LB1

ELSE                             ; Secondary PubSub run
    
    UD 5,x39                     ; Get pixel size (A) from [params] file
    [parameters]
 
    UD 17,x52                    ; Get window size (pixels) from [params] file
    [parameters]

    x22 = INT(x20/(2.0*x39))     ; Compute object radius (pixels) for last alignment ring

    x46 = x22 + x31              ; Compute last ring + translation range
    x45 = INT(x52/2) - 1         ; Compute max. radius of object within window

    IF (x46.GE.x45) THEN
       x22 = x45 - x31 - 1       ; Reduce radius of last alignment ring to fit window
    ENDIF

    DE                           ; Delete existing alignment parameter output doc. files.
    [align_parameters]



    AP SH                        ; Align sample images using multiple references  
    [ref_images_template]        ; Reference image name template          (input)
    [ref_images_selection]       ; Reference images selection doc. file   (input)
    x31,x32                      ; Search range, step size  
    x21,x22                      ; First and last ring      
    [ref_images_angles]          ; Ref. angles doc file                   (input)
    [sample_images_template]     ; Sample images  name template           (input)
    [sample_images_selection]    ; Sample images selection doc. file      (input) 
    *                            ; Sample images align. doc file          (input)
    (0)                          ; NO restriction on Angular proj search 
    (1)                          ; Check mirrored positions
    [align_parameters]           ; Alignment angles doc. file             (output)      

ENDIF

EN                               ; This SPIDER ends now

;