([radius],[maxspfreq],[iter]) ; ; End of refinement
; 
; SOURCE: spider/docs/techs/recon/newprogs/endrefine.pam     
;         New                                  Nov 2000  ArDean Leith
;         Filenames                            Dec 2009  ArDean Leith
;         echo date                            Sep 2010  ArDean Leith
;         softmask call                        Nov 2010  ArDean Leith  
;         softmask call active, nodala         Jan 2012  ArDean Leith  
; 
; PURPOSE:  Corrects group volumes for CTF and merges them into a 
;           final output volume. Computes final reconstruction resolution.
;
; CALLED FROM: refine.pam
;
; INPUT REGISTERS (SENT FROM CALLER):
;    [radius]                   Radius of object 
;    [maxspfreq]                Max. spatial frequency
;    [iter]                     Current iteration
;
;  '##' denotes iteration,  '##+' denotes next iteration, '***' denotes group
; INPUT FILES 
;    [sel_group]                input/sel_group         Group selection file             
;    [temp_ctf_file_template]   work/ctf***             Group CTF correction files                      
;    [group_bpr_template]       final/bpr##+_***        Group volumes-overall    
;    [group_bpr_template]_sub1  final/bpr##+_***_sub1   Group volumes-sub1  
;    [group_bpr_template]_sub2  final/bpr##+_***_sub2   Group volumes-sub2
;
; OUTPUT FILES: 
;    [bpr]                      final/bpr##+            Final volume-overall               
;    [bpr]_sub1                 final/bpr1##+_sub1      Final volumes-sub1        
;    [bpr]_sub2                 final/bpr2##+_sub2      Final volumes-sub2          
;    [dbpr]                     final/dbpr##+           Final doc. file
; 
;......................................................................

[softmask]  = 1            ; Resolution volume masking

[next-iter] = [iter] + 1

VM
echo ' '; echo -n " Starting endrefine, Iteration: {**[iter]} --- "; date '+ %x  %X'

TF CTS                       ; CTF correction & vol. merge
  [group_bpr_template]       ; Group volumes              (input)
  [sel_group]                ; Group file numbers 
  [temp_ctf_file_template]   ; Volume ctf files           (input)
  3.0                        ; SNR
  [bpr]                      ; Final volume-overall       (output)

TF CTS                       ; CTF correction & vol. merge 
  [group_bpr_template]_sub1  ; Group volume-1             (input)
  [sel_group]                ; Group file numbers 
  [temp_ctf_file_template]   ; Volume ctf files           (input) 
  3.0                        ; SNR
  [bpr]_sub1                 ; Final volume-1             (output)

TF CTS                       ; CTF correction & vol. merge 
  [group_bpr_template]_sub2  ; Group volume-2             (input)
  [sel_group]                ; Group file numbers 
  [temp_ctf_file_template]   ; Volume ctf files           (input) 
  3.0                        ; SNR
  [bpr]_sub2                 ; Final volume-2             (output)

IF ([softmask].EQ.1) THEN    ; Gaussian mask for resolution volumes
   VM
   echo " Using spherical softmask for resolution calculation"

   @softmask([radius])       ; Pixel radius for masking
   [bpr]_sub1                ; Final volume-1             (input)
   [bpr]_sub2                ; Final volume-2             (input)
   _13                       ; Inline mask volume         (output)
   [bpr]_sub1                ; Masked volume-1            (output)
   [bpr]_sub2                ; Masked volume-2            (output)

   DE                        ; Delete incore mask volume
   _13                       ; Inline file                (removed)

ENDIF

; Final volume resolution calculation
RF 3 [unused],[fsc]          ; Phase Residual & FSC 
  [bpr]_sub1                 ; Final volume-1             (input)
  [bpr]_sub2                 ; Final volume-2             (input)
  0.5                        ; Ring width
  0.2, 2.0                   ; Scale factor
  C                          ; Missing cone 
  90.0                       ; Max. tilt angle
  3                          ; Factor for noise comparision
  [dbpr]                     ; Resolution doc. file      (output)

; Record final volume resolution in doc. file
[val] = 0
@saveresp([maxspfreq],[iter],[val],[fsc]) 
[dbpr_resol]                 ; Resolution doc. file      (output)

RE
;