; Controls and synchronizes alignment
; apsh_pub.bat                        Mar 2003 ArDean Leith   
;                      From apmq_pub  Feb 2005 ArDean Leith 
;  
; Multireference alignment of an image series. 
; Sample images are aligned with reference projections via shifts (translations) 
; and rotations.  For project with multiple micrographs.  The work
; is partitioned into sets of images.  The number of images in a set can be 
; less than the number used in a whole micrograph.
;
; MASTER COPY: /net/bali/usr1/spider/docs/techs/recon/programs/
;
; INSTRUCTIONS:  apsh_pub.html
;
; I/O PARAMETERS AND FILES ARE SET HERE:
;
;------------ Edit next 5 Alignment parameters --------------------- 
; -------------- DO NOT COPY FROM WEB BROWSER     ---------------------

x16 = 25        ; Number of images / partition set

x31 = 5 	; Translation search range (must be divisible by step size)
x32 = 1		; Translation step size

x21 = 5		; Rotation first ring, only radii within specified rings will be analyzed
x22 = 30	; Rotation last ring, if pixel size is different than 4.78, change the rings

; ----------------- Input files,  May have to edit these names ---------

FR G
[micrograph_selection_file]input/selmcg          ; Micrograph selection doc. file

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

FR G
[ref_images_selection]input/selref               ; Reference images selection doc. file

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

FR G
[sample_images_template]input/data{***x55}@****  ; Sample image name template

FR G
[sample_images_selection]input/selimg            ; Sample images selection doc. file


; ----------------- Output file,  May want to edit this name --------

FR G
[align_parameters]output/jnk_apsh               ; Alignment parameters doc. file stem
;

; ----------------- Temporary Output file ----------------------------
FR G
[sync_doc]output/jnk_sync                        ; Sync doc. file stem

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

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

IF (x14 .NE. 1) THEN  ; Primary PubSub run creates parallel processes 
   ; Get number of micrographs from micrograph selection file
   ; If you do not have a "micrograph selection file" you can
   ;   create it using: 'DOC CREATE' and inputting the number of micrographs

   UD N,x20
   [micrograph_selection_file]

   ;  Loop over all entries in micrograph selection file
   x45=0                               ; Set number
   DO LB1 x11=1,x20

       ; Get micrograph file-number from micrograph selection file
       UD x11,x55                      ; Current micrograph number = x55
       [micrograph_selection_file]

       ; Get image numbers in this micrograph from image selection file
       UD x55,x43                      ; 
       [sample_images_selection]

       x40 = 1                         ; Starting image        
       DO LB2 x42=1,1000000            ; Loop over all image sets
          x41 = x40 + x16 - 1          ; Final image for this image set
          IF (x41.GT.x43) x41=x43      ; Final image for this image set
          x45 = x45 + 1                ; Set number

          ; Use PubSub to create parallel 'AP SH' process for each image set   
          VM
          publish './spider bat/$DATEXT @apsh_pub {***x45} x14=1 x55={***x55} x45={****x45} x40={****x40} x41={****x41}'

          IF (x41.GE.x43) GOTO LB99    ; Final image set for this micrograph
          x40 = x41 + 1                ; Increment for next starting image

          MY FL                        ; Flush results file
          
       LB2
       LB99
    LB1

ELSE                                       ; Secondary PubSub run (does actual alignments)
  
    MD  
    SET MP                                 ; Use all processors (needed here for secondary runs)
    0

    DE                                     ; Delete existing alignment parameter output doc. files.
    [align_parameters]_{***x55}_{****x45}

    AP SH                                  ; Align sample images using Multireference Method 
    [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]                    ; Reference images angles doc. file          (input)
    [sample_images_template]               ; Sample images name template                (input)
    x40-x41                                ; Sample image numbers                      
    *                                      ; No sample images alignment angles doc. file  
    (0)                                    ; No resturiction on angles
    (0)                                    ; Check mirrored projections
    [align_parameters]_{***x55}_{****x45}  ; Alignment angles doc. file                 (output)

    SD 1,x40,x41                           ; Sync doc. file created when finished
    [sync_doc]_{****x45}
    SD E
    [sync_doc]_{****x45}

    EN
ENDIF

LB99
;  Wait till all angular output files complete
x29=1
@wait_pub(x29,x45)

;  Loop over all entries in micrograph selection file, combining the angular output files
DO LB3 x11=1,x20
   DE
   [align_parameters]_{***x11}           ; Alignment angles doc. file                 (output)

   VM
   cat [align_parameters]_{***x11}_*  > [align_parameters]_{***x11}_tmp.$DATEXT

   DOC REN   
   [align_parameters]_{***x11}_tmp
   [align_parameters]_{***x11}

   VM
   \rm -f [align_parameters]_{***x11}_* [sync_doc]* 

LB3

EN                                         ; This SPIDER ends now


;