;+
;  $Id: unpack_tst_jill.pro,v 1.3 2007/08/16 17:23:59 jredfern Exp $
;
;  $Source: /usr/lib/cvsroot/twins-idl/lib/util/unpack_tst_jill.pro,v $
;  $Revision: 1.3 $
;  $Date: 2007/08/16 17:23:59 $
;

PRO unpack_tst_jill, datafile, bdata,ftype_t,tst, cal=cal

;
; PURPOSE:
;	unpack test data from binary data file and generate 
;       idl saveset 
;
;  Example:
;   unpack_tst, '2002031',1,dataarray,ftypearray
;  Notes:
;  See Also:
;
;  Author:  Dorothea DeLapp LANL 2007
;  Modification $Author: jredfern $
;-

  
;
; determine mas number of direct events
;
   tsmode=where(tst.telem eq 3,tscount)
   tdmode=where(tst.telem eq 4,tdcount)
   if tdcount gt 0 then begin
        maxde=9315/5
   endif else begin
        maxde=7832/5
    endelse

;
; determine number of messages
;
   if not cal then begin
   num_msgs=n_elements(uniq(tst.c_pt))
   endif else begin
   num=where(tst.group_flag eq 1,num_msgs)
   endelse 
;
;
; define test message
;
  tstmsg=replicate(create_struct(name='testing_'+strtrim(string(maxde),2),  $
; Wrapper
'c_pt',long64(0), $
'c_gr',long64(0), $
'fr_sz',0L, $
'f_gr',0L, $
;
; primary header
;
'versionid',0, $
'typeid',0, $
'header_flag',0, $
'istid',0,$
'pktid',0,$
'pkt_len',0,$
;
;secondaryheader
'c_pt_sh',long64(0),$
'f_pt_sh',0L,$
'telem',0,$
;
;instrumentstatus
;
'ISECTORPOS',0.0D,$
'ISECTORPOS333',0.0D,$
'ISECTORPOS666',0.0D,$
'ISECTORPOS999',0.0D,$
'ISECTORTIMELAPSE',0L,$
'ISECTORNUMBER',0,$
'ISCANSTATE',0,$
'ISECTORVELOCITY',0.0D,$
'ILADCNT1OFFSET0',0,$
'ILADCNT2OFFSET0',0,$
'ILADCNT1OFFSET2',0,$
'ILADCNT2OFFSET2',0,$
'IFEETOTALVALIDS',0,$
;
;'singlesdata
;
'ISTARTCNT1',0,$
'ISTOPCNT1',0,$
'IVALIDCNT1',0,$
'ISTARTCNT2',0,$
'ISTOPCNT2',0,$
'IVALIDCNT2',0,$
'ISTARTNOSTOPCNT1',0,$
'ITOFUNDRFLWCNT1',0,$
'ISTARTSTARTCNT1',0,$
'ISTARTHGHTOVRLD1',0,$
'ISTOPHGHTOVRLD1',0,$
'ISTARTPOSUNDRFLW1',0,$
'ISTARTPOSOVRFLW1',0,$
'ISTOPPOSUNDRFLW1',0,$
'ISTOPPOSOVRFLW1',0,$
'IEVENTBUFOVRFLW1',0,$
'ISTARTNOSTOPCNT2',0,$
'ITOFUNDRFLWCNT2',0,$
'ISTARTSTARTCNT2',0,$
'ISTARTHGHTOVRLD2',0,$
'ISTOPHGHTOVRLD2',0,$
'ISTARTPOSUNDRFLW2',0,$
'ISTARTPOSOVRFLW2',0,$
'ISTOPPOSUNDRFLW2',0,$
'ISTOPPOSOVRFLW2',0,$
'IEVENTBUFOVRFLW2',0,$
;
;'directevents
;
'TOF',intarr(maxde),$
'STARTHT',intarr(maxde),$
'STARTPOS',intarr(maxde),$
'STOPHT',intarr(maxde),$
'STOPPOS',intarr(maxde),$
'slice',intarr(maxde),$
'DEID',intarr(maxde),$

'DataFileID',0),num_msgs)
  
i=0L
j=-1L
lasttime=0
for i=0L,n_elements(ftype_t)-1L do begin
; check to see if first or new time (new message)
;
;    if lasttime ne tst[i].c_pt then begin
    if ((cal and tst[i].group_flag eq 1) or (not cal and lasttime ne tst[i].c_pt)) then begin
;
; in order to use last time of ground receipt time. reset ground receipt time
;
       if j ge 0 then begin
          tstmsg[j].c_gr = tst[i-1].c_gr
          tstmsg[j].f_gr = tst[i-1].f_gr
       endif

       j=j+1L
       lasttime=tst[i].c_pt
       decounter=0
;
;   fill wrapper primary and secondary header
;
; Wrapper
       tstmsg[j].c_pt = tst[i].c_pt
       tstmsg[j].c_gr = tst[i].c_gr
       tstmsg[j].fr_sz = tst[i].fr_sz
       tstmsg[j].f_gr = tst[i].f_gr
;
; primary header
;
       tstmsg[j].versionid  = tst[i].versionid
       tstmsg[j].typeid  = tst[i].typeid
       tstmsg[j].header_flag  = tst[i].header_flag
       tstmsg[j].istid  = tst[i].istid
       tstmsg[j].pktid  = tst[i].pktid
       tstmsg[j].pkt_len  = tst[i].pkt_len
;
; secondary header
       tstmsg[j].c_pt_sh  = tst[i].c_pt_sh
       tstmsg[j].f_pt_sh  = tst[i].f_pt_sh
       tstmsg[j].telem  = tst[i].telem

    endif
    CASE tst[i].group_flag OF
      ; first packet in message
      1 : BEGIN
          tstmsg[j].ISECTORPOS = double(ishft(long(bdata[tst[i].dataidx]),8)+ishft(long(bdata[tst[i].dataidx+1]),0))/17.7832D - 2035.D /17.7832D
          tstmsg[j].ISECTORPOS333  = double(ishft(long(bdata[tst[i].dataidx+2]),8)+ishft(long(bdata[tst[i].dataidx+3]),0))/17.7832D - 2035.D /17.7832D
          tstmsg[j].ISECTORPOS666  = double(ishft(long(bdata[tst[i].dataidx+4]),8)+ishft(long(bdata[tst[i].dataidx+5]),0))/17.7832D - 2035.D /17.7832D
          tstmsg[j].ISECTORPOS999     = double(ishft(long(bdata[tst[i].dataidx+6]),8)+ishft(long(bdata[tst[i].dataidx+7]),0))/17.7832D - 2035.D /17.7832D
          tstmsg[j].ISECTORTIMELAPSE = ishft(long(bdata[tst[i].dataidx+8]),8)+ishft(long(bdata[tst[i].dataidx+9]),0)
          tstmsg[j].ISECTORNUMBER   = ishft(long(bdata[tst[i].dataidx+10]),0)
          tstmsg[j].ISCANSTATE     = ishft(long(bdata[tst[i].dataidx+11]),0)
          tstmsg[j].ISECTORVELOCITY  = (double(ishft(long(bdata[tst[i].dataidx+12]),8)+ishft(long(bdata[tst[i].dataidx+13]),0))-2035.D)/314.725D
          tstmsg[j].ILADCNT1OFFSET0 = ishft(long(bdata[tst[i].dataidx+14]),8)+ishft(long(bdata[tst[i].dataidx+15]),0)
          tstmsg[j].ILADCNT2OFFSET0 = ishft(long(bdata[tst[i].dataidx+16]),8)+ishft(long(bdata[tst[i].dataidx+17]),0)
          tstmsg[j].ILADCNT1OFFSET2 = ishft(long(bdata[tst[i].dataidx+18]),8)+ishft(long(bdata[tst[i].dataidx+19]),0)
          tstmsg[j].ILADCNT2OFFSET2 = ishft(long(bdata[tst[i].dataidx+20]),8)+ishft(long(bdata[tst[i].dataidx+21]),0)
          tstmsg[j].IFEETOTALVALIDS = ishft(long(bdata[tst[i].dataidx+22]),8)+ishft(long(bdata[tst[i].dataidx+23]),0)
          tstmsg[j].ISTARTCNT1  = ishft(long(bdata[tst[i].dataidx+32]),8)+ishft(long(bdata[tst[i].dataidx+33]),0)
          tstmsg[j].ISTOPCNT1  = ishft(long(bdata[tst[i].dataidx+34]),8)+ishft(long(bdata[tst[i].dataidx+35]),0)
          tstmsg[j].IVALIDCNT1  = ishft(long(bdata[tst[i].dataidx+36]),8)+ishft(long(bdata[tst[i].dataidx+37]),0)
          tstmsg[j].ISTARTCNT2  = ishft(long(bdata[tst[i].dataidx+38]),8)+ishft(long(bdata[tst[i].dataidx+39]),0)
          tstmsg[j].ISTOPCNT2   = ishft(long(bdata[tst[i].dataidx+40]),8)+ishft(long(bdata[tst[i].dataidx+41]),0)
          tstmsg[j].IVALIDCNT2  = ishft(long(bdata[tst[i].dataidx+42]),8)+ishft(long(bdata[tst[i].dataidx+43]),0)
          tstmsg[j].ISTARTNOSTOPCNT1  = ishft(long(bdata[tst[i].dataidx+44]),8)+ishft(long(bdata[tst[i].dataidx+45]),0)
          tstmsg[j].ITOFUNDRFLWCNT1   = ishft(long(bdata[tst[i].dataidx+46]),8)+ishft(long(bdata[tst[i].dataidx+47]),0)
          tstmsg[j].ISTARTSTARTCNT1   = ishft(long(bdata[tst[i].dataidx+48]),8)+ishft(long(bdata[tst[i].dataidx+49]),0)
          tstmsg[j].ISTARTHGHTOVRLD1  = ishft(long(bdata[tst[i].dataidx+50]),8)+ishft(long(bdata[tst[i].dataidx+51]),0)
          tstmsg[j].ISTOPHGHTOVRLD1   = ishft(long(bdata[tst[i].dataidx+52]),8)+ishft(long(bdata[tst[i].dataidx+53]),0)
          tstmsg[j].ISTARTPOSUNDRFLW1  = ishft(long(bdata[tst[i].dataidx+54]),8)+ishft(long(bdata[tst[i].dataidx+55]),0)
          tstmsg[j].ISTARTPOSOVRFLW1 = ishft(long(bdata[tst[i].dataidx+56]),8)+ishft(long(bdata[tst[i].dataidx+57]),0)
          tstmsg[j].ISTOPPOSUNDRFLW1 = ishft(long(bdata[tst[i].dataidx+58]),8)+ishft(long(bdata[tst[i].dataidx+59]),0)
          tstmsg[j].ISTOPPOSOVRFLW1  = ishft(long(bdata[tst[i].dataidx+60]),8)+ishft(long(bdata[tst[i].dataidx+61]),0)
          tstmsg[j].IEVENTBUFOVRFLW1     = ishft(long(bdata[tst[i].dataidx+62]),8)+ishft(long(bdata[tst[i].dataidx+63]),0)
          tstmsg[j].ISTARTNOSTOPCNT2     = ishft(long(bdata[tst[i].dataidx+64]),8)+ishft(long(bdata[tst[i].dataidx+65]),0)
          tstmsg[j].ITOFUNDRFLWCNT2      = ishft(long(bdata[tst[i].dataidx+66]),8)+ishft(long(bdata[tst[i].dataidx+67]),0)
          tstmsg[j].ISTARTSTARTCNT2      = ishft(long(bdata[tst[i].dataidx+68]),8)+ishft(long(bdata[tst[i].dataidx+69]),0)
          tstmsg[j].ISTARTHGHTOVRLD2     = ishft(long(bdata[tst[i].dataidx+70]),8)+ishft(long(bdata[tst[i].dataidx+71]),0)
          tstmsg[j].ISTOPHGHTOVRLD2      = ishft(long(bdata[tst[i].dataidx+72]),8)+ishft(long(bdata[tst[i].dataidx+73]),0)
          tstmsg[j].ISTARTPOSUNDRFLW2    = ishft(long(bdata[tst[i].dataidx+74]),8)+ishft(long(bdata[tst[i].dataidx+75]),0)
          tstmsg[j].ISTARTPOSOVRFLW2     = ishft(long(bdata[tst[i].dataidx+76]),8)+ishft(long(bdata[tst[i].dataidx+77]),0)
          tstmsg[j].ISTOPPOSUNDRFLW2     = ishft(long(bdata[tst[i].dataidx+78]),8)+ishft(long(bdata[tst[i].dataidx+79]),0)
          tstmsg[j].ISTOPPOSOVRFLW2      = ishft(long(bdata[tst[i].dataidx+80]),8)+ishft(long(bdata[tst[i].dataidx+81]),0)
          tstmsg[j].IEVENTBUFOVRFLW2     = ishft(long(bdata[tst[i].dataidx+82]),8)+ishft(long(bdata[tst[i].dataidx+83]),0)
;
; first packet contains 405 bytes of Direct Events data (or 81 sets)
;
; unpack direct events
;
; set byte index to start of direct events
;
         deindex=tst[i].dataidx+84+8
         k=0
         for k=0, 80 do begin
            tstmsg[j].TOF[decounter] = ishft(bdata[deindex+k*5],0)
            tstmsg[j].STARTHT[decounter]= ishft(bdata[deindex+1+k*5],0)
            tstmsg[j].STARTPOS[decounter]= ishft(bdata[deindex+2+k*5] AND 252,-2)
            tstmsg[j].STOPHT[decounter]= ishft(bdata[deindex+3+k*5] AND 254,-1)
            tstmsg[j].STOPPOS[decounter]= ishft(bdata[deindex+4+k*5],0)
            tstmsg[j].SLICE[decounter]= ishft(bdata[deindex+2+k*5] AND 2,0) + $
                          ishft(bdata[deindex+3+k*5] AND 1,0)
            tstmsg[j].DEID[decounter]= ishft(bdata[deindex+2+k*5] AND 1,0)
            decounter=decounter+1 
         endfor


          ENDCASE
      ; middle packet in message
      0 : BEGIN
;
; set byte index to start of direct events
;
         deindex=tst[i].dataidx
         k=0
         for k=0, 98 do begin
            tstmsg[j].TOF[decounter] = ishft(bdata[deindex+k*5],0)
            tstmsg[j].STARTHT[decounter]= ishft(bdata[deindex+1+k*5],0)
            tstmsg[j].STARTPOS[decounter]= ishft(bdata[deindex+2+k*5] AND 252,-2)
            tstmsg[j].STOPHT[decounter]= ishft(bdata[deindex+3+k*5] AND 254,-1)
            tstmsg[j].STOPPOS[decounter]= ishft(bdata[deindex+4+k*5],0)
            tstmsg[j].SLICE[decounter]= ishft(bdata[deindex+2+k*5] AND 2,0) + $
                          ishft(bdata[deindex+3+k*5] AND 1,0)
            tstmsg[j].DEID[decounter]= ishft(bdata[deindex+2+k*5] AND 1,0)
            decounter=decounter+1 

         endfor


          ENDCASE
      ; last packet in message
      2 : BEGIN
;
; set byte index to start of direct events
;
         deindex=tst[i].dataidx
         k=0
         for k=0, 98 do begin
            tstmsg[j].TOF[decounter] = ishft(bdata[deindex+k*5],0)
            tstmsg[j].STARTHT[decounter]= ishft(bdata[deindex+1+k*5],0)
            tstmsg[j].STARTPOS[decounter]= ishft(bdata[deindex+2+k*5] AND 252,-2)
            tstmsg[j].STOPHT[decounter]= ishft(bdata[deindex+3+k*5] AND 254,-1)
            tstmsg[j].STOPPOS[decounter]= ishft(bdata[deindex+4+k*5],0)
            tstmsg[j].SLICE[decounter]= ishft(bdata[deindex+2+k*5] AND 2,0) + $
                          ishft(bdata[deindex+3+k*5] AND 1,0)
            tstmsg[j].DEID[decounter]= ishft(bdata[deindex+2+k*5] AND 1,0)
            decounter=decounter+1 

         endfor
          ENDCASE
      ENDCASE

endfor



; save test
; 
 tfilename=strcompress(datafile+'_tst.idl',/remove_all)
 save,tstmsg, filename=tfilename

END
