	program GetLSGHKs
c get LSG house keeping
c **27-Jun-17**rev. 22-Mar-18**rev. 4-Jun-18**
	character filen*29,day*3,cr*1,tf*1,df(10)*1,time*21
	integer*2 in(44,45)
	integer in4(22,45),ifc(10),idt(10),kv(10),kt(2),sf(4)
	logical first,badt
	parameter (cr='0d'x)
	equivalence (in(1,1),in4(1,1))
	data ifc/39,10,23,68,89,54,24,38,53,69/
&	filen/'a17_lsg_1975_l1_arcsav_hk.tab'/
&	,time(1:15)/'1975-***T**:**:'/
c
	dt=1000./106.
	do j=1,10
	 idt(j)=nint(((ifc(j)-10)*64+32)*dt)
	 end do
	open(7,file='../lsgl1aPDSs/'//filen)
	write(7,1) 
&	'year-dayThr:mn:sc.ms  f stat',(i,i=1,9),cr,'time+ms  |    x',idt,cr
1	format(a28,9('  AG-',i1,' f'),' AG-10 f',a1/13xa15,10(i6,' |'),a1)
	idts=0
	do i=1,10
	 kv(i)=-9
	 df(i)='-'
	 end do
	first=.true.
	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='../lsgl1bs/lsgl1b.'//day,access='direct'
&	 ,form='unformatted',recl=3960,status='old',err=29)
	 write(*,*) day
	 nr=1
10	 read(1,rec=nr,end=19) in
	 do k=1,45
	  if (in(1,k).eq.0) go to 17
c get frame count
	  kfc=and(in(6,k),'7f'x)
	  do j=1,10
	   if (kfc.eq.ifc(j)) go to 12
	   end do
	  go to 17
12	  ksyn=and(rshift(in4(3,k),8),'3fffff'x)
	  if (ksyn.ne.'3890ed'x.or.and(in(7,k),'8000'x).ne.0) then
	   if(kfc.ne.nfc) then
	    nfc=mod(nfc+1,90)
	    go to 18
	    end if
	   end if
c a special case - skip record 898 of day 151
	  if (id.eq.151.and.nr.eq.898) go to 18
c get time/16 and write out if passed current range
	  if (and(in4(1,k),8).eq.0) then
	   ktt=or(and(lshift(in4(1,k),28),'70000000'x)
&	   ,and(rshift(in4(2,k),4),'fffffff'x))
	   badt=iabs(ktt-lit).gt.5400000
	   if (first.or.ktt.lt.lit.or.(badt.and.nr.ne.1)) go to 15
	  else
	   if (idt(j).gt.idts) go to 15
	   go to 18
	   end if
	  write(7,'(a21,1xa1,1x4i1,10(i6,1xa1),a1)')
&	   time,tf,sf,(kv(i),df(i),i=1,10),cr
	  first=.true.
	  do i=1,10
	   kv(i)=-9
	   df(i)='-'
	   end do
15	  if (first) then
c get full time
	   idts=idt(j)
	   tf='-'
	   if (and(in4(1,k),'4008'x).ne.0) tf='*'
           kt(1)=or(and(lshift(in4(1,k),16),'70000'x)
&          ,and(in(3,k),'ffff'x))
           kt(2)=and(in4(2,k),'ffff'x)-idt(j)+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(1,k)
	   do i=4,1,-1
	    sf(i)=and(isf,7)
	    isf=rshift(isf,4)
	    end do
	   first=.false.
	   end if
	  if (ksyn.ne.'3890ed'x.or.and(in(7,k),'8000'x).ne.0) df(j)='*'
	  kv(j)=in(7,k)
	  if (and(kv(j),'8000'x).ne.0) then
	   kv(j)=and(kv(j),'3ff'x)
	   df(j)='*'
	   end if
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
