;+
;NAME:
; thm_ui_summplot
;PURPOSE:
; This widget prompts the user for a choice of probe, and then Calls
; the thm_gen_overplot routine.
;HISTORY:
; 21-jun-2007, jmm, jimm@ssl.berkeley.edu
; 31-jul-2007, jmm, resets active data in correct order after plotting
;$LastChangedBy$
;$LastChangedDate$
;$LastChangedRevision$
;$URL$
;-
Pro thm_ui_summplot_event, event

  @tplot_com

  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
      For j = 0, n_elements(state.button_arr)-1 Do widget_control, state.button_arr[j], sensitive = 1
      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
        For j = 0, n_elements(state.button_arr)-1 Do widget_control, state.button_arr[j], sensitive = 1
        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
;start here
  widget_control, event.id, get_uval = uval
  Case uval Of
    'EXIT': widget_control, event.top, /destroy
    'PRLIST':Begin
      widget_control, event.top, get_uval = state, /no_copy
      For j = 0, n_elements(state.button_arr)-1 Do widget_control, state.button_arr[j], sensitive = 0
      pindex = widget_info(state.prlist, /list_select)
      state.this_probe = state.probes[pindex]
      history_ext = 'probe = '''+state.this_probe+''''
      thm_ui_update_history, state.cw, history_ext
      For j = 0, n_elements(state.button_arr)-1 Do widget_control, state.button_arr[j], sensitive = 1
      widget_control, event.top, set_uval = state, /no_copy
    End
   'PLOT':Begin
      widget_control, event.top, get_uval = state, /no_copy
      For j = 0, n_elements(state.button_arr)-1 Do widget_control, state.button_arr[j], sensitive = 0
      widget_control, state.cw, get_uval = wstate, /no_copy
      progobj = wstate.progobj
      t0 = wstate.st_time & t1 = wstate.en_time
      widget_control, state.cw, set_uval = wstate, /no_copy
      If(t0 Eq 0 Or t1 Eq 0) Then Begin
        thm_ui_update_progress, state.cw, 'Please Choose a Time Range'
      Endif Else Begin
;You need a start time, and a duration in hours
        thm_ui_update_progress, state.cw, 'Creating Overview Plot'
        n_hours = ceil((t1-t0)/3600.0d0)
        history_ext = 'thm_gen_overplot, probe='+''''+state.this_probe+''''+$
          ', date ='+''''+time_string(t0)+''''+', dur = '+$
          strtrim(string(n_hours), 2)+', /hours, /dont_delete_data'
        thm_ui_update_history, state.cw, history_ext
        thm_gen_overplot, probe = state.this_probe, date = t0, dur = n_hours, $
          /hours, /dont_delete_data
        sc = state.this_probe & typ = 'f'
	tvn0 = tnames(['THEMIS_AU/AL_index','Keogram', $
                'th'+sc+'_fgs_gse', 'th'+sc+'_Nie', $
                'th'+sc+'_pei'+typ+'_V', 'th'+sc+'_Tie', 'sample_rate', $
                'th'+sc+'_psif_en_eflux', 'th'+sc+'_pei'+typ+'_en_eflux', $
                'th'+sc+'_psif_en_eflux', $
                'th'+sc+'_pee'+typ+'_en_eflux', 'th'+sc+'_fb_*', $
                'th'+sc+'_pos_gse_z'])
        history_ext = thm_ui_multichoice_history('varnames = ', tvn0)
;        thm_ui_temp_overplot, sc = state.this_probe, trange = [t0, t1], $
;          varnames_out = tvn, progobj = progobj
        tvn = tvn0
        thm_ui_update_data_all, state.cw, tvn
;kludge? to reset the active data in the order that it's plotted in
;case someone wants to replot it
        widget_control, state.cw, get_uval = wstate, /no_copy
        ptr_free, wstate.active_vnames
        wstate.active_vnames = ptr_new(tvn0)
        widget_control, state.cw, set_uval = wstate, /no_copy
        thm_ui_update_progress, state.cw, 'Finished Overview Plot'
      Endelse
      For j = 0, n_elements(state.button_arr)-1 Do widget_control, state.button_arr[j], sensitive = 1
      widget_control, event.top, set_uval = state, /no_copy
    End
  Endcase
  Return
End

Pro thm_ui_summplot, gui_id
  
  master = widget_base(title = 'THEMIS Science Software: Overview Plot', $
                       group_leader = gui_id, /row, scr_xsize = 360)
  probes = ['a', 'b', 'c', 'd', 'e']
  probes_ext = ['A (P5)',  'B (P1)',  'C (P2)',  'D (P3)', 'E (P4)']
  
  left = widget_base(master, /col, frame = 5, /align_center)
  label = widget_label(left, value = 'Choose One Probe')
  right = widget_base(master, /col, frame = 5, /align_center)

  prlist = widget_list(left, xsiz = 12, ysiz = 5, value = probes_ext, $
                       uval = 'PRLIST')

;plot button
  plbut = widget_button(right, val = ' Draw Overview Plot  ', $
                        uval = 'PLOT', /align_center, scr_xsize = 200)
;exit button
  exbut = widget_button(right, val = ' Close ', uval = 'EXIT', $
                        /align_center, scr_xsize = 050)

;set summplot_id in main GUI state
  widget_control, gui_id, get_uval = wstate, /no_copy
  wstate.summp_id = master
  widget_control, gui_id, set_uval = wstate, /no_copy
  
  state = {cw:gui_id, prlist:prlist, probes:probes, this_probe:'', $
          button_arr:[plbut, prlist]}

  widget_control, master, set_uval = state, /no_copy
  widget_control, master, /realize
  xmanager, 'thm_ui_summplot', master, /no_block
  

Return
End