c***********************************************************************
c***********************************************************************
c                                                                      *
c       program gmt2plates                                             *
c                                                                      *
c purpose:  this program reads in a gmt-formatted xy file and converts *
c      it to a PLATES-formatted file                                   *
c                                                                      *
c                                                                      *
c                       PLATES                                         *
c                       U.T. Institute for Geophysics                  *
c                       4412 Spicewood Springs Road, Bldg. 600         *
c                       Austin, Texas  78759-8500                      *
c                                                                      *
c  written by:  L.M. Gahagan            July 16, 1996                  *
c  modified by:  LMG                    August 18, 1998                *
c                                                                      *
c***********************************************************************
c***********************************************************************
	program gmt2plates
c
        character name*60,idtype*2,astr*30
	character acom*40, bcom*30
	character bstr*10
        integer iref,istr,id,itype,iclr,ipx
        real xlat,xlon,t1,t2
c
c  initialize variables
c
	astr=' test'
        iref=9999
        idtype='CS'
        itype=1
        t1=999
        t2=-999
        istr=1
	icnt = 1
        iclr=1
	ipoint=1
	id=101
	ipx=1
c
        write(*,*)' '
        write(*,*)'               gmt2plates'
        write(*,*)'  '
        write(*,*)'                 PLATES'
        write(*,*)'        Institute for Geophysics'
        write(*,*)'      University of Texas at Austin'
        write(*,*)'  4412 Spicewood Springs Road, Bldg. 600' 
        write(*,*)'         Austin, TX  78759-8500' 
        write(*,*)' '
	write(*,*)'This program takes a gmt-formatted file'
	write(*,*)'and converts it to PLATES-formatted file'
c
 5      write(*,*)'Enter name of input file'
	write(*,*)'(type quit to end program)'
        read(*,'(a)',err=5) name
	if(name(1:4).eq.'quit' .or. name(1:4).eq.'QUIT') goto 99
        open(unit=2,file=name,status='old',err=5)
        write(*,*) '  '
        write(*,*) 'enter name of output file'
        read(*,'(a)') name
        open(unit=3,file=name,status='unknown')
        write(*,*)' '
        write(*,*)'Are these data:'
	write(*,*)'1) lines  or 2) points?'
        read(*,*) ipx
        if(ipx.ne.2) ipx=1
	write(*,*)' '
	write(*,*)'...working...'
	write(*,*)' '
	ipen=2
	ihead=0
c
 15	continue
	read(2,17,err=97,end=98) acom
 17     format(a40)
        if(acom(1:1).eq.'>') then 
	  if (istr.ne.1) write(3,1004) 99.,99.,3
          write(3,1001) iref,istr,acom
          write(3,1002) 
     *    id,t1,t2,idtype,itype,id,iclr,ipoint,bstr
	  ihead=1
	  istr=istr+1
          goto 15
	endif
	read(acom,*,err=97,end=98) xlon,xlat
	if (ihead.eq.1) then
	  ipen=3
	else 
          if (ipx.eq.1)then
	    ipen=2
          else
 	    ipen=3
	  endif
	endif
        if(xlon.gt.180) xlon=xlon-360
	write(3,1004) xlat,xlon,ipen
	ihead=0
	goto 15
c
c
c
 1001	format(i4,i5,x,a40)
 1002   format(x,i3,x,f6.1,x,f6.1,x,a2,i4,x,i3,x,i3,x,i5,x,a10)
 1004   format(f9.4,1x,f9.4,x,i1)
c
 97     write(*,*)'Error'
        write(*,1001) iref,istr,astr
        write(*,17) acom,xlon,xlat
 98     continue
	write(3,1004) 99.,99.,3
	write(*,*)'End program'
	close(unit=2)
	close(unit=3)
	goto 5
 99	continue
        stop
        end
