; Calculates average power spectra and defocus values for set of micrographs
 ;
 ; PURPOSE: Calculates average power spectra for a set of micrographs, 
 ;          estimates defocus from the power spectra, and places defocus
 ;          value in a doc. file.

 ; PURPOSE: Inputs 2D power spectrum: power/roo****
 ;          Creates CTF document file.
 ;          Uses SPIDER operation 'TF ED'
 ;
 ; SOURCE:  spider/docs/techs/recon1/Procs/ctfinfo.spi 
 ;
 ; Edit following filenames as needed.
 ;
 ; ----------- Input files --------------

 [params]  = '../params'             ; Parameter doc file            (one)

 [sel_mic] = '../sel_micrograph'     ; Micrograph selection doc file (one)

 [indir]   = 'power'                 ; Power spectra directory       (one)

 [pow]     = '[indir]/pw_avg_****'   ; Power spectrum images         (one/micrograph)

;[roo]     = '[outdir]/roo_****'     ; Rotational average file       (one/micrograph)

;[rod]     = '[outdir]/roo_doc_****' ; Rotational average doc file   (one/micrograph)

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

 [outdir]  = 'power'                 ; Power spectra directory       (one)

 [ctf]     = '[outdir] ctf_****'     ; CTF noise doc files           (one/micrograph)

 ; ----------- Temp file --------------

 ; Get Parameters
 UD 5,[sp_pixsiz]             ; Get pixelsize (A)
   [params]                   ; Params file           (input)
 UD 6,[sp_kev]                ; Electron beam voltage (kV)
   [params]                   ; Params file           (input)
 UD 7,[sp_sph_abb]            ; Spherical aberration
   [params]                   ; Params file           (input)
 UD 12,[sp_acr]               ; Amplitude contrast ratio
   [params]                   ; Params file           (input)

 [sp_lambda] = 12.398 / SQR([sp_kev] * (1022.0 + [sp_kev]))


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

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

   UD [key], [mic]            ; Get micrograph number
     [sel_mic]                ; Micrograph selection doc file  (input)

   SYS
      echo '  'Loading Power Spectrum: [pow][mic]  


   DE                         ; Delete doc file
     [ctf]                    ;                          (removed)

   ; Transfer Function -- Estimate CTF parameters

   TF ED [ang], [mag],[def],[overdef],[cutoff]
     [pow][mic]               ; 2D power spectrum        (input)
     [sp_pixsiz],[sp_sph_abb] ; Pixel size, Spherical aberration
     [sp_lambda]              ; Electron wavelength 
     [sp_acr]                 ; Ampl. contrast ratio
     [ctf][mic]               ; CTF noise doc file       (output)

 ENDDO                        ; End of loop over all micrographs -------

 EN