; Remove duplicates from particle selection doc files
;
; PURPOSE: Remove duplicates from particle selection doc files.
;          First, sorts the particle selection  files (from Web) and
;          saves list of selected particles into a new selection file.
;          Finds statistics for picked and selected particles by micrograph.
;
; SOURCE:  renumber.spi  
; 
; ------------------ Input files -----------------------

FR G
[sel_micrograph]../sel_micrograph   ; Micrograph selection file

FR G
[sel_particles]good/good{****[mic]} ; Particle selection files (from Web) 

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

FR G
[ngood]good/ngood{****[mic]}        ; Renumbered particle selection files

FR G
[percent]percent_selected           ; Statistics file of picked vs selected 

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

MD                                  ; Skip unnecessary output 
VB OFF
MD                                  ; Skip unnecessary output 
TR OFF

VM
echo "Removing duplicates from particle selection doc files."

FR G                               ; Temporary files
[tmp1]tmpa{****[mic]}

DE                                 ; Remove any existing statistics file
[percent]

SD /     MICROGRAPH    PICKED   SELECTED    %
[percent]                          ; Statistics doc file comment  (output)

[ntot]=0  
[ntotpick]=0                       ; Overall cumulative total
[ntotsel] =0                       ; Overall cumulative total

UD N [nummics]                     ; Get total number of micrographs
[sel_micrograph]                   ; Micrograph selection doc file (input)

DO [key]=1,[nummics]               ; Loop over all micrographs ------------

   UD [key], [mic]                 ; Get current micrograph number
   [sel_micrograph]                ; Micrograph selection doc file (input)

   [selected] = 0                  ; Zero the selected counter
   [picked]   = 0                  ; Zero the picked counter

   DE                              ; Remove any existing output file
   [ngood]
   SD /    PARTICLE #
   [ngood]                         ; Doc file title             (output)

   DOC SORT                        ; Renumber lines in doc file
   [sel_particles]                 ; Doc file                    (input)  
   [tmp1]                          ; Renumbered list            (output) 
   (1)                             ; Sorted column
   Y                               ; Compress and renumber keys

   UD N [n]                        ; Get total number of particles
   [tmp1]                          ;                            (input)

   DO [key2]=1,[n]                 ; Loop over all particles ----------

      ;      KEY,   PART-#, CATEGORY
      UD IC [key2], [npart],[cat]  ; Retrieve lines from: tmp1
      [tmp1]                       ; Doc file              (input)

      [picked]= [picked]+1         ; Total from this micrograph

      IF([cat].EQ.1) THEN          ; Save particles from category: 1
         [selected]=[selected]+1   ; Increment number of selected particles

         SD [selected],[npart],[cat]  ; Put particle in: tmp2
         [ngood]
      ENDIF
   ENDDO

   [perc] = 100 * [selected] / [picked] 
   ;          MICROGRAPH    PICKED  SELECTED   %
   SD [key], [mic],[picked],[selected],[pcent]
   [percent]

   [ntotpick]=[ntotpick]+[picked]   ; Overall cumulative total
   [ntotsel] =[ntotsel]+[selected]  ; Overall cumulative total

   VM
   echo "Micrograph: {******[mic]}  Using: {******[selected]}  From: {******[picked]}"

   SD E
   [ngood]                         ; Finished writing to this file 

   UD ICE
   [tmp1]
   DE                              ; Delete temp file
   [tmp1]
ENDDO

; Put overall statistics in comment key in doc file
[perc]  = 100 * [ntotsel] / [ntotpick]

SD /      TOTALs: PICKED      SELECTED     FRACTION
[percent]                          ; Doc file               (output)

SD -1, [ntotpick],[ntotsel],[perc]
[percent]                          ; Doc file                (output)

SD E
[percent]                          ; Finished with this file 

VM
echo "Overall selected: {******[ntotsel]}  Percent: {***[perc]%}"
VM
echo " "

EN
;