c                                                                               
c     Program to summarize logs                    
c     1997 by jh
c
c   to change number of stations, change variable text and all format
c   statments where the number is used, one station takes 5 columns
c
c   Updates:
c   june 98 by jh   : include gps statistics
c   sep 98          : fix text
c   sep  25 by jh   : average over all days instead of active days
c                     also write out total detection in interval
c   oct 2           : divide by one day too little
c   feb 3 99   jh   : -------------   verison 7.0  -----------------
c   jun 24          : enscript instead of nenscript
c   jan 15, 02 jh   : more def stations
c   feb 6, 2002 jh  : count wav files
c   feb 7           : one day operation from prompt
c   may 6 2002      : add domb
c   sep 23 2002     : add OSL
c   nov 6 2006      : add hopen,berb,ckono,ckbs
c   nov 9 2007      : more changws, now 38 stat
c                                                                               
      implicit none                                                             
      include 'libsei.inc'                ! Open file definitions
      include 'seidim.inc'                ! Dimentions
      include 'seisan.inc'
C
c-- event records                                  
      character*80 data(500)			
      real up_time,up_t     ! up times in %
      real up_time_gps,up_g     ! up times gps in %
c-- filenames                        
      character*80 evfile	
      character*80 wave_file
      character*207 text
      character*80 txt
c-- times for event selection             
      character*14 start_time,end_time		
c-- database name               
      character*40 base_name/' '/		
c-- selection key                                   
      character*10 key/' '/			
c-- Event type indicators                         
      character*1 type1			
      character*12 t12      ! system time
      character*14 t14      ! system time
c-- explosion indicator                                  
      character*1 exp				
      integer det(50,31)    ! number of detections all stations, one month
      real up(50,31)        ! up times             -----------------------
      real up_gps(50,31)    ! --------------- gps
      integer wav_total(50,31)    !  total number of waveforms in s-file
      integer wav_present(50,31) ! number of waveforms present
      character*5 stat(50)  ! station data bases
      character*250 stat_c  ! --- in one string
      real average_up(50)   ! average up time
      real average_up_gps(50) ! --- gps -----
      real average_det(50)  ! average no of detections
      integer ava_wav_total(50) ! total number of wav files
      integer ava_wav_present(50) ! total number for wav files present
      integer n_up(50)      ! number of days with up times
      integer n_up_gps(50)  ! ------------------ gps -----
      integer day1,day2     ! range of days for log
c-- See routine: find_ev_index     
      integer status,new_month,fstart,event_no	
c-- timing of first event           
      integer year1,month1,hr1,min1,sec1
      real sec
      double precision total_time
c   arguments 
      character*80 arg(5)
c   number of arguments
      integer nargs
      integer day_back      ! days back to do a one day check
c-- same as sec1                  
      real ssec1                                
      integer seiclen
c-- not used                                               
      integer nphas				
c-- See routine: Indata                        
      integer nhead,nrecord,nstat,nsta		
c-- counters                                     
      integer i,k,l,nin,nout,istat,nout_gps
      logical exist                                                            
      equivalence (stat_c,stat)
c
c print version
c
      include 'version.inc'
      out_version_date='July 23, 2001'
      if (version_new) out_version_date=version_date
      call print_ver
c
c   get arguments
c
      call get_arguments(nargs,arg)
c
c   find how many minutes back if given as an argument
c
      if(nargs.gt.0) then
         read(arg(1),'(i2)') day_back
      endif

c
c   get system time
c
       call systime(t12,t14)
c
c   calculate abs time
c
       read(t12,'(4i2)') year1,month1,day1,hr1
       year1=year1+2000
       call timsec(year1,month1,day1,hr1,0,0.0,total_time)
c
c   subtract days day_back
c
       total_time=total_time-day_back*24*3600

       call sectim(total_time,year1,i,month1,day1,hr1,min1,sec)
c
      stat_c=' '
      stat_c(1:50)= 
     *'FOO  HYA  KMY  LOF  MOL  NSS  ODD  SUE  TRO  STEI '
      stat_c(51:100)=
     *'WNN  BLS  BJO  KBS  KONO KTK  MOR  RUND STAV JMI  ' 
      stat_c(101:150)=
     *'DOMB OSL  SNARTSTOK HOPENBERB FLOS KONS MELS CBJO '
      stat_c(151:200)=
     *'CJMICCKONOCKBS CBER CNSS CMOL CTRO CKTK           '
c                                                                               
c------First inizialize -------                                                 
c                                                                               
      nout=0               
      nout_gps=0                                                     
      nin=0                                                                     
      up_time=0.0
      up_time_gps=0.0
c
c   set number of stations, also set all format stations at end
c
      nstat=38
      do i=1,50
        do k=1,31
           up(i,k)=-1.5
           up_gps(i,k)=-1.5
           det(i,k)=0
           wav_total(i,k)=0
           wav_present(i,k)=0
        enddo
      enddo
      text=' '
      call get_seisan_def
c
c   ask for time interval if not jst one day day_back
c
      if(day_back.lt.1) then
         write(6,*)'Start time(yyyymmdd)'
         read(5,'(a)') start_time
         write(6,*)'End time, return for end of month'
         read(5,'(a)') end_time
      else
         start_time=' '
         write(start_time(1:8),'(i4,2i2)') year1,month1,day1
         do i=1,8
           if(start_time(i:i).eq.' ') start_time(i:i)='0'
         enddo
         end_time=start_time
      endif
c                                                                               
c-------Open infile' ------                                               
c                                                                               
c      write(6,*)'Enter input file name'
c      read(5,'(a)') infile
c      open(2,file=infile,status='old')                                    
c
   
      open(3,file='stalog.out',status='unknown')                      
      open(9,file='stalog_wav.out',status='unknown')
c          
c---------------------------------------------------------------------
c  loop of all data bases
c--------------------------------------------------------------------
c
      do istat=1,nstat
        base_name=' '
        base_name(1:5)=stat(istat)
        write(9,*)
        write(9,'(a,a)') ' Station:  ', stat(istat)
c                                                                               
c  back here for next event, same base                                                    
c
 30    continue                                                                
c-- always use next event                                
       key='          '		
       CALL findevin                                                       
     * (base_name,start_time,end_time,key,0,
     * event_no,evfile,fstart,new_month,status)                                 
       if(status.eq.0) then                                                     
         inquire(file=evfile,exist=exist)                         
         if (exist) then
            open(2,file=evfile,status='old')                                          
            call indata(2,nsta,nphas,nhead,nrecord,type1,exp,data,i)
c            if(nrecord.eq.0) goto 999                                                 
c                                                                               
c   count number of input files                                                 
c                                                                               
            nin=nin+1                                                                 
c
c  get date etc
c
            read(data(1),'(1x,i4,1x,2i2,1x,2i2,1x,f4.1,1x,a1)')                       
     *      year1,month1,day1,hr1,min1,ssec1,type1                                    
            sec1=ssec1                                                                
c
c   check if a log file                                                   
c
            if(data(1)(22:23).eq.'LL') then
               do k = 1,nhead                                             
                  if(data(k)(10:16).eq.'up time') then
                     read(data(k)(31:36),'(f7.2)') up_t
                     up(istat,day1)=up_t       ! save up time for this day
                     up_time=up_time+up_t
c                     write(3,'(a,5x,f7.2)')data(1)(1:10),up_t
                     nout=nout+1
                  endif
                  if(data(k)(2:7).eq.'GPS UP') then
                     read(data(k)(31:36),'(f7.2)') up_g
c                    write(6,*) up_g
                     up_gps(istat,day1)=up_g       ! save up gps for this day
                     up_time_gps=up_time_gps+up_g
c                     write(3,'(a,5x,f7.2)')data(1)(1:10),up_t
                     nout_gps=nout_gps+1
                  endif
               enddo                                                                 
            else         
c
c   count detections for this day
c
               det(istat,day1)=det(istat,day1)+1
            endif
c
c   check for waveform files
c
             do i = 2, nhead
                if(data(i)(80:80).eq.'6'.and.
     *             data(i)(2:2).ne.' ') then
                      wav_total(istat,day1)=
     *                wav_total(istat,day1)+1
                      wave_file=' '
                      wave_file(1:78)=data(i)(2:79)
                      call  get_full_wav_name(wave_file,txt)
                      if(txt.eq.' ') then
                         write(9,'(a,a)')
     *                   ' File does not exist: ',
     *                   wave_file(:seiclen(wave_file))
                         write(9,*) txt
                      else
                         wav_present(istat,day1)=
     *                   wav_present(istat,day1)+1
                      endif
                endif
            enddo
c
            goto 30   ! next event
         endif
      else       ! must be end of month, jump to end of loop
         goto 40
      endif
c
 40   continue
c
c----------------------------------------------------------------------
c   end of station loop
c----------------------------------------------------------------------
c
      enddo
c                                                                              
c
c   write out total statistics for that month or interval
c
 999  continue                                                                  
c
      write(3,'(a,a,2x,a)')
     *'  Statistics for year and month interval : ',
     * start_time(1:8),end_time(1:8)
      write(3,*)
      read(start_time(7:8),'(i2)')day1
      if(day1.eq.0) day1=1
      read(end_time(7:8),'(i2)') day2
      if(day2.eq.0) then
        call month_day(year1,month1,day2)  ! find number of days in month
      endif
      write(3,'(a3,6x,38(a5))') 'Day',(stat(i),i=1,nstat)
      do i=day1,day2
c
c   system 
c
         write(text,'(i3,a4,38i5)')i,' SEI',(int(up(k,i)+0.5),k=1,nstat)
         do l=1,207
           if(text(l:l).eq.'-') text(l:l+1)=' .'
         enddo 
         write(3,'(a)') text(1:207)
c
c   gps
c
         write(text,'(3x,a4,38i5)')' GPS',
     *   (int(up_gps(k,i)+0.5),k=1,nstat)
         do l=1,157
           if(text(l:l).eq.'-') text(l:l+1)=' .'
         enddo 
         write(3,'(a)') text(1:207)
c
         write(3,'(3x,a4,38i5)')' DET',(det(k,i),k=1,nstat)
c
c  wav files
c
         write(text,'(3x,a4,38i5)')' WAV',
     *   (wav_total(k,i),k=1,nstat)
         do l=1,207
           if(text(l:l).eq.'-') text(l:l+1)=' .'
         enddo 
         write(3,'(a)') text(1:207)

         write(text,'(3x,a4,38i5)')' WAP',
     *   (wav_present(k,i),k=1,nstat)
         do l=1,207
           if(text(l:l).eq.'-') text(l:l+1)=' .'
         enddo 
         write(3,'(a)') text(1:207)

         write(3,*)
      enddo
c----------------------------------
c   average values if more than one day
c----------------------------------
c
      if(day2.gt.day1) then
      do k=1,nstat
         do i=day1,day2
            if(up(k,i).gt.-1.0) then
              average_up(k)=up(k,i)+average_up(k)
              n_up(k)=n_up(k)+1
            endif
c
            average_det(k)=average_det(k)+det(k,i)
c
c   gps
            if(up_gps(k,i).gt.-1.0) then
              average_up_gps(k)=up_gps(k,i)+average_up_gps(k)
              n_up_gps(k)=n_up_gps(k)+1
            endif
c
c   wav
c
            ava_wav_total(k)=wav_total(k,i)+ava_wav_total(k)
            ava_wav_present(k)=wav_present(k,i)+ava_wav_present(k)
         enddo
      enddo
c
      do k=1,nstat
         if(n_up(k).ne.0) then
c            average_up(k)=average_up(k)/n_up(k)     ! average over active days
            average_up(k)=average_up(k)/(day2-day1+1)  ! average over all days
         else
            average_up(k)=-1.5
         endif
c
         average_det(k)=average_det(k)/(day2-day1+1)
c
c   gps
c
         if(n_up_gps(k).ne.0) then
c           average_up_gps(k)=average_up_gps(k)/n_up_gps(k)  ! average active days
            average_up_gps(k)=average_up_gps(k)/(day2-day1+1)  ! average all days
         else
            average_up_gps(k)=-1.5
         endif
         
      enddo
      write(3,*)
      write(text,'(a7,38i5)')'AVA SEI',
     *(int(average_up(k)+0.5),k=1,nstat)
      do l=1,207
        if(text(l:l).eq.'-') text(l:l+1)=' .'
      enddo 
      write(3,'(a)') text
c
c gps

      write(text,'(a7,76i5)')'AVA GPS',(int(average_up_gps(k)+0.5),
     *k=1,nstat)
      do l=1,207
        if(text(l:l).eq.'-') text(l:l+1)=' .'
      enddo 
      write(3,'(a)') text
c
      write(3,'(a7,38i5)')'AVA DET',
     *(int(average_det(k)+0.5),k=1,nstat)
      write(3,'(a7,38i5)')'TOT DET',
     *(int(average_det(k)*(day2-day1+1)),k=1,nstat)
      write(3,'(a7,38i5)')'TOT WAV',
     *(ava_wav_total(k),k=1,nstat)
      write(3,'(a7,38i5)')'TOT WAP',
     *(ava_wav_present(k),k=1,nstat)
      write(3,'(a3,6x,38(a5))') '   ',(stat(i),i=1,nstat)

      endif

      close(3)
      call systemc('enscript -r -fCourier6 -Phpbw11 stalog.out',
     *42)
            
c
      write(6,*)                                                                
      write(6,*)'Output file is stalog.out and stalog_wav.out' 
      stop                                                                      
      end

