c     Program that extracts the heading from the raw navigation file
c     on Hudson
c
c     CCGS Hudson  June 24, 2001
c
c     Thomas Funck
c
      character*60 input,output
      character*5 id,hid,tid
      integer t(90000),t1,t2
      real h(90000)
c
      hid='HEHDT'
      tid='GPGGA'
c
c--------------------------------------------------------------
c
c     Opening IO files
c
   15 write (*,*) 'Name of input file:'
      read (*,999) input
  999 format (a60) 
      open (10,file=input,status='old',err=10)
      goto 20
   10 write (*,*) 'File does not exist. Try again.'
      goto 15
c
   20 write (*,*) 'Name of output file:'
      read (*,999) output
      open (11,file=output,status='new',err=30)
      goto 40
   30 write (*,*) 'File exists already. Try again.'
      goto 20
c
   40 write (*,*) 'Enter Julian Day'
      read (*,997) jd
  997 format(i4)
c
c--------------------------------------------------------------
c
c     Scanning for header ID
c
      ic=1
c
 50   read (10,998,err=50,end=100) id
 998  format (1x,a5)
c
      if (id.eq.hid) then
          backspace 10
          read (10,996,err=80,end=100) head
 996      format (7x,f5.1)
          goto 90
        else
          goto 50
      endif
c
c     Scanning for next time stamp after header
c
 90   read (10,998,err=90,end=100) id
c
      if (id.eq.tid) then
          backspace 10
          read (10,995,err=80,end=100) it
 995      format (7x,i6)
c
          ih=int(it/10000)
          it=it-ih*10000
          im=int(it/100)
          it=it-im*100
          is=it
          t(ic)=ih*3600+im*60+is
          h(ic)=head
          ic=ic+1
          goto 50
        else
          goto 90
      endif
c
c--------------------------------------------------------------
c
c     Do the interpolation for full minutes
c
 100  ic=ic-1
c
c     Determine first minute to calculate
c
      t1=(int(float(t(1))/60.)+1)*60
c
c     Scan to appropriate time
c
      k=1
 110  continue
      if (t1.ge.t(k).and.t1.le.t(k+1)) then
          head=(h(k+1)-h(k))/(t(k+1)-t(k))*(t1-t(k))+h(k)
          t2=t1
          ih=int(t2/3600)
          t2=t2-ih*3600
          im=int(t2/60)
          t2=t2-im*60
          is=t2
          write (11,994) jd,ih,im,is,head
 994      format (i4,i3,':',i2,':',i2,f6.1)
          t1=t1+4
c
        else
          k=k+1
          if (k.ge.ic) goto 120
      endif
c
      goto 110
c               
c
c--------------------------------------------------------------
c
c     Ending program
c
 80   write (*,*) 'Error in reading input file.'
 120  close (10)
      close (11)
      stop
      end
