;+ ;NAME: ; thm_ui_call_tplot ;PURPOSE: ; A widget interface for calling tplot from the themis_w widget, ; currently of limited usefulness, but expandable ;CALLING SEQUENCE: ; thm_ui_call_tplot, master_widget_id ;INPUT: ; master_widget_id = the id number of the widget that calls this ;OUTPUT: ; none, there are buttons to push for plotting, setting limits, not ; sure what else yet... ;HISTORY: ; 14-dec-2006, jmm, jimm@ssl.berkeley.edu ; 13-feb-2007, jmm, fixed problem with !p.background resetting in ; set_plot commands ; 12-mar-2007, jmm, Added wavelet button ; 2-apr-2007, jmm, Added pwrspc options, NEEDS TESTING. ; jun-2007, jmm, split from dproc widget. ; 12-jul-2007, jmm, added a message widget, for invalid user input ; errors, grays out all buttons while processing ; 31-jul-2007, jmm, various checks for windows present to avoid tplot ; and ctime bombs ; ;$LastChangedBy: kenb-mac $ ;$LastChangedDate: 2007-01-26 15:52:34 -0800 (Fri, 26 Jan 2007) $ ;$LastChangedRevision: 241 $ ;$URL: svn+ssh://thmsvn@ambrosia.ssl.berkeley.edu/repos/thmsoc/trunk/idl/themis/common/thm_ui_call_tplot.pro $ ; ;- Pro thm_ui_call_tplot_event, event @tplot_com Common tlimits_saved, t0_sav, t1_sav, t0_init, t1_init common thm_ui_call_tplot_sav, plot_types, ptyp_sav 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 widget_control, state.messw, set_val = 'Error--See history' 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 widget_control, state.messw, set_val = 'Error--See history' 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 If(uval Eq 'EXIT') Then widget_control, event.top, /destroy Else Begin ;find the tplot variables that are selected, and do the tplot widget_control, event.top, get_uval = state, /no_copy widget_control, state.cw, get_uval = wstate, /no_copy If(ptr_valid(wstate.active_vnames)) Then Begin ;Clear out messw widget_control, state.messw, set_val = '' ;Disable all buttons button_arr = state.button_arr For j = 0, n_elements(button_arr)-1 Do widget_control, button_arr[j], sensitive = 0 tv_plot = *wstate.active_vnames widget_control, state.cw, set_uval = wstate, /no_copy ;Now you have valid names, do the plotting, etc... Case uval Of 'PTYPE':Begin pindex = widget_info(state.ptyplist, /list_select) state.ptyp = plot_types[pindex] ptyp_sav = state.ptyp history_ext = 'plot_type = '''+plot_types[pindex]+'''' thm_ui_update_history, state.cw, history_ext End 'PLOT':Begin widget_control, state.messw, set_val = 'Plotting '+tv_plot thm_ui_update_progress, state.cw, 'Plotting '+tv_plot tv_plot_s = ''''+tv_plot+'''' ;get the plot type ptyp = state.ptyp If(ptyp Eq 'PNG') Then Begin xt = time_string(systime(/sec)) ttt = strmid(xt, 0, 4)+strmid(xt, 5, 2)+strmid(xt, 8, 2)+$ '_'+strmid(xt, 11, 2)+strmid(xt, 14, 2)+strmid(xt, 17, 2) filename = 'thm_gui_plot_'+ttt osf = strupcase(!version.os_family) If(osf Eq 'WINDOWS') Then filename0 = file_expand_path('')+'\'+filename $ Else filename0 = file_expand_path('')+'/'+filename filename = dialog_pickfile(title = 'THEMIS GUI Plot Filename', $ filter = '*.png', file = filename0) If(is_string(filename)) Then Begin dname = !d.name ;'X' or 'WIN' set_plot, 'z' ;do this in the z-buffer !p.background = !d.table_size-1 ;White background (color table 34) !p.color = 0 ; Black Pen !p.font = -1 ; Use default fonts tplot, tv_plot makepng, filename history_ext = ['dname = !d.name', 'set_plot, ''z''', $ 'tplot, '+tv_plot_s, 'makepng, '+''''+filename+'''', $ 'set_plot, dname', $ ';CREATED: '+filename] widget_control, state.messw, set_val = 'CREATED: '+filename+'.png' thm_ui_update_progress, state.cw, 'CREATED: '+filename+'.png' set_plot, dname !p.background = !d.table_size-1 ;White background (color table 34) !p.color = 0 ; Black Pen !p.font = -1 ; Use default fonts Endif Else Begin widget_control, state.messw, set_val = 'Operation Cancelled' thm_ui_update_progress, state.cw, 'Operation Cancelled' Endelse Endif Else Begin If(!version.os_family Eq 'Windows') Then begin set_plot, 'win' history_ext = ['set_plot, ''win''', 'tplot, '+tv_plot_s] Endif Else Begin set_plot, 'x' history_ext = ['set_plot, ''x''', 'tplot, '+tv_plot_s] Endelse !p.background = !d.table_size-1 ;White background (color table 34) !p.color = 0 ; Black Pen !p.font = -1 ; Use default fonts window, state.current_wno, xs = state.windowsize[0], ys = state.windowsize[1] tplot, tv_plot, window = state.current_wno widget_control, state.messw, set_val = 'Finished Plotting ' thm_ui_update_progress, state.cw, 'Finished Plotting ' Endelse thm_ui_update_history, state.cw, history_ext End ;set ylimits for the active dataset 'YLIMIT':Begin For j = 0, n_elements(tv_plot)-1 Do Begin widget_control, state.messw, set_val = 'Setting ylimits: '+tv_plot[j] thm_ui_update_progress, state.cw, 'Setting ylimits: '+tv_plot[j] get_data, tv_plot[j], data = d, dlim = dlim, lim = lim yr = [0.0d0, 0.0d0] yl = 0 If(is_struct(lim)) Then Begin If(tag_exist(lim, 'yrange')) Then yr = lim.yrange If(tag_exist(lim, 'ylog')) Then yl = lim.ylog Endif yr = strcompress(/remove_all, string(yr)) yl = strcompress(/remove_all, string(yl)) ylims = thm_ui_npar(tv_plot[j]+': '+['YMAX', 'YMIN', 'YLOG'], [yr[1], yr[0], yl]) If(ylims[0] Eq 'Cancelled') Then Begin widget_control, state.messw, set_val = 'Operation Cancelled' thm_ui_update_progress, state.cw, 'Operation Cancelled' Endif Else Begin yr = [double(ylims[1]), double(ylims[0])] yl = fix(ylims[2]) ylim, tv_plot[j], yr[0], yr[1], yl ystring = ylims[1]+', '+ylims[0]+', '+ylims[2] history_ext = 'ylim, '+''''+tv_plot[j]+''''+'. '+ystring thm_ui_update_history, state.cw, history_ext widget_control, state.messw, $ set_val = 'Finished setting ylimits: '+tv_plot[j] thm_ui_update_progress, state.cw, $ 'Finished setting ylimits: '+tv_plot[j] Endelse Endfor End ;set zlimits for the active dataset 'ZLIMIT':Begin For j = 0, n_elements(tv_plot)-1 Do Begin widget_control, state.messw, set_val = 'Setting zlimits: '+tv_plot[j] thm_ui_update_progress, state.cw, 'Setting zlimits: '+tv_plot[j] get_data, tv_plot[j], data = d, dlim = dlim, lim = lim zr = [0.0d0, 0.0d0] zl = 0 If(is_struct(lim)) Then Begin If(tag_exist(lim, 'zrange')) Then zr = lim.zrange If(tag_exist(lim, 'zlog')) Then zl = lim.zlog Endif zr = strcompress(/remove_all, string(zr)) zl = strcompress(/remove_all, string(zl)) zlims = thm_ui_npar(tv_plot[j]+': '+['ZMAX', 'ZMIN', 'ZLOG'], [zr[1], zr[0], zl]) If(zlims[0] Eq 'Cancelled') Then Begin widget_control, state.messw, set_val = 'Operation Cancelled' thm_ui_update_progress, state.cw, 'Operation Cancelled' Endif Else Begin zr = [double(zlims[1]), double(zlims[0])] zl = fix(zlims[2]) zlim, tv_plot[j], zr[0], zr[1], zl zstring = zlims[1]+', '+zlims[0]+', '+zlims[2] history_ext = 'zlim, '+''''+tv_plot[j]+''''+'. '+zstring thm_ui_update_history, state.cw, history_ext widget_control, state.messw, set_val = $ 'Finished setting zlimits: '+tv_plot[j] thm_ui_update_progress, state.cw, $ 'Finished setting zlimits: '+tv_plot[j] Endelse Endfor End ;Set spectrogram, or not 'SPEC':Begin ;Done for each active dataset For j = 0, n_elements(tv_plot)-1 Do Begin widget_control, state.messw, set_val = 'Setting Spectrogram for '+tv_plot[j] thm_ui_update_progress, state.cw, 'Setting Spectrogram '+tv_plot[j] get_data, tv_plot[j], data = d, dlim = dlim, lim = lim spec = 0 If(is_struct(lim)) Then Begin If(tag_exist(lim, 'spec')) Then spec = lim.spec Endif If(tag_exist(d, 'v') Or tag_exist(d, 'v2')) Then Begin ttl = tv_plot[j]+': Set to 1 for spectrogram, 0 for lines' spec = thm_ui_par(ttl, strcompress(string(spec))) If(spec[0] Eq 'Cancelled') Then Begin widget_control, state.messw, set_val = 'Operation Cancelled' thm_ui_update_progress, state.cw, 'Operation Cancelled' Endif Else Begin spec = fix(spec[0]) If(spec Ne 0 And spec Ne 1) Then Begin widget_control, state.messw, set_val = 'Invalid Input, Please Enter 0 or 1' thm_ui_update_progress, state.cw, 'Invalid Input, Please Enter 0 or 1' Endif Else Begin spec = fix(spec[0]) < 1 options, tv_plot[j], 'spec', spec history_ext = 'options,'+''''+tv_plot[j]+''''+', ''spec'', '+$ strcompress(string(spec)) thm_ui_update_history, state.cw, history_ext widget_control, state.messw, set_val = 'Finished Setting Spectrogram for '+tv_plot[j] thm_ui_update_progress, state.cw, 'Finished Setting Spectrogram '+tv_plot[j] Endelse Endelse Endif Else Begin widget_control, state.messw, set_val = 'Spectrogram is not an option for '+tv_plot[j] thm_ui_update_progress, state.cw, 'Spectrogram is not an option for '+tv_plot[j] thm_ui_update_history, state.cw, ';Spectrogram is not an option for '+tv_plot[j] Endelse Endfor End 'SETWNSZ':Begin widget_control, state.messw, set_val = 'Setting window size' thm_ui_update_progress, state.cw, 'Setting window size' wsz = strcompress(string(state.windowsize), /remove_all) wsz = thm_ui_npar(['XSIZE (pixels)', 'YSIZE (pixels)'], wsz) If(wsz[0] Eq 'Cancelled') Then Begin widget_control, state.messw, set_val = 'Operation Cancelled' thm_ui_update_progress, state.cw, 'Operation Cancelled' Endif Else Begin wsz = fix(wsz) If(wsz[0] Gt 0 And wsz[1] Gt[0]) Then Begin state.windowsize = wsz ptyp = state.ptyp If(ptyp Eq 'PNG') Then Begin set_plot, 'z' device, set_resolution = state.windowsize history_ext = $ thm_ui_multichoice_history('device, set_resolution = ', wsz) If(!version.os_family Eq 'Windows') Then set_plot, 'win' $ Else set_plot, 'x' Endif Else Begin wno = state.current_wno If(!version.os_family Eq 'Windows') Then set_plot, 'win' $ Else set_plot, 'x' history_ext = ['xsize_w ='+strcompress(string(state.windowsize[0])), $ 'ysize_w ='+strcompress(string(state.windowsize[1]))] Endelse thm_ui_update_history, state.cw, history_ext widget_control, state.messw, set_val = 'Finished setting window size' thm_ui_update_progress, state.cw, 'Finished setting window size' Endif Else Begin widget_control, state.messw, set_val = 'Invalid window size' thm_ui_update_progress, state.cw, 'Invalid window size' Endelse Endelse End 'SETWNNO':Begin widget_control, state.messw, set_val = 'Setting window number' thm_ui_update_progress, state.cw, 'Setting window number' wno0 = strcompress(string(state.current_wno), /remove_all) wno = thm_ui_npar('CURRENT WINDOW_NUMBER', '0') If(wno[0] Eq 'Cancelled') Then Begin widget_control, state.messw, set_val = 'Operation Cancelled' thm_ui_update_progress, state.cw, 'Operation Cancelled' Endif Else Begin wno = fix(wno[0]) If(wno Lt 32 And wno ge 0) Then Begin state.current_wno = wno If(!version.os_family Eq 'Windows') Then set_plot, 'win' $ Else set_plot, 'x' ; window, wno, xs = state.windowsize[0], ys = state.windowsize[1] history_ext = 'winno ='+strcompress(string(wno)) thm_ui_update_history, state.cw, history_ext widget_control, state.messw, set_val = 'Finished setting window number' thm_ui_update_progress, state.cw, 'Finished setting window number' Endif Else Begin widget_control, state.messw, set_val = 'Invalid window number, must be LT 32' thm_ui_update_progress, state.cw, 'Invalid window number, must be LT 32' Endelse Endelse End 'CREATEWIN':Begin ptyp = state.ptyp If(ptyp Eq 'PNG') Then Begin widget_control, state.messw, set_val = 'Window Not Used for PNG plots' thm_ui_update_progress, state.cw, 'Window Not Used for PNG plots' Endif Else Begin widget_control, state.messw, set_val = 'Creating New window' thm_ui_update_progress, state.cw, 'Creating New window' window, state.current_wno, xs = state.windowsize[0], ys = state.windowsize[1] history_ext = 'window,'+strcompress(string(state.current_wno))+$ ', xs ='+strcompress(string(state.windowsize[0]))+$ ', ys ='+strcompress(string(state.windowsize[1])) thm_ui_update_history, state.cw, history_ext widget_control, state.messw, set_val = 'Finished cresting new window' thm_ui_update_progress, state.cw, 'Finished cresting new window' Endelse End ;set tlimit, using the cursor 'TLIMIT':Begin widget_control, state.messw, set_val = 'Choosing time limits for plotting' thm_ui_update_progress, state.cw, 'Choosing time limits for plotting' widget_control, event.top, set_uval = state, /no_copy thm_ui_set_tlimits, event.top widget_control, event.top, get_uval = state, /no_copy state.st_time = t0_sav & state.en_time = t1_sav history_ext = 'tlimit, '+''''+time_string(t0_sav)+''''+$ ', '+''''+time_string(t1_sav)+'''' thm_ui_update_history, state.cw, history_ext widget_control, state.messw, set_val = 'Finished choosing time limits for plotting' thm_ui_update_progress, state.cw, 'Finished choosing time limits for plotting' End Endcase ;Reset the buttons For j = 0, n_elements(button_arr)-1 Do widget_control, button_arr[j], sensitive = 1 Endif Else Begin widget_control, state.cw, set_uval = wstate, /no_copy thm_ui_update_progress, state.cw, 'No Active Dataset, Please Load or Click on Data' widget_control, state.messw, set_val = 'No Active Dataset, Nothing happened' Endelse widget_control, event.top, set_uval = state, /no_copy Endelse Return End Pro thm_ui_call_tplot, gui_id @tplot_com common thm_ui_call_tplot_sav, plot_types, ptyp_sav If(n_elements(plot_types) Eq 0) Then plot_types = ['SCREEN', 'PNG'] ;build master widget tplot_master = widget_base(/row, $ title = 'THEMIS Science Software: Plot Menu', $ group_leader = gui_id, scr_xsize = 330) ;button widget buttons1 = widget_base(tplot_master, /col, /align_left, frame = 5) ;button widget buttons = widget_base(tplot_master, /col, /align_left, frame = 5) ;plot type list plottype = widget_base(buttons, /col, /align_left) plottype_label = widget_label(plottype, value = 'Plot Type') plottypelist = widget_list(plottype, value = plot_types, $ ysize = n_elements(plot_types), $ xsize = 10, uval = 'PTYPE') ;options widget optionsbase = widget_base(buttons1, /col, /align_left, frame = 5) ;tlimit button tlimitbut = widget_button(optionsbase, val = ' Set Time limits ', $ uval = 'TLIMIT', /align_left, scr_xsize = 130) ;ylimit button ylimbut = widget_button(optionsbase, val = ' Ylimit', $ uval = 'YLIMIT', /align_left, scr_xsize = 130) ;zlimit button zlimbut = widget_button(optionsbase, val = ' Zlimit', $ uval = 'ZLIMIT', /align_left, scr_xsize = 130) ;spectrogram button specbut = widget_button(optionsbase, val = ' Spectrogram', $ uval = 'SPEC', /align_left, scr_xsize = 130) ;window_size button winszbut = widget_button(optionsbase, val = ' Plot Window Size', $ uval = 'SETWNSZ', /align_left, scr_xsize = 130) ;window_number button winnobut = widget_button(optionsbase, val = ' Plot Window Number', $ uval = 'SETWNNO', /align_left, scr_xsize = 130) ;Create window button cwinbut = widget_button(optionsbase, val = ' Create New Window', $ uval = 'CREATEWIN', /align_left, scr_xsize = 130) ;plot button plotbut = widget_button(buttons, val = ' Draw Plot', uval = 'PLOT', $ /align_left, scr_xsize = 130) ;exit button exitbut = widget_button(buttons, val = ' Close ', uval = 'EXIT', $ /align_left, scr_xsize = 130) ;message widget messw = widget_text(buttons, value = '', xsize = 20, ysize = 5, /wrap, /scroll) If(n_elements(ptyp_sav) Eq 0) Then Begin ptyp_sav = 'SCREEN' thm_ui_update_history, gui_id, 'plot_type = '''+ptyp_sav+'''' ENdif widget_control, gui_id, get_uval = wstate, /no_copy wstate.plt_id = tplot_master t0 = wstate.st_time & t1 = wstate.en_time If(ptr_valid(wstate.active_vnames)) Then Begin tvn = *wstate.active_vnames Endif Else tvn = '*' widget_control, gui_id, set_uval = wstate, /no_copy ; Some things won't work unless there has already been a tplot called cwno = 0 If(is_struct(tplot_vars)) Then Begin If(is_struct(tplot_vars.options)) Then Begin ;set up a window here If(tplot_vars.options.window Ge 0) Then Begin cwno = tplot_vars.options.window Endif Endif Endif window, cwno tplot, tvn tplot_options, 'title', '' button_arr = [tlimitbut, ylimbut, zlimbut, specbut, $ winszbut, winnobut, cwinbut, plotbut, $ plottypelist, exitbut] ; State structure state = {tplot_master:tplot_master, $ st_time:t0, $ ;start_time, double en_time:t1, $ ;end_time windowsize:[640, 480], $ ;last stored window size current_wno:0, $ ;the current plotting window ptyp:ptyp_sav, $ ptyplist:plottypelist, $ messw:messw, $ ;the message widget button_arr:button_arr, $ ;an array with the buttons, to be greyed out cw:gui_id} ;the id of the calling widget widget_control, tplot_master, set_uval = state, /no_copy widget_control, tplot_master, /realize xmanager, 'thm_ui_call_tplot', tplot_master, /no_block Return End