c**************************************************
c   ECOMP : Compensate CompBox Flight Data        *
c             with removing linear Trend surface  *
c**************************************************
c           must be linked with sml5.o
c--------------------------------------------------
      dimension ctrd(3), coef(9)
      character*80 wdr, flog, fnam, fout
      character buf*150, sdtm*20
      real*8 wtm
      call premsg('ECOMP : Compensate CompBox Flight Data')
      call premsg('         with removing linear Trend surface')
      call opnpin()
      ldr = lwkdir(50,wdr) + 1
      flog = wdr
      fnam = wdr
      fout = wdr
      call gparma('Enter LOG filename ==> ', 81-ldr, flog(ldr:80))
      call gparma('Enter Input filename ==> ', 81-ldr, fnam(ldr:80))
      open(1,file=fnam,status='old')
      call gparma('Enter Output filename ==> ', 81-ldr, fout(ldr:80))
      call clspin()
      open(2,file=fout,status='new')
      si = 0.
      sk = 0.
      sn = 0.
    1 read(1,'(a)',end=2) buf
      if (buf(1:1).eq.'&'.or.buf(1:1).eq.'%') goto 1
      read(buf,*) fid,iymd,wtm,km,fido,fkdo,falt,tmag,tres,
     *            fx,fy,fz,utc
      if (km.eq.(km/2*2)) call abendm('Input data already compensated')
      si = si + fido
      sk = sk + fkdo
      sn = sn + 1.
      goto 1
    2 rewind(1)
      si = si / sn
      sk = sk / sn
      write(6,'(a)') '----------------------------------------'
      write(6,'(a)') 'ECOMP : Compensate CompBox Flight Data'
      write(6,'(a)') '         with removing linear Trend surface'
      write(6,'(a,a)') ' Input filename  : ', fnam(1:lrtrim(fnam))
      write(6,'(a,a)') ' Output filename : ', fout(1:lrtrim(fout))
      write(6,'(a)')   ' ---'
      write(6,*) '  Average Lat./Long. : ', si, sk
      call sm5opn()
    3 read(1,'(a)',end=4) buf
      if (buf(1:1).eq.'&'.or.buf(1:1).eq.'%') goto 3
      read(buf,*) fid,iymd,wtm,km,fido,fkdo,falt,tmag,tres,
     *            fx,fy,fz,utc
      call sm5ex((fido-si)*60., (fkdo-sk)*60., fx, fy, fz, tres)
      goto 3
    4 rewind(1)
      call sm5cls(ctrd, 3, coef, 9)
      write(6,*) '  Coef. of Trend Surface : ', ctrd
      write(6,*) '  Compensation coefficients : '
      write(6,*) '   (x)   ', coef(1), coef(2), coef(3)
      write(6,*) '   (xx)  ', coef(4), coef(7), coef(9)
      write(6,*) '   (xy)  ', coef(5), coef(8), coef(6)
    5 read(1,'(a)',end=6) buf
      if (buf(1:1).eq.'&'.or.buf(1:1).eq.'%') then
        write(2,'(a)') buf(1:lrtrim(buf))
      else
        read(buf,*) fid,iymd,wtm,km,fido,fkdo,falt,tmag,tres,
     *              fx,fy,fz,utc
        call sm5rv((fido-si)*60., (fkdo-sk)*60., 0., 0., 0., trend)
        call sm5rv(0., 0., fx, fy, fz, corr)
        corr = corr - ctrd(1)
        tmag = tmag - corr
        tcor = tres - corr
        rand = tcor - trend
        write(2,1000) fid,iymd,wtm,(km-1),fido,fkdo,falt,tmag,tcor,
     *                fx,fy,fz,utc,tres,corr,rand,trend
      endif
      goto 5
    6 close(1)
      close(2)
c
      if(flog(ldr:ldr).ne.' ') then
        open(99,file=flog,access='append')
        write(99,'(//a)') 'ECOMP : Compensate CompBox Flight Data'
        write(99,'(a)') '         with removing linear Trend surface'
        write(99,'(a,a)') ' Input filename  : ', fnam(1:lrtrim(fnam))
        write(99,'(a,a)') ' Output filename : ', fout(1:lrtrim(fout))
        write(99,'(a)')   ' ---'
        write(99,*) '  Average Lat./Long. : ', si, sk
        write(99,*) '  Coef. of Trend Surface : ', ctrd
        write(99,*) '  Compensation coefficients : '
        write(99,*) '   (x)   ', coef(1), coef(2), coef(3)
        write(99,*) '   (xx)  ', coef(4), coef(7), coef(9)
        write(99,*) '   (xy)  ', coef(5), coef(8), coef(6)
        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, 4(1x,f8.2))
      end
