; Normalizes stacks and creates star file
;
; PURPOSE: Normalizes groups of spider stacks and creates 'star' file for Relion.
;  
; SOURCE:  norm_n_star.spi  
; 
; USAGE:   spider spi/dat @norm_n_star

 [id] = '1'                              ; New stack id letter

 ; -------- Adjust these parameters for your project ----------- EDIT -----

 [DefAng]  =  0        ; Defocus angle from 'params.spi' file (degrees)  
 
 [Volt]    = 200       ; Electron voltage from 'params.spi' file (KV)  

 [ACR]     = 0.1       ; Amplitude Contrast Ratio from 'params.spi' file   

 [SphAb]   =  2        ; Spherical Aberration from 'params.spi' file  

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

 [sel_grp]   = 'sel_group'                           ; Group selection file

 [sel_part]  = 'Alignment/sel_particles_{***[grp]}'  ; Particle selection files (one/group) 

 [part]      = 'Alignment/data{***[grp]}'            ; Particle stacks (one/group)

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

 [id]        = '1'                           ; New stack id letter

 [starfile]  = 'relion_[id]_data.star'       ; New star files (one) 

 [norm_part] = 'input/data_norm_{***[grp]}'  ; New particle stacks (one / group)

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

 [select]    = '{*****[n]}@[norm_part]'

 MD
   VB OFF

 SYS
   \rm [starfile]
 SYS 
   echo data_                    >> [starfile]
 SYS 
   echo loop_                    >> [starfile]
 SYS 
   echo _rlnImageName            >> [starfile]
 SYS 
   echo _rlnDefocusU             >> [starfile]
 SYS 
   echo _rlnDefocusV             >> [starfile]
 SYS 
   echo _rlnDefocusAngle         >> [starfile]
 SYS 
   echo _rlnVoltage              >> [starfile]
 SYS 
   echo _rlnAmplitudeContrast    >> [starfile]
 SYS 
   echo _rlnSphericalAberration  >> [starfile]


 [ngrps] = 0
 [ntot]  = 0

 DO                                ; Loop over all groups -------------------

   UD NEXT [key], [grp],[np],[Def] ; Get input group number
     [sel_grp]                     ; Group selection doc file (input)
   IF ( [key] <= 0 ) EXIT

   SYS
     echo " Group: {%I3%[grp]}   Images: {%I5%[np]}   Defocus: {%I5%[Def]}"
   
   [ntot]  = [ntot]  + [np]
   [ngrps] = [ngrps] + 1

   NORM                            ; Normalize image contrast
     [part]@*                      ; Spider stack file     (input)
     [sel_part]                    ; Selection file        (input)
     *                             ; No mask file      
     [norm_part]@*                 ; Spider stack file     (output)
     N                             ; No flat field 

   DO                              ; Loop over all particles ------------

     UD NEXT [key], [n]            ; Get input image number
       [sel_part]                  ; Selection file        (input)
     IF ( [key] <= 0 ) EXIT

     SYS
       echo [select].spi {*****[Def]} {*****[Def]} {%f6.2%[DefAng]} {%i4%[Volt]} {%f5.2%[ACR]} {%f5.1%[SphAb]} >> [starfile]
      !echo [select].spi {*****[Def]} {*****[Def]} 0.0 200 0.1 2.00 >> [starfile]

   ENDDO                           ; End loop over all particles --------

   UD NEXT E                       ; Get input image number
     [sel_part]                    ; Selection file        (input)

 ENDDO                             ; End loop over all groups ---------------

 UD NEXT E                         ; Get input image number
   [sel_grp]                       ; Group selection doc file (input)

 SYS
    echo " Groups: {%I0%[ngrps]} Images: {%I0%[ntot]} "

 EN



 ! ------------------------ UNUSED BELOW HERE ------------------------------
 FS
   [part]@2                        ; Spider stack file     (input)
 FS
   [norm_part]@2                   ; Spider stack file     (input)