	program GetLHFEHKs
c get HFE house keeping
c **27-Jun-17**rev. 12-Jul-18**
	character filen*29,sta*2,day*3,tf*1,df(6)*1,cr*1,time*21
	integer*2 in(3200)
	integer ifc(6),idt(6),kv(6),kt(2),sf(4)
	logical first,badt,syncerr
	parameter (cr='0d'x)
	data ifc/29,45,55,74,57,75/,time(1:15)/'1975-***T**:**:'/
&	,filen/'a**_hfe_1975_l1_arcsav_hk.tab'/
	equivalence (filen(2:3),sta)
c
	write(*,"('station: '$)")
	read(*,'(a2)') sta
	if (sta.eq.'15') then
	 dt=9.4336
	else
	 dt=9.4339
	 end if
	do j=1,6
	 idt(j)=nint(((ifc(j)-29)*64+32)*dt)
	 end do
	open(7,file='../hfel1aPDSs/'//filen)
	write(7,1) 'year-dayThr:mn:sc.ms  f stat'
&	,1,2,3,4,6,7,cr,'time+ms  |    x',idt,cr
1	format(a28,6('  AH-',i1,' f'),a1/13xa15,6(i6,' |'),a1)
	idts=0
	do i=1,6
	 kv(i)=-9
	 df(i)='-'
	 end do
	first=.true.
c
	write(*,"('days: '$)")
	read(*,*) id1,id2
	do id=id1,id2
	 write(day,'(i3)') id
	 if (day(1:1).eq.' ') day(1:1)='0'
	 open(1,file='../../cleanup/asr'//sta//'/asr.'//sta//'.'//day
&	 ,access='direct',form='unformatted',recl=6400,status='old',err=29)
	 write(*,*) day
	 nr=1
10	 read(1,rec=nr,end=19) in
	 do k=1,45
	  if (in(k).eq.0) go to 17
c get frame count
	  k0=k*48+140
	  k3=in(k0+3)
	  kfc=or(and(rshift(k3,5),'78'x),and(rshift(k3,3),7))
	  do j=1,6
	   if (kfc.eq.ifc(j)) go to 12
	   end do
	  go to 17
12	  syncerr=in(k0+1).ne.'3864'x.or.in(k0+2).ne.'432c'x
&	  .or.and(k3,'8080'x.ne.0)
	  if (syncerr) then
	   if (kfc.ne.nfc) then
	    nfc=mod(nfc+1,90)
	    go to 18
	    end if
	   end if	    
c get time/16 and write out if passed current range
	  i0=k*4+4
	  i2=in(i0+2)
	  i3=in(i0+3)
	  i4=in(i0+4)
	  if (and(i2,'8080'x).eq.0.and.and(or(i3,i4),'8080'x).eq.0) then
	   ktt=or(or(or(or(or(and(lshift(i2,18),'7c000000'x),
&	   and(lshift(i2,20),'3f00000'x)),and(lshift(i3,6),'fc000'x)),
&	   and(lshift(i3,8),'3f00'x)),and(rshift(i4,6),'fc'x)),
&	   and(rshift(i4,4),3))
	   badt=iabs(ktt-lit).gt.5400000
	   tf='-'
	   if (and(i2,'2000'x).ne.0) tf='*'
	   if (first.or.ktt.lt.lit.or.(badt.and.nr.ne.1)) go to 15
	  else
	   tf='*'
	   if (idt(j).gt.idts) go to 15
	   go to 18
	   end if
	  write(7,'(a21,1xa1,1x4i1,6(i6,1xa1),a1)')
&	   time,tf,sf,(kv(i),df(i),i=1,6),cr
	  first=.true.
	  do i=1,6
	   kv(i)=-9
	   df(i)='-'
	   end do
15	  if (first) then
c get full time
	   idts=idt(j)
           kt(1)=and(rshift(ktt,12),'7ffff'x)
           kt(2)=or(and(lshift(ktt,4),'fff0'x),and(i4,15))-idts+302
           if (kt(2).lt.0) then
            kt(2)=and(kt(2),'ffff'x)
            kt(1)=kt(1)-1
            end if                 
           it=or(and(lshift(kt(1),12),'7ffff000'x)
&          ,and(rshift(kt(2),4),'fff'x))
	   lit=it+3396
	   kd=it/5400000
	   it=mod(it,5400000)
	   kh=it/225000
	   it=mod(it,225000)
	   km=it/3750
	   sec=(mod(it,3750)*16+and(kt(2),15))/1000.
	   write(time(6:8),'(i3)') kd
	   if (time(6:6).eq.' ') time(6:6)='0'
	   if (time(7:7).eq.' ') time(7:7)='0'
	   write(time(10:11),'(i2)') kh
	   write(time(13:14),'(i2)') km
	   write(time(16:21),'(f6.3)') sec
	   do i=10,16
	    if (time(i:i).eq.' ') time(i:i)='0'
	    end do
	   isf=in(i0+1)
	   do i=4,1,-1
	    sf(i)=and(isf,7)
	    isf=rshift(isf,4)
	    end do
	   first=.false.
	   end if
	  df(j)='-'
	  if (syncerr) df(j)='*'
	  k25=in(k0+25)
	  kv(j)=or(and(rshift(k25,4),'fc'x),and(rshift(k25,2),15))
	  if (and(k25,'8080'x).ne.0) df(j)='*'
17	  nfc=mod(kfc+1,90)
18	  end do
	 nr=nr+1
	 go to 10
19	 close(1)
29	 end do
	close(7)
	stop
	end
