pro extract_slld, infile, sctime, slld

;
;  Structure for data records in MSFC HKG_DATA.DAT file.
;

hkg_STR = {hkg_str,$           ;124

          SCTIME:INTARR(3),$       ;spacecraft time (GRO time format)
          COUNTER:0L,$             ;packet counter
          X_POS:0,$                ;X coordinate  (GRO position in 
          Y_POS:0,$                ;Y coordinate  Earth-centered inertial
          Z_POS:0,$                ;Z coordinate  coordinates in 
                                   ;              1/4 km units)
          GEO_AZ:0,$               ;azimuth of geocenter position in
                                   ;units of .0001 RAD
          GEO_EL:0,$               ;elevation of geocenter position
                                   ;in units of .0001 RAD
          Z_RA:0,$                 ;+Z right ascension  (GRO pointing 
          Z_DEC:0,$                ;+Z declination      direction astronomical 
          X_RA:0,$                 ;+X right ascension  coordinates in units
          X_DEC:0,$                ;+X declination      of .0001 RAD)
          HKGSEQ:0,$               ;housekeeping sequence number
          HKGMOD:INTARR(8),$       ;module housekeeping data
          HKGMSC1:0,$              ;miscellaneous housekeeping word 1
          HKGMSC2:0,$              ;miscellaneous housekeeping word 2
          HKGMTB:0,$               ;module parameter table
          DEADTM:0,$               ;program deadtime
          NLDI:0,$                 ;number of LDI interrupts
          STAT:INTARR(3),$         ;status words
          ERRF:0,$                 ;error flags
          ERRDAT:INTARR(2),$       ;error data
          NSPEC:0,$                ;number of SHERB spectra stored
          OUTADD:0,$               ;output word address
          COMXQT:0B,$              ;commands executed
          COMREC:0B,$              ;commands received
          LRCVD:0,$                ;last command received
          LXQT:0,$                 ;last command executed
          CMECHO:0,$               ;command echo
          EXFLAG:INTARR(8),$       ;flags for detectors above threshold
          COINC:0,$                ;coincident threshold exceedances
          CHECK:INTARR(8),$        ;checksums
          ENG_TELEM:0,$            ;0 = science telemetry record
                                   ;-1 = engineering telemetry record
                                   ; (during "gap") NOTE:  for ENG_TELEM
                                   ; = -1, DISC(1:3,0:7,2) has meaningful
                                   ; data, the remaining portions of DISC
                                   ; should contain "0"
          SPARE:INTARR(4)}        ;spare fields


e_slld = {extract_slld, $
    sctime: intarr(3), $
    hkgseq: 0, $
    hkgmtb: 0}

openr,lu1,/get, infile
in_struct = hkg_str 

;readu, lu1, hskpng
;read in the file definition record, but ignore it except to get the number of recs
readu, lu1, in_struct

fstat = fstat(lu1)
nrec =  (fstat.size-fstat.cur_ptr)/124

inchunk = replicate( in_struct, 1000)


;   
;   Use the same space each time for outdat
;
common extract_slld, outdat
if n_elements(outdat) ne 45000L then outdat = replicate( e_slld, 45000L)
outdat.(1) = outdat.(1)*0

w = -1
nrec = ( (fstat.size-fstat.cur_ptr)/124 ) < 1000
while nrec ge 1 do begin
    w = max(w)+lindgen(nrec)+1
    inchunk = inchunk(0:nrec-1)
    readu, lu1, inchunk
    outdat(w).sctime = inchunk.sctime
    outdat(w).hkgseq = inchunk.hkgseq
    outdat(w).hkgmtb  = inchunk.hkgmtb
    fstat=fstat(lu1)
    nrec =  (fstat.size-fstat.cur_ptr)/124 < 1000
endwhile 


free_lun,lu1


    w = where(outdat.(1) eq 3, nw)  ;start at id 0
    w = w(where( w(1:*)-w eq 32, nw))   ;only complete tables
    ww = lonarr(8,nw)
    ww(0,*) = w
    for i=0,7 do ww(i,*)= ww(0,*)+i*4
    
    slld = reform(((byte(outdat(ww(*)).(2),0,2,8*nw))(0,*))(*),8,nw)
    sctime = outdat(w).sctime
end