;+
;NAME:
; thm_ui_call_cal_data
;PURPOSE:
; A wrapper for thm_ui_cal_data that handles history correctly
;CALLING SEQUENCE:
; thm_ui_cal_data, widget_id, instrument
;INPUT:
; widget_id = the state structure of the calling widget
; instrument = the type of data to calibrate, 'EFI','FBK','FIT', 'FGM', 'MOM'
;OUTPUT:
; None explicit
;HISTORY:
; 10-apr-2007, jmm, rewritten again...
; 29-may-2007, jmm, changed to a widget of its own for new version of thm_gui
;$LastChangedBy$
;$LastChangedDate$
;$LastChangedRevision$
;$URL$
;
;-
Pro thm_ui_call_cal_data_event, event, _extra = _extra

  err_xxx = 0
  catch, err_xxx
  If(err_xxx Ne 0) Then Begin
    catch, /cancel
    help, /last_message, output = err_msg
    For j = 0, n_elements(err_msg)-1 Do print, err_msg[j]
    If(is_struct(state)) Then Begin
      cw = state.cw
      widget_control, event.top, set_uval = state, /no_copy
    Endif Else Begin
      widget_control, event.top, get_uval = state, /no_copy
      If(is_struct(state)) Then Begin
        cw = state.cw
        widget_control, event.top, set_uval = state, /no_copy
      Endif Else cw = -1
    Endelse
    If(widget_valid(cw)) Then Begin
      If(is_struct(wstate)) Then widget_control, cw, set_uval = wstate
      thm_ui_update_history, cw, [';*** FYI', ';'+err_msg]
      thm_ui_update_progress, cw, 'Error--See history'
    Endif
    thm_ui_error
    Return
  Endif

  widget_control, event.id, get_uval = uval
  If(uval Eq 'EXIT') Then widget_control, event.top, /destroy Else Begin
      Case uval Of
          'CALEFI': dtyp = 'EFI'
          'CALFBK': dtyp = 'FBK'
          'CALFGM': dtyp = 'FGM'
          'CALFIT': dtyp = 'FIT'
          'CALMOM': dtyp = 'MOM'
      Endcase
      vars = ''
;Hunt down the data for the appropriate instrument
      vnames = tnames()
      probes = strmid(vnames, 2, 1)
      ssu =  uniq(probes)
      probe = probes[ssu]
      np = n_elements(probe)
;trange is often not used, so create a timespan
      tr = time_double(trange)
      t1 = str2time(strmid(time_string(tr[0]), 0, 10))
      t2 = str2time(strmid(time_string(tr[1]-1.), 0, 10))
      ndays = 1+fix((t2-t1)/(24.*3600.))
      timespan, t1, ndays
      Case dtyp Of
          'EFI': thm_cal_efi, valid_names = vvv
          'FBK': thm_cal_fbk, valid_names = vvv
          'FGM': vvv = ['fgl', 'fgh', 'fge']
          'FIT': thm_cal_fit, valid_names = vvv
          'MOM': thm_load_mom, /valid, datatype = vvv
      Endcase
;check loaded data for probes, and datatypes
;for each probe, check for valid datatypes
      use_probe = bytarr(np)
      For j = 0, np-1 Do Begin
          this_probe = where(probes Eq probe[j]) ;must be true for some
          dtyptest = strmid(vnames[this_probe], 4, 3)
          ctest = sswhere_arr(vvv, dtyptest)
          If(ctest[0] Ne -1) Then Begin
              use_probe[j] = 1
              For i = 0, n_elements(ctest)-1 Do $
                vars = [vars, thm_tplot_var(probe[j], vvv[ctest[i]])]
          Endif
      Endfor
      ok = where(use_probe Eq 1, nok)
      If(nok Eq 0) Then Begin
          message, 'No '+dtyp+' Data, please load data'
      Endif Else Begin
          scsin = probe[ok]
          Case dtyp Of
              'EFI': thm_cal_efi, scs = scsin, trange = tr
              'FBK': thm_cal_fbk, scs = scsin, trange = tr
              'FGM': Begin      ;FGM data requires the spin period
                  thm_ui_check4spin, vars[1], vspinper, vspinphase, h
                  thm_cal_fgm, scs = scsin
              End
              'FIT': thm_cal_fit, scs = scsin, trange = tr
              'MOM': thm_cal_mom, scs = scsin
          Endcase
;set the new variables to active
          all_vars = tnames()
          new_ssn = sswhere_arr(all_vars, vnames, /notequal)
          If(new_ssn[0] Ne -1) Then vars = all_vars[new_ssn] Else vars = ''
      End
      If(is_string(vars)) Then Begin
          vars = vars[1:*]
          widget_control, event.top, get_uval = state, /no_copy
          If(is_struct(state)) Then Begin
              If(widget_valid(state.cw)) Then Begin
                  data_ss = thm_ui_set_active_dset(state.cw, vars)
                  thm_ui_update_history, state.cw, $
                    thm_ui_multichoice_history('varnames = ',vars)
                  thm_ui_update_progress, state.cw, 'Cal of '+dtyp+' Successful'
              Endif
              widget_control, event.top, set_uval = state, /no_copy
          Endif
      Endif
  Endelse
End

Pro thm_ui_call_cal_data, gui_id, _extra = _extra

;Build the widget
  master = widget_base(/col, title = 'THEMIS Science Software: Calibration', $
                       scr_xsize = 300, /align_top, group_leader = gui_id)
;Instrument Buttons widget
  calbutbase = widget_base(master, /col, /align_center, frame = 5)
  blabel = widget_label(calbutbase, value = 'Instrument')
;Buttons for instruments
  calbut1 = widget_button(calbutbase, val = '     EFI     ', uval = 'CALEFI', $
                          /align_center)
  calbut2 = widget_button(calbutbase, val = '     FBK     ', uval = 'CALFBK', $
                          /align_center)
  calbut3 = widget_button(calbutbase, val = '     FGM     ', uval = 'CALFGM', $
                          /align_center)
  calbut4 = widget_button(calbutbase, val = '     FIT     ', uval = 'CALFIT', $
                          /align_center)
  calbut6 = widget_button(calbutbase, val = '     FFT     ', uval = 'CALFFT', $
                          /align_center)
  calbut5 = widget_button(calbutbase, val = '     MOM     ', uval = 'CALMOM', $
                          /align_center)
  calbut7 = widget_button(calbutbase, val = '     SCM     ', uval = 'CALSCM', $
                          /align_center)
  calbut8 = widget_button(calbutbase, val = '     SST     ', uval = 'CALSST', $
                          /align_center)
;Exit button
  exitbut = widget_button(master, val = '   Close   ', uval = 'EXIT')
  
  widget_control, gui_id, get_uval = wstate, /no_copy
  wstate.cal_id = master
  widget_control, gui_id, set_uval = wstate, /no_copy

  state = {thisw:master, cw:gui_id}

  If(state.cw Ne -1) Then thm_ui_update_progress, state.cw, $
    'Calibrating Data'

  widget_control, master, set_uval = state, /no_copy
  widget_control, master, /realize
  xmanager, 'thm_ui_call_cal_data', master

End