
      program statstat
c
c program to do station statistics:
c
c     - number of detections in catalog file for each station
c
c Lars Ottemoller, January 2003
c
c changes:
c

      implicit none                       ! force delcaration of all variables
      include 'seidim.inc'                ! dimensions for rea block
      include 'rea.inc'                   ! parameter common bliock
      integer seiclen

      character*80 data(5000)             ! s-file with data in text array
      character*80 err_text               ! error text
      character*80 infile                 ! input file

      logical all                         ! true: read all data, false: headers
      integer code                        ! error return code
      integer nevent                      ! number of events in file
      integer i,j                         ! counter

      integer maxstation                  ! maximum number in arrays
      parameter (maxstation=1000)
      character*5 stations(maxstation)    ! names of stations
      logical stationflag(maxstation)     ! true if station already found
      integer stationcount(maxstation)
      integer nstations                   ! number of stations
      integer ind                         ! index
      real slat,slon,height               ! station coordinates
      integer maxdetect                   ! maximum number of events detected

c
c print version
c
      include 'version.inc'
      out_version_date='September 4, 2003'
      if (version_new) out_version_date=version_date
      call print_ver
                                          ! one 1 station
c
c   open output file
c

       open(2,file='statstat.xy',status='unknown')
       open(3,file='statstat.out',status='unknown')
       open(4,file='statstat.gis',status='unknown')
    
c
c   get input file name, check if exist
c

 9    continue
      write(6,*) 'Give input file'
      read(5,'(a)') infile
      open(1,file=infile,status='old',err=10)
      goto 11
 10    continue
      write(6,*)' No such input file'
      goto 9
 11   continue

      all=.true.                  ! read all parameters
      nevent=0                    ! initialize counter
      nstations=0
      do i=1,maxstation
        stationcount(i)=0
      enddo

c
c-----------------------------------------------------------------
c  Loop to read events start here
c-----------------------------------------------------------------
c

  50  continue
c
c   read all parameters for one event form file unit 1
c
      call rea_event_in(1,all,data,code)
c
c   check if end of file (code=1), if so jump out of loop
c
      if(code.eq.1) goto 1000
c
      nevent=nevent+1               ! count events

c
c   write the whole first header line
c
      write(6,'(a)') data(1)(1:79)

      do i=1,maxstation 
        stationflag(i)=.true.
      enddo 
      do i=1,rea_nphase
c
c add station to array if not included yet
c
        ind=0
        do j=1,nstations
          if (rea_stat(i).eq.stations(j)) ind=j
        enddo
        if (ind.eq.0) then
          nstations=nstations+1
          ind=nstations
          if (nstations.eq.maxstation) then
             write(*,*) ' too many stations '
             stop
          endif
          stations(nstations)=rea_stat(i)
        endif
c
c count phases
c
c        if (rea_phase(i)(2:2).eq.'P'.or.
c     &      rea_phase(i)(2:2).eq.'S') then 
          if (stationflag(ind)) then
            stationcount(ind)=stationcount(ind)+1
            stationflag(ind)=.false.
          endif
c        endif
      enddo
c
c   get next event
c
      goto 50
c
c     end of file
c
 1000 continue

      maxdetect=1
      do i=1,nstations
        if (stationcount(i).gt.maxdetect) maxdetect=stationcount(i)
      enddo
c
c write output
c
      write(3,'(a,i7)') ' Number of events in input file', nevent
      write(3,'(a)') ' List of stations with less than 10% '
     &   // ' detections of the maximum number detected on one '
     &   // ' station: '
      write(4,'(a)') 'station,longitude,latitude,detections'
      do i=1,nstations
        slat=0.
        slon=0.
        if (seiclen(stations(i)).gt.0) then
c          write(*,*) i,stations(i)
          call stat_loc(stations(i),' ',slat,slon,height)
        endif
        if (slat.ne.0..and.slon.ne.0.) then
          write(2,'(f8.3,1x,f8.3,1x,i8,1x,a1,1x,a5)') 
     &      slon,slat,stationcount(i),'#',stations(i)
          write(4,'(a5,2x,a1,f9.3,a1,f8.3,a1,i10)') 
     &      stations(i),',',slon,',',slat,',',stationcount(i)

          if (stationcount(i).le.maxdetect*.1) then
            write(3,'(a5,1x,i5)') stations(i),stationcount(i)
          endif
        endif
      enddo
      write(6,*)            ! blank line
      close(2)              ! close output file
      close(3)              ! close output file
      close(4)              ! close output file
      write(6,*) 'Number of events in input file', nevent
      write(6,*) 'Output file name is statstat.out'
      write(6,*) 'Input file for psxy is statstat.xy'
      write(6,*) 'Input file for arcview is statstat.gis'

      stop
      end
