; Generates CTF correction profiles and files for micrographs
 ;
 ; PURPOSE:  Generates CTF correction profiles and files for micrographs based on defocus, etc.
 ;
 ; SOURCE:   spider/techs/recon1/Procs/make-ctf-cor.spi
 ;
 ; USAGE:    clean ; spider spi/dat @make-ctf-cor

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

 [wantplots-yn]   = 1         ; Want doc files for plotting CTF's? (0 == No)

 [viewplot-yn]    = 1         ; Open Gnuplot automatically?        (0 == No)

 [mic2plot]       = -1        ; Micrograph # to plot (<= 0 for highest defocus)

 [progress]       = 1         ; Prints progress message to screen every Nth micrograph

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

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

 [defocus_doc]    = 'defocus'                    ; Defocus doc file lists defocus    (one)

 [calc_power]     = 'power/roo_doc_****'         ; 1D calculated power spectrum      (one/micrograph)
                                                 ; Only need if plotting             (one/micrograph)

 ; ----------------- Outputs -----------------

 [ctf_dir]        = 'power'                      ; Output directory                  (one)

 [ctf_cor]        = '[ctf_dir]/flipctf_****'     ; Phase-flipped CTF correction file (one/micrograph)

 [ctf_cor_doc]    = '[ctf_dir]/flipctf_doc_****' ; Phase-flipped CTF doc file        (one/micrograph, if wantplots)

 [gnuplot_script] = 'viewtrap.gnu'               ; Gnuplot script                    (one, if wantplots)

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

 ; Set temporary filenames
 [temp_defocus_renumbered] = 'tmpdocdefocus-renum'
 [temp_defocus_sorted]     = 'tmpdocdefocus-sort'
 [temp_ctf_nohead]         = 'tmpdocctf-nohead'

 ; Get parameters
 UD IC  5,[sp_pixsiz]
   [parameter_doc]
 UD IC  7,[sp_sph_abb]
   [parameter_doc]
 UD IC  8,[sp_sourcesiz]       
   [parameter_doc]
 UD IC  9,[sp_def_spread]
   [parameter_doc]
 UD IC 10,[sp_astig]
   [parameter_doc]
 UD IC 11,[sp_az_astig]
   [parameter_doc]
 UD IC 12,[sp_acr]
   [parameter_doc]
 UD IC 13,[sp_geh]
   [parameter_doc]
 UD IC 14,[sp_lambda]
   [parameter_doc]
 UD IC 15,[sp_max_spfreq]
   [parameter_doc]
 UD IC 17,[sp_winsiz]
   [parameter_doc]
 UD ICE                                  ; Close document
   [parameter_doc]

 SYS                                     ; Create output directory
   mkdir -p [ctf_dir]

 SYS 
   echo " Generating CTF correction files" ;  echo ' '

 DOC REN                                 ; Renumber in case keys are not-consecutive
   [defocus_doc]                         ; Lists defocus for micrographs  (input)
   [temp_defocus_renumbered]             ; Renumbered: [defocus_doc]      (output) 

 ; Get number of micrographs
 UD N [num-mics]                         ; Get number
   [temp_defocus_renumbered]             ; Renumbered: [defocus_doc]      (input)

 ; Loop through micrographs
 DO [mic-key] = 1,[num-mics]
    ; Get micrograph#, defocus value
    UD IC [mic-key],[mic-num],[defocus-angst]
      [temp_defocus_renumbered]   ; Renumbered: [defocus_doc]      (input)

    IF ( INT([mic-key]/[progress]) == [mic-key]/[progress] ) then
        SYS 
          echo " Processing micrograph: {%I4%[mic-num]} ({%I4%[mic-key]}th out of {%I4%[num-mics]})"
    ENDIF

    ; Delete old files
    DE
      [ctf_cor][mic-num]
    DE
      [ctf_cor_doc][mic-num]
    DE
      [ctf_cor][mic-num]

    ; Generate phase flipping CTF file for this micrograph
    TF CT                                ; Generate phase flipping transfer function
      [ctf_cor][mic-num]                 ; CTF correction file           (output)
      [sp_sph_abb]                       ; Spherical abberation (MM)
      [defocus-angst],[sp_lambda]        ; Defocus(A), Lambda(A)
      [sp_winsiz], [sp_winsiz]           ; Number of spatial freq. points
      [sp_max_spfreq]                    ; Maximum spatial frequency(1/A)
      0,[sp_def_spread]                  ; Source size(unused), Defocus spread
      [sp_astig], [sp_az_astig]          ; Astigmatism(A), Astigmatism azimuth(DEG)
      [sp_acr]                           ; ACR  
      -1                                 ; Sign

    IF ([wantplots-yn] == 0) CYCLE       ; Skip plot file creation

    ; Generate phase flipping CTF doc file for this micrograph
    ; For plot only.  Do not use with 'FD' for phase flipping 
    TF LIS                               ; Generate straight & flipped CTF profiles
      [ctf_cor_doc][mic-num]             ; CTF doc file                  (output)
      [sp_sph_abb]                       ; Spherical abberation (MM)
      [defocus-angst], [sp_lambda]       ; Defocus(A), Lambda(A)
      [sp_winsiz]                        ; Number of spatial freq. points
      [sp_pixsiz]                        ; Pixel size
      0, [sp_def_spread]                 ; Source size as in 'TF CT', Defocus spread
      [sp_acr], 0                        ; ACR, Gaussian env halfwidth as used by 'TF CT' 

 ENDDO                                   ; End micrograph-loop

 IF ([wantplots-yn] == 0) THEN           ; Skip plot file creation
    RE
 ENDIF

 ; Sort defocus docs file columns
 DOC SORT                                ; Sort 
   [defocus_doc]                         ; Defocus doc file            (input)
   [temp_defocus_sorted]                 ; Sorted defocus doc file     (output)
   2                                     ; Column # to sort: defocus
   Y                                     ; Renumber?

 ; Get highest defocus if micrograph # not specified
 IF ( [mic2plot] <= 0 ) THEN
    UD [num-mics], [mic2plot]
      [temp_defocus_sorted]
    UD E
 ENDIF

 ; Find scaling constant for graphing power spectrum
 DOC STAT [nval],[min],[max] 
   [calc_power][mic2plot]
   1                                     ; Column 

 [maxstr] = ' {%f9.4%[max]}'                ; Scaling for power spectrum
 [constr] = '(\00443 / [maxstr])'           ; Scaling for power spectrum

 !sys
 !  echo " Max: {%f9.3%[max]}   constr: [constr] "

 SYS 
   echo ; echo " Generating Gnuplot script for micrograph: {%I0%[mic2plot]}" 

 ; Clean up
 DE   
   [temp_defocus_sorted]
 SYS 
   rm -f [gnuplot_script] [temp_ctf_nohead]

 ; Remove header comments of doc file
 SYS                                       
   tail +10 [ctf_cor_doc][mic2plot].$DATEXT > [temp_ctf_nohead] 

 SYS 
   echo 'set xzeroaxis' > [gnuplot_script]
 SYS 
   echo 'set xlabel "Resolution (1/Pixels)"'  >> [gnuplot_script]
 SYS 
   echo 'plot  \' >> [gnuplot_script]
  
 SYS 
   echo -ne '"[calc_power][mic2plot].$DATEXT"  using  5:[constr] ' >> [gnuplot_script]  
 SYS 
   echo 'title "[calc_power][mic2plot].$DATEXT" with lines, \'     >> [gnuplot_script] 

 SYS 
   echo '"[temp_ctf_nohead]" using  3:6 title "-Straight CTF" with lines, \' >> [gnuplot_script]
 SYS 
   echo '"[temp_ctf_nohead]" using  3:7 title "Flipped CTF"  with lines, \' >> [gnuplot_script]
 SYS 
   echo '"[temp_ctf_nohead]" using  3:8 title "Trapped CTF"  with lines'    >> [gnuplot_script]

 ; If requested, open Gnuplot
 IF ( [viewplot-yn] .NE. 0 ) THEN
    SYS 
      echo " Gnuplot usage: gnuplot -persist [gnuplot_script]" ; echo
    SYS 
      gnuplot -persist [gnuplot_script]
 ELSE
    SYS 
      echo ; echo " gnuplot usage: load '[gnuplot_script]'" ; echo
 ENDIF

 EN

 ; Modified 2016-06-29
 ;     2016-06-29 (agl) -- Fixed plot
 ;     2015-11-16 (agl) -- Use operation 'TF CT' for phase flipping!!
 ;     2013-10-16 (agl) -- Modernized syntax, changed filenames, cosmetic
 ;     2012-05-09 (trs) -- Optionally spawns gnuplot, plotting by default highest-defocus micrograph
 ;     2004-02-24 (trs) -- Added padding factor to allow for oversampled FT's
 ;