c--------( XSLINA : extract StdLIN data from DPAM data      --------
c--------                        with averaged re-sampling )--------
      real*8 wtm
      character*80 wdr, flog, fnam1, fnam2
      character cu*4, cg*3, cgn*3, ca*14, can*14, buf*150, sdtm*20
c
      call premsg('XSLINA : extract StdLIN data from DPAM data '
     *            // 'with averaged re-sampling')
      call opnpin()
      ldr = lwkdir(50,wdr) + 1
      flog = wdr
      fnam1 = wdr
      fnam2 = wdr
      call gparma('Enter LOG filename ==> ', 81-ldr, flog(ldr:80))
      call gparma('Enter Input DPAM 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
c
      call gparmf('Averaging interval (in sec.) ==> ', tint)
      if (tint.lt.0.1 .or. tint.gt.5.) call abendm('invalid value')
      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)') 'XSLINA : extract StdLIN data from DPAM data '
     *               // 'with averaged re-sampling'
      write(6,'(a,a)')
     *     ' Input AM line data filename : ', fnam1(1:lrtrim(fnam1))
      write(6,'(8x,4a)')
     *     ' 5: ', cg, '-Lat.(Deg.)    7: Alt.(m) ', ca,
     *     ' 6: ', cg, '-Lon.(Deg.)    9: Mag.(nT)', ''
      write(6,'(a,f6.2,a)') '  Averaging interval :', tint, ' sec.'
      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
      kr = 0
      nd = 0
    1 read(1,'(a)',end=2) buf
      if (buf(1:1).eq.'&' .or. buf(1:1).eq.'%'
     *                    .or. buf(1:1).eq.'#') then
        if (nd.ne.0) then
          fn = float(nd)
          slat = slat / fn
          slon = slon / fn
          salt = salt / fn
          smag = smag / fn
          if (kr.eq.1) then
            write(2,'(f10.4,2hN ,f10.4,2hE ,f7.1,2hm ,f7.1,2hnT)')
     *           (rlat+slat)/2., (rlon+slon)/2.,
     *           (ralt+salt)/2., (rmag+smag)/2.
          endif
        endif
        kr = 0
        nd = 0
        call premsg(buf(1:lrtrim(buf)))
        write(2,'(a)') buf(1:lrtrim(buf))
        goto 1
      endif
      read(buf,*) fid, iymd, wtm, km, vlat, vlon, valt, tmag, vmag
      ihm = int(wtm) / 100
      sec = wtm - dble(ihm*100)
      ihm = ihm - ihm/100*40
      sec = sec + float(ihm*60)
      ihsec = int(sec/tint)
      vlat = vlat * 60.
      vlon = vlon * 60.
      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
      if (nd.eq.0) then
        jhsec = ihsec
        slat = vlat
        slon = vlon
        salt = valt
        smag = vmag
        nd = 1
      else
        if (ihsec.eq.jhsec) then
          slat = slat + vlat
          slon = slon + vlon
          salt = salt + valt
          smag = smag + vmag
          nd = nd + 1
        else
          fn = float(nd)
          slat = slat / fn
          slon = slon / fn
          salt = salt / fn
          smag = smag / fn
          if (kr.eq.1) then
            write(2,'(f10.4,2hN ,f10.4,2hE ,f7.1,2hm ,f7.1,2hnT)')
     *           (rlat+slat)/2., (rlon+slon)/2.,
     *           (ralt+salt)/2., (rmag+smag)/2.
          endif
          if (ihsec.eq.(jhsec+1)) then
            kr = 1
            rlat = slat
            rlon = slon
            ralt = salt
            rmag = smag
            jhsec = ihsec
            slat = vlat
            slon = vlon
            salt = valt
            smag = vmag
            nd = 1
          else
            call premsg('*** non-continuous data')
            kr = 0
            jhsec = ihsec
            slat = vlat
            slon = vlon
            salt = valt
            smag = vmag
            nd = 1
          endif
        endif
      endif
      goto 1
    2 if (nd.ne.0) then
        fn = float(nd)
        slat = slat / fn
        slon = slon / fn
        salt = salt / fn
        smag = smag / fn
        if (kr.eq.1) then
          write(2,'(f10.4,2hN ,f10.4,2hE ,f7.1,2hm ,f7.1,2hnT)')
     *         (rlat+slat)/2., (rlon+slon)/2.,
     *         (ralt+salt)/2., (rmag+smag)/2.
        endif
      endif
      close(2)
      close(1)
c
      if(flog(ldr:ldr).ne.' ') then
        open(99,file=flog,access='append')
        write(99,'(//a)') 'XSLINA : extract StdLIN data from DPAM data'
     *                    // ' with averaged re-sampling'
        write(99,'(a,a)')
     *       ' Input AM line data filename : ', fnam1(1:lrtrim(fnam1))
        write(99,'(8x,4a)')
     *       ' 5: ', cg, '-Lat.(Deg.)    7: Alt.(m) ', ca,
     *       ' 6: ', cg, '-Lon.(Deg.)    9: Mag.(nT)', ''
        write(99,'(a,f6.2,a)') '  Averaging interval :', tint, ' sec.'
        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
