; Creates histogram of CC values
;
; SOURCE: cchistogram.spi
;
; PURPOSE: For each defocus group:
;          Creates a histogram showing number of
;             particles vs. cross correlation value. 
;          Creates additional histogram of all combined defocus groups. 
;          Optionally creates gnuplot commands for plotting
;             the combined overall histogram and displays plot.
;          All histograms have 128 bins.
;
;          To view individual defocus group plots use:  plotviews.bat
;
; MASTER COPY: /net/bali/usr1/spider/docs/techs/recon/newprogs/
;
; I/O PARAMETERS AND FILES ARE SET HERE:
;
;------------ Edit next  Input parameter ------------------------------ 

[doplot] = 1             ; If >0 create pllt commands and plot histogram  

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

FR G                                     
[defgrps]sel_group                   ; List of groups (group selection doc file)

FR G
[docalign]../Alignment/align_01_***    ; Alignment files from 'AP SH'

FR G
[doccombine]align_01_all               ; Combined output from 'AP SH' 

; --------------- Output files  -------------------------------------

FR G
[cchist]hist/cchist***         ; Output histogram doc files

FR G
[cchistall]hist/cchist_all     ; Overall histogram doc file

FR G
[gnuplot_commands]plot_hist    ; Text file of gnuplot commands

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

UD N [numgrps]              ; Get number of groups 
[defgrps]                   ; Group selection doc file      (input)

VM                          ; Make directory for histograms
mkdir -p hist

DO LB1 [numgrp]=1,[numgrps] ; Loop over all defocus group(s)

   UD [numgrp],[grp]        ; Get current group number 
   [defgrps]                ; Group selection doc file     (input)

   DE                       ; Delete any existing histogram
   [cchist]

   ; PHI,THE,PSI, MIR-REF#,IMG#,INPLANE, SX,SY,NPROJ, DIFF,CCROT,INPLANE,SX,SY

   HD D             ; Create histogram
   [docalign][grp]  ; Doc file                             (input)
   (11)             ; Column to plot ('AP SH' specific)
   (128)            ; Number of bins
   [cchist][grp]    ; Histogram Doc. file                  (output)

   VM
   echo  ' 'Histogramed group: {****[grp]}    
LB1

DE                  ; Delete any existing combined histogram
[cchistall]

HD D                ; Create the combined histogram
[doccombine]        ; Combined alignment parameter file     (input)
(11)                ; Column to plot ('AP SH' specific)
(128)               ; Number of bins
[cchistall]         ; Doc file                             (output)

IF ([doplot] .GT. 0) THEN
   ; Create gnuplot command file
   VM               ; Delete any existing file first
   \rm -f [gnuplot_commands].$DATEXT
 
   VM               ; Gnuplot header
   echo #!/bin/sh > [gnuplot_commands].$DATEXT
   VM 
   echo ' 'set ylabel \"# Particles\" >> [gnuplot_commands].$DATEXT
   VM 
   echo ' 'set xlabel \"cross correlation value\" >> [gnuplot_commands].$DATEXT
   VM 
   echo ' 'set title \"Histogram of all groups\" >> [gnuplot_commands].$DATEXT
   VM               ; Gnuplot graph
   echo ' 'plot \'[cchistall].$DATEXT\' using 3:4 notitle  with boxes >> [gnuplot_commands].$DATEXT

   VM               ; Display plot                       
   gnuplot -persist [gnuplot_commands].$DATEXT &

   VM                                 
   echo ' 'Overall Histogram Plotted with: gnuplot script --------
   VM               ; Echo plot commands to terminal                        
   cat [gnuplot_commands].$DATEXT 
   VM                                      
   echo ' '-----------------------
   VM                                      
   echo ' '

ENDIF

EN D
 
;