; Creates average and variance images
;
; SOURCE: average.spi 
;
; PURPOSE: Creates average and variance for images which come from a 
;          each reference projection view.  Works for a single
;          group
;
; I/O PARAMETERS AND FILES ARE SET HERE:
;
;  ------------ Parameters ---------------------------------------

[grp] = 1                              ; Defocus group

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

FR G
[params]../params                      ; The parameter file

FR G
[part_per_proj]df{***[grp]}/how_many   ; Number of particles for each projection for group

FR G
[select]df{***[grp]}/select/sel***     ; Particle numbers assigned to each projection set

FR G
[ali]../Alignment/dala01_{***[grp]}@****** ; Aligned particle images

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

FR G
[dir]avg                              ; Output directory

FR G
[avg][dir]/avg***                     ; Average image stack for each projection set

FR G
[var][dir]/var_{***[grp]}@******      ; Variance image stack for each projection set

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

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

UD 17,[winsiz]                        ; Get image size
[params]

VM                                    ; Create output directory if necessary                            
mkdir -p [dir]                   

UD N [numprojs]                       ; Get number of projections
[part_per_proj]

[proj]=1
UD N,x30,x31                          ; Get number of cols in particle selection files
[select][proj]                        ; First selection file                    (input)

[navg]=0                              ; Zero average counter

VM
echo " Group: {****[grp]} Creating average and variance images for {***[numprojs]} projections"

DO LB1 [proj]=1,[numprojs]            ; Loop over all projections

   UD IC [proj], [part]               ; Get number of particles for current projection
   [part_per_proj]                    ; Number of particles per projection (input)

   IF ([part] .EQ. 0) THEN            ; Zero particles for this projection
      BL
      [avg][proj]                     ; Average output                    (output)
      [winsiz],[winsiz]               ; Size
      N                               ; No background
      (0.0)                           ; Value

      BL
      [var][proj]                     ; Variance output                   (output)
      [winsiz],[winsiz]               ; Size
      N                               ; No background
      (0.0)                           ; Value

      GOTO LB10                       ; Zero particle (should not occur!)
   ENDIF

   IF (x31 .GT. 2) THEN               ; New 'AP SH' & 'VO MQ' input
      UD IC 1, [pn],[cc],[mir]        ; Retrieve particle number, CC, & mirror flag
      [select][proj]                  ; Selection file                   (input)
   ELSE                               ; Old 'VO MQ' style input
      UD IC 1, [pn],[cc]              ; Retrieve particle number, CC
      [select][proj]                  ; Selection file                   (input)
      
      [mir] = 1.0                     ; No need to mirror
   ENDIF

   IF ([part] .EQ. 1) THEN            ; Only a single particle for this projection
      IF ([mir] .LT. 0.0) THEN
         MR                           ; Must mirror the image
         [ali][pn]                    ; Image file
         [avg][proj]                  ; Average file                     (output)
         Y                            ; Y axis
      ELSE
         CP                           ; Copy image to average
         [ali][pn]                    ; Image file                       (input)
         [avg][proj]                  ; Average file                     (output)
      ENDIF

      BL
      [var][proj]                     ; Variance                         (output) 
      [winsiz],[winsiz]               ; Size
      N                               ; No background
      (0.0)                           ; Value

   ELSE                               ; Multiple particles for this projection
      MS
      _1@
      [winsiz],[winsiz]               ; Dimensions
      [part]                          ; Num. slices

      DO LB2 [partorder]=1,[part]     ; Loop over particles
         UD IC [partorder],[pn],[cc],[mir]
         [select][proj]

         IF ([mir] .LT. 0.0) THEN
            MR                        ; Must mirror the image
            [ali][pn]                 ; Image file                     (input)
            _1@{*****[partorder]}     ; Temp. mirrored file            (output)
            Y                         ; Y axis
         ELSE
            CP
            [ali][pn]                 ; Image file                     (input)
            _1@{*****[partorder]}     ; Temp. mirrored file            (output)
         ENDIF
      LB2                             ; End particle-loop

      AS R                            ; Add to average/variance
      _1@******
      (1-[part])                      ; Selection file
      A
      [avg][proj]                     ; Average input
      [var][proj]                     ; Average output                  (output)

      DE
      _1@
   ENDIF

   UD ICE
   [select][proj]                     ; Selection file                  (input)

   [navg] = [navg] + 1                ; Increment counter for average

   LB10
LB1

UD ICE
[part_per_proj]

DE
jnk_mir_tmp_1                         ; Temp. mirrored file cleanup

VM
echo ' '
EN
;