; Multiplies reference volume by the CTF & creates ref projections
;
; SOURCE: refproj.spi
;
; PURPOSE:  Creates reference projections from a reference volume.
;           If desired, separate references can be created for each
;           defocus group. In that case, the reference volume is first
;           multiplied by the CTF for each defocus group 
;           (Set [usectf] = 1).
;
;           If [usectf] = 0, then a single set of reference projections 
;           is created, with no modification by the CTF.
;
; The following values are retrieved from the params file
;   [numgrp]   = Number of defocus groups
;   [pix_size] = Pixel size, Angstroms
;   [pixels]   = Array dimension, pixels
;   [ee]       = Electron energy
;   [spab]     = Spherical aberration
;   [ss]       = Source size
;   [defspr]   = Defocus spread
;   [acr]      = Amplitude contrast ratio
;   [geh]      = Gaussian envelope halfwidth
;
;  ------------ Parameters ---------------------------------------

[dtheta] = 15  ; Delta theta: 15 gives 83 projections
[radius] = -1  ; Radius: use -1 for default value

[usectf] = 1   ; Multiply references by CTF (0= Do not, use non-CTF references)  

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

FR G
[params]../params                       ; The parameter doc. file

FR G
[refvol]../reference_volume             ; Reference volume

FR G
[defocus]../Power_Spectra/order_defgrps ; Defocus group doc. file

; --------------- Output files  -------------------------------------
FR G
[refangles]refangles       ; Doc file of projection angles

FR G
[proj_dir]projs            ; Dir for projection stacks 

FR G
[prj][proj_dir]/prj_       ; Template for 2D projections (stack files)

FR G
[usectf_file]ctf_refs      ; Signal File indicates refs are multiplied by CTF


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

MD
TR OFF                     ; Decrease results file output
MD
VB OFF                     ; Decrease results file output

MD 
SET MP 
0                          ; 0 = use all processors available  

VM
mkdir -p [proj_dir]

; Delete output document files and get parameters
DE
[refangles]
DE
[usectf_file]

IF ([radius].LT.0) THEN    ; Get actual size and compute radius
   UD 18,[size]
   [params] 
   [radius] = 0.69 * [size]
ENDIF

IF ([usectf].GT.0) THEN
   ; Set "flag" file for 'AP SH'. If this file exists, use CTF-multiplied refs
   SD 1,[usectf]
   [usectf_file]

   UD 5,[pix_size]         ; Pixel size
   [params]
   [spfreq]=0.5/[pix_size] ; Max. spatial frequency

   UD 6,[ee]               ; Electron energy
   [params]
   [lambda] = 12.398 / SQR([ee] * (1022.0 + [ee]))  ; Lambda

   UD 7,[spab]             ; Spherical aberration
   [params]
   UD 8,[ss]               ; Source size
   [params]
   UD 9,[defspr]           ; Defocus spread
   [params]
   UD 12,[acr]             ; Amplitude contrast ratio
   [params]
   UD 13,[geh]             ; Gaussian envelope halfwidth
   [params]
   UD 17,[pixels]          ; Window size
   [params]

   UD N [numgrp]           ; Number of defocus groups
   [defocus]
ENDIF

; Create the angles document files
VO EA [numang]
[dtheta]                   ; Delta theta
0,0                        ; Range of theta
0,0                        ; Range of phi
[refangles]                ; Output doc file of angles

[numang] = [numang] - 1

;------------------------------------------
; For each defocus group, multiply the reference volume by the
; appropriate defocus, then use this ctf-reference to generate
; 83 projection images

IF ([usectf].GT.0) THEN
   ; CTF correction
   FT                        ; Fourier transform  
   [refvol]                  ; Reference volume                (input)
   _2                        ; Fourier of reference volume     (output)

   DO LB1 [iter]=1,[numgrp]  ; Loop over all defocus groups
      UD [iter], [grp],[def] ; Get defocus group & defocus value
      [defocus]

      TF C3                ; Transfer Function - Complex 3D 
      _7                   ; Complex model CTF                 (output)
      [spab]               ; Spherical aberration
      [def],[lambda]       ; Defocus, lambda
      [pixels]             ; Array dimension
      [spfreq]             ; Maximum spatial frequency
      [ss],[defspr]        ; Source size, defocus spread
      (0,0)                ; Astigmatism, azimuth
      [acr],[geh]          ; Amplitude contrast ratio, envelope 1/2-width
      (-1)                 ; Sign

      MU                   ; Multiply
      _2                   ; Fourier transform of ref. volume  (input)
      _7                   ; Complex model CTF                 (input)
      _4                   ; CTF multiplied Fourier volume     (output)
      *

      FT                   ; Reverse Fourier transform
      _4                   ; CTF multiplied Fourier volume     (input)
      _7                   ; CTF_multiplied volume             (output)

      VM
      echo ' 'Group: {***[grp]} Created: {***[numang]} CTF corrected projections

      ; Projection
      PJ 3Q                ; Generate projections of a volume
      _7                   ; Volume to be projected           (input)
      [radius]             ; Radius
      (1-[numang])         ; List of file numbers
      [refangles]          ; Angles doc file                  (input)
      [prj]{***[grp]}@**** ; Template for projection stack    (output)
   LB1

ELSE
   ; No CTF correction i.e. single defocus group

   [grp]=1                 ; Group number

   VM
   echo  ' 'Creating: {***[numang]} reference projections

   ; Projection
   PJ 3Q                   ; Generate projections of the volume
   [refvol]                ; Volume to be projected          (input)
   [radius]                ; Radius
   (1-[numang])            ; List of file numbers
   [refangles]             ; Angles doc file                 (input)
   [prj]{***[grp]}@****    ; Template for projection stack   (output)
ENDIF
VM
echo ' '   

EN 
;