	program L1b2L1aLSGPDSb2
c level 1b to level 1a in AACII for LSG in PDS format
c **05-Oct-17** binary output for DG-11 & 19**w/ staus flags 3/12/18**
	character filen*30,tf*1,df(43)*1,time*21,cr*1
	integer*2 in(44,45)
	integer in4(22,45),ip(43),ia(43),itd(43),dv(43),dv11(6),dv19(6)
&	,sf(7)
	logical syncer
	parameter (dt=9.4339,cr='0d'x)
	equivalence (in(1,1),in4(1,1))
	data ip/9,10,11,12,13,14,15,16,17,18,19,21,23,25,26,27
&	,29,31,32,33,34,35,36,37,38,39,40,41,42,43,44,20,22,24
&	,28,0,0,0,30,0,0,0,0/,time(1:15)/'1975-***T**:**:'/
&	,filen/'a17_lsg_1975***_l1_arcsavb.tab'/
&	,ia/4,6,8,10,12,14,16,18,20,22,24,26,28,30,32,34,36,38,40,42
&	,44,46,48,50,52,54,56,58,60,62,64,25,27,29,35,35,35,35,37,37
&	,37,37,37/
c
	do i=1,43
	 itd(i)=nint(dt*(ia(i)-1))
	 end do
	write(*,"('days: '$)")
	read(*,*) id1,id2
	do id=id1,id2
	 write(filen(13:15),'(i3)') id
	 if (filen(13:13).eq.' ') filen(13:13)='0'
	 open(1,file='../lsgl1bs/lsgl1b.'//filen(13:15),access='direct'
&	 ,form='unformatted',recl=3960,status='old',err=29)
	 write(*,*) filen(13:15)
	 open(7,file='../lsgl1aPDSs/'//filen)
	 write(7,"(a32,34(' DG-',i1,' f'),'  DG-11 f',7(' DG-',i2,' f') 
&	 ,'  DG-19 f',a1)")'year-dayThr:mn:sc.ms  f stat tsd'
&	 ,(1,i=1,31),(i,i=2,4),(i,i=12,18),cr
	 write(7,"(14xa18,34(i5,' |'),i7,' |',7(i6,' |'),i7,' |',a1)")
&	 'time+ms |    x   x',(itd(i),i=1,34),(itd(i),i=35,43),cr
	 open(8,file='../lsgl1aPDSs/lsgl1alogb.'//filen(13:15))
	 nr=1
10	 read(1,rec=nr,end=19) in
	 do k=1,45
	  if (in4(1,k).eq.0) go to 17
c time, status flags & sync
	  tf='-'
	  it=or(and(lshift(in4(1,k),28),'70000000'x),
&	  and(rshift(in4(2,k),4),'fffffff'x))
	  kd=it/5400000
	  it=mod(it,5400000)
	  kh=it/225000
	  it=mod(it,225000)
	  km=it/3750
	  sec=(mod(it,3750)*16+and(in4(2,k),15))/1000.
	  write(time(6:8),'(i3)')kd
	  write(time(10:11),'(i2)') kh
	  write(time(13:14),'(i2)') km
	  write(time(16:21),'(f6.3)') sec
	  if (and(in4(1,k),8).ne.0.or.iabs(kd-id).gt.1) tf='*'	   
	  do i=6,16
	   if (time(i:i).eq.' ') time(i:i)='0'
	   end do
	  isf=rshift(in4(1,k),12)
	  do i=7,5,-1
	   sf(i)=and(isf,1)
	   isf=rshift(isf,1)
	   end do
	  if (sf(5).eq.1) tf='*'
	  isf=rshift(isf,1)
	  do i=4,1,-1
	   sf(i)=and(isf,7)
	   isf=rshift(isf,4)
	   end do
	  ksyn=and(rshift(in4(3,k),8),'3fffff'x)
	  if(ksyn.ne.'3890ed'x) then
	   kxs=xor(ksyn,'3890ed'x)
	   nib=0
	   do i=1,22
	    if (and(kxs,1).ne.0) nib=nib+1
	    if (nib.gt.2) then
	     write(8,"(a12,' sync error',z7)") time(10:21),ksyn
	     syncer=.true.
	     go to 12
	     end if
	    kxs=rshift(kxs,1)
	    end do
	   write(8,"(a12,' sync bit error',z7)") time(10:21),ksyn
	   end if
	  syncer=.false.
c LSG data 
12	  do j=1,43
	   if (ip(j).ne.0) then
	    kv=in(ip(j),k)
	    df(j)='-'
	    if (and(kv,'8000'x).ne.0) then
	     df(j)='*'
	     kv=and(kv,'3ff'x)
	     end if
	    if (syncer) df(j)='*'
	   else
	    df(j)=df(j-1)
	    end if
	   if (j.le.34) then
	    dv(j)=511-kv
	   else if (j.eq.35) then
	    kv=rshift(kv,4)
	    do i=6,1,-1
	     dv11(i)=and(kv,1)
	     kv=rshift(kv,1)
	     end do
	   else if (j.eq.36) then
	    dv(j)=and(rshift(kv,3),1)
	   else if (j.eq.37) then
	    dv(j)=and(rshift(kv,1),3)
	   else if (j.eq.38) then
	    dv(j)=and(kv,1)
 	   else if (j.eq.39) then
	    dv(j)=and(rshift(kv,9),1)
	   else if (j.eq.40) then
	    dv(j)=and(rshift(kv,8),1)
	   else if (j.eq.41) then
	    dv(j)=and(rshift(kv,7),1)
	   else if (j.eq.42) then
	    dv(j)=and(rshift(kv,6),1)
	   else if (j.eq.43) then
	    do i=6,1,-1
	     dv19(i)=and(kv,1)
	     kv=rshift(kv,1)
	     end do
	   else
	    dv(j)=kv
	    end if
	   end do
	  write(7,'(a21,1xa1,1x4i1,1x3i1,34(i5,1xa1),1x6i1,1xa1,7(i6,1xa1)
&	  ,1x6i1,1x2a1)') time,tf,sf,(dv(j),df(j),j=1,34),dv11,df(35)
&	  ,(dv(j),df(j),j=36,42),dv19,df(43),cr
17	  end do
	 nr=nr+1
	 go to 10
19	 close(1)
	 close(7)
29	 end do
	stop
	end

