c--------( XSLIN : convert AM line data into StdLIN data )--------
      dimension v(30)
      character*80 wdr, flog, fnam1, fnam2
      character cu*4, cg*3, cgn*3, ca*14, can*14, buf*150, sdtm*20
c
      call premsg('XSLIN : convert AM line data into StdLIN data')
      call opnpin()
      ldr = lwkdir(50,wdr) + 1
      flog = wdr
      fnam1 = wdr
      fnam2 = 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.1) then
        k1 = 5
        k2 = 6
        k3 = 7
        k4 = 9
        kmx = 9
        ku = 2
        cu = 'Deg.'
      else if (ktyp.eq.2) then
        k1 = 2
        k2 = 3
        k3 = 0
        k4 = 4
        kmx = 4
      else if (ktyp.eq.3) then
        k1 = 3
        k2 = 4
        k3 = 10
        k4 = 8
        kmx = 10
      else if (ktyp.eq.4) then
        k1 = 1
        k2 = 2
        k3 = 3
        k4 = 4
        kmx = 4
      else if (ktyp.eq.0) then
      else
        call abendm('invalid value')
      endif
      call gparma('Enter Input AM line data filename ==> ',
     *            81-ldr, fnam1(ldr:80))
      open(1,file=fnam1,status='old')
      call gparmi(' Geodetic ?   in WGS(1) or TokyoDatum(2) ==> ', kg)
      if (kg.eq.1) then
        cg = 'WGS'
        call premsg(' Alt. ?   above ITRF/GRS Ellipsoid (1)')
        call gparmi('            or Geoid / Tokyo-Datum (2) ==> ', ka)
        if (ka.eq.1) then
          ca = 'above ITRF-GRS'
        else if (ka.eq.2) then
          ca = 'above Geoid   '
        else
          call abendm('invalid value')
        endif
      else if (kg.eq.2) then
        cg = 'TYO'
        ka = 2
        ca = 'above Geoid   '
      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')
        call gparmi2('  data positions of  Alt., Mag. : ', k3,k4)
        if (k3.le.0.or.k3.eq.k1.or.k3.eq.k2 .or.
     *      k4.le.0.or.k4.eq.k1.or.k4.eq.k2.or.k4.eq.k3)
     *       call abendm('invalid value')
        kmx = max(k1, k2, k3, k4)
        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')
        read(1,'(20x,f6.0,a2)') calt, buf
        valt = calt * 0.3048
      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 StdLIN data filename ==> ',
     *            81-ldr, fnam2(ldr:80))
      open(2,file=fnam2,status='new')
      call gparmi(' Geodetic ?   in WGS(1) or TokyoDatum(2) ==> ', kgn)
      if (kgn.eq.1) then
        cgn = 'WGS'
        call premsg(' Alt. ?   above ITRF/GRS Ellipsoid (1)')
        call gparmi('            or Geoid / Tokyo-Datum (2) ==> ', kan)
        if (kan.eq.1) then
          can = 'above ITRF-GRS'
        else if (kan.eq.2) then
          can = 'above Geoid   '
        else
          call abendm('invalid value')
        endif
      else if (kgn.eq.2) then
        cgn = 'TYO'
        kan = 2
        can = 'above Geoid   '
      else
        call abendm('invalid value')
      endif
      write(6,'(a)') '----------------------------------------'
      write(6,'(a)') 'XSLIN : convert AM line data into StdLIN data'
      write(6,'(a,a)')
     *     ' Input AM line data filename : ', fnam1(1:lrtrim(fnam1))
      write(6,'(8x,i2,5a,i2,2a)')
     *     k1, ': ', cg, '-Lat.(', cu, ')   ', k3, ': Alt.(m) ', ca,
     *     k2, ': ', cg, '-Lon.(', cu, ')   ', k4, ': Mag.(nT)', ''
      write(6,'(a,a)')
     *     ' Output StdLIN data filename : ', fnam2(1:lrtrim(fnam2))
      write(6,'(8x,4a)') 'Geodetic in ', cgn, ';  Alt. ', can
      write(2,'(a)') '# output from XSLIN'
      write(2,'(4a)') '#   Geodetic in ', cgn, ';  Alt. ', can
      write(2,'(a,a)')
     *     '# org. line data filename : ', fnam1(1:lrtrim(fnam1))
c
    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)))
        write(2,'(a)') buf(1:lrtrim(buf))
        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
        if (ktyp.eq.0) then
   21     lc = index(buf,':')
          if (lc.eq.0) goto 22
          buf(lc:lc) = ' '
          goto 21
        endif
   22   read(buf,*) (v(i),i=1,kmx)
      endif
      vlat = v(k1)
      vlon = v(k2)
      if (ktyp.ne.2) valt = v(k3)
      vmag = v(k4)
      if (ku.eq.2) then
        vlat = vlat * 60.
        vlon = vlon * 60.
      endif
      if (kg.eq.1) then
        if (kgn.eq.1) then
          if (ka.ne.kan) then
            call xw84t(vlat, vlon, valt, tlat, tlon, talt)
            if (ka.eq.1) then
              valt = talt
            else
              valt = valt+valt - talt
            endif
          endif
        else
          call xw84t(vlat, vlon, valt, tlat, tlon, talt)
          vlat = tlat
          vlon = tlon
          if (ka.eq.1) valt = talt
        endif
      else
        if (kgn.eq.1) then
          call xtw84(vlat, vlon, valt, wlat, wlon, walt)
          vlat = wlat
          vlon = wlon
          if (kan.eq.1) valt = walt
        endif
      endif
      write(2,'(f10.4,2hN ,f10.4,2hE ,f7.1,2hm ,f7.1,2hnT)')
     *     vlat, vlon, valt, vmag
      goto 1
    2 close(2)
      close(1)
c
      if(flog(ldr:ldr).ne.' ') then
        open(99,file=flog,access='append')
        write(99,'(//a)')
     *       'XSLIN : convert AM line data into StdLIN data'
        write(99,'(a,a)')
     *       ' Input AM line data filename : ', fnam1(1:lrtrim(fnam1))
        write(99,'(8x,i2,5a,i2,2a)')
     *       k1, ': ', cg, '-Lat.(', cu, ')   ', k3, ': Alt.(m) ', ca,
     *       k2, ': ', cg, '-Lon.(', cu, ')   ', k4, ': Mag.(nT)', ''
        write(99,'(a,a)')
     *       ' Output StdLIN data filename : ', fnam2(1:lrtrim(fnam2))
        write(99,'(8x,4a)') 'Geodetic in ', cgn, ';  Alt. ', can
        call strdtm(sdtm)
        write(99,'(a,a,a)') '=============== ', sdtm, '==============='
        close(99)
      endif
      stop
      end
