c--------( DVCORR : Correction of Diurnal var. for AM data )--------
      character*80 wdr, flog, fgst, fnam, fout
      character buf*150, str*40, sdtm*20
      real*8 wtm, tmag
      call premsg('DVCORR : Diurnal var. correction for AM data')
      call opnpin()
      ldr = lwkdir(50,wdr) + 1
      flog = wdr
      fgst = wdr
      fnam = wdr
      fout = wdr
      call gparma('Enter LOG filename ==> ', 81-ldr, flog(ldr:80))
      call gparma('Enter AM data filename ==> ', 81-ldr, fnam(ldr:80))
      open(1,file=fnam,status='old')
      call gparma('Enter GS data filename ==> ', 81-ldr, fgst(ldr:80))
      open(10,file=fgst,status='old')
      call gparma('Enter Output filename  ==> ', 81-ldr, fout(ldr:80))
      open(2,file=fout,status='new')
      call clspin()
      write(6,'(a)') '----------------------------------------'
      write(6,'(a)')
     *     'DVCORR : Correction of Diurnal variation for AM data'
      write(6,'(a,a)') ' Input filename   : ', fnam(1:lrtrim(fnam))
      write(6,'(a,a)') ' GS data filename : ', fgst(1:lrtrim(fgst))
      write(6,'(a,a)') ' Output filename  : ', fout(1:lrtrim(fout))
c
      base = 0.
      jymd = 0
    1 read(1,'(a)',end=10) buf
      if (buf(1:1).eq.'&'.or.buf(1:1).eq.'%') then
        write(2,'(a)') buf(1:lrtrim(buf))
        goto 1
      endif
      read(buf,*) fid,iymd,wtm,km,fido,fkdo,falt,tmag,tres,
     *            fx,fy,fz,utc
      if (mod(km,4).lt.2)
     *     call abendm('already Corrected for Diurnal variation')
      ihm = int(wtm/100.)
      tm = wtm - dble(ihm*100)
      nm = ihm - ihm/100*40
      tm = tm + float(nm*60)
    2 if (iymd.ne.jymd) goto 3
      if (tm.lt.ts1) goto 9
      if (tm.gt.ts2) goto 7
      dv = (dm1*(ts2-tm) + dm2*(tm-ts1)) / (ts2-ts1)
      tmag = tmag - dv
      tres = tres - dv
      write(2,1000) fid,iymd,wtm,(km-2),fido,fkdo,falt,tmag,tres,
     *              fx,fy,fz,utc
      goto 1
c
    3 read(10,'(a)',end=8) str
    4 if (str(1:5).ne.'/base' .and. str(1:5).ne.'/Base') goto 5
      read(str(7:40),*) base
      if (base.ge.200000.) base = base / 10.
      if (jymd.eq.iymd) goto 6
      goto 3
    5 if (str(1:5).ne.'/date' .and. str(1:5).ne.'/Date') goto 3
      read(str(7:40),*) jymd
      if (jymd.ne.iymd .or. base.eq.0.) goto 3
    6 read(10,'(a)',end=8) str
      if (str(1:1).eq.'/') goto 4
      read(str,'(3i2,1x,i6)') jh,jm,js, mg
      ts2 = float(jh*3600 + jm*60 + js)
      dm2 = float(mg)/10. - base
    7 ts1 = ts2
      dm1 = dm2
      read(10,'(a)',end=8) str
      if (str(1:1).eq.'/') goto 4
      read(str,'(3i2,1x,i6)') jh,jm,js, mg
      ts2 = float(jh*3600 + jm*60 + js)
      dm2 = float(mg)/10. - base
      goto 2
c
    8 call abendm('EOF on GS data file')
    9 write(str,'(i9,1x,f10.2)') iymd, tm
      call abendm('out of GS data range : ' // str(1:20))
c
   10 close(2)
      close(1)
      close(10)
      if(flog(ldr:ldr).ne.' ') then
        open(99,file=flog,access='append')
        write(99,'(//a)')
     *        'DVCORR : Correction of Diurnal variation for AM data'
        write(99,'(a,a)') ' Input filename   : ', fnam(1:lrtrim(fnam))
        write(99,'(a,a)') ' GS data filename : ', fgst(1:lrtrim(fgst))
        write(99,'(a,a)') ' Output filename  : ', fout(1:lrtrim(fout))
        call strdtm(sdtm)
        write(99,'(a,a,a)') '=============== ', sdtm, '==============='
        close(99)
      endif
      stop
c
 1000 format(f7.1, 1x,i8, 1x,f9.2, 1x,i2, 1x,f9.5, 1x,f10.5, 1x,f6.1,
     *       2(1x,f8.2), 3(1x,f7.3), 1x,f9.2)
      end
