; Choose best images per direction
;
; SOURCE:      bestim.spi      ArDean Leith
;
; PURPOSE:     Limits particles corresponding to each reference direction  
;
; MASTER COPY: /usr1/spider/docs/techs/recon/newprogs/
;
; NOTE:        Continue with reconstruction following this limitation using:
;              deffsc.spi
;
; I/O PARAMETERS AND FILES ARE SET HERE:
;
;  -------------- Parameters -------------------------------------

[maxim] = 40                                    ; Max number of images / reference view to retain 

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

FR G
[defgrps]sel_group                              ; Defocus groups selection file

FR G
[imgperview]df{***[grp]}/how_many               ; Listing of  number of images for each view

FR G
[grp_sel_sel]df{***[grp]}/select/sel{***[view]} ; Selection & CCC files for each view

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

FR G
[sel_particles]sel_particles_{***[grp]}         ; Output file (one for each defocus group)
;                                               ; Contains the selected particle numbers

FR G
[defgrps_lim]sel_group_cclim                    ; Defocus groups selection file

FR G
[jnk_seltotal]df{***[grp]}/jnk_seltotal         ; Temp file (DELETED)

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

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

VM
echo  ' 'Limiting particles from each reference direction to: {****[maxim]} 
VM
echo  ' '     

DE
[defgrps_lim]                         ; Delete any existing defocus group sel. file

[totall] = 0
[totold] = 0

UD N,[numgrps]                        ; Find number of defocus groups
[defgrps]                             ;                             (input)

DO LB1 [numgrp] = 1,[numgrps]         ; Loop over all defocus groups

   UD [numgrp], [grp],[oldnum],[def]  ; Get current group number and particles 
   [defgrps]                          ; Group selection doc file     (input)
   
   [totold] = [totold] + [oldnum]     ; Total of old selected particles

   VM                                 ; Rename existing selection file 
   \mv [sel_particles].$DATEXT [sel_particles]_prev.$DATEXT

   UD N [views]                       ; Get number of views
   [imgperview]                       ; Doc file                    (input)

   [totgrp] = 0
   [keyout] = 0                       ; Output key counter

   DO LB2 [view]=1,[views]            ; Loop over all views for this group

      UD N [images]                   ; Get number of images for this view
      [grp_sel_sel]                   ; Listing file                (input)

      IF ([images] .LE. 0) GOTO LB2
     
      IF ([images] .LE. [maxim]) THEN

         DO LB3[image]=1,[images]
            UD IC [image],[part],[cc] ; Get particle number & CC
            [grp_sel_sel]             ; Listing file                 (input)

            [keyout] = [keyout] + 1

            SD [keyout],[part],[cc]   ; Save particle number & CC
            [sel_particles]           ; Overall limited listing file  (output)
         LB3

         [totgrp] = [totgrp] + [images]

         UD ICE
         [grp_sel_sel]                ; Temp sorted listing file

      ELSE

         DE
         [jnk_seltotal]               ; Temp sorted listing file
  
         DOC SORT
         [grp_sel_sel]                ; Particle listing file
         [jnk_seltotal]               ; Temp sorted listing file
         (2)                          ; Column to be sorted by (CCC for this view)
         Y                            ; Compress & renumber keys

         DO LB4 [image]=1,[maxim]
            UD IC [image],[part],[cc] ; Get particle number             (input)
            [jnk_seltotal]            ; Temp sorted listing file

            [keyout] = [keyout] + 1
            SD [keyout],[part],[cc]   ; Save particle number & CC
            [sel_particles]            ; Overall limited listing file   (output)
         LB4

         [totgrp] = [totgrp] + [maxim]

         UD ICE
         [jnk_seltotal]               ; Temp sorted listing file
      ENDIF
   LB2

   SD E
   [sel_particles]                     ; Limited listing file

   DE
   [jnk_seltotal]                     ; Temp listing working file

   [totall] = [totall] + [totgrp]

   VM
   echo  ' 'Group: {****[grp]}  Saved: {******[totgrp]}  Out of: {******[oldnum]}    

   SD [numgrp], [grp],[totgrp],[def]  ; Save group number and particles & defocus
   [defgrps_lim]                      ; Group selection doc file     (output)

LB1

SD E
[defgrps_lim]                         ; Finished

[percent] = 100.0 * [totall] / [totold]
VM
echo  ' 'Overall Saved: {******[totall]}  Out of: {******[totold]} = {***[percent]}% 
VM
echo  ' '     

EN