; Applies alignment parameters
;
; SOURCE:               Adapted from apshgrp.spi          Jan 11, 2009
;
;----------------- Input files --------------------------------------------

FR G
[defgrps]../Reconstruction/sel_group                  ; List of groups (group selection doc file)

FR G
[unaligned_images]../Alignment/data{***[grp]}@        ; Unaligned particles stacks

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

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

FR G
[aligned_images]../Alignment/dala01_{***[grp]}@       ; Rotated, shifted (aligned) particles

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

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

UD N [numgrps]                        ; Get number of groups 
[defgrps]                             ; Groups selection file           (input)

MD                 
SET MP                             ; Use all processors (needed here for secondary runs)
0

VM
echo "Realigning particles" ; date

DO LB2 [numgrp]=1,[numgrps]        ; Loop over all group(s)
    UD [numgrp],[grp]            ; Get current group number 
    [defgrps]                    ; Group selection file                  (input)
    
    FI X [numpart]                  ; Get number of particles 
    [unaligned_images]              ; Unaligned particles name template      (input)
    (26)                            ; Max. image number in stack      
    
    VM
    echo "Working on group #{****[grp]} with {******[numpart]} particles"  
    
    ; Apply alignment parameters to unaligned particles
    ; Order in doc file: PHI,THETA,PSI, REF#,IMG#,  ROT,SX,SY
    RT SQ                        ; Rotate & shift whole stack operation
    [unaligned_images]           ; Unaligned particles stack             (input) 
    [aligned_images]             ; Aligned particles stack               (output)
    (6,0,7,8)                    ; Reg. numbers for angle, scale,& shifts
    [align_parameters]           ; Alignment angles doc. file            (input)
    
    MY FL                           ; Flush results file
LB2                                ; End loop for multiple groups

VM
echo "Done" ; date

EN D

;