c  GS2RAW - converts a GSMAP ascii file into a .raw file for input to REORDER.
c           written by Jeff Phillips.
        character tab*1, ifile*80
        dimension mtype(1000)
        tab=char(9)
        print*
        print*,'Convert selected line types from a GSMAP ascii file'
        print*,'into a .raw file for input to REORDER.'
        print*
        print '(a\)',' Name of gsmap ascii file: '
	read(5,102) ifile
        open(10,file=ifile,form='formatted',status='old')
        print '(a\)',' Name of output .raw file: '
	read(5,102) ifile
        open(11,file=ifile,form='formatted',status='unknown')
5       rewind(10)
        print '(a\)',' Selected line type code (-1 to exit): '
        read(5,*) jtype
        if(jtype.eq.-1) go to 999
        ntype=0
        n=0
        read(10,102,end=29,err=29) ifile
	do i=1,4
                read(10,*) alat,alon
	enddo
10      read(10,*,end=29,err=29) iline,ipts,itype,ip1,ip2
        if(itype.ne.jtype) then
                if(ntype.eq.0) go to 21
                do 20 i=1,ntype
                if(itype.eq.mtype(i)) go to 22
20              continue
21              ntype=ntype+1
                mtype(ntype)=itype
22              continue
                do i=1,ipts
                read(10,*) alat,alon
                end do
                go to 10
        endif
	if(ipts.gt.5000) then
		print *,'too many points'
		stop 0
	endif
        if(ipts.eq.1) then
          read(10,*) alat,alon
          go to 10
        endif
        n=n+1
        write(11,*) itype
        do i=1,ipts-1
        read(10,*) alat,alon
        write(11,*) alat,alon
        end do
        read(10,*) alat,alon
        write(11,105) alat,alon
	go to 10
102     format(a)
105     format(2f16.6,'//')
106     format(a5,1x,2f16.6,1x,a5)
29      print*,n,' lines converted.'
        if(ntype.eq.0) go to 31
        print*,'Other line types found:'
        do 30 i=1,ntype
        print*,mtype(i)
30      continue
31      go to 5
999     stop
	end	

