Example #1
0
def tdpwrspc(varname,
             newname=None,
             nboxpoints=256,
             nshiftpoints=128,
             binsize=3,
             nohanning=False,
             noline=False,
             notperhz=False,
             notmvariance=False):
    if newname is None:
        newname = varname + '_dpwrspc'

    data_tuple = get_data(varname)

    if data_tuple is not None:
        if data_tuple[1][0].shape != ():
            split_vars = split_vec(varname)
            out_vars = []
            for var in split_vars:
                out_vars.append(
                    tdpwrspc(var,
                             newname=var + '_dpwrspc',
                             nboxpoints=nboxpoints,
                             nshiftpoints=nshiftpoints))
            return out_vars
        else:
            pwrspc = dpwrspc(data_tuple[0],
                             data_tuple[1],
                             nboxpoints=nboxpoints,
                             nshiftpoints=nshiftpoints,
                             binsize=binsize,
                             nohanning=nohanning,
                             noline=noline,
                             notperhz=notperhz,
                             notmvariance=notmvariance)

            if pwrspc != None:
                store_data(newname,
                           data={
                               'x': pwrspc[0],
                               'y': pwrspc[2],
                               'v': pwrspc[1]
                           })
                options(newname, 'spec', True)
                options(newname, 'ylog', True)
                options(newname, 'zlog', True)
                options(newname, 'Colormap', 'jet')
            # options(newname, 'yrange', [0.01, 16])
        return newname
Example #2
0
def wavelet(names,
            new_names=None,
            suffix='_pow',
            wavename='morl',
            scales=None,
            method='fft',
            sampling_period=1.0):
    """
    Find the wavelet transofrmation of a tplot variable.

    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 '_pow'.
    wavename: str, optional
        The name of the continous wavelet function to apply.
        Examples: 'gaus1', 'morl', 'cmorlB-C'.
    scales: list of float, optional
        The wavelet scales to use.
    method: str, optional
        Either ‘fft’ for  frequency domain convolution,
        or 'conv' for numpy.convolve.
    sampling_period: float, optional
        The sampling period for the frequencies output.

    Returns
    -------
    A list of pytplot variables that contain the wavelet power.

    """
    varnames = pytplot.split_vec(names)
    powervar = []

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

    if scales is None:
        scales = np.arange(1, 128)

    for i, old in enumerate(varnames):
        old = varnames[i]

        if (new_names is not None) and (len(new_names) == len(varnames)):
            new = new_names[i]
        else:
            new = old + suffix

        alldata = pytplot.get_data(old)
        time = alldata[0]
        len_time = len(time)
        data = alldata[1]

        if len_time < 2:
            print('wavelet error: Not enought data points for ' + old)
            continue

        coef, freqs = pywt.cwt(data,
                               scales=scales,
                               wavelet=wavename,
                               method=method,
                               sampling_period=sampling_period)

        power = np.abs(coef)**2
        power = power.transpose()
        pytplot.store_data(new, data={'x': time, 'y': power, 'v': freqs})
        pytplot.options(new, 'spec', 1)
        powervar.append(new)

        print('wavelet was applied to: ' + new)

    return powervar
Example #3
0
def tdpwrspc(varname,
             newname=None,
             nboxpoints=256,
             nshiftpoints=128,
             binsize=3,
             nohanning=False,
             noline=False,
             notperhz=False,
             notmvariance=False):
    """
    Compute power spectra for a tplot variable.

    Parameters
    ----------
    varname: str
        Name of pytplot variable.
    newname: str, optional
        Name of new pytplot variable to save data to.
    nboxpoints: int, optional
        The number of points to use for the hanning window.
        The default is 256.
    nshiftpoints: int, optional
        The number of points to shift for each spectrum.
        The default is 128.
    binsize: int, optional
        Size for binning of the data along the frequency domain.
        The default is 3.
    nohanning: bool, optional
        If True, no hanning window is applied to the input.
        The default is False.
    noline: bool, optional
        If True, no straight line is subtracted.
        The default is False.
    notperhz: bool, optional
        If True, the output units are the square of the input units.
        The default is False.
    notmvariance: bool, optional
        If True, replace output spectrum for any windows that have variable.
        cadence with NaNs.
        The default is False.

    Returns
    -------
    str
        Name of new pytplot variable.

    """
    if newname is None:
        newname = varname + '_dpwrspc'

    data_tuple = get_data(varname)

    if data_tuple is not None:
        if data_tuple[1][0].shape != ():
            split_vars = split_vec(varname)
            out_vars = []
            for var in split_vars:
                out_vars.append(
                    tdpwrspc(var,
                             newname=var + '_dpwrspc',
                             nboxpoints=nboxpoints,
                             nshiftpoints=nshiftpoints))
            return out_vars
        else:
            pwrspc = dpwrspc(data_tuple[0],
                             data_tuple[1],
                             nboxpoints=nboxpoints,
                             nshiftpoints=nshiftpoints,
                             binsize=binsize,
                             nohanning=nohanning,
                             noline=noline,
                             notperhz=notperhz,
                             notmvariance=notmvariance)

            if pwrspc != None:
                store_data(newname,
                           data={
                               'x': pwrspc[0],
                               'y': pwrspc[2],
                               'v': pwrspc[1]
                           })
                options(newname, 'spec', True)
                options(newname, 'ylog', True)
                options(newname, 'zlog', True)
                options(newname, 'Colormap', 'jet')
            # options(newname, 'yrange', [0.01, 16])
        return newname
Example #4
0
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)
    
Example #5
0
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')
print(pytplot.data_quants['h_specmult'].data)

print('split_vec')
pytplot.split_vec('b', ['b1', 'b2', 'b3'], [0, [1, 3], 4])
print(pytplot.data_quants['b1'].data)
print(pytplot.data_quants['b2'].data)
print(pytplot.data_quants['b3'].data)
defaultlist = pytplot.split_vec('b')
print(pytplot.data_quants['b'].data)
print(pytplot.data_quants['data_3'].data)
print(defaultlist)

print('subtract')
pytplot.subtract('a', 'c', 'a-c')
print(pytplot.data_quants['a-c'].data)