def link(names, link_name, link_type='alt'): ''' Simply adds metadata to the tplot variables, specifying which other tplot variables contain other coordinates, typically positional information. Parameters: names: str or list of str The names to link link_name: str The tplot variable to link to the names link_type: str The relationship that link_name has to the names. Values can be lat, lon, alt, x, y, z, mso_x, mso_y, mso_z ''' link_type = link_type.lower() if not isinstance(names, list): names = [names] for i in names: if i not in data_quants.keys(): print(str(i) + " is not currently in pytplot.") return if isinstance(i, int): i = list(data_quants.keys())[i - 1] data_quants[data_quants[i]. name].attrs['plot_options']['links'][link_type] = link_name return
def tplot_save(names, filename=None): """ This function will save tplot variables into a single file by using the python "pickle" function. This file can then be "restored" using tplot_restore. This is useful if you want to end the pytplot session, but save all of your data/options. All variables and plot options can be read back into tplot with the "tplot_restore" command. Parameters: names : str/list A string or a list of strings of the tplot variables you would like saved. filename : str, optional The filename where you want to save the file. Returns: None Examples: >>> # Save a single tplot variable >>> import pytplot >>> x_data = [1,2,3,4,5] >>> y_data = [1,2,3,4,5] >>> pytplot.store_data("Variable1", data={'x':x_data, 'y':y_data}) >>> pytplot.ylim('Variable1', 2, 4) >>> pytplot.save('Variable1', filename='C:/temp/variable1.pytplot') """ if isinstance(names,int): names = list(data_quants.keys())[names-1] if not isinstance(names, list): names = [names] #Check that we have all available data for name in names: if isinstance(data_quants[name].data, list): for data_name in data_quants[name].data: if data_name not in names: names.append(data_name) #Pickle it up to_pickle =[] for name in names: if name not in data_quants.keys(): print("That name is currently not in pytplot") return to_pickle.append(data_quants[name]) num_quants = len(to_pickle) to_pickle = [num_quants] + to_pickle temp_tplot_opt_glob = tplot_opt_glob to_pickle.append(temp_tplot_opt_glob) if filename==None: filename='var_'+'-'.join(names)+'.pytplot' pickle.dump(to_pickle, open(filename, "wb")) return
def del_data(name=None): """ This function will delete tplot variables that are already stored in memory. Parameters: name : str Name of the tplot variable to be deleted. If no name is provided, then all tplot variables will be deleted. Returns: None Examples: >>> # Delete Variable 1 >>> import pytplot >>> pytplot.del_data("Varaible1") """ if name is None: tplot_names = list(data_quants.keys()) for i in tplot_names: del data_quants[i] return if not isinstance(name, list): name = [name] entries = [] ### for i in name: if ('?' in i) or ('*' in i): for j in data_quants.keys(): var_verif = fnmatch.fnmatch(data_quants[j].name, i) if var_verif == 1: entries.append(data_quants[j].name) else: continue for key in entries: if key in data_quants: del data_quants[key] ### elif i not in data_quants.keys(): print(str(i) + " is currently not in pytplot.") return else: temp_data_quants = data_quants[i] str_name = temp_data_quants.name del data_quants[str_name] return
def get_ylimits(name, trg=None): """ This function will get extract the y-limits from the Tplot Variables stored in memory. Parameters: name : str Name of the tplot variable trg : list, optional The time range that you would like to look in Returns: ymin : float The minimum value of y ymax : float The maximum value of y Examples: >>> # Retrieve the y-limits from Variable 1 >>> import pytplot >>> x_data = [1,2,3,4,5] >>> y_data = [1,2,3,4,5] >>> pytplot.store_data("Variable1", data={'x':x_data, 'y':y_data}) >>> y1, y2 = pytplot.get_ylimits("Variable1") """ if isinstance(name, int): name = list(data_quants.keys())[name - 1] if not isinstance(name, list): name = [name] name_num = len(name) ymin = None ymax = None for i in range(name_num): if name[i] not in data_quants.keys(): print(str(name[i]) + " is currently not in pytplot.") return y = data_quants[name[i]] # Slice the data around a time range if trg is not None: y = y.sel(time=slice(trg[0], trg[1])) ymin = y.min(skipna=True) ymax = y.max(skipna=False) return ymin, ymax
def get_timespan(name): """ This function will get extract the time span from the Tplot Variables stored in memory. Parameters: name : str Name of the tplot variable Returns: time_begin : float The beginning of the time series time_end : float The end of the time series Examples: >>> # Retrieve the time span from Variable 1 >>> import pytplot >>> x_data = [1,2,3,4,5] >>> y_data = [1,2,3,4,5] >>> pytplot.store_data("Variable1", data={'x':x_data, 'y':y_data}) >>> time1, time2 = pytplot.get_timespan("Variable1") """ if name not in data_quants.keys(): print("That name is currently not in pytplot") return print("Start Time: " + tplot_utilities.int_to_str(data_quants[name].trange[0])) print("End Time: " + tplot_utilities.int_to_str(data_quants[name].trange[1])) return (data_quants[name].trange[0], data_quants[name].trange[1])
def get_data(name): """ This function will get extract the data from the Tplot Variables stored in memory. Parameters: name : str Name of the tplot variable Returns: time_val : pandas dataframe index data_val : list Examples: >>> # Retrieve the data from Variable 1 >>> import pytplot >>> x_data = [1,2,3,4,5] >>> y_data = [1,2,3,4,5] >>> pytplot.store_data("Variable1", data={'x':x_data, 'y':y_data}) >>> time, data = pytplot.get_data("Variable1") """ global data_quants if name not in data_quants.keys(): print("That name is currently not in pytplot") return temp_data_quant = data_quants[name] data_val = temp_data_quant.data.values time_val = temp_data_quant.data.index return (time_val, data_val)
def get_timespan(name): """ This function extracts the time span from the Tplot Variables stored in memory. Parameters: name : str Name of the tplot variable Returns: time_begin : float The beginning of the time series time_end : float The end of the time series Examples: >>> # Retrieve the time span from Variable 1 >>> import pytplot >>> x_data = [1,2,3,4,5] >>> y_data = [1,2,3,4,5] >>> pytplot.store_data("Variable1", data={'x':x_data, 'y':y_data}) >>> time1, time2 = pytplot.get_timespan("Variable1") """ if name not in data_quants.keys(): print("That name is currently not in pytplot") return return data_quants[name].attrs['plot_options']['trange'][0], data_quants[ name].attrs['plot_options']['trange'][1]
def zlim(name, min, max): """ This function will set the z axis range displayed for a specific tplot variable. This is only used for spec plots, where the z axis represents the magnitude of the values in each bin. Parameters: name : str The name of the tplot variable that you wish to set z limits for. min : flt The start of the z axis. max : flt The end of the z axis. Returns: None Examples: >>> # Change the z range of Variable1 >>> import pytplot >>> x_data = [1,2,3] >>> y_data = [ [1,2,3] , [4,5,6], [7,8,9] ] >>> v_data = [1,2,3] >>> pytplot.store_data("Variable3", data={'x':x_data, 'y':y_data, 'v':v_data}) >>> pytplot.zlim('Variable1', 2, 3) """ if name not in data_quants.keys(): print("That name is currently not in pytplot.") return temp_data_quant = data_quants[name] temp_data_quant.zaxis_opt['z_range'] = [min, max] return
def ylim(name, min, max): """ This function will set the y axis range displayed for a specific tplot variable. Parameters: name : str The name of the tplot variable that you wish to set y limits for. min : flt The start of the y axis. max : flt The end of the y axis. Returns: None Examples: >>> # Change the y range of Variable1 >>> import pytplot >>> x_data = [1,2,3,4,5] >>> y_data = [1,2,3,4,5] >>> pytplot.store_data("Variable1", data={'x':x_data, 'y':y_data}) >>> pytplot.ylim('Variable1', 2, 4) """ if name not in data_quants.keys(): print("That name is currently not in pytplot.") return temp_data_quant = data_quants[name] temp_data_quant.yaxis_opt['y_range'] = [min, max] return
def get_data(name, xarray=False): """ This function extracts the data from the tplot Variables stored in memory. Parameters: name : str Name of the tplot variable Returns: tuple of data/dimensions stored in pytplot time_val : numpy array of seconds since 1970 data_val : n-dimensional array of data spec_bins_val (if exists) : spectral bins if the plot is a spectrogram v1_val (if exists) : numpy array of v1 dimension coordinates v2_val {if exists} : numpy array of v2 dimension coordinates v3_val (if exists) : numpy array of v3 dimension coordinates Examples: >>> # Retrieve the data from Variable 1 >>> import pytplot >>> x_data = [1,2,3,4,5] >>> y_data = [1,2,3,4,5] >>> pytplot.store_data("Variable1", data={'x':x_data, 'y':y_data}) >>> time, data = pytplot.get_data("Variable1") """ global data_quants if name not in data_quants.keys(): print("That name is currently not in pytplot") return temp_data_quant = data_quants[name] if xarray: return temp_data_quant if 'v1' in temp_data_quant.coords.keys( ) and 'v2' in temp_data_quant.coords.keys( ) and 'v3' in temp_data_quant.coords.keys(): return (temp_data_quant.time.values, temp_data_quant.data, temp_data_quant.coords['v1'].values, temp_data_quant.coords['v2'].values, temp_data_quant.coords['v3'].values) elif 'v1' in temp_data_quant.coords.keys( ) and 'v2' in temp_data_quant.coords.keys(): return (temp_data_quant.time.values, temp_data_quant.data, temp_data_quant.coords['v1'].values, temp_data_quant.coords['v2'].values) elif 'v1' in temp_data_quant.coords.keys(): return (temp_data_quant.time.values, temp_data_quant.data, temp_data_quant.coords['v1'].values) elif 'v' in temp_data_quant.coords.keys(): return (temp_data_quant.time.values, temp_data_quant.data, temp_data_quant.coords['v'].values) elif 'spec_bins' in temp_data_quant.coords.keys(): return (temp_data_quant.time.values, temp_data_quant.data, temp_data_quant.coords['spec_bins'].values) return (temp_data_quant.time.values, temp_data_quant.data)
def link(names, link_name, link_type='alt'): link_type = link_type.lower() if not isinstance(names, list): names = [names] for i in names: if i not in data_quants.keys(): print(str(i) + " is currently not in pytplot.") return if isinstance(i, int): i = list(data_quants.keys())[i - 1] data_quants[i].link_to_tvar(link_type, link_name) return
def del_data(name=None): """ This function will delete tplot variables that are already stored in memory. Parameters: name : str Name of the tplot variable to be deleted. If no name is provided, then all tplot variables will be deleted. Returns: None Examples: >>> # Delete Variable 1 >>> import pytplot >>> pytplot.del_data("Varaible1") """ if name is None: tplot_names = list(data_quants.keys()) for i in tplot_names: del data_quants[i] return if not isinstance(name, list): name = [name] for i in name: if i not in data_quants.keys(): print(str(i) + " is currently not in pytplot.") return temp_data_quants = data_quants[i] str_name = temp_data_quants.name del data_quants[str_name] return
def get_ylimits(name, trg = None): """ This function will get extract the y limites from the Tplot Variables stored in memory. Parameters: name : str Name of the tplot variable trg : list, optional The time range that you would like to look in Returns: ymin : float The minimum value of y ymax : float The maximum value of y Examples: >>> # Retrieve the y limits from Variable 1 >>> import pytplot >>> x_data = [1,2,3,4,5] >>> y_data = [1,2,3,4,5] >>> pytplot.store_data("Variable1", data={'x':x_data, 'y':y_data}) >>> y1, y2 = pytplot.get_ylimits("Variable1") """ if isinstance(name,int): name = list(data_quants.keys())[name-1] if not isinstance(name, list): name = [name] if not isinstance(name, list): name = [name] name_num = len(name) ymin = None ymax = None for i in range(name_num): if name[i] not in data_quants.keys(): print(str(name[i]) + " is currently not in pytplot.") return temp_data_quant = data_quants[name[i]] yother = temp_data_quant.data if trg is not None: for column_name in yother.columns: y = yother[column_name] trunc_tempt_data_quant = y.truncate(before = trg[0], after = trg[1]) loc_min = trunc_tempt_data_quant.min(skipna=True) loc_max = trunc_tempt_data_quant.max(skipna=True) if (ymin is None) or (loc_min < ymin): ymin = loc_min if (ymax is None) or (loc_max > ymax): ymax = loc_max else: for column_name in yother.columns: y = yother[column_name] loc_min = y.min(skipna=True) loc_max = y.max(skipna=False) if (ymin is None) or (loc_min < ymin): ymin = loc_min if (ymax is None) or (loc_max > ymax): ymax = loc_max print("Y Minimum: " + str(ymin)) print("Y Maximum: " + str(ymax)) return(ymin, ymax)
def timebar(t, varname=None, databar=False, delete=False, color='black', thick=1, dash=False): """ This function will add a vertical bar to all time series plots. This is useful if you want to bring attention to a specific time. Parameters: t : flt/list The time in seconds since Jan 01 1970 to place the vertical bar. If a list of numbers are supplied, multiple bars will be created. If "databar" is set, then "t" becomes the point on the y axis to place a horizontal bar. varname : str/list, optional The variable(s) to add the vertical bar to. If not set, the default is to add it to all current plots. databar : bool, optional This will turn the timebar into a horizontal data bar. If this is set True, then variable "t" becomes the point on the y axis to place a horizontal bar. delete : bool, optional If set to True, at lease one varname must be supplied. The timebar at point "t" for variable "varname" will be removed. color : str The color of the bar thick : int The thickness of the bar dash : bool If set to True, the bar is dashed rather than solid Returns: None Examples: >>> # Place a green time bar at 2017-07-17 00:00:00 >>> import pytplot >>> pytplot.timebar(1500249600, color='green') >>> # Place a dashed data bar at 5500 on the y axis >>> pytplot.timebar(5500, dashed=True, databar=True) >>> Place 3 magenta time bars of thickness 5 at [2015-12-26 05:20:01, 2015-12-26 08:06:40, 2015-12-26 08:53:19] for variable 'sgx' plot >>> pytplot.timebar([1451107201,1451117200,1451119999],'sgx',color='m',thick=5) """ # make sure t entered is a list if not isinstance(t, list): t = [t] # if entries in list not numerical, run str_to_int if not isinstance(t[0], (int, float, complex)): t1 = [] for time in t: t1.append(tplot_utilities.str_to_int(time)) t = t1 dim = 'height' if databar is True: dim = 'width' dash_pattern = 'solid' if dash is True: dash_pattern = 'dashed' if delete is True: tplot_utilities.timebar_delete(t, varname, dim) return #if no varname specified, add timebars to every plot if varname is None: num_bars = len(t) for i in range(num_bars): tbar = {} tbar['location'] = t[i] tbar['dimension'] = dim tbar['line_color'] = pytplot.tplot_utilities.rgb_color(color)[0] tbar['line_width'] = thick tbar['line_dash'] = dash_pattern for name in data_quants: temp_data_quants = data_quants[name] temp_data_quants.attrs['plot_options']['time_bar'].append(tbar) #if varname specified else: if not isinstance(varname, list): varname = [varname] for j in varname: if j not in data_quants.keys(): print(str(j) + "is currently not in pytplot") else: num_bars = len(t) for i in range(num_bars): tbar = {} tbar['location'] = t[i] tbar['dimension'] = dim tbar['line_color'] = pytplot.tplot_utilities.rgb_color( color)[0] tbar['line_width'] = thick tbar['line_dash'] = dash_pattern temp_data_quants = data_quants[j] temp_data_quants.attrs['plot_options']['time_bar'].append( tbar) return
def options(name, option=None, value=None, opt_dict=None): """ This function allows the user to set a large variety of options for individual plots. Parameters: name : str Name or number of the tplot variable option : str The name of the option. See section below. value : str/int/float/list The value of the option. See section below. dict : dict This can be a dictionary of option:value pairs. Option and value will not be needed if this dictionary item is supplied. Options: =================== ========== ===== Options Value type Notes =================== ========== ===== Color str/list red, green, blue, etc. Also takes in RGB tuples, i.e. (0,255,0) for green Colormap str/list https://matplotlib.org/examples/color/colormaps_reference.html. Spec int 1 sets the Tplot Variable to spectrogram mode, 0 reverts. Alt int 1 sets the Tplot Variable to altitude plot mode, 0 reverts. Map int 1 sets the Tplot Variable to latitude/longitude mode, 0 reverts. link list Allows a user to reference one tplot variable to another. ylog int 1 sets the y axis to log scale, 0 reverts. zlog int 1 sets the z axis to log scale, 0 reverts (spectrograms only). legend_names list A list of strings that will be used to identify the lines. xlog_slice bool Sets x axis on slice plot to log scale if True. ylog bool Set y axis on main plot window to log scale if True. ylog_slice bool Sets y axis on slice plot to log scale if True. zlog bool Sets z axis on main plot window to log scale if True. line_style str scatter (to make scatter plots), or solid_line, dot, dash, dash_dot, dash_dot_dot_dot, long_dash. char_size int Defines character size for plot labels, etc. name str The title of the plot. panel_size flt Number between (0,1], representing the percent size of the plot. basemap str Full path and name of a background image for "Map" plots. alpha flt Number between [0,1], gives the transparancy of the plot lines. thick flt Sets plot line width. yrange flt list Two numbers that give the y axis range of the plot. zrange flt list Two numbers that give the z axis range of the plot. xrange_slice flt list Two numbers that give the x axis range of spectrogram slicing plots. yrange_slice flt list Two numbers that give the y axis range of spectrogram slicing plots. ytitle str Title shown on the y axis. ztitle str Title shown on the z axis. Spec plots only. ysubtitle str Subtitle shown on the y axis. zsubtitle str Subtitle shown on the z axis. Spec plots only. plotter str Allows a user to implement their own plotting script in place of the ones herein. crosshair_x str Title for x-axis crosshair. crosshair_y str Title for y-axis crosshair. crosshair_z str Title for z-axis crosshair. static str Datetime string that gives desired time to plot y and z values from a spec plot. static_tavg str Datetime string that gives desired time-averaged y and z values to plot from a spec plot. t_average int Seconds around which the cursor is averaged when hovering over spectrogram plots. 'spec_plot_dim' int If variable two dimensions, this sets which dimension the variable will have on on the y axis. All other dimensions are summed into this one. =================== ========== ===== Returns: None Examples: >>> # Change the y range of Variable1 >>> import pytplot >>> x_data = [1,2,3,4,5] >>> y_data = [1,2,3,4,5] >>> pytplot.store_data("Variable1", data={'x':x_data, 'y':y_data}) >>> pytplot.options('Variable1', 'yrange', [2,4]) >>> # Change Variable1 to use a log scale >>> pytplot.options('Variable1', 'ylog', 1) """ if isinstance(name, int): name = list(pytplot.data_quants.keys())[name] if opt_dict is None: opt_dict = {option: value} else: if not isinstance(opt_dict, dict): print("dict must be a dictionary object. Returning.") return if not isinstance(name, list): name = [name] for i in name: for option, value in opt_dict.items(): # Lower case option for consistency option = option.lower() if i not in data_quants.keys(): print(str(i) + " is currently not in pytplot.") return if option == 'color': if isinstance(value, list): data_quants[i].attrs['plot_options']['extras'][ 'line_color'] = value else: data_quants[i].attrs['plot_options']['extras'][ 'line_color'] = [value] if option == 'link': if isinstance(value, list): pytplot.link(i, value[1], value[0]) if option == 'colormap': if isinstance(value, list): data_quants[i].attrs['plot_options']['extras'][ 'colormap'] = value else: data_quants[i].attrs['plot_options']['extras'][ 'colormap'] = [value] if option == 'spec': _reset_plots(i) if value: if 'spec_bins' not in data_quants[i].coords: print( f"{i} does not contain coordinates for spectrogram plotting. Continuing..." ) else: data_quants[i].attrs['plot_options']['extras'][ 'spec'] = value data_quants[i].attrs['plot_options']['yaxis_opt'][ 'y_range'] = utilities.get_y_range(data_quants[i]) else: data_quants[i].attrs['plot_options']['extras'][ 'spec'] = value data_quants[i].attrs['plot_options']['yaxis_opt'][ 'y_range'] = utilities.get_y_range(data_quants[i]) if option == 'alt': _reset_plots(i) data_quants[i].attrs['plot_options']['extras']['alt'] = value if option == 'map': _reset_plots(i) data_quants[i].attrs['plot_options']['extras']['map'] = value if option == 'legend_names': data_quants[i].attrs['plot_options']['yaxis_opt'][ 'legend_names'] = value if option == 'xlog_slice': if value: data_quants[i].attrs['plot_options']['slice_xaxis_opt'][ 'xi_axis_type'] = 'log' else: data_quants[i].attrs['plot_options']['slice_xaxis_opt'][ 'xi_axis_type'] = 'linear' if option == 'ylog': negflag = 0 # _ylog_check(data_quants, value, i) if negflag == 0 and value: data_quants[i].attrs['plot_options']['yaxis_opt'][ 'y_axis_type'] = 'log' else: data_quants[i].attrs['plot_options']['yaxis_opt'][ 'y_axis_type'] = 'linear' if option == 'ylog_slice': if value: data_quants[i].attrs['plot_options']['slice_yaxis_opt'][ 'yi_axis_type'] = 'log' else: data_quants[i].attrs['plot_options']['slice_yaxis_opt'][ 'yi_axis_type'] = 'linear' if option == 'zlog': negflag = _zlog_check(data_quants, value, i) if negflag == 0: data_quants[i].attrs['plot_options']['zaxis_opt'][ 'z_axis_type'] = 'log' else: data_quants[i].attrs['plot_options']['zaxis_opt'][ 'z_axis_type'] = 'linear' if option == 'nodata': data_quants[i].attrs['plot_options']['line_opt'][ 'visible'] = value if option == 'line_style': to_be = [] if value == 0 or value == 'solid_line': to_be = [] elif value == 1 or value == 'dot': to_be = [2, 4] elif value == 2 or value == 'dash': to_be = [6] elif value == 3 or value == 'dash_dot': to_be = [6, 4, 2, 4] elif value == 4 or value == 'dash_dot_dot_dot': to_be = [6, 4, 2, 4, 2, 4, 2, 4] elif value == 5 or value == 'long_dash': to_be = [10] else: to_be = value data_quants[i].attrs['plot_options']['line_opt'][ 'line_style'] = to_be if (value == 6 or value == 'none'): data_quants[i].attrs['plot_options']['line_opt'][ 'visible'] = False if option == 'char_size': data_quants[i].attrs['plot_options']['extras'][ 'char_size'] = value if option == 'name': data_quants[i].attrs['plot_options']['line_opt'][ 'name'] = value if option == "panel_size": if value > 1 or value <= 0: print("Invalid value. Should be (0, 1]") return data_quants[i].attrs['plot_options']['extras'][ 'panel_size'] = value if option == 'basemap': data_quants[i].attrs['plot_options']['extras'][ 'basemap'] = value if option == 'alpha': if value > 1 or value < 0: print("Invalid value. Should be [0, 1]") return data_quants[i].attrs['plot_options']['extras']['alpha'] = value if option == 'thick': data_quants[i].attrs['plot_options']['line_opt'][ 'line_width'] = value if option == 'yrange' or option == 'y_range': data_quants[i].attrs['plot_options']['yaxis_opt'][ 'y_range'] = [value[0], value[1]] if option == 'zrange' or option == 'z_range': data_quants[i].attrs['plot_options']['zaxis_opt'][ 'z_range'] = [value[0], value[1]] if option == 'xrange_slice': data_quants[i].attrs['plot_options']['slice_xaxis_opt'][ 'xi_range'] = [value[0], value[1]] if option == 'yrange_slice': data_quants[i].attrs['plot_options']['slice_yaxis_opt'][ 'yi_range'] = [value[0], value[1]] if option == 'xtitle': data_quants[i].attrs['plot_options']['xaxis_opt'][ 'axis_label'] = value if option == 'ytitle': data_quants[i].attrs['plot_options']['yaxis_opt'][ 'axis_label'] = value if option == 'ztitle': data_quants[i].attrs['plot_options']['zaxis_opt'][ 'axis_label'] = value if option == 'xsubtitle': data_quants[i].attrs['plot_options']['xaxis_opt'][ 'axis_subtitle'] = value if option == 'ysubtitle': data_quants[i].attrs['plot_options']['yaxis_opt'][ 'axis_subtitle'] = value if option == 'zsubtitle': data_quants[i].attrs['plot_options']['zaxis_opt'][ 'axis_subtitle'] = value if option == 'ybar': data_quants[i].attrs['plot_options']['extras']['ybar'] = value if option == 'ybar_color': data_quants[i].attrs['plot_options']['extras']['ybar'] = value if option == 'ybar_size': data_quants[i].attrs['plot_options']['extras']['ysize'] = value if option == 'plotter': _reset_plots(i) data_quants[i].attrs['plot_options']['extras'][ 'plotter'] = value if option == 'crosshair_x': data_quants[i].attrs['plot_options']['xaxis_opt'][ 'crosshair'] = value if option == 'crosshair_y': data_quants[i].attrs['plot_options']['yaxis_opt'][ 'crosshair'] = value if option == 'crosshair_z': data_quants[i].attrs['plot_options']['zaxis_opt'][ 'crosshair'] = value if option == 'static': data_quants[i].attrs['plot_options']['extras'][ 'static'] = value if option == 'static_tavg': data_quants[i].attrs['plot_options']['extras'][ 'static_tavg'] = [value[0], value[1]] if option == 't_average': data_quants[i].attrs['plot_options']['extras'][ 't_average'] = value if option == 'spec_plot_dim': attr_dict = deepcopy(data_quants[i].attrs) data_dict = {} data_dict['x'] = data_quants[i].coords['time'].values data_values = data_quants[i].values if len(data_values.shape) <= 2: pass else: data_dict['y'] = np.swapaxes(data_values, 2, value) for c in data_quants[i].coords: if c == 'time' or c == 'spec_bins': continue data_dict[c] = data_quants[i].coords[c].values v2_values = data_quants[i].coords["v2"].values data_dict['v2'] = data_dict['v' + str(value)] data_dict['v' + str(value)] = v2_values pytplot.store_data(i, data=data_dict) data_quants[i].attrs = attr_dict data_quants[i].attrs['plot_options']['yaxis_opt'][ 'y_range'] = utilities.get_y_range(data_quants[i]) return
def cdf_to_tplot(filenames, varformat=None, get_support_data=False, prefix='', suffix='', plot=False, merge=False): """ This function will automatically create tplot variables from CDF files. .. note:: Variables must have an attribute named "VAR_TYPE". If the attribute entry is "data" (or "support_data"), then they will be added as tplot variables. Additionally, data variables should have attributes named "DEPEND_TIME" or "DEPEND_0" that describes which variable is x axis. If the data is 2D, then an attribute "DEPEND_1" must describe which variable contains the secondary axis. Parameters: filenames : str/list of str The file names and full paths of CDF files. varformat : str The file variable formats to load into tplot. Wildcard character "*" is accepted. By default, all variables are loaded in. get_support_data: bool Data with an attribute "VAR_TYPE" with a value of "support_data" will be loaded into tplot. By default, only loads in data with a "VAR_TYPE" attribute of "data". prefix: str The tplot variable names will be given this prefix. By default, no prefix is added. suffix: str The tplot variable names will be given this suffix. By default, no suffix is added. plot: bool The data is plotted immediately after being generated. All tplot variables generated from this function will be on the same plot. merge: bool If True, then data from different cdf files will be merged into a single pytplot variable. Returns: List of tplot variables created. """ stored_variables = [] global data_quants if isinstance(filenames, str): filenames = [filenames] elif isinstance(filenames, list): filenames = filenames else: print("Invalid filenames input.") return stored_variables var_type = ['data'] if varformat == None: varformat = ".*" if get_support_data: var_type.append('support_data') try: varformat = varformat.replace("*", ".*") var_regex = re.compile(varformat) except: print("Error reading the varformat.") return for filename in filenames: cdf_file = cdflib.CDF(filename) cdf_info = cdf_file.cdf_info() all_cdf_variables = cdf_info['rVariables'] + cdf_info['zVariables'] # Find the data variables for var in all_cdf_variables: if not re.match(var_regex, var): continue var_atts = cdf_file.varattsget(var) if 'VAR_TYPE' not in var_atts: continue if var_atts['VAR_TYPE'] in var_type: var_properties = cdf_file.varinq(var) if "DEPEND_TIME" in var_atts: x_axis_var = var_atts["DEPEND_TIME"] elif "DEPEND_0" in var_atts: x_axis_var = var_atts["DEPEND_0"] else: print("Cannot find x axis.") print("No attribute named DEPEND_TIME or DEPEND_0 in variable " + var) continue data_type_description = cdf_file.varinq(x_axis_var)['Data_Type_Description'] # Find data name and if it is already in stored variables var_name = prefix + var + suffix to_merge = False if (var_name in data_quants.keys()) and (merge == True): prev_data_quant = data_quants[var_name].data to_merge = True xdata = cdf_file.varget(x_axis_var) if ('CDF_TIME' in data_type_description) or ('CDF_EPOCH' in data_type_description): xdata = cdflib.cdfepoch.unixtime(xdata) ydata = cdf_file.varget(var) if ydata is None: continue if "FILLVAL" in var_atts: if (var_properties['Data_Type_Description'] == 'CDF_FLOAT' or var_properties['Data_Type_Description'] == 'CDF_REAL4' or var_properties['Data_Type_Description'] == 'CDF_DOUBLE' or var_properties['Data_Type_Description'] == 'CDF_REAL8'): if ydata[ydata == var_atts["FILLVAL"]].size != 0: ydata[ydata == var_atts["FILLVAL"]] = np.nan tplot_data = {'x': xdata, 'y': ydata} depend_1 = None depend_2 = None if "DEPEND_1" in var_atts: if var_atts["DEPEND_1"] in all_cdf_variables: depend_1 = cdf_file.varget(var_atts["DEPEND_1"]) if "DEPEND_2" in var_atts: if var_atts["DEPEND_2"] in all_cdf_variables: depend_2 = cdf_file.varget(var_atts["DEPEND_2"]) if (depend_1 is not None) and (depend_2 is not None): tplot_data['v1'] = depend_1 tplot_data['v2'] = depend_2 elif depend_1 is not None: tplot_data['v'] = depend_1 elif depend_2 is not None: tplot_data['v'] = depend_2 store_data(var_name, data=tplot_data) if var_name not in stored_variables: stored_variables.append(var_name) display_type = var_atts.get("DISPLAY_TYPE", "time_series") scale_type = var_atts.get("SCALE_TYP", "linear") if display_type == "spectrogram": options(var, 'spec', 1) if scale_type == 'log': options(var, 'ylog', 1) if to_merge: cur_data_quant = data_quants[var_name].data merged_data = [prev_data_quant, cur_data_quant] data_quants[var_name].data = pd.concat(merged_data) cdf_file.close() if hasattr(cdf_file, "close") else None if plot: tplot(stored_variables) return stored_variables
def timebar(t, varname=None, databar=False, delete=False, color='black', thick=1, dash=False): """ This function will add a vertical bar to all time series plots. This is useful if you want to bring attention to a specific time. Parameters: t : flt/list The time in seconds since Jan 01 1970 to place the vertical bar. If a list of numbers are supplied, multiple bars will be created. If "databar" is set, then "t" becomes the point on the y axis to place a horizontal bar. varname : str/list, optional The variable(s) to add the vertical bar to. If not set, the default is to add it to all current plots. databar : bool, optional This will turn the timebar into a horizontal data bar. If this is set True, then variable "t" becomes the point on the y axis to place a horizontal bar. delete : bool, optional If set to True, at lease one varname must be supplied. The timebar at point "t" for variable "varname" will be removed. color : str The color of the bar thick : int The thickness of the bar dash : bool If set to True, the bar is dashed rather than solid Returns: None Examples: >>> # Place a green time bar at 2017-07-17 00:00:00 >>> import pytplot >>> pytplot.timebar(1500249600, color='grean) >>> # Place a dashed data bar at 5500 on the y axis >>> pytplot.timebar(5500, dashed=True, databar=True) """ if not isinstance(t, (int, float, complex)): t = tplot_utilities.str_to_int(t) dim = 'height' if databar is True: dim = 'width' dash_pattern = 'solid' if dash is True: dash_pattern = 'dashed' # Convert single value to a list so we don't have to write code to deal with # both single values and lists. if not isinstance(t, list): t = [t] # Convert values to seconds by multiplying by 1000 if databar is False: num_bars = len(t) for j in range(num_bars): t[j] *= 1000 if delete is True: tplot_utilities.timebar_delete(t, varname, dim) return # If no varname specified, add timebars to every plot if varname is None: num_bars = len(t) for i in range(num_bars): tbar = {} tbar['location'] = t[i] tbar['dimension'] = dim tbar['line_color'] = color tbar['line_width'] = thick tbar['line_dash'] = dash_pattern for name in data_quants: temp_data_quants = data_quants[name] temp_data_quants.time_bar.append(tbar) else: if not isinstance(varname, list): varname = [varname] for j in varname: if j not in data_quants.keys(): print(str(j) + "is currently not in pytplot") else: num_bars = len(t) for i in range(num_bars): tbar = Span(location=t[i], dimension=dim, line_color=color, line_width=thick, line_dash=dash_pattern) temp_data_quants = data_quants[j] temp_data_quants.time_bar.append(tbar) return
def options(name, option, value): """ This function allows the user to set a large variety of options for individual plots. Parameters: name : str Name of the tplot variable option : str The name of the option. See section below value : str/int/float/list The value of the option. See section below. Options: =================== ========== ===== Options Value type Notes =================== ========== ===== Color str/list Red, Orange, Yellow, Green, Blue, etc. Colormap str/list https://matplotlib.org/examples/color/colormaps_reference.html. Spec int 1 sets the Tplot Variable to spectrogram mode, 0 reverts. Alt int 1 sets the Tplot Variable to altitude plot mode, 0 reverts. Map int 1 sets the Tplot Variable to latitude/longitude mode, 0 reverts. link list Allows a user to reference one tplot variable to another. ylog int 1 sets the y axis to log scale, 0 reverts. zlog int 1 sets the z axis to log scale, 0 reverts (spectrograms only). legend_names list A list of strings that will be used to identify the lines. xlog_interactive bool Sets x axis on interactive plot to log scale if True. ylog bool Set y axis on main plot window to log scale if True. ylog_interactive bool Sets y axis on interactive plot to log scale if True. zlog bool Sets z axis on main plot window to log scale if True. line_style str scatter (to make scatter plots), or solid_line, dot, dash, dash_dot, dash_dot_dot_dot, long_dash. char_size int Defines character size for plot labels, etc. name str The title of the plot. panel_size flt Number between (0,1], representing the percent size of the plot. basemap str Full path and name of a background image for "Map" plots. alpha flt Number between [0,1], gives the transparancy of the plot lines. thick flt Sets plot line width. yrange flt list Two numbers that give the y axis range of the plot. zrange flt list Two numbers that give the z axis range of the plot. xrange_interactive flt list Two numberes that give the x axis range of interactive plots. yrange_interactive flt list Two numberes that give the y axis range of interactive plots. ytitle str Title shown on the y axis. ztitle str Title shown on the z axis. Spec plots only. plotter str Allows a user to implement their own plotting script in place of the ones herein. crosshair_x str Title for x-axis crosshair. crosshair_y str Title for y-axis crosshair. crosshair_z str Title for z-axis crosshair. static str Datetime string that gives desired time to plot y and z values from a spec plot. static_tavg str Datetime string that gives desired time-averaged y and z values to plot from a spec plot. t_average int Seconds around which the cursor is averaged when hovering over spectrogram plots. =================== ========== ===== Returns: None Examples: >>> # Change the y range of Variable1 >>> import pytplot >>> x_data = [1,2,3,4,5] >>> y_data = [1,2,3,4,5] >>> pytplot.store_data("Variable1", data={'x':x_data, 'y':y_data}) >>> pytplot.options('Variable1', 'yrange', [2,4]) >>> # Change Variable1 to use a log scale >>> pytplot.options('Variable1', 'ylog', 1) """ if not isinstance(name, list): name = [name] option = option.lower() for i in name: if i not in data_quants.keys(): print(str(i) + " is currently not in pytplot.") return if option == 'color': if isinstance(value, list): data_quants[i].attrs['plot_options']['extras']['line_color'] = value else: data_quants[i].attrs['plot_options']['extras']['line_color'] = [value] if option == 'link': if isinstance(value, list): pytplot.link(i, value[1], value[0]) if option == 'colormap': if isinstance(value, list): data_quants[i].attrs['plot_options']['extras']['colormap'] = value else: data_quants[i].attrs['plot_options']['extras']['colormap'] = [value] if option == 'spec': _reset_plots(i) data_quants[i].attrs['plot_options']['extras']['spec'] = value if option == 'alt': _reset_plots(i) data_quants[i].attrs['plot_options']['extras']['alt'] = value if option == 'map': _reset_plots(i) data_quants[i].attrs['plot_options']['extras']['map'] = value if option == 'legend_names': data_quants[i].attrs['plot_options']['yaxis_opt']['legend_names'] = value if option == 'xlog_interactive': if value: data_quants[i].attrs['plot_options']['interactive_xaxis_opt']['xi_axis_type'] = 'log' else: data_quants[i].attrs['plot_options']['interactive_xaxis_opt']['xi_axis_type'] = 'linear' if option == 'ylog': negflag = 0 # _ylog_check(data_quants, value, i) if negflag == 0 and value: data_quants[i].attrs['plot_options']['yaxis_opt']['y_axis_type'] = 'log' else: data_quants[i].attrs['plot_options']['yaxis_opt']['y_axis_type'] = 'linear' if option == 'ylog_interactive': if value: data_quants[i].attrs['plot_options']['interactive_yaxis_opt']['yi_axis_type'] = 'log' else: data_quants[i].attrs['plot_options']['interactive_yaxis_opt']['yi_axis_type'] = 'linear' if option == 'zlog': negflag = _zlog_check(data_quants, value, i) if negflag == 0: data_quants[i].attrs['plot_options']['zaxis_opt']['z_axis_type'] = 'log' else: data_quants[i].attrs['plot_options']['zaxis_opt']['z_axis_type'] = 'linear' if option == 'nodata': data_quants[i].attrs['plot_options']['line_opt']['visible'] = value if option == 'line_style': to_be = [] if value == 0 or value == 'solid_line': to_be = [] elif value == 1 or value == 'dot': to_be = [2, 4] elif value == 2 or value == 'dash': to_be = [6] elif value == 3 or value == 'dash_dot': to_be = [6, 4, 2, 4] elif value == 4 or value == 'dash_dot_dot_dot': to_be = [6, 4, 2, 4, 2, 4, 2, 4] elif value == 5 or value == 'long_dash': to_be = [10] else: to_be=value data_quants[i].attrs['plot_options']['line_opt']['line_style'] = to_be if(value == 6 or value == 'none'): data_quants[i].attrs['plot_options']['line_opt']['visible'] = False if option == 'char_size': data_quants[i].attrs['plot_options']['extras']['char_size'] = value if option == 'name': data_quants[i].attrs['plot_options']['line_opt']['name'] = value if option == "panel_size": if value > 1 or value <= 0: print("Invalid value. Should be (0, 1]") return data_quants[i].attrs['plot_options']['extras']['panel_size'] = value if option == 'basemap': data_quants[i].attrs['plot_options']['extras']['basemap'] = value if option == 'alpha': if value > 1 or value < 0: print("Invalid value. Should be [0, 1]") return data_quants[i].attrs['plot_options']['extras']['alpha'] = value if option == 'thick': data_quants[i].attrs['plot_options']['line_opt']['line_width'] = value if option == ('yrange' or 'y_range'): data_quants[i].attrs['plot_options']['yaxis_opt']['y_range'] = [value[0], value[1]] if option == ('zrange' or 'z_range'): data_quants[i].attrs['plot_options']['zaxis_opt']['z_range'] = [value[0], value[1]] if option == 'xrange_interactive': data_quants[i].attrs['plot_options']['interactive_xaxis_opt']['xi_range'] = [value[0], value[1]] if option == 'yrange_interactive': data_quants[i].attrs['plot_options']['interactive_yaxis_opt']['yi_range'] = [value[0], value[1]] if option == 'xtitle': data_quants[i].attrs['plot_options']['xaxis_opt']['axis_label'] = value if option == 'ytitle': data_quants[i].attrs['plot_options']['yaxis_opt']['axis_label'] = value if option == 'ztitle': data_quants[i].attrs['plot_options']['zaxis_opt']['axis_label'] = value if option == 'plotter': _reset_plots(i) data_quants[i].attrs['plot_options']['extras']['plotter'] = value if option == 'crosshair_x': data_quants[i].attrs['plot_options']['xaxis_opt']['crosshair'] = value if option == 'crosshair_y': data_quants[i].attrs['plot_options']['yaxis_opt']['crosshair'] = value if option == 'crosshair_z': data_quants[i].attrs['plot_options']['zaxis_opt']['crosshair'] = value if option == 'static': data_quants[i].attrs['plot_options']['extras']['static'] = value if option == 'static_tavg': data_quants[i].attrs['plot_options']['extras']['static_tavg'] = [value[0], value[1]] if option == 't_average': data_quants[i].attrs['plot_options']['extras']['t_average'] = value return
def options(name, option, value): """ This function allows the user to set a large variety of options for individual plots. Parameters: name : str Name of the tplot variable option : str The name of the option. See section below value : str/int/float/list The value of the option. See section below. Options: ============ ========== ===== Options Value type Notes ============ ========== ===== Color str/list Red, Orange, Yellow, Green, Blue, etc Colormap str/list https://matplotlib.org/examples/color/colormaps_reference.html Spec int 1 sets the Tplot Variable to spectrogram mode, 0 reverts Alt int 1 sets the Tplot Variable to altitude plot mode, 0 reverts Map int 1 sets the Tplot Variable to latitude/longitude mode, 0 reverts ylog int 1 sets the y axis to log scale, 0 reverts zlog int 1 sets the z axis to log scale, 0 reverts (spectrograms only) legend_names list A list of strings that will be used to identify the lines line_style str solid_line, dot, dash, dash_dot, dash_dot_dot_dot, long_dash name str The title of the plot panel_size flt Number between (0,1], representing the percent size of the plot basemap str Full path and name of a background image for "Map" plots alpha flt Number between [0,1], gives the transparancy of the plot lines yrange flt list Two numbers that give the y axis range of the plot zrange flt list Two numbers that give the z axis range of the plot ytitle str Title shown on the y axis ztitle str Title shown on the z axis. Spec plots only. ============ ========== ===== Returns: None Examples: >>> # Change the y range of Variable1 >>> import pytplot >>> x_data = [1,2,3,4,5] >>> y_data = [1,2,3,4,5] >>> pytplot.store_data("Variable1", data={'x':x_data, 'y':y_data}) >>> pytplot.options('Variable1', 'yrange', [2,4]) >>> # Change Variable1 to use a log scale >>> pytplot.options('Variable1', 'ylog', 1) >>> # Change the line color of Variable1 >>> pytplot.options('Variable1', 'ylog', 1) """ #if isinstance(name,int): # name = tplot_common.data_quants.keys()[name] if not isinstance(name, list): name = [name] option = option.lower() for i in name: if i not in data_quants.keys(): print(str(i) + " is currently not in pytplot.") return if option == 'color': if isinstance(value, list): data_quants[i].extras['line_color'] = value else: data_quants[i].extras['line_color'] = [value] if option == 'link': if isinstance(value, list): data_quants[i].link_to_tvar(value[0], value[1]) if option == 'colormap': if isinstance(value, list): data_quants[i].extras['colormap'] = value else: data_quants[i].extras['colormap'] = [value] if option == 'spec': data_quants[i].extras['spec'] = value if option == 'alt': data_quants[i].extras['alt'] = value if option == 'map': data_quants[i].extras['map'] = value if option == 'ylog': negflag = 0 namedata = data_quants[i] ##check variable data #if negative numbers, don't allow log setting datasets = [] if isinstance(namedata.data, list): for oplot_name in namedata.data: datasets.append(data_quants[oplot_name]) else: datasets.append(namedata) for dataset in datasets: if 'spec' not in dataset.extras: for column in dataset.data: if np.nanmin(dataset.data[column]) < 0: print( 'Negative data is incompatible with log plotting.' ) negflag = 1 break else: if dataset.extras['spec'] == 1: for column in dataset.spec_bins: if np.nanmin(dataset.spec_bins[column]) < 0: print( 'Negative data is incompatible with log plotting.' ) negflag = 1 break if value == 1 and negflag == 0: data_quants[i].yaxis_opt['y_axis_type'] = 'log' else: data_quants[i].yaxis_opt['y_axis_type'] = 'linear' if option == 'legend_names': data_quants[i].yaxis_opt['legend_names'] = value if option == 'zlog': negflag = 0 namedata = data_quants[i] ##check variable data #if negative numbers, don't allow log setting datasets = [] if isinstance(namedata.data, list): for oplot_name in namedata.data: datasets.append(data_quants[oplot_name]) else: datasets.append(namedata) for dataset in datasets: if 'spec' in dataset.extras: if dataset.extras['spec'] == 1: negflag = 0 for column in dataset.data: if np.nanmin(dataset.data[column]) < 0: print( 'Negative data is incompatible with log plotting.' ) negflag = 1 break #verify there are no negative values if negflag == 0 and value == 1: data_quants[i].zaxis_opt['z_axis_type'] = 'log' else: data_quants[i].zaxis_opt['z_axis_type'] = 'linear' else: if value == 1: data_quants[i].zaxis_opt['z_axis_type'] = 'log' else: data_quants[i].zaxis_opt['z_axis_type'] = 'linear' else: if value == 1: data_quants[i].zaxis_opt['z_axis_type'] = 'log' else: data_quants[i].zaxis_opt['z_axis_type'] = 'linear' if option == 'nodata': data_quants[i].line_opt['visible'] = value if option == 'line_style': to_be = [] if value == 0 or value == 'solid_line': to_be = [] elif value == 1 or value == 'dot': to_be = [2, 4] elif value == 2 or value == 'dash': to_be = [6] elif value == 3 or value == 'dash_dot': to_be = [6, 4, 2, 4] elif value == 4 or value == 'dash_dot_dot_dot': to_be = [6, 4, 2, 4, 2, 4, 2, 4] elif value == 5 or value == 'long_dash': to_be = [10] data_quants[i].line_opt['line_dash'] = to_be if (value == 6 or value == 'none'): data_quants[i].line_opt['visible'] = False if option == 'name': data_quants[i].line_opt['name'] = value if option == "panel_size": if value > 1 or value <= 0: print("Invalid value. Should be (0, 1]") return data_quants[i].extras['panel_size'] = value if option == 'basemap': data_quants[i].extras['basemap'] = value if option == 'alpha': if value > 1 or value < 0: print("Invalid value. Should be [0, 1]") return data_quants[i].extras['alpha'] = value if option == 'thick': data_quants[i].line_opt['line_width'] = value if option == 'transparency': alpha_val = value / 100 data_quants[i].line_opt['line_alpha'] = alpha_val if option == ('yrange' or 'y_range'): data_quants[i].yaxis_opt['y_range'] = [value[0], value[1]] if option == ('zrange' or 'z_range'): data_quants[i].zaxis_opt['z_range'] = [value[0], value[1]] if option == 'ytitle': data_quants[i].yaxis_opt['axis_label'] = value if option == 'ztitle': data_quants[i].zaxis_opt['axis_label'] = value if option == 'plotter': data_quants[i].extras['plotter'] = value return
def netcdf_to_tplot(filenames, time ='', prefix='', suffix='', plot=False, merge=False): ''' This function will automatically create tplot variables from CDF files. Parameters: filenames : str/list of str The file names and full paths of netCDF files. time: str The name of the netCDF file's time variable. prefix: str The tplot variable names will be given this prefix. By default, no prefix is added. suffix: str The tplot variable names will be given this suffix. By default, no suffix is added. plot: bool The data is plotted immediately after being generated. All tplot variables generated from this function will be on the same plot. By default, a plot is not created. merge: bool If True, then data from different netCDF files will be merged into a single pytplot variable. Returns: List of tplot variables created. Examples: >>> #Create tplot variables from a GOES netCDF file >>> import pytplot >>> file = "/Users/user_name/goes_files/g15_epead_a16ew_1m_20171201_20171231.nc" >>> pytplot.netcdf_to_tplot(file, prefix='mvn_') >>> #Add a prefix, and plot immediately. >>> import pytplot >>> file = "/Users/user_name/goes_files/g15_epead_a16ew_1m_20171201_20171231.nc" >>> pytplot.netcdf_to_tplot(file, prefix='goes_prefix_', plot=True) ''' from netCDF4 import Dataset stored_variables = [] global data_quants if isinstance(filenames, str): filenames = [filenames] elif isinstance(filenames, list): filenames = filenames else: print("Invalid filenames input.") #return stored_variables for filename in filenames: # Read in file file = Dataset(filename, "r+") # Creating dictionary that will contain variables and their attributes vars_and_atts = {} for name, variable in file.variables.items(): vars_and_atts[name] = {} for attrname in variable.ncattrs(): vars_and_atts[name][attrname] = getattr(variable, attrname) # Filling in missing values for each variable with np.nan (if values are not already nan) # and saving the masked variables to a new dictionary masked_vars = {} # Dictionary containing properly masked variables for var in vars_and_atts.keys(): reg_var = file.variables[var] try: var_fill_value = vars_and_atts[var]['missing_value'] if np.isnan(var_fill_value) != True: # We want to force missing values to be nan so that plots don't look strange var_mask = np.ma.masked_where(reg_var == np.float32(var_fill_value), reg_var) var_filled = np.ma.filled(var_mask, np.nan) masked_vars[var] = var_filled elif np.isnan(var_fill_value) == True: # missing values are already np.nan, don't need to do anything var_filled = reg_var masked_vars[var] = var_filled except: # continue # Go to next iteration, this variable doesn't have data to mask (probably just a descriptor variable (i.e., 'base_time') var_filled = reg_var masked_vars[var] = var_filled # Most files are from GOES data, which seems to usually have 'time_tag' in them that contain time information. # There is an exception filter below that will allow a user to pick a different time variable if time_tag doesn't exist. if time != '': time_var = file[time] unix_times = change_time_to_unix_time(time_var) elif time == '': time = input('Please enter time variable name. \nVariable list: {l}'.format(l=vars_and_atts.keys())) while True: if time not in vars_and_atts.keys(): # Making sure we input a valid response (i.e., the variable exists in the dataset), and also avoiding # plotting a time variable against time.... because I don't even know what that would mean and uncover. print('Not a valid variable name, please try again.') continue elif time in vars_and_atts.keys(): time_var = time unix_times = change_time_to_unix_time(time_var) for i,var in enumerate(file.variables): # Here, we are making sure that the variables are time-based, otherwise we don't want to store them as tplot variables! if 'record' in file[var].dimensions[0] or 'time' in file[var].dimensions[0]: # Store the data now, as well as merge variables if that's desired var_name = prefix + var + suffix to_merge = False if (var_name in data_quants.keys() and (merge == True)): prev_data_quant = data_quants[var_name].data to_merge = True tplot_data = {'x': unix_times, 'y': masked_vars[var]} store_data(var_name, tplot_data) if var_name not in stored_variables: stored_variables.append(var_name) if to_merge == True: cur_data_quant = data_quants[var_name].data merged_data = [prev_data_quant, cur_data_quant] data_quants[var_name].data = pd.concat(merged_data) # If we are interested in seeing a quick plot of the variables, do it if plot: tplot(stored_variables) else: # If the variable isn't time-bound, we're going to look at the next variable continue return (stored_variables)
def cdf_to_tplot(filenames, varformat=None, get_support_data=False, prefix='', suffix='', plot=False, merge=False, center_measurement=False, notplot=False): """ This function will automatically create tplot variables from CDF files. .. note:: Variables must have an attribute named "VAR_TYPE". If the attribute entry is "data" (or "support_data"), then they will be added as tplot variables. Additionally, data variables should have attributes named "DEPEND_TIME" or "DEPEND_0" that describes which variable is x axis. If the data is 2D, then an attribute "DEPEND_1" must describe which variable contains the secondary axis. Parameters: filenames : str/list of str The file names and full paths of CDF files. varformat : str The file variable formats to load into tplot. Wildcard character "*" is accepted. By default, all variables are loaded in. get_support_data: bool Data with an attribute "VAR_TYPE" with a value of "support_data" will be loaded into tplot. By default, only loads in data with a "VAR_TYPE" attribute of "data". prefix: str The tplot variable names will be given this prefix. By default, no prefix is added. suffix: str The tplot variable names will be given this suffix. By default, no suffix is added. plot: bool The data is plotted immediately after being generated. All tplot variables generated from this function will be on the same plot. merge: bool If True, then data from different cdf files will be merged into a single pytplot variable. center_measurement: bool If True, the CDF epoch variables are time-shifted to the middle of the accumulation interval by their DELTA_PLUS_VAR and DELTA_MINUS_VAR variable attributes notplot: bool If True, then data are returned in a hash table instead of being stored in tplot variables (useful for debugging, and access to multi-dimensional data products) Returns: List of tplot variables created (unless notplot keyword is used). """ stored_variables = [] epoch_cache = {} output_table = {} metadata = {} data_quants = {} if isinstance(filenames, str): filenames = [filenames] elif isinstance(filenames, list): filenames = filenames else: print("Invalid filenames input.") return stored_variables var_type = ['data'] if varformat is None: varformat = ".*" if get_support_data: var_type.append('support_data') varformat = varformat.replace("*", ".*") var_regex = re.compile(varformat) for filename in filenames: cdf_file = cdflib.CDF(filename) cdf_info = cdf_file.cdf_info() all_cdf_variables = cdf_info['rVariables'] + cdf_info['zVariables'] # Find the data variables for var in all_cdf_variables: if not re.match(var_regex, var): continue var_atts = cdf_file.varattsget(var) if 'VAR_TYPE' not in var_atts: continue if var_atts['VAR_TYPE'] in var_type: var_atts = cdf_file.varattsget(var) var_properties = cdf_file.varinq(var) if "DEPEND_TIME" in var_atts: x_axis_var = var_atts["DEPEND_TIME"] elif "DEPEND_0" in var_atts: x_axis_var = var_atts["DEPEND_0"] else: if var_atts['VAR_TYPE'].lower() == 'data': print("Cannot find x axis.") print("No attribute named DEPEND_TIME or DEPEND_0 in \ variable " + var) continue data_type_description \ = cdf_file.varinq(x_axis_var)['Data_Type_Description'] # Find data name and if it is already in stored variables var_name = prefix + var + suffix if epoch_cache.get(filename+x_axis_var) is None: delta_plus_var = 0.0 delta_minus_var = 0.0 delta_time = 0.0 xdata = cdf_file.varget(x_axis_var) epoch_var_atts = cdf_file.varattsget(x_axis_var) # check for DELTA_PLUS_VAR/DELTA_MINUS_VAR attributes if center_measurement: if 'DELTA_PLUS_VAR' in epoch_var_atts: delta_plus_var = cdf_file.varget(epoch_var_atts['DELTA_PLUS_VAR']) delta_plus_var_att = cdf_file.varattsget(epoch_var_atts['DELTA_PLUS_VAR']) # check if a conversion to seconds is required if 'SI_CONVERSION' in delta_plus_var_att: si_conv = delta_plus_var_att['SI_CONVERSION'] delta_plus_var = delta_plus_var.astype(float)*np.float(si_conv.split('>')[0]) elif 'SI_CONV' in delta_plus_var_att: si_conv = delta_plus_var_att['SI_CONV'] delta_plus_var = delta_plus_var.astype(float)*np.float(si_conv.split('>')[0]) if 'DELTA_MINUS_VAR' in epoch_var_atts: delta_minus_var = cdf_file.varget(epoch_var_atts['DELTA_MINUS_VAR']) delta_minus_var_att = cdf_file.varattsget(epoch_var_atts['DELTA_MINUS_VAR']) # check if a conversion to seconds is required if 'SI_CONVERSION' in delta_minus_var_att: si_conv = delta_minus_var_att['SI_CONVERSION'] delta_minus_var = delta_minus_var.astype(float)*np.float(si_conv.split('>')[0]) elif 'SI_CONV' in delta_minus_var_att: si_conv = delta_minus_var_att['SI_CONV'] delta_minus_var = delta_minus_var.astype(float)*np.float(si_conv.split('>')[0]) # sometimes these are specified as arrays if isinstance(delta_plus_var, np.ndarray) and isinstance(delta_minus_var, np.ndarray): delta_time = (delta_plus_var-delta_minus_var)/2.0 else: # and sometimes constants if delta_plus_var != 0.0 or delta_minus_var != 0.0: delta_time = (delta_plus_var-delta_minus_var)/2.0 if epoch_cache.get(filename + x_axis_var) is None: if ('CDF_TIME' in data_type_description) or \ ('CDF_EPOCH' in data_type_description): xdata = cdfepoch.unixtime(xdata) epoch_cache[filename+x_axis_var] = np.array(xdata)+delta_time else: xdata = epoch_cache[filename + x_axis_var] try: ydata = cdf_file.varget(var) except: continue if ydata is None: continue if "FILLVAL" in var_atts: if (var_properties['Data_Type_Description'] == 'CDF_FLOAT' or var_properties['Data_Type_Description'] == 'CDF_REAL4' or var_properties['Data_Type_Description'] == 'CDF_DOUBLE' or var_properties['Data_Type_Description'] == 'CDF_REAL8'): if ydata[ydata == var_atts["FILLVAL"]].size != 0: ydata[ydata == var_atts["FILLVAL"]] = np.nan tplot_data = {'x': xdata, 'y': ydata} depend_1 = None depend_2 = None depend_3 = None if "DEPEND_1" in var_atts: if var_atts["DEPEND_1"] in all_cdf_variables: depend_1 = np.array(cdf_file.varget(var_atts["DEPEND_1"])) if "DEPEND_2" in var_atts: if var_atts["DEPEND_2"] in all_cdf_variables: depend_2 = np.array(cdf_file.varget(var_atts["DEPEND_2"])) if "DEPEND_3" in var_atts: if var_atts["DEPEND_3"] in all_cdf_variables: depend_3 = np.array(cdf_file.varget(var_atts["DEPEND_3"])) nontime_varying_depends = [] if depend_1 is not None and depend_2 is not None and depend_3 is not None: tplot_data['v1'] = depend_1 tplot_data['v2'] = depend_2 tplot_data['v3'] = depend_3 if len(depend_1.shape) == 1: nontime_varying_depends.append('v1') if len(depend_2.shape) == 1: nontime_varying_depends.append('v2') if len(depend_3.shape) == 1: nontime_varying_depends.append('v3') elif depend_1 is not None and depend_2 is not None: tplot_data['v1'] = depend_1 tplot_data['v2'] = depend_2 if len(depend_1.shape) == 1: nontime_varying_depends.append('v1') if len(depend_2.shape) == 1: nontime_varying_depends.append('v2') elif depend_1 is not None: tplot_data['v'] = depend_1 if len(depend_1.shape) == 1: nontime_varying_depends.append('v') elif depend_2 is not None: tplot_data['v'] = depend_2 if len(depend_2.shape) == 1: nontime_varying_depends.append('v') metadata[var_name] = {'display_type': var_atts.get("DISPLAY_TYPE", "time_series"), 'scale_type': var_atts.get("SCALE_TYP", "linear")} if var_name not in output_table: output_table[var_name] = tplot_data else: var_data = output_table[var_name] for output_var in var_data: if output_var not in nontime_varying_depends: var_data[output_var] = np.concatenate((var_data[output_var], tplot_data[output_var])) if notplot: return output_table for var_name in output_table.keys(): to_merge = False if var_name in data_quants.keys() and merge: prev_data_quant = data_quants[var_name] to_merge = True try: store_data(var_name, data=output_table[var_name]) except ValueError: continue if var_name not in stored_variables: stored_variables.append(var_name) if metadata.get(var_name) is not None: if metadata[var_name]['display_type'] == "spectrogram": options(var_name, 'spec', 1) if metadata[var_name]['scale_type'] == 'log': options(var_name, 'ylog', 1) if to_merge is True: cur_data_quant = data_quants[var_name] plot_options = copy.deepcopy(data_quants[var_name].attrs['plot_options']) data_quants[var_name] = xr.concat([prev_data_quant, cur_data_quant], dim='time') data_quants[var_name].attrs['plot_options'] = plot_options if notplot: return output_table if plot: tplot(stored_variables) return stored_variables
def options(name, option, value): """ This function allows the user to set a large variety of options for individual plots. Parameters: name : str Name of the tplot variable option : str The name of the option. See section below value : str/int/float/list The value of the option. See section below. Options: ============ ========== ===== Options Value type Notes ============ ========== ===== Color str/list Red, Orange, Yellow, Green, Blue, etc Colormap str/list https://matplotlib.org/examples/color/colormaps_reference.html Spec int 1 sets the Tplot Variable to spectrogram mode, 0 reverts Alt int 1 sets the Tplot Variable to altitude plot mode, 0 reverts Map int 1 sets the Tplot Variable to latitude/longitude mode, 0 reverts ylog int 1 sets the y axis to log scale, 0 reverts zlog int 1 sets the z axis to log scale, 0 reverts (spectrograms only) legend_names list A list of strings that will be used to identify the lines line_style str solid_line, dot, dash, dash_dot, dash_dot_dot_dot, long_dash name str The title of the plot panel_size flt Number between (0,1], representing the percent size of the plot basemap str Full path and name of a background image for "Map" plots alpha flt Number between [0,1], gives the transparancy of the plot lines yrange flt list Two numbers that give the y axis range of the plot zrange flt list Two numbers that give the z axis range of the plot ytitle str Title shown on the y axis ztitle str Title shown on the z axis. Spec plots only. ============ ========== ===== Returns: None Examples: >>> # Change the y range of Variable1 >>> import pytplot >>> x_data = [1,2,3,4,5] >>> y_data = [1,2,3,4,5] >>> pytplot.store_data("Variable1", data={'x':x_data, 'y':y_data}) >>> pytplot.options('Variable1', 'yrange', [2,4]) >>> # Change Variable1 to use a log scale >>> pytplot.options('Variable1', 'ylog', 1) >>> # Change the line color of Variable1 >>> pytplot.options('Variable1', 'ylog', 1) """ #if isinstance(name,int): # name = tplot_common.data_quants.keys()[name] if not isinstance(name, list): name = [name] option = option.lower() for i in name: if i not in data_quants.keys(): print(str(i) + " is currently not in pytplot.") return (new_yaxis_opt, new_zaxis_opt, new_line_opt, new_extras) = set_options( option, value, data_quants[i].yaxis_opt, data_quants[i].zaxis_opt, data_quants[i].line_opt, data_quants[i].extras) data_quants[i].yaxis_opt = new_yaxis_opt data_quants[i].zaxis_opt = new_zaxis_opt data_quants[i].line_opt = new_line_opt data_quants[i].extras = new_extras return