c234567
      program spillstat
      common/pawc/hmemor(200000)
      dimension icc(1),cont(5000)
      character*6 aa

      call hlimit(100000)  
      call hbook1(100,' spill 1ms ',5000,1000.,6000.,0.) ! full range
      call hbook1(101,' spill 1ms ',4000,1500.,5500.,0.)
      call hbook1(102,' spill 5ms ',800,1500.,5500.,0.)
      call hbook1(103,' spill 25ms ',160,1500.,5500.,0.)

      call hbook1(200,' mean 1 ms ',50,50.,150.,0.)
      call hbook1(201,' sd/sqrt(mean) 1 ms ',60,1.,4.,0.)
      call hbook1(202,' skewness 1 ms ',50,-1.,1.,0.)
      call hbook1(203,' kurtosis  1 ms ',40,1.,5.,0.)
     
      call hbook2(301,' sd/sqrt(mean) vs skewness ',
     1 60,1.,4.,50,-1.,1.,0.)
      call hbook2(302,' sd/sqrt(mean) vs kurtosis ',
     1 60,1.,4.,40,1.,5.,0.)
      call hbook2(303,' skewness  vs kurtosis ',
     1 50,-1.,1.,40,1.,5.,0.)
      

      do ii = 1,12
      icc(1) = ii
       if(icc(1).eq.1)
     1 open(unit=19,file='elis-12567-0336.txt',status='unknown') ! golden file 
       if(icc(1).eq.2)
     1 open(unit=19,file='elis-12567-0337.txt',status='unknown') ! golden file 
       if(icc(1).eq.3)
     1 open(unit=19,file='elis-12567-0338.txt',status='unknown') ! golden file 
       if(icc(1).eq.4)
     1 open(unit=19,file='elis-12567-0339.txt',status='unknown') ! golden file 
       if(icc(1).eq.5)
     1 open(unit=19,file='elis-12567-0340.txt',status='unknown') ! golden file 
       if(icc(1).eq.6)
     1 open(unit=19,file='elis-12567-1146.txt',status='unknown') ! golden file 
       if(icc(1).eq.7)
     1 open(unit=19,file='elis-2465-0170.txt',status='unknown') ! dataplot31.kumac
       if(icc(1).eq.8)
     1 open(unit=19,file='elis-2566-1230.txt',status='unknown') ! dataplot32.kumac
       if(icc(1).eq.9)
     1 open(unit=19,file='elis-2066-1129.txt',status='unknown') ! dataplot33.kumac
      if(icc(1).eq.10)
     1 open(unit=19,file='elis-12288-1360.txt',status='unknown') ! gold 22
      if(icc(1).eq.11)
     1 open(unit=19,file='elis-13019-0133.txt',status='unknown') !  new 23
      if(icc(1).eq.12)
     1 open(unit=19,file='elis-13046-0011.txt',status='unknown') ! new 23

      read(19,5510,END=999)aa,nrun,nbur
 5510 format(a6,2i8)
      write(*,*)' '
      write(*,*)' run burst ',nrun,nbur



      nnn =0
      nn = 500000 !  max input - see arrays
      do k =1,nn
      read(19,5513,END=999)aa,kk,kid,kcode,kstamp
      nnn = nnn+1
 5513 format(a6,2i8,2z10)
      fm = float(kstamp)/40000d0 ! ms
      call hfill(100,fm,0.,1.)
      call hfill(101,fm,0.,1.)
      call hfill(102,fm,0.,1.)
      call hfill(103,fm,0.,1.)
      enddo
 999  continue

      write(*,*)' count  ', nnn
      write(*,*)' '
      close (19)

c     spill statistics

      call hunpak(101,cont,' ',0) ! 1 ms interval
      fnn = 4000
      smean = 0.
      do i = 1,fnn
      smean = smean +cont(i)
      enddo
      smean = smean/fnn
      sd = 0.
      skew = 0.
      skurt = 0.
      do i = 1,fnn
      sd = sd + (cont(i) - smean)**2
      skew = skew + (cont(i) - smean)**3
      skurt = skurt + (cont(i) - smean)**4
      enddo

      sd =sqrt(sd/fnn)
      skew = skew/sd**3/fnn
      skurt = skurt/sd**4/fnn
      sdn = sd/sqrt(smean)

      write(*,*)' mean,sd/sqrt(mean),skewness,kurtosis ',
     1  smean,sdn,skew,skurt    
 
      call hfill(200,smean,0.,1.)
      call hfill(201,sdn,0.,1.)
      call hfill(202,skew,0.,1.)
      call hfill(203,skurt,0.,1.)

      call hfill(301,sdn,skew,1.)
      call hfill(302,sdn,skurt,1.)
      call hfill(303,skew,skurt,1.)
      

      call hreset(100,' ')
      call hreset(101,' ')
      call hreset(102,' ')
      call hreset(103,' ')

      enddo  ! loop over files

      call hprint(200)
      call hprint(201)
      call hprint(202)
      call hprint(203)

      call hprint(301)
      call hprint(302)
      call hprint(303)

      call hrput(0,'spillstat.dat','n')
      stop
      end



