function mvn_sta_apid_decom,ccsds,lastpkt=lastpkt,apid=apid,pcyc=pcyc,len=len
data = mvn_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 = lastpkt.data
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
met = ccsds.met + 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
if time lt time_double('2013-07-01') then offset = 4.d else offset=0.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=lastpkt.size-16
endif
if apid eq 'D9' or apid eq 'D8' then offset=0.d
npts=n_elements(data) - 6
lpts=lastpkt.size-16
ncyc = npts/pcyc
lcyc = lpts/lpcyc
if (npts mod pcyc) ne 0 then begin
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
if 0 then begin
str = {time: time - offset + 2.d*n2 + 4.d*nn*findgen(ncyc),$
met: met - 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) }
endif
str2 = {time: 0.d ,$
met: 0.d, $
seq_cntr: ccsds.seq_cntr ,$
valid: 1 ,$
mode: byte(data[2] and 127) ,$
avg: byte(data[3]) ,$
atten: byte(data[4]) ,$
diag: byte(data[5]) ,$
data : bytarr(pcyc) }
str = replicate(str2,ncyc)
str.time = time - offset + 2.d*n2 + 4.d*nn*findgen(ncyc)
str.met = met - offset + 2.d*n2 + 4.d*nn*findgen(ncyc)
str.data = reform(data[6:pcyc*ncyc+5],pcyc,ncyc)
lastpkt = ccsds
return, str
end