; Calculates avg power spectra for a set of micrographs
;
; PURPOSE: ; Calculates avg power spectra for a set of micrographs.
;            Converts a scanned file to SPIDER format.
;            Input: window size, percentage of the overlap
;            distance of the window from the micrograph border
;            Spectra placed in: power/roo***
;
; SOURCE: power.spi 
;
; CALLS:   power_p1.spi 
;
; Edit the following parameters as needed, including the filenames

[winsiz] = 500    ; Window size of small pieces (Sx=Sy)
[xover]  = 50     ; % of the overlap in x
[yover]  = 50     ; % of the overlap in y
[xd]     = 500    ; Dist. from the edge (x)
[yd]     = 500    ; Dist. from the edge (y)
[mask]   = 1      ; 1 = mask out center of spectrum; 0 = don't mask

; [deci] = decimation factor for 'DC S' operation
;  0 = get value from param file (key=16)
;  1 = full sized image
;  2 = 1/2 size
;  4 = 1/4 size
[deci] = 1     ; Decimation factor (0 = get value from  param file)

; ----------- Input files --------------
FR L
[params]../params                     ; Parameter file

FR L
[sel_mic]../sel_micrograph            ; Micrograph selection doc file 

FR L
[micgr]../Micrographs/mic{****[mic]}  ; Micrographs

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

FR L
[outdir]power                         ; Power spectra output directory

FR L
[spectrum][outdir]/pw_avg{****[mic]}  ; Power spectra

FR L
[roo][outdir]/roo{****[mic]}          ; Rotational average (doc file)

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

; This gets the file numbers,
; checks if they need to be unzipped or converted,
; then calls power_p1 in the main loop

UD N [nummics]                 ; Get number of micrographs
[sel_mic]

VM                             ; Make sure output dir. present
mkdir -p [outdir]

FR G                           ; Name for incore doc file
[tmparr]tmparray

[winsizd2]=[winsiz]/2
SD IC NEW                      ; Create an in-core doc file array
[tmparr]
4,[winsizd2]                   ; Creates a 4 column doc file

DO LB1 [key]=1,[nummics]       ; Loop over all micrographs -------------------

  UD [key], [mic]
  [sel_mic]                    ; Doc file              (inpu)

   VM
   echo ' 'Processing micrograph: {*****[mic]}

   ; Get zip and format flags from parameter file
   UD 1,[zflag]                ; Get zip flag
   [params]

   UD 2,[fflag]                ; Get tif flag
   [params]

   IF ([zflag]*[fflag].GT.0) THEN   ; Both tif & zip flags set
       VM                           ; Unzip the file
       gunzip [micgr].tif.gz
       GOTO LB3
   ENDIF

   IF ([zflag].GT.0) THEN     ; Zip, but not tif, flag set
       VM                     ; Unzip the file
       gunzip [micgr].$DATEXT.gz
       GOTO LB3
   ENDIF

   LB3

   IF ([fflag].EQ.1) THEN
       UD 3,x51        ; If it's a HiScan tif, get X,Y size parameters
       [params]
       UD 4,x52
       [params]

       CP FROM RAW
       [micgr].tif       ; Raw file      (input)
       (16)              ; Bits per pixel
       x51,x52           ; Columns & rows
       (342)             ; Header bytes
       (1)               ; Most significant byte
       N                 ; Do not fold negatives
       junk12987         ; Spider file   (output)

       AR
       junk12987
       _1
       log(p1+1)

       DE
       junk12987

   ENDIF

   ; If it's a ZI tif, make system call to zi2spi.
   ; NB OVERVIEW SHOULD ALWAYS = 1, USE 'DC S' TO REDUCE SIZE
   IF ([fflag].EQ.3) THEN

       VM                       ; Convert to SPIDER format
       zi2spi [micgr].tif [micgr].$DATEXT 1

       CP                       ; Place in incore file
       [micgr]
       _1

       DE                       ; Delete the SPIDER format file
       [micgr]                  ;

   ENDIF

   IF ([fflag].eq.0) THEN	; If not tif, still need to put input in _1
       CP                       ; Place in incore file
       [micgr]                  ;                       (input)
       _1
   ENDIF

   ; Size reduction, if any
   IF ([deci].EQ.1) GOTO LB7   ; No reduction

   IF ([deci].LT.1) THEN
      UD 16,[deci]          ; Get reduction factor from param file
      [params]              ;                             (input)

      IF ([deci].LE.1) GOTO LB7
   ENDIF

   DC S                     ; Decimate image - Sum neighbouring pixels
   _1
   _2
   [deci],[deci]            ; Decimation factor

   CP                       ; Copy file
   _2
   _1

   LB7

   ; If the output doc files already exist, delete them
   DE
   [roo]

   @power_p1([winsiz],[xover],[yover],[xd],[yd],[mask])
   _1
   [spectrum]
   [roo]
   [tmparr]

   IF ([zflag]*[fflag].GT.0) THEN     ; Both tif & zip flags set
       VM
       gzip [micgr].tif
       GOTO LB4
   ENDIF

   IF ([zflag].eq.1) THEN             ; Only zip flag
       VM
       gzip [micgr].$DATEXT
       GOTO LB4
   ENDIF

   LB4
LB1

EN
;