;+
; Project     : SOHO - CDS     
;                   
; Name        : LOAD_ENG_A
;               
; Purpose     : Loads Eng_A packets into data structure.
;               
; Explanation : Given a packet (306 bytes) of telemetry data that has
;               been identified as an EngA packet, this routine
;               loads the data into the data structure defined by ENG_STRUCT.
;               Note that this has to be a convoluted process since the
;               structure contains variables which are derived by masking
;               single input bytes.
;               
; Use         : load_eng_ab, packet, mask_etc, eng_a [,select=select]
;    
; Inputs      : packet   - bytarr(306) containing telemetry packet
;               mask_etc - array containing masks etc (from GET_MASK_ETC)
;               
; Opt. Inputs : None
;               
; Outputs     : eng_a   -  the Engineering A data structure
;               
; Opt. Outputs: None
;               
; Keywords    : select - if present contains a string array of the tags that
;                        are to be loaded (if absent, all are loaded)
;
; Calls       : None
;
; Common      : None
;               
; Restrictions: None
;               
; Side effects: None
;               
; Category    : Telemetry
;               
; Prev. Hist. : None
;
; Written     : C D Pike, 26-Sep-94
;               
; Modified    : Introduce select keyword.  CDP, 10-Oct-94
;               Fixed bug on use of masks.  CDP, 21-Dec-94
;
; Version     : Version 3, 21-Dec-94
;-            

pro load_eng_a, packet, mask_etc, enga, select=select


;
;  do each substructure separately
;
;
;  first tag ENGA.CDHS
;
if keyword_set(select) then begin
   nt=n_elements(select) 
endif else begin
   nt = n_tags(enga.enga_cdhs)
endelse

;
;  get list of tag names
;
list = tag_names(enga.enga_cdhs)

for i=0,nt-1 do begin            
 
   if keyword_set(select) then begin
      name = select(i)
      it = where(list eq name) & it = it(0)
   endif else begin
      name = list(i)
      it = i
   endelse

;
;  check if exists in this sub-structure
;
   if it ge 0 then begin

      name = repstr(name,'_POS_','+')
      name = repstr(name,'_NEG_','-')
   
      n = where(mask_etc(0,*) eq name)
      if n(0) ge 0 then begin
      n = n(0)
      temp = strmid(mask_etc(5,n),2,4)
      hex2dec,temp,mask,/quiet
      offset = fix(mask_etc(3,n))
      case mask_etc(2,n) of
          'B': begin
                  if mask gt 0 then begin
                     enga.enga_cdhs.(it) = packet(offset) and mask
                  endif else begin
                     enga.enga_cdhs.(it) = packet(offset)
                  endelse
               end
          'W': begin
                temp = byteswap((fix(packet,offset,1))(0))
                if mask gt 0 then begin
                  enga.enga_cdhs.(it) = unsign(temp) and long(mask)
                endif else begin
                  enga.enga_cdhs.(it) = unsign(temp)
                endelse
               end
          'L': enga.enga_cdhs.(it) = (long(packet,offset,1))(0)
         endcase
      endif
   endif   
endfor

;
;  before leaving this tag, calculate the OBT
;
obt = bytarr(6)
t1 = long(packet,12,1)
t2 =  fix(packet,16,1)
obt(0:3) = byte(t1,0,4)
obt(4:5) = byte(t2,0,2)
enga.enga_cdhs.a_obt_time = anytim2cal(tai2utc(obt2tai(obt)))
;
;
;  then tag ENGA.EPS
;
;
if keyword_set(select) then begin
   nt=n_elements(select) 
endif else begin
   nt = n_tags(enga.enga_eps)
endelse

;
;  get list of tag names
;
list = tag_names(enga.enga_eps)

for i=0,nt-1 do begin            
 
   if keyword_set(select) then begin
      name = select(i)
      it = where(list eq name) & it = it(0)
   endif else begin
      name = list(i)
      it = i
   endelse

;
;  check if exists in this sub-structure
;
   if it ge 0 then begin

      name = repstr(name,'_POS_','+')
      name = repstr(name,'_NEG_','-')
   
      n = where(mask_etc(0,*) eq name)
      if n(0) ge 0 then begin
      n = n(0)
      temp = strmid(mask_etc(5,n),2,4)
      hex2dec,temp,mask,/quiet
      offset = fix(mask_etc(3,n))
      case mask_etc(2,n) of
          'B': begin
                  if mask gt 0 then begin
                     enga.enga_eps.(it) = packet(offset) and mask
                  endif else begin
                     enga.enga_eps.(it) = packet(offset)
                  endelse
               end
          'W': begin
                temp = byteswap((fix(packet,offset,1))(0))
                if mask gt 0 then begin
                  enga.enga_eps.(it) = unsign(temp) and long(mask)
                endif else begin
                  enga.enga_eps.(it) = unsign(temp)
                endelse
               end
          'L': enga.enga_eps.(it) = (long(packet,offset,1))(0)
         endcase
      endif
   endif   
endfor

;
;
;  then tag ENGA.VDS
;
if keyword_set(select) then begin
   nt=n_elements(select) 
endif else begin
   nt = n_tags(enga.enga_vds)
endelse

;
;  get list of tag names
;
list = tag_names(enga.enga_vds)

for i=0,nt-1 do begin            
 
   if keyword_set(select) then begin
      name = select(i)
      it = where(list eq name) & it = it(0)
   endif else begin
      name = list(i)
      it = i
   endelse

;
;  check if exists in this sub-structure
;
   if it ge 0 then begin

      name = repstr(name,'_POS_','+')
      name = repstr(name,'_NEG_','-')
   
      n = where(mask_etc(0,*) eq name)
      if n(0) ge 0 then begin
      n = n(0)
      temp = strmid(mask_etc(5,n),2,4)
      hex2dec,temp,mask,/quiet
      offset = fix(mask_etc(3,n))
      case mask_etc(2,n) of
          'B': begin
                  if mask gt 0 then begin
                     enga.enga_vds.(it) = packet(offset) and mask
                  endif else begin
                     enga.enga_vds.(it) = packet(offset)
                  endelse
               end
          'W': begin
                temp = byteswap((fix(packet,offset,1))(0))
                if mask gt 0 then begin
                  enga.enga_vds.(it) = unsign(temp) and long(mask)
                endif else begin
                  enga.enga_vds.(it) = unsign(temp)
                endelse
               end
          'L': enga.enga_vds.(it) = (long(packet,offset,1))(0)
         endcase
      endif
   endif   
endfor


end


