コード例 #1
0
ファイル: ex_create.py プロジェクト: xqinshan/pyspedas
def ex_create():
    # 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 = pyspedas.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 tclip
    pyspedas.tclip('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')
    pytplot.tplot(
        ['sinx', 'sinx-clip', 'sinx-clip-deflag', 'sinx-clip-deflag-itrp'])
コード例 #2
0
def ex_wavelet(plot=True):
    """Demonstrates how to use wavelets with pyspedas."""
    # Delete any existing pytplot variables
    pytplot.del_data()

    # Create a pytplot variable.
    t = np.arange(4000.)
    y = np.sin(2 * np.pi * t / 32.)
    y2 = np.sin(2 * np.pi * t / 64.)
    y[1000:3000] = y2[1000:3000]
    var = 'sin_wav'
    time = time_float('2010-01-01') + 10 * t
    pytplot.store_data(var, data={'x': time, 'y': y})

    # Complex wavelet transformation.
    powervar = wavelet(var, wavename='cmorl0.5-1.0')
    # Also try the following and compare:
    # powervar = wavelet(var, wavename='gaus1')
    pvar = powervar[0]

    # Plot.
    pytplot.options(pvar, 'colormap', 'jet')
    pytplot.options(pvar, 'ylog', True)
    pytplot.options(pvar, 'ytitle', pvar)
    pytplot.ylim(pvar, 0.001, 1.0)

    if plot:
        pytplot.tplot([var, pvar])

    return 1
コード例 #3
0
ファイル: ex_deriv.py プロジェクト: donglai96/pyspedas
def ex_deriv2():
    """Find the derivative of sinx."""
    # Delete any existing pytplot variables
    pytplot.del_data()

    # Create a sin wave plot
    a = list(range(0, 101))
    b = [2.0 / 100.0 * np.pi * s for s in a]
    c = pyspedas.time_float('2017-01-01')
    x = list()
    y = list()
    for i in range(len(b)):
        x.append(c + 60.0 / (2 * np.pi) * 60.0 * b[i])
        y.append(1000.0 * np.sin(b[i]))

    # Store data
    pytplot.store_data('sinx', data={'x': x, 'y': y})

    var = 'sinx'
    deriv_data(var)
    pytplot.tplot([var, var + '-der'])

    return 1
コード例 #4
0
def time_clip(names,
              time_start,
              time_end,
              new_names=None,
              suffix=None,
              overwrite=None):
    """
    Clip data from time_start to time_end.

    Parameters:
    names: str/list of str
        List of pytplot names.
    time_start : float
        Start time.
    time_end : float
        End time.
    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 '-m'.
    overwrite: bool, optional
        Replace the existing tplot name.

    Returns
    -------
    None.

    """
    old_names = pyspedas.tnames(names)

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

    if suffix is None:
        suffix = '-tclip'

    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 j in range(len(old_names)):
        if old_names[j] != n_names[j]:
            pyspedas.tcopy(old_names[j], n_names[j])

        alldata = pytplot.get_data(n_names[j])
        time = alldata[0]
        data = alldata[1]

        index_start = 0
        index_end = len(time)

        if index_end < 1:
            print('Time clip found empty list.')
            continue

        new_time = pyspedas.time_float(time)
        new_time_start = pyspedas.time_float(time_start)
        new_time_end = pyspedas.time_float(time_end)

        if new_time_start > new_time_end:
            print('Error: Start time is larger than end time.')
            continue

        if (new_time_start > new_time[-1]) or (new_time_end < new_time[0]):
            print('Time clip returns empty data.')
            continue

        if (new_time_start <= new_time[0]) and (new_time_end >= new_time[-1]):
            print('Time clip returns full data set.')
            continue

        for i in range(index_end):
            if new_time[i] >= new_time_start:
                index_start = i
                break
        found_end = index_end
        for i in range(index_start, index_end):
            if new_time[i] > new_time_end:
                found_end = i
                break
        index_end = found_end

        tmp_q = data_quants[n_names[j]]

        if 'v1' in tmp_q.coords.keys():
            if len(tmp_q.coords['v1'].values.shape) == 2:
                v1_data = tmp_q.coords['v1'].values[index_start:index_end, :]
            else:
                v1_data = tmp_q.coords['v1'].values

        if 'v2' in tmp_q.coords.keys():
            if len(tmp_q.coords['v2'].values.shape) == 2:
                v2_data = tmp_q.coords['v2'].values[index_start:index_end, :]
            else:
                v2_data = tmp_q.coords['v2'].values

        if 'v3' in tmp_q.coords.keys():
            if len(tmp_q.coords['v3'].values.shape) == 2:
                v3_data = tmp_q.coords['v3'].values[index_start:index_end, :]
            else:
                v3_data = tmp_q.coords['v3'].values

        if 'v' in tmp_q.coords.keys():
            if len(tmp_q.coords['v'].values.shape) == 2:
                v_data = tmp_q.coords['v'].values[index_start:index_end, :]
            else:
                v_data = tmp_q.coords['v'].values

        if 'spec_bins' in tmp_q.coords.keys():
            if len(tmp_q.coords['spec_bins'].values.shape) == 2:
                v_data = tmp_q.coords['spec_bins']\
                    .values[index_start:index_end, :]
            else:
                v_data = tmp_q.coords['spec_bins'].values

        try:
            if 'v1' in tmp_q.coords.keys() and\
                'v2' in tmp_q.coords.keys() and\
                    'v3' in tmp_q.coords.keys():
                pytplot.store_data(n_names[j],
                                   data={
                                       'x': time[index_start:index_end],
                                       'y':
                                       data[index_start:index_end, :, :, :],
                                       'v1': v1_data,
                                       'v2': v2_data,
                                       'v3': v3_data
                                   })
            elif 'v1' in tmp_q.coords.keys() and\
                    'v2' in tmp_q.coords.keys():
                pytplot.store_data(n_names[j],
                                   data={
                                       'x': time[index_start:index_end],
                                       'y': data[index_start:index_end, :, :],
                                       'v1': v1_data,
                                       'v2': v2_data
                                   })
            elif 'v1' in tmp_q.coords.keys():
                pytplot.store_data(n_names[j],
                                   data={
                                       'x': time[index_start:index_end],
                                       'y': data[index_start:index_end, :],
                                       'v1': v1_data
                                   })
            elif 'spec_bins' in tmp_q.coords.keys():
                pytplot.store_data(n_names[j],
                                   data={
                                       'x': time[index_start:index_end],
                                       'y': data[index_start:index_end, :],
                                       'v': v_data
                                   })
            elif 'v' in tmp_q.coords.keys():
                pytplot.store_data(n_names[j],
                                   data={
                                       'x': time[index_start:index_end],
                                       'y': data[index_start:index_end, :],
                                       'v': v_data
                                   })
            elif data.ndim == 1:
                pytplot.store_data(n_names[j],
                                   data={
                                       'x': time[index_start:index_end],
                                       'y': data[index_start:index_end]
                                   })
            else:
                pytplot.store_data(n_names[j],
                                   data={
                                       'x': time[index_start:index_end],
                                       'y': data[index_start:index_end]
                                   })
        except:
            print('Problem time clipping: ' + n_names[j])
            continue

        print('Time clip was applied to: ' + n_names[j])
コード例 #5
0
ファイル: time_clip.py プロジェクト: owlJoe/pyspedas
def time_clip(names, time_start, time_end, new_names=None, suffix=None):

    old_names = pyspedas.tnames(names)

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

    if suffix is None:
        suffix = '-tclip'

    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 j in range(len(old_names)):
        if old_names[j] != n_names[j]:
            pyspedas.tcopy(old_names[j], n_names[j])

        time, data = pytplot.get_data(n_names[j])
        index_start = 0
        index_end = len(time)

        if index_end < 1:
            print('Time clip found empty list.')
            continue

        new_time = pyspedas.time_float(time)
        new_time_start = pyspedas.time_float(time_start)
        new_time_end = pyspedas.time_float(time_end)

        if new_time_start > new_time_end:
            print('Error: Start time is larger than end time.')
            continue

        if (new_time_start > new_time[-1]) or (new_time_end < new_time[0]):
            print('Time clip returns empty data.')
            continue

        if (new_time_start <= new_time[0]) and (new_time_end >= new_time[-1]):
            print('Time clip returns full data set.')
            continue

        for i in range(index_end):
            if new_time[i] >= new_time_start:
                index_start = i
                break
        found_end = index_end
        for i in range(index_start, index_end):
            if new_time[i] > new_time_end:
                found_end = i
                break
        index_end = found_end

        pytplot.store_data(n_names[j], data={'x': time[index_start:index_end],
                           'y': data[index_start:index_end, :]})
        print('Time clip was applied to: ' + n_names[j])
コード例 #6
0
ファイル: avg_data.py プロジェクト: spedas/pyspedas
def avg_data(names,
             dt=None,
             width=60,
             noremainder=False,
             new_names=None,
             suffix=None,
             overwrite=None):
    """
    Get a new tplot variable with averaged data.

    Parameters
    ----------
    names: str/list of str
        List of pytplot names.
    dt: float, optional
        Time window in seconds for averaging data. It can be less than 1 sec.
    width: int, optional
        Number of values for the averaging window.
        Default is 60 points (usually this means 60 seconds).
        If dt is set, then width is ignored.
    noremainder: boolean, optional
        If True, the remainter (last part of data) will not be included.
        If False. the remainter will be included.
    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('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 old_idx, old in enumerate(old_names):
        new = n_names[old_idx]

        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 = []
        if dt is None:
            # Use width
            width = int(width)
            # print(dim0, width)
            for i in range(0, dim0, width):
                last = (i + width) if (i + width) < dim0 else dim0
                # idx = int(i + width/2) # redefined below before it's ever used?
                if (i + width > dim0) and noremainder:
                    continue  # Skip the last part of data.
                else:
                    idx = int((i + last - 1) / 2)  # Include the last part.
                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)
        else:
            # Use dt
            dt = float(dt)
            timedbl = np.array(pyspedas.time_float(time))
            alldt = timedbl[-1] - timedbl[0]
            if not dt > 0.0:
                print("avg_data: Time interval dt<=0.0. Exiting.")
                return
            if dt > alldt:
                print("avg_data: Time interval dt is too large. Exiting.")
                return

            # Find bins for time: equal bins of length dt.
            bincount = int(alldt / dt)
            if alldt % dt > 0.0 and not noremainder:  # residual bin
                # Include the last bin which might not be the same size.
                bincount += 1

            time0 = timedbl[0]
            maxtime = timedbl[-1]
            for i in range(bincount):
                time1 = time0 + dt
                bintime = time0 + dt / 2.0
                if bintime > maxtime:
                    bintime = maxtime
                new_time.append(bintime)
                # Find all indexes between time0 and time1.
                idx = np.where((timedbl >= time0) & (timedbl < time1))

                # Check if idx is empty, ie. there is a gap in data.
                idx_is_empty = False
                if not idx:
                    idx_is_empty = True
                elif len(idx) == 1:
                    if len(idx[0]) == 0:
                        idx_is_empty = True

                if dim1 < 2:
                    if idx_is_empty:  # Empty list.
                        nd0 = np.nan
                    else:
                        nd0 = np.average(data[idx])
                else:
                    nd0 = []
                    for j in range(dim1):
                        if idx_is_empty:  # Empty list.
                            nd0.append(np.nan)
                        else:
                            nd0.append(np.average(data[idx, j]))
                new_data.append(nd0)
                time0 = time1

        pytplot.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)
コード例 #7
0
ファイル: time_clip.py プロジェクト: bryan-harter/pyspedas
def time_clip(names, time_start, time_end, new_names=None, suffix=None):

    old_names = pyspedas.tnames(names)

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

    if suffix is None:
        suffix = '-tclip'

    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 j in range(len(old_names)):
        if old_names[j] != n_names[j]:
            pyspedas.tcopy(old_names[j], n_names[j])

        alldata = pytplot.get_data(n_names[j])
        time = alldata[0]
        data = alldata[1]

        index_start = 0
        index_end = len(time)

        if index_end < 1:
            print('Time clip found empty list.')
            continue

        new_time = pyspedas.time_float(time)
        new_time_start = pyspedas.time_float(time_start)
        new_time_end = pyspedas.time_float(time_end)

        if new_time_start > new_time_end:
            print('Error: Start time is larger than end time.')
            continue

        if (new_time_start > new_time[-1]) or (new_time_end < new_time[0]):
            print('Time clip returns empty data.')
            continue

        if (new_time_start <= new_time[0]) and (new_time_end >= new_time[-1]):
            print('Time clip returns full data set.')
            continue

        for i in range(index_end):
            if new_time[i] >= new_time_start:
                index_start = i
                break
        found_end = index_end
        for i in range(index_start, index_end):
            if new_time[i] > new_time_end:
                found_end = i
                break
        index_end = found_end

        tmp_dquant = data_quants[n_names[j]]

        if 'v1' in tmp_dquant.coords.keys():
            if len(tmp_dquant.coords['v1'].values.shape) is 2:
                v1_data = tmp_dquant.coords['v1'].values[
                    index_start:index_end, :]
            else:
                v1_data = tmp_dquant.coords['v1'].values

        if 'v2' in tmp_dquant.coords.keys():
            if len(tmp_dquant.coords['v2'].values.shape) is 2:
                v2_data = tmp_dquant.coords['v2'].values[
                    index_start:index_end, :]
            else:
                v2_data = tmp_dquant.coords['v2'].values

        if 'v3' in tmp_dquant.coords.keys():
            if len(tmp_dquant.coords['v3'].values.shape) is 2:
                v3_data = tmp_dquant.coords['v3'].values[
                    index_start:index_end, :]
            else:
                v3_data = tmp_dquant.coords['v3'].values

        if 'v' in tmp_dquant.coords.keys():
            if len(tmp_dquant.coords['v'].values.shape) is 2:
                v_data = tmp_dquant.coords['v'].values[
                    index_start:index_end, :]
            else:
                v_data = tmp_dquant.coords['v'].values

        if 'spec_bins' in tmp_dquant.coords.keys():
            if len(tmp_dquant.coords['spec_bins'].values.shape) is 2:
                v_data = tmp_dquant.coords['spec_bins'].values[
                    index_start:index_end, :]
            else:
                v_data = tmp_dquant.coords['spec_bins'].values

        try:
            if 'v1' in tmp_dquant.coords.keys(
            ) and 'v2' in tmp_dquant.coords.keys(
            ) and 'v3' in tmp_dquant.coords.keys():
                pytplot.store_data(n_names[j],
                                   data={
                                       'x': time[index_start:index_end],
                                       'y':
                                       data[index_start:index_end, :, :, :],
                                       'v1': v1_data,
                                       'v2': v2_data,
                                       'v3': v3_data
                                   })
            elif 'v1' in tmp_dquant.coords.keys(
            ) and 'v2' in tmp_dquant.coords.keys():
                pytplot.store_data(n_names[j],
                                   data={
                                       'x': time[index_start:index_end],
                                       'y': data[index_start:index_end, :, :],
                                       'v1': v1_data,
                                       'v2': v2_data
                                   })
            elif 'v1' in tmp_dquant.coords.keys():
                pytplot.store_data(n_names[j],
                                   data={
                                       'x': time[index_start:index_end],
                                       'y': data[index_start:index_end, :],
                                       'v1': v1_data
                                   })
            elif 'spec_bins' in tmp_dquant.coords.keys():
                pytplot.store_data(n_names[j],
                                   data={
                                       'x': time[index_start:index_end],
                                       'y': data[index_start:index_end, :],
                                       'v': v_data
                                   })
            elif 'v' in tmp_dquant.coords.keys():
                pytplot.store_data(n_names[j],
                                   data={
                                       'x': time[index_start:index_end],
                                       'y': data[index_start:index_end, :],
                                       'v': v_data
                                   })
            elif data.ndim == 1:
                pytplot.store_data(n_names[j],
                                   data={
                                       'x': time[index_start:index_end],
                                       'y': data[index_start:index_end]
                                   })
            else:
                pytplot.store_data(n_names[j],
                                   data={
                                       'x': time[index_start:index_end],
                                       'y': data[index_start:index_end, ...]
                                   })
        except IndexError:
            print('Problem time clipping: ' + n_names[j])
            continue

        print('Time clip was applied to: ' + n_names[j])