Exemple #1
0
def ex_spikes():
    """Load GMAG data and average over 5 min intervals."""
    # Delete any existing pytplot variables.
    pytplot.del_data()

    # Define a time rage as a list
    trange = ['2007-03-23', '2007-03-23']

    # Download gmag files and load data into pytplot variables.
    sites = ['ccnv']
    var = 'thg_mag_ccnv'
    pyspedas.themis.gmag(sites=sites, trange=trange, varnames=[var])
    pytplot.tplot_options('title', 'GMAG data, thg_mag_ccnv 2007-03-23')

    # Add spikes to data.
    data = pytplot.data_quants[var].values
    dlen = len(data)
    for i in range(1, 16):
        s = (1 if random.random() < 0.5 else -1)
        p1 = int(i * dlen / 16)
        data[p1, 0] = s * i * 40000
        data[p1 + 2000, 1] = s * i * 30000
        data[p1 + 4000, 2] = s * i * 20000

    pytplot.data_quants[var].values = data

    # Clean spikes.
    clean_spikes(var, sub_avg=True)

    # Plot all variables.
    pytplot.tplot(pytplot.tplot_names())

    # Return 1 as indication that the example finished without problems.
    return 1
Exemple #2
0
 def test_load_data_no_update(self):
     data = mms_load_fgm(trange=['2015-10-16', '2015-10-16/01:00'
                                 ])  # make sure the files exist locally
     del_data('*')
     data = mms_load_fgm(
         trange=['2015-10-16', '2015-10-16/01:00'],
         no_update=True)  # load the file from the local cache
     self.assertTrue(data_exists('mms1_fgm_b_gse_srvy_l2'))
 def test_load_brst_data(self):
     del_data('*')
     data = mms_load_feeps(trange=['2015-10-16/13:06', '2015-10-16/13:07'],
                           data_rate='brst')
     self.assertTrue(
         data_exists('mms1_epd_feeps_brst_l2_electron_intensity_omni'))
     self.assertTrue(
         data_exists('mms1_epd_feeps_brst_l2_electron_intensity_omni_spin'))
Exemple #4
0
 def test_load_eph_no_update(self):
     data = mms_load_state(
         datatypes=['pos', 'vel'])  # ensure the files are stored locally
     del_data('*')  # remove the current tplot vars
     data = mms_load_state(
         datatypes=['pos', 'vel'],
         no_update=True)  # ensure the files are stored locally
     self.assertTrue(data_exists('mms1_defeph_pos'))
     self.assertTrue(data_exists('mms1_defeph_vel'))
 def test_load_ion_omni(self):
     del_data('*')
     data = mms_load_hpca(trange=['2016-10-16/5:00', '2016-10-16/6:00'], datatype='ion')
     mms_hpca_calc_anodes(fov=[0, 360], probe='1')
     mms_hpca_spin_sum()
     self.assertTrue(data_exists('mms1_hpca_hplus_flux_elev_0-360_spin'))
     self.assertTrue(data_exists('mms1_hpca_heplus_flux_elev_0-360_spin'))
     self.assertTrue(data_exists('mms1_hpca_heplusplus_flux_elev_0-360_spin'))
     self.assertTrue(data_exists('mms1_hpca_oplus_flux_elev_0-360_spin'))
Exemple #6
0
    def test_all_cotrans(self):
        """Test all cotrans pairs.

        Apply transformation, then inverse transformation and compare.
        """
        cotrans()
        all_cotrans = ['gei', 'geo', 'j2000', 'gsm', 'mag', 'gse', 'sm']
        d = [[245.0, -102.0, 251.0], [775.0, 10.0, -10], [121.0, 545.0, -1.0],
             [304.65, -205.3, 856.1], [464.34, -561.55, -356.22]]
        dd1 = d[1]
        t = [1577112800, 1577308800, 1577598800, 1577608800, 1577998800]
        in_len = len(t)
        name1 = "name1"
        name2 = "name2"
        count = 0
        # Test non-existent system.
        cotrans(name_out=name1,
                time_in=t,
                data_in=d,
                coord_in="coord_in",
                coord_out="coord_out")
        # Test empty data.
        cotrans(name_out=name1,
                time_in=t,
                data_in=[],
                coord_in="gei",
                coord_out="geo")
        cotrans(time_in=t, data_in=d, coord_in="gse", coord_out="gsm")
        # Test all combinations.
        for coord_in in all_cotrans:
            for coord_out in all_cotrans:
                count += 1
                del_data()
                cotrans(name_out=name1,
                        time_in=t,
                        data_in=d,
                        coord_in=coord_in,
                        coord_out=coord_out)
                dout = get_data(name1)
                out_len1 = len(dout[0])
                self.assertTrue(out_len1 == in_len)
                # Now perform inverse transformation.
                cotrans(name_in=name1,
                        name_out=name2,
                        coord_in=coord_out,
                        coord_out=coord_in)
                dout2 = get_data(name2)
                out_len2 = len(dout2[0])
                dd2 = dout2[1][1]
                print(count, "--- in:", coord_in, "out:", coord_out)
                # print(dout[1][1])
                # print(dd2)
                self.assertTrue(out_len2 == in_len)
                self.assertTrue(abs(dd1[0] - dd2[0]) <= 1e-6)
                self.assertTrue(abs(dd1[1] - dd2[1]) <= 1e-6)
                self.assertTrue(abs(dd1[2] - dd2[2]) <= 1e-6)
Exemple #7
0
 def test_load_brst_ion_data(self):
     del_data('*')
     data = mms_load_feeps(
         probe=4,
         data_rate='brst',
         datatype='ion',
         trange=['2015-10-01/10:48:16', '2015-10-01/10:49:16'])
     self.assertTrue(
         data_exists('mms4_epd_feeps_brst_l2_ion_intensity_omni'))
     self.assertTrue(
         data_exists('mms4_epd_feeps_brst_l2_ion_intensity_omni_spin'))
Exemple #8
0
def ex_spectra():
    # Delete any existing pytplot variables
    pytplot.del_data()
    # Download THEMIS data for 2015-12-31
    pyspedas.load_data('themis', ['2015-12-31'], ['tha'], 'sst', 'l2')
    # Specify options
    pytplot.tplot_options('title', 'tha_psif_en_eflux 2015-12-31')
    pytplot.ylim('tha_psif_en_eflux', 10000.0, 10000000.0)
    pytplot.options('tha_psif_en_eflux', 'colormap', 'viridis')
    # Plot spectrogram
    pytplot.tplot(['tha_psif_en_eflux'])
Exemple #9
0
def ex_analysis():
    # Print the installed version of pyspedas
    pyspedas.version()
    # Delete any existing pytplot variables
    pytplot.del_data()
    # Download THEMIS state data for 2015-12-31
    pyspedas.load_data('themis', '2015-12-31', ['tha'], 'state', 'l1')
    # Use some analysis functions on tplot variables
    pyspedas.subtract_average('tha_pos')
    pyspedas.subtract_median('tha_pos')
    # Plot
    pytplot.tplot(["tha_pos", "tha_pos-d", "tha_pos-m"])
Exemple #10
0
 def test_load_ion_omni_suffix(self):
     del_data('*')
     data = mms_load_hpca(
         probe=2,
         trange=['2016-08-09/09:10', '2016-08-09/10:10:00'],
         datatype='ion',
         data_rate='brst',
         suffix='_brst')
     mms_hpca_calc_anodes(fov=[0, 360], probe=2, suffix='_brst')
     mms_hpca_spin_sum(probe=2, suffix='_brst', avg=True)
     self.assertTrue(
         data_exists('mms2_hpca_hplus_flux_brst_elev_0-360_spin'))
Exemple #11
0
def ex_omni():
    # Print the installed version of pyspedas
    pyspedas.version()
    # Delete any existing pytplot variables
    pytplot.del_data()
    # Download OMNI data for 2015-12-31
    pyspedas.load_data('omni', ['2015-12-31 00:00:00', '2016-01-01 23:59:59'],
                       '', '', '1min')

    # Plot
    pytplot.tplot_options('title', 'OMNI flow_speed 2015-12-31 to 2016-01-01')
    pytplot.tplot(['flow_speed'])
Exemple #12
0
def ex_omni():
    # Delete any existing pytplot variables
    pytplot.del_data()

    # Download OMNI data for 2015-12-31
    trange = ['2015-12-31 00:00:00', '2015-12-31 23:59:59']
    pyspedas.omni.load(trange=trange, datatype='1min')

    # Plot
    pytplot.tplot_options('title', 'OMNI flow_speed 2015-12-31')
    pytplot.tplot(['flow_speed'])

    # Return 1 as indication that the example finished without problems.
    return 1
Exemple #13
0
def ex_analysis():

    # Delete any existing pytplot variables
    pytplot.del_data()

    # Download THEMIS state data for 2015-12-31
    time_range = ['2015-12-31 00:00:00', '2015-12-31 23:59:59']
    pyspedas.themis.state(probe='a', trange=time_range)

    # Use some analysis functions on tplot variables
    pyspedas.subtract_average('tha_pos')
    pyspedas.subtract_median('tha_pos')

    # Plot
    pytplot.tplot(["tha_pos", "tha_pos-d", "tha_pos-m"])

    # Return 1 as indication that the example finished without problems.
    return 1
Exemple #14
0
def ex_basic():
    # Delete any existing pytplot variables
    pytplot.del_data()
    # Download THEMIS state data for 2015-12-31
    time_range = ['2015-12-31 00:00:00', '2016-01-01 12:00:00']
    pyspedas.load_data('themis', time_range, ['tha'], 'state', 'l1')
    # Get data into python variables
    alldata = pytplot.get_data("tha_pos")
    time = alldata[0]
    data = alldata[1]
    # Store a new pytplot variable
    pytplot.store_data("tha_position", data={'x': time, 'y': data})
    # Define the y-axis limits
    pytplot.ylim('tha_pos', -23000.0, 81000.0)
    pytplot.ylim('tha_position', -23000.0, 81000.0)
    pytplot.ylim('tha_vel', -8.0, 12.0)
    # Plot position and velocity using the pyqtgraph library (default)
    pytplot.tplot(["tha_pos", "tha_position", "tha_vel"])
Exemple #15
0
def ex_gmag():
    # Delete any existing pytplot variables
    pytplot.del_data()
    # Define list of dates
    time_list = ['2015-12-31']
    # Get a list of EPO gmag stations
    sites = pyspedas.gmag_list(group='epo')
    # Download gmag files and load data into pytplot variables
    pyspedas.load_data('gmag', time_list, sites, '', '')
    # Get a list of loaded sites
    sites_loaded = pyspedas.tplot_names()
    # Subtact mean values
    pyspedas.subtract_average(sites_loaded, '')
    # Download AE index data
    pyspedas.load_data('gmag', time_list, ['idx'], '', '')
    # Plot
    sites_loaded = pyspedas.tplot_names()
    pytplot.tplot_options('title', 'EPO GMAG 2015-12-31')
    pytplot.tplot(sites_loaded)
Exemple #16
0
def degap(tvar, margin, dt, func=None):
    tv1 = pytplot.data_quants[tvar].data
    indices = []
    #create array of dt between margin parameters
    for m in margin:
        if type(m) == int:
            new_index = np.arange(margin[0], margin[1], dt)
        else:
            new_index = np.arange(m[0], m[1], dt)
        new_index = np.append(new_index, new_index[-1] + dt)
        indices = indices + [new_index]
    #add any new indices to current dataframe indices
    new_index = np.unique(np.append(indices, tv1.index))
    #interpolate to create data for new values
    pytplot.tplot_resample(tvar, new_index, tvar + '_interp')

    if func == 'nan':
        tv1 = pytplot.data_quants[tvar + '_interp'].data
        #any location between margins will be changed to NaN
        for i in indices:
            tv1.loc[i[0]:i[-1]] = np.nan
        pytplot.store_data(tvar + '_nan',
                           data={
                               'x': tv1.index.values,
                               'y': tv1
                           })
        pytplot.del_data(name=tvar + '_interp')

    if func == 'ffill':
        tv1 = pytplot.data_quants[tvar + '_interp'].data
        #any location between margins will be changed to NaN
        for i in indices:
            tv1.loc[i[0]:i[-1]] = np.nan
        #forward fill NaNs in the dataframe
        tv1 = tv1.fillna(method='ffill')
        pytplot.store_data(tvar + '_ffill',
                           data={
                               'x': tv1.index.values,
                               'y': tv1
                           })
        pytplot.del_data(name=tvar + '_interp')

    return
Exemple #17
0
 def test_cotrans_geigsm(self):
     """Test daisy chain of multiple transofrmations, GEI->GSE->GSM."""
     del_data()
     d = [[245.0, -102.0, 251.0], [775.0, 10.0, -10], [121.0, 545.0, -1.0],
          [304.65, -205.3, 856.1], [464.34, -561.55, -356.22]]
     t = [1577112800, 1577308800, 1577598800, 1577608800, 1577998800]
     name_out = "tname_out"
     cotrans(name_out=name_out,
             time_in=t,
             data_in=d,
             coord_in="gei",
             coord_out="gsm")
     in_len = len(t)
     dout = get_data(name_out)
     out_len = len(dout[0])
     gsm = dout[1][1]
     res = [4.59847513e+01, 7.62303493e+02, 1.32679267e+02]
     self.assertTrue(out_len == in_len)
     self.assertTrue(abs(gsm[0] - res[0]) <= 1e-6)
     self.assertTrue(abs(gsm[1] - res[1]) <= 1e-6)
     self.assertTrue(abs(gsm[2] - res[2]) <= 1e-6)
Exemple #18
0
 def test_cotrans(self):
     """Test cotrans.py GEI->GEO with Themis data."""
     del_data()
     trange = ['2010-02-25/00:00:00', '2010-02-25/23:59:59']
     probe = 'a'
     name_in = "tha_pos"
     name_out = "tha_pos_new_geo"
     pyspedas.themis.state(probe=probe,
                           trange=trange,
                           time_clip=True,
                           varnames=[name_in])
     cotrans(name_in=name_in,
             name_out=name_out,
             coord_in="gei",
             coord_out="geo")
     din = get_data(name_in)
     in_len = len(din[0])
     dout = get_data(name_out)
     out_len = len(dout[0])
     cotrans(name_in=name_in, coord_in="gei", coord_out="geo")
     self.assertTrue(out_len == in_len)
Exemple #19
0
 def test_cotrans_igrf(self):
     """Test GSE->GSM and IGRF."""
     del_data()
     d = [[245.0, -102.0, 251.0], [775.0, 10.0, -10], [121.0, 545.0, -1.0],
          [304.65, -205.3, 856.1], [464.34, -561.55, -356.22]]
     t = [1577112800, 1577308800, 1577598800, 1577608800, 1577998800]
     name_out = "tname_out"
     cotrans(name_out=name_out,
             time_in=t,
             data_in=d,
             coord_in="gse",
             coord_out="gsm")
     in_len = len(t)
     dout = get_data(name_out)
     out_len = len(dout[0])
     gsm = dout[1][1]
     res = [775.0, 11.70325822, -7.93937951]
     self.assertTrue(out_len == in_len)
     self.assertTrue(abs(gsm[0] - res[0]) <= 1e-6)
     self.assertTrue(abs(gsm[1] - res[1]) <= 1e-6)
     self.assertTrue(abs(gsm[2] - res[2]) <= 1e-6)
Exemple #20
0
 def test_cotrans_j2000(self):
     """Test GEI->J2000 and J2000 params."""
     del_data()
     d = [[245.0, -102.0, 251.0], [775.0, 10.0, -10], [121.0, 545.0, -1.0],
          [304.65, -205.3, 856.1], [464.34, -561.55, -356.22]]
     t = [1577112800, 1577308800, 1577598800, 1577608800, 1577998800]
     name_out = "tname_out"
     cotrans(name_out=name_out,
             time_in=t,
             data_in=d,
             coord_in="gei",
             coord_out="j2000")
     in_len = len(t)
     dout = get_data(name_out)
     out_len = len(dout[0])
     j2000 = dout[1][1]
     res = [775.01595209, 6.59521058, -11.47942543]
     self.assertTrue(out_len == in_len)
     self.assertTrue(abs(j2000[0] - res[0]) <= 1e-6)
     self.assertTrue(abs(j2000[1] - res[1]) <= 1e-6)
     self.assertTrue(abs(j2000[2] - res[2]) <= 1e-6)
Exemple #21
0
def ex_create(plot=True):
    """Show how to create and plot pytplot variables."""
    # Delete any existing pytplot variables
    pytplot.del_data()

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

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

    # Apply yclip
    pyspedas.yclip('sinx', -800.0, 800.0)

    # Remove NaN values
    pyspedas.tdeflag('sinx-clip')

    # Interpolate
    pyspedas.tinterpol(['sinx-clip-deflag'], 'sinx', 'quadratic')

    # Plot
    pytplot.ylim('sinx', -1100.0, 1100.0)
    pytplot.ylim('sinx-clip', -1100.0, 1100.0)
    pytplot.ylim('sinx-clip-deflag', -1100.0, 1100.0)
    pytplot.ylim('sinx-clip-deflag-itrp', -1100.0, 1100.0)
    pytplot.tplot_options('title', 'Interpolation example')

    if plot:
        pytplot.tplot(
            ['sinx', 'sinx-clip', 'sinx-clip-deflag', 'sinx-clip-deflag-itrp'])

    # Return 1 as indication that the example finished without problems.
    return 1
Exemple #22
0
def tplot_resample(tvar1, times, newtvar):
    #create dummy dataframe for times to interpolate to
    pytplot.store_data('times_for_resample',
                       data={
                           'x': times,
                           'y': np.zeros(len(times))
                       })
    df_index = pytplot.data_quants[tvar1].data.columns
    new_df = []
    tvar_orig = pytplot.data_quants[tvar1]
    #for each column of dataframe
    for i in df_index:
        tv2_col = [item[i] for item in tvar_orig.data.values]
        #linear interpolation
        f = interp1d(tvar_orig.data.index, tv2_col, fill_value="extrapolate")
        new_df = new_df + [f(times)]
    new_df = np.transpose((list(new_df)))
    #store interpolated tvar'
    pytplot.store_data(newtvar, data={'x': times, 'y': new_df})
    #delete dummy dataframe from pytplot.data_quants
    pytplot.del_data(name='times_for_resample')
    return
Exemple #23
0
def mms_feeps_split_integral_ch(units_type, species, probe, suffix='', data_rate='srvy', level='l2', sensor_eyes=None):
    if sensor_eyes is None:
        print('Error: sensor_eyes not defined')
        return

    top_sensors = sensor_eyes['top']
    bot_sensors = sensor_eyes['bottom']

    for sensor in top_sensors:
        top_name = 'mms'+str(probe)+'_epd_feeps_'+data_rate+'_'+level+'_'+species+'_top_'+units_type+'_sensorid_'+str(sensor)

        time, data, energies = pytplot.get_data(top_name+suffix)

        top_name_out = top_name+'_clean'+suffix
        try:
            pytplot.store_data(top_name_out, data={'x': time, 'y': data[:, :-1], 'v': energies[:-1]})
            pytplot.store_data(top_name+'_500keV_int'+suffix, data={'x': time, 'y': data[:, -1]})
        except Warning:
            continue

        pytplot.del_data(top_name)

    if level == 'sitl': # SITL only has top sensors
        return

    for sensor in bot_sensors:
        bot_name = 'mms'+str(probe)+'_epd_feeps_'+data_rate+'_'+level+'_'+species+'_bottom_'+units_type+'_sensorid_'+str(sensor)

        time, data, energies = pytplot.get_data(bot_name+suffix)

        bot_name_out = bot_name+'_clean'+suffix
        try:
            pytplot.store_data(bot_name_out, data={'x': time, 'y': data[:, :-1], 'v': energies[:-1]})
            pytplot.store_data(bot_name+'_500keV_int'+suffix, data={'x': time, 'y': data[:, -1]})
        except Warning:
            continue

        pytplot.del_data(bot_name)
Exemple #24
0
def ex_avg(plot=False):
    """Load GMAG data and average over 5 min intervals."""
    # Delete any existing pytplot variables
    pytplot.del_data()

    # Define a time rage as a list
    trange = ['2007-03-23', '2007-03-23']

    # Download gmag files and load data into pytplot variables
    sites = ['ccnv']
    var = 'thg_mag_ccnv'
    pyspedas.themis.gmag(sites=sites, trange=trange, varnames=[var])
    pytplot.tplot_options('title', 'GMAG data, thg_mag_ccnv 2007-03-23')
    pyspedas.subtract_average(var, median=1)
    var += '-m'

    # Five minute average
    avg_data(var, width=5 * 60)
    if plot:
        pytplot.tplot([var, var + '-avg'])

    # Return 1 as indication that the example finished without problems.
    return 1
Exemple #25
0
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
Exemple #26
0
def ex_spectra():
    # Delete any existing pytplot variables
    pytplot.del_data()

    # Download THEMIS data for 2015-12-31
    # pyspedas.load_data('themis', ['2015-12-31'], ['tha'], 'state', 'l1')
    # pyspedas.load_data('themis', ['2015-12-31'], ['tha'], 'sst', 'l2')
    time_range = ['2015-12-31 00:00:00', '2015-12-31 23:59:59']
    pyspedas.themis.state(probe='a', trange=time_range)
    pyspedas.themis.sst(probe='a', trange=time_range,
                        varnames=['tha_psif_en_eflux'])

    # Specify options
    pytplot.ylim('tha_pos', -23000.0, 81000.0)
    pytplot.ylim('tha_psif_en_eflux', 10000.0, 4000000.0)
    pytplot.options('tha_psif_en_eflux', 'colormap', 'viridis')
    pytplot.tplot_options('title', 'tha 2015-12-31')

    # Plot line and spectrogram
    pytplot.tplot(['tha_pos', 'tha_psif_en_eflux'])

    # Return 1 as indication that the example finished without problems.
    return 1
Exemple #27
0
def ex_basic(plot=True):
    """Download and plot THEMIS data."""
    # Delete any existing pytplot variables
    pytplot.del_data()

    # Download THEMIS state data for 2015-12-31
    time_range = ['2015-12-31 00:00:00', '2016-01-01 12:00:00']
    pyspedas.themis.state(probe='a', trange=time_range, time_clip=True)

    # Get data into python variables
    alldata = pytplot.get_data("tha_pos")
    time = alldata[0]
    data = alldata[1]
    # Here we could work on the data before saving a new tplot variable.

    # Store a new pytplot variable
    pytplot.store_data("tha_position", data={'x': time, 'y': data})

    # Define the y-axis limits
    pytplot.ylim('tha_pos', -23000.0, 81000.0)
    pytplot.ylim('tha_position', -23000.0, 81000.0)
    pytplot.ylim('tha_vel', -8.0, 12.0)

    # Give a title to the plot and labels for the y-axis panels.
    pytplot.tplot_options('title', 'tha position and velocity, 2015-12-31')
    pytplot.options('tha_pos', 'ytitle', 'Position')
    pytplot.options('tha_vel', 'ytitle', 'Velocity')

    # Plot position and velocity using the pyqtgraph library (default)
    if plot:
        pytplot.tplot(["tha_pos", "tha_position", "tha_vel"])

    # Plot position and velocity using the bokeh library
    # pytplot.tplot(["tha_pos", "tha_position", "tha_vel"], bokeh=True)

    # Return 1 as indication that the example finished without problems.
    return 1
Exemple #28
0
def ex_deriv():
    """Find the derivative of a GMAG variable."""
    # Derivative of data
    pytplot.del_data()

    # Define a time rage as a list
    trange = ['2007-03-23', '2007-03-23']

    # Download gmag files and load data into pytplot variables
    sites = ['ccnv']
    var = 'thg_mag_ccnv'
    pyspedas.themis.gmag(sites=sites, trange=trange, varnames=[var])
    # pytplot.tplot_options('title', 'GMAG data, thg_mag_ccnv 2007-03-23')
    pyspedas.subtract_average(var, median=1)
    var += '-m'

    # Five minute average
    deriv_data(var)
    # pytplot.options(var, 'ytitle', var)
    # pytplot.options(var + '-der', 'ytitle', var + '-der')
    pytplot.tplot([var, var + '-der'])

    # Return 1 as indication that the example finished without problems.
    return 1
Exemple #29
0
 def tearDown(self):
     del_data('*')
Exemple #30
0
def load_data(filenames=None,
              instruments=None,
              level='l2',
              type=None,
              insitu=True,
              iuvs=False,
              start_date='2014-01-01',
              end_date='2020-01-01',
              update_prefs=False,
              only_update_prefs=False,
              local_dir=None,
              list_files=False,
              new_files=True,
              exclude_orbit_file=False,
              download_only=False,
              varformat=None,
              varnames=[],
              prefix='',
              suffix='',
              get_support_data=False,
              auto_yes=False):
    """
    This function downloads MAVEN data loads it into tplot variables, if applicable.
    """

    if not isinstance(instruments, list) and instruments is not None:
        instruments = [instruments]

    if not isinstance(type, list) and type is not None:
        type = [type]

    if not isinstance(filenames, list) and filenames is not None:
        filenames = [filenames]

    # 1. Get a list of MAVEN files queries from the above seach parameters
    maven_files = maven_filenames(filenames, instruments, level, insitu, iuvs,
                                  start_date, end_date, update_prefs,
                                  only_update_prefs, local_dir)

    # If we are not asking for KP data, this flag ensures only ancillary data is loaded in from the KP files
    if level != 'kp':
        ancillary_only = True
    else:
        ancillary_only = False

    # Convert to list
    if not isinstance(type, list):
        type = [type]

    # Keep track of what files are downloaded
    files_to_load = []

    # Loop through all instruments, download files locally if needed
    for instr in maven_files.keys():
        bn_files_to_load = []
        if maven_files[instr]:
            s = maven_files[instr][0]
            data_dir = maven_files[instr][1]
            public = maven_files[instr][2]

            # Add to list of files to load
            for f in s:
                # Filter by type
                if type != [None] and instr != 'kp':
                    file_type_match = False
                    desc = l2_regex.match(f).group("description")
                    for t in type:
                        if t in desc:
                            file_type_match = True
                    if not file_type_match:
                        continue

                # Check if the files are KP data
                if instr == 'kp':
                    full_path = create_dir_if_needed(f, data_dir, 'insitu')
                else:
                    full_path = create_dir_if_needed(f, data_dir, level)
                bn_files_to_load.append(f)
                files_to_load.append(os.path.join(full_path, f))

            if list_files:
                for f in s:
                    print(f)
                return

            if new_files:
                if instr == 'kp':
                    s = get_new_files(bn_files_to_load, data_dir, instr,
                                      'insitu')
                else:
                    s = get_new_files(bn_files_to_load, data_dir, instr, level)
            if len(s) == 0:
                continue
            print("Your request will download a total of: " + str(len(s)) +
                  " files for instrument " + str(instr))
            print('Would you like to proceed with the download? ')
            valid_response = False
            cancel = False
            if auto_yes:
                valid_response = True
            while not valid_response:
                response = (input('(y/n) >  '))
                if response == 'y' or response == 'Y':
                    valid_response = True
                    cancel = False
                elif response == 'n' or response == 'N':
                    print('Cancelled download. Returning...')
                    valid_response = True
                    cancel = True
                else:
                    print('Invalid input.  Please answer with y or n.')

            if cancel:
                continue

            i = 0
            display_progress(i, len(s))
            for f in s:
                i = i + 1
                if instr == 'kp':
                    full_path = create_dir_if_needed(f, data_dir, 'insitu')
                else:
                    full_path = create_dir_if_needed(f, data_dir, level)
                get_file_from_site(f, public, full_path)
                display_progress(i, len(s))

    # 2. Load files into tplot

    if files_to_load:
        # Flatten out downloaded files from list of lists of filenames
        if isinstance(files_to_load[0], list):
            files_to_load = [
                item for sublist in files_to_load for item in sublist
            ]

        # Only load in files into tplot if we actually downloaded CDF files
        cdf_files = [f for f in files_to_load if '.cdf' in f]
        sts_files = [f for f in files_to_load if '.sts' in f]
        kp_files = [f for f in files_to_load if '.tab' in f]

        loaded_tplot_vars = []
        if not download_only:

            for f in cdf_files:
                # Loop through CDF files
                desc = l2_regex.match(os.path.basename(f)).group("description")
                if desc != '' and suffix == '':
                    created_vars = pytplot.cdf_to_tplot(
                        f,
                        varformat=varformat,
                        varnames=varnames,
                        string_encoding='utf-8',
                        get_support_data=get_support_data,
                        prefix=prefix,
                        suffix=desc,
                        merge=True)
                else:
                    created_vars = pytplot.cdf_to_tplot(
                        f,
                        varformat=varformat,
                        varnames=varnames,
                        string_encoding='utf-8',
                        get_support_data=get_support_data,
                        prefix=prefix,
                        suffix=suffix,
                        merge=True)

                # Specifically for SWIA and SWEA data, make sure the plots have log axes and are spectrograms
                instr = l2_regex.match(os.path.basename(f)).group("instrument")
                if instr in ["swi", "swe"]:
                    pytplot.options(created_vars, 'spec', 1)
                loaded_tplot_vars.append(created_vars)

            for f in sts_files:
                # Loop through STS (Mag) files
                desc = l2_regex.match(os.path.basename(f)).group("description")
                if desc != '' and suffix == '':
                    loaded_tplot_vars.append(
                        pytplot.sts_to_tplot(f,
                                             prefix=prefix,
                                             suffix=desc,
                                             merge=True))
                else:
                    loaded_tplot_vars.append(
                        pytplot.sts_to_tplot(f,
                                             prefix=prefix,
                                             suffix=suffix,
                                             merge=True))

                # Remove the Decimal Day column, not really useful
                for tvar in loaded_tplot_vars:
                    if "DDAY_" in tvar:
                        pytplot.del_data(tvar)
                        del tvar

            # Flatten out the list and only grab the unique tplot variables
            flat_list = list(
                set([
                    item for sublist in loaded_tplot_vars for item in sublist
                ]))

            # Load in KP data specifically for all of the Ancillary data (position, attitude, Ls, etc)
            if kp_files != []:
                kp_data_loaded = maven_kp_to_tplot(
                    filename=kp_files,
                    ancillary_only=ancillary_only,
                    instruments=instruments)

                # Link all created KP data to the ancillary KP data
                for tvar in kp_data_loaded:
                    pytplot.link(tvar,
                                 "mvn_kp::spacecraft::altitude",
                                 link_type='alt')
                    pytplot.link(tvar,
                                 "mvn_kp::spacecraft::mso_x",
                                 link_type='x')
                    pytplot.link(tvar,
                                 "mvn_kp::spacecraft::mso_y",
                                 link_type='y')
                    pytplot.link(tvar,
                                 "mvn_kp::spacecraft::mso_z",
                                 link_type='z')
                    pytplot.link(tvar,
                                 "mvn_kp::spacecraft::geo_x",
                                 link_type='geo_x')
                    pytplot.link(tvar,
                                 "mvn_kp::spacecraft::geo_y",
                                 link_type='geo_y')
                    pytplot.link(tvar,
                                 "mvn_kp::spacecraft::geo_z",
                                 link_type='geo_z')
                    pytplot.link(tvar,
                                 "mvn_kp::spacecraft::sub_sc_longitude",
                                 link_type='lon')
                    pytplot.link(tvar,
                                 "mvn_kp::spacecraft::sub_sc_latitude",
                                 link_type='lat')

            # Link all created tplot variables to the corresponding KP data
            for tvar in flat_list:
                pytplot.link(tvar,
                             "mvn_kp::spacecraft::altitude",
                             link_type='alt')
                pytplot.link(tvar, "mvn_kp::spacecraft::mso_x", link_type='x')
                pytplot.link(tvar, "mvn_kp::spacecraft::mso_y", link_type='y')
                pytplot.link(tvar, "mvn_kp::spacecraft::mso_z", link_type='z')
                pytplot.link(tvar,
                             "mvn_kp::spacecraft::geo_x",
                             link_type='geo_x')
                pytplot.link(tvar,
                             "mvn_kp::spacecraft::geo_y",
                             link_type='geo_y')
                pytplot.link(tvar,
                             "mvn_kp::spacecraft::geo_z",
                             link_type='geo_z')
                pytplot.link(tvar,
                             "mvn_kp::spacecraft::sub_sc_longitude",
                             link_type='lon')
                pytplot.link(tvar,
                             "mvn_kp::spacecraft::sub_sc_latitude",
                             link_type='lat')

            # Return list of unique KP data
            return flat_list