c$debug
c
c	This program identifies possible "Cluster"
c       activity, around a time-space window centered
c       in a "Big" event.
c	The program receives a files, which contains
c	a catalog of earthquakes (inf1). This catalog must
c	be in nordic format
c	Some of the more relevant aspects of the pro-
c	cedure followed are:
c		- The Magnitude above which value, an
c		  event is considered a main one (xmag).
c		- The Time window considered for sear-
c		  ching the cluster (lday & hday)
c		- The distance from the source
c      The program output will be (cluster.cat) a catalog
c      equal to inf1 but now containing '?' marks at the
c      end of the line of each apparent cluster event.
c 
c      May , 1994         Juan Pablo Ligorria
c      Modified March,95     -  "  -
c      Feb 1999 by jh   : ----------   version 7.0 check ----------------
c                         no changes
c
c
      character*80 inf1
      character*81 card1(14000)
      real lat2,lon2,lat1,lon1,dist,distance,mdis
      real mag1,mag2,mag3,max
      integer yr2,mn2,day2,yr1,mn1,day1
      integer lday,hday,day,cday,bef,aft



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

      write(*,*)' File with catalog of earthquakes....'
      read(*,'(a)')inf1
      open(1,file=inf1,status='old')
      open(4,file='cluster.out',status='unknown')
      do i=1,14000
       read(1,'(a)',end=99)card1(i)
      enddo

99    n1 = i - 1
      write(*,*)n1,' events in catalog'

      write(*,*)' Magnitude of Big event ? '
      read(*,*)max 
      write(*,*)' Number of days before event ? (integer)'
      read(*,*)bef
      write(*,*)' Number of days after Big event ? (integer)'
      read(*,*)aft
      write(*,*)' Minimum distance for analysis ? '
      read(*,*)mdis

      do i=1,n1

c     if(card1(i)(80:80) .ne. '1') go to 98
c     if(card1(i)(1:1) .ne. '*') go to 98

      read(card1(i),100)yr2,mn2,day2,lat2,lon2,mag1,mag2,mag3
100   format(1x,i4,1x,2i2,13x,f7.0,f8.0,18x,3(f3.0,5x))

      xmag = amax1(mag1,mag2,mag3)

      if(xmag .ge. max)then
         write(*,*)'Big event in',i,' card'
         write(4,*)'    '
         card1(i)(81:81)= ' '
         write(4,'(a)')card1(i)
	 day = yr2*365 + mn2*30 + day2
	 lday = day - bef
	 hday = day + aft
	 do k = i-50,i+50
	   kk = k
	   if(kk .lt. 1)kk = 1
	   if(kk .gt. n1)kk = n1
	   if(card1(kk) .eq. card1(i)) go to 97
c	   if(card1(kk)(80:80) .ne. '1') go to 97
	   read(card1(kk),100)yr1,mn1,day1,lat1,lon1,mag1,
     +         mag2,mag3
	   cday = yr1*365 + mn1*30 + day1

	   if(cday .gt. lday .and. cday .lt. hday)then
	      dist = distance(lat1,lat2,lon1,lon2)
	      if(dist .le. mdis)then
	        card1(kk)(81:81) = '?'
                write(4,'(a)')card1(kk)
c               write(*,'(a)')card1(kk)
              endif
           endif
97         continue
	 enddo
       endif
98    continue
      enddo

c     do i =1,n1
c	write(4,'(a)')card1(i)
c     enddo

      write(*,*)' Output file is "cluster.out"'
      end
ccccccccccccccccccccccccccccccccccccccccccccccccccccccc
      function distance(lat1,lat2,lon1,lon2)
      real lat1,lat2,lon1,lon2
      rad = (lat1*3.1415)/180.
      y = abs(lat1-lat2)*111.
      x = abs(lon1-lon2)*111.*cos(rad)
      distance = sqrt(x**2 + y**2)
      end
ccccccccccccccccccccccccccccccccccccccccccccccccccccccc
