; Make selection doc files reference views
 ;
 ; SOURCE:   spider/docs/techs/recon/Procs/select-byview.spi
 ;
 ; PURPOSE:  Make selection doc files for reference views
 ;
 ; USAGE:    clean ; ./spider spi/dat @select-byview

 ; REQUIRES: spider/docs/techs/recon/Procs/verify-settings.spi
 ;
 ; INPUT FILES: ([win_dir] denotes input directory, 
 ;               [rec_dir] denotes reconstruction output directory and '***' denotes group)
 ;   [sel_group]      [win_dir]/sel_group               Group selection doc file      (one)
 ;   [sel_part]       [win_dir]/sel_part_***            Particle selection doc file   (one/group)
 ;   [iter_refangs]   [rec_dir]/ref_angs_01             Reference angles doc file     (one)                  
 ;   [align_doc]      [rec_dir]/align_01_***            Particle alignment doc files  (one/group)
 ;
 ; OUTPUTS:   (Where ### denotes projection view)
 ;   [view_list]        views/prj###/sel_part_byv       By-view particle list         (one/view)
 ;   [sorted_particles] views/prj###/sel_part_byv_sort  Particle list sorted by CCROT (one/view)
 ;   [parts_vsview]     views/parts_vsview_all          List of particles per view    (one)
 ;
 ;
 ; ----------- END BATCH HEADER ----------------

 MD
   SET MP
   0 
 MD
   VB OFF

 ;[view-progress] = 1

 ; Set common filenames & parameters
 @verify-settings

 UD N [num-views]
   [ref_view_list]                ; Reference view angles (input)
 SYS
   echo "  Separating particles for: {%I0%[num-views]} reference views" ; echo

 SYS                                 ; Make sure output dir. present
   mkdir -p [view_dir] 

 ; Merge alignment parameter doc files into overall file

 DE                                  ; So can not append to any pre-existing file
   [parts_vsview]                    ; # particles / view summary file   (removed)
 ; Lable summary file
 SD /   #-OF-PARTS 
   [parts_vsview]                    ; # particles / view summary file   (output)
 
 ; Initialize variables
 [max-imgs]       = -1
 [min-imgs]       = 9999999
 [combined-parts] = 0
 
 DO  [view] = 1,[num-views]          ; Loop over all view numbers ----------------------

   SYS
     mkdir -p [prj_dir][view]

   [view-parts] = 0

   DO                                ; Loop over all groups ----------------------------
     UD NEXT [gkey], [grp]
       [sel_group]                   ; Group selection file (input)
     IF ( [gkey] <= 0 ) EXIT

     DO                              ; Loop over particles in this group --------------
       UD NEXT [pkey],[grp-win], [mic],[mic-win],[glo-num] 
         [sel_part][grp]             ; Particle selection file     (input)
       IF ( [pkey] <= 0 ) EXIT

       UD IC [grp-win], [r1],[r2],[r3],[ref],[exp],[r6],[r7],[r8],[r9],[r10],[r11],[ccrot],[r13],[r14],[mir]
         [align_doc][grp]            ; Particle alignment doc files  (input)

       IF ( [ref] == [view] ) THEN
         ; Particle uses this view

         IF ( [view-parts] == 0 ) THEN
           ; First partilcle for this view, clean existing files and lable headers

           DE                         ; Delete
             [view_list][view]        ; Particle selection for view doc file (output)
           ; First particle in view, label columns
           SD /           NUM       GLO_NUM      GRP_WIN        CCROT      MIRROR_FLAG    GRP_NUM        VIEW        MIC
             [view_list][view]        ; Particle selection for view doc file (output)
           SD E
             [view_list][view]        ; Particle selection for view doc file (closed)

           DE                         ; Delete
             [sorted_particles][view] ; Particle selection for view doc file (output)
           SD /           NUM       GLO_NUM      GRP_WIN        CCROT      MIRROR_FLAG    GRP_NUM        VIEW        MIC
             [sorted_particles][view] ; Sorted particle selection for view doc file (output)
           SD E
             [sorted_particles][view] ; Sorted particle selection for view doc file (closed)
           
         ENDIF

         [view-parts] = [view-parts] + 1

         ; Set mirror flag
         [mirror] = 0
         IF ( [mir] < 0 ) [mirror] = -1
        
         ; Write to view list doc file
         SD  [view-parts], [view-parts],[glo-num],[grp-win],[ccrot],[mirror],[grp],[view],[mic]
           [view_list][view]         ; Particle selection for view doc file (output)
         
       ENDIF
     ENDDO                           ; End: Loop over particles in this group --------------

     ; Close documents
     UD NEXT E
       [sel_part][grp]               ; Particle selection file      (closed)
     UD IC E
       [align_doc][grp]              ; Particle alignment doc files (closed)
     SD E
      [view_list][view]              ; Particle selection for view doc file (closed)

     ; Update min, max
     IF ( [view-parts] > [max-imgs] ) [max-imgs] = [view-parts]
     IF ( [view-parts] < [min-imgs] ) [min-imgs] = [view-parts]


   ENDDO                             ; End: Loop over all groups ---------------------------

   UD NEXT E
       [sel_group]                   ; Group selection file (closed)

   ; Check if particle-list exists
   IQ FI [list-exists]
      [view_list][view]              ; Particle selection for view doc file (input)

   IF ( [list-exists] == 1) THEN
      ; Skip if particle-list does not exist

      ; Sort by CC
      DOC SORT A                     ; Sort doc file & append to existing lable
        [view_list][view]            ; Particle selection for view doc file  (input)
        [sorted_particles][view]     ; Particle selection for view doc file  (output)
        -4                           ; Column # to sort by CCROT, Reverse order
        Y                            ; Renumber the keys
   ENDIF
 
   IF ( [view-parts] > 0 ) THEN   
     ; Update  combined counter
     [combined-parts] = [combined-parts] + [view-parts]

     ; Write to summary
     SD [view], [view-parts]
        [parts_vsview]               ; # particles / view summary file   (output)
   ENDIF

   IF ( int( [view]/[view-progress] ) == [view]/[view-progress] ) THEN
      SYS
        echo "  View: {%I3%[view]}   Particles: {%I6%[view-parts]}"
   ENDIF

 ENDDO                              ; End: Loop over all view numbers ------------------
  
 ; Finish summary file
 SD /   #VIEWS  TOTPARTS       MAXIMGS       MINIMGS
    [parts_vsview]          ; # particles / view summary file   (output)
 [dummy] = -[num-views]
 SD [dummy], [combined-parts],[max-imgs],[min-imgs]
    [parts_vsview]          ; # particles / view summary file   (output)
 SD E
    [parts_vsview]          ; # particles / view summary file   (closed)

 SYS
   echo ; echo -n "  Done    "; date

 EN

 ; Modified 2016-3-25
 ;    2016-03-25 (agl) -- New file names, MODERNIZED SYNTAX & COSMETIC
 ;    2013-10-23 (agl) -- New file names, modernized syntax & cosmetic
 ;    2012-04-11 (trs) -- Prints min & max populated views in HOW_MANY file
 ;    2012-04-10 (trs) -- uses combined alignment doc as input
 ;    2012-04-06 (trs) -- Adapted from selectbyviewall.spi
 ;    2011-11-02 (trs) -- Added output: combined HOW_MANY file
 ;    2011-10-21 (trs) -- Do not need reversedoc_7col anymore 
 ;    2011-10-21 (trs) -- Updated to named registers
 ;    2009-04-22 (trs) -- Using 'SD IC'
 ;    2009-04-21 (trs) -- Added version of select/sel sorted by CCROT
 ;    2009-02-23 (trs) -- Added view# to select/sel output
 ;    2008-04-24 (trs) -- Number of reference projections now an input parameter
 ;    2008-01-30 (trs) -- Last angle in refangles not necessarily redundant
 ;    2007-08-07 (trs) -- Reflects changes to input doc file sel_part
 ;    2007-06-06 (trs) -- Bug fix cleaning out pre-existing files, format change
 ;    2007-05-30 (trs) -- Reflects changes to input doc file sel_part
 ;    2007-03-27 (trs) -- Reflects changes to input doc file sel_part
 ;    2007-02-20 (trs & djt) -- Projlist now an output
 ;    2007-02-20 (trs & djt) -- Bug fix: group directories not correctly created

 ;