; Create new particle selection doc files
 ;
 ; SOURCE:  spider/docs/techs/recon1/Procs/number-parts.spi     Jul 2013 ArDean Leith
 ;
 ; PURPOSE: Create new particle selection doc files listing the verified particle numbers.
 ;          Optionally stores project ID, micrograph number, original window number, and
 ;          a new global particle number in image file header. 
 ;          This procedure does NOT restack images, it just creates particle selection doc files.
 ;
 ; USAGE:   clean ; spider spi/dat @number-parts

 ; ------------------- Parameters -------------------

 [projid]         = 1      ; Project id number  (arbitrary)

 [first-global]   = 1      ; First global particle number

 [max-particles]  = 0      ; Max number of particles/micrograph (0 == keep all)

 [labelheader-yn] = 1      ; Put label in the image header? (0 == no)

 ; --------------------- Inputs ---------------------

 [sel_micrograph] = '../sel_micrograph'        ; Micrograph selection list   (one)

;[old_sel_part]   = 'win/sel_part_****'        ; Original        particle selection files (one/micrograph)
 [old_sel_part]   = 'win/sel_good_****'        ; Visually picked particle selection files (one/micrograph)

 [win_stacks]     = 'win/data_bymic_****'      ; OPTIONAL Micrograph stacks (Used if labelling image header)

 ; --------------------- Output ---------------------

 [good_dir]       = 'good'                     ; New selection doc files directory (one)

 [new_sel_part]   = '[good_dir]/sel_part_****' ; Particle selection doc files  (one/micrograph)

 [micwin2global]  = 'win/micwin2glonum_****'   ; Global particle lookup tables (one/micrograph)

 [global2micwin]  = 'win/glonum2micwin'        ; Global particle lookup tables (one)
 
 [summary_doc]    = 'percent_selected'         ; Summary doc file              (one)

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

 ; Temporary file
 [temp_old_doc_nodupes] = 'tmpgood_nodupes'

 DE                                 ; Clean up pre-existing file
   [summary_doc]                    ; Doc file        (removed)

 SD /       MIC_NUM     KEPT_PARTS    TOTAL_PARTS      %_KEPT
   [summary_doc]                    ; Doc file        (output)

 DE                                 ; Clean up pre-existing file
   [global2micwin]                  ; Doc file        (removed)

 SD / KEY: GLOBAL_NUM   REG: MIC-NUM   MIC-WIN 
   [global2micwin]                  ; Global particle lookup table (output)

 ; Initialize global-particle counter
 [global-num] = [first-global] - 1

 IF ( [labelheader-yn] >= 1) THEN
   SYS
     echo "  Placing global particle # in image header" ; echo 
 ELSE
   SYS
     echo "  Not placing global particle # in image header" ; echo 
 ENDIF

 SYS                                 ; Create directory if needed
   mkdir -p [good_dir] 

 ; Get number of micrographs
 UD N [num-mics]                     ; Get number
   [sel_micrograph]                  ; Doc file        (input)  

 DO [mic-key]=1,[num-mics]           ; Loop over all all micrographs ----------

   UD IC [mic-key], [mic]            ; Get micrograph number
     [sel_micrograph]                ; Micrograph selection file (input)

   ; Delete pre-existing files and label new files
   DE
     [micwin2global][mic]            ; Doc file        (removed)
   ; Label for new global number doc file
   SD / KEY: WIN_NUM    REG:  GLOBAL-NUM    MICROGRAPH  
     [micwin2global][mic]            ; Doc file        (output)

   DE
     [new_sel_part][mic]             ; Doc file        (removed)
   SD /         WIN         MIC_WIN         MIC      GLOBAL_NUM        GRP
     [new_sel_part][mic]             ; Doc file        (output)

   ; Get number of particles including duplicates
   UD N [tot-parts]
     [old_sel_part][mic]             ; Doc file        (input)

   ; Remove duplicates and sort
   AT IT                             ; Sort doc file
     [old_sel_part][mic]             ; Doc file        (input)
     [temp_old_doc_nodupes]          ; Doc file        (output)

   ; Get # particles kept
   UD N [num-particles]              ; Get # particles
     [temp_old_doc_nodupes]          ; Doc file        (input)   

   ; Limit # particles/micrograph if desired
   IF ([max-particles] > 0) THEN
     IF ( [num-particles] > [max-particles] ) [num-particles] = [max-particles]
   ENDIF

   ; Loop through all particles for this micrograph --------------------
   DO [p-key] = 1,[num-particles]

     ; Read micrograph window #
     UD IC [p-key], [win]
       [temp_old_doc_nodupes]        ; Doc file        (input)               ;  

     ; Increment global #
     [global-num] = [global-num] + 1

     ; Write global # micrograph to global-particle lookup table
     SD [p-key], [global-num], [mic]
       [micwin2global][mic]          ; Doc file        (output)
        
     SD [global-num],[mic],[win]
       [global2micwin]               ; Global particle lookup table (output)

    ; Write details to particle selection file
     SD [p-key],[p-key],[win],[mic],[global-num],  [mic]
       [new_sel_part][mic]                 ; Doc file        (output)
        
     IF ( [labelheader-yn] >= 1 ) THEN

        ; Put global # into image header
        ST H                               ; Set header values
          [win_stacks]@****[mic][p-key]    ; Image file   (input)
          PROJ,MIC,NUM,GLONUM              ; Header locations
          [projid],[mic],[win],[global-num]  ; Values

     ENDIF
   ENDDO                              ; End particle-loop

   ; Close docs
   UD ICE                             ; Close document file
     [temp_old_doc_nodupes]           ; Doc file        (closed) 
   SD E                               ; Close document file
     [micwin2global][mic]             ; Doc file        (closed)
   SD E                               ; Close document file
     [new_sel_part][mic]              ; Doc file        (closed)
   DE                                 ; Delete document file
     [temp_old_doc_nodupes]           ; Doc file        (removed)

   ; Count # of particles
   UD N [kept]                        ; Get # of particles
     [new_sel_part][mic]              ; Doc file        (input)

   SYS
     echo "  Kept: {%I6%[kept]} particles from micrograph: {%I4%[mic]}"

   [percent-kept] = 100 * [kept]/[tot-parts]
   ; Maybe should trap for zero someday

   ; Write to summary doc
   SD [mic-key], [mic],[kept],[tot-parts],[percent-kept]
     [summary_doc]                  ; Doc file        (output)
 ENDDO                              ; End micrograph loop

 UD ICE                             ; Close document file
   [sel_micrograph]                 ; Doc file        (closed)

 SD /          MICROGRAPHS   TOTAL_PARTS
   [summary_doc]                    ; Doc file        (output)
 SD -1, [num-mics],[global-num]
   [summary_doc]                    ; Doc file        (output)
 SD E                               ; Close document file
   [summary_doc]                    ; Doc file        (output)
 SD E                               ; Close document file
   [global2micwin]                  ; Doc file        (closed) 

 SYS
   echo ; echo "  Kept: {%I0%[global-num]} particles from: {%I0%[num-mics]} micrographs" ; echo 

 EN

 ; Modified 2013-10-26
 ;    2013-10-26 (agl) -- Added current group col. to particle selection files
 ;    2013-10-16 (agl) -- Syntax modernized, file names changed
 ;    2012-07-19 (trs) -- Creates output directory if needed
 ;    2012-05-01 (trs) -- Optionally writes global particle number to header position 42
 ;    2012-04-05 (trs) -- Doesn't use output from pnums.spi anymore
 ;    2012-04-05 (trs) -- Adapted from listallparticles.spi
 ;    2010-09-02 (trs & ay) --  Trap for >999 micrographs for 'SD [dummy]' line
 ;    2009-10-16 (trs) -- Removed option for unstacked images
 ;    2008-10-03 (trs) -- Added output file micwin2global
 ;    2008-10-03 (trs) -- Assigns global particle number
 ;    2007-12-17 (trs) -- Optional parameter for maximum number of particles per micrograph
 ;    2007-10-10 (trs) -- Gets number of micrographs from SEL_MICROGRAPH 
 ;