([iter]) ; Make dala files
;
; SOURCE:      newdala.pam     Nov 2004 ArDean Leith
;              []              Dec 2005 ArDean Leith
;              UD E            Feb 2006 ArDean Leith
;               
; MASTER COPY: /net/bali/usr1/spider/docs/techs/recon/newprogs/
; 
; PURPOSE:     Make dala files from unaligned images & alignment angles

; INPUT REGISTERS:
;    [iter]          Current iteration
;
; INITIAL INPUT FILES NEEDED:
;   input/align_{**iter}_{***group} [initial_group_align} Alignment parameters 
;   input/data{**iter}_{***group}@  [unaligned_images]    Aligned stacked image files
;
; OUTPUT FILES:
;   final/align_{**iter}_{***group} [group_align}         Alignment parameters 
;   work/dala{**iter}_{***group}@   [aligned_images]      Aligned stacked image files
;


   UD N [num-grp]                ; Get number of defocus groups
   [sorted_order_select]

   ; Process all defocus groups one by one
   DO LB3 i=1,[num-grp]
      UD S i,[grp],[img-num]   ; Get defocus group number & img numbers     
      [sorted_order_select]

      VM
      echo " Preparing alignment for group: {***[grp]} with: {******[img-num]} images"
      MY FL   ; Flush results file
 
      [mirrored] = 0

      DE
      [group_align]              ; Doc file created here

      DO LB5 j=1,[img-num]           ; Loop over all sample images

         ; Retrieve current alignment file data 
         ;        PSI,  THE,  PHI,    MIR-REF#,EXP#,  ANG, SX,SY,    NPROJ,DIFF,CCROT
         UD IC j, [psi],[the],[phi], [mir-ref],[exp], [inp],[sx],[sy], [nproj],[diff],[ccrot]
         [initial_group_align]        ; Doc file created by 'AP REF/SH'

         ; Find non-mirrored projection parameters
         IF([mir-ref].LE.0) THEN            ; This was mirrored
             [psi] = -[psi]
             [the] = 180+[the]
             [mir-ref] = -[mir-ref]

             [mirrored]=[mirrored] + 1
         ENDIF

         RT SQ                        ; Rotate & shift
         [unaligned_images]{*****j}   ; Unaligned original images (stack)
         [aligned_images]{*****j}     ; Current aligned images (output)
         [inp]                          ; Inplane rotation angle
         [sx],[sy]                      ; Shifts

         SD  j, [psi],[the],[phi], [mir-ref],[exp], [inp],[sx],[sy], [nproj],[diff],[ccrot]
         [group_align]                ; Doc file created here

         ;;VM
         ;;echo prepared: [aligned_images]{*****j}

     LB5

     VM
     echo " Mirrored: {****[mirrored]} from: {****[img-num]}"

     UD ICE
     [initial_group_align]           ; Doc file created by 'AP REF/SH'

     SD E
     [group_align]                   ; Doc file created here

     UD E                            ; close: [sorted_order_select]
   LB3

   RE