;+
; Project     : SOHO - CDS     
;                   
; Name        : INPUT_ENGAB
;                                                      
; Purpose     : Background control for engineering telemtery 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     : 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, 29-Sep-94
;               
; Modified    : Modified message format.  CDP, 3-Feb-95
;               Fix number of packets output format.  CDP, 20-Apr-95
;               Fix for lack of xbackregister in IDL v5.  CDP, 12-Apr-99
;
; Version     : Version 4, 12-Apr-99
;-            

pro input_engab, widget_base

;
;  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
                        if (nparm_data mod 10) eq 0 then begin
                           nc = strtrim(string(nparm_data,format='(i8)'),2)
                           widget_control, msg_widg,/append,set_val=$
                             'Eng A packet # '+nc+' --> '+time
                        endif
           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
                        if (nparm_data mod 10) eq 0 then begin
                           nc = strtrim(string(nparm_data,format='(i8)'),2)
                           widget_control, msg_widg,/append,set_val=$
                             'Eng B packet # '+nc+' --> '+time
                        endif
           end
         else:
endcase

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
      emon_sensitive,1
;      kill_engab, widget_base, msg_widg
      widget_control, dummy_label, /destroy
      flash_msg, msg_widg, /append, 'Data taking has detected a file error.'
      return
   endif
   
   widget_control, msg_widg, /append, set_v='Opened new file '+$
                                      file_name(file_seq_no)
   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
   emon_sensitive,1
;   kill_engab, widget_base, msg_widg
    widget_control, dummy_label, /destroy
    flash_msg, msg_widg, /append, 'Data taking has stopped.'
   return
endelse


end
