def get_params(model): support_trange = [ time_double(trange[0]) - 60 * 60 * 24, time_double(trange[1]) + 60 * 60 * 24 ] pyspedas.kyoto.dst(trange=support_trange) pyspedas.omni.data(trange=trange) join_vec(['BX_GSE', 'BY_GSM', 'BZ_GSM']) return get_tsy_params('kyoto_dst', 'BX_GSE-BY_GSM-BZ_GSM_joined', 'proton_density', 'flow_speed', model, pressure_tvar='Pressure', speed=True)
def sts_to_tplot(sts_file=None, read_only=False, prefix='', suffix='', merge=True, notplot=False): """ Read in a given filename in situ file into a dictionary object Optional keywords maybe used to downselect instruments returned and the time windows. Input: filename: str/list of str The file names and full paths of STS files to be read and parsed. read_only: boolean If True, just reads data into dict and returns the dict. If False, loads data into dict and loads data in the dict into tplot variables. 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. Output: Either a dictionary (data structure) containing up to all of the columns included in a STS data file, or tplot variable names. """ # Create a dictionary and list in which we'll store STS variable data and variable names, respectively start_t = time.time() sts_dict = {} stored_variables = [] # Code assumes a list of STS files if isinstance(sts_file, str): sts_file = [sts_file] elif isinstance(sts_file, list): sts_file = sts_file else: print("Invalid filenames input.") return stored_variables sts_file.sort() for s_file in sts_file: column_names, vec_names = read_column_names(s_file) headers = [item for sublist in column_names for item in sublist] with open(s_file, 'r') as f: lines = f.readlines() # In STS files, the beginning of the data starts after the last time 'END_OBJECT' is found end_objects = [ l for l, line in enumerate(lines) if 'END_OBJECT' in line ] end_headers = end_objects[-1] data = lines[end_headers + 1:] data = [d.strip().split() for d in data ] # Remove extra spaces, then split on whitespaces # Create the STS dictionary for h, head in enumerate(headers): data_column = [d[h] for d in data] if head not in sts_dict: sts_dict[head] = data_column else: sts_dict[head].extend(data_column) # We need to create datetime objects from the sts_dict's year, doy, hour, min, sec, and msec data year = sts_dict['TIME_YEAR'] doy = sts_dict['TIME_DOY'] hour = sts_dict['TIME_HOUR'] min = sts_dict['TIME_MIN'] sec = sts_dict['TIME_SEC'] msec = sts_dict['TIME_MSEC'] dtimes = [ datetime.datetime(int(yr), 1, 1, int(hr), int(mn), int(s), int(ms), tzinfo=datetime.timezone.utc) + datetime.timedelta(int(dy) - 1) for yr, dy, hr, mn, s, ms in zip(year, doy, hour, min, sec, msec) ] sts_dict['time_unix'] = dtimes # These keys are no longer necessary, nix them remove_time_keys = [ 'TIME_YEAR', 'TIME_DOY', 'TIME_HOUR', 'TIME_MIN', 'TIME_SEC', 'TIME_MSEC' ] for key in remove_time_keys: try: sts_dict.pop(key) except KeyError: print('Key {} was not found'.format(key)) # Don't create tplot vars if that's not what's desired if read_only: return sts_dict for var_name in sts_dict.keys(): to_merge = False if var_name in pytplot.data_quants.keys() and merge: prev_data_quant = pytplot.data_quants[var_name] to_merge = True # create variable name obs_specific = prefix + var_name + suffix # if all values are NaN, continue if all(v is None for v in sts_dict[var_name]): continue # store data in tplot variable if var_name != 'time_unix': try: pytplot.store_data( obs_specific, data={ 'x': sts_dict['time_unix'], 'y': [np.float(val) for val in sts_dict[var_name]] }) except ValueError: continue if to_merge is True: cur_data_quant = pytplot.data_quants[var_name] plot_options = copy.deepcopy(pytplot.data_quants[var_name].attrs) pytplot.data_quants[var_name] = xr.concat( [prev_data_quant, cur_data_quant], dim='time').sortby('time') pytplot.data_quants[var_name].attrs = plot_options # Now merge vectors for cn, vn in zip(column_names, vec_names): if vn == 'TIME': continue if vn is not None: names_to_join = [] for c in cn: names_to_join.append(prefix + c + suffix) pytplot.join_vec(names_to_join, vn, merge=True) stored_variables.append(vn) pytplot.del_data(names_to_join) else: stored_variables.append(prefix + cn[0] + suffix) if notplot: return sts_dict return stored_variables
def mms_lingradest(fields=None, positions=None, suffix=''): """ """ if fields is None or positions is None: print('B-field and spacecraft position keywords required.') return # interpolate the magnetic field data all onto the same timeline (MMS1): # should be in GSE coordinates tinterpol(fields[1], fields[0], newname=fields[1] + '_i') tinterpol(fields[2], fields[0], newname=fields[2] + '_i') tinterpol(fields[3], fields[0], newname=fields[3] + '_i') # interpolate the definitive ephemeris onto the magnetic field timeseries # should be in GSE coordinates tinterpol(positions[0], fields[0], newname=positions[0] + '_i') tinterpol(positions[1], fields[0], newname=positions[1] + '_i') tinterpol(positions[2], fields[0], newname=positions[2] + '_i') tinterpol(positions[3], fields[0], newname=positions[3] + '_i') B1 = get_data(fields[0]) B2 = get_data(fields[1] + '_i') B3 = get_data(fields[2] + '_i') B4 = get_data(fields[3] + '_i') Bx1 = B1.y[:, 0] By1 = B1.y[:, 1] Bz1 = B1.y[:, 2] Bx2 = B2.y[:, 0] By2 = B2.y[:, 1] Bz2 = B2.y[:, 2] Bx3 = B3.y[:, 0] By3 = B3.y[:, 1] Bz3 = B3.y[:, 2] Bx4 = B4.y[:, 0] By4 = B4.y[:, 1] Bz4 = B4.y[:, 2] R1 = get_data(positions[0] + '_i') R2 = get_data(positions[1] + '_i') R3 = get_data(positions[2] + '_i') R4 = get_data(positions[3] + '_i') # start the calculation output = lingradest(Bx1, Bx2, Bx3, Bx4, By1, By2, By3, By4, Bz1, Bz2, Bz3, Bz4, R1.y, R2.y, R3.y, R4.y) # end of the calculations # store the results store_data('Bt' + suffix, data={'x': B1.times, 'y': output['Bbc']}) store_data('Bx' + suffix, data={'x': B1.times, 'y': output['Bxbc']}) store_data('By' + suffix, data={'x': B1.times, 'y': output['Bybc']}) store_data('Bz' + suffix, data={'x': B1.times, 'y': output['Bzbc']}) join_vec(['Bt' + suffix, 'Bx' + suffix, 'By' + suffix, 'Bz' + suffix], new_tvar='Bbc' + suffix) # B-field gradients store_data('gradBx' + suffix, data={'x': B1.times, 'y': output['LGBx']}) store_data('gradBy' + suffix, data={'x': B1.times, 'y': output['LGBy']}) store_data('gradBz' + suffix, data={'x': B1.times, 'y': output['LGBz']}) CB = np.sqrt(output['LCxB']**2 + output['LCyB']**2 + output['LCzB']**2) # in nT/1000km store_data('absCB' + suffix, data={'x': B1.times, 'y': CB}) store_data('CxB' + suffix, data={'x': B1.times, 'y': output['LCxB']}) store_data('CyB' + suffix, data={'x': B1.times, 'y': output['LCyB']}) store_data('CzB' + suffix, data={'x': B1.times, 'y': output['LCzB']}) store_data('divB_nT/1000km' + suffix, data={ 'x': B1.times, 'y': output['LD'] }) join_vec( ['absCB' + suffix, 'CxB' + suffix, 'CyB' + suffix, 'CzB' + suffix], new_tvar='curlB_nT/1000km' + suffix) # jx in nA/m^2 store_data('jx' + suffix, data={'x': B1.times, 'y': 0.8 * output['LCxB']}) # jy in nA/m^2 store_data('jy' + suffix, data={'x': B1.times, 'y': 0.8 * output['LCyB']}) # jz in nA/m^2 store_data('jz' + suffix, data={'x': B1.times, 'y': 0.8 * output['LCzB']}) join_vec(['jx' + suffix, 'jy' + suffix, 'jz' + suffix], new_tvar='jtotal' + suffix) store_data('curvx' + suffix, data={'x': B1.times, 'y': output['curv_x_B']}) store_data('curvy' + suffix, data={'x': B1.times, 'y': output['curv_y_B']}) store_data('curvz' + suffix, data={'x': B1.times, 'y': output['curv_z_B']}) join_vec(['curvx' + suffix, 'curvy' + suffix, 'curvz' + suffix], new_tvar='curvB' + suffix) store_data('Rc_1000km' + suffix, data={ 'x': B1.times, 'y': output['RcurvB'] })
def test_math(): pytplot.cdf_to_tplot(os.path.dirname(os.path.realpath(__file__)) + "/testfiles/mvn_euv_l2_bands_20170619_v09_r03.cdf") pytplot.tplot_names() pytplot.tplot_math.split_vec('mvn_euv_calib_bands') pytplot.tplot('mvn_euv_calib_bands_x', testing=True) pytplot.tplot_math.subtract('mvn_euv_calib_bands_x', 'mvn_euv_calib_bands_y', new_tvar='s') pytplot.tplot('s', testing=True) pytplot.tplot_math.add('s', 'mvn_euv_calib_bands_x', new_tvar='a') pytplot.tplot(['mvn_euv_calib_bands_x', 'a'], testing=True) pytplot.tplot_math.subtract('mvn_euv_calib_bands_x', 'mvn_euv_calib_bands_z', new_tvar='m') pytplot.tplot('m', testing=True) pytplot.tplot_math.divide('m', 'mvn_euv_calib_bands_z', new_tvar='d') pytplot.tplot('d', testing=True) pytplot.add_across('mvn_euv_calib_bands', new_tvar='data_summed') pytplot.tplot('mvn_euv_calib_bands', testing=True) pytplot.avg_res_data('data_summed', res=120) pytplot.tplot('data_summed', testing=True) pytplot.deflag('mvn_euv_calib_bands', 0, new_tvar='deflagged') pytplot.tplot('deflagged', testing=True) pytplot.flatten('mvn_euv_calib_bands') pytplot.tplot('data_flattened', testing=True) pytplot.join_vec(['mvn_euv_calib_bands_x', 'mvn_euv_calib_bands_y', 'mvn_euv_calib_bands_z'], new_tvar='data2') pytplot.tplot('data2', testing=True) pytplot.pwr_spec('mvn_euv_calib_bands_x') pytplot.tplot('mvn_euv_calib_bands_x_pwrspec', testing=True) pytplot.derive('mvn_euv_calib_bands_x') pytplot.store_data("data3", data=['mvn_euv_calib_bands_x', 'mvn_euv_calib_bands_y', 'mvn_euv_calib_bands_z']) pytplot.tplot('data3', testing=True) pytplot.cdf_to_tplot(os.path.dirname(os.path.realpath(__file__))+ "/testfiles/mvn_swe_l2_svyspec_20170619_v04_r04.cdf") pytplot.resample('mvn_euv_calib_bands_y', pytplot.data_quants['diff_en_fluxes'].coords['time'].values, new_tvar='data_3_resampled') pytplot.tplot('data_3_resampled', testing=True) pytplot.options('diff_en_fluxes', 'spec', 1) pytplot.spec_mult('diff_en_fluxes') pytplot.add_across('diff_en_fluxes_specmult', new_tvar='tot_en_flux', column_range=[[0, 10], [10, 20], [20, 30]]) pytplot.options('diff_en_fluxes', 'ylog', 1) pytplot.options('diff_en_fluxes', 'zlog', 1) pytplot.options('tot_en_flux', 'ylog', 1) pytplot.ylim('tot_en_flux', 1, 100) pytplot.tplot(['diff_en_fluxes', 'tot_en_flux'], testing=True) pytplot.split_vec('tot_en_flux') pytplot.add('tot_en_flux_x', 'mvn_euv_calib_bands_y', new_tvar='weird_data') pytplot.tplot('weird_data', testing=True)
def tplot_varcreate(insitu): """Creates tplot variables from the insitu variable """ # initialize each instrument created_vars = [] for obs in insitu["SPACECRAFT"]: obs_specific = "mvn_kp::spacecraft::" + obs.lower() try: pytplot.store_data(obs_specific, data={ 'x': insitu['Time'], 'y': insitu["SPACECRAFT"][obs] }) created_vars.append(obs_specific) except: pass # Join together the matricies and remove the individual points pytplot.join_vec([ 'mvn_kp::spacecraft::t11', 'mvn_kp::spacecraft::t12', 'mvn_kp::spacecraft::t13', 'mvn_kp::spacecraft::t21', 'mvn_kp::spacecraft::t22', 'mvn_kp::spacecraft::t23', 'mvn_kp::spacecraft::t31', 'mvn_kp::spacecraft::t32', 'mvn_kp::spacecraft::t33' ], 'mvn_kp::geo_to_mso_matrix') pytplot.del_data([ 'mvn_kp::spacecraft::t11', 'mvn_kp::spacecraft::t12', 'mvn_kp::spacecraft::t13', 'mvn_kp::spacecraft::t21', 'mvn_kp::spacecraft::t22', 'mvn_kp::spacecraft::t23', 'mvn_kp::spacecraft::t31', 'mvn_kp::spacecraft::t32', 'mvn_kp::spacecraft::t33' ]) pytplot.join_vec([ 'mvn_kp::spacecraft::spacecraft_t11', 'mvn_kp::spacecraft::spacecraft_t12', 'mvn_kp::spacecraft::spacecraft_t13', 'mvn_kp::spacecraft::spacecraft_t21', 'mvn_kp::spacecraft::spacecraft_t22', 'mvn_kp::spacecraft::spacecraft_t23', 'mvn_kp::spacecraft::spacecraft_t31', 'mvn_kp::spacecraft::spacecraft_t32', 'mvn_kp::spacecraft::spacecraft_t33' ], 'mvn_kp::spacecraft_to_mso_matrix') pytplot.del_data([ 'mvn_kp::spacecraft::spacecraft_t11', 'mvn_kp::spacecraft::spacecraft_t12', 'mvn_kp::spacecraft::spacecraft_t13', 'mvn_kp::spacecraft::spacecraft_t21', 'mvn_kp::spacecraft::spacecraft_t22', 'mvn_kp::spacecraft::spacecraft_t23', 'mvn_kp::spacecraft::spacecraft_t31', 'mvn_kp::spacecraft::spacecraft_t32', 'mvn_kp::spacecraft::spacecraft_t33' ]) created_vars.remove('mvn_kp::spacecraft::t11') created_vars.remove('mvn_kp::spacecraft::t12') created_vars.remove('mvn_kp::spacecraft::t13') created_vars.remove('mvn_kp::spacecraft::t21') created_vars.remove('mvn_kp::spacecraft::t22') created_vars.remove('mvn_kp::spacecraft::t23') created_vars.remove('mvn_kp::spacecraft::t31') created_vars.remove('mvn_kp::spacecraft::t32') created_vars.remove('mvn_kp::spacecraft::t33') created_vars.remove('mvn_kp::spacecraft::spacecraft_t11') created_vars.remove('mvn_kp::spacecraft::spacecraft_t12') created_vars.remove('mvn_kp::spacecraft::spacecraft_t13') created_vars.remove('mvn_kp::spacecraft::spacecraft_t21') created_vars.remove('mvn_kp::spacecraft::spacecraft_t22') created_vars.remove('mvn_kp::spacecraft::spacecraft_t23') created_vars.remove('mvn_kp::spacecraft::spacecraft_t31') created_vars.remove('mvn_kp::spacecraft::spacecraft_t32') created_vars.remove('mvn_kp::spacecraft::spacecraft_t33') inst_list = ["EUV", "LPW", "STATIC", "SWEA", "SWIA", "MAG", "SEP", "NGIMS"] for instrument in inst_list: # for each observation for each instrument if instrument in insitu: if insitu[instrument] is not None: for obs in insitu[instrument]: # create variable name obs_specific = "mvn_kp::" + instrument.lower( ) + "::" + obs.lower() try: # store data in tplot variable pytplot.store_data(obs_specific, data={ 'x': insitu['Time'], 'y': insitu[instrument][obs] }) created_vars.append(obs_specific) pytplot.link(obs_specific, "mvn_kp::spacecraft::altitude", link_type='alt') pytplot.link(obs_specific, "mvn_kp::spacecraft::mso_x", link_type='x') pytplot.link(obs_specific, "mvn_kp::spacecraft::mso_y", link_type='y') pytplot.link(obs_specific, "mvn_kp::spacecraft::mso_z", link_type='z') pytplot.link(obs_specific, "mvn_kp::spacecraft::geo_x", link_type='geo_x') pytplot.link(obs_specific, "mvn_kp::spacecraft::geo_y", link_type='geo_y') pytplot.link(obs_specific, "mvn_kp::spacecraft::geo_z", link_type='geo_z') pytplot.link(obs_specific, "mvn_kp::spacecraft::sub_sc_longitude", link_type='lon') pytplot.link(obs_specific, "mvn_kp::spacecraft::sub_sc_latitude", link_type='lat') except: pass return created_vars
print(pytplot.data_quants['d_ff'].data) print('flatten') pytplot.flatten('d', 8, 14, 'd_flatten') print(pytplot.data_quants['d_flatten'].data) print('interp_nan') pytplot.interp_nan('e', 'e_nonan', s_limit=5) print(pytplot.data_quants['e_nonan'].data) print('interpolate') pytplot.tinterp('a', 'c', interp='cubic') print(pytplot.data_quants['c_interp'].data) print('join_vec') pytplot.join_vec(['d', 'e', 'g'], 'deg') print(pytplot.data_quants['deg'].data) print('multiply') pytplot.multiply('a', 'c', 'ac', interp='linear') print(pytplot.data_quants['ac'].data) print('resample') # pytplot.resample('d',[3,4,5,6,7,18],'d_resampled') # print(pytplot.data_quants['d_resampled'].data) pytplot.resample('h', [3, 4, 5, 6, 7, 18], 'h_resampled') print(pytplot.data_quants['h_resampled'].data) print(pytplot.data_quants['h_resampled'].spec_bins) print('spec_mult') pytplot.spec_mult('h', 'h_specmult')