def test_time_double(self): """Test time_double function.""" t0 = time_float() t1 = time_float_one() t2 = time_float(1450181243) print("Time_double functions test:", t0, t1, t2) self.assertTrue(time_double('2015-12-15 12:07:23.767000') == 1450181243.767) self.assertTrue(time_double(['2015-12-15 12:07:23.767000', '2015-12-15 12:07:43.767000']) == [1450181243.767, 1450181263.767])
def time_datetime(time=None, tz=None): """Find python datetime. Transform a list of float daytime values to a list of pythonic 'datetime.datetime' values. Parameters ---------- time: float/list of floats or str/list of str, optional Input time. The default is None, which returns the time now. Returns ------- list of datetime.datetime Datetimes as `datetime.datetime`. """ if tz == None: tz = timezone.utc if time is None: return datetime.now() if isinstance(time, str): return time_datetime(time_float(time)) if isinstance(time, (int, float)): return datetime.fromtimestamp(time, tz=tz) return [time_datetime(_time) for _time in time]
def ex_create(plot=True): """Show how to create and plot pytplot variables.""" # Delete any existing pytplot variables pytplot.del_data() # Create a sin wave plot a = list(range(0, 101)) b = [2.0 / 100.0 * numpy.pi * s for s in a] c = time_float('2017-01-01') x = list() y = list() for i in range(len(b)): x.append(c + 60.0 / (2 * numpy.pi) * 60.0 * b[i]) y.append(1000.0 * numpy.sin(b[i])) # Store data pytplot.store_data('sinx', data={'x': x, 'y': y}) # Apply yclip pyspedas.yclip('sinx', -800.0, 800.0) # Remove NaN values pyspedas.tdeflag('sinx-clip') # Interpolate pyspedas.tinterpol(['sinx-clip-deflag'], 'sinx', 'quadratic') # Plot pytplot.ylim('sinx', -1100.0, 1100.0) pytplot.ylim('sinx-clip', -1100.0, 1100.0) pytplot.ylim('sinx-clip-deflag', -1100.0, 1100.0) pytplot.ylim('sinx-clip-deflag-itrp', -1100.0, 1100.0) pytplot.tplot_options('title', 'Interpolation example') if plot: pytplot.tplot( ['sinx', 'sinx-clip', 'sinx-clip-deflag', 'sinx-clip-deflag-itrp']) # Return 1 as indication that the example finished without problems. return 1
def pwe_wfc(trange=['2017-04-01/12:00:00', '2017-04-01/13:00:00'], datatype='waveform', mode='65khz', level='l2', suffix='', coord='sgi', component='all', get_support_data=False, varformat=None, varnames=[], downloadonly=False, notplot=False, no_update=False, uname=None, passwd=None, time_clip=False, ror=True): """ This function loads data from the PWE experiment from the Arase mission Parameters: trange : list of str time range of interest [starttime, endtime] with the format 'YYYY-MM-DD','YYYY-MM-DD'] or to specify more or less than a day ['YYYY-MM-DD/hh:mm:ss','YYYY-MM-DD/hh:mm:ss'] datatype: str Data type; Valid options: level: str Data level; Valid options: suffix: str The tplot variable names will be given this suffix. By default, no suffix is added. 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". varformat: str The file variable formats to load into tplot. Wildcard character "*" is accepted. By default, all variables are loaded in. varnames: list of str List of variable names to load (if not specified, all data variables are loaded) downloadonly: bool Set this flag to download the CDF files, but not load them into tplot variables notplot: bool Return the data in hash tables instead of creating tplot variables no_update: bool If set, only load data from your local cache time_clip: bool Time clip the variables to exactly the range specified in the trange keyword ror: bool If set, print PI info and rules of the road Returns: List of tplot variables created. """ initial_notplot_flag = False if notplot: initial_notplot_flag = True file_res = 3600. if level == 'l2': prefix = 'erg_pwe_wfc_' + level + '_' + mode + '_' loaded_data = [] if level == 'l2': if datatype == 'waveform': tplot_name_list = [] if component == 'all': component_list = ['e', 'b'] elif (component == 'e') or (component == 'b'): component_list = [component] for com in component_list: prefix = 'erg_pwe_wfc_' + level + '_' + com + '_' + mode + '_' pathformat = 'satellite/erg/pwe/wfc/'+level+'/'+datatype+'/%Y/%m/erg_pwe_wfc_' + \ level+'_'+com+'_'+datatype+'_'+mode+'_'+coord+'_%Y%m%d%H_v??_??.cdf' loaded_data.append( load(pathformat=pathformat, trange=trange, level=level, datatype=datatype, file_res=file_res, prefix=prefix, suffix=suffix, get_support_data=get_support_data, varformat=varformat, varnames=varnames, downloadonly=downloadonly, notplot=notplot, time_clip=time_clip, no_update=no_update, uname=uname, passwd=passwd)) if com == 'e': tplot_name_list += [ prefix + 'Ex_waveform', prefix + 'Ey_waveform' ] elif com == 'b': tplot_name_list += [ prefix + 'Bx_waveform', prefix + 'By_waveform', prefix + 'Bz_waveform' ] elif datatype == 'spec': prefix_list = [] component_suffix_list = [] if component == 'all': component_list = ['e', 'b'] elif (component == 'e') or (component == 'b'): component_list = [component] for com in component_list: prefix = 'erg_pwe_wfc_' + level + '_' + com + '_' + mode + '_' pathformat = 'satellite/erg/pwe/wfc/'+level+'/'+datatype+'/%Y/%m/erg_pwe_wfc_' + \ level+'_'+com+'_'+datatype+'_'+mode+'_%Y%m%d%H_v??_??.cdf' loaded_data.append( load(pathformat=pathformat, trange=trange, level=level, datatype=datatype, file_res=file_res, prefix=prefix, suffix=suffix, get_support_data=get_support_data, varformat=varformat, varnames=varnames, downloadonly=downloadonly, notplot=notplot, time_clip=time_clip, no_update=no_update, uname=uname, passwd=passwd)) prefix_list.append(prefix) component_suffix_list.append(com.upper() + '_spectra') if (len(loaded_data) > 0) and ror: try: if isinstance(loaded_data, list): if downloadonly: cdf_file = cdflib.CDF(loaded_data[-1][-1]) gatt = cdf_file.globalattsget() elif notplot: gatt = loaded_data[-1][list( loaded_data[-1].keys())[-1]]['CDF']['GATT'] else: gatt = get_data(loaded_data[-1][-1], metadata=True)['CDF']['GATT'] # --- print PI info and rules of the road print(' ') print(' ') print( '**************************************************************************' ) print(gatt["LOGICAL_SOURCE_DESCRIPTION"]) print('') print('Information about ERG PWE WFC') print('') print('PI: ', gatt['PI_NAME']) print("Affiliation: " + gatt["PI_AFFILIATION"]) print('') print( 'RoR of ERG project common: https://ergsc.isee.nagoya-u.ac.jp/data_info/rules_of_the_road.shtml.en' ) print( 'RoR of PWE/WFC: https://ergsc.isee.nagoya-u.ac.jp/mw/index.php/ErgSat/Pwe/Wfc' ) print('') print('Contact: erg_pwe_info at isee.nagoya-u.ac.jp') print( '**************************************************************************' ) except: print('printing PI info and rules of the road was failed') if initial_notplot_flag or downloadonly: return loaded_data if datatype == 'spec': trange_in_float = time_float(trange) for i in range(len(prefix_list)): t_plot_name = prefix_list[i] + component_suffix_list[i] options(t_plot_name, 'spec', 1) options(t_plot_name, 'colormap', 'jet') options(t_plot_name, 'ylog', 1) options(t_plot_name, 'zlog', 1) options(t_plot_name, 'ysubtitle', '[Hz]') ylim(t_plot_name, 32., 2e4) if 'E_spectra' in component_suffix_list[i]: zlim(t_plot_name, 1e-9, 1e-2) options(t_plot_name, 'ztitle', '[mV^2/m^2/Hz]') options(t_plot_name, 'ytitle', 'E\nspectra') elif 'B_spectra' in component_suffix_list[i]: zlim(t_plot_name, 1e-4, 1e2) options(t_plot_name, 'ztitle', '[pT^2/Hz]') options(t_plot_name, 'ytitle', 'B\nspectra') get_data_vars = get_data(t_plot_name) time_array = get_data_vars[0] if time_array[0] <= trange_in_float[0]: t_ge_indices = np.where(time_array <= trange_in_float[0]) t_min_index = t_ge_indices[0][-1] else: t_min_index = 0 if trange_in_float[1] <= time_array[-1]: t_le_indices = np.where(trange_in_float[1] <= time_array) t_max_index = t_le_indices[0][0] else: t_max_index = -1 if t_min_index == t_max_index: t_max_index = +1 if (t_min_index != 0) or (t_max_index != -1): meta_data = get_data(t_plot_name, metadata=True) store_data(t_plot_name, newname=t_plot_name + '_all_loaded_time_range') store_data(t_plot_name, data={ 'x': time_array[t_min_index:t_max_index], 'y': get_data_vars[1][t_min_index:t_max_index], 'v': get_data_vars[2] }, attr_dict=meta_data) options(t_plot_name, 'zlog', 1) if 'E_spectra' in t_plot_name: zlim(t_plot_name, 1e-9, 1e-2) elif 'B_spectra' in t_plot_name: zlim(t_plot_name, 1e-4, 1e2) if datatype == 'waveform': trange_in_float = time_float(trange) yn = '' all_time_range_flag = False if trange_in_float[1] - trange_in_float[0] <= 0.: yn = input('Invalid time range. Use full time range ?:[y/n] ') if yn == 'y': all_time_range_flag = True t_min_index = 0 t_max_index = -1 else: return for t_plot_name in tplot_name_list: get_data_vars = get_data(t_plot_name) dl_in = get_data(t_plot_name, metadata=True) time_array = get_data_vars[0] if not all_time_range_flag: if time_array[0] <= trange_in_float[0]: t_ge_indices = np.where(time_array <= trange_in_float[0]) t_min_index = t_ge_indices[0][-1] else: t_min_index = 0 if trange_in_float[1] <= time_array[-1]: t_le_indices = np.where(trange_in_float[1] <= time_array) t_max_index = t_le_indices[0][0] else: t_max_index = -1 if t_min_index == t_max_index: t_max_index = +1 data = np.where(get_data_vars[1] <= -1e+30, np.nan, get_data_vars[1]) dt = get_data_vars[2] ndt = dt.size ndata = (t_max_index - t_min_index) * ndt time_new = (np.tile(time_array[t_min_index:t_max_index], (ndt, 1)).T + dt * 1e-3).reshape(ndata) data_new = data[t_min_index:t_max_index].reshape(ndata) store_data(t_plot_name, data={ 'x': time_new, 'y': data_new }, attr_dict=dl_in) options(t_plot_name, 'ytitle', '\n'.join(t_plot_name.split('_'))) # ylim settings because pytplot.timespan() doesn't affect in ylim. # May be it will be no need in future. if not all_time_range_flag: if time_new[0] <= trange_in_float[0]: t_min_index = np.where( (time_new <= trange_in_float[0]))[0][-1] else: t_min_index = 0 if trange_in_float[1] <= time_new[-1]: t_max_index = np.where( trange_in_float[1] <= time_new)[0][0] else: t_max_index = -1 ylim_min = np.nanmin(data_new[t_min_index:t_max_index]) ylim_max = np.nanmax(data_new[t_min_index:t_max_index]) ylim(t_plot_name, ylim_min, ylim_max) return loaded_data
def att(trange=['2017-04-01', '2017-04-02'], level='l2', downloadonly=False, notplot=False, no_update=False, uname=None, passwd=None): """ This function loads attitude data from the Arase mission Parameters: trange : list of str time range of interest [starttime, endtime] with the format 'YYYY-MM-DD','YYYY-MM-DD'] or to specify more or less than a day ['YYYY-MM-DD/hh:mm:ss','YYYY-MM-DD/hh:mm:ss'] level: str Data level; Valid options: downloadonly: bool Set this flag to download the files, but not load them into tplot variables notplot: bool Return the data in hash tables instead of creating tplot variables no_update: bool If set, only load data from your local cache Returns: None """ file_res = 24*3600. pathformat = 'satellite/erg/att/txt/erg_att_'+level+'_%Y%m%d_v??.txt' out_files = load(pathformat=pathformat, trange=trange, file_res=file_res, downloadonly=True, no_update=no_update, uname=uname, passwd=passwd) if downloadonly: return out_files data_flame_list = [] for file in out_files: raw_read_table = pd.read_table(file) data_flame_list.append( raw_read_table[10:][raw_read_table.keys()[0]].str.split(expand=True)) concat_frame_for_tplot = pd.concat(data_flame_list) time_float_array = time_float(concat_frame_for_tplot.iloc[:, 0]) Omega_float_array = concat_frame_for_tplot.iloc[:, 1].astype(float) Phase_float_array = concat_frame_for_tplot.iloc[:, 9].astype(float) I_Alpha_float_array = concat_frame_for_tplot.iloc[:, 2].astype(float) I_Delta_float_array = concat_frame_for_tplot.iloc[:, 3].astype(float) GX_Alpha_float_array = concat_frame_for_tplot.iloc[:, 10].astype(float) GX_Delta_float_array = concat_frame_for_tplot.iloc[:, 11].astype(float) GZ_Alpha_float_array = concat_frame_for_tplot.iloc[:, 12].astype(float) GZ_Delta_float_array = concat_frame_for_tplot.iloc[:, 13].astype(float) if notplot: output_dictionary = {} output_dictionary['erg_att_sprate'] = { 'x': time_float_array, 'y': Omega_float_array} output_dictionary['erg_att_spphase'] = { 'x': time_float_array, 'y': Phase_float_array} output_dictionary['erg_att_izras'] = { 'x': time_float_array, 'y': I_Alpha_float_array} output_dictionary['erg_att_izdec'] = { 'x': time_float_array, 'y': I_Delta_float_array} output_dictionary['erg_att_gxras'] = { 'x': time_float_array, 'y': GX_Alpha_float_array} output_dictionary['erg_att_gxdec'] = { 'x': time_float_array, 'y': GX_Delta_float_array} output_dictionary['erg_att_gzras'] = { 'x': time_float_array, 'y': GZ_Alpha_float_array} output_dictionary['erg_att_gzdec'] = { 'x': time_float_array, 'y': GZ_Delta_float_array} return output_dictionary else: store_data('erg_att_sprate', data={ 'x': time_float_array, 'y': Omega_float_array}) store_data('erg_att_spphase', data={ 'x': time_float_array, 'y': Phase_float_array}) store_data('erg_att_izras', data={ 'x': time_float_array, 'y': I_Alpha_float_array}) store_data('erg_att_izdec', data={ 'x': time_float_array, 'y': I_Delta_float_array}) store_data('erg_att_gxras', data={ 'x': time_float_array, 'y': GX_Alpha_float_array}) store_data('erg_att_gxdec', data={ 'x': time_float_array, 'y': GX_Delta_float_array}) store_data('erg_att_gzras', data={ 'x': time_float_array, 'y': GZ_Alpha_float_array}) store_data('erg_att_gzdec', data={ 'x': time_float_array, 'y': GZ_Delta_float_array}) return None
def pwe_efd(trange=['2017-04-01', '2017-04-02'], datatype='E_spin', level='l2', suffix='', coord='dsi', get_support_data=False, varformat=None, varnames=[], downloadonly=False, notplot=False, no_update=False, uname=None, passwd=None, time_clip=False, ror=True): """ This function loads data from the PWE experiment from the Arase mission Parameters: trange : list of str time range of interest [starttime, endtime] with the format 'YYYY-MM-DD','YYYY-MM-DD'] or to specify more or less than a day ['YYYY-MM-DD/hh:mm:ss','YYYY-MM-DD/hh:mm:ss'] datatype: str Data type; Valid options: level: str Data level; Valid options: suffix: str The tplot variable names will be given this suffix. By default, no suffix is added. 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". varformat: str The file variable formats to load into tplot. Wildcard character "*" is accepted. By default, all variables are loaded in. varnames: list of str List of variable names to load (if not specified, all data variables are loaded) downloadonly: bool Set this flag to download the CDF files, but not load them into tplot variables notplot: bool Return the data in hash tables instead of creating tplot variables no_update: bool If set, only load data from your local cache time_clip: bool Time clip the variables to exactly the range specified in the trange keyword ror: bool If set, print PI info and rules of the road Returns: List of tplot variables created. """ initial_notplot_flag = False if notplot: initial_notplot_flag = True file_res = 3600. * 24 prefix = 'erg_pwe_efd_' + level + '_' if ('64' in datatype) or ('256' in datatype): if '64' in datatype: mode = '64Hz' elif '256' in datatype: mode = '256Hz' md = 'E' + mode pathformat = 'satellite/erg/pwe/efd/'+level+'/'+md + \ '/%Y/%m/erg_pwe_efd_'+level+'_'+md+'_'+coord+'_%Y%m%d_v??_??.cdf' prefix += md + '_' + coord + '_' if coord == 'wpt': component = ['Eu_waveform', 'Ev_waveform'] elif coord == 'dsi': component = [ 'Ex_waveform', 'Ey_waveform', 'Eu_offset' + '_' + mode, 'Ev_offset' + '_' + mode ] else: pathformat = 'satellite/erg/pwe/efd/'+level+'/'+datatype + \ '/%Y/%m/erg_pwe_efd_'+level+'_'+datatype+'_%Y%m%d_v??_??.cdf' prefix += datatype + '_' if 'spin' in datatype: component = ['Eu', 'Ev', 'Eu1', 'Ev1', 'Eu2', 'Ev2'] labels = ['Ex', 'Ey'] if datatype == 'pot': component = ['Vu1', 'Vu2', 'Vv1', 'Vv2'] if datatype == 'pot8Hz': component = [ 'Vu1_waveform_8Hz', 'Vu2_waveform_8Hz', 'Vv1_waveform_8Hz', 'Vv2_waveform_8Hz' ] loaded_data = load(pathformat=pathformat, trange=trange, level=level, datatype=datatype, file_res=file_res, prefix=prefix, suffix=suffix, get_support_data=get_support_data, varformat=varformat, varnames=varnames, downloadonly=downloadonly, notplot=notplot, time_clip=time_clip, no_update=no_update, uname=uname, passwd=passwd) if (len(loaded_data) > 0) and ror: try: if isinstance(loaded_data, list): if downloadonly: cdf_file = cdflib.CDF(loaded_data[-1]) gatt = cdf_file.globalattsget() else: gatt = get_data(loaded_data[-1], metadata=True)['CDF']['GATT'] elif isinstance(loaded_data, dict): gatt = loaded_data[list(loaded_data.keys())[-1]]['CDF']['GATT'] # --- print PI info and rules of the road print(' ') print(' ') print( '**************************************************************************' ) print(gatt["LOGICAL_SOURCE_DESCRIPTION"]) print('') print('Information about ERG PWE EFD') print('') print('PI: ', gatt['PI_NAME']) print("Affiliation: " + gatt["PI_AFFILIATION"]) print('') print( 'RoR of ERG project common: https://ergsc.isee.nagoya-u.ac.jp/data_info/rules_of_the_road.shtml.en' ) print( 'RoR of PWE/EFD: https://ergsc.isee.nagoya-u.ac.jp/mw/index.php/ErgSat/Pwe/Efd' ) print('') print('Contact: erg_pwe_info at isee.nagoya-u.ac.jp') print( '**************************************************************************' ) except: print('printing PI info and rules of the road was failed') if initial_notplot_flag or downloadonly: return loaded_data time_min_max = time_float(trange) if 'spin' in datatype: for elem in component: t_plot_name = prefix + elem + '_dsi' options(t_plot_name, 'ytitle', elem + ' vector in DSI') options(t_plot_name, 'legend_names', labels) # ylim settings because pytplot.timespan() doesn't affect in ylim. # May be it will be no need in future. get_data_vars = get_data(t_plot_name) if get_data_vars[0][0] < time_min_max[0]: min_time_index = np.where( (get_data_vars[0] <= time_min_max[0]))[0][-1] else: min_time_index = 0 if time_min_max[1] < get_data_vars[0][-1]: max_time_index = np.where( time_min_max[1] <= get_data_vars[0])[0][0] else: max_time_index = -1 ylim_min = np.nanmin( get_data_vars[1][min_time_index:max_time_index]) ylim_max = np.nanmax( get_data_vars[1][min_time_index:max_time_index]) ylim(t_plot_name, ylim_min, ylim_max) if ('64' in datatype) or ('256' in datatype) or (datatype == 'pot8Hz'): for elem in component: t_plot_name = prefix + elem get_data_vars = get_data(t_plot_name) dl_in = get_data(t_plot_name, metadata=True) time1 = get_data_vars[0] data = np.where(get_data_vars[1] <= -1e+30, np.nan, get_data_vars[1]) dt = get_data_vars[2] ndt = dt.size ndata = data.size time_new = (np.tile(time1, (ndt, 1)).T + dt * 1e-3).reshape(ndata) data_new = data.reshape(ndata) store_data(t_plot_name, data={ 'x': time_new, 'y': data_new }, attr_dict=dl_in) options(t_plot_name, 'ytitle', '\n'.join(t_plot_name.split('_'))) # ylim settings because pytplot.timespan() doesn't affect in ylim. # May be it will be no need in future. if time_new[0] < time_min_max[0]: min_time_index = np.where((time_new <= time_min_max[0]))[0][-1] else: min_time_index = 0 if time_min_max[1] < time_new[-1]: max_time_index = np.where(time_min_max[1] <= time_new)[0][0] else: max_time_index = -1 ylim_min = np.nanmin(data_new[min_time_index:max_time_index]) ylim_max = np.nanmax(data_new[min_time_index:max_time_index]) ylim(t_plot_name, ylim_min, ylim_max) if datatype == 'pot': for elem in component: t_plot_name = prefix + elem options(t_plot_name, 'ytitle', elem + ' potential') if datatype == 'spec': options(tnames(prefix + '*spectra*'), 'spec', 1) options(tnames(prefix + '*spectra*'), 'colormap', 'jet') options(tnames(prefix + '*spectra*'), 'zlog', 1) ylim(prefix + 'spectra', 0, 100) zlim(prefix + 'spectra', 1e-6, 1e-2) options(tnames(prefix + '*spectra*'), 'ysubtitle', '[Hz]') options(tnames(prefix + '*spectra*'), 'ztitle', '[mV^2/m^2/Hz]') for t_plot_name in (tnames(prefix + '*spectra*')): options(t_plot_name, 'ytitle', '\n'.join(t_plot_name.split('_'))) return loaded_data