; Determine and apply threshold for the cross correlation
;
; SOURCE: ccthresh.spi
;                       Merged with dftotals.spi       Nov 2006 ArDean Leith
;                       Selection bugs                 Feb 2008 Magali
;
; PURPOSE: Creates selection files by applying cutoff
;          Determines a threshold for the cross correlation value by 
;          specifying a percentage of particles to eliminate.
;
; NOTE: Particle totals are approximate due to binning of data in histograms.
;
; MASTER COPY: /net/bali/usr1/spider/docs/techs/recon/newprogs/
;
; I/O PARAMETERS AND FILES ARE SET HERE:
;
;  ------------ Parameters ---------------------------------------

[cutoff]      = 0.15           ; Percentage of particles to eliminate

[make_select] = 0              ; >0 uses cutoff to create selection files

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

FR G                                     
[defgrps]sel_group                        ; Defocus groups selection file

FR G
[docapsh]../Alignment/align_01_{***[grp]} ; Document file created by 'AP SH'

FR G
[cchist]hist/cchist{***[grp]}             ; Histogram doc files (IF: [make_select]>0)

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

FR G
[thresh]thresh                          ; Doc file with CC thresholds

FR G
[sel_particles]sel_particles_{***[grp]} ; Output file (one for each defocus group)
;                                       ;   Contains the particle numbers whose   
;                                       ;   correlation coeffs. are greater than threshold

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

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

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

DE                              ; Remove any existing output doc file
[thresh]

SD /    CC_THRESH   N_ABOVE_THR   N_BELOW_THR   N_TOTAL
[thresh]

IF ([cutoff].LE.0) THEN
   ; ***** The threshold = 0 case : keep using  all particles *************

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

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

      SD [grp],[zero],[numparts],[zero],[numparts] ; Make threshold file entry
      [thresh]                        ; Threshold file               (output)

      IF ([make_select].GT.0) THEN
         ; Create selection file
         DE                           ; Remove any existing output doc file
         [sel_particles]              ; Selection file                 
         SD/    PARTICLE #   CC VALUE  
         [sel_particles]              ; Selection file               (output)                
                                             
         DO LB2 [part]=1,[numparts]   ; Loop over particles in this defocus group

            ;            PHI,THE,PSI, REF#,IMG#,INPLANE, SX,SY,NPROJ, DIFF,CCROT,INPLANE,SX,SY
            UD IC [part], [d],[d],[d], [d],[d],[d],      [d],[d],[d], [d],[cc]
            [docapsh]

            SD [part],[part],[cc]     ; Save: Particle #, CC value
            [sel_particles]           ; Selection file                 (output)
         LB2
         SD E                         ; Free doc file pointer
         [sel_particles]
      ENDIF
   LB1
   SD E                               ; Free doc file pointer
   [thresh]

ELSE
   ; ********* Apply cuttoff  threshold  **********************************
   VM
   echo  ' 'Cutoff: {%f5.2%[cutoff]}    

   [all] = 0
   [saved]=0

   DO LB3 [numgrp]=1,[numgrps]          ; Loop over all defocus group(s)
      UD [numgrp],[grp],[numparts]      ; Get current group number and particles 
      [defgrps]                         ; Group selection doc file    (input)

      ; Determines the number of particles below the percent cutoff ([cutoff]),
      ; [cutoff] = Percent cutoff
      ; [grp]    = Defocus group
      ; Gets the total number of particles (from column 2 in histogram),
      ; Determines the number of particles below the percent cutoff,
      ; Finds the corresponding CC value (column 1 in histogram).

      [ncum]    = 0                    ; Cumulative no. of particles
      [Nbelow] = 0     
      [nbad]    = [cutoff]*[numparts]  ; Number of particles to eliminate

      UD N,[nbins]                     ; Find number of bins in histogram
      [cchist]                         ; Histogram doc file      (input)  

      DO LB4 [key] = 1,[nbins]         ; Loop over all bins

         UD [key],[CC_threshold],[parts]
         [cchist]                      ; Histogram doc file      (input)

         [ncum] = [ncum] + [parts]     ; Number of particles below cuttof

         IF ([ncum].GT.[nbad]) THEN
            [cuttoffbin] = [key]-1     ; Last bin to discard

            UD [cuttoffbin],[CC_threshold]  ; Get the cuttoff
            [cchist]                   ; Histogram doc file     (input)

            GOTO LB5                   ; Leave the loop now
         ENDIF

         [Nbelow] = [ncum]             ; No. of particles from previous bins
      LB4

      LB5
      [Nabove] = [numparts] - [Nbelow] ; Number above threshold

      ; Save [CC_threshold], N_above_threshold, N_below_threshold, total_N
      SD [grp],[CC_threshold],[Nabove],[Nbelow],[numparts]
      [thresh]

      VM
      echo ' 'Group: {****[grp]} Saved: {******[Nabove]} Out of: {******[numparts]}  

      [all]   = [all] + [numparts]
      [saved] = [saved] + [Nabove]]

      IF ([make_select].GT.0) THEN
         ; Create selection file
         [key]=0

         DE                             ; Remove any existing output doc file
         [sel_particles]                ; Selection file                 

         DO LB6 [part]=1,[numparts]     ; Loop over particles in this defocus group

            ;            PHI,THE,PSI, REF#,IMG#,INPLANE,  SX,SY,NPROJ, DIFF,CCROT,INPLANE,SX,SY
            UD IC [part], [d],[d],[d], [d],[img],[d],     [d],[d],[d],  [d],[cc]
            [docapsh]

            IF ([cc].GE.[CC_threshold])THEN ; CC above threshold for this particle
               [key]=[key]+1                ; Increment new particle counter = key

               SD [key],[img],[cc]          ; Save: Particle #, CC value
               [sel_particles]              ; Selection file                 (output)
            ENDIF
         LB6
         UD ICE
         [docapsh]
         SD E                               ; Free doc file pointer
         [sel_particles]
      ENDIF
   LB3
   VM
   echo  ' '; echo ' 'Overall saved: {******[saved]} Out of: {******[all]}  
ENDIF

VM
echo  ' '
    
EN D
;