c--------( PLTRK : plot trackline path from AM line data )--------
      dimension v(30)
      character*80 wdr, flog, fnin, fnam
      character area*8, buf*150, aval*8, anc*4
      character cu*4, cg*3, yn*1, spl*3
      external cviken
c
      call premsg('PLTRK : plot trackline path from AM line data')
      call opnpin()
      ldr = lwkdir(50,wdr) + 1
      flog = wdr
      fnin = wdr
      fnam = wdr
      call gparma('Enter LOG filename ==> ', 81-ldr, flog(ldr:80))
      call premsg('Select AM Line data type :')
      call premsg('  DPAM Line data (1), AMDB-GSJ Line data (2),')
      call premsg('  AMDB-NEDO Line data (3), StdLIN data (4),')
      call gparmi('   or general Random data (0) ==> ', ktyp)
      ku = 1
      cu = 'Min.'
      if (ktyp.eq.0) then
      else if (ktyp.eq.1) then
        k1 = 5
        k2 = 6
        kmx = 6
        ku = 2
        cu = 'Deg.'
      else if (ktyp.eq.2) then
        k1 = 2
        k2 = 3
        kmx = 3
      else if (ktyp.eq.3) then
        k1 = 3
        k2 = 4
        kmx = 4
      else if (ktyp.eq.4) then
        k1 = 1
        k2 = 2
        kmx = 2
      else
        call abendm('invalid value')
      endif
      call gparma('Enter Input AM line data filename ==> ',
     *            81-ldr, fnin(ldr:80))
      open(1,file=fnin,status='old')
      call gparmi(' Geodetic ?   in WGS(1) or TokyoDatum(2) ==> ', kg)
      if (kg.eq.1) then
        cg = 'WGS'
      else if (kg.eq.2) then
        cg = 'TYO'
      else
        call abendm('invalid value')
      endif
      if (ktyp.eq.0) then
   10   read(1,'(a)',end=2) buf
        call premsg(buf(1:lrtrim(buf)))
        if (buf(1:1).eq.'&' .or. buf(1:1).eq.'%'
     *                      .or. buf(1:1).eq.'#') goto 10
        rewind(1)
        call gparmi('LatLon unit ?   in Min.(1) or Deg.(2) ==> ', ku)
        if (ku.eq.1) then
          cu = 'Min.'
        else if (ku.eq.2) then
          cu = 'Deg.'
        else
          call abendm('invalid value')
        endif
        call gparmi2('  data positions of  Lat., Lon. : ', k1,k2)
        if (k1.le.0.or.k2.le.0.or.k1.eq.k2)
     *       call abendm('invalid value')
        if (k1.gt.30.or.k2.gt.30) call abendm('too large data position')
        kmx = max(k1, k2)
        if (kmx.gt.30) call abendm('too large data position')
      else if (ktyp.eq.2) then
        read(1,'(a)') buf
        if (buf(1:2).ne.'##' .or. buf(27:28).ne.'ft')
     *       call abendm(' seems non AMDB-GSJ data')
      else if (ktyp.eq.3) then
        read(1,'(a)') buf
        if (buf(1:8).ne.'##  NEDO')
     *       call abendm('seems non AMDB-NEDO data')
      endif
c
      call gparma('Enter Output PS filename  ==> ',
     *            81-ldr, fnam(ldr:80))
      if(fnam(ldr:ldr).eq.' ') fnam(1:1) = char(0)
      if (fnam(80:80).ne.' ') call abendm('too long filename')
      call gparma('  Areaname (8 chrs max) : ', 8, area)
      call gparmi('Select Coordinate Number (+200 for WGS) ==> ', ncc)
      nc = ncc
      if(ncc.ge.200) nc = ncc - 200
      ispa = 0
      ispb = 0
      if(nc.ge.1.and.nc.le.62) then
        iorg = 0
        korg = (nc-30)*360 - 180
        if(nc.gt.60) korg = 0
        if(nc.eq.61) iorg = +5400
        if(nc.eq.62) iorg = -5400
      else
        call gparmi2('    Origin  Lat. & Long. in minutes : ',
     *               iorg, korg)
        if(nc.eq.72) then
          call gparmi2('    two Standard Latitudes of (in min.) : ',
     *                 ispa, ispb)
        endif
      endif
      call premsg('  Southwest corner Coord. in km ?')
      call gparmf('    Northing : ', xs)
      call gparmf('    Easting  : ', ys)
      call premsg('  Area size in km ?')
      call gparmf('    N-S range : ', rx)
      if (rx.le.0.) call abendm('invalid value')
      call gparmf('    E-W range : ', ry)
      if (ry.le.0.) call abendm('invalid value')
      call gparmi('  Select  1 (Portrait) or 2 (Landscape) ==> ', kpl)
      if (kpl.eq.1) then
        spl = 'A4P'
      else if (kpl.eq.2) then
        spl = 'A4L'
      else
        call abendm('invalid value')
      endif
      call psopn(fnam, spl)
      if(kpl.eq.1) then
        call gparmf('  Width of Drawing (in cm) ?  ==> ', wide)
        fscl = ry*1000. / wide
        high = rx*1000. / fscl
      else
        call gparmf('  Height of Drawing (in cm) ?  ==> ', high)
        fscl = rx*1000. / high
        wide = ry*1000. / fscl
      endif
      fscl = fscl * 100.
      ccm = fscl/100000.
      kscl = 0
      sclr = 0.
      sclu = 0.
      klll = 0
      kshl = 0
      if(fscl.lt.3333334..and.fscl.ge.20000.) then
        km = nint(fscl)/25000
        if(km.eq.0)  km = 1
        if(km.gt.50) km = 50
        if(km.gt.2.and.km.lt.5)   km = 2
        if(km.gt.5.and.km.lt.10)  km = 5
        if(km.gt.10.and.km.lt.20) km = 10
        if(km.gt.20.and.km.lt.50) km = 20
        iinc = km
        kinc = km + km/2
        if(km.eq.50) iinc = 60
        if(km.eq.1.or.km.eq.5.or.km.eq.50) kinc = iinc
        dscl = float(km)/ccm
   11   call prompt('  Draw  Scale Bar  ?  ')
        call gparma('  "y" (Yes) or "n" (No) ==> ', 1, yn)
        if(yn.ne.'y'.and.yn.ne.'n') goto 11
        if(yn.eq.'y') then
          kscl = 1
          write(anc,'(f4.1)') dscl
          call premsg('      size :' // anc // 'cm Wide, 0.8cm High')
          call prompt('    Lower-Left Pos. ? (right and up)')
          call gparmf2(' in cm ==> ', sclr, sclu)
        endif
   12   call prompt('  Draw Lat.Lon.Lines ?')
        call gparma('  "y" (Yes) or "n" (No) ==> ', 1, yn)
        if(yn.ne.'y'.and.yn.ne.'n') goto 12
        if(yn.eq.'y') klll = 1
      endif
   13 call prompt('  Draw Coastlines ?   ')
      call gparma('  "y" (Yes) or "n" (No) ==> ', 1, yn)
      if(yn.ne.'y'.and.yn.ne.'n') goto 13
      if(yn.eq.'y') then
        kshl = 2
   14   call prompt('       Rivers  ?      ')
        call gparma('  "y" (Yes) or "n" (No) ==> ', 1, yn)
        if(yn.ne.'y'.and.yn.ne.'n') goto 14
        if(yn.eq.'y') kshl = 12
   15   call prompt('       Pref.Boundary ?')
        call gparma('  "y" (Yes) or "n" (No) ==> ', 1, yn)
        if(yn.ne.'y'.and.yn.ne.'n') goto 15
        if(yn.eq.'y') then
          kshl = kshl + 200
          if(kshl.eq.202) kshl = 102
        endif
      endif
      call clspin()
      write(6,'(a)') '----------------------------------------'
      write(6,'(a)') 'PLTRK : plot trackline path from AM line data'
      write(6,'(a,a)')
     *     ' Input AM line data filename : ', fnin(1:lrtrim(fnin))
      write(6,'(8x,i2,5a,3x,i2,2a)')
     *     k1, ': ', cg, '-Lat.(', cu, ')',
     *     k2, ': ', cg, '-Lon.(', cu, ')'
      write(6,'(a,a)') ' Output PS filename  : ', fnam(1:lrtrim(fnam))
      write(6,'(a,a8,a,i4,4x,4i8)')
     *     'Areaname : ', area, '   Coord.', ncc, iorg,korg, ispa,ispb
      write(6,'(12x,a,f8.2,a,f8.2)')
     *     'SW corner :      x =', xs, '   y =', ys
      write(6,'(12x,a,f8.2,a,f8.2)')
     *     '  area size :    NS=', rx,   '   EW=', ry
      write(6,'(6x,a,f8.0,4x,a3,f8.1,a1,f8.1,a1)')
     *     'scale  1 /', fscl, spl, high, 'H', wide, 'W'
      if(kscl.ne.0.or.klll.ne.0.or.kshl.ne.0) then
        write(6,'(a,$)') '      with '
        if(kshl.ne.0) write(6,'(a,$)') '  Coast'
        if(mod(kshl,100)/10.ne.0) write(6,'(a,$)') '+River'
        if(kshl/100.ne.0) write(6,'(a,$)') '+PrefB'
        if(klll.ne.0) write(6,'(a,$)') '  LatLonLines'
        if(kscl.ne.0) then
          write(6,'(a,f5.1,$)') '  ScaleBar [', sclr
          write(6,'(a,f5.1,a,$)') ',', sclu, ' ]'
        endif
        write(6,'(a)')
      endif
c
      call cvinit(ncc,float(iorg),float(korg),float(ispa),float(ispb))
      call plots(2., 2.)
      call wrect(0., 0., wide, high)
      call lstyle('T', 0.4, 0., 0, -255)
      write(aval,'(f8.1)') xs
      call ptext(aval, 8, -0.1, 0., 1)
      write(aval,'(f8.1)') xs+rx
      call ptext(aval, 8, -0.1, high-0.28, 1)
      write(aval,'(f8.1)') ys
      call ptext(aval, 8, 0.7, -0.4, 1)
      write(aval,'(f8.1)') ys+ry
      call ptext(aval, 8, wide, -0.4, 1)
      kp = 3
    1 read(1,'(a)',end=2) buf
      if(buf(1:1).eq.'&'.or.buf(1:1).eq.'%'.or.buf(1:1).eq.'#') then
        call premsg(buf(1:lrtrim(buf)))
        kp = 3
        goto 1
      endif
      if (ktyp.eq.4) then
        read(buf,'(f10.4,1x,f11.4,1x,f8.1,1x,f8.1,2x)') (v(i),i=1,4)
      else
        read(buf,*) (v(i),i=1,kmx)
      endif
      if (ku.eq.1) then
        vlat = v(k1)
        vlon = v(k2)
      else
        vlat = v(k1)*60.
        vlon = v(k2)*60.
      endif
      if (kg.eq.1.and.ncc.lt.200) then
        call xw84t(vlat, vlon, 0., tlat, tlon, alt)
        call cviken(tlat, tlon, ye, xn)
      else if (kg.eq.2.and.ncc.ge.200) then
        call xtw84(vlat, vlon, 0., wlat, wlon, alt)
        call cviken(wlat, wlon, ye, xn)
      else
        call cviken(vlat, vlon, ye, xn)
      endif
      px = (ye-ys) / ccm
      py = (xn-xs) / ccm
      call plot(px, py, kp)
      kp = 2
      goto 1
    2 close(1)
      if(kscl.ne.0) then
        call plot(sclr,      sclu+0.1, 3)
        call plot(sclr+dscl, sclu+0.1, 2)
        call newpen(2)
        call plot(sclr+dscl, sclu+0.3, 3)
        call plot(sclr+dscl, sclu,     2)
        call plot(sclr,      sclu,     2)
        call plot(sclr,      sclu+0.3, 2)
        write(anc,'(i2,a2)') km, 'km'
        call lstyle('T', 0.5, 0., 0, -255)
        call ptext(anc, 4, sclr+dscl/2., sclu+0.5, 2)
        call newpen(1)
      endif
      call scisor(0., 0., wide, high)
      if(klll.ne.0.or.kshl.ne.0) then
        call cvinit(ncc,float(iorg),float(korg),float(ispa),float(ispb))
        xt = xs + rx
        yt = ys + ry
        call cvenik(ys, xs, fi1, fk1)
        call cvenik(yt, xs, fi2, fk2)
        call cvenik(ys, xt, fi3, fk3)
        call cvenik(yt, xt, fi4, fk4)
        isf = nint(amin1(fi1,fi2) - 0.5)
        itf = nint(amax1(fi3,fi4) + 0.5)
        ksf = nint(amin1(fk1,fk3) - 0.5)
        ktf = nint(amax1(fk2,fk4) + 0.5)
      endif
      if(klll.ne.0) then
        do 200 i=isf/iinc,itf/iinc
          idf = i * iinc
          if(idf.lt.isf.or.idf.gt.itf) goto 200
          call cviken(float(idf), float(ksf), y, x)
          call plot((y-ys)/ccm, (x-xs)/ccm, 3)
          do 100 kdf=ksf+1,ktf
            call cviken(float(idf), float(kdf), y, x)
            call plot((y-ys)/ccm, (x-xs)/ccm, 2)
  100     continue
  200   continue
        do 400 k=ksf/kinc,ktf/kinc
          kdf = k * kinc
          if(kdf.lt.ksf.or.kdf.gt.ktf) goto 400
          call cviken(float(isf), float(kdf), y, x)
          call plot((y-ys)/ccm, (x-xs)/ccm, 3)
          do 300 idf=isf+1,itf
            call cviken(float(idf), float(kdf), y, x)
            call plot((y-ys)/ccm, (x-xs)/ccm, 2)
  300     continue
  400   continue
      endif
      if(kshl.ne.0) then
        call rshore(mc, isf/60, (itf+59)/60, ksf/60, (ktf+59)/60)
        call pshore(-ys/ccm, -xs/ccm, kshl, ccm, cviken)
      endif
      call scisor(0., 0., 0., 0.)
      call lstyle('T', 0.4, 0., 0, -255)
      call ptext(area, 8, -1., -1.5, 0)
      call ptext(fnin, 50, 2., -1.5, 0)
      call plote()
      call pscls()
c
      if(flog(ldr:ldr).ne.' ') then
        open(99,file=flog,status='old',iostat=ios)
        if(ios.eq.0) then
   90     read(99,*,end=91)
          goto 90
   91     backspace(99)
        else
          open(99,file=flog,status='new')
        endif
        write(99,'(//a,$)') 'PLTRK :'
        write(99,'(a)')   ' plot trackline path from AM line data'
        write(99,'(a,a)')
     *       ' Input AM line data filename : ', fnin(1:lrtrim(fnin))
        write(99,'(8x,i2,5a,3x,i2,2a)')
     *       k1, ': ', cg, '-Lat.(', cu, ')',
     *       k2, ': ', cg, '-Lon.(', cu, ')'
        write(99,'(a,a)')
     *       ' Output PS filename  : ', fnam(1:lrtrim(fnam))
        write(99,'(a,a8,a,i4,4x,4i8)')
     *       'Areaname : ', area, '   Coord.', ncc, iorg,korg,ispa,ispb
        write(99,'(12x,a,f8.2,a,f8.2)')
     *       'SW corner :      x =', xs, '   y =', ys
        write(99,'(12x,a,f8.2,a,f8.2)')
     *       '  area size :    NS=', rx,   '   EW=', ry
        write(99,'(6x,a,f8.0,4x,a3,f8.1,a1,f8.1,a1)')
     *       'scale  1 /', fscl, spl, high, 'H', wide, 'W'
        if(kscl.ne.0.or.klll.ne.0.or.kshl.ne.0) then
          write(99,'(a,$)') '      with '
          if(kshl.ne.0) write(99,'(a,$)') '  Coast'
          if(mod(kshl,100)/10.ne.0) write(99,'(a,$)') '+River'
          if(kshl/100.ne.0) write(99,'(a,$)') '+PrefB'
          if(klll.ne.0) write(99,'(a,$)') '  LatLonLines'
          if(kscl.ne.0) then
            write(99,'(a,f5.1,$)') '  ScaleBar [', sclr
            write(99,'(a,f5.1,a,$)') ',', sclu, ' ]'
          endif
          write(99,'(a)')
        endif
        close(99)
      endif
      stop
      end
