; Plots number of particles vs_defocus
 ;
 ; SOURCE:   spider/docs/techs/recon1/Procs/parts-vs-defocus.spi
 ;
 ; PURPOSE:  Sorts particles by micrograph and thus defocus. 
 ;           Tabulates number of particles versus defocus level.
 ;           Plots number of particles vs defocus
 ;
 ; USAGE:    clean ; ./spider spi/dat @parts-vs-defocus
 ;
 ; ----------------- Parameters -----------------

 [viewplot-yn]        = 1       ; Open Gnuplot automatically? (0 == no)

 [progress-interval]  = 100     ; Print progress message to screen every Nth particle

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

 [sel_mic]           = '../Power_Spectra/defocus'         ; Micrograph selection file       (one)

 [sel_part_combined] = 'sel_part_combined_good'           ; Combined list of all particles  (one)

 [mic_sel_part]      = '../Particles/good/sel_part_****'  ; Initial particle selection doc file (one/micrograph)

 [global2grp_lut]    = '../Particles/win/glonum2micwin'   ; Global particle look-up table   (one)

 ; ------------------- Outputs -------------------

 [sel_part_bymic]    = '../Micrographs/sel_part_bymic_****'  ; New particle selection doc file (one/micrograph)

 [good_parts_bymic]  = 'parts_vsmic'                         ; Summary doc file            (one)

 [gnuplot_script]    = 'parts_vsdefocus.gnu'                 ; Gnuplot script              (one)

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

 MD
   TR OFF                                                  ; Decrease results file output
 MD
   VB OFF                                                  ; Decrease results file output


 [good_parts_bymic_sorted] = '[good_parts_bymic]_sorted'   ; Sorted summary doc file     (one)

 SYS
    echo -n "  Making good particle list for micrographs    "; date ; echo

 ; Clean up pre-existing files 
 DE                              ; Delete
   [good_parts_bymic]            ; Summary doc file             (deleted)

 ; Put header in output doc file 
 SD /       MIC_NUM       DEFOCUS      PARTSGOOD     PARTS_ALL
   [good_parts_bymic]            ; Summary doc file             (output)
 
 [goodtotal]     = 0
 [combinedparts] = 0
 [mic-counter]   = 0

 DO                              ; Loop over all micrographs ----------------------------
   UD NEXT [mkey], [mic],[defocus]
     [sel_mic]                   ; Micrograph selection file (input)
   IF ( [mkey] <= 0 ) EXIT

   [mic-parts] = 0
   [outkey]    = 0

   DO                            ; Loop over particles ----------------------------------

    ;   VIEW_WIN   GLOBAL_NUM  GRP_WIN  CCROT   MIRROR    GRP_NUM   VIEW   MIC
    UD NEXT [pkey],[vwin], [glonum] ,[grpwin],[ccr],[mir],[grp],[view],[micnow] 
       [sel_part_combined]       ; Particle  file            (input)
    IF ( [pkey] <= 0 ) EXIT

    IF ( [micnow] == [mic] ) THEN
       ; Particle from this micrograph

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

         DE                         ; Delete
           [sel_part_bymic][mic]    ; Selection doc file (deleted)
 
         ; First particle in mic, label columns
         SD /         WIN          MIC_NUM       MIC_WIN      GLOBAL_NUM        GRP
           [sel_part_bymic][mic]    ; Selection doc file (output)

         [outkey] = 0
       ENDIF

       [outkey]    = [outkey] + 1
       [mic-parts] = [mic-parts]+ 1
       
       ; Refer to global particle look-up table   
       ;  KEY: GLOBAL_NUM   REG: MIC-NUM,   MIC-WIN
       UD IC [glonum], [mic-num], [mic-win] 
          [global2grp_lut]           ; Global particle look-up table      (one)
 
       ; Write to sel particle list
       ;   WIN   MIC_NUM  MIC_WIN  GLOBAL_NUM   GRP
       SD  [outkey], [mic-win],[mic],[mic-win],[glonum],[grp]
         [sel_part_bymic][mic]      ; Selection doc file (output)

     ENDIF
   ENDDO                            ; End of: Loop over particles ----------------------------------

   [goodtotal] = [goodtotal] + [mic-parts] ; Overall total parts

   SYS
     echo "  Kept: {%I7%[mic-parts]} particles from micrograph: {%I4%[mic]} " 

   [mic-counter] = [mic-counter] + 1

   ; Get number of particles before verification
   UD N [tot-parts]
     [mic_sel_part][mic]

   ; Increment combined particle counter
   [combinedparts] = [combinedparts] + [tot-parts]

   SD  [mic-counter], [mic], [defocus],[mic-parts],[tot-parts]
     [good_parts_bymic]            ; Summary doc file (output)
  
   ; Close docs
   UD ICE
     [sel_part_bymic][mic]         ; Selection doc file (closed)

 ENDDO                             ; End of: Loop over all micrographs ----------------------------

 SD E                              ; Close
   [good_parts_bymic]              ; Summary  file                (closed)

 DE  
   [good_parts_bymic_sorted]       ; Sorted summary  file         (deleted)
 SD /       MIC_NUM       DEFOCUS      PARTSGOOD     PARTS_ALL
   [good_parts_bymic_sorted]       ; Sorted summary  file         (output)
 SD E                              ; Close
   [good_parts_bymic_sorted]       ; Sorted summary  file         (output)

 DOC SORT A                        ; Sort doc file into existing labeled file
   [good_parts_bymic]              ; Summary  file                (input)
   [good_parts_bymic_sorted]       ; Sorted summary  file         (output)
   2                               ; Register to sort by: defocus
   Y                               ; Renumber


 ; Write to summary doc
 SD /            NUMMICS     SAVED_PARTS  INITIAL_PARTS     
   [good_parts_bymic]              ; Summary  file                 (output)
 [dummy] = -[mic-counter]
 SD [dummy], [mic-counter],[goodtotal],[combinedparts]
   [good_parts_bymic]              ; Summary  file                (output)
 SD E
   [good_parts_bymic]              ; Summary  file                (closed)


 ; Close docs
 UD ICE
   [sel_part_combined]             ; Particle  file                (closed)
 UD NEXT E
   [sel_mic]                       ; Micrograph selection file     (closed)
 UD ICE 
   [global2grp_lut]                ; Global particle look-up table (closed)
 SYS
   echo "  Kept: {%I7%[goodtotal]} particles from: {%I0%[mic-counter]} micrographs" ; echo
 

 ; GENERATE GNUPLOT SCRIPT -------------------------------------------------------------------

 ; Delete pre-existing file
 SYS
   rm -f [gnuplot_script]

 ; Will use arbitrary numbers in case version <18.20
 SYS
   echo 'set xlabel "Micrograph (sorted by defocus)"' > [gnuplot_script]
 SYS
   echo 'set ylabel "#Particles"' >> [gnuplot_script]
 SYS
   echo 'set boxwidth 0.95 relative ' >> [gnuplot_script]
 SYS
   echo 'set style data histogram' >> [gnuplot_script]
 SYS
   echo 'set style fill solid'   >> [gnuplot_script]
 SYS
   echo 'set key outside box'    >> [gnuplot_script]
 SYS
   echo 'plot [] [0:] \'         >> [gnuplot_script]
 SYS
   echo '"[good_parts_bymic_sorted].$DATEXT" using 1:6               title "total" with boxes, \' >> [gnuplot_script]
 SYS
   echo '"[good_parts_bymic_sorted].$DATEXT" using 1:5               title "good"  with boxes' >> [gnuplot_script]
;;   echo '"[good_parts_bymic_sorted].$DATEXT" using 1:5:xticlabels(3) title "good"  with boxes' >> [gnuplot_script]

 ; If requested, open Gnuplot
 IF ( [viewplot-yn] .NE. 0 ) THEN
    SYS
      echo " gnuplot -persist [gnuplot_script]" ; echo
    SYS
      gnuplot -persist [gnuplot_script]
 ELSE
    SYS
      echo ; echo "gnuplot usage: load '[gnuplot_script]'" ; echo
 ENDIF

 EN D

 ; Modified 2013-11-27
 ;    2013-11-27 (trs) -- Overlays number of particles before verification, removed xticlabels
 ;    2013-10-23 (agl) -- New file names, modernized syntax & cosmetic
 ;    2012-05-10 (trs) -- Optionally spawns Gnuplot
 ;    2012-04-30 (trs) -- Adapted from goodpartsbygroup.spi

 ;