c$debug
c
c    program for reading eqrisk output file and making 
c    a file sutible for epimap for plotting
c
c    jh jun 95
c
c    changes:
c    june 99 jh  : ----------  version 7.0 -------------------
c                 add version call
c
      implicit none
      real lat(5000),lon(5000)            ! latitude and longitude
      real minlat,maxlat,minlon,maxlon    ! extreme -----------
      integer nlat,nlon                   ! number of gridpoints ---
      integer ncon                        ! number of contours
      character*80 infile     ! input file name
      character*80 text(100)  ! help text
      character*80 textt      ! text
      character*63 help(10)   ! more help text
      real levels(6,5000)     ! intensity levels for different return periods
      integer ntypes          ! number of types of output pgv,pga etc
      integer nfiles          ! number of files for output
      integer nvalues         ! total number of values
      real   oldlat,oldlon    ! help variable
      real xnumber(14)        ! contour levels to plot
      real risks(6)           ! risks, max 6
      real del_level          ! level division
      character*10 xlevel (20)! contour levels to plot
      real   maxlevel,minlevel ! extreme levels
      integer i,j,k,n,l,nout,nlevel,kstart
      data xnumber /0.01,0.02,0.05,0.1,0.2,0.5,1.0,2.0,5.0,10.0,
     *              20.0,50.0,100.0,200.0/


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

      help(1)=
     *' This file is input for epimap contour plotting. The input of   '
      help(2)=
     *' parameters must appear as listed below with the actual data    '
      help(3)=
     *' starting after the line Fields to use. There can be any        '
      help(4)=
     *' number of header lines. The contour values plotted are         '
      help(5)=
     *' plotted with the number of decimals given below. The last      '
      help(6)=
     *' part of the file is the actual longitudes, latiudes and        '
      help(7)=
     *' levels. Color use is optional and the field can be left        '
      help(8)=
     *' blank, the codes are as follows:                              '
      help(9)=
     *'                                                               '
      help(10)=
     *' 1: blue   2: green   3: red   4: yellow   5: white   6: black '

c
      ncon=10
      write(6,*) ' Input file name'
      read(5,'(a)') infile
      open(1,file=infile,status='old')
c
c   read some text
c
      do i=1,14
        read(1,'(a)') text(i)
      enddo
c
      i=1
c
c   read lon lat points and levels
c
 100  continue
      read(1,'(a)',end=102) textt
      if(textt(2:26).eq.'RESULTS FOR SITE LOCATION') then
         read(textt(28:47),'(2f10.2)') lon(i),lat(i)
c
c   read forward to intensity
c
 101     continue
         read(1,'(a)',end=102) textt
         if(textt(18:22).eq.'RISKS') then
             read(textt(25:80),'(6f9.6)') risks
             read(1,'(a)',end=102) textt
             read(1,'(a)',end=102) textt
             read(textt(25:80),'(6f9.2)')(levels(k,i),k=1,6)
             i=i+1
c             write(6,*) lon(i-1),lat(i-1)
         else
             goto 101
         endif
      endif
      goto 100
c
c   end of input
c
 102  continue
      nvalues=i-1
      write(6,*) ' Number of points', nvalues
c
c   find how many risk levels which is the same as number of output files
c
      do k=1,6
         if(risks(k).eq.0.0) then
            nfiles=k-1
            goto 103
         endif
      enddo
 103  continue
      write(6,*)' Number of risk levels', nfiles
c
c   open output files
c
      textt(1:11)='eqrsei1.out'
      do k=1,nfiles
         write(textt(7:7),'(i1)') k
         open(k+9,file=textt(1:11),status='unknown')
      enddo
c
c   find range
c
      nlat=0
      nlon=0
      maxlon=-500.0
      minlon=500.0
      maxlat=-500.0
      minlat=500.0
      oldlat=-999.0
c
      do i=1,nvalues
         if(lat(i).lt.minlat) minlat=lat(i)
         if(lat(i).gt.maxlat) maxlat=lat(i)
         if(lon(i).gt.maxlon) maxlon=lon(i)
         if(lon(i).lt.minlon) minlon=lon(i)
         if(lat(i).ne.oldlat) nlat=nlat+1
         oldlat=lat(i)
      enddo
      write(6,*)' Min and max lat', minlat,maxlat
      write(6,*)' Min and max lon', minlon,maxlon 
      write(6,*)' Number of latitude points', nlat
c
      oldlon=lon(1)
      do i=1,nvalues
c	    write(6,*)oldlon,lon(i)
        if(lon(i).lt.oldlon) then
          nlon=i-1
          goto 105
        endif
        oldlon=lon(i)
      enddo
 105  continue
c
      write(6,*)' Number of longitude points', nlon
c
c   write out
c
c
c  write help text in files
c
      do k=10,nfiles+9
	    nlevel=k-9
        write(k,'(a)') (text(i),i=1,14)
        write(k,'(a)') (help(i),i=1,10)
        maxlevel=0
        minlevel=5000.0
        do i=1,nvalues
           if(levels(nlevel,i).gt.maxlevel) maxlevel=
     *     levels(nlevel,i)
           if(levels(nlevel,i).lt.minlevel) minlevel=
     *     levels(nlevel,i)
        enddo
        write(6,*) ' Min and max level = ',minlevel,maxlevel
        write(k,*)
        write(k,'(a,2f10.4)') ' Min and max level',minlevel,maxlevel
        write(k,*)
c
c   find levels to use
c
        del_level=(maxlevel-minlevel)/float(ncon)
        do i=1,14
          if(xnumber(i).ge.del_level) then
             del_level=xnumber(i)
             goto 20
          endif
        enddo
 20     continue  
c
c   write levels in xlevel with appropiate accuracy
c
         do i=1,ncon
           if(del_level.lt.0.1)  then
              write(xlevel(i),'(3x,f5.2)') del_level*i+minlevel
              goto 30
           endif
           if(del_level.lt.1.0)  then 
              write(xlevel(i),'(3x,f5.1)') del_level*i+minlevel
              goto 30
           endif
           if(del_level.lt.1000.)  then
              write(xlevel(i),'(3x,i3)') int(del_level*i+minlevel)
           endif
         enddo
 30      continue
c
         write(k,'(a,a)')
     *  ' Fields to use                         ',
     *  ' I.........I.........I.........I   '
        write(k,'(a,2f10.2,i9)')
     *' Latitude range and number of values    ',
     *  minlat,maxlat,nlat
        write(k,'(a,2f10.2,i9)')
     *' Lontitude range and number of values   ',
     *  minlon,maxlon,nlon
        do l=1,ncon
           write(k,'(a,a10)')
     *     ' Contour level to plot and color        ',xlevel(l)
        enddo
c
c   write contours
c
      do l=1,nvalues
        write(k,*)lon(l),lat(l),levels(nlevel,l)
      enddo
      enddo
      write(6,*)' Output files are eqrsei1.out,',
     *' eqrsei2.out, eqrsei3.out, etc'
      stop
      end

        
