function mav_sta_apid_decom,ccsds,lastpkt=lastpkt,apid=apid,pcyc=pcyc,len=len
data = mav_pfdpu_part_decompress_data(ccsds)
if not keyword_set(lastpkt) then nolstpkt = 0 else nolstpkt=1
if not keyword_set(lastpkt) then lastpkt = ccsds
last = mav_pfdpu_part_decompress_data(lastpkt)
subsec1 = data[0]/256.d
subsec2 = data[1]/(256.d)^2
lstsub1 = last[0]/256.d
lstsub2 = last[1]/(256.d)^2
time = ccsds.time + subsec1 + subsec2
lasttime = lastpkt.time + lstsub1 + lstsub2
nn = 2^(7 and data[3])
ss = (8 and data[3])/2^3
if ss eq 0 then n2=1 else n2=nn
lstnn = 2^(7 and last[3])
pp = (15 and data[5])
lpcyc=pcyc
offset = 4.d
if apid eq 'DA' then begin
offset=0.d
pp = 0
gg=[64,128,256,1024]
nd = gg[(48 and data[3])/2^4]
nm = 1024/nd
n2 = 0
ld = gg[(48 and last[3])/2^4]
lm = 1024/ld
lstnn = lstnn*lm
lpcyc=n_elements(last) - 6
endif
if apid eq 'D9' or apid eq 'D8' then offset=0.d
npts=n_elements(data) - 6
lpts=n_elements(last) - 6
ncyc = npts/pcyc
lcyc = lpts/lpcyc
if abs(time-lasttime-4.*lcyc*lstnn) gt 0.01 and nolstpkt and (pp eq 0) then print,'Error: ',apid,' pkt time jump ',time-lasttime,' ',time_string(time),' ',time_string(lasttime)
if npts ne len then print,'Error in APID ',apid,' - length: ',npts,' Should be ',len,' ', time_string(ccsds.time)
if (npts mod pcyc) ne 0 then begin
print,'Error in APID ',apid,' - length: ',npts,' pts_cyc= ',pcyc,' ', time_string(ccsds.time)
if apid eq 'DA' then begin
pad = bytarr(len - npts) & pad(*)=255
data = [data,pad]
ncyc=1
endif else begin
npts=npts - (npts mod pcyc)
ncyc = npts/pcyc
if npts eq 0 then return, {time:ccsds.time,valid: 0}
endelse
endif
str = {time: time - offset + 2.d*n2 + 4.d*nn*findgen(ncyc),$
seq_cntr: ccsds.seq_cntr#replicate(1,ncyc) ,$
valid: 1#replicate(1,ncyc) ,$
mode: byte((data[2] and 127)#replicate(1,ncyc)) ,$
avg: byte(data[3]#replicate(1,ncyc)) ,$
atten: byte(data[4]#replicate(1,ncyc)) ,$
diag: byte(data[5]#replicate(1,ncyc)) ,$
data : reform(data[6:pcyc*ncyc+5],pcyc,ncyc) }
lastpkt = ccsds
return, str
end