Example #1
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
Example #2
0
def ex_spikes(plot=True):
    """Load GMAG data and show how to remove spikes."""
    # 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.
    if plot:
        pytplot.tplot(pytplot.tplot_names())

    # Return 1 as indication that the example finished without problems.
    return 1
Example #3
0
def tnames(pattern=None):
    """
    Find pytplot names.

    Parameters
    ----------
    pattern : str, optional
        Patern to search for. The default is None, which returns all names.

    Returns
    -------
    name_list : list of str
        List of pytplot variables.

    """
    name_list = list()
    all_names = tplot_names(quiet=True)

    if pattern is None:
        name_list.extend(all_names)
    else:
        if isinstance(pattern, str):
            name_list.extend(fnmatch.filter(all_names, pattern))
        else:
            for p in pattern:
                name_list.extend(fnmatch.filter(all_names, p))

    return name_list
Example #4
0
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'

        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(tplot_names())

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

                for var_specifier in [
                        '_b_gse_', '_b_gsm_', '_b_dmpa_', '_b_bcs_'
                ]:
                    var_name = 'mms' + 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})
Example #5
0
                     no_download=False)

# Load validation variables
filename = calfile[0]
pytplot.tplot_restore(filename)
tha_fit_idl = pytplot.get_data('tha_fit')
tha_fgs_idl = pytplot.get_data('tha_fgs')
tha_fgs_sigma_idl = pytplot.get_data('tha_fgs_sigma')
tha_fit_bfit_idl = pytplot.get_data('tha_fit_bfit')
tha_fit_efit_idl = pytplot.get_data('tha_fit_efit')
tha_efs_idl = pytplot.get_data('tha_efs')
tha_efs_sigma_idl = pytplot.get_data('tha_efs_sigma')
tha_efs_0_idl = pytplot.get_data('tha_efs_0')
tha_efs_dot0_idl = pytplot.get_data('tha_efs_dot0')

pytplot.tplot_names()
pytplot.del_data('*')

filename = nocalfile[0]
pytplot.tplot_restore(filename)
tha_efs_idl_no_cal = pytplot.get_data('tha_efs')

pytplot.tplot_names()
pytplot.del_data('*')

t = ['2008-03-15', '2008-03-16']
pyspedas.themis.fit(trange=t,
                    probe='a',
                    level='l1',
                    get_support_data=True,
                    varnames=['tha_fit', 'tha_fit_code', 'tha_fit_npts'],
Example #6
0
def dsl2gse(name_in, spinras, spindec, name_out, isgsetodsl=0):
    """Transform dsl to gse.

    Parameters
    ----------
        name_in: str
            Name of input pytplot variable (eg. 'tha_fgl_dsl')
        spinras: str
            Name of pytplot variable for spin (eg.'tha_spinras').
        spindec: str
            Name of pytplot variable for spin (eg.'tha_spinras').
        name_out: str
            Name of output pytplot variable (eg. 'tha_fgl_gse')
        isgsetodsl: bool
            If 0 (default) then DSL to GSE.
            If 1, then GSE to DSL.

    Returns
    -------
        1 for sucessful completion.

    """
    all_names = pytplot.tplot_names()
    needed_vars = [name_in, spinras, spindec]
    c = [value for value in needed_vars if value in all_names]
    if len(c) < 3:
        print("Variables needed: " + str(needed_vars))
        m = [value for value in needed_vars if value not in c]
        print("Variables missing: " + str(m))
        print("Please load missing variables.")
        return

    # Interpolate spinras and spindec
    spinnames_in = [spinras, spindec]
    hiras_name = spinras + '_hires'
    hidec_name = spindec + '_hires'
    hi_names = [hiras_name, hidec_name]

    # If new names exist, delete the variables
    if hiras_name in pytplot.tplot_names():
        pytplot.del_data(hiras_name)
    if hidec_name in pytplot.tplot_names():
        pytplot.del_data(hidec_name)

    pyspedas.tinterpol(spinnames_in,
                       name_in,
                       method="linear",
                       newname=hi_names,
                       suffix='')

    # Get data
    data_in = pytplot.get_data(name_in)
    data_ras = pytplot.get_data(hiras_name)
    data_dec = pytplot.get_data(hidec_name)

    # Make a unit vector that points along the spin axis
    spla = (90.0 - (data_dec[1])) * np.pi / 180.0
    splo = data_ras[1] * np.pi / 180.0
    # spherical to cartesian
    zscs0 = np.sin(spla) * np.cos(splo)
    zscs1 = np.sin(spla) * np.sin(splo)
    zscs2 = np.cos(spla)
    zscs = np.column_stack((zscs0, zscs1, zscs2))

    # unit vector that points along the spin axis in GSE
    trgse = subgei2gse(data_in[0], zscs)
    zgse = [trgse[:, 0], trgse[:, 1], trgse[:, 2]]
    sun = [1.0, 0.0, 0.0]
    yscs = [
        zgse[1] * sun[2] - zgse[2] * sun[1],
        zgse[2] * sun[0] - zgse[0] * sun[2],
        zgse[0] * sun[1] - zgse[1] * sun[0]
    ]
    yscsNorm = np.sqrt(yscs[0]**2.0 + yscs[1]**2.0 + yscs[2]**2.0)
    yscs = yscs / yscsNorm
    xscs = [
        yscs[1] * zgse[2] - yscs[2] * zgse[1],
        yscs[2] * zgse[0] - yscs[0] * zgse[2],
        yscs[0] * zgse[1] - yscs[1] * zgse[0]
    ]

    if isgsetodsl == 0:
        # DSL -> GSE
        dd = data_in[1]
        d0 = dd[:, 0] * xscs[0] + dd[:, 1] * yscs[0] + dd[:, 2] * zgse[0]
        d1 = dd[:, 0] * xscs[1] + dd[:, 1] * yscs[1] + dd[:, 2] * zgse[1]
        d2 = dd[:, 0] * xscs[2] + dd[:, 1] * yscs[2] + dd[:, 2] * zgse[2]

    else:
        # GSE -> DSL
        dd = data_in[1]
        d0 = dd[:, 0] * xscs[0] + dd[:, 1] * xscs[1] + dd[:, 2] * xscs[2]
        d1 = dd[:, 0] * yscs[0] + dd[:, 1] * yscs[1] + dd[:, 2] * yscs[2]
        d2 = dd[:, 0] * zgse[0] + dd[:, 1] * zgse[1] + dd[:, 2] * zgse[2]

    dd_out = [d0, d1, d2]
    data_out = np.column_stack(dd_out)

    pytplot.store_data(name_out, data={'x': data_in[0], 'y': data_out})

    return 1
Example #7
0
def dsi2j2000(name_in=None,
              name_out=None,
              no_orb=False,
              J20002DSI=False,
              noload=False):
    """
    This function transform a time series data between the DSI and J2000 coordinate systems

    Parameters:

        name_in : str
            input tplot variable to be transformed

        name_out : str
            Name of the tplot variable in which the transformed data is stored

        J20002DSI : bool
            Set to transform data from J2000 to DSI. If not set, it transforms data from DSI to J2000.

    Returns:
        None

    """
    if (name_in is None) or (name_in not in tplot_names(quiet=True)):
        print('Input of Tplot name is undifiend')
        return

    if name_out is None:
        print('Tplot name for output is undifiend')
        name_out = 'result_of_dsi2j2000'

    # prepare for transformed Tplot Variable
    reload = not noload
    dl_in = get_data(name_in, metadata=True)
    get_data_array = get_data(name_in)
    time_array = get_data_array[0]
    time_length = time_array.shape[0]
    dat = get_data_array[1]

    # Get the SGI axis by interpolating the attitude data
    dsiz_j2000 = erg_interpolate_att(name_in, noload=noload)['sgiz_j2000']

    # Sun direction in J2000
    sundir = np.array([[1., 0., 0.]]*time_length)

    if no_orb:
        store_data('sundir_gse', data={'x': time_array, 'y': sundir})

    else:  # Calculate the sun directions from the instantaneous satellite locations
        if reload:
            tr = get_timespan(name_in)
            orb(trange=time_string([tr[0] - 60., tr[1] + 60.]))
            tinterpol('erg_orb_l2_pos_gse', time_array)
            scpos = get_data('erg_orb_l2_pos_gse-itrp')[1]
            sunpos = np.array([[1.496e+08, 0., 0.]]*time_length)
            sundir = sunpos - scpos
            store_data('sundir_gse', data={'x': time_array, 'y': sundir})
            tnormalize('sundir_gse', newname='sundir_gse')

    # Derive DSI-X and DSI-Y axis vectors in J2000.
    # The elementary vectors below are the definition of DSI. The detailed relationship
    # between the spin phase, sun pulse timing, sun direction, and the actual subsolar point
    # on the spining s/c body should be incorporated into the calculation below.
    if reload:
        cotrans(name_in='sundir_gse', name_out='sundir_j2000',
                coord_in='gse', coord_out='j2000')
    sun_j2000 = get_data('sundir_j2000')
    dsiy = tcrossp(dsiz_j2000['y'], sun_j2000[1], return_data=True)
    dsix = tcrossp(dsiy, dsiz_j2000['y'], return_data=True)
    dsix_j2000 = {'x': time_array, 'y': dsix}
    dsiy_j2000 = {'x': time_array, 'y': dsiy}

    if not J20002DSI:
        print('DSI --> J2000')
        mat = cart_trans_matrix_make(
            dsix_j2000['y'], dsiy_j2000['y'], dsiz_j2000['y'])
        j2000x_in_dsi = np.dot(mat, np.array([1., 0., 0.]))
        j2000y_in_dsi = np.dot(mat, np.array([0., 1., 0.]))
        j2000z_in_dsi = np.dot(mat, np.array([0., 0., 1.]))
        mat = cart_trans_matrix_make(
            j2000x_in_dsi, j2000y_in_dsi, j2000z_in_dsi)
        dat_new = np.einsum("ijk,ik->ij", mat, dat)
    else:
        print('J2000 --> DSI')
        mat = cart_trans_matrix_make(
            dsix_j2000['y'], dsiy_j2000['y'], dsiz_j2000['y'])
        dat_new = np.einsum("ijk,ik->ij", mat, dat)

    store_data(name_out, data={'x': time_array, 'y': dat_new}, attr_dict=dl_in)
    options(name_out, 'ytitle', '\n'.join(name_out.split('_')))
Example #8
0
"""
"""""" """""" """""" """""" """""" """""" """""" """""" """""" """""" """""" """""" """""" """""" """""" """
BARREL 2016 mission
""" """""" """""" """""" """""" """""" """""" """""" """""" """""" """""" """""" """""" """""" """""" """"""

p4 = ['4A', '4D', '4G', '4B', '4E', '4H']
p4s = []
for count, i in enumerate(p4):
    p4s.append("$" + p4[count] + "$")

fn = 'barrel_mission4_ephem_allpayloads.tplot'
pytplot.tplot_restore(pathbarrel + fn)

#Get the times for the first BARREL mission. All payloads have been interpolated to a common time base
tn = pytplot.tplot_names()
d = pytplot.get_data('L_Kp2_' + p4[0] + '_interp')
bar4t = d[0]

#Turn BARREL times into datetime list
bar4tt = np.ndarray.tolist(bar4t)
bar4t_dt = []
for i in bar4tt:
    bar4t_dt.append(datetime.datetime.fromtimestamp(i))

#Now load the data for each payload and append to array
dat4L = []
for i in p4:
    d = pytplot.get_data('L_Kp2_' + i + '_interp')
    tmp = np.ndarray.tolist(d[1])
    dat4L.append([tmp])
Example #9
0
def sga2sgi(name_in=None, name_out=None, SGI2SGA=False, noload=False):
    """
    This transform a time series data between the SGA and SGI coordinate systems.

    Parameters:

        name_in : str
            input tplot variable to be transformed

        name_out : str
            Name of the tplot variable in which the transformed data is stored

        SGI2SGA : bool
              Set to transform data from SGI to SGA. If not set, it transforms data from SGA to SGI.

    Returns:
        None

    """
    if (name_in is None) or (name_in not in tplot_names(quiet=True)):
        print('Input of Tplot name is undifiend')
        return

    if name_out is None:
        print('Tplot name for output is undifiend')
        name_out = 'result_of_sga2sgi'

    get_data_vars = get_data(name_in)
    dl_in = get_data(name_in, metadata=True)
    time_array = get_data_vars[0]
    time_length = time_array.shape[0]
    dat = get_data_vars[1]

    # Get the SGA and SGI axes by interpolating the attitude data
    interpolated_values = erg_interpolate_att(name_in, noload=noload)
    sgix = interpolated_values['sgix_j2000']['y']
    sgiy = interpolated_values['sgiy_j2000']['y']
    sgiz = interpolated_values['sgiz_j2000']['y']
    sgax = interpolated_values['sgax_j2000']['y']
    sgay = interpolated_values['sgay_j2000']['y']
    sgaz = interpolated_values['sgaz_j2000']['y']

    if not SGI2SGA:
        print('SGA --> SGI')
        coord_out = 'sgi'

        # Transform SGI-X,Y,Z axis unit vectors in J2000 to those in SGA
        mat = cart_trans_matrix_make(sgax, sgay, sgaz)
        sgix_in_sga = np.einsum("ijk,ik->ij", mat, sgix)
        sgiy_in_sga = np.einsum("ijk,ik->ij", mat, sgiy)
        sgiz_in_sga = np.einsum("ijk,ik->ij", mat, sgiz)

        # Now transform the given vector in SGA to those in SGI
        mat = cart_trans_matrix_make(sgix_in_sga, sgiy_in_sga, sgiz_in_sga)
        dat_new = np.einsum("ijk,ik->ij", mat, dat)

    else:
        print('SGI --> SGA')
        coord_out = 'sga'

        # Transform SGA-X,Y,Z axis unit vectors in J2000 to those in SGI
        mat = cart_trans_matrix_make(sgix, sgiy, sgiz)
        sgax_in_sgi = np.einsum("ijk,ik->ij", mat, sgax)
        sgay_in_sgi = np.einsum("ijk,ik->ij", mat, sgay)
        sgaz_in_sgi = np.einsum("ijk,ik->ij", mat, sgaz)

        # Now transform the given vector in SGI to those in SGA
        mat = cart_trans_matrix_make(sgax_in_sgi, sgay_in_sgi, sgaz_in_sgi)
        dat_new = np.einsum("ijk,ik->ij", mat, dat)

    # Store the converted data in a tplot variable
    store_data(name_out, data={'x': time_array, 'y': dat_new}, attr_dict=dl_in)
    options(name_out, 'ytitle', '\n'.join(name_out.split('_')))
Example #10
0
# and all GMAG stations, see: http://themis.ssl.berkeley.edu/gmag/gmag_list.php
#
# Get a list of the GMAG stations that belong to the EPO group.
sites = gmag_list(group='epo')

####################################################################################
# Download cdf files for all EPO GMAG stations and load data into pytplot variables.
#
# Some GMAG stations may not have any data files for the specified time interval.
# In that case, we will get an error message that the remote file does not exist
# for that GMAG station.
load_data('gmag', time_range, sites, '', '')

####################################################################################
# Print the names of the loaded GMAG sites.
sites_loaded = tplot_names()

####################################################################################
# Subtract the average values for these sites.
subtract_average(sites_loaded, '')

####################################################################################
# Download AE index data.
load_data('gmag', time_range, ['idx'], '', '')

####################################################################################
# Get a list of all the loaded GMAG sites plus the AE index data.
sites_loaded = tplot_names()

####################################################################################
# Plot GMAG and AE index data.
Example #11
0
def cal_fit(probe='a', no_cal=False):
    """
    Converts raw FIT parameter data into physical quantities.
    Warning: This function is in debug state

    Currently, it assumes that "th?_fit" variable is already loaded

    Parameters:
        probe: str
            Spacecraft probe letter ('a', 'b', 'c', 'd' and/or 'e')
        no_cal: bool
            If ture do not apply boom shortening factor or Ex offset defaults

    Returns:
        th?_fgs tplot variable
    """
    import math
    import numpy as np
    import logging

    from pytplot import get_data, store_data, tplot_names, options
    from pyspedas.utilities.download import download
    from pyspedas.themis.config import CONFIG
    from pyspedas.utilities.time_double import time_float_one
    from copy import deepcopy
    from numpy.linalg import inv

    if probe == 'f':
        logging.warning(
            f"Probe f is not supported. Please use IDL version of 'thm_cal_fit' in SPEDAS."
        )
        return

    # calibration parameters
    lv12 = 49.6  # m
    lv34 = 40.4  # m
    lv56 = 5.6  # m

    # This values provide better agreement with IDL
    lv12 = 49.599997
    lv34 = 40.400003
    lv56 = 5.59999981

    # calibration table
    cpar = {
        "e12": {
            "cal_par_time": '2002-01-01/00:00:00',
            "Ascale": -15000.0 / (lv12 * 2.**15.),
            "Bscale": -15000.0 / (lv12 * 2.**15.),
            "Cscale": -15000.0 / (lv12 * 2.**15.),
            "theta": 0.0,
            "sigscale": 15000. / (lv12 * 2.**15.),
            "Zscale": -15000. / (lv56 * 2.**15.),
            "units": 'mV/m'
        },
        "e34": {
            "cal_par_time": '2002-01-01/00:00:00',
            "Ascale": -15000.0 / (lv34 * 2.**15.),
            "Bscale": -15000.0 / (lv34 * 2.**15.),
            "Cscale": -15000.0 / (lv34 * 2.**15.),
            "theta": 0.0,
            "sigscale": 15000. / (lv34 * 2.**15.),
            "Zscale": -15000. / (lv56 * 2.**15.),
            "units": 'mV/m'
        },
        "b": {
            "cal_par_time": '2002-01-01/00:00:00',
            "Ascale": 1.,
            "Bscale": 1.,
            "Cscale": 1.,
            "theta": 0.0,
            "sigscale": 1.,
            "Zscale": 1.,
            "units": 'nT'
        }
    }

    # tplot options
    color_str = ['blue', 'green', 'red']
    color_str2 = ['magenta', 'blue', 'cyan', 'green', 'orange']
    b_str = ['Bx', 'By', 'Bz']
    e_str = ['Ex', 'Ey', 'Ez']
    b_units = cpar['b']['units']
    e_units = cpar['e12']['units']
    b_units_str = f'[{b_units}]'
    e_units_str = f'[{e_units}]'
    b_data_att = {
        'units': b_units,
        'cal_par_time': cpar['b']['cal_par_time'],
        'data_type': 'calibrated',
        'coord_sys': 'dsl'
    }
    b_data_att_sigma = {'units': b_units}
    e_data_att = {
        'units': e_units,
        'cal_par_time': cpar['e12']['cal_par_time'],
        'data_type': 'calibrated',
        'coord_sys': 'dsl'
    }
    e_data_att_sigma = {'units': e_units}

    b_opt_dict = {
        'legend_names': b_str,
        'ysubtitle': b_units_str,
        'color': color_str,
        'alpha': 1
    }
    e_opt_dict = {
        'legend_names': e_str,
        'ysubtitle': e_units_str,
        'color': color_str,
        'alpha': 1
    }

    # if tplot does not show 5th legend name, update tplot
    b_opt_dict2 = {
        'legend_names': ['A', 'B', 'C', 'Sig', '<Bz>'],
        'ysubtitle': b_units_str,
        'color': color_str2,
        'alpha': 1
    }
    e_opt_dict2 = {
        'legend_names': ['A', 'B', 'C', 'Sig', '<Ez>'],
        'ysubtitle': e_units_str,
        'color': color_str2,
        'alpha': 1
    }

    # Get list of tplot variables
    tnames = tplot_names(True)  # True for quiet output

    # Get data from th?_fit variable
    tvar = 'th' + probe + '_fit'

    # B-field fit (FGM) processing

    if tvar not in tnames:
        logging.warning(f"Variable {tvar} is not found")
        return

    # Using deep copy to create an independent instance
    d = deepcopy(get_data(
        tvar))  # NOTE: Indexes are not the same as in SPEDAS, e.g. 27888x2x5

    # establish probe number in cal tables
    sclist = {
        'a': 0,
        'b': 1,
        'c': 2,
        'd': 3,
        'e': 4,
        'f': -1
    }  # for probe 'f' no flatsat FGM cal files
    scn = sclist[probe]

    #  Rotation vectors
    rotBxy_angles = [
        29.95, 29.95, 29.95, 29.95, 29.95
    ]  # vassilis 6/2/2007: deg to rotate FIT on spin plane to match DSL on 5/4
    rotBxy = rotBxy_angles[
        scn]  # vassilis 4/28: probably should be part of CAL table as well...
    cs = math.cos(rotBxy * math.pi / 180)  # vassilis
    sn = math.sin(rotBxy * math.pi / 180)  # vassilis

    adc2nT = 50000. / 2.**24  # vassilis 2007 - 04 - 03

    # B - field fit(FGM)
    i = 1

    d.y[:, i, 0] = cpar['b']['Ascale'] * d.y[:, i, 0] * adc2nT  # vassilis
    d.y[:, i, 1] = cpar['b']['Bscale'] * d.y[:, i, 1] * adc2nT  # vassilis
    d.y[:, i, 2] = cpar['b']['Cscale'] * d.y[:, i, 2] * adc2nT  # vassilis
    d.y[:, i, 3] = cpar['b']['sigscale'] * d.y[:, i, 3] * adc2nT  # vassilis
    d.y[:, i, 4] = cpar['b']['Zscale'] * d.y[:, i, 4] * adc2nT  # vassilis

    # Calculating Bzoffset using thx+'/l1/fgm/0000/'+thx+'_fgmcal.txt'
    thx = 'th' + probe
    remote_name = thx + '/l1/fgm/0000/' + thx + '_fgmcal.txt'
    calfile = download(remote_file=remote_name,
                       remote_path=CONFIG['remote_data_dir'],
                       local_path=CONFIG['local_data_dir'],
                       no_download=False)
    if not calfile:
        # This code should never be executed
        logging.warning(f"Calibration file {thx}_fgmcal.txt is not found")
        return

    caldata = np.loadtxt(calfile[0], converters={0: time_float_one})
    # TODO: In SPEDAS we checks if data is already calibrated
    # Limit to the time of interest
    caltime = caldata[:, 0]
    t1 = np.nonzero(caltime < d.times.min())[0]  # left time
    t2 = np.nonzero(caltime <= d.times.max())[0]  # right time

    Bzoffset = np.zeros(d.times.shape)
    if t1.size + t2.size > 1:  # Time range exist in the file
        tidx = np.arange(t1[-1], t2[-1] + 1)
        caltime = caltime[tidx]
        offi = caldata[tidx, 1:4]  # col 1-3
        cali = caldata[tidx, 4:13]  # col 4-13
        # spinperii = caldata[tidx, 13]  # col 14 not in use
        flipxz = -1 * np.fliplr(np.identity(3))
        # SPEDAS: offi2 = invert(transpose([cali[istart, 0:2], cali[istart, 3:5], cali[istart, 6:8]]) ## flipxz)##offi[istart, *]
        for t in range(0, caltime.size):
            offi2 = inv(np.c_[cali[t, 0:3], cali[t, 3:6], cali[t, 6:9]].T
                        @ flipxz) @ offi[t, :]
            tidx = d.times >= caltime[t]
            Bzoffset[tidx] = offi2[2]  # last element

    Bxprime = cs * d.y[:, i, 1] + sn * d.y[:, i, 2]
    Byprime = -sn * d.y[:, i, 1] + cs * d.y[:, i, 2]
    Bzprime = -d.y[:, i,
                   4] - Bzoffset  # vassilis 4/28 (SUBTRACTING offset from spinaxis POSITIVE direction)

    # d is a namedtuple and does not support direct copy by value
    dprime = deepcopy(d)
    dprime.y[:, i, 1] = Bxprime  # vassilis DSL
    dprime.y[:, i, 2] = Byprime  # vassilis DSL
    dprime.y[:, i, 4] = Bzprime  # vassilis DSL

    # Create fgs variable and remove nans
    fgs = dprime.y[:, i, [1, 2, 4]]
    idx = ~np.isnan(fgs[:, 0])
    fgs_data = {'x': d.times[idx], 'y': fgs[idx, :]}

    # Save fgs tplot variable
    tvar = 'th' + probe + '_fgs'
    store_data(tvar, fgs_data, attr_dict=b_data_att)
    options(tvar, opt_dict=b_opt_dict)

    # Save fgs_sigma variable
    fit_sigma_data = {'x': d.times[idx], 'y': d.y[idx, i, 3]}
    tvar = 'th' + probe + '_fgs_sigma'
    store_data(tvar, fit_sigma_data, attr_dict=b_data_att_sigma)
    options(tvar, opt_dict=b_opt_dict)

    # Save bfit variable
    bfit_data = {'x': d.times[:], 'y': d.y[:, i, :].squeeze()}
    tvar = 'th' + probe + '_fit_bfit'
    store_data(tvar, bfit_data, attr_dict=b_data_att)
    options(tvar, opt_dict=b_opt_dict2)

    # E-field fit (EFI) processing

    # Get data from th?_fit_code variable
    tvar = 'th' + probe + '_fit_code'
    d_code = None  # Blank variable, if d_code is not used

    if tvar in tnames:
        i = 0
        d_code = get_data(tvar)
        e12_ss = (d_code.y[:, i] == int("e1", 16)) | (d_code.y[:, i] == int(
            "e5", 16))
        e34_ss = (d_code.y[:, i] == int("e3", 16)) | (d_code.y[:, i] == int(
            "e7", 16))
    else:
        # Default values (if no code)
        ne12 = d.times.size
        e12_ss = np.ones(ne12, dtype=bool)  # create an index arrays
        e34_ss = np.zeros(ne12, dtype=bool)

    # Save 'efs' datatype before "hard wired" calibrations.
    # An EFI-style calibration is performed below.
    i = 0
    efs = d.y[:, i, [1, 2, 4]]
    # Locate samples with non-NaN data values.  Save the indices in
    # efsx_good, then at the end of calibration, pull the "good"
    # indices out of the calibrated efs[] array to make the thx_efs
    # tplot variable.
    efsx_good = ~np.isnan(efs[:, 0])

    if np.any(efsx_good):
        if np.any(
                e34_ss
        ):  # rotate efs 90 degrees if necessary, if e34 was used in spinfit
            tmp = d.y[e34_ss, i, :]  # Apply logical arrays
            # TODO: there should be a better way to combine indexes
            efs[e34_ss, :] = tmp[:, [2, 1, 4]]  # Apply dimension selection
            efs[e34_ss, 0] = -efs[e34_ss, 0]

    efsz = d.y[:, i,
               4]  # save Ez separately, for possibility that it's the SC potential

    # Use cpar to calibrate
    if np.any(e12_ss):
        d.y[e12_ss, i, 0] = cpar["e12"]["Ascale"] * d.y[e12_ss, i, 0]
        d.y[e12_ss, i, 1] = cpar["e12"]["Bscale"] * d.y[e12_ss, i, 1]
        d.y[e12_ss, i, 2] = cpar["e12"]["Cscale"] * d.y[e12_ss, i, 2]
        d.y[e12_ss, i, 3] = cpar["e12"]["sigscale"] * d.y[e12_ss, i, 3]
        d.y[e12_ss, i, 4] = cpar["e12"]["Zscale"] * d.y[e12_ss, i, 4]
    if np.any(e34_ss):
        d.y[e34_ss, i, 0] = cpar["e34"]["Ascale"] * d.y[e34_ss, i, 0]
        d.y[e34_ss, i, 1] = cpar["e34"]["Bscale"] * d.y[e34_ss, i, 1]
        d.y[e34_ss, i, 2] = cpar["e34"]["Cscale"] * d.y[e34_ss, i, 2]
        d.y[e34_ss, i, 3] = cpar["e34"]["sigscale"] * d.y[e34_ss, i, 3]
        d.y[e34_ss, i, 4] = cpar["e34"]["Zscale"] * d.y[e34_ss, i, 4]

    # save fit_efit variable
    fit_efit_data = {'x': d.times, 'y': d.y[:, i, :]}
    tvar = 'th' + probe + '_fit_efit'
    store_data(tvar, fit_efit_data, attr_dict=e_data_att)
    options(tvar, opt_dict=e_opt_dict2)

    # thx_efs and thx_efs_sigma,
    # Calibrate efs data by applying E12 calibration factors, not despinning, then applying despun (spin-dependent)
    # calibration factors from E12 (the spin-independent offset is subtracted on-board):

    # Load calibration file, e.g. tha/l1/eff/0000/tha_efi_calib_params.txt
    remote_name = thx + '/l1/eff/0000/' + thx + '_efi_calib_params.txt'
    eficalfile = download(remote_file=remote_name,
                          remote_path=CONFIG['remote_data_dir'],
                          local_path=CONFIG['local_data_dir'],
                          no_download=False)

    if not eficalfile:
        # This code should never be executed
        logging.warning(
            f"Calibration file {thx}_efi_calib_params.txt is not found")
        return

    colnums = {
        "time": [0],
        "edc_offset": [14, 15, 16],
        "edc_gain": [17, 18, 19],
        "BOOM_LENGTH": [26, 27, 28],
        "BOOM_SHORTING_FACTOR": [29, 30, 31],
        "DSC_OFFSET": [32, 33, 34]
    }  # List of columns to be loaded
    collist = list()
    [collist.extend(cnum) for cnum in colnums.values()]
    collist.sort()  # ensurer that the list of columns is sorted
    eficaltxt = np.loadtxt(eficalfile[0],
                           skiprows=1,
                           max_rows=1,
                           converters={0: time_float_one},
                           usecols=collist)
    eficaldata = {
        "time": eficaltxt[0],
        "gain": eficaltxt[4:7],
        "offset": eficaltxt[1:4],
        "boom_length": eficaltxt[7:10],
        "boom_shorting_factor": eficaltxt[10:13],
        "dsc_offset": eficaltxt[13:16]
    }

    # Boom
    exx = eficaldata["boom_length"]
    if not no_cal:
        exx *= eficaldata["boom_shorting_factor"]

    # Calibrate E field
    # Calibrate Ex and Ey spinfits that are derived from E12 only!
    if np.any(e12_ss):
        efs[e12_ss,
            0:2] = -1000. * eficaldata["gain"][0] * efs[e12_ss, 0:2] / exx[0]
    if np.any(e34_ss):
        efs[e34_ss,
            0:2] = -1000. * eficaldata["gain"][1] * efs[e34_ss, 0:2] / exx[1]
    # Calibrate Ez spinfit by itself:
    efs[:, 2] = -1000. * eficaldata["gain"][2] * efs[:, 2] / exx[2]

    # DC Offset
    if not no_cal:
        efs -= eficaldata["dsc_offset"]

    # Here, if the fit_code is 'e5'x (229) then efs[*,2] contains the spacecraft potential, so set all of those values
    # to Nan, jmm, 19-Apr-2010
    # Or if the fit_code is 'e7'x (231), this will also be including the SC potential, jmm,22-oct-2010
    if d_code is not None:
        sc_port = (d_code.y[:, i] == int("e5", 16)) | (d_code.y[:, i] == int(
            "e7", 16))
        if np.any(sc_port):
            efs[sc_port, 2] = np.nan

    # save efs variable
    efs_data = {
        'x': d.times[efsx_good],
        'y': efs[efsx_good, :]
    }  # efs[efsx_good,*]
    tvar = 'th' + probe + '_efs'
    store_data(tvar, efs_data, attr_dict=e_data_att)
    options(tvar, opt_dict=e_opt_dict)

    # save efs_sigma variable
    efs_sigma_data = {
        'x': d.times[efsx_good],
        'y': d.y[efsx_good, i, 3]
    }  # d.y[efsx_good, 3, idx]
    tvar = 'th' + probe + '_efs_sigma'
    store_data(tvar, efs_sigma_data, attr_dict=e_data_att_sigma)
    options(tvar, opt_dict=e_opt_dict)

    # save efs_0
    efs_0_data = deepcopy(efs)
    efs_0_data[:, 2] = 0
    efs_0 = {'x': d.times[efsx_good], 'y': efs_0_data[efsx_good, :]}
    tvar = 'th' + probe + '_efs_0'
    store_data(tvar, efs_0, attr_dict=e_data_att_sigma)
    options(tvar, opt_dict=e_opt_dict)

    # calculate efs_dot0
    Ez = (efs[:, 0] * fgs[:, 0] + efs[:, 1] * fgs[:, 1]) / (-1 * fgs[:, 2])
    angle = np.arccos(
        fgs[:, 2] / np.sqrt(np.sum(fgs**2, axis=1))) * 180 / np.pi
    angle80 = angle > 80
    if np.any(angle80):
        Ez[angle80] = np.NaN
    efx_dot0_data = deepcopy(efs)
    efx_dot0_data[:, 2] = Ez

    # save efs_dot0
    efs_dot0 = {'x': d.times[efsx_good], 'y': efx_dot0_data[efsx_good, :]}
    tvar = 'th' + probe + '_efs_dot0'
    store_data(tvar, efs_dot0, attr_dict=e_data_att_sigma)
    options(tvar, opt_dict=e_opt_dict)
Example #12
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 #13
0
def mms_fpi_set_metadata(probe, data_rate, datatype, level, suffix=''):
    """
    This function updates the metadata for FPI 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 FPI include 'brst' and 'fast'. The
            default is 'fast'.

        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(datatype, list): datatype = [datatype]
    if not isinstance(level, list): level = [level]

    probe = [str(p) for p in probe]

    tvars = set(tplot_names())

    for this_probe in probe:
        for this_dr in data_rate:
            for this_lvl in level:
                for this_dtype in datatype:
                    if this_dtype == 'des-moms':
                        if 'mms' + this_probe + '_des_energyspectr_par_' + this_dr + suffix in tvars:
                            options(
                                'mms' + this_probe + '_des_energyspectr_par_' +
                                this_dr + suffix, 'ytitle',
                                'MMS' + this_probe + ' DES (eV)')
                            options(
                                'mms' + this_probe + '_des_energyspectr_par_' +
                                this_dr + suffix, 'ylog', True)
                            options(
                                'mms' + this_probe + '_des_energyspectr_par_' +
                                this_dr + suffix, 'zlog', True)
                            options(
                                'mms' + this_probe + '_des_energyspectr_par_' +
                                this_dr + suffix, 'Colormap', 'jet')
                            options(
                                'mms' + this_probe + '_des_energyspectr_par_' +
                                this_dr + suffix, 'ztitle',
                                '[keV/(cm^2 s sr keV)]')
                            options(
                                'mms' + this_probe + '_des_energyspectr_par_' +
                                this_dr + suffix, 'spec', True)

                        if 'mms' + this_probe + '_des_energyspectr_anti_' + this_dr + suffix in tvars:
                            options(
                                'mms' + this_probe +
                                '_des_energyspectr_anti_' + this_dr + suffix,
                                'ytitle', 'MMS' + this_probe + ' DES (eV)')
                            options(
                                'mms' + this_probe +
                                '_des_energyspectr_anti_' + this_dr + suffix,
                                'ylog', True)
                            options(
                                'mms' + this_probe +
                                '_des_energyspectr_anti_' + this_dr + suffix,
                                'zlog', True)
                            options(
                                'mms' + this_probe +
                                '_des_energyspectr_anti_' + this_dr + suffix,
                                'Colormap', 'jet')
                            options(
                                'mms' + this_probe +
                                '_des_energyspectr_anti_' + this_dr + suffix,
                                'ztitle', '[keV/(cm^2 s sr keV)]')

                        if 'mms' + this_probe + '_des_energyspectr_perp_' + this_dr + suffix in tvars:
                            options(
                                'mms' + this_probe +
                                '_des_energyspectr_perp_' + this_dr + suffix,
                                'ytitle', 'MMS' + this_probe + ' DES (eV)')
                            options(
                                'mms' + this_probe +
                                '_des_energyspectr_perp_' + this_dr + suffix,
                                'ylog', True)
                            options(
                                'mms' + this_probe +
                                '_des_energyspectr_perp_' + this_dr + suffix,
                                'zlog', True)
                            options(
                                'mms' + this_probe +
                                '_des_energyspectr_perp_' + this_dr + suffix,
                                'Colormap', 'jet')
                            options(
                                'mms' + this_probe +
                                '_des_energyspectr_perp_' + this_dr + suffix,
                                'ztitle', '[keV/(cm^2 s sr keV)]')
                            options(
                                'mms' + this_probe +
                                '_des_energyspectr_anti_' + this_dr + suffix,
                                'spec', True)

                        if 'mms' + this_probe + '_des_energyspectr_omni_' + this_dr + suffix in tvars:
                            options(
                                'mms' + this_probe +
                                '_des_energyspectr_omni_' + this_dr + suffix,
                                'ytitle', 'MMS' + this_probe + ' DES (eV)')
                            options(
                                'mms' + this_probe +
                                '_des_energyspectr_omni_' + this_dr + suffix,
                                'ylog', True)
                            options(
                                'mms' + this_probe +
                                '_des_energyspectr_omni_' + this_dr + suffix,
                                'zlog', True)
                            options(
                                'mms' + this_probe +
                                '_des_energyspectr_omni_' + this_dr + suffix,
                                'Colormap', 'jet')
                            options(
                                'mms' + this_probe +
                                '_des_energyspectr_omni_' + this_dr + suffix,
                                'ztitle', '[keV/(cm^2 s sr keV)]')
                            options(
                                'mms' + this_probe +
                                '_des_energyspectr_omni_' + this_dr + suffix,
                                'spec', True)

                        if 'mms' + this_probe + '_des_pitchangdist_lowen_' + this_dr + suffix in tvars:
                            options(
                                'mms' + this_probe +
                                '_des_pitchangdist_lowen_' + this_dr + suffix,
                                'zlog', True)
                            options(
                                'mms' + this_probe +
                                '_des_pitchangdist_lowen_' + this_dr + suffix,
                                'Colormap', 'jet')
                            options(
                                'mms' + this_probe +
                                '_des_pitchangdist_lowen_' + this_dr + suffix,
                                'ytitle', 'MMS' + this_probe + ' DES (deg)')
                            options(
                                'mms' + this_probe +
                                '_des_pitchangdist_lowen_' + this_dr + suffix,
                                'ztitle', '[keV/(cm^2 s sr keV)]')
                            options(
                                'mms' + this_probe +
                                '_des_pitchangdist_lowen_' + this_dr + suffix,
                                'spec', True)

                        if 'mms' + this_probe + '_des_pitchangdist_miden_' + this_dr + suffix in tvars:
                            options(
                                'mms' + this_probe +
                                '_des_pitchangdist_miden_' + this_dr + suffix,
                                'zlog', True)
                            options(
                                'mms' + this_probe +
                                '_des_pitchangdist_miden_' + this_dr + suffix,
                                'Colormap', 'jet')
                            options(
                                'mms' + this_probe +
                                '_des_pitchangdist_miden_' + this_dr + suffix,
                                'ytitle', 'MMS' + this_probe + ' DES (deg)')
                            options(
                                'mms' + this_probe +
                                '_des_pitchangdist_miden_' + this_dr + suffix,
                                'ztitle', '[keV/(cm^2 s sr keV)]')
                            options(
                                'mms' + this_probe +
                                '_des_pitchangdist_miden_' + this_dr + suffix,
                                'spec', True)

                        if 'mms' + this_probe + '_des_pitchangdist_highen_' + this_dr + suffix in tvars:
                            options(
                                'mms' + this_probe +
                                '_des_pitchangdist_highen_' + this_dr + suffix,
                                'zlog', True)
                            options(
                                'mms' + this_probe +
                                '_des_pitchangdist_highen_' + this_dr + suffix,
                                'Colormap', 'jet')
                            options(
                                'mms' + this_probe +
                                '_des_pitchangdist_highen_' + this_dr + suffix,
                                'ytitle', 'MMS' + this_probe + ' DES (deg)')
                            options(
                                'mms' + this_probe +
                                '_des_pitchangdist_highen_' + this_dr + suffix,
                                'ztitle', '[keV/(cm^2 s sr keV)]')
                            options(
                                'mms' + this_probe +
                                '_des_pitchangdist_highen_' + this_dr + suffix,
                                'spec', True)

                        if 'mms' + this_probe + '_des_bulkv_dbcs_' + this_dr + suffix in tvars:
                            options(
                                'mms' + this_probe + '_des_bulkv_dbcs_' +
                                this_dr + suffix, 'color', ['b', 'g', 'r'])
                            options(
                                'mms' + this_probe + '_des_bulkv_dbcs_' +
                                this_dr + suffix, 'legend_names',
                                ['Vx DBCS', 'Vy DBCS', 'Vz DBCS'])
                            options(
                                'mms' + this_probe + '_des_bulkv_dbcs_' +
                                this_dr + suffix, 'ytitle',
                                'MMS' + this_probe + ' DES velocity (km/s)')

                        if 'mms' + this_probe + '_des_bulkv_gse_' + this_dr + suffix in tvars:
                            options(
                                'mms' + this_probe + '_des_bulkv_gse_' +
                                this_dr + suffix, 'color', ['b', 'g', 'r'])
                            options(
                                'mms' + this_probe + '_des_bulkv_gse_' +
                                this_dr + suffix, 'legend_names',
                                ['Vx GSE', 'Vy GSE', 'Vz GSE'])
                            options(
                                'mms' + this_probe + '_des_bulkv_gse_' +
                                this_dr + suffix, 'ytitle',
                                'MMS' + this_probe + ' DES velocity (km/s)')

                        if 'mms' + this_probe + '_des_numberdensity_' + this_dr + suffix in tvars:
                            options(
                                'mms' + this_probe + '_des_numberdensity_' +
                                this_dr + suffix, 'ytitle',
                                'MMS' + this_probe + ' DES density (cm^-3)')

                    elif this_dtype == 'dis-moms':
                        if 'mms' + this_probe + '_dis_energyspectr_omni_' + this_dr + suffix in tvars:
                            options(
                                'mms' + this_probe +
                                '_dis_energyspectr_omni_' + this_dr + suffix,
                                'ytitle', 'MMS' + this_probe + ' DIS (eV)')
                            options(
                                'mms' + this_probe +
                                '_dis_energyspectr_omni_' + this_dr + suffix,
                                'ylog', True)
                            options(
                                'mms' + this_probe +
                                '_dis_energyspectr_omni_' + this_dr + suffix,
                                'zlog', True)
                            options(
                                'mms' + this_probe +
                                '_dis_energyspectr_omni_' + this_dr + suffix,
                                'Colormap', 'jet')
                            options(
                                'mms' + this_probe +
                                '_dis_energyspectr_omni_' + this_dr + suffix,
                                'ztitle', '[keV/(cm^2 s sr keV)]')
                            options(
                                'mms' + this_probe +
                                '_dis_energyspectr_omni_' + this_dr + suffix,
                                'spec', True)

                        if 'mms' + this_probe + '_dis_bulkv_dbcs_' + this_dr + suffix in tvars:
                            options(
                                'mms' + this_probe + '_dis_bulkv_dbcs_' +
                                this_dr + suffix, 'color', ['b', 'g', 'r'])
                            options(
                                'mms' + this_probe + '_dis_bulkv_dbcs_' +
                                this_dr + suffix, 'legend_names',
                                ['Vx DBCS', 'Vy DBCS', 'Vz DBCS'])
                            options(
                                'mms' + this_probe + '_dis_bulkv_dbcs_' +
                                this_dr + suffix, 'ytitle',
                                'MMS' + this_probe + ' DIS velocity (km/s)')

                        if 'mms' + this_probe + '_dis_bulkv_gse_' + this_dr + suffix in tvars:
                            options(
                                'mms' + this_probe + '_dis_bulkv_gse_' +
                                this_dr + suffix, 'color', ['b', 'g', 'r'])
                            options(
                                'mms' + this_probe + '_dis_bulkv_gse_' +
                                this_dr + suffix, 'legend_names',
                                ['Vx GSE', 'Vy GSE', 'Vz GSE'])
                            options(
                                'mms' + this_probe + '_dis_bulkv_gse_' +
                                this_dr + suffix, 'ytitle',
                                'MMS' + this_probe + ' DIS velocity (km/s)')

                        if 'mms' + this_probe + '_dis_numberdensity_' + this_dr + suffix in tvars:
                            options(
                                'mms' + this_probe + '_dis_numberdensity_' +
                                this_dr + suffix, 'ytitle',
                                'MMS' + this_probe + ' DIS density (cm^-3)')
Example #14
0
mms_load_mec(probe=4)

# find info on a load routine
help(mms_load_mec)

from pyspedas.mms import mms_load_fgm

# note that the keywords are the same as in IDL
mms_load_fgm(probe='4',
             data_rate='brst',
             trange=['2015-10-16/13:06', '2015-10-16/13:07'],
             time_clip=True)

# find which variables were loaded
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_*')
Example #15
0
##############################################################################
# Loading data into memory
# ------------------------
# The pytplot package comes with a sample IDL tplot save file which will be used for the following tutorial,
# located in PyTplot Github (https://github.com/MAVENSDC/PyTplot) under pytplot/pytplot/sampledata.
# This file was generated in IDL, and therefore can be loaded into tplot if you wish to compare the
# IDL and python versions.
# Download the test file from the PyTplot Github acct
url = 'https://github.com/MAVENSDC/PyTplot/raw/master/docs/test_data.tplot'

urllib.request.urlretrieve(url, "./test_data.tplot")

##############################################################################
# To load this data into pytplot, type in the following command:
pytplot.tplot_restore(r'test_data.tplot')
print(pytplot.tplot_names())

##############################################################################
# Storing data
# ------------
# pytplot works by interacting with global variables (called tplot variables) that contain information necessary for
# creating one panel (or even one portion of a panel) of a plot, that can be accessed from the main level or
# within any Python routine. Before anything can be plotted, tplot variables must be loaded into memory.
# Single line Plot: to store data to plot a single line, the 'y' data will be a single list of data
# that will correspond to the points that will make up the line.
# Multi-line Plot: to store data to plot a multi-line plot, the 'y' data will be a list of lists.
# Each column of the "matrix" created is the data for one line. So if you had a list with four lists in it
# and three elements in each list [[1,2,3],[4,5,6],[7,8,9],[10,11,12]] three lines with four data points each
# will be plotted.
# Spectrogram Plot: to store data to plot for a spectrogram, 'v' data will be a list of bins for the data,
# and the 'y' data will be a matrix of values with dimensions of the length of "x" time the length of "v"
Example #16
0
def sgi2dsi(name_in=None,
            name_out=None,
            DSI2SGI=False,
            noload=False):
    """
    This transform a time series data between the SGI and DSI coordinate systems.

    Parameters:

        name_in : str
            input tplot variable to be transformed

        name_out : str
            Name of the tplot variable in which the transformed data is stored

        DSI2SGI : bool
             Set to transform data from DSI to SGI (despun coord --> spinning coord).
             If not set, it transforms data from SGI to DSI (spinning coord --> despun coord).

    Returns:
        None

    """
    if (name_in is None) or (name_in not in tplot_names(quiet=True)):
        print('Input of Tplot name is undifiend')
        return

    if name_out is None:
        print('Tplot name for output is undifiend')
        name_out = 'result_of_sgi2dsi'

    # prepare for transformed Tplot Variable
    reload = not noload
    dl_in = get_data(name_in, metadata=True)
    get_data_array = get_data(name_in)
    time_array = get_data_array[0]
    time_length = time_array.shape[0]
    dat = get_data_array[1]

    # Get the SGA and SGI axes by interpolating the attitude data
    interpolated_values = erg_interpolate_att(name_in, noload=noload)
    sgix2ssix_angle = interpolated_values['sgiz_j2000']['y'][:, 0]
    # [deg] Now the constant angle is used, which is not correct, though
    sgix2ssix_angle[:] = 90. + 21.6

    spperiod = interpolated_values['spinperiod']['y']
    spphase = interpolated_values['spinphase']['y']
    rot_axis = np.array([[0., 0., 1.]]*time_length)

    # SGI --> DSI (despin)
    if not DSI2SGI:
        print('SGI --> DSI')
        coor_out = 'dsi'
        rotated_vector = vector_rotate(x0=dat[:, 0], y0=dat[:, 1], z0=dat[:, 2],
                                       nx=rot_axis[:, 0], ny=rot_axis[:, 1], nz=rot_axis[:, 2],
                                       theta=-sgix2ssix_angle + spphase)
    else:  # DSI --> SGI (spin)
        print('DSI --> SGI')
        coord_out = 'sgi'
        rotated_vector = vector_rotate(x0=dat[:, 0], y0=dat[:, 1], z0=dat[:, 2],
                                       nx=rot_axis[:, 0], ny=rot_axis[:,1], nz=rot_axis[:, 2],
                                       theta=-1.*(-sgix2ssix_angle + spphase))

    store_data(name_out, data={'x': time_array,
               'y': rotated_vector}, attr_dict=dl_in)
    options(name_out, 'ytitle', '\n'.join(name_out.split('_')))
Example #17
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'

        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(tplot_names())

    for this_probe in probe:
        for this_dr in data_rate:
            for this_lvl in level:
                if 'mms' + this_probe + '_' + instrument + '_b_gse_' + this_dr + '_' + this_lvl + suffix in tvars:
                    options(
                        'mms' + this_probe + '_' + instrument + '_b_gse_' +
                        this_dr + '_' + this_lvl + suffix, 'ytitle',
                        'MMS' + this_probe + ' FGM')
                    options(
                        'mms' + this_probe + '_' + instrument + '_b_gse_' +
                        this_dr + '_' + this_lvl + suffix, 'color',
                        ['b', 'g', 'r', '#000000'])
                    options(
                        'mms' + this_probe + '_' + instrument + '_b_gse_' +
                        this_dr + '_' + this_lvl + suffix, 'legend_names',
                        ['Bx GSE', 'By GSE', 'Bz GSE', 'B total'])
                if 'mms' + this_probe + '_' + instrument + '_b_gsm_' + this_dr + '_' + this_lvl + suffix in tvars:
                    options(
                        'mms' + this_probe + '_' + instrument + '_b_gsm_' +
                        this_dr + '_' + this_lvl + suffix, 'ytitle',
                        'MMS' + this_probe + ' FGM')
                    options(
                        'mms' + this_probe + '_' + instrument + '_b_gsm_' +
                        this_dr + '_' + this_lvl + suffix, 'color',
                        ['b', 'g', 'r', '#000000'])
                    options(
                        'mms' + this_probe + '_' + instrument + '_b_gsm_' +
                        this_dr + '_' + this_lvl + suffix, 'legend_names',
                        ['Bx GSM', 'By GSM', 'Bz GSM', 'B total'])
                if 'mms' + this_probe + '_' + instrument + '_b_dmpa_' + this_dr + '_' + this_lvl + suffix in tvars:
                    options(
                        'mms' + this_probe + '_' + instrument + '_b_dmpa_' +
                        this_dr + '_' + this_lvl + suffix, 'ytitle',
                        'MMS' + this_probe + ' FGM')
                    options(
                        'mms' + this_probe + '_' + instrument + '_b_dmpa_' +
                        this_dr + '_' + this_lvl + suffix, 'color',
                        ['b', 'g', 'r', '#000000'])
                    options(
                        'mms' + this_probe + '_' + instrument + '_b_dmpa_' +
                        this_dr + '_' + this_lvl + suffix, 'legend_names',
                        ['Bx DMPA', 'By DMPA', 'Bz DMPA', 'B total'])
                if 'mms' + this_probe + '_' + instrument + '_b_bcs_' + this_dr + '_' + this_lvl + suffix in tvars:
                    options(
                        'mms' + this_probe + '_' + instrument + '_b_bcs_' +
                        this_dr + '_' + this_lvl + suffix, 'ytitle',
                        'MMS' + this_probe + ' FGM')
                    options(
                        'mms' + this_probe + '_' + instrument + '_b_bcs_' +
                        this_dr + '_' + this_lvl + suffix, 'color',
                        ['b', 'g', 'r', '#000000'])
                    options(
                        'mms' + this_probe + '_' + instrument + '_b_bcs_' +
                        this_dr + '_' + this_lvl + suffix, 'legend_names',
                        ['Bx BCS', 'By BCS', 'Bz BCS', 'B total'])