c-------------------------------------------------
      program convert1m

c This program generates longitude and latitude of points 
c of border prepared by digital maps
c 
c         put *.MBY data file at the same directory
c input : (1) First mesh number (ex:5941 for Morioka)
c         (2) 1:coastal line, 2:pref. border and coastal line, 3:all
c         (see convert1m.ctl)
c output: standard output to generate multi segment files for GMT
c usage: convert1m < convert1m.ctl > coast.dat
c
c coded by Tomotaka Iwata 27/03/98
c modified by Yasumaro Kakehi 09/01/99
c modified by Satoshi Fujiwara 05/12/2000

      character line*72,filnm*70

      read(5,*)n1stmesh
      write(filnm,1001)n1stmesh
1001  format(i4,'.MBY')
      nlat=int(n1stmesh/100)
      nlon=n1stmesh-nlat*100
      xorig=nlon+100.
      yorig=real(nlat)/1.5
c      write(*,*)xorig,yorig
      read(5,*) iboth

      open(10,file=filnm,form='formatted')

   90 continue
      read(10,'(a72)',end=100) line

      if(line(1:1).eq.'M') then
c        write(6,'(a72)') line
        call chg(line(7:7),im)
        call chg(line(8:8),in)
c       write(6,*) im*10+in
      else

c selection of information

        if(line(1:1).eq.'L'.and.line(4:4).eq.'1') then
          call chg(line(6:6),ip)
          if(iboth.eq.1) then
            if(ip.eq.5) then
              call gen(10,line,xorig,yorig,ip,im,in)
            endif
          else if(iboth.eq.2) then
            if(ip.eq.5.or.ip.eq.1) then
              call gen(10,line,xorig,yorig,ip,im,in)
            endif
          else if(iboth.eq.3) then
            if(ip.le.5) then
              call gen(10,line,xorig,yorig,ip,im,in)
            endif
          endif
        endif

        if(line(1:1).eq.'L'.and.line(4:4).eq.'5') then
          call chg(line(6:6),ip)
          if(ip.eq.2) then
            call gen(10,line,xorig,yorig,ip,im,in)
          endif
        endif

      endif

      go to 90
  100 continue
      close(10)
c      write(6,'(a1)') '>'

      stop
      end


      subroutine chg(cha,nint)

      character cha*1
      integer*4 nint

      if(cha.eq.' ') then
      nint=0
      else
      nint=ichar(cha)-48
      endif

    3 return
      end


      subroutine gen(ifile,line,xorig,yorig,ip,im,in)

      parameter (nn=10000)
      character line*72
      integer*4 ix(nn),iy(nn)
      real*4  x(nn),y(nn)

      call chg(line(50:50),i1)
      call chg(line(51:51),i2)
      call chg(line(52:52),i3)
      call chg(line(53:53),i4)
      call chg(line(54:54),i5)
      call chg(line(55:55),i6)
c      write(6,'(a72)') line
c      write(6,*) i1*100000+i2*10000+i3*1000+i4*100+i5*10+i6
      jj=i1*100000+i2*10000+i3*1000+i4*100+i5*10+i6

      do 1000  i=1,nn
      ix(i)=0
      iy(i)=0
      x(i)=0.
      y(i)=0.
 1000 continue

      read(ifile,'(14i5)') (ix(i),iy(i),i=1,jj)

      write(6,'(a1,i5)') '>',ip
      do  200  i=1,jj
      ix(i)=ix(i)+10000*in
      iy(i)=iy(i)+10000*im
      x(i)=xorig+ix(i)/80000.
      y(i)=yorig+iy(i)/80000.*(2./3.)

  200 continue
      write(6,'(2f9.5)') (x(i),y(i),i=1,jj)

      return
      end
c-------------------------------------------------
