; Computes alignment parameters for sample images
;
; SOURCE: apsh.spi                 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 SINGLE OR MULTIPLE GROUPS.
;
; MASTER COPY: /net/bali/usr1/spider/docs/techs/recon/newprogs/
;
; INSTRUCTIONS:  pub_apsh.html
;
;------------ Edit next 7 Input parameters ------------------------------ 

x12=0           ; If >0 use PubSub

x13=2           ; If >1 do multiple groups

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

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

x21 = 5		; First rotation alignment ring, only radii > this will be analyzed
x22 = 0         ; Last rotation ring (If zero, compute from object diameter)

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

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

FR G                                     
[order]../Power_Spectra/order_defgrps      ; List of groups (group selection doc file)
                                           ; If file missing, create using: 'DOC CREATE' 

IF (x13.GT.1) THEN
   FR G
   [ref_images_template]df{***x77}/prj**** ; Reference image name template  
ELSE
   FR G
   [ref_images_template]prj****            ; Reference image name template  
ENDIF

FR G
[ref_images_selection]projlist             ; Reference images selection doc. file

FR G
[ref_images_angles]refangles               ; Reference images angles doc. file

FR G
[sample_images_template]../Particles/win/ser****** ; Sample image name template

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

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

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

; ----------------- END BATCH HEADER ------------------------------------------- 

IF (x77 .GT. 0) x12 = 0         ; Secondary PubSub run
IF (x13 .LE. 1) x77 = 1         ; Single group only use default output file name

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

IF (x12 .EQ. 0) THEN            ; Normal run (Not a primary PubSub run)
  
   IF (x22.LE.0) THEN           ; Compute alignment radius from object size  
      UD 5,x39                  ; Get pixel size (A) from [parameters] file
      [parameters]
 
      UD 17,x52                 ; Get window size (pixels) from [parameters] 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
   ENDIF


   IF (x77 .LE. 0) THEN         ; Group not on command line, get it from selection file        

      UD N,x70                  ; Get number of groups 
      [order]                   ; Groups selection file  (input)
x70=2 ;;;;;;;;  
      DO LB2 x11=1,x70          ; Loop over all groups

         UD x11,x77             ; Get current group number 
         [order]                ; Group selection file   (input)
   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) 
   *                            ; No sample images align. doc file        
   (0)                          ; NO restriction on Angular proj search 
   (1)                          ; Check mirrored positions
   [align_parameters]           ; Alignment angles doc. file             (output)

   MY FL                        ; Flush results file

   LB2                          ; End loop for multiple groups
    
ELSE                            ; Primary PubSub run

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

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

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

    LB2
ENDIF

EN                              

;