; Average, filter & shrink a set of particle images
 ;
 ; SOURCE:       spider/docs/techs/recon1/Procs/avg-filter-byview.spi
 ;
 ; ADAPTED FROM: filterbyview.spi, pfilt.bat
 ;
 ; PURPOSE:      Average, filter & shrink set of particle images
 ;
 ; USAGE:        clean ; ./spider spi/dat @avg-filter-byview
 ;
 ; REQUIRES:     spider/docs/techs/recon1/Procs/verify-settings.spi
 ;
 ; INPUTS:  (Where *** denotes image group,  ### denotes view)
 ;  [params]          ../params                  Parameter doc file         (one)
 ;  [ref_view_list]   [rec_dir]/sel_proj         List of reference views    (one)
 ;  [aligned_images]  [rec_dir]/dala_01_***      Aligned-images stack       (one/group)
 ;  [view_list]       views/prj###/sel_part_byv  Particle list              (one/view)
 ;
 ; OUTPUTS: 
 ;  [sd_vsview]       sd_vsview                  View average summary doc file           (one)
 ;  [filtered_stack]  views/prj###/filtavg       Filtered view average stacks            (one/view)
 ;  [total_view_avg]  views/prj###]/allavg       OPTIONAL full-sized view average stacks (one/view)
 ;
 ; ----------------------- Parameters -----------------------

 [save-avg-yn]   = 1     ; Flag to save unfiltered view-average (1 == save)

 [save-filt-yn]  = 1     ; Flag to save filtered images         (1 == save)
 [reduction]     = 2     ; Size factor (integer) for optional filtered images
 [filter-type]   = 7     ; Filter type for optional filtered images (7 == Butterworth)

 [filt-param1]   = 37.6  ; Pass-band, Angstroms (assuming Butterworth)
 [filt-param2]   = 28.2  ; Stop-band, Angstroms (assuming Butterworth)

 [last-view]     = 0     ; Last reference-view to filter  (0 == all)
 [view-progress] = 1     ; Prints progress message every Nth reference view

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

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

 ; Set common filenames & parameters
 @verify-settings

 ; Set temporary filenames

 [fullsize_mask]        = '_6'
 [temp_mirrored_incore] = '_5'
 [temp_filtered_incore] = '_4'
 [temp_filtered_stack]  = '_2'
 [temp_fullsize_stack]  = '_1'
 [temp_variance_incore] = '_3'

 SYS
   echo -n "  Filtering images    "; date ; echo

 ; Get parameters
 UD 5, [sp_pixsiz]
   [params]
 UD 17, [sp_winsiz]
   [params]
 UD E

 [reduced-size] = [sp_winsiz]/[reduction]
 [mask-radius]  = INT([reduced-size]/2)

 IF ( [save-filt-yn]*[save-avg-yn] == 0 ) THEN
    SYS
      echo "  WARNING: Neither unfiltered nor filtered average requested -- Continuing..."
 ENDIF

 ; Make circular mask
 IF ( [save-filt-yn]  > 0 ) THEN
   MO
     [fullsize_mask]              ; Circle image (output)
     [sp_winsiz],[sp_winsiz]      ; Image dimensions
     C                            ; Circle
     [mask-radius]                ; Mask radius
 ENDIF

 DE
   [sd_vsview]
 ; Label summary doc
 IF ( [save-avg-yn]  > 0 ) THEN
   SD /       VIEWNUM      PARTICLES        SD_VAR
     [sd_vsview]                  ; File         (output)
 ELSE
   SD /       VIEWNUM      PARTICLES      
     [sd_vsview]                  ; File         (output)
 ENDIF

 ; Change filter radii to reciprocal pixels
 [filt-param1] = [sp_pixsiz]/[filt-param1]  ; Pass-band if Butterworth
 [filt-param2] = [sp_pixsiz]/[filt-param2]  ; Stop-band

 IF ( [last-view] == 0 ) THEN
   UD N [last-view]
     [ref_view_list]
 ENDIF

 ; Loop through reference-views
 DO [view] = 1,[last-view]            ; Loop through reference-views ------------

    ; Skip unpopulated views
    IQ FI [exists]
      [view_list][view]               ; File      (input)
    IF ( [exists] == 0 ) THEN
       SYS
         echo " View: {%I3%[view]}  Out of: {%I3%[last-view]}    Is empty"
       CYCLE
    ENDIF

    SYS
       mkdir -p [prj_dir][view]

    ; Get # of particles
    UD N [num-particles]
      [view_list][view]               ; File      (input)

    IF ( int( [view]/[view-progress] ) == [view]/[view-progress] ) THEN
      SYS
        echo "  View: {%I3%[view]}  Out of: {%I3%[last-view]}    Particles: {%I5%[num-particles]}"
    ENDIF

    ; Skip unpopulated views
    IF ( [num-particles] == 0 ) CYCLE

    ; Initialize stacks
    IF ( [save-filt-yn]  > 0 ) THEN    
      DE
        [temp_filtered_stack]         ; Stack     (output)
      MS
        [temp_filtered_stack]@        ; Stack     (output)
        [reduced-size],[reduced-size] ; Dimensions
        [num-particles]               ; Max image allowed

    ENDIF

    IF ( [save-avg-yn]  > 0 ) THEN
      DE
        [temp_fullsize_stack]         ; Stack     (removed)

      MS
        [temp_fullsize_stack]@        ; Stack     (output)
        [sp_winsiz],[sp_winsiz]       ; Dimensions
        [num-particles]               ; Max image allowed

    ENDIF

    DE
      [filtered_stack][view]

    ; Loop through particles
    DO  [grp-part-key] = 1,[num-particles] ; Loop through particles ------------

        ; Get group-slice#, mirroring, group#
        UD IC [grp-part-key], [view-slice],[global-num],[grp-slice],[ccrot],[mirror-flag],[grp-num]
          [view_list][view]

        ; Mirror, if necessary
        IF ( [mirror-flag] < 0 ) THEN
          MR
            [aligned_images][grp-num][grp-slice] ; Image          (input)
            [temp_mirrored_incore]               ; Mirrored image (output)
            Y                                    ; Mirror around y-axis
        ELSE
          CP
            [aligned_images][grp-num][grp-slice]
            [temp_mirrored_incore]
        ENDIF

        ; Copy to full-sized stack, if necessary
        IF ( [save-avg-yn]  > 0 ) THEN

          CP
            [temp_mirrored_incore]         ; File         (input)
            [temp_fullsize_stack]@{*****[grp-part-key]} ; (output)
        ENDIF

        ; Filter, if necessary
        IF ( [save-filt-yn]  > 0 ) THEN
          ; If Butterworth filter
          IF ( [filter-type] == 7 ) THEN
            FQ NP
              [temp_mirrored_incore]       ; File         (input)
              [temp_filtered_incore]       ; File         (output)
              [filter-type]                ; Filter type
              [filt-param1],[filt-param2]  ; passband, stopband
          ENDIF

          ; If Fermi filter
          IF ( [filter-type] == 5) THEN
            FQ NP
              [temp_mirrored_incore]       ; File         (input)
              [temp_filtered_incore]       ; File         (output)
              [filter-type]                ; Filter type = Fermi
              [filt-param1]                ; Filter radius
              [filt-param2]                ; Temperature cutoff
          ENDIF

          ; If Gaussian or top-hat filter
          IF  ( [filter-type] <= 3 ) THEN
            FQ NP
              [temp_mirrored_incore]       ; File         (input)
              [temp_filtered_incore]       ; File         (output)
              [filter-type]                ; Filter type = top hat, Gaussian
              [filt-param1]                ; Filter radius
          ENDIF

          ; Shrink, if necessary
          IF ( [reduction]  > 1 ) THEN
            DC S
              [temp_filtered_incore]      ; File          (input)
              [temp_filtered_stack]@{*****[grp-part-key]}
              [reduction],[reduction]     ; Reduction in X,Y
          ELSE
            CP
              [temp_filtered_incore]      ; File            (input)
              [temp_filtered_stack]@{*****[grp-part-key]}   ;  (output)
          ENDIF
      ENDIF                               ; Filter IF-THEN
    ENDDO                                 ; End particle-loop -----------------

    ; Close files
    UD ICE
      [view_list][view]

    IF ( [save-avg-yn]  > 0 ) THEN
      AS R                              ; Average
        [temp_fullsize_stack]@*****     ; File          (input)
        1-[num-particles]               ; Image range
        A                               ; All particles
        [total_view_avg][view]          ; Average file  (output)
        [temp_variance_incore]          ; variance  (output, not saved)

      ; Get standard deviation of the variance
      FS M [min],[max],[avg],[sd-var]
        [temp_variance_incore]          ; Variance
        [fullsize_mask]
    ELSE
      [sd-var] = 1                      ; Dummy variable
    ENDIF

    CP
      [temp_filtered_stack]@              ; File         (input)
      [filtered_stack][view]@             ; File         (output)

    DE
      [temp_filtered_stack]@

    ; Write info to document
    SD [view], [view],[num-particles],[sd-var]
      [sd_vsview]                     ; File         (output)

 ENDDO                                   ; End view-loop -----------------------

 ; Close summary doc
 SD E
  [sd_vsview]                            ; File        (closed)

 SYS
   echo ; echo -n "  Done   "; date ; echo

 EN D

 ; Modified 2013-10-16
 ;    TO DO: add option to save variance
 ;    2013-10-23 (agl)       -- New file names, modernized syntax & cosmetic
 ;    2011-10-24 (trs)       -- Added options to average unshrunken images and save shrunken images
 ;    2011-10-24 (trs)       -- Adapted from filterbyview.spi
 ;    2009-11-20 (trs)       -- Bug fix: #particles written to HOW_MANY file was 0
 ;    2009-06-03 (trs)       -- Changed format of select/sel files
 ;    2009-04-22 (trs)       -- Generates average for all particles
 ;    2009-03-13 (trs      ) -- Makes directory for unpopulated views
 ;    2007-10-19 (trs      ) -- Added check for unpopulated views
 ;    2007-09-28 (trs)       -- Bug fixes: select/sel format fixed and needed to delete re-used stack _2
 ;    2007-06-07 (trs)       -- Now filtering by view and into stacks
 ;    2007-06-07 (trs)       -- Now getting mirror information from VO MQ output
 ;    2007-05-30 (trs & gsa) -- Reflects change to input file sel_part
 ;    2007-03-27 (trs & xa)  -- Reflects changes to input file sel_part
 ;    2007-02-22 (trs & djt) -- Bug fix for filenames
 ;    2007-01-24 (trs)       -- Input aligned images are now stacked
 ;    2006-08-17 (trs)       -- Bug fix for mirroring in prj001 
 ;    2006-04-05 (trs)       -- Changed units for filter radii
 ;    2004-12-28 (trs)       -- Mirrors aligned image, if necessary
 ;    2004-06-16 (trs)       -- Dusts images

 ;