; Compute histogram of particles' CC Value
 ;
 ; SOURCE:  spider/docs/techs/recon1/Procs/plot-cc-vs-view.spi    
 ;
 ; PURPOSE: Compute histogram of each view's particles CC value s
 ;          Selects  '*_good'input file if it exists otherwise selects: '*_all'.
 ;          Can display plot.
 ;
 ; ADAPTED FROM: histgoodccc.spi
 ;
 ; USAGE:        clean ; ./spider spi/dat @plot-cc-vs-view
 ;
 ; ------------- Parameters -------------

 [want-good-yn] = 1    ; Set to: 1  If you want to plot the 'good' verified ref views (not 'all')

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

 [num-bins]     = 128  ; Number of bins in histogram

 ; -------------- Input files --------------

 [ref_view_list]      = '[rec_dir]/sel_proj'              ; List of ref. projections (one)

 [inlist]             = '[view_dir]/prj***/sel_part_byv'  ; Particle list        (one/view)

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

 ;  VIEW_WIN  GLOBAL_NUM  GRP_WIN  CCROT      MIRROR  GRP_NUM    VIEW  MIC
 [combined_part_list] = 'sel_part_combined' ; Combined list of particles (one)

 [ccrot_hist]         = 'cc_vsview'         ; CCC histogram of particles (one)

 [gnuplot]            = 'cc_vsview'         ; Gnuplot script             (one)

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

 MD                                ; Quiet
   VB OFF
 
 ; Set common filenames & parameters
 @verify-settings

 ; Set temporary filenames

 [one] = 1
 IQ FI [goodexist]           ; See if this should access 'all' or 'good'
   [inlist]_good[one]
 [goodexist]

 ; Select 'all' or 'good' for input
 IF ( [goodexist] == 1 ) THEN
    [in_allvsgood]  = '_good'
    [allvsgood]     = 'good'
    [out_allvsgood] = 'good'
 ELSE
    [in_allvsgood]  = '_sort'
    [allvsgood]     = 'all'
    [out_allvsgood] = 'all'
  ENDIF

 ; Concatenate name
 [part_list_by_view]  = '[inlist][in_allvsgood]'
 [plot]               = '[gnuplot]_[allvsgood].gnu'
 [out]                = '[ccrot_hist]_[out_allvsgood]'
 [out_combined]       = '[combined_part_list]_[out_allvsgood]' 

 ; Temporary files
 [temp_parts_by_view]           = 'tmp_partsbyview'

 [temp_combined_parts_unsorted] = 'tmp_combined'

 ; Generate view-selection list

 ; Clean up
 DE
   [temp_parts_by_view]

 ; Initialize counters
 [view-counter] = 0

 [dummy]        = 1

 ; Get # of views
 UD N [num-views]
   [ref_view_list]              ; File          (input)

 ; Loop through views
 DO  [vkey] = 1,[num-views]      ; Loop through views ----------

    ; Get reference-view #
    UD IC [vkey],[view]
      [ref_view_list]            ; File          (input)

    ; Check if view list exists
    IQ FI [list-exists]
      [part_list_by_view][view]

    IF ( [list-exists] == 1 ) THEN
      [view-counter] = [view-counter] + 1

      SD [view-counter], [view],[dummy]
        [temp_parts_by_view]      ; File          (output)
    ENDIF
 ENDDO                            ; End view-loop ----------------

 SD E
   [temp_parts_by_view]

 SYS
   echo -n "  Making combined particle list   "; date

 ; Combine selection lists for all reference views
 DOC COMBINE
   [part_list_by_view]             ; Doc files     (input)
   [temp_parts_by_view]            ; File list     (input)
   [temp_combined_parts_unsorted]  ; Doc file      (output)

 DE
   [out_combined]                  ; Doc file      (removed)
 SD /      VIEW_WIN     GLOBAL_NUM     GRP_WIN         CCROT        MIRROR        GRP_NUM       VIEW        MIC
   [out_combined]                  ; Doc file      (output)
 SD E
   [out_combined]                  ; Doc file      (closed)

 DOC SORT A                        ; Sort doc file into existing labeled file
   [temp_combined_parts_unsorted]  ; Doc files     (input)
   [out_combined]                  ; Doc file      (output)
   2                               ; Register to sort by: global particle #
   Y                               ; Renumber

 UD N [num-parts]
   [out_combined]            ; Files         (input)

 ; Delete pre-existing histogram
 DE
   [ccrot_hist]

 ; Generate histogram of particle CCROTs
 HD D
   [temp_combined_parts_unsorted]
   4                               ; Column to bin (CCROT)
   [num-bins]                      ; Histogram bins
   [ccrot_hist]                    ; File          (output)

 ; Clean up
 DE
   [temp_parts_by_view]
 DE
   [temp_combined_parts_unsorted]

 SYS
   echo "  Done -- Combined particles: {%I0%[num-parts]}" ;


 ; WRITE GNUPLOT SCRIPT

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

 ; Will use arbitrary numbers in case version <18.20
 SYS
   echo 'max_height = 1200' > [plot]
 SYS
   echo 'average    = 7500' >> [plot]
 SYS
   echo 'std_dev    = 1500' >> [plot]
 SYS
   echo 'histgood(x)=max_height*exp(-(x-average)**2/std_dev**2)' >> [plot]
 SYS
   echo 'plot  [][] \' >> [plot]
 SYS
   echo '"[ccrot_hist].$DATEXT" using 3:4 title "good" with boxes, \' >> [plot]
 SYS
   echo 'histgood(x) title "fitted good"' >> [plot]


 ; Will attempt DOC SORT in case version >=18.20

 ; Get CCROT stats
 DOC STAT [dummy1],[dummy2],[dummy3],[dummy4],[cc-avg],[cc-stdev]
   [out_combined]
   4          ; Column # to analyze: CCROT

 ; Get maximum #particles from histogram
 DOC STAT [dummy1],[dummy2],[max-parts]
   [ccrot_hist]
   2     ; Column # to analyze: #parts

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

 ; Will use arbitrary numbers in case version <18.20
 SYS
   echo 'set xlabel "CCROT"' > [plot]
 SYS
   echo 'set ylabel "#Particles"' >> [plot]
 SYS
   echo 'max_height = {*****[max-parts]}' >> [plot]
 SYS
   echo 'max_height = {*****[max-parts]}' >> [plot]
; SYS
;   echo 'average    = {*****[cc-avg]}'    >> [plot]
 SYS
   echo 'average    = {%f6.2%[cc-avg]}'    >> [plot]
 SYS
   echo 'std_dev    = {%f6.2%[cc-stdev]}'  >> [plot]
 SYS
   echo 'histgood(x)=max_height*exp(-(x-average)**2/std_dev**2)' >> [plot]
 SYS
   echo 'plot  [][] \' >> [plot]
 SYS
   echo '"[ccrot_hist].$DATEXT" using 3:4 title "[out_allvsgood]" with boxes, \' >> [plot]
 SYS
   echo 'histgood(x) title "Fitted [out_allvsgood]"' >> [plot]

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

 EN D

 ; Modified 2013-12-02
 ;    2013-12-02 (trs) -- Gnuplot script changed to allow floats for avg & stdev
 ;    2013-10-23 (agl) -- new file names, modernized syntax & cosmetic
 ;    2012-05-10 (trs) -- optionally spawns Gnuplot
 ;    2011-11-02 (trs) -- adapted from histgoodccc.spi
 ;    2009-05-14 (trs) -- added parameter for number of bins
 ;    2009-02-23 (trs) -- added view# to combinedgood output
 ;    2008-12-22 (trs & djt) -- labelled COMBINEDGOOD columns
 ;    2007-01-19 (trs) -- HD D now allows arbitrary #bins
 ;