; Calculates avg power spectra for a set of micrographs
;
; PURPOSE: ; Calculates average 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]   = 225    ; Power spectrum mask radius (Angstroms); 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  [key]=1,[nummics]              ; Loop over all micrographs -------------------

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

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

   ;  -----  Get zip & format flags from parameter file (can params vary??) ---------
   UD 1,[zflag]                    ; Get zip flag
   [params]

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

   ; ----------- Checks if files are gzip compressed ------------------

   IF ([zflag]*[fflag].GT.0) THEN  ; Both tif & zip flags set
       VM                          ; Unzip the file
       gunzip [micgr].tif.gz
   ELSEIF ([zflag].GT.0) THEN      ; Zip, but not tif, flag set
       VM                          ; Unzip the file
       gunzip [micgr].$DATEXT.gz
   ENDIF

   ; ----------- Conversion based on scanner type -------------------
   IF ([fflag] .eq .0) THEN	; Spider file, need to put input in _1
      CP                        ; Place in incore file
      [micgr]                   ; Spider file   (input)
      _1                        ; Spider file   (output)

   ELSEIF ([fflag] .EQ. 1) THEN
      UD 3,[nsam]              ; HiScan raw file, Get X,Y size parameters 
      [params]                 ; Params file    (input)
      UD 4,[nrow]              ;         
      [params]                 ; Params file    (input)

      CP FROM RAW
      16                       ; Bits / pixel
      [micgr].tif              ; Raw file      (input)
      [raw]                    ; File (input)
      [nsam],[nrow]            ; Size
      (342)                    ; Header bytes
      (1)
      N
      _2                       ; Spider file   (output)

      AR
      _2                       ; Spider file   (input)
      _1                       ; Spider file   (output)
      log(p1+1)

   ELSEIF ([fflag] .EQ. 3) THEN

      VM                       ; ZI tif file.  Overview should always = 1
      zi2spi [micgr].tif [micgr].$DATEXT 1

      CP                       ; Place in incore file
      [micgr]
      _1                       ; Spider file   (output)

      DE                       ; Delete the SPIDER format file
      [micgr]                  ;

   ELSEIF ([format].EQ.4) THEN     
      CP FROM NIKON            ; Nikon Tif Scanner file
      [micgr].tif              ; Nikon tif file (input)
      _1                       ; Spider file    (output)

   ENDIF 

   ; ----------- Size reduction, if any -------------------

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

   IF ([deci].GT.1) THEN       ; Reduction

      DC S                     ; Decimate image - Sum neighbouring pixels
      _1                       ; Spider file (input)
      _2                       ; Spider file (output)
      [deci],[deci]            ; Decimation factor

      CP                       ; Copy file
      _2                       ; Spider file (input)
      _1                       ; Spider file (output)
   ENDIF

   ; ----------- Masking ---------------------------------------

   IF ([mask].NE.0) THEN
      UD 5,[pxsiz]               ; Get pixel size
      [params]                   ; Params file (input)

      [pxsiz]=[pxsiz]*[deci]     ; Adjust pixel size for decimation

      [radius]=2*[pxsiz]/[mask]  ; Mask radius (now in 1/px)
   ENDIF

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

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

   ; ----------- Rezip if necessary -------------------
   IF ([zflag]*[fflag] .GT. 0) THEN  ; Both tif & zip flags set
       VM                            ; Recompress the file
       gzip [micgr].tif

   ELSEIF ([zflag].eq.1) THEN        ; Only zip flag
       VM                            ; Recompress the file
       gzip [micgr].$DATEXT
   ENDIF

ENDDO

EN
;