Exemple #1
0
def mms_eis_set_metadata(tplotnames,
                         data_rate='srvy',
                         datatype='extof',
                         suffix=''):
    """
    This function updates the metadata for the EIS data products
    
    Parameters:
        tplotnames : list of str
            list of tplot variables loaded by the load routine

        data_rate : str 
            Data rate

        datatype : str 
            EIS datatype loaded (extof or phxtof)
            
        suffix: str
            The tplot variable names will be given this suffix.  By default, 
            no suffix is added.

    """
    if datatype == 'extof':
        options(tnames('*_extof_proton_flux_omni*'), 'yrange', [55, 1000])
        options(tnames('*_extof_alpha_flux_omni*'), 'yrange', [80, 650])
        options(tnames('*_extof_oxygen_flux_omni*'), 'yrange', [145, 950])
Exemple #2
0
def tcopy(names_in, names_out=None, suffix=None):

    names_in = pyspedas.tnames(names_in)
    if len(names_in) < 1:
        print('tcopy error: No pytplot variables found.')
        return

    if suffix is None:
        suffix = '-copy'

    if names_out is None:
        names_out = [s + suffix for s in names_in]

    if isinstance(names_out, str):
        names_out = [names_out]

    if len(names_in) != len(names_out):
        print('tcopy error: List with the names_in does not match list\
              with the names out.')
        return

    for i in range(len(names_in)):
        n = names_in[i]
        o = names_out[i]
        if len(pyspedas.tnames(n)) == 1:
            tcopy_one(n, o)
        else:
            print('tplot name not found: ' + n)
def mms_hpca_set_metadata(probe='1', fov=[0, 360], suffix=''):
    """
    This function sets the plot metadata for HPCA data products, and is meant 
    to be called from the HPCA load routine

    Parameters:
        fov : list of int
            field of view, in angles, from 0-360

        probe : str
            probe #, e.g., '4' for MMS4
            
        suffix: str
            suffix of the loaded data

    Returns:
        None
    """
    prefix = 'mms'+str(probe)
    valid_density = prefix+'*_number_density'+suffix
    valid_vel = prefix+'*_ion_bulk_velocity*'+suffix
    valid_temp = prefix+'*_scalar_temperature'+suffix

    for var in tnames(valid_density):
        if var == prefix+'_hpca_hplus_number_density'+suffix: options(var, 'ytitle', 'H+ density')
        if var == prefix+'_hpca_heplus_number_density'+suffix: options(var, 'ytitle', 'He+ density')
        if var == prefix+'_hpca_heplusplus_number_density'+suffix: options(var, 'ytitle', 'He++ density')
        if var == prefix+'_hpca_oplus_number_density'+suffix: options(var, 'ytitle', 'O+ density')
        if var == prefix+'_hpca_oplusplus_number_density'+suffix: options(var, 'ytitle', 'O++ density')

    for var in tnames(valid_vel):
        if var == prefix+'_hpca_hplus_ion_bulk_velocity'+suffix:
            options(var, 'legend_names', ['Vx (H+)', 'Vy (H+)', 'Vz (H+)'])
            options(var, 'color', ['b', 'g', 'r'])
            options(var, 'ytitle', 'H+ velocity')
        if var == prefix+'_hpca_heplus_ion_bulk_velocity'+suffix:
            options(var, 'legend_names', ['Vx (He+)', 'Vy (He+)', 'Vz (He+)'])
            options(var, 'color', ['b', 'g', 'r'])
            options(var, 'ytitle', 'He+ velocity')
        if var == prefix+'_hpca_heplusplus_ion_bulk_velocity'+suffix:
            options(var, 'legend_names', ['Vx (He++)', 'Vy (He++)', 'Vz (He++)'])
            options(var, 'color', ['b', 'g', 'r'])
            options(var, 'ytitle', 'He++ velocity')
        if var == prefix+'_hpca_oplus_ion_bulk_velocity'+suffix:
            options(var, 'legend_names', ['Vx (O+)', 'Vy (O+)', 'Vz (O+)'])
            options(var, 'color', ['b', 'g', 'r'])
            options(var, 'ytitle', 'O+ velocity')
        if var == prefix+'_hpca_oplusplus_ion_bulk_velocity'+suffix:
            options(var, 'legend_names', ['Vx (O++)', 'Vy (O++)', 'Vz (O++)'])
            options(var, 'color', ['b', 'g', 'r'])
            options(var, 'ytitle', 'O++ velocity')

    for var in tnames(valid_temp):
        if var == prefix+'_hpca_hplus_scalar_temperature'+suffix: options(var, 'ytitle', 'H+ temp')
        if var == prefix+'_hpca_heplus_scalar_temperature'+suffix: options(var, 'ytitle', 'He+ temp')
        if var == prefix+'_hpca_heplusplus_scalar_temperature'+suffix: options(var, 'ytitle', 'He++ temp')
        if var == prefix+'_hpca_oplus_scalar_temperature'+suffix: options(var, 'ytitle', 'O+ temp')
        if var == prefix+'_hpca_oplusplus_scalar_temperature'+suffix: options(var, 'ytitle', 'O++ temp')
Exemple #4
0
def subtract_average(names, new_names=None, suffix=None, overwrite=None,
                     median=None):

    old_names = pyspedas.tnames(names)

    if len(old_names) < 1:
        print('Subtract Average error: No pytplot names were provided.')
        return

    if suffix is None:
        if median:
            suffix = '-m'
        else:
            suffix = '-d'

    if overwrite is not None:
        n_names = old_names
    elif new_names is None:
        n_names = [s + suffix for s in old_names]
    else:
        n_names = new_names

    if isinstance(n_names, str):
        n_names = [n_names]

    if len(n_names) != len(old_names):
        n_names = [s + suffix for s in old_names]

    old_names = pyspedas.tnames(names)

    for i, old in enumerate(old_names):
        new = n_names[i]

        if new != old:
            pyspedas.tcopy(old, new)

        data = pytplot.data_quants[new].values
        dim = data.shape
        if median:
            if len(dim) == 1:
                data -= numpy.median(data, axis=0)
            else:
                for i in range(dim[1]):
                    data[:, i] -= numpy.median(data[:, i], axis=0)
            ptype = 'Median'
        else:
            if len(dim) == 1:
                data -= numpy.mean(data, axis=0)
            else:
                for i in range(dim[1]):
                    data[:, i] -= numpy.mean(data[:, i], axis=0)
            ptype = 'Mean'

        pytplot.data_quants[new].values = data

        print('Subtract ' + ptype + ' was applied to: ' + new)
Exemple #5
0
def mms_eis_set_metadata(tplotnames,
                         data_rate='srvy',
                         datatype='extof',
                         suffix=''):
    """

    """
    if datatype == 'extof':
        options(tnames('*_extof_proton_flux_omni*'), 'yrange', [55, 1000])
        options(tnames('*_extof_alpha_flux_omni*'), 'yrange', [80, 650])
        options(tnames('*_extof_oxygen_flux_omni*'), 'yrange', [145, 950])
Exemple #6
0
def mms_hpca_set_metadata(probe='1', fov=[0, 360], suffix=''):
    prefix = 'mms' + str(probe)
    valid_density = prefix + '*_number_density' + suffix
    valid_vel = prefix + '*_ion_bulk_velocity*' + suffix
    valid_temp = prefix + '*_scalar_temperature' + suffix

    for var in tnames(valid_density):
        if var == prefix + '_hpca_hplus_number_density' + suffix:
            options(var, 'ytitle', 'H+ density')
        if var == prefix + '_hpca_heplus_number_density' + suffix:
            options(var, 'ytitle', 'He+ density')
        if var == prefix + '_hpca_heplusplus_number_density' + suffix:
            options(var, 'ytitle', 'He++ density')
        if var == prefix + '_hpca_oplus_number_density' + suffix:
            options(var, 'ytitle', 'O+ density')
        if var == prefix + '_hpca_oplusplus_number_density' + suffix:
            options(var, 'ytitle', 'O++ density')

    for var in tnames(valid_vel):
        if var == prefix + '_hpca_hplus_ion_bulk_velocity' + suffix:
            options(var, 'legend_names', ['Vx (H+)', 'Vy (H+)', 'Vz (H+)'])
            options(var, 'color', ['b', 'g', 'r'])
            options(var, 'ytitle', 'H+ velocity')
        if var == prefix + '_hpca_heplus_ion_bulk_velocity' + suffix:
            options(var, 'legend_names', ['Vx (He+)', 'Vy (He+)', 'Vz (He+)'])
            options(var, 'color', ['b', 'g', 'r'])
            options(var, 'ytitle', 'He+ velocity')
        if var == prefix + '_hpca_heplusplus_ion_bulk_velocity' + suffix:
            options(var, 'legend_names',
                    ['Vx (He++)', 'Vy (He++)', 'Vz (He++)'])
            options(var, 'color', ['b', 'g', 'r'])
            options(var, 'ytitle', 'He++ velocity')
        if var == prefix + '_hpca_oplus_ion_bulk_velocity' + suffix:
            options(var, 'legend_names', ['Vx (O+)', 'Vy (O+)', 'Vz (O+)'])
            options(var, 'color', ['b', 'g', 'r'])
            options(var, 'ytitle', 'O+ velocity')
        if var == prefix + '_hpca_oplusplus_ion_bulk_velocity' + suffix:
            options(var, 'legend_names', ['Vx (O++)', 'Vy (O++)', 'Vz (O++)'])
            options(var, 'color', ['b', 'g', 'r'])
            options(var, 'ytitle', 'O++ velocity')

    for var in tnames(valid_temp):
        if var == prefix + '_hpca_hplus_scalar_temperature' + suffix:
            options(var, 'ytitle', 'H+ temp (eV)')
        if var == prefix + '_hpca_heplus_scalar_temperature' + suffix:
            options(var, 'ytitle', 'He+ temp (eV)')
        if var == prefix + '_hpca_heplusplus_scalar_temperature' + suffix:
            options(var, 'ytitle', 'He++ temp (eV)')
        if var == prefix + '_hpca_oplus_scalar_temperature' + suffix:
            options(var, 'ytitle', 'O+ temp (eV)')
        if var == prefix + '_hpca_oplusplus_scalar_temperature' + suffix:
            options(var, 'ytitle', 'O++ temp (eV)')
Exemple #7
0
def mms_hpca_calc_anodes(fov=[0, 360], probe='1', suffix=''):
    """
    This function will sum (or average, for flux) the HPCA data over the requested field-of-view (fov)
    Parameters:
        fov : list of int
            field of view, in angles, from 0-360
        probe : str
            probe #, e.g., '4' for MMS4
        suffix: str
            suffix of the loaded data

    Returns:
        List of tplot variables created.
    """
    sum_anodes = [a+suffix for a in ['*_count_rate', '*_RF_corrected', '*_bkgd_corrected', '*_norm_counts']]
    avg_anodes = ['*_flux'+suffix]
    output_vars = []

    fov_str = '_elev_'+str(fov[0])+'-'+str(fov[1])

    for sum_anode in sum_anodes:
        vars_to_sum = tnames(sum_anode)

        for var in vars_to_sum:
            times, data, angles, energies = get_data(var)

            updated_spectra = mms_hpca_sum_fov(times, data, angles, energies, fov=fov)

            store_data(var+fov_str, data={'x': times, 'y': updated_spectra, 'v': energies})
            options(var+fov_str, 'spec', True)
            options(var+fov_str, 'ylog', True)
            options(var+fov_str, 'zlog', True)
            options(var+fov_str, 'Colormap', 'jet')
            output_vars.append(var+fov_str)

    for avg_anode in avg_anodes:
        vars_to_avg = tnames(avg_anode)

        for var in vars_to_avg:
            times, data, angles, energies = get_data(var)

            updated_spectra = mms_hpca_avg_fov(times, data, angles, energies, fov=fov)

            store_data(var+fov_str, data={'x': times, 'y': updated_spectra, 'v': energies})
            options(var+fov_str, 'spec', True)
            options(var+fov_str, 'ylog', True)
            options(var+fov_str, 'zlog', True)
            options(var+fov_str, 'Colormap', 'jet')
            output_vars.append(var+fov_str)
Exemple #8
0
def tinterpol(names, interp_names=None, method=None, new_names=None,
              suffix=None):

    old_names = pyspedas.tnames(names)

    if len(old_names) < 1:
        print('tinterpol error: No pytplot names were provided.')
        return

    if suffix is None:
        suffix = '-itrp'

    if method is None:
        method = 'linear'

    if new_names is None:
        n_names = [s + suffix for s in old_names]
    elif new_names == '':
        n_names = old_names
    else:
        n_names = new_names

    if len(n_names) != len(old_names):
        n_names = [s + suffix for s in old_names]

    for i in range(len(old_names)):
        time, data = pytplot.get_data(old_names[i])
        new_time, data1 = pytplot.get_data(interp_names[i])
        data = numpy.asarray(data).squeeze()
        f2 = interp1d(time, data, kind=method)
        new_data = f2(new_time)
        pytplot.store_data(n_names[i], data={'x': new_time, 'y': new_data})
        print('tinterpol (' + method + ') was applied to: ' + n_names[i])
Exemple #9
0
def deriv_data(names, new_names=None, suffix=None, overwrite=None):

    old_names = pyspedas.tnames(names)

    if len(old_names) < 1:
        print('avg_data error: No pytplot names were provided.')
        return

    if suffix is None:
        suffix = '-der'

    if overwrite is not None:
        n_names = old_names
    elif new_names is None:
        n_names = [s + suffix for s in old_names]
    else:
        n_names = new_names

    if isinstance(n_names, str):
        n_names = [n_names]

    if len(n_names) != len(old_names):
        n_names = [s + suffix for s in old_names]

    for i, old in enumerate(old_names):
        new = n_names[i]

        if new != old:
            pyspedas.tcopy(old, new)

        data = pytplot.data_quants[new]
        data_new = data.differentiate('time').copy()
        pytplot.data_quants[new].values = data_new.values

        print('deriv_data was applied to: ' + new)
Exemple #10
0
def tdeflag(names, method=None, new_names=None, suffix=None, overwrite=None):
    """
    Remove NaNs from tplot variables.

    Parameters
    ----------
    names: str/list of str
        List of pytplot names.
    method: str, optional
        Method to apply. Default is 'remove_nan.
        Other options 'repeat' (repeat last good value).
    new_names: str/list of str, optional
        List of new_names for pytplot variables.
        If '', then pytplot variables are replaced.
        If not given, then a suffix is applied.
    suffix: str, optional
        A suffix to apply. Default is '-clip'.
    overwrite: bool, optional
        Replace the existing tplot name.

    Returns
    -------
    None.

    """
    old_names = pyspedas.tnames(names)

    if len(old_names) < 1:
        print('tdeflag error: No pytplot names were provided.')
        return

    if suffix is None:
        suffix = '-deflag'

    if overwrite is not None:
        n_names = old_names
    elif new_names is None:
        n_names = [s + suffix for s in old_names]
    else:
        n_names = new_names

    if isinstance(n_names, str):
        n_names = [n_names]

    if len(n_names) != len(old_names):
        n_names = [s + suffix for s in old_names]

    for i in range(len(old_names)):
        alldata = pytplot.get_data(old_names[i])
        time = alldata[0]
        data = alldata[1]
        new_time = []
        new_data = []
        for j in range(len(time)):
            if not numpy.isnan(data[j]):
                new_time.append(time[j])
                new_data.append(data[j])
        pytplot.store_data(n_names[i], data={'x': new_time, 'y': new_data})

        print('tdeflag was applied to: ' + n_names[i])
Exemple #11
0
def subtract_median(names, new_names=None, suffix=None):

    old_names = pyspedas.tnames(names)

    if len(old_names) < 1:
        print('Subtract Median error: No pytplot names were provided.')
        return

    if suffix is None:
        suffix = '-m'

    if new_names is None:
        n_names = [s + suffix for s in old_names]
    elif new_names == '':
        n_names = old_names
    else:
        n_names = new_names

    if len(n_names) != len(old_names):
        n_names = [s + suffix for s in old_names]

    for i in range(len(old_names)):
        alldata = pytplot.get_data(old_names[i])
        time = alldata[0]
        data = alldata[1]
        new_data = data-numpy.median(data, axis=0)
        pytplot.store_data(n_names[i], data={'x': time, 'y': new_data})
        print('Subtract Median was applied to: ' + n_names[i])
Exemple #12
0
def tclip(names, ymin, ymax, flag=None, new_names=None, suffix=None):

    old_names = pyspedas.tnames(names)

    if len(old_names) < 1:
        print('tclip error: No pytplot names were provided.')
        return

    if suffix is None:
        suffix = '-clip'

    if flag is None:
        flag = float('nan')

    if new_names is None:
        n_names = [s + suffix for s in old_names]
    elif new_names == '':
        n_names = old_names
    else:
        n_names = new_names

    if len(n_names) != len(old_names):
        n_names = [s + suffix for s in old_names]

    for i in range(len(old_names)):
        alldata = pytplot.get_data(old_names[i])
        time = alldata[0]
        data = alldata[1]
        new_data = numpy.array(data)
        new_data[new_data <= ymin] = flag
        new_data[new_data >= ymax] = flag
        pytplot.store_data(n_names[i], data={'x': time, 'y': new_data})
        print('tclip was applied to: ' + n_names[i])
Exemple #13
0
def yclip(names, ymin, ymax, flag=None, new_names=None, suffix=None,
          overwrite=None):
    """
    Clip data values.

    Parameters
    ----------
    names: str/list of str
        List of pytplot names.
    ymin: float
        Minimum value to clip.
    ymax: float
        Maximum value to clip.
    flag: float, optional
        Values outside (ymin, ymax) are replaced with flag.
        Default is float('nan').
    new_names: str/list of str, optional
        List of new_names for pytplot variables.
        If not given, then a suffix is applied.
    suffix: str, optional
        A suffix to apply. Default is '-clip'.
    overwrite: bool, optional
        Replace the existing tplot name.

    Returns
    -------
    None.

    """
    old_names = pyspedas.tnames(names)

    if len(old_names) < 1:
        print('yclip error: No pytplot names were provided.')
        return

    if suffix is None:
        suffix = '-clip'

    if flag is None:
        flag = np.nan

    if overwrite is not None:
        n_names = old_names
    elif new_names is None:
        n_names = [s + suffix for s in old_names]
    else:
        n_names = new_names

    if isinstance(n_names, str):
        n_names = [n_names]

    if len(n_names) != len(old_names):
        n_names = [s + suffix for s in old_names]

    for i, old in enumerate(old_names):
        new = n_names[i]

        data = pytplot.clip(old, ymin, ymax, new)

        print('yclip was applied to: ' + new)
Exemple #14
0
def tdeflag(names, method=None, flag=None, new_names=None, suffix=None):

    old_names = pyspedas.tnames(names)

    if len(old_names) < 1:
        print('tdeflag error: No pytplot names were provided.')
        return

    if suffix is None:
        suffix = '-deflag'

    if flag is None:
        flag = float('nan')

    if new_names is None:
        n_names = [s + suffix for s in old_names]
    elif new_names == '':
        n_names = old_names
    else:
        n_names = new_names

    if len(n_names) != len(old_names):
        n_names = [s + suffix for s in old_names]

    for i in range(len(old_names)):
        time, data = pytplot.get_data(old_names[i])
        new_time = []
        new_data = []
        for j in range(len(time)):
            if not numpy.isnan(data[j]):
                new_time.append(time[j])
                new_data.append(data[j])
        pytplot.store_data(n_names[i], data={'x': new_time, 'y': new_data})
        print('tdeflag was applied to: ' + n_names[i])
Exemple #15
0
def ex_gmag(plot=True):
    """Demonstrate how to use gmag functions."""
    # Delete any existing pytplot variables
    pytplot.del_data()

    # Define a time rage as a list
    trange = ['2015-12-31', '2015-12-31']

    # Get a list of EPO gmag stations
    sites = pyspedas.themis.ground.gmag.gmag_list('epo')

    # Download gmag files and load data into pytplot variables
    pyspedas.themis.gmag(sites=sites, trange=trange)

    # Get a list of loaded sites
    sites_loaded = pyspedas.tnames()

    # Subtract mean values
    pyspedas.subtract_average(sites_loaded, '')

    # Download AE index data
    # pyspedas.load_data('gmag', time_list, ['idx'], '', '')
    pyspedas.themis.gmag(sites='idx', trange=trange)

    # Plot
    sites_loaded = pytplot.tplot_names()
    pytplot.tplot_options('title', 'EPO GMAG 2015-12-31')

    if plot:
        pytplot.tplot(sites_loaded)

    # Return 1 as indication that the example finished without problems.
    return 1
def mms_fgm_remove_flags(probe, data_rate, level, instrument, suffix=''):
    """
    This function removes data flagged by the FGM 'flag' variable (flags > 0), 
    in order to only show science quality data by default.
    
    Parameters:
        probe : str or list of str
            probe or list of probes, valid values for MMS probes are ['1','2','3','4']. 

        data_rate : str or list of str
            instrument data rates for FGM include 'brst' 'fast' 'slow' 'srvy'. The
            default is 'srvy'.

        level : str
            indicates level of data processing. the default if no level is specified is 'l2'

        instrument : str
            instrument; probably 'fgm'
            
        suffix: str
            The tplot variable names will be given this suffix.  By default, 
            no suffix is added.

    """
    if not isinstance(probe, list): probe = [probe]
    if not isinstance(data_rate, list): data_rate = [data_rate]
    if not isinstance(level, list): level = [level]

    tplot_vars = set(tnames())

    for this_probe in probe:
        for this_dr in data_rate:
            for this_lvl in level:
                if level == 'ql':
                    flag_var = 'mms' + str(
                        this_probe
                    ) + '_' + instrument + '_' + this_dr + '_' + this_lvl + '_flag' + suffix
                else:
                    flag_var = 'mms' + str(
                        this_probe
                    ) + '_' + instrument + '_flag_' + this_dr + '_' + this_lvl + suffix
                flagged = get_data(flag_var)
                if flagged == None:
                    continue
                times, flags = flagged
                flagged_data = np.where(flags != 0.0)[0]

                for var_specifier in [
                        '_b_gse_', '_b_gsm_', '_b_dmpa_', '_b_bcs_'
                ]:
                    var_name = 'mms' + str(
                        this_probe
                    ) + '_' + instrument + var_specifier + this_dr + '_' + this_lvl + suffix
                    if var_name in tplot_vars:
                        times, var_data = get_data(var_name)
                        var_data[flagged_data] = np.nan
                        store_data(var_name, data={'x': times, 'y': var_data})
Exemple #17
0
def tcopy(names_in, names_out=None, suffix=None):
    """
    Copy a list of pytplot variables.

    Parameters
    ----------
    names_in : str/list of str
        List of pytplot names.
    names_out : str/list of str, optional
        List of pytplot names. The default is None.
    suffix : str, optional
        Suffix to apply to names_in. The default is '-copy'.

    Returns
    -------
    None.

    """
    names_in = pyspedas.tnames(names_in)
    if len(names_in) < 1:
        print('tcopy error: No pytplot variables found.')
        return

    if suffix is None:
        suffix = '-copy'

    if names_out is None:
        names_out = [s + suffix for s in names_in]

    if isinstance(names_out, str):
        names_out = [names_out]

    if len(names_in) != len(names_out):
        print('tcopy error: List with the names_in does not match list\
              with the names out.')
        return

    for i in range(len(names_in)):
        n = names_in[i]
        o = names_out[i]
        if len(pyspedas.tnames(n)) == 1:
            tcopy_one(n, o)
        else:
            print('tplot name not found: ' + n)
Exemple #18
0
def yclip(names,
          ymin,
          ymax,
          flag=None,
          new_names=None,
          suffix=None,
          overwrite=None):

    old_names = pyspedas.tnames(names)

    if len(old_names) < 1:
        print('yclip error: No pytplot names were provided.')
        return

    if suffix is None:
        suffix = '-clip'

    if flag is None:
        flag = np.nan

    if overwrite is not None:
        n_names = old_names
    elif new_names is None:
        n_names = [s + suffix for s in old_names]
    else:
        n_names = new_names

    if isinstance(n_names, str):
        n_names = [n_names]

    if len(n_names) != len(old_names):
        n_names = [s + suffix for s in old_names]

    for i, old in enumerate(old_names):
        new = n_names[i]

        if new != old:
            pyspedas.tcopy(old, new)

        data = pytplot.data_quants[new].values
        try:
            for j, v in enumerate(data):
                try:
                    iterator = enumerate(v)
                except TypeError:
                    if not np.isnan(v) and (v <= ymin or v >= ymax):
                        data[j] = flag
                else:
                    for k, s in iterator:
                        if not np.isnan(s) and (s <= ymin or s >= ymax):
                            data[j][k] = flag

        except TypeError:  # data Not itterable
            print("Cannot clip data.")

        print('yclip was applied to: ' + new)
Exemple #19
0
def tinterpol(names, interp_to, method=None, newname=None, suffix=None):

    if not isinstance(names, list):
        names = [names]
    if not isinstance(newname, list):
        newname = [newname]

    old_names = tnames(names)

    if len(old_names) < 1:
        print('tinterpol error: No pytplot names were provided.')
        return

    if suffix == None:
        suffix = '-itrp'

    if method == None:
        method = 'linear'

    if (newname == None) or (len(newname) == 1 and newname[0] == None):
        n_names = [s + suffix for s in old_names]
    elif newname == '':
        n_names = old_names
    else:
        n_names = newname

    interp_to_data = get_data(interp_to)

    if interp_to_data == None:
        print('Error, tplot variable: ' + interp_to + ' not found.')
        return

    interp_to_times = interp_to_data[0]

    for name_idx, name in enumerate(old_names):
        xdata = get_data(name, xarray=True)
        xdata_interpolated = xdata.interp({'time': interp_to_times},
                                          method=method)

        if 'spec_bins' in xdata.coords:
            store_data(n_names[name_idx],
                       data={
                           'x': interp_to_times,
                           'y': xdata_interpolated.values,
                           'v': xdata_interpolated.coords['spec_bins'].values
                       })
        else:
            store_data(n_names[name_idx],
                       data={
                           'x': interp_to_times,
                           'y': xdata_interpolated.values
                       })

        print('tinterpol (' + method + ') was applied to: ' +
              n_names[name_idx])
Exemple #20
0
def deriv_data(names, new_names=None, suffix=None, overwrite=None):
    """
    Compute the derivative.

    Parameters
    ----------
    names: str/list of str
        List of pytplot names.
    new_names: str/list of str, optional
        List of new_names for pytplot variables.
        If not given, then a suffix is applied.
    suffix: str, optional
        A suffix to apply. Default is '-avg'.
    overwrite: bool, optional
        Replace the existing tplot name.


    Returns
    -------
    None.

    """
    old_names = pyspedas.tnames(names)

    if len(old_names) < 1:
        print('deriv_data error: No pytplot names were provided.')
        return

    if suffix is None:
        suffix = '-der'

    if overwrite is not None:
        n_names = old_names
    elif new_names is None:
        n_names = [s + suffix for s in old_names]
    else:
        n_names = new_names

    if isinstance(n_names, str):
        n_names = [n_names]

    if len(n_names) != len(old_names):
        n_names = [s + suffix for s in old_names]

    for i, old in enumerate(old_names):
        new = n_names[i]

        if new != old:
            pyspedas.tcopy(old, new)

        data = pytplot.data_quants[new]
        data_new = data.differentiate('time').copy()
        pytplot.data_quants[new].values = data_new.values

        print('deriv_data was applied to: ' + new)
Exemple #21
0
def remove_duplicated_tframe(tvars=[]):

    tvars = tnames(tvars)

    if len(tvars) < 1:
        return

    for tvar in tvars:

        input_attr_dict = get_data(tvar, metadata=True)
        get_data_vars = get_data(tvar)

        unique_array, counts_array = np.unique(
            get_data_vars[0], return_counts=True)
        duplicate_time_indices = np.where(counts_array > 1)[0]
        if duplicate_time_indices.shape[0] > 0:  # duplication check
            delete_indices_array = np.array(
                [np.where(get_data_vars[0] == unique_array[index])[0][0]
                    for index in duplicate_time_indices])
            input_data_dictionary = {}
            input_data_dictionary['x'] = np.delete(
                get_data_vars[0], delete_indices_array, axis=0)
            input_data_dictionary['y'] = np.delete(
                get_data_vars[1], delete_indices_array, axis=0)

            # for v or v1, v2.. elements. not tested yet.
            if len(get_data_vars) >= 3:
                element_counts = len(get_data_vars)
                if element_counts == 3:
                    if get_data_vars[2].ndim >= 2:
                        input_data_dictionary['v'] = np.delete(
                            get_data_vars[2], delete_indices_array, axis=0)
                    elif get_data_vars[2].ndim == 1:
                        input_data_dictionary['v'] = get_data_vars[2]
                elif element_counts > 3:
                    for element_index in range(2, element_counts):
                        v_element_name = f'v{element_index-1}'
                        if get_data_vars[element_index].ndim >= 2:
                            input_data_dictionary[v_element_name] = np.delete(
                                get_data_vars[element_index], delete_indices_array,
                                axis=0)
                        elif get_data_vars[element_index].ndim == 1:
                            input_data_dictionary[v_element_name] = get_data_vars[element_index]

            store_data(tvar, data=input_data_dictionary,
                       attr_dict=input_attr_dict)

    return
Exemple #22
0
def mms_fgm_set_metadata(probe, data_rate, level, instrument, suffix=''):
    """
    This function updates the metadata for FGM data products
    
    Parameters:
        probe : str or list of str
            probe or list of probes, valid values for MMS probes are ['1','2','3','4']. 

        data_rate : str or list of str
            instrument data rates for FGM include 'brst' 'fast' 'slow' 'srvy'. The
            default is 'srvy'.

        level : str
            indicates level of data processing. the default if no level is specified is 'l2'

        instrument : str
            instrument; probably 'fgm'
            
        suffix: str
            The tplot variable names will be given this suffix.  By default, 
            no suffix is added.

    """
    if not isinstance(probe, list): probe = [probe]
    if not isinstance(data_rate, list): data_rate = [data_rate]
    if not isinstance(level, list): level = [level]

    tvars = set(tnames())

    for this_probe in probe:
        for this_dr in data_rate:
            for this_lvl in level:
                if 'mms'+str(this_probe)+'_'+instrument+'_b_gse_'+this_dr+'_'+this_lvl+suffix in tvars:
                    options('mms'+str(this_probe)+'_'+instrument+'_b_gse_'+this_dr+'_'+this_lvl+suffix, 'ytitle', 'MMS'+str(this_probe)+' FGM')
                    options('mms'+str(this_probe)+'_'+instrument+'_b_gse_'+this_dr+'_'+this_lvl+suffix, 'color', ['b', 'g', 'r', '#000000'])
                    options('mms'+str(this_probe)+'_'+instrument+'_b_gse_'+this_dr+'_'+this_lvl+suffix, 'legend_names', ['Bx GSE', 'By GSE', 'Bz GSE', 'B total'])
                if 'mms'+str(this_probe)+'_'+instrument+'_b_gsm_'+this_dr+'_'+this_lvl+suffix in tvars:
                    options('mms'+str(this_probe)+'_'+instrument+'_b_gsm_'+this_dr+'_'+this_lvl+suffix, 'ytitle', 'MMS'+str(this_probe)+' FGM')
                    options('mms'+str(this_probe)+'_'+instrument+'_b_gsm_'+this_dr+'_'+this_lvl+suffix, 'color', ['b', 'g', 'r', '#000000'])
                    options('mms'+str(this_probe)+'_'+instrument+'_b_gsm_'+this_dr+'_'+this_lvl+suffix, 'legend_names', ['Bx GSM', 'By GSM', 'Bz GSM', 'B total'])
                if 'mms'+str(this_probe)+'_'+instrument+'_b_dmpa_'+this_dr+'_'+this_lvl+suffix in tvars:
                    options('mms'+str(this_probe)+'_'+instrument+'_b_dmpa_'+this_dr+'_'+this_lvl+suffix, 'ytitle', 'MMS'+str(this_probe)+' FGM')
                    options('mms'+str(this_probe)+'_'+instrument+'_b_dmpa_'+this_dr+'_'+this_lvl+suffix, 'color', ['b', 'g', 'r', '#000000'])
                    options('mms'+str(this_probe)+'_'+instrument+'_b_dmpa_'+this_dr+'_'+this_lvl+suffix, 'legend_names', ['Bx DMPA', 'By DMPA', 'Bz DMPA', 'B total'])
                if 'mms'+str(this_probe)+'_'+instrument+'_b_bcs_'+this_dr+'_'+this_lvl+suffix in tvars:
                    options('mms'+str(this_probe)+'_'+instrument+'_b_bcs_'+this_dr+'_'+this_lvl+suffix, 'ytitle', 'MMS'+str(this_probe)+' FGM')
                    options('mms'+str(this_probe)+'_'+instrument+'_b_bcs_'+this_dr+'_'+this_lvl+suffix, 'color', ['b', 'g', 'r', '#000000'])
                    options('mms'+str(this_probe)+'_'+instrument+'_b_bcs_'+this_dr+'_'+this_lvl+suffix, 'legend_names', ['Bx BCS', 'By BCS', 'Bz BCS', 'B total'])
Exemple #23
0
def mms_edp_set_metadata(probe, data_rate, level, suffix=''):
    """
    This function updates the metadata for EDP data products
    
    Parameters:
        probe : str or list of str
            probe or list of probes, valid values for MMS probes are ['1','2','3','4']. 

        data_rate : str or list of str
            instrument data rate for EDP

        level : str
            indicates level of data processing. the default if no level is specified is 'l2'

        suffix: str
            The tplot variable names will be given this suffix.  By default, 
            no suffix is added.

    """
    if not isinstance(probe, list): probe = [probe]
    if not isinstance(data_rate, list): data_rate = [data_rate]
    if not isinstance(level, list): level = [level]

    instrument = 'edp'

    tvars = set(tnames())

    for this_probe in probe:
        for this_dr in data_rate:
            for this_lvl in level:
                if 'mms'+str(this_probe)+'_'+instrument+'_dce_gse_'+this_dr+'_'+this_lvl+suffix in tvars:
                    options('mms'+str(this_probe)+'_'+instrument+'_dce_gse_'+this_dr+'_'+this_lvl+suffix, 'ytitle', 'MMS'+str(this_probe)+' EDP DCE [mV/m]')
                    options('mms'+str(this_probe)+'_'+instrument+'_dce_gse_'+this_dr+'_'+this_lvl+suffix, 'color', ['b', 'g', 'r'])
                    options('mms'+str(this_probe)+'_'+instrument+'_dce_gse_'+this_dr+'_'+this_lvl+suffix, 'legend_names', ['Ex GSE', 'Ey GSE', 'Ez GSE'])
                if 'mms'+str(this_probe)+'_'+instrument+'_dce_dsl_'+this_dr+'_'+this_lvl+suffix in tvars:
                    options('mms'+str(this_probe)+'_'+instrument+'_dce_dsl_'+this_dr+'_'+this_lvl+suffix, 'ytitle', 'MMS'+str(this_probe)+' EDP DCE [mV/m]')
                    options('mms'+str(this_probe)+'_'+instrument+'_dce_dsl_'+this_dr+'_'+this_lvl+suffix, 'color', ['b', 'g', 'r'])
                    options('mms'+str(this_probe)+'_'+instrument+'_dce_dsl_'+this_dr+'_'+this_lvl+suffix, 'legend_names', ['Ex DSL', 'Ey DSL', 'Ez DSL'])
                if 'mms'+str(this_probe)+'_'+instrument+'_hfesp_'+this_dr+'_'+this_lvl+suffix in tvars:
                    options('mms'+str(this_probe)+'_'+instrument+'_hfesp_'+this_dr+'_'+this_lvl+suffix, 'ytitle', 'MMS'+str(this_probe)+' EDP HFesp [Hz]')
                    options('mms'+str(this_probe)+'_'+instrument+'_hfesp_'+this_dr+'_'+this_lvl+suffix, 'ztitle', '(V/m)^2/Hz')
                    options('mms'+str(this_probe)+'_'+instrument+'_hfesp_'+this_dr+'_'+this_lvl+suffix, 'ylog', True)
                    options('mms'+str(this_probe)+'_'+instrument+'_hfesp_'+this_dr+'_'+this_lvl+suffix, 'zlog', True)
                    options('mms'+str(this_probe)+'_'+instrument+'_hfesp_'+this_dr+'_'+this_lvl+suffix, 'spec', True)
                    options('mms'+str(this_probe)+'_'+instrument+'_hfesp_'+this_dr+'_'+this_lvl+suffix, 'Colormap', 'jet')
Exemple #24
0
def tsmooth(names, width=10, median=None, preserve_nans=None,
            new_names=None, suffix=None, overwrite=None):

    old_names = pyspedas.tnames(names)

    if len(old_names) < 1:
        print('tsmooth error: No pytplot names were provided.')
        return

    if suffix is None:
        suffix = '-s'

    if overwrite is not None:
        n_names = old_names
    elif new_names is None:
        n_names = [s + suffix for s in old_names]
    else:
        n_names = new_names

    if isinstance(n_names, str):
        n_names = [n_names]

    if len(n_names) != len(old_names):
        n_names = [s + suffix for s in old_names]

    for i, old in enumerate(old_names):
        new = n_names[i]

        if new != old:
            pyspedas.tcopy(old, new)

        data = pytplot.data_quants[new].values

        dim = data.shape
        if len(dim) == 1:
            data = smooth(data, width=width, preserve_nans=preserve_nans)
        else:
            for k in range(dim[1]):
                data[:, k] = smooth(data[:, k], width=width,
                                    preserve_nans=preserve_nans)

        pytplot.data_quants[new].values = data

        print('tsmooth was applied to: ' + new)
Exemple #25
0
def mms_feeps_correct_energies(probes, data_rate, level='l2', suffix=''):

    types = ['top', 'bottom']
    sensors = range(1, 13)
    units_types = ['intensity', 'count_rate']

    for probe in probes:
        for sensor_type in types:
            for sensor in sensors:
                if sensor >= 6 and sensor <= 8:
                    species = 'ion'
                else:
                    species = 'electron'

                for units in units_types:
                    var_name = tnames('mms' + probe + '_epd_feeps_' +
                                      data_rate + '_' + level + '_' + species +
                                      '_' + sensor_type + '_' + units +
                                      '_sensorid_' + str(sensor))

                    if var_name == []:
                        continue
                    else:
                        var_name = var_name[0]

                    var_data = get_data(var_name + suffix)
                    if var_data is not None:
                        times, data, energies = var_data
                    else:
                        continue

                    energy_map = mms_feeps_energy_table(
                        probe, sensor_type[0:3], sensor)

                    try:
                        store_data(var_name + suffix,
                                   data={
                                       'x': times,
                                       'y': data,
                                       'v': energy_map
                                   })
                    except:
                        continue
Exemple #26
0
def mms_edi_set_metadata(probe, data_rate, level, suffix=''):
    """
    This function updates the metadata for EDI data products
    
    Parameters
    ----------
        probe : str or list of str
            probe or list of probes, valid values for MMS probes are ['1','2','3','4']. 

        data_rate : str or list of str
            instrument data rate for EDI

        level : str
            indicates level of data processing. the default if no level is specified is 'l2'

        suffix: str
            The tplot variable names will be given this suffix.  By default, 
            no suffix is added.

    """
    if not isinstance(probe, list): probe = [probe]
    if not isinstance(data_rate, list): data_rate = [data_rate]
    if not isinstance(level, list): level = [level]

    instrument = 'edi'

    tvars = set(tnames())

    for this_probe in probe:
        for this_dr in data_rate:
            for this_lvl in level:
                if 'mms' + str(
                        this_probe
                ) + '_' + instrument + '_vdrift_dsl_' + this_dr + '_' + this_lvl + suffix in tvars:
                    options(
                        'mms' + str(this_probe) + '_' + instrument +
                        '_vdrift_dsl_' + this_dr + '_' + this_lvl + suffix,
                        'ytitle',
                        'MMS' + str(this_probe) + ' EDI drift velocity')
                    options(
                        'mms' + str(this_probe) + '_' + instrument +
                        '_vdrift_dsl_' + this_dr + '_' + this_lvl + suffix,
                        'color', ['b', 'g', 'r'])
                    options(
                        'mms' + str(this_probe) + '_' + instrument +
                        '_vdrift_dsl_' + this_dr + '_' + this_lvl + suffix,
                        'legend_names', ['Vx DSL', 'Vy DSL', 'Vz DSL'])
                if 'mms' + str(
                        this_probe
                ) + '_' + instrument + '_vdrift_gse_' + this_dr + '_' + this_lvl + suffix in tvars:
                    options(
                        'mms' + str(this_probe) + '_' + instrument +
                        '_vdrift_gse_' + this_dr + '_' + this_lvl + suffix,
                        'ytitle',
                        'MMS' + str(this_probe) + ' EDI drift velocity')
                    options(
                        'mms' + str(this_probe) + '_' + instrument +
                        '_vdrift_gse_' + this_dr + '_' + this_lvl + suffix,
                        'color', ['b', 'g', 'r'])
                    options(
                        'mms' + str(this_probe) + '_' + instrument +
                        '_vdrift_gse_' + this_dr + '_' + this_lvl + suffix,
                        'legend_names', ['Vx GSE', 'Vy GSE', 'Vz GSE'])
                if 'mms' + str(
                        this_probe
                ) + '_' + instrument + '_vdrift_gsm_' + this_dr + '_' + this_lvl + suffix in tvars:
                    options(
                        'mms' + str(this_probe) + '_' + instrument +
                        '_vdrift_gsm_' + this_dr + '_' + this_lvl + suffix,
                        'ytitle',
                        'MMS' + str(this_probe) + ' EDI drift velocity')
                    options(
                        'mms' + str(this_probe) + '_' + instrument +
                        '_vdrift_gsm_' + this_dr + '_' + this_lvl + suffix,
                        'color', ['b', 'g', 'r'])
                    options(
                        'mms' + str(this_probe) + '_' + instrument +
                        '_vdrift_gsm_' + this_dr + '_' + this_lvl + suffix,
                        'legend_names', ['Vx GSM', 'Vy GSM', 'Vz GSM'])
                if 'mms' + str(
                        this_probe
                ) + '_' + instrument + '_e_dsl_' + this_dr + '_' + this_lvl + suffix in tvars:
                    options(
                        'mms' + str(this_probe) + '_' + instrument +
                        '_e_dsl_' + this_dr + '_' + this_lvl + suffix,
                        'ytitle', 'MMS' + str(this_probe) + ' EDI e-field')
                    options(
                        'mms' + str(this_probe) + '_' + instrument +
                        '_e_dsl_' + this_dr + '_' + this_lvl + suffix, 'color',
                        ['b', 'g', 'r'])
                    options(
                        'mms' + str(this_probe) + '_' + instrument +
                        '_e_dsl_' + this_dr + '_' + this_lvl + suffix,
                        'legend_names', ['Ex DSL', 'Ey DSL', 'Ez DSL'])
                if 'mms' + str(
                        this_probe
                ) + '_' + instrument + '_e_gse_' + this_dr + '_' + this_lvl + suffix in tvars:
                    options(
                        'mms' + str(this_probe) + '_' + instrument +
                        '_e_gse_' + this_dr + '_' + this_lvl + suffix,
                        'ytitle', 'MMS' + str(this_probe) + ' EDI e-field')
                    options(
                        'mms' + str(this_probe) + '_' + instrument +
                        '_e_gse_' + this_dr + '_' + this_lvl + suffix, 'color',
                        ['b', 'g', 'r'])
                    options(
                        'mms' + str(this_probe) + '_' + instrument +
                        '_e_gse_' + this_dr + '_' + this_lvl + suffix,
                        'legend_names', ['Ex GSE', 'Ey GSE', 'Ez GSE'])
                if 'mms' + str(
                        this_probe
                ) + '_' + instrument + '_e_gsm_' + this_dr + '_' + this_lvl + suffix in tvars:
                    options(
                        'mms' + str(this_probe) + '_' + instrument +
                        '_e_gsm_' + this_dr + '_' + this_lvl + suffix,
                        'ytitle', 'MMS' + str(this_probe) + ' EDI e-field')
                    options(
                        'mms' + str(this_probe) + '_' + instrument +
                        '_e_gsm_' + this_dr + '_' + this_lvl + suffix, 'color',
                        ['b', 'g', 'r'])
                    options(
                        'mms' + str(this_probe) + '_' + instrument +
                        '_e_gsm_' + this_dr + '_' + this_lvl + suffix,
                        'legend_names', ['Ex GSM', 'Ey GSM', 'Ez GSM'])
def mms_hpca_calc_anodes(fov=[0, 360], probe='1', suffix=''):
    """
    This function will sum (or average, for flux) the HPCA data over the requested field-of-view (fov)
    
    Parameters
    ----------
        fov : list of int
            field of view, in angles, from 0-360

        probe : str
            probe #, e.g., '4' for MMS4

        suffix: str
            suffix of the loaded data

    Returns
    ----------
        List of tplot variables created.
    """
    sum_anodes = [a+suffix for a in ['*_count_rate', '*_RF_corrected', '*_bkgd_corrected', '*_norm_counts']]
    avg_anodes = ['*_flux'+suffix]
    output_vars = []
    species_map = {'hplus': 'H+', 'oplus': 'O+', 'heplus': 'He+', 'heplusplus': 'He++', 'oplusplus': 'O++'}

    fov_str = '_elev_'+str(fov[0])+'-'+str(fov[1])

    for sum_anode in sum_anodes:
        vars_to_sum = tnames(sum_anode)

        for var in vars_to_sum:
            var_species = var.split('_')[2]
            times, data, angles, energies = get_data(var)

            updated_spectra = mms_hpca_sum_fov(times, data, angles, energies, fov=fov)

            store_data(var+fov_str, data={'x': times, 'y': updated_spectra, 'v': energies})
            options(var+fov_str, 'spec', True)
            options(var+fov_str, 'ylog', True)
            options(var+fov_str, 'zlog', True)
            options(var+fov_str, 'ztitle', species_map[var_species] + ' ' + var.split('_')[3] + ' (cm^2-s-sr-eV)^-1')
            options(var+fov_str, 'ytitle', species_map[var_species] + ' Energy (eV)')
            options(var+fov_str, 'Colormap', 'spedas')
            output_vars.append(var+fov_str)

    for avg_anode in avg_anodes:
        vars_to_avg = tnames(avg_anode)

        for var in vars_to_avg:
            var_species = var.split('_')[2]
            times, data, angles, energies = get_data(var)

            updated_spectra = mms_hpca_avg_fov(times, data, angles, energies, fov=fov)

            store_data(var+fov_str, data={'x': times, 'y': updated_spectra, 'v': energies})
            options(var+fov_str, 'spec', True)
            options(var+fov_str, 'ylog', True)
            options(var+fov_str, 'zlog', True)
            options(var+fov_str, 'ztitle', species_map[var_species] + ' ' + var.split('_')[3] + ' (cm^2-s-sr-eV)^-1')
            options(var+fov_str, 'ytitle', species_map[var_species] + ' Energy (eV)')
            options(var+fov_str, 'Colormap', 'spedas')
            output_vars.append(var+fov_str)
    return output_vars
def mms_feeps_correct_energies(probes, data_rate, level='l2', suffix=''):
    """
    This function modifies the energy table in FEEPS spectra (intensity, count_rate, counts) variables
       using the function: mms_feeps_energy_table (which is s/c, sensor head and sensor ID dependent)
    
    Parameters:
        probes: list of str
            list of probes #, e.g., '4' for MMS4

        data_rate: str
            instrument data rate, e.g., 'srvy' or 'brst'

        level: str
            data level (default: 'l2')

        suffix: str
            suffix of the loaded data

    Notes:
        BAD EYES are replaced by NaNs
    """
    types = ['top', 'bottom']
    sensors = range(1, 13)
    units_types = ['intensity', 'count_rate']

    for probe in probes:
        for sensor_type in types:
            for sensor in sensors:
                if sensor >= 6 and sensor <= 8:
                    species = 'ion'
                else:
                    species = 'electron'

                for units in units_types:
                    var_name = tnames('mms' + probe + '_epd_feeps_' +
                                      data_rate + '_' + level + '_' + species +
                                      '_' + sensor_type + '_' + units +
                                      '_sensorid_' + str(sensor) + suffix)

                    if var_name == []:
                        continue
                    else:
                        var_name = var_name[0]

                    var_data = get_data(var_name)
                    if var_data is not None:
                        times, data, energies = var_data
                    else:
                        continue

                    energy_map = mms_feeps_energy_table(
                        probe, sensor_type[0:3], sensor)

                    try:
                        store_data(var_name,
                                   data={
                                       'x': times,
                                       'y': data,
                                       'v': energy_map
                                   })
                    except:
                        continue
Exemple #29
0
def avg_data(names,
             width=60,
             noremainder=True,
             new_names=None,
             suffix=None,
             overwrite=None):
    """Get a new tplot variable with averaged data."""
    old_names = pyspedas.tnames(names)

    if len(old_names) < 1:
        print('avg_data error: No pytplot names were provided.')
        return

    if suffix is None:
        suffix = '-avg'

    if overwrite is not None:
        n_names = old_names
    elif new_names is None:
        n_names = [s + suffix for s in old_names]
    else:
        n_names = new_names

    if isinstance(n_names, str):
        n_names = [n_names]

    if len(n_names) != len(old_names):
        n_names = [s + suffix for s in old_names]

    for i, old in enumerate(old_names):
        new = n_names[i]

        d = pytplot.data_quants[old].copy()
        data = d.values
        time = d.time.values

        dim = data.shape
        dim0 = dim[0]
        if len(dim) < 2:
            dim1 = 1
        else:
            dim1 = dim[1]

        new_data = []
        new_time = []
        for i in range(0, dim0, width):
            last = (i + width) if (i + width) < dim0 else dim0
            idx = int(i + width / 2)
            if idx > dim0 - 1:
                if noremainder:  # Skip the last part of data
                    continue
                idx = dim0 - 1  # Include the last part of data
            new_time.append(time[idx])
            if dim1 < 2:
                nd0 = np.average(data[i:last])
            else:
                nd0 = []
                for j in range(dim1):
                    nd0.append(np.average(data[i:last, j]))
            new_data.append(nd0)

        store_data(new, data={'x': new_time, 'y': new_data})
        # copy attributes
        pytplot.data_quants[new].attrs = d.attrs.copy()

        print('avg_data was applied to: ' + new)
from pytplot import tplot_names
tplot_names()

from pyspedas.mms import mms_load_edp

# load some burst mode electric field data
mms_load_edp(probe='4',
             data_rate='brst',
             trange=['2015-10-16/13:06', '2015-10-16/13:07'],
             time_clip=True)

from pyspedas import tnames

# the tnames function supports filtering with wild cards, e.g.,
# to find the E-field variables:
dce_vars = tnames('*_edp_dce_*')

# trange also accepts datetime objects
# note: be aware of potential time zone issues
from pyspedas.mms import mms_load_fpi
from datetime import datetime
from datetime import timezone as tz

start_time = datetime(year=2015,
                      month=10,
                      day=16,
                      hour=13,
                      minute=6,
                      tzinfo=tz.utc)
end_time = datetime(year=2015,
                    month=10,