; List number of particles in each defocus group
;
; SOURCE: ordefocus.spi
;
; PURPOSE: List the number of particles in each defocus group and the 
;          average defocus value for the group.
;
; MASTER COPY: /net/bali/usr1/spider/docs/techs/recon/newprogs/
;
; I/O PARAMETERS AND FILES ARE SET HERE:
;
;  ------------ Input files ---------------------------------------

FR G
[order_defgrps]../Power_Spectra/order_defgrps ; Defocus group selection doc file

FR G
[def_avg]../Power_Spectra/def_avg   ; Defocus doc file

FR G
[order_select]../Particles/order_selected ; lists selected particles per micrograph

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

FR G
[order]order_defocus               ; No. of particles / defocus group

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

DE         ; Delete any existing output doc file
[order]

x91 = 0    ; cumulative total
x92 = 0    ; cum. total for each defocus group

; Title for output doc file
SD /      PARTICLES  CUM.TOTAL   AVG.DEFOCUS
[order]

UD N,x88      ; x88 = no. of defocus groups
[order_defgrps]

UD N,x98      ; x98 = no. of micrographs
[def_avg]

vm
echo  ' 'Number of micrographs: {****x98}   Number of groups: {***x88} 

; Loop over defocus groups

DO LB1 x11 = 1,x88

   UD x11,x55, x56
   [order_defgrps]    ; x55 is the current defocus group no.

   x92 = 0 ; cum. total for this defocus group

   ; go thru def_avg; when the defgroup matches, use that micrograph

   DO LB2 x41 = 1,x98
      ; key   4    micrograph  defocus  def_group  def_group_average
      UD IC x41,  x42,         x43,     x44,       x45
      [def_avg]

      IF (x44.EQ.x55) THEN
         ; Micrograph x42 is in current defocus group
         ; Go thru order_selected to find total for this micrograph

         DO LB3 x13 = 1,x98
            ; key   4    micrograph  particles  total
            UD IC x13,    x62,         x63,       x64
            [order_select]

            IF (x62.EQ.x42) THEN
               x92 = x92 + x63     ; increment particles for this group

VM
echo  ' 'Group:{***x55}      micrograph:{****x62}    no. particles:{*******x63} 
               GOTO LB4
            ENDIF
         LB3
         LB4
      ENDIF
   LB2

   ; we've gone thru def_avg, thus we have all particles for this group
   x91 = x91 + x92
   ;       total, cum.total, avg.defocus
   SD x11, x92, x91,x56
   [order] 

LB1

EN D