c   program to pick in automatic mode several eventd from 
c   data base.                  
c                                                                      
c   jens havskov, mar 92                                             
c                                                                  
c   uppdates                                                      
c   jul 8 93 by jh: VERSION 3.0
c   aug 23        : filename to 80 char, data to 400
c   nov 24, 1994   : opening files changed
c   dec 7           ************    version 5.0 *****************
c   jan 11   95   : new find ev in
c   nov 16 98 jh  : --------------   version 7.0 check----------------------
c                   start_time to 14, system-c to systemc
c                                                                   
       implicit none	
C
C    Seisan library inserts and routines...
C    ======================================
C
       include 'libsei.inc'                ! Library definitions & data defns.
       external sei open,                  ! Open file routine.
     &          sei close,                 ! Close file routine.
     &          sei code                   ! Error handler.
       integer  code,                      ! Local condition.
     &          read1                      ! Read unit 1.
       logical  b_old                      ! File is old file?
C
C    ============= end of list ==========
C
       include 'seidim.inc'                                                
c-- event data                                     
       character*80	data(max_data)	
c-- exp indicator                                         
       character*1	exp		
c-- event type                                      
       character*1      type		
c-- number of recors for event                           
       integer		nrecord		
c-- number of header lines                                 
       integer		nhead		
c-- number of stations                                     
       integer		nstat		
c-- number of phases
       integer nphase
c-- start and end time of select      
       character*14    start_time,end_time 
c-- data base name if not rea (blank)               
       character*40	base_name	
c-- event file name                                   
       character*80	evfile		
c-- select key                                       
       character*10     key		
c-- see subroutine find...       
       integer		status,new_month,fstart,event_no 
c-- counters for events, records etc.      
       integer nr,nd,nl,records,nevent	
c-- id line 
       integer id
c


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   input base name and time interval                                      
c                                                                               
       write(6,*)' Base name, return for default'                               
       read(5,'(a40)') base_name                                                
       write(6,*)' Start time'                                                  
       read(5,'(a14)') start_time                                               
       write(6,*)' End time, return for end of month'                           
       read(5,'(a14)') end_time                                                 
 1     continue                                                                 
	                                                                    
c                                                                               
c  reset counters                                                               
c                                                                               
       nl=0                                                                     
       nr=0                                                                     
       nd=0                                                                     
       nevent=0                                                                 
       records=0                                                                
c                                                                               
c  start event loop                                                    
c                                                                               
 5     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                                                     
       call sei open(old$+warn$,            ! Open & warn of error.
     &               ' ',                   ! Prompt file name (n/a).
     &               evfile,                ! File name
     &               read1,                 ! Read unit #1
     &               b_old,                 ! Already exists? (n/a).
     &               code)                  ! Returned condition.
C
         if( b_old ) then                   ! File exists & is open.
         call indata(read1,nstat,nphase,
     &               nhead,nrecord,type,exp,data,id)              
c                                                                               
c   count events etc                                                            
c                                                                               
         if(type.eq.'L') nl=nl+1                                              
         if(type.eq.'R') nr=nr+1                                              
         if(type.eq.'D') nd=nd+1                                              
         records=records+nrecord                                              
         nevent=nevent+1                                                      
c
c   only process event if no readings from before
c          
           if(nstat.lt.1) then
           write(6,'(1x,a60,a)') evfile(1:60),' picking phases'
           call put_env_event(evfile)
           call systemc("autopic",7)
C
           else
           write(6,'(1x,a60)') evfile(1:60)                                      
           call sei close (close$,read1,code)
           endif
         endif                                                     
c-- back for next event                                         
         goto 5			
       else                                                                     
c-- 3 is end of time period                       
       if(status.ne.3)       
     * write(6,*)' ****** Something wrong, status= ', status                 
       endif                                                                    
c
c blank out name
c
       call put_env_event('     ')
c                                                                               
c  print out statistics                                                         
c                                                                               
      write(6,*)                                                                
c     write(6,200) outfile                                                      
c200  format(' Output file is:   ',a40)                                         
      write(6,*)                                                                
      write(6,201) nevent,nl,nr,nd,records                                      
 201  format(' Total number of events          ',i7,                            
     *     /,' Total number local events       ',i7                             
     *     /,' Total number of regional events ',i7                             
     *     /,' Total number of distant events  ',i7                             
     *     /,' Total number of records         ',i7)                            
      stop                                                                      
      end
