c--------------------------------------------------------------------------
c  Sample program for reading one s-file at a time in a loop and put
c  all parameters into the rea common block.
c
c  Some parameters are then modified and the modified event is written
c  to the output file.
c--------------------------------------------------------------------------c
c
c  For detail on parameters and variables naames, see rea.inc
c
c  To compile this program, use command make sample_read_write_s on sun
c  and nmake sample_read_write_s on pc
c
c
      implicit none                       ! force delcaration of all variables
      include 'seidim.inc'                ! dimensions for rea block
      include 'rea.inc'                   ! parameter common bliock

      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                           ! counter
c
c   open output file

       open(2,file='sample_read_write_s.out',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
c
      all=.true.                  ! read all parameters
      nevent=0                    ! initialize counter

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 on screen a bit info on event, number of headers and
c   total number of records
c
      write(6,*)' Number of headers and number of records',
     *rea_nhead,rea_nrecord

c
c   write the whole first header line
c
      write(6,'(a)') data(1)(1:79)
c
c   some example of modifying event
c
c----------------------------------------------------
c - remove lines with waveform file names
c----------------------------------------------------
c
      rea_nwav=0
c
c----------------------------------------------------
c - add 1 sec to all s-readings, no no check fo rchange
c   hour unless option abslute time used, see routiene
c   rea_phase:_out
c
c   delete p-phases
c----------------------------------------------------
c
      do i=1,rea_nphase
        if(rea_phase(i)(1:1).eq.'S') rea_sec(i)=rea_sec(i)+1.0
        if(rea_phase(i)(1:1).eq.'P') rea_phase(i)(1:6)='DELETE'
      enddo
c
c---------------------------------------------------------
c   select all traces with s-wave spectral values, print
c   on screen station, time and log moment
c----------------------------------------------------------
c
      do i=1,rea_nphase
          if(rea_phase(i)(1:5).eq.'SPECS') then
              write(6,'(1x,a4,1x,a5,2i2,f6.1,f6.2)')
     *        rea_stat(i), rea_comp(i), rea_hour(i),
     *        rea_min(i),rea_sec(i),rea_moment(i)
          endif
      enddo
c
c------------------------------------------------
c   write out all magnitudes form all hypocneters
c------------------------------------------------
c
      write(6,'(1x,6(f4.1,a1,a4,1x))') (hyp_mag_all(i),
     *hyp_mag_type_all(i),hyp_mag_agency_all(i),i=1,rea_nmag)
c
c   write out modified event, the array data has also been modified
c
       call rea_event_out(2,all,data,code)
c
c   get next event
c
      goto 50
c
c     end of file
c
 1000 continue
c
      write(6,*)            ! blank line
      close(2)              ! close output file
      write(6,*) 'Number of events in input file', nevent
      write(6,*) 'Output file name is sample_read_write_s.out'

      stop
      end
