; Creates parameter file for reconstruction
 ;
 ; SOURCE: spider/docs/techs/recon1/Procs/make-params.spi 
 ;
 ; PURPOSE: Creates parameter file, including values for 
 ;          lambda, spatial frequency, window size, particle size, etc. 
 ;
 ; USAGE:   clean ; ./spider spi/dat @make-params

                                                                 
 ; ----------- Registers ------------------------
 
 ; Following values may be converted to interactive mode

 ; Default values for ribosomes (only used if zero is input for query below)
 [diam]         = 276             ; Angstroms, default actual ribosome particle diameter

 ;? Source size, 1/A )?
 [sp_sourcesiz] = 0.000           ; Usually OK. This value = no effect on phase flipping CTF

 ;? Defocus spread, A )?
 [sp_def_spread] = 0.000          ; Usually OK. This value = no effect on phase flipping CTF

 ;? Astigmatism, A ?
 [sp_astig]      = 0.000          ; None. Usually OK if microscope is well adjusted

 ;? Azimuth, degrees ?
 [sp_az_astig]   = 0.000          ; None. Usually OK if microscope is well adjusted

 ;? Amplitude contrast ratio (0..1) ?
 [sp_acr]        = 0.1            ; Used for CTF correction. Usual value

 ;? Gaussian envelope halfwidth ?
 [sp_geh]        = 10000.00       ; Used for CTF correction, This value = no effect on phase flipping CTF

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

 [docfile] = 'params'

 [lisfile] = 'params-set'

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

 ; -------------- Interactive input --------------------------

 RR [sp_zipflag]
   ? Do the micrographs need to be unzipped (1 = yes, 0 = no)?

 RR [sp_format]
   ?File format (0 = SPIDER, 1 = TIF, 2 = PerkinElmer, 3 = ZI, 4 = Nikon, 5 = MRC)?

 [sp_xm] = 0  ; These values are left blank for SPIDER & ZI formats
 [sp_ym] = 0  ;

 IF ([sp_format].NE.0) THEN
   IF ([sp_format].NE.3) THEN
      IF ([sp_format].NE.5) THEN
         RR [sp_xm]
            ?X dimension (width) in pixels?
         RR [sp_ym]
            ?Y dimension (height) in pixels?
      ENDIF
   ENDIF
 ENDIF

 ; For ZI files, pixelsize refers to overview sampling, not original sampling
 RR [sp_pixsiz]
   ?Pixel size (in Angstroms)?

 RR [sp_kev]
   ? Electron energy, keV ?

 RR [sp_sph_abb]
   ?Spherical aberration constant (mm)?
 
 RR [sp_partsiz]
   ?Particle size, pixels (0 for ribosome default)?

 IF ([sp_partsiz].EQ.0) THEN      ; Use default particle size
   [sp_partsiz] = INT([diam]/[sp_pixsiz])     ; Compute particle size (pixels)
   
   SYS
   echo "Using particle size of {%i0%[sp_partsiz]} pixels" ; echo
 ENDIF

 RR [sp_winsiz]
   ?Window size, pixels (0 for default)?

 IF ([sp_winsiz].EQ.0) THEN      ; Use default window size 
   [sp_winsiz]   = INT([sp_partsiz]*4/3)       ; Compute window size (pixels)
   
   ; Ensure window size is even
   [xwt] = INT([sp_winsiz]/2.0)
   [v90] = [sp_winsiz] - (2*[xwt])
   IF ([v90] .NE. 0) [sp_winsiz] = [sp_winsiz] + 1
   
   SYS
   echo "Using window size of {%i0%[sp_winsiz]} pixels" ; echo
 ENDIF

 RR [sp_mag]
   ?Magnification?

 RR [sp_scanres]               ; Scanning resolution = DetectorPixelSize, um (Usually: 7 or 14)
   ? Detector pixel size, um (7, 14, ...)?

 RR [sp_dec]
   ? Decimation factor ?

 ; -------------- Compute dependent  parameters ---------------------------------

 ; Compute relativistic lambda from kev
 [sp_lambda] = 12.398 / SQR([sp_kev] * (1022.0 + [sp_kev]))

 ; Compute max. spatial freq. from pixel size
 [sp_max_spfreq] = 1.0 / (2.0 * [sp_pixsiz])

 ; -------------- Output parameters to procedure file --------------------------

 ; Delete existing output files
 DE
   [lisfile]

 SYS
   echo    ' ; Parameters for single particle reconstruction'   >> [lisfile].$DATEXT              
 SYS
   echo -n ' ; $PRJEXT/$DATEXT    ' >> [lisfile].$DATEXT                
 SYS
   date >> [lisfile].$DATEXT                
 SYS
   echo ' ' >> [lisfile].$DATEXT  
              
 SYS
   echo ' [sp_zipflag] =       ' {%f8.0%[sp_zipflag]}  \; Zip flag \(0: Do not unzip, 1: Must unzip\) >> [lisfile].$DATEXT
 SYS
   echo ' [sp_format] =        ' {%f8.0%[sp_format]}  \; File format \(0:SPIDER, 1:HiScan TIF, 2:PE, 3:ZI \) >> [lisfile].$DATEXT
 SYS
   echo ' [sp_xm] =            ' {%f8.0%[sp_xm]}  \; Micrograph width,  pixels \(ignored if present in header\) >> [lisfile].$DATEXT
 SYS
   echo ' [sp_ym] =            ' {%f8.0%[sp_ym]}  \; Micrograph height, pixels \(ignored if present in header\) >> [lisfile].$DATEXT
 SYS
   echo ' [sp_pixsiz] =      ' {%f8.2%[sp_pixsiz]}  \; Pixel size, A >> [lisfile].$DATEXT
 SYS
   echo ' [sp_kev] =       ' {%f8.2%[sp_kev]}     \; Electron energy, KeV >> [lisfile].$DATEXT
 SYS
   echo ' [sp_sph_abb] =    ' {%f8.3%[sp_sph_abb]}  \; Spherical aberration, mm >> [lisfile].$DATEXT
 SYS
   echo ' [sp_sourcesiz] =  ' {%f8.3%[sp_sourcesiz]} \; Source size, 1/A >> [lisfile].$DATEXT
 SYS
   echo ' [sp_def_spread] = ' {%f8.3%[sp_def_spread]}  \; Defocus spread, A >> [lisfile].$DATEXT
 SYS
   echo ' [sp_astig] =        ' {%f8.1%[sp_astig]}    \; Astigmatism, A >> [lisfile].$DATEXT
 SYS
   echo ' [sp_az_astig] =    ' {%f8.2%[sp_az_astig]}  \; Azimuth of astigmatism, degrees >> [lisfile].$DATEXT
 SYS
   echo ' [sp_acr] =        ' {%f8.3%[sp_acr]}     \; Amplitude contrast ratio >> [lisfile].$DATEXT
 SYS
   echo ' [sp_geh] =     ' {%f9.2%[sp_geh]}     \;  Gaussian envelope halfwidth  >> [lisfile].$DATEXT
 SYS
   echo ' [sp_lambda] =     ' {%f8.5%[sp_lambda]}  \;  Lambda, A >> [lisfile].$DATEXT
 SYS
   echo ' [sp_max_spfreq] = ' {%f8.3%[sp_max_spfreq]}  \;  Maximum spatial frequency, 1/A >> [lisfile].$DATEXT
 SYS
   echo ' [sp_dec] =          ' {%f8.1%[sp_dec]}      \;  Decimation factor >> [lisfile].$DATEXT
 SYS
   echo ' [sp_winsiz] =     ' {%f8.1%[sp_winsiz]}   \;  Particle window size, pixels >> [lisfile].$DATEXT
 SYS
   echo ' [sp_partsiz] =    ' {%f8.1%[sp_partsiz]}  \;  Size of particle, pixels >> [lisfile].$DATEXT
 SYS
   echo ' [sp_mag] =     ' {%f10.1%[sp_mag]}  \;  Magnification >> [lisfile].$DATEXT
 SYS
   echo ' [sp_scanres] =     ' {%f8.1%[sp_scanres]}  \;  Detector pixel size, um  >> [lisfile].$DATEXT
 SYS
   echo ' '   >> [lisfile].$DATEXT
 SYS
   echo ' RE' >> [lisfile].$DATEXT

 SYS
   echo '  'Reconstruction Parameters saved in: [lisfile].$DATEXT


 ; -------------- Output parameters to doc file --------------------------

 ; Delete existing output file
 DE
   [docfile]

 SD / key       Parameters for single particle reconstruction
   [docfile]

 SD /1) Zip flag    (0 = Do not unzip, 1 = Needs to be unzipped)
   [docfile]
 SD 1,[sp_zipflag]
   [docfile]

 SD /2) File format (0:SPIDER, 1:HiScan tif, 2:PerkinElmer, 3:ZI Scanner)
   [docfile]
 SD 2,[sp_format]
   [docfile]

 SD /3) Micrograph width, in pixels (Ignored if present in header)
   [docfile]
 SD 3,[xm]
   [docfile]

 SD /4) Micrograph height, pixels (Ignored if present in header)
   [docfile]
 SD 4,[sp_ym]
   [docfile]

 SD /5) Pixel size, A
   [docfile]
 SD 5,[sp_pixsiz]
   [docfile]

 SD /6) Electron energy, KeV
   [docfile]
 SD 6,[sp_kev]
   [docfile]

 SD /7) Spherical aberration, mm
   [docfile]
 SD 7,[sp_sph_abb]
   [docfile]

 SD /8) Source size,  1/A  
   [docfile]
 SD 8,[sp_sourcesiz]
   [docfile]

 SD /9) Defocus spread, A  
   [docfile]
 SD 9,[sp_def_spread]
   [docfile]

 SD /10) Astigmatism,   A
   [docfile]
 SD 10,[sp_astig]
   [docfile]

 SD /11) Azimuth of astigmatism, degrees
   [docfile]
 SD 11,[sp_az_astig]
   [docfile]

 SD /12) Amplitude contrast ratio
   [docfile]
 SD 12,[sp_acr]
   [docfile]

 SD /13) Gaussian envelope halfwidth 
   [docfile]
 SD 13,[sp_geh]
   [docfile]

 SD /14) Lambda, A
   [docfile]
 SD 14,[sp_lambda]
   [docfile]

 SD /15) Maximum spatial frequency, 1/A
   [docfile]
 SD 15,[sp_max_spfreq]
   [docfile]

 SD /16) Decimation factor (Usually unused)
   [docfile]
 SD 16,[sp_dec]
   [docfile]

 SD /17) Particle window size,    pixels 
   [docfile]
 SD 17,[sp_winsiz]
   [docfile]

 SD /18) Actual size of particle, pixels
   [docfile]
 SD 18,[sp_partsiz]
   [docfile]

 SD /19) Magnification
   [docfile]
 SD 19,[sp_mag]
   [docfile]

 SD /20) Detector Pixel Size, um (Usually: 7 or 14)
   [docfile]
 SD 20,[sp_scanres]
   [docfile]

 SD E
   [docfile]

 SYS
   echo '  'Reconstruction Parameters saved in: [docfile].$DATEXT
 SYS
   echo ' ' 

 EN
 
; ---------------------------  Unused below here -------------------------------


 ; Test data
 0
 1
 0
 0
 1.1
 300
 400
 368
 112000
 14
 1
 
; Modified 2016-05-24
;    2016-01-14 (trs) -- added option for MRC format