      subroutine evread (record, ios)

      character*96 record

c      read in an event

c nin - input stream number
c record - last record read from ffb file
c 
c ios - i/o status code - normally returns 0, -1 after eof detected

c Sept 8th 2003 - decide to use a direct-access file for ALL records
c - enabling direct reference to a specific record

      character*3 source
      logical primed, head, addin
      integer nov/0/
c      character*4 chival

      include 'basics.inc'

      include 'hold.inc'

c open direct-access scratch file

      open(unit=ntemp,form='unformatted',status='scratch',
     1 access='direct',recl=96)
     
c head is set .true. until an initial phase record is read

      head = .true.
      addin = .true.
      primed = .false.
      norec = 0
      nov = nov + 1
      
c Main 'DO' loop of evread
      
      do while (ios .eq. 0 .and. addin)
      
        norec = norec + 1
        
        if (.not.primed) then

c primed set .true. if primary estimate found

          primed = record(1:2) .eq. ' 1' .and. record(26:26) .eq. 'A'
          if (primed) then
            source = record(23:25)
            locprm = norec
          end if
        end if
        
        lenrec = len_trim(record)
        
c guarantee remainder of record is blank

        do i=lenrec+1,96
           record(i:i) = ' '
        end do
                
c Sept 8th 2003 - write EACH record directly to direct-access file
   

        write(ntemp,rec=norec) record
  
       
c read next record from input FFB file
        
        read (nin, '(a)', iostat=ios) record
        
        if (ios .eq. 0) then
        
          lenrec = len_trim(record)
          
          if (lenrec .gt. 4) then    ! if rec-length > 4
            addin = .true.
            if (primed) then
              if (record(1:2) .eq. ' 1') then

c if another estimate is found after having read the prime estimate
c then this must be a new event

                addin = .false.
              else if (record(1:2) .eq. ' 3') then

c a comment from the prime agency could follow the prime estimate
c but cannot be the same event if phase records have been read

c Changed 20/11/03 - such that ONLY comments after the prime with
c agency=prime-agency AND the prime-estimate-flag = 'A' are in the
c same event

c                addin = (record(21:23) .eq. source) .and. head

                addin = (record(21:23) .eq. source) .and. head
     1 .and. (record(24:24) .eq. 'A') 
     
                    
              else if (record(1:2) .eq. ' 5'
     2             .or. record(1:2) .eq. '15') then

c phase data has been reached so the next epicentre or comment record
c group must be the next event

                head = .false.
              end if
            end if
            
          else       ! else of 'if rec-length > 4 ' IF
          
            call abort ('Short record found')
            
          end if     ! end of 'if rec-length > 4 ' IF
          
        end if      ! end of 'if(ios .eq. 0)' IF for input FFB
        
      end do
      
c End of Main 'DO' loop of evread
      
      
      return
      end

