; Controls reconstruction
;
; FILE: spider/docs/techs/recon/newprogs/pub_recon.pam
;       New                                       ArDean Leith Dec 2009
;
; PURPOSE: Runs on one node to control and reconstrtuction
;
; INPUT 
;   [params]      ../params                          ; Reconstruction parameter file
;   [sel_grp]     sel_group_cclim                    ; Defocus group selection file
;   [sel_part]    sel_particles_{***[grp]}           ; Particle selection files (one / defocus group)
;   [ali]         ../Alignment/dala01_{***[grp]}@**  ; Aligned particle images (one stack / defocus group)
;   [angles]      ../Alignment/align_01_{***[grp]}   ; Alignment parameter doc files (one / defocus group)
;
; OUTPUT:
;   [selodd]      df{***[grp]}/sel_odd         ; Selection files for odd-numbered particles (one / defocus group)
;   [seleven]     df{***[grp]}/sel_even        ; Selection files for even-numbered particles (one / defocus group)
;   [volodd]      vol01_odd                    ; Volume created from "odd" particles
;   [voleven]     vol01_even                   ; Volume created from "even" particles
;   [grpvolodd]   df{***[grp]}/vol01_odd       ; Volumes for even-numbered particles (one per group)
;   [grpvoleven]  df{***[grp]}/vol01_even       ; Volumes for odd-numbered particles (one per group)
;   [grpfsc]      df{***[grp]}/fscdoc          ; FSC doc files (one per group)
;   [ctf]         df***/ctffile                ; CTF correction files (one per group)
;   [vol]         vol01                        ; CTF-corrected combined volume  
;   [combfsc]     combires                     ; FSC doc file with FSC curve for combined volume
;   [res_file]    resolution                   ; Resolution doc file for combined volume
;
; PROCEDURES CALLED:
;    recon                    recon.spi
;    publish                  publish
;
; -------------------- Inpub files ( From recon.spi) ---------------------------

[sel_grp]    = 'sel_group_cclim'                       ; Defocus group selection file

; -------------------- Output files ( From recon.spi) ---------------------------

[ctf]     = 'df{***[grp]}/ctffile'  ; CTF correction files (completion flag)
[combfsc] = 'combires'              ; FSC doc file with FSC curve for combined volume

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

MD
TR OFF                            ; Loop info turned off
MD
VB OFF                            ; File info turned off
MD
() OFF                            ; No need for () in DO loops          
MD
SET MP                            ; Use single OMP processor
1

DE                                ; Delete existing file
[combfsc]                         ; FSC doc file file used as completion flag

DO   ; ---------------------------  Loop starts group back projections --------------

   UD NEXT [key],[grp],[p],[def]  ; Get group from group sel. file
   [sel_grp]                      ; Group selection file       (input)
   IF ([key] .LE. 0) EXIT         ; End of groups in selection file

   DE                             ; Delete existing file
   [ctf]                          ; CTF correction file used as completion flag

   VM                             ; Process all defocus groups in parallel
   publish './spider spi/$DATEXT @recon {***[grp]}  grp={***[grp]}' 
      
ENDDO
UD NEXT END                       ; Reset group sel. file
[sel_grp]                         ; Group selection file       (ends)

VM                                ; Echo start time
date '+ TIME: %x  %X'

DO   ; ---------------------------  Loop waits for group back projections ------------

   UD NEXT [key],[grp]            ; Get group from group sel. file
   [sel_grp]                      ; Group selection file       (input)
   IF ([key] .LE. 0) EXIT         ; End of groups in selection file

   VM
   echo " Waiting for group: {**[grp]}"
   MY FL                          ; Flush results file

   ;  Wait for all subscribers to finish back projections
   IQ SYNC                        ; Wait for CTF file creation 
   [ctf]
   (10 3600000)

   VM
   echo " Finished group:    {**[grp]}"
   VM
   date '+ TIME: %x  %X'
ENDDO

; --------------------------------- Start volume combination & resolution calc. -----

VM                                ; Combine group volumes and find resolution
publish './spider spi/$DATEXT @recon 0  grp=-99' 
      
VM
date '+ TIME: %x  %X'

IQ SYNC                           ; Wait for FSC file creation signal 
[combfsc]                         ; Doc file                   (output)
(10 3600000)

VM
date '+ TIME: %x  %X'
VM
echo " RECONSTRUCTION FINISHED"

EN
;