C     Program to extract a set of GEBCO coastline data info.
C     GEBCO data file is read from standard input.
C     Data extraction directives are given on the command line:
C
C     -contour x - Contour level x
C     -contours x y - Contour levels x <= z <= y
C     -prefix n - Prefix to output files generated.  A succession of files
C         is written, each containing no more than -max points.
C     -max x - Max number of points in a file.  If the number of data points
C         exceeds this value, a new file is written.
C     -summary - just give a summary of contour levels encountered in file.

      parameter (nblk=8)
      real x(nblk),y(nblk)
      character arg*128, fpfx*128
      integer clo,chi,cold
      logical summ
      data nmax /10000/, summ /.false./, clo, chi, cold /0,99999,-1/

C     Parse parameters
      fpfx = ' '
      iskip = 0
      do 10 i=1,iargc()
	 if (i .le. iskip) go to 10
	 call getarg(i,arg)
	 if (arg .eq. '-contour') then
	    call getarg(i+1,arg)
	    ios = -1
	    if (arg .ne. ' ') read(arg,*,iostat=ios) clo
	    if (ios .ne. 0) stop '**Invalid contour level.'
	    chi = clo
	    iskip = i+1
	 else if (arg .eq. '-contours') then
	    call getarg(i+1,arg)
	    call getarg(i+2,arg(1+index(arg,' '):))
	    ios = -1
	    if (arg .ne. ' ') read(arg,*,iostat=ios) clo,chi
	    if (ios .ne. 0) stop '**Invalid contour levels.'
	    iskip = i+2
	 else if (arg .eq. '-max') then
	    call getarg(i+1,arg)
	    ios = -1
	    if (arg .ne. ' ') read(arg,*,iostat=ios) nmax
	    if (ios .ne. 0) stop '**Invalid max point count.'
	    iskip = i+1
	 else if (arg .eq. '-prefix') then
	    call getarg(i+1,fpfx)
	    iskip = i+1
	 else if (arg .eq. '-summary') then
	    summ = .true.
	 else
	    write(0,*) '**Unrecognized parameter: ',
     +         arg(1:index(arg,' '))
	 endif
10    continue

C     Now read data from standard input and dump out data in correct format.

      n = 0
      if (fpfx .ne. ' ') then
	 iunit = 1
	 nsfx = -1
	 call checkf(n,-1,iunit,fpfx,nsfx)
      else
	 iunit = 6
      endif
      if (.not. summ) write(iunit,*) 'GEBCO data, contours ',clo,chi
      cold = -1
100   continue
199   format(16f7.3)
	 read(5,*,end=1000) icode,icount
	 if (icode .gt. chi) go to 1000
C        GEBCO database version of Dec. 1994 has wrong count on record
C           49360 of sheet 5.03.  No other count field has this value, so
C           we just correct it.
	 if (icount .eq. 16422) icount = 17363
	 if (icode .ge. clo .and. icode .le. chi) then
	    if (icode .ne. cold) then
	       cold = icode
	       if (summ) then
		  write(6,*) 'Contour level at ',icode
	       else
		  write(iunit,*) 'GEBCO data, contour ',icode
	       endif
	    endif
	    if (summ) then
	       read(5,*) (x(1),y(1),i=1,icount)
	       go to 100
	    endif
	    k = 0
	    do i=1,icount
	       k = k + 1
	       read(5,*) x(k),y(k)
	       if (y(k).le.-100.00) y(k) = y(k)+360.0
	       if (k .eq. nblk) then
		  call checkf(n,nmax-nblk,iunit,fpfx,nsfx)
		  write(iunit,199) (x(j),y(j),j=1,nblk)
		  k = 0
		  n = n + nblk
	       endif
	    enddo
	    call checkf(n,nmax-(k+1),iunit,fpfx,nsfx)
	    write(iunit,199) (x(j),y(j),j=1,k),99.0,0.0
	    n = n + k + 1
	 else
	    do i=1,icount
	       read(5,'(a1)') arg
	    enddo
	 endif
      go to 100

1000  continue
      end

      subroutine checkf(n,nmax,iunit,fpfx,nsfx)
C     Subroutine to check whether data in file has overflowed.
      character fpfx*(*), sfx*2
      logical q

      if (n .gt. nmax .and. fpfx .ne. ' ') then
	 inquire(unit=iunit,opened=q)
	 if (q) close(iunit)
	 nsfx = nsfx + 1
	 write(sfx,'(i2.2)') nsfx
	 open(iunit,file=fpfx(1:index(fpfx,' ')-1)//'.'//sfx)
	 n = 0
      endif
      end

