; Apply CTF correction
;
; SOURCE:   ctf.spi
;
; PURPOSE: Apply CTF correction to all the defocus group odd/even volumes,
;          then compute the FSC resolution curve for the combined data set.
;
;          This batch file produces the initial volume, vol001, which is 
;          copied to the Refinement/input  directory.
;
; I/O PARAMETERS AND FILES ARE SET HERE:
;
;  ----------------- I/O PARAMETERS --------------------------------

x73 = 3                  ; SNR for CTF correction (TF CTS)

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

FR G
[params]../params          ; Parameter file

FR G
[defgrps]sel_group_cclim   ; Selection file for defocus groups

FR G
[voleven]vol001_even       ; "even" volumes

FR G
[volodd]vol001_odd         ; "odd" volumes

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

FR G
[ctf]ctf/ctf               ; Ctf file stem

FR G
[res]combires              ; Resolution curve for combined output

FR G
[vol]vol001                ; CTF-corrected volume of combined data set

FR G
[volcopy]../Refinement/input/vol01  ; Copy of volume used for refinement

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

MD
SET MP
0              ; Use all processors available

VM             ; Make the ctf output directory
mkdir -p ctf  
VM             ; Make the Refinement directory
mkdir -p ../Refinement  
VM             ; Make the Refinement/input directory
mkdir -p ../Refinement/input  

FR G                    
[tmp]tmpres88  ; Temp. file (deleted)

DE
[res]

; Get parameters
UD 5,x41                 ; Pixel size
[params]
UD 6,[kv]                ; KV
[params]
UD 7,[cs]                ; Cs
[params]
UD 8,[ss]                ; Source size
[params]
UD 9,[defspr]            ; Defocus spread
[params]
UD 12,[acr]              ; ACR
[params]
UD 13,[genv]             ; Gaussian envelope
[params]
UD 17,[winsiz]           ; Window size
[params]

[lambda] = 12.398 / SQR([kv] * (1022.0 + [kv])) ; Lambda
[maxspf] = 1.0 / (2.0 * x41)                    ; Max. spatial. freq.

;  Create CTF file for each defocus group

UD N,[numgrps]           ; Find number of defocus groups
[defgrps]                ;                                 (input)

DO LB1 [numgrp] = 1,[numgrps]   ; Loop over all defocus groups

   UD [numgrp],[grp],[numparts],[def]  ; Get current group number 
   [defgrps]             ; Group selection file            (input)

   TF C3                 ; Create a model CTF (3D, complex)
   [ctf]{***[grp]}       ; Output file                     (output)
   [cs]                  ; Cs
   [def],[lambda]        ; Defocus, lambda
   [winsiz]              ; Dimension of volume
   [maxspf]              ; Max. spatial freq.
   [ss],[defspr]         ; Source size, defocus spread
   (0,0)                 ; Astigmatism, azimuth
   [acr],[genv]          ; Amplitude contrast ratio, Gaussian halfwidth
   (-1)                  ; Sign
LB1

; The 'TF CTS' command works on all defocus groups at once
TF CTS                   ; CTF correction
df***/[voleven]          ; Template for image file       (input)
[defgrps]                ; Group selection file          (input)
[ctf]***                 ; Template for ctf file         (input)
x73                      ; SNR
_1                       ; Temp. inline file             (output)

TF CTS
df***/[volodd]           ; Template for image file       (input)
[defgrps]                ; Group selection file          (input)
[ctf]***                 ; Template for ctf file         (input)
x73                      ; SNR
_2                       ; Temp. inline file             (output)

; Add the odd and even volumes together to make combined volume
AD       
_1
_2
[vol]                    ; Volume                        (output) 
*

; Compute the combined resolution curve
DE
[tmp]

RF 3                     ; Compute resolution curve
_1                       ; First input volume
_2                       ; Second input volume 
(0.5)                    ; Ring width 
(0.2,2.0)                ; Lower, upper scale factors
C                        ; C = missing cone    
(90.0)                   ; Max. tilt angle
(3)                      ; Factor for noise comparison
[tmp]                    ; Output doc file              (output)

DOC REN                  ; Renumber the doc file
[tmp]
[tmp]                    ; Doc file                    (output)

UD N,[nline]             ; Determine number of lines in doc file
[tmp]                    ; Doc file                    (input)

SD /    NORM'D FREQ    DPR        FSC        FSCCRIT     VOXELS
[res]                     ; Doc file                   (output)

DO LB2 [line] = 1,[nline]
   UD [line], [v62],[v63],[v64],[v65],[v66]
   [tmp]                  ; Doc file                    (input)
   SD [line], [v62],[v63],[v64],[v65],[v66]
   [res]                  ; Doc file                    (output)
LB2

DE
[tmp]

CP                        ; Copy initial volume to refinement dir
[vol]                     ; Volume                     (output)
[volcopy]                 ; Volume                     (output)

VM                                      
echo ' 'Created CTF-corrected volume of combined data set: [vol] 
VM                                      
echo ' '

EN
;