; Shrinks micrographs and rescales them
 ;
 ; SOURCE:  spider/docs/techs/recon1/Procs/shrink-mic.spi
 ;
 ; PURPOSE: Shrinks micrographs and rescales them
 ;
 ; USAGE:   clean ;  spider spi/dat @shrink-mic


 ; ------------- Parameters -------------

 [reduction]    = 8                    ; Reduction factor
 
 [progress]     = 1                    ; Progress message written every Nth micrograph

; --------------- Inputs ---------------

 [sel_mic]      = '../sel_micrograph'  ; Micrograph list

 [input_micros] = 'raw****'            ; Micrographs

 ; --------------- Output ---------------

 [small_micros] = 'sm_mic_****'        ; Reduced size micrographs

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

 ; Get # of micrographs
 UD N [num-micros]
   [sel_mic]                           ; Doc file        (input)

 SYS
   echo -n "  Shrinking: {%I6%[num-micros]} micrographs   "; date ; echo 

 ; Loop through micrographs
 DO  [index] = 1,[num-micros]          ; Loop through micrographs -----------------

    ; Get micrograph #
    UD IC [index], [micro-num]
       [sel_mic]                       ; Doc file        (input)

    IF (int([index]/[progress]) == [index]/[progress]) THEN
      SYS
        echo "  Processing micrograph: {%I6%[micro-num]},  {%I6%[index]} out of {%I6%[num-micros]}"
    ENDIf

    ; Decimate image size by summing neighboring pixels
    DC S                                ; Decimate image 
      [input_micros][micro-num]         ; File        (input)
      _1                                ; File        (output)
      [reduction], [reduction]          ; Reduction factor

    FI H [img-avg],[img-sd]
      _1                                ; File        (input)
      AVG,SIG                           ; Header positions for avg, s.d.

    ; Normalize image intensity
    AR
      _1                                ; File        (input)
      [small_micros][micro-num]         ; File        (output)
      (p1-[img-avg]) / [img-sd]
 ENDDO                                  ; End micrograph-loop

 UD ICE
   [sel_mic]

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

 EN D

 ; Modified 2013-10-16
 ;    2013-10-16 (agl) -- modernized syntax, cosmetic
 ;    2009-04-14 (trs) -- normalizes shrunken micrographs