; Fixes refinement if a group(s) is missing
 ;
 ; SOURCE: spider/docs/techs/recon1/Procs/pub-fixrefine.spi
 ;         New                               Sep 2004  ArDean Leith 
 ;         [var]                             Dec 2005  ArDean Leith
 ;
 ; PURPOSE: Fix refinement for missing groups
 ;
 ; Files are set in: refine settings refine-settings.spi)
 ;
 ;  '##' denotes iteration,  '##+' denotes next iteration,  '***' denotes group
 ; INPUT FILES:
 ;    ../params              [params]             Parameter doc file          
 ;    input/sel_group        [sel_group]          Group listing     
 ;    input/data***@         [unaligned_images]   Unaligned stacked image file  
 ;    input/align_##_***     [group_align]        Original alignment parameters doc file 
 ;    input/vol_##           [initial_vol]        Initial volume
 ;
 ; OUTPUT FILES 
 ;    final/vol##+           [next_vol]           Final output volume for each iter.
 ;    final/align##+_***     [next_group_align]   Alignment parameter doc file
 ;    final/fscdoc_##+_***   [next_group_m_fsc]   FSC curve doc file
 ;    jnk_sync_xxxx_***      [finished_file]      Sync doc file from PubSub tasks
 ;
 ; PROCEDURES CALLED:
 ;    refine-settings            refine-settings.spi
 ;    publish                           publish
 ;    ... pub-refine-start      pub-refine-start          
 ;    ....... refine-settings    refine-settings.spi            
 ;    ....... refine-loop            refine-loop.spi            
 ;    ....... refine-smangloop  refine-smangloop.spi            
 ;    pub-refine-wait            pub-refine-wait.spi
 ;    refine-merge                  refine-merge.spi
 ;
 ; -------------------------------- Edit this data --------------------------

 [iter]    = 2    ; Iteration (Set this)

 [rn]      = 0539  ; Random number appended to sync files (Set this)

 [task]    = 0     ; Pub_starter selector flag 
 ;                 ; (0 for regular refinement, 1 for small-angle)

 ; ---------------------------------------------------------------------

 MD
   TR OFF   ; Loop info turned off
 MD
   VB OFF   ; File info turned off

 ; Get global parameters & set filenames
 @refine-settings([pixsiz],[r2],[alignsh],[prj-radius],[iter1],[iter-end],[sphdecon],[smallang-yn],[winsiz],[incore-yn],[prepare-yn],[bp-type])

 [next-iter] = [iter] + 1

 UD N [num-grps]                 ; Find number of groups              
   [sel_group]                   ; Group listing     (input)

 ; Check if reference projections exist
 [s] = 2                  ; Assume s=1 also exists??
 IQ FI [refprojs-exist]
   [ref_projs_s]@

 SYS 
   echo "  Ref projections: [ref_projs_s]   Exist: {%I0%[refprojs-exist]}"

 IF ( [refprojs-exist] == 0) THEN

   ; Set angles used for making reference projections using 'VO EA'. 
   ; Creates: [iter_refangs] doc file for angles

   @refine-refangles([iter],[smallang-yn],[ampenhance],[ang-step],[ang-limit],[num-angs])      

   IF ( [smallang-yn] == 0 ) THEN  
     ; For normal angle refinement
     
     ; Create stacks holding reference projections from both subset reference volumes using 'PJ 3F'
     ; This step can be parallelized within: pub-prjrefs.
     [nparallel] = 1
     @pub-prjrefs([iter],[nparallel],[prj-radius])

   ENDIF
 ENDIF

 ; Process all  groups in parallel

 DO  [grp-key]=1,[num-grps]      ; Loop over all groups --------------------------------

    UD IC [grp-key],[grp]        ; Get this group number
      [sel_group]                ; Group listing     (input)

    DE                           ; Delete file
      [finished_file]{***[grp]}  ; Sync file          (removed)

    ; Check for output volume
    IQ FI [vol-exists]           ; Inquire if file exists
      [next_group_vol]           ; Volume file      (input)           
    MY FL

    IF ( [vol-exists] == 0 ) THEN
      ; If output volume doesn't exist, start refinement for this group
      SYS
        publish './spider spi/$DATEXT @pub-refine-start {***[grp]} task={*[task]} iter={**[iter]} grp={**[grp]} rn={****[rn]}' 

    ELSE
      ; Create a sync file to signal master SPIDER that task already finished
      SD 11, [type]                ; Set sync file output
        [finished_file]{***[grp]}  ; Sync file          (output)
      SD E                         ; Close sync file
        [finished_file]{***[grp]}  ; Sync file          (closed)

   ENDIF
 ENDDO                             ; End of loop over all groups -----------------------

 UD ICE                            ; Close docs
   [sel_group]                     ; Doc file    (closed)

 MY FL                             ; Flush results file

 ;  Wait for all subscribers to finish 
 @pub-refine-wait([num-grps],[iter],[rn])
    [finished_file]
 
 SYS
   echo " Alignment finished for iteration: {%I0%[iter]}"

 EN
 ;