;+
; Project     : SOHO - CDS     
;                   
; Name        : B_INPUT_ENGAB
;                                                      
; Purpose     : Batch control for engineering telemetry read. 
;
; Explanation : Reads the packets in a telemetry file sequentially, checking
;               the IDs for values which indicate an engineering A or B 
;               packet.  Loads the data structures when these packets are seen.
;               
; Use         : Only within the EMON package
;    
; Inputs      : None
;
; Opt. Inputs : None
;               
; Outputs     : status - equals 1 while still more to read.
;               Updates storage arrays in common
;
; Opt. Outputs: None
;               
; Keywords    : None
;
; Calls       :
;
; Common      : emon.com
;               
; Restrictions: None
;               
; Side effects: None
;               
; Category    : Telemetry
;               
; Prev. Hist. : None
;
; Written     : C D Pike, RAL, 23-Dec-94
;               
; Modified    : 
;
; Version     : Version 1, 23-Dec-94
;-            

pro b_input_engab, status

;
;  common for variables
;
@emon.com

;
;  array for raw packet data
;
packet=bytarr(306)


;
;  trap i/o errors
;
on_ioerror, err_finish

;
;  read the file looking for eng packets
;
read_packet:

readu, eng_lun, packet

case  1 of
        (packet(18) eq '0A'x) and (wanted_packets eq 'A'): begin
                        load_eng_a, packet, mask, enga, select=wanted_parms
                        load_eng_parms, wanted_parms, enga, time, data
                        ut_data(nparm_data) = time
                        parm_data(*,nparm_data) = data
                        nparm_data = nparm_data + 1
           end

        (packet(18) eq '0B'x) and (wanted_packets eq 'B'): begin
                        load_eng_b, packet, mask, engb, select=wanted_parms
                        load_eng_parms, wanted_parms, engb, time, data
                        ut_data(nparm_data) = time
                        parm_data(*,nparm_data) = data
                        nparm_data = nparm_data + 1
           end
         else:
endcase

status = 1
return

;
;  handle i/o error (hopefully end-of-file)
;
err_finish:

free_lun, eng_lun
;
;  if there are more files, get the next one
;

if file_seq_no lt (num_data_files-1) then begin
   file_seq_no = file_seq_no + 1
   openr, eng_lun, file_name(file_seq_no), error=err, /get_lun
;
;  the end because of open error, so tidy up and quit
;
   if err ne 0 then begin
      reading = 0
      n = where(ut_data ne '')
      if n(0) ge 0 then begin
         ut_data = ut_data(n)
         parm_data = parm_data(*,n)
      endif
      status = 0
      return
   endif
   goto, read_packet  ; sorry!

endif else begin
;
;  it really is the end so tidy up and quit
;
   reading = 0
   n = where(ut_data ne '')
   if n(0) ge 0 then begin
      ut_data = ut_data(n)
      parm_data = parm_data(*,n)
   endif
   status = 0
   return
endelse


end
