; >Create group and particle selection files
;
; SOURCE: dftotals.spi       More stacks, merged with sort.pam      Dec. 2006 ArDean Leith
;
; PURPOSE: Creates particle selection files by applying cutoff. Creates an
;          updated group selection file
;
; I/O PARAMETERS AND FILES ARE SET HERE:
;
;  ------------ Input files ---------------------------------------

FR G                                     
[defgrps]sel_group                         ; Defocus groups selection file

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

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

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

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

FR G
[defgrps_lim]sel_group_cclim                ; Defocus groups selection file

FR G
[defgrps_lim_sorted]sel_group_cclim_sorted  ; Defocus groups selection file

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

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

DE                                        ; Remove any existing output doc file
[defgrps_lim] 

SD / GROUP   PARTICLES   DEFOCUS
[defgrps_lim]                             ; Group election file          (output)

IQ FI [exists]                            ; If thresh file exists, use thresh
[thresh]                                  ; Threshold doc file           (input) 

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

[all]   = 0
[saved] = 0

IF ([exists].LE.0) THEN
   ; ***** No threshold doc file : Keep all particles ********************************

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

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

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

      SD / PARTICLE NUMBER  
      [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]                ; Save: Particle #, CC value, Defocus
         [sel_particles]                  ; Selection file                 (output)
      LB2
      UD ICE
      [docapsh]
      SD E                                ; Free doc file 
      [sel_particles]

      SD [numgrp], [grp],[numparts],[def] ; Save: Group, Particle #, Defocus
      [defgrps_lim]                       ; Selection file                (output)
 
      [all] = [all] + [numparts]
   LB1

   VM
   echo  ' ' ; echo ' 'Overall particles: {******[all]}   

ELSE
   ; ********* Apply cuttoff  threshold  **************************************************


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

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

      [all] = [all] + [numparts]
      [key] = 0

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

      SD / PARTICLE NUMBER   
      [sel_particles]                     ; Selection file                (output)

      DO LB4 [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]                        ; Alignment doc file             (input)

         UD [grp],[ccthresh]              ; Get CC threshold for this defocus group
         [thresh]                         ; Threshold doc file             (input) 

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

            SD [key], [part]              ; Save:  Particle #
            [sel_particles]               ; Selection file                 (output)
         ENDIF
      LB4

      UD ICE
      [docapsh]
      SD E                               ; Free doc file 
      [sel_particles]

      SD [numgrp], [grp],[key],[def]     ; Save: Group, Particle #, Defocus
      [defgrps_lim]                      ; Selection file                (output)

      VM
      echo ' 'Group: {***[grp]}  Cutoff: {%F8.2%[ccthresh]}   Saved: {******[key]} Out of: {******[numparts]}  
      [saved] = [saved] + [key]   
   LB3

   VM
   echo  ' '; echo ' 'Overall saved: {******[saved]} Out of: {******[all]}  
ENDIF

SD E
[defgrps_lim]                            ; Selection file                (output)

FR L
[tmp1]order_select_jnk1                  ; Temp output file (deleted)

; DOC file sorting
DOC SORT
[defgrps_lim]                            ; Defocus selection doc file   (input)
[tmp1]                                   ; Sorted Temp file             (output)
2                                        ; Sort column
Yes                                      ; Renumber keys

DE                                       ; Remove any existing output file
[defgrps_lim_sorted]

UD N, [nkey]                             ; Get number of keys in sorted     
[tmp1]                                   ; Temp file                  (input)

; Reverse sorting order of output doc file
DO LB5 [i]=1,[nkey]                      ; Loop over defocus groups 
   [grp]=[nkey]-[i]+1                    ; Reverse order

   ;            GROUP, PARTICLES, DEFOCUS              
   UD IC [grp], [grp],  [part],   [def]
   [tmp1]                                ; Doc file                  (input)

   SD    [i],   [grp],  [part],   [def]
   [defgrps_lim_sorted]                  ; Reverse sorted doc file  (output)
LB5

SD E
[defgrps_lim_sorted]

UD ICE                                   ; Close doc files
[tmp1]
DE                                       ; Destroy temp. doc files
[tmp1]

VM
echo  ' '
    
EN D
;