
; @(#)packet.pro	1.1 01/23/98 :LASCO IDL LIBRARY
;

PRO PACKET_EVENT, event

COMMON packet_common, base, stall, sci, hk, W_sci_cnt, W_hk1_cnt, W_hk2_cnt, W_hk3_cnt, W_sci_miss, $
                      W_hk1_miss, W_hk2_miss, W_hk3_miss

   IF (event.id EQ event.handler) THEN BEGIN   ; A top level base timer event

       SCI_PCKT_CNTR
       HK_PCKT_CNTR
       WIDGET_CONTROL, event.id, TIMER=stall   ; Request another

   ENDIF ELSE BEGIN

       WIDGET_CONTROL, event.id, GET_UVALUE=uval

       CASE (1) OF
           (uval EQ 99) : BEGIN     ;** exit program
               WIDGET_CONTROL, /DESTROY, base
               GOTO, DONE
           END
           ELSE : BEGIN
              PRINT, 'ERROR: Unknown uval: ', uval
           END
       ENDCASE
   ENDELSE
   DONE:
END

;-------------------------------------------------------------------------------

PRO PACKET, SELECT=select

COMMON packet_common, base, stall, sci, hk, W_sci_cnt, W_hk1_cnt, W_hk2_cnt, W_hk3_cnt, W_sci_miss, $
                      W_hk1_miss, W_hk2_miss, W_hk3_miss

   stall = 10	;** secs to wait in background process

   ;*****************************
   ;** SET UP SCIENCE PACKET INFO

   sci = {sci, file:'', unit:0L, file_size:0L, apid_offset:0L, cntr_offset:0L, pckt_len:0L, $
               pckt_cnt:0L, pckt_cntr:0L, pckt_cntr0:0L, pos:0L, missing_cntr:0L, loop:0L}
   sci.apid_offset = 16L		;** long location of apid word in science packet
   sci.cntr_offset = 18L		;** long location of packet counter word in science packet
   sci.pckt_len = 216*2L		;** length of science packet in long words
   sci.pckt_cnt = 0L			;** local count of science packets read in
   sci.pckt_cntr = 0L			;** science packet counter in science packets
   sci.missing_cntr = 0L		;** missing science packet counter
   sci.pos = sci.cntr_offset		;** position into science file for variable we are looking at
   sci.loop = 0L			;** loop count used to determine if file is being written to (active)

   SPAWN, 'ls /tools/lasco/LASSC*.raiv', result
   sci.file = result(N_ELEMENTS(result)-1)	;** use latest file
   IF KEYWORD_SET(SELECT) THEN BEGIN	;** let user select file
      sci.file = PICKFILE(FILE=sci.file, PATH='/tools/lasco', FILTER='LASSC*.raiv')
   ENDIF

   ;** get initial packet count
   GET_LUN, unit & sci.unit = unit
   OPENR, sci.unit, sci.file
   fs = FSTAT(sci.unit)
   sci.file_size = fs.size
   sci_cntr_pos = ASSOC(sci.unit, LONARR(1), sci.pos)
   sci.pckt_cntr = (sci_cntr_pos(0) AND '3fff'xl)(0)
   sci.pckt_cnt = sci.pckt_cnt + 1
   sci.pos = LONG(sci.pckt_cnt * sci.pckt_len) + sci.cntr_offset


   ;************************
   ;** SET UP HK PACKET INFO

   hk = {hk, file:'', unit:0L, file_size:0L, apid_offset:0L, cntr_offset:0L, pckt_len:0L, $
               pckt_cnt:0L, hk1_pckt_cntr:0L, hk2_pckt_cntr:0L, hk3_pckt_cntr:0L, pos:0L, $
               hk1_missing_cntr:0L, hk2_missing_cntr:0L, hk3_missing_cntr:0L, loop:0L}
   hk.apid_offset = 16L		;** long location of apid word in hk packet
   hk.cntr_offset = 18L		;** long location of packet counter word in hk packet
   hk.pckt_len = 157*2L		;** length of hk packet in long words
   hk.pckt_cnt = 0L		;** local count of hk packets read in
   hk.hk1_pckt_cntr = 0L	;** hk1 packet counter in hk packets
   hk.hk2_pckt_cntr = 0L	;** hk2 packet counter in hk packets
   hk.hk3_pckt_cntr = 0L	;** hk3 packet counter in hk packets
   hk.hk1_missing_cntr = 0L	;** missing hk1 packet counter
   hk.hk2_missing_cntr = 0L	;** missing hk2 packet counter
   hk.hk3_missing_cntr = 0L	;** missing hk3 packet counter
   hk.pos = hk.apid_offset	;** position into hk file for variable we are looking at
   hk.loop = 0L			;** loop count used to determine if file is being written to (active)

   ;** USE CORRESPONDING HK FILE
   temp = sci.file
   pos = STRPOS(sci.file, 'LASSC')
   STRPUT, temp, 'HK', pos+3
   hk.file = temp

   ;** get initial packet count for the 3 types
   GET_LUN, unit & hk.unit = unit
   OPENR, hk.unit, hk.file
   fs = FSTAT(hk.unit)
   hk.file_size = fs.size
   FOR i=0, 2 DO BEGIN
      hka = ASSOC(hk.unit, LONARR(1), hk.pos)
      apid = (hka(0) AND 'ffff'xl)(0)
      CASE (apid) OF
         ('8869'xl) : hk.hk1_pckt_cntr = ((hka(1)/65535L) AND '00003fff'xl)(0)
         ('886a'xl) : hk.hk2_pckt_cntr = ((hka(1)/65535L) AND '00003fff'xl)(0)
         ('886c'xl) : hk.hk3_pckt_cntr = ((hka(1)/65535L) AND '00003fff'xl)(0)
         ELSE : PRINT, '%%PACKET:  Unknown APID ', apid
      ENDCASE
      hk.pckt_cnt = hk.pckt_cnt + 1
      hk.pos = LONG(hk.pckt_cnt * hk.pckt_len) + hk.apid_offset
   ENDFOR


   ;*****************
   ;** SET UP WIDGETS

   base = WIDGET_BASE(/COLUMN, TITLE='TELEMETRY PACKET STATISTICS', XSIZE=370, XOFFSET=20)
      row0 = WIDGET_BASE(base, /ROW)
         temp = WIDGET_LABEL(row0, VALUE='                 Science')
         temp = WIDGET_LABEL(row0, VALUE='   HK1')
         temp = WIDGET_LABEL(row0, VALUE='      HK2')
         temp = WIDGET_LABEL(row0, VALUE='      HK3')
      row1 = WIDGET_BASE(base, /ROW)
         temp = WIDGET_LABEL(row1, VALUE='  Valid Count :')
         W_sci_cnt = WIDGET_TEXT(row1, ysize=1, xsize=7, value='0')
         W_hk1_cnt = WIDGET_TEXT(row1, ysize=1, xsize=7, value='0')
         W_hk2_cnt = WIDGET_TEXT(row1, ysize=1, xsize=7, value='0')
         W_hk3_cnt = WIDGET_TEXT(row1, ysize=1, xsize=7, value='0')
      row2 = WIDGET_BASE(base, /ROW)
         temp = WIDGET_LABEL(row2, VALUE='Missing Count :')
         W_sci_miss = WIDGET_TEXT(row2, ysize=1, xsize=7, value='0')
         W_hk1_miss = WIDGET_TEXT(row2, ysize=1, xsize=7, value='0')
         W_hk2_miss = WIDGET_TEXT(row2, ysize=1, xsize=7, value='0')
         W_hk3_miss = WIDGET_TEXT(row2, ysize=1, xsize=7, value='0')

      done = WIDGET_BUTTON(base, value='    Done     ', UVALUE=99)

   WIDGET_CONTROL, /REALIZE, base
   WIDGET_CONTROL, base, TIMER=1
   XMANAGER, 'PACKET', base

END
