;+ 
;NAME:
; thm_ui_run_calc
;
;PURPOSE:
; Function that interprets program for thm_ui_calculate
;
;CALLING SEQUENCE:
; thm_ui_run_calc,programtext,loadeddata,historywin,statusbar,fail=fail
;
;INPUT:
; programText: array of strings, text of program
; loadeddata: the loaded data object
; historywin: the historywin object
; statusbar: the statusbar object
;
;OUTPUT:
; fail=fail: set to named variable, will be 1 if an error occurs during processing.
;
;HISTORY:
;$LastChangedBy: jimmpc $
;$LastChangedDate: 2009-05-29 15:10:52 -0700 (Fri, 29 May 2009) $
;$LastChangedRevision: 6004 $
;$URL: svn+ssh://thmsvn@ambrosia.ssl.berkeley.edu/repos/thmsoc/tags/tdas_5_02/idl/themis/thm_ui_new/utilities/thm_ui_run_calc.pro $
;
;---------------------------------------------------------------------------------


pro thm_ui_run_calc,programtext,loadeddata,historywin,statusbar,fail=fail

  compile_opt hidden,idl2
  
  pi = !DPI
  e = exp(1)
  
  fail = 0
  
  ;list of names so that we can delete any newly created names
  tname_list = tnames()
  
  for i = 0,n_elements(programtext)-1 do begin
  
    ;widget_control,state.programLabel,set_value="Calculating line: " + strtrim(string(i),2)
    
    statusBar->update,'Calculating line: ' + strtrim(string(i),2)
    historyWin->update,'Calculating line: ' + strtrim(string(i),2)
    
    if keyword_set(programtext[i]) then begin
      calc,programtext[i],gui_data_obj=loadedData,error=fail
    endif
    
    if keyword_set(fail) then begin
    
      break
    
    endif
  
  endfor
  
  ;list of names after processing
  tnew_list = tnames()
  
  if tnew_list[0] ne '' then begin
  
    if tname_list[0] eq '' then begin
      del_list = tnew_list
    endif else begin
      del_list = ssl_set_complement([tname_list],[tnew_list])
    endelse
    
    ;delete any names that didn't pre-exist to prevent duplication
    ;of data inside the gui and in tplot
    if ~is_num(del_list[0]) then begin
      store_data,del_list,/delete
    endif 
  
  endif
  
end