	program L1b2L1aLSMPDS
c level 1b to level 1a in ASCII for LSM in PDS format - 1 line/data cycle
c **2-Oct-17**rev. 06-Jun-18*
	character filen*29,tf*1,df(130)*1,dfj*1,time*21,cr*1
	integer*2 in(15,45)
	integer ia(130),itd(130),dv(130),ia1(2,8),ia2(16),ia3(2,16,3),ib(18)
	logical newline,syncer
	equivalence (ia1(1,1),ia(1)),(ia2(1),ia(17)),(ia3(1,1,1),ia(33))
	parameter (cr='0d'x)
	data filen/'a16_lsm_1975***_l1_arcsav.tab'/
&	,ib/1,2,3,4,4,5,5,7,9,10,12,13,14,15,15,16,6,8/
&	,time(1:15)/'1975-***T**:**:'/
c
c get frame delays
	ia1(1,1)=5
	do j=1,8
	 if (j.gt.1) ia1(1,j)=ia1(2,j-1)+64
	 ia1(2,j)=ia1(1,j)+64 
	 end do
	do i=1,16
	 ia2(i)=(ib(i)-1)*64+5
	 end do
	ia3(1,1,1)=17
	do j=1,16
	 if (j.gt.1) ia3(1,j,1)=ia3(1,j-1,1)+64
	 do k=1,3
	  if (k.gt.1) ia3(1,j,k)=ia3(1,j,k-1)+2
	  ia3(2,j,k)=ia3(1,j,k)+32
	  end do
	 end do
	ia(129)=(ib(17)-1)*64+5
	ia(130)=(ib(18)-1)*64+5
	open(2,file='../clockrates/FrameLength.16.txt',status='old') 
	read(2,*) kd,frl
c
	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='../lsml1b16s/lsml1b.'//filen(13:15),access='direct'
&	 ,form='unformatted',recl=1350,status='old',err=29)
	 write(*,*) filen(13:15)
	 open(7,file='../lsml1aPDSs/'//filen)
	 write(7,"(a23,17(' DM-',i1,' f'),113(' DM-',i2,' f'),a1)") 
&	 'year day hr:mn:sc.ms  f',(i,i,i=1,8),9,(i,i=10,24)
&	 ,(i,i,i,i,i,i,i,i,i,i,i,i,i,i,i,i,i,i,i,i,i,i,i,i,i,i,i,i,i,i,i,i
&	 ,i=25,27),(i,i=28,29),cr
	 open(8,file='../lsml1aPDSs/lsml1alog.'//filen(13:15))
	 do while (kd.ne.id)
	  read(2,*) kd,frl
	  end do
	 dt=frl/64.
	 do i=1,130
	  itd(i)=nint(dt*(ia(i)-1))
	  end do
	 write(7,"(14xa9,17(i5,' |'),113(i6,' |'),a1)")
&	 'time+ms |',(itd(i),i=1,130),cr
c
	 nr=1
	 nw=0
	 newline=.true.
	 nsf=0
	 do i=1,130
	  dv(i)=-999
	  df(i)='-'
	  end do
10	 read(1,rec=nr,end=19) in
	 do k=1,45
	  if (in(1,k).eq.0) then
	   write(8,"(i4,i3,1xa8,z5,' skip')") nr,k,time(10:17),in(1,k)
	   go to 17
	   end if
c search for LSM word 1 if nw=0
	  if (nw.eq.0) then
	   do k1=k,45
	    in9=in(9,k1)
	    if (and(in9,'200'x).ne.0) then
	     nw=mod(k+16-k1,16)+1
	     in6=in(6,k)
c	     nf=mod(and(in6,'7f'x)+nw+73,90)
	     go to 11
	     end if
	    end do
	   write(*,*) nr,k,k1
	   stop
	   end if
c get time if new line
11	  if (newline) then
	   in2=in(2,k)
	   in3=in(3,k)
	   in4=in(4,k)
	   it=or(or(and(lshift(in2,28),'70000000'x)
&	   ,and(lshift(in3,12),'ffff000'x)),and(rshift(in4,4),'fff'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,15))/1000.
	   tf='-'
	   if (and(it2,'4008'x).ne.0.or.iabs(kd-id).gt.1) tf='*'
c adjust time if not LMS word 1
	   if (nw.ne.1) then
	    sec=sec-(nw-1)*frl/1000.
	    if (sec.lt.0) then
	     sec=sec+60.
	     km=km-1
	     if (km.lt.0) then
	      km=km+60
	      kh=kh-1
	      if (kh.lt.0) then
	       kh=kh+24
	       kd=kd-1
	       end if
	      end if
	     end if
	    end if
c decode time
	   write(time(6:8),'(i3)') kd
	   if (time(6:6).eq.' ') time(6:6)='0'
	   write(time(10:11),'(i2)') kh
	   write(time(13:14),'(i2)') km
	   write(time(16:21),'(f6.3)') sec
	   do i=7,16,3
	    if (time(i:i).eq.' ') time(i:i)='0'
	    end do
	   end if
c get sync
	  in5=in(5,k)
	  in6=in(6,k)
	  ksyn=or(and(lshift(in5,8),'3fff00'x),and(rshift(in6,8),'ff'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,3) nr,k,time(10:21),ksyn,'out of sync - skip'
3	     format(i4,i3,1xa12,z7,1xa)
	     syncer=.true.
	     go to 12
	     end if
	    kxs=rshift(kxs,1)
	    end do
	   write(8,3) nr,k,time(10:21),ksyn,'sync bit error'
	   end if
	  syncer=.false.
c LSM data 
12	  do j=1,7
	   kv=in(j+8,k)
	   dfj='-'
	   if (and(kv,'8000'x).ne.0) then
	    dfj='*'
	    kv=and(kv,'3ff'x)
	    end if
	   if (j.eq.1) then
	    kv1=and(rshift(kv,2),'7f'x)
	    go to (101,102,103,104,105,106,107,108,109,110,111,112
&	    ,113,114,115,116) nw
101	    dv(1)=kv1
	    df(1)=dfj
	    dv(17)=and(kv,3)
	    df(17)=dfj
	    if (syncer) then
	     df(1)='*'
	     df(17)='*'
	     end if
	    go to 13
102	    dv(3)=kv1
	    df(3)=dfj
	    dv(18)=and(kv,3)
	    df(18)=dfj
	    if (syncer) then
	     df(3)='*'
	     df(18)='*'
	     end if
	    go to 13
103	    dv(5)=kv1
	    df(5)=dfj
	    dv(19)=and(kv,3)
	    df(19)=dfj
	    if (syncer) then
	     df(5)='*'
	     df(19)='*'
	     end if
	    go to 13
104	    dv(7)=kv1
	    df(7)=dfj
	    dv(20)=and(rshift(kv,1),1)
	    df(20)=dfj
	    dv(21)=and(kv,1)
	    df(21)=dfj
	    if (syncer) then
	     df(7)='*'
	     df(20)='*'
	     df(21)='*'
	     end if
	    go to 13
105	    dv(9)=kv1
	    df(9)=dfj
	    dv(22)=and(rshift(kv,1),1)
	    df(22)=dfj
	    dv(23)=and(kv,1)
	    df(23)=dfj
	    if (syncer) then
	     df(9)='*'
	     df(22)='*'
	     df(23)='*'
	     end if
	    go to 13
106	    dv(11)=kv1
	    df(11)=dfj
	    dv(129)=and(kv,1)
	    df(129)=dfj
	    if (syncer) then
	     df(11)='*'
	     df(129)='*'
	     end if
	    go to 13
107	    dv(13)=kv1
	    df(13)=dfj
	    dv(24)=and(kv,3)
	    df(24)=dfj
	    if (syncer) then
	     df(13)='*'
	     df(24)='*'
	     end if
	    go to 13
108	    dv(15)=kv1
	    df(15)=dfj
	    dv(130)=and(kv,3)
	    df(130)=dfj
	    if (syncer) then
	     df(15)='*'
	     df(130)='*'
	     end if
	    go to 13
109	    dv(2)=kv1
	    df(2)=dfj
	    kvs=and(lshift(kv,1),6)
	    df(25)=dfj
	    if (syncer) then
	     df(2)='*'
	     df(25)='*'
	     end if
	    go to 13
110	    dv(4)=kv1
	    df(4)=dfj
	    dv(25)=or(kvs,and(rshift(kv,1),1))
	    kvs=and(lshift(kv,2),4)
	    df(25)=dfj
	    df(26)=dfj
	    if (syncer) then
	     df(25)='*'
	     df(26)='*'
	     end if
	    go to 13
111	    dv(6)=kv1
	    df(6)=dfj
	    dv(26)=or(kvs,and(kv,3))
	    if (dfj.eq.'*'.or.syncer) then
	     df(6)='*'
	     df(26)='*'
	     end if
	    go to 13
112	    dv(8)=kv1
	    df(8)=dfj
	    if (syncer) df(8)='*'
	    kvs=and(lshift(kv,1),6)
	    df(27)=dfj
	    if (syncer) then
	     df(8)='*'
	     df(27)='*'
	     end if
	    go to 13
113	    dv(10)=kv1
	    df(10)=dfj
	    dv(27)=or(kvs,and(rshift(kv,1),1))
	    if (dfj.eq.'*') df(27)=dfj
	    dv(28)=and(kv,1)
	    df(28)=dfj
	    if (syncer) then
	     df(10)='*'
	     df(27)='*'
	     df(28)='*'
	     end if
	    go to 13
114	    dv(12)=kv1
	    df(12)=dfj
	    dv(29)=and(kv,3)
	    df(29)=dfj
	    if (syncer) then
	     df(12)='*'
	     df(29)='*'
	     end if
	    go to 13
115	    dv(14)=kv1
	    df(14)=dfj
	    dv(30)=and(rshift(kv,1),1)
	    df(30)=dfj
	    dv(31)=and(kv,1)
	    df(31)=dfj
	    if (syncer) then
	     df(14)='*'
	     df(30)='*'
	     df(31)='*'
	     end if
	    go to 13
116	    dv(16)=kv1
	    df(16)=dfj
	    dv(32)=and(kv,3)
	    df(32)=dfj
	    if (syncer) then
	     df(16)='*'
	     df(32)='*'
	     end if
	    go to 13
	   else
	    kv1=and(kv,'1ff'x)
	    if (and(kv,'200'x).ne.0) kv1=-kv1
	    mj=(mod(j+1,3)+1)*32+j/5+nw*2-1
	    dv(mj)=kv1
	    df(mj)=dfj
	    if (syncer) df(mj)='*'
	    end if
13	   end do
	  if (nw.eq.16) then
	   write(7,1) time,tf,(dv(i),df(i),i=1,130),cr
1	   format(a21,1xa1,17(i5,1xa1),113(i6,1xa1),a1)
	   if (nsf.ne.16) write(8,2) nr,k,time(10:21),nsf
2	   format(i4,i3,1xa12,i4)
	   do i=1,130
	    dv(i)=-999
	    df(i)='-'
	    end do
	   newline=.true.
	   nsf=0
	   end if
	  nw=mod(nw,16)+1
	  nsf=nsf+1
17	  end do
	 nr=nr+1
	 go to 10
19	 close(1)
	 if (nsf.ne.0) then
	  write(7,1) time,tf,(dv(i),df(i),i=1,130),cr
	  write(8,2) nr,k,time(10:21),nsf
	  end if
	 close(7)
	 close(8)
29	 end do
	stop
	end
