Math Routines

Arithmetic

pytplot.tplot_math.add(tvar1, tvar2, newname=None, new_tvar=None)[source]

Adds two tplot variables together. Will interpolate if the two are not on the same time cadence.

Parameters:
  • tvar1 – str Name of first tplot variable.

  • tvar2 – int/float Name of second tplot variable

  • newname – str Name of new tvar for added data. If not set, then the data in tvar1 is replaced.

  • new_tvar – str (Deprecated) Name of new tvar for added data. If not set, then the data in tvar1 is replaced.

Returns:

None

Examples

>>> pyspedas.store_data('a', data={'x':[0,4,8,12,16], 'y':[1,2,3,4,5]})
>>> pyspedas.store_data('c', data={'x':[0,4,8,12,16,19,21], 'y':[1,4,1,7,1,9,1]})
>>> pyspedas.add('a','c',newname='a+c')
>>> pyspedas.get_data('a+c')
pytplot.tplot_math.subtract(tvar1, tvar2, newname=None, new_tvar=None)[source]

Subtracts two tplot variables. Will interpolate if the two are not on the same time cadence.

Parameters:
  • tvar1 (str) – Name of first tplot variable.

  • tvar2 (int/float) – Name of second tplot variable

  • newname (str) – Name of new tvar for added data. Default: None. If not set, then the data in tvar1 is replaced.

  • new_tvar (str (Deprecated)) – Name of new tvar for added data. If not set, then the data in tvar1 is replaced.

Return type:

None

Examples

>>> pyspedas.store_data('a', data={'x':[0,4,8,12,16], 'y':[1,2,3,4,5]})
>>> pyspedas.store_data('c', data={'x':[0,4,8,12,16,19,21], 'y':[1,4,1,7,1,9,1]})
>>> pyspedas.subtract('a','c',new_tvar='a-c')
pytplot.tplot_math.multiply(tvar1, tvar2, newname=None, new_tvar=None)[source]

Multiplies two tplot variables. Will interpolate if the two are not on the same time cadence.

Parameters:
  • tvar1 (str) – Name of first tplot variable.

  • tvar2 (int/float) – Name of second tplot variable.

  • newname (str) – Name of new tplot variable. If not set, then the data in tvar1 is replaced.

  • new_tvar (str (Deprecated)) – Name of new tplot variable. If not set, then the data in tvar1 is replaced.

Return type:

None

Examples

>>> x1 = [0, 4, 8, 12, 16]
>>> x2 = [0, 4, 8, 12, 16, 19, 21]
>>> time1 = [pyspedas.time_float("2020-01-01") + i for i in x1]
>>> time2 = [pyspedas.time_float("2020-01-01") + i for i in x2]
>>> pyspedas.store_data("a", data={"x": time1, "y": [1, 2, 3, 4, 5]})
>>> pyspedas.store_data("c", data={"x": time2, "y": [1, 4, 1, 7, 1, 9, 1]})
>>> n = pyspedas.multiply("a", "c", newname="a_x_c")
>>> print('new tplot variable:', n)
>>> ac = pyspedas.get_data(n)
>>> print(ac)
pytplot.tplot_math.divide(tvar1, tvar2, newname=None, new_tvar=None)[source]

Divides two tplot variables. Will interpolate if the two are not on the same time cadence.

Parameters:
  • tvar1 (str) – Name of first tplot variable.

  • tvar2 (int/float) – Name of second tplot variable

  • new_tvar (str (Deprecated)) – Name of new tvar for divided data. If not set, then the data in tvar1 is replaced.

  • newname (str) – Name of new tvar for divided data. If not set, then the data in tvar1 is replaced.

Return type:

None

Examples

>>> pyspedas.store_data('a', data={'x':[0,4,8,12,16], 'y':[1,2,3,4,5]})
>>> pyspedas.store_data('c', data={'x':[0,4,8,12,16,19,21], 'y':[1,4,1,7,1,9,1]})
>>> pyspedas.divide('a','c','a_over_c')

Add Across Columns

pytplot.tplot_math.add_across(tvar, column_range=None, newname=None, new_tvar=None)[source]

Adds across columns in the tplot variable

Note

This analysis routine assumes the data is no more than 2 dimensions. If there are more, they may become flattened!

Parameters:
  • tvar (str) – Name of tplot variable.

  • column_range (list of ints) – The columns to add together. For example, if [1,4] is given here, columns 1, 2, 3, and 4 will be added together. If not set, then every column is added.

  • newname (str) – Name of new tvar for averaged data. If not set, then the variable is replaced

  • new_tvar (str (Deprecated)) – Name of new tvar for averaged data. If not set, then the variable is replaced

Return type:

None

Examples

>>> #Add across every column in the data
>>> pyspedas.store_data('d', data={'x':[2,5,8,11,14,17,21], 'y':[[1,1,50],[2,2,3],[100,4,47],[4,90,5],[5,5,99],[6,6,25],[7,7,-5]]})
>>> pyspedas.add_across('d',newname='d_aa')
>>> #Add across specific columns in the data
>>> pyspedas.store_data('b', data={'x':[2,5,8,11,14,17,20], 'y':[[1,1,1,1,1,1],[2,2,5,4,1,1],[100,100,3,50,1,1],[4,4,8,58,1,1],[5,5,9,21,1,1],[6,6,2,2,1,1],[7,7,1,6,1,1]]})
>>> pyspedas.add_across('b',column_range=[[1,2],[3,4]],newname='b_aap')

Average over time

pytplot.tplot_math.avg_res_data(tvar, res, newname=None, new_tvar=None)[source]

Averages the variable over a specified period of time.

Parameters:
  • tvar (str) – Name of tplot variable.

  • res (int/float) – The new data resolution

  • new_tvar (str (Deprecated)) – Name of new tvar for averaged data. If not set, then the data in tvar is replaced.

  • newname (str) – Name of new tvar for averaged data. If not set, then the data in tvar is replaced.

Return type:

None

Note

This routine uses the xarray coarsen() method to reduce the time resolution. It will only work if the data is evenly gridded, and the res parameter evenly divides the number of samples. For most purposes, it is more appropriate to use pyspedas.avg_data() instead.

Examples

>>> #Average the data over every two seconds
>>> pyspedas.store_data('d', data={'x':[2,5,8,11,14,17,21], 'y':[[1,1,50],[2,2,3],[100,4,47],[4,90,5],[5,5,99],[6,6,25],[7,7,-5]]})
>>> pyspedas.avg_res_data('d',2,'d2res')

Clean Spikes

pytplot.tplot_math.clean_spikes(names, nsmooth=10, thresh=0.3, sub_avg=False, new_names=None, newname=None, suffix=None, overwrite=None)[source]

Clean spikes from data.

Parameters:
  • names (str/list of str) – List of pytplot names (wildcards accepted)

  • new_names (str/list of str, optional (Deprecated)) – List of new names for tplot variables. If not given, then a suffix is applied.

  • newname (str/list of str, optional) – List of new names for tplot variables. If not given, then a suffix is applied.

  • suffix (str, optional) – A suffix to apply. Default is ‘-avg’.

  • overwrite (bool, optional) – Replace the existing tplot name.

  • nsmooth (int, optional) – the number of data points for smoothing

  • thresh (float, optional) – threshold value

  • sub_avg (bool, optional) – if set, subtract the average value of the data prior to checking for spikes

Return type:

None.

Clip Data

pytplot.tplot_math.clip(tvar, ymin, ymax, newname=None, new_tvar=None)[source]

Change out-of-bounds data to NaN.

Parameters:
  • tvar (str or list[str]) – tplot variable names to clip (wildcards accepted)

  • ymin (int/float) – Minimum value to keep (inclusive)

  • ymax (int/float) – Maximum value to keep (inclusive)

  • new_tvar (str (Deprecated)) – Name of new tvar for clipped data storage. If not specified, tvar will be replaced THIS is not an option for multiple variable input, for multiple or pseudo variables, the data is overwritten.

  • newname (str) – Name of new tvar for clipped data storage. If not specified, tvar will be replaced THIS is not an option for multiple variable input, for multiple or pseudo variables, the data is overwritten.

Return type:

None

Examples

>>> Make any values below 2 and above 6 equal to NaN.
>>> pyspedas.store_data('d', data={'x':[2,5,8,11,14,17,21], 'y':[[1,1],[2,2],[100,100],[4,4],[5,5],[6,6],[7,7]]})
>>> pyspedas.clip('d',2,6,'e')

Crop Data

pytplot.tplot_math.crop(tvar1, tvar2, replace=True)[source]

Crops both tplot variable so that their times are the same. This is done automatically by other processing routines if times do not match up.

Parameters:
  • tvar1 (str) – Name of the first tplot variable

  • tvar2 (str) – Name of the second tplot variable

  • replace (bool, optional) – If true, the data in the original tplot variables are replaced. Otherwise, new variables are created.

Return type:

None

Examples

>>> pyspedas.store_data('a', data={'x':[0,4,8,12,16], 'y':[1,2,3,4,5]})
>>> pyspedas.store_data('b', data={'x':[2,5,8,11,14,17,20], 'y':[[1,1,1,1,1,1],[2,2,5,4,1,1],[100,100,3,50,1,1],[4,4,8,58,1,1],[5,5,9,21,1,1],[6,6,2,2,1,1],[7,7,1,6,1,1]]})
>>> pyspedas.crop('a','b')

Deflag Data

pytplot.tplot_math.deflag(tvar, flag=None, newname=None, new_tvar=None, method=None, fillval=None)[source]

Replace NaN or other ‘flag’ values in arrays with interpolated or other values.

Parameters:
  • tvar (str or list[str]) – Names of tplot variables to deflag (wildcards accepted)

  • flag (int,list) – Flagged data will be converted to NaNs.

  • method (str, optional) –

    Method to apply. Valid options:

    'repeat': Repeat last good value
    'linear': Interpolate linearly over gap
    'replace': Replace flagged values with fillval, or NaN if fillval not specified
    'remove_nan': Remove timestamps and values with a NaN in any dimension
    
  • newname (str) – Name of new tvar for deflagged data storage. If not specified, then the data in tvar1 will be replaced. This is not an option for multiple variable input, for multiple or pseudo variables, the data is overwritten.

  • new_tvar (str (Deprecated)) – Name of new tvar for deflagged data storage. If not specified, then the data in tvar1 will be replaced. THIS is not an option for multiple variable input, for multiple or pseudo variables, the data is overwritten.

  • fillval (int, float (optional)) – Value to use as replacement when method=’replace’

Notes

deflag only works for 1 or 2-d data arrays; ntimes or (ntimes, nspectral_bins)

Return type:

None

Examples

>>> pyspedas.store_data('d', data={'x':[2,5,8,11,14,17,21], 'y':[[1,1],[2,2],[100,4],[4,90],[5,5],[6,6],[7,7]]})
>>> # Remove any instances of [100,90,7,2,57] from 'd', store in 'e'.
>>> pyspedas.deflag('d',[100,90,7,2,57],newname='e')

Degap Data

pytplot.tplot_math.degap(tvar, dt=None, margin=0.25, maxgap=None, func='nan', newname=None, new_tvar=None, onenanpergap=False, twonanpergap=False)[source]

Fills gaps in the data either with NaNs or the last number.

Parameters:
  • tvar (str or list[str]) – Names of tplot variables to degap (wildcards accepted)

  • dt (int/float) – Step size of the data in seconds, default is to use the median time interval

  • margin (int/float, optional, default is 0.25 seconds) – The maximum deviation from the step size allowed before degapping occurs. In other words, if you’d like to fill in data every 4 seconds but occasionally the data is 4.1 seconds apart, set the margin to .1 so that a data point is not inserted there.

  • maxgap (int|float, optional) – Maximum gap length (in seconds) that will be filled. If None, defaults to entire time range (i.e. all gaps with length > (dt+margin) will be filled)

  • func (str, optional) – Either ‘nan’ or ‘ffill’, which overrides normal interpolation with NaN substitution or forward-filled values.

  • newname (str, optional) – The new tplot variable name to store the data into. If None, then the data is overwritten. THIS is not an option for multiple variable input, for multiple or pseudo variables, the data is overwritten.

  • new_tvar (str, optional (Deprecated)) – The new tplot variable name to store the data into. If None, then the data is overwritten. THIS is not an option for multiple variable input, for multiple or pseudo variables, the data is overwritten.

  • onenanpergap (bool) – if set to True, then only insert one NaN value, rather than adding NaN values at dt resolution

  • twonanpergap (bool) – if set to True, then only insert one NaN value, rather than adding NaN values at dt resolution

Returns:

Creates a new tplot variable with the degap data

Return type:

None

Examples

>>> import pyspedas
>>> time = [pyspedas.time_float("2020-01-01") + i for i in [1, 2, 3, 4, 5, 6, 9, 10, 11]]
>>> y = [1, 2, 3, 4, 5, 6, 9, 10, 11]
>>> pyspedas.store_data("a", data={"x": time, "y": y})
>>> degap("a", newname="b")
>>> b = pyspedas.get("b")
>>> print(b)

Derivative

pytplot.tplot_math.derive(tvar, newname=None, new_tvar=None)[source]

Takes the derivative of the tplot variable.

Parameters:
  • tvar (str) – Name of tplot variable.

  • new_tvar (str (Deprecated)) – Name of new tplot variable. If not set, then the data in tvar is replaced.

  • newname (str) – Name of new tplot variable. If not set, then the data in tvar is replaced.

Return type:

None

Examples

>>> pyspedas.store_data('b', data={'x':[2,5,8,11,14,17,20], 'y':[[1,1,1,1,1,1],[2,2,5,4,1,1],[100,100,3,50,1,1],[4,4,8,58,1,1],[5,5,9,21,1,1],[6,6,2,2,1,1],[7,7,1,6,1,1]]})
>>> pyspedas.derive('b','dbdt')

Dynamic Power Spectrum

pytplot.tplot_math.dpwrspc(time, quantity, nboxpoints=256, nshiftpoints=128, bin=3, tbegin=-1.0, tend=-1.0, noline=False, nohanning=False, notperhz=False, notmvariance=False, tm_sensitivity=None)[source]

Compute power spectra.

Parameters:
  • time (list of float) – Time array.

  • quantity (list of float) – Data array.

  • nboxpoints (int, optional) – The number of points to use for the hanning window. The default is 256.

  • nshiftpoints (int, optional) – The number of points to shift for each spectrum. The default is 128.

  • bin (int, optional) – Size for binning of the data along the frequency domain. The default is 3.

  • tbegin (float, optional) – Start time for the calculation. If -1.0, the start time is the first time in the time array.

  • tend (float, optional) – End time for the calculation. If -1.0, the end time is the last time in the time array.

  • noline (bool, optional) – If True, no straight line is subtracted. The default is False.

  • nohanning (bool, optional) – If True, no hanning window is applied to the input. The default is False.

  • notperhz (bool, optional) – If True, the output units are the square of the input units. The default is False.

  • notmvariance (bool, optional) – If True, replace output spectrum for any windows that have variable cadence with NaNs. The default is False.

  • tm_sensitivity (float, optional) – If noTmVariance is set, this number controls how much of a dt anomaly is accepted. The default is None.

Returns:

tdps: array of float

The time array for the dynamic power spectrum, the center time of the interval used for the spectrum.

fdps: array of float

The frequency array (units =1/time units).

dps: array of float

The power spectrum, (units of quantity)^2/frequency_units.

Return type:

tuple

Examples

>>> # Compute the power spectrum of a given time series
>>> import numpy as np
>>> time = range(3000)
>>> quantity = np.random.random(3000)
>>> power = pyspedas.dpwrspc(time, quantity)

Flatten Data

Interpolate through NaN values

pytplot.tplot_math.interp_nan(tvar, newname=None, new_tvar=None, s_limit=None)[source]

Interpolates the tplot variable through NaNs in the data. This is basically just a wrapper for xarray’s interpolate_na function.

Note

This analysis routine assumes the data is no more than 2 dimensions. If there are more, they may become flattened!

Parameters:
  • tvar (str) – Name of tplot variable.

  • newname (str) – Name of new tvar for added data. If not set, then the original tvar is replaced.

  • new_tvar (str (Deprecated)) – Name of new tvar for added data. If not set, then the original tvar is replaced.

  • s_limit (int or float, optional) – The maximum size of the gap in seconds to not interpolate over. I.e. if there are too many NaNs in a row, leave them there.

Return type:

None

Examples

>>> import pyspedas
>>> import numpy as np
>>> pyspedas.store_data('e', data={'x':[2,5,8,11,14,17,21], 'y':[[np.nan,1,1],[np.nan,2,3],[4,np.nan,47],[4,np.nan,5],[5,5,99],[6,6,25],[7,np.nan,-5]]})
>>> pyspedas.interp_nan('e','e_nonan',s_limit=5)

Join/Split Data

pytplot.tplot_math.join_vec(tvars, newname=None, new_tvar=None, merge=False)[source]

Joins 1D tplot variables into one tplot variable.

Note

This analysis routine assumes the data is no more than 2 dimensions. If there are more, they may become flattened!

Parameters:
  • tvars (list of str) – Name of tplot variables to join together.

  • newname (str, optional) – The name of the new tplot variable. If not specified (the default), a name will be assigned.

  • new_tvar (str, optional (Deprecated)) – The name of the new tplot variable. If not specified, a name will be assigned.

  • merge (bool, optional) – Whether or not to merge the created variable into an older variable. Default is False.

Return type:

None

Examples

>>> import pyspedas
>>> import numpy as np
>>> pyspedas.store_data('d', data={'x':[2,5,8,11,14,17,21], 'y':[[1,1,50],[2,2,3],[100,4,47],[4,90,5],[5,5,99],[6,6,25],[7,7,-5]]})
>>> pyspedas.store_data('e', data={'x':[2,5,8,11,14,17,21], 'y':[[np.nan,1,1],[np.nan,2,3],[4,np.nan,47],[4,np.nan,5],[5,5,99],[6,6,25],[7,np.nan,-5]]})
>>> pyspedas.store_data('g', data={'x':[0,4,8,12,16,19,21], 'y':[[8,1,1],[100,2,3],[4,2,47],[4,39,5],[5,5,99],[6,6,25],[7,-2,-5]]})
>>> pyspedas.join_vec(['d','e','g'],newname='deg')
pytplot.tplot_math.split_vec(tvar, polar=False, newname=None, new_name=None, columns='all', suffix=None)[source]

Splits up 2D data into many 1D tplot variables. Takes a stored tplot vector like Vp and stores tplot variables Vp_x, Vp_y, Vp_z

Parameters:
  • tvar (str) – Name of tplot variable to split up

  • polar (bool, optional) – If True, the input data is in polar coordinates. Suffix will be set to [‘_mag’, ‘_th’, ‘_phi’]. Default: False

  • new_name (int/list, optional (Deprecated)) – The names of the new tplot variables. This must be the same length as the number of variables created.

  • newname (int/list, optional) – The names of the new tplot variables. This must be the same length as the number of variables created. Default: None

  • columns (list of ints, optional) – The specific column numbers to grab from the data. Default: ‘all’ (splits all columns)

  • suffix (str) – Suffix str to be added to end of tplot variable name Default: None

Returns:

List of variables created

Return type:

list[str]

Examples

>>> pyspedas.store_data('b', data={'x':[2,5,8,11,14,17,20], 'y':[[1,1,1,1,1,1],[2,2,5,4,1,1],[100,100,3,50,1,1],[4,4,8,58,1,1],[5,5,9,21,1,1],[6,6,2,2,1,1],[7,7,1,6,1,1]]})
>>> pyspedas.split_vec('b')

Make a data gap

pytplot.tplot_math.makegap(var_data, dt=None, margin=0.0, func='nan')[source]

Fill gaps in the data either with NaNs or the last number. This is identical to degap, except operates directly on the data and time arrays, rather than the tplot variable. This is intended for use with the data_gap option. This version actually puts the data into a temporary tplot variable, and call degap, then extracts that data into the proper form.

Parameters:
  • var_data (named tuple) – The data for the tplot variable, a structure that contains at least, tags for ‘y’ and ‘times’

  • dt (int/float) – Step size of the data in seconds, default is to use the median time interval

  • margin (int/float, optional, default is 0.0 seconds (there is no margin in the IDL tplot makegap)) – The maximum deviation from the step size allowed before degapping occurs. In other words, if you’d like to fill in data every 4 seconds but occasionally the data is 4.1 seconds apart, set the margin to .1 so that a data point is not inserted there.

  • func (str, optional) – Either ‘nan’ or ‘ffill’, which overrides normal interpolation with NaN substitution or forward-filled values.

Returns:

A tuple returned by calling get_data() on the degapped temp variable

Return type:

tuple

Examples

>>> import pyspedas
>>> time = [pyspedas.time_float("2020-01-01") + i for i in [1, 2, 3, 4, 5, 6, 9, 10, 11]]
>>> y = [1, 2, 3, 4, 5, 6, 9, 10, 11]
>>> pyspedas.store_data("a", data={"x": time, "y": y})
>>> a = pyspedas.get_data("a")
>>> print(a)
>>> b = pyspedas.makegap(a)
>>> print(b)

Power Spectrum

pytplot.tplot_math.pwr_spec(tvar, nbp=256, nsp=128, newname=None)[source]

Calculates the power spectrum of a line, and adds a tplot variable for this new spectrogram

Parameters:
  • tvar (str) – Name of tvar to use

  • nbp (int, optional) – The number of points to use when calculating the FFT

  • nsp (int, optional) – The number of points to shift over to calculate the next FFT

  • newname (str, optional) – The name of the new tplot variable created,

Return type:

None

Examples

>>> import pyspedas
>>> import math
>>> time = [pyspedas.time_float("2020-01-01") + i for i in range(10000)]
>>> quantity = [math.sin(i) for i in range(10000)]
>>> pyspedas.store_data("dp", data={"x": time, "y": quantity})
>>> pyspedas.pwr_spec("dp", newname="dp_pwrspec")
>>> pyspedas.tplot("dp_pwrspec")

Subtract Average/Median

pytplot.tplot_math.subtract_average(names, newname=None, new_names=None, suffix=None, overwrite=None, median=None)[source]

Subtracts the average or median from data.

Parameters:
  • names (str/list of str) – List of tplot variable names to process (wildcards accepted)

  • newname (str/list of str, optional) – List of new names for tplot variables. Default: None. If not given, then a suffix is applied.

  • new_names (str/list of str, optional (Deprecated)) – List of new names for tplot variables. Default: None. If not given, then a suffix is applied.

  • suffix (str, optional) – A suffix to apply. Default: ‘-d’.

  • overwrite (bool, optional) – If set, then tplot variables are replaced. Default: None

  • median (float, optional) – If it is 0 or not set, then it computes the mean. Otherwise, it computes the median. Default: None.

Returns:

List of new tplot variables created

Return type:

list of str

Examples

>>> pyspedas.store_data('a', data={'x':[0,4,8,12,16], 'y':[1,2,3,4,5]})
>>> pyspedas.subtract_average('a')
>>> pyspedas.tplot(['a','a-d'])
pytplot.tplot_math.subtract_median(names, newname=None, new_names=None, suffix=None, overwrite=None)[source]

Subtracts the median from data.

Parameters:
  • names (str/list of str) – List of pytplot names.

  • newname (str/list of str, optional) – List of new names for tplot variables. Default: None. If not given, then a suffix is applied.

  • new_names (str/list of str, optional (Deprecated)) – List of new names for tplot variables. Default: None. If not given, then a suffix is applied.

  • suffix (str, optional) – A suffix to apply. Default: ‘-d’.

  • overwrite (bool, optional) – If set, then tplot variables are replaced. Default: None

Returns:

Returns a list of new tplot variables created

Return type:

list of str

Examples

>>> from pyspedas import subtract_median
>>> pyspedas.store_data('a', data={'x':[0,4,8,12,16], 'y':[1.,2.,3.,4.,5.]})
>>> pyspedas.subtract_median('a')

Vector Dot/Cross Products

pytplot.tplot_math.tdotp(variable1, variable2, newname=None)[source]

Calculate the sample-by-sample dot product of two tplot variables

Parameters:
  • variable1 (str) – First tplot variable

  • variable2 (str) – Second tplot variable

  • newname (str) – Name of the output variable Default: variable1_dot_variable2

Returns:

Name of the tplot variable created

Return type:

str

Examples

>>> # Compute the dot products of a given time series
>>> import pyspedas
>>> x1 = [0, 4, 8]
>>> x2 = [0, 4, 8]
>>> time1 = [pyspedas.time_float("2020-01-01") + i for i in x1]
>>> time2 = [pyspedas.time_float("2020-01-01") + i for i in x2]
>>> pyspedas.store_data("a", data={"x": time1, "y": [[1, 2, 3],[2, 3, 4],[3, 4, 5]]})
>>> pyspedas.store_data("c", data={"x": time2, "y": [[1, 4, 1],[2, 5, 2],[3, 5, 3]]})
>>> n = pyspedas.tdotp("a", "c", newname="a_dot_c")
>>> print('new tplot variable:', n)
>>> ac = pyspedas.get_data(n)
>>> print(ac)
pytplot.tplot_math.tcrossp(v1, v2, newname=None, return_data=False)[source]

Calculates the cross product of two tplot variables

Parameters:
  • v1 (str) – First tplot variable

  • v2 (str) – Second tplot variable

  • newname (str, optional) – Name of the output variable Default: v1_cross_v2

  • return_data (bool) – Returns the data as an ndarray instead of creating a tplot variable Default: False

Returns:

Name of the tplot variable created

Return type:

str

Examples

>>> # Compute the cross product of two time series
>>> import pyspedas
>>> x1 = [0, 4, 8]
>>> x2 = [0, 4, 8]
>>> time1 = [pyspedas.time_float("2020-01-01") + i for i in x1]
>>> time2 = [pyspedas.time_float("2020-01-01") + i for i in x2]
>>> pyspedas.store_data("a", data={"x": time1, "y": [[1, 2, 3],[2, 3, 4],[3, 4, 5]]})
>>> pyspedas.store_data("c", data={"x": time2, "y": [[1, 4, 1],[2, 5, 2],[3, 5, 3]]})
>>> n = pyspedas.tcrossp("a", "c", newname="a_cross_c")
>>> print('new tplot variable:', n)
>>> ac = pyspedas.get_data(n)
>>> print(ac)

Dynamic Power Spectrum of Tplot variable

pytplot.tplot_math.tdpwrspc(varname, newname=None, trange=['0.0', '0.0'], nboxpoints=None, nshiftpoints=None, polar=False, bin=3, nohanning=False, noline=False, notperhz=False, notmvariance=False)[source]

Compute power spectra for a tplot variable.

Parameters:
  • varname (str) – Name of tplot variable.

  • newname (str, optional) – Name of new tplot variable to save data to. Default: None. If newname is not set ‘_dpwrspc’ will be appended to the varname

  • trange (list of float, optional) – Start and end times for the calculation.

  • nboxpoints (int, optional) – The number of points to use for the hanning window. Default: 256

  • nshiftpoints (int, optional) – The number of points to shift for each spectrum. Default: 128

  • polar (bool, optional) – If True, the input data is in polar coordinates. Default: False

  • bin (int, optional) – Size for binning of the data along the frequency domain. Default: 3

  • nohanning (bool, optional) – If True, no hanning window is applied to the input. Default: False

  • noline (bool, optional) – If True, no straight line is subtracted. Default: False

  • notperhz (bool, optional) – If True, the output units are the square of the input units. Default: False

  • notmvariance (bool, optional) – If True, replace output spectrum for any windows that have variable. cadence with NaNs. Default: False

Returns:

Name of new tplot variable.

Return type:

str

Example

>>> # Compute the power spectrum of a given time series
>>> import pyspedas
>>> import numpy as np
>>> pyspedas.store_data('a', data={'x': range(3000), 'y': np.random.random(3000)})
>>> pyspedas.tdpwrspc('a')

Time Clip

pytplot.tplot_math.time_clip(names, time_start, time_end, newname=None, new_names=None, suffix='-tclip', overwrite=False, interior_clip=False)[source]

Clip data from time_start to time_end.

Parameters:
  • names (str/list of str) – List of tplot variable names to time clip (wildcards accepted)

  • time_start (float or string) – Start time.

  • time_end (float or string) – End time.

  • newname (str/list of str, optional) – List of new names for tplot variables. Default: None. If not given, then a suffix is applied or the variables are overwritten

  • new_names (str/list of str, optional (Deprecated)) – List of new names for tplot variables. Default: None. If not given, then a suffix is applied or the variables are overwritten

  • suffix (str, optional) – A suffix to apply. Default: ‘-tclip’

  • overwrite (bool, optional) – If true, overwrite the existing tplot variable. Default: False

  • interior_clip (bool, optional) – If true, reverse sense of operation and clip out times within the start/end range, for example, when manually despiking data. Default: False

Returns:

Returns a list of tplot variables created or changed

Return type:

list of str

Example

>>> # Clip time
>>> import pyspedas
>>> x1 = [0, 4, 8, 12, 16]
>>> time1 = [pyspedas.time_float("2020-01-01") + i for i in x1]
>>> pyspedas.store_data("a", data={"x": time1, "y": [[1, 2, 3],[2, 3, 4],[3, 4, 5],[4, 5, 6],[5, 6,7]]})
>>> time_start=time1[0]
>>> time_end=time1[2]
>>> pyspedas.time_clip('a',time_start,time_end)
>>> ac = pyspedas.get_data('a-tclip')
>>> print(ac)

Interpolate Tplot Variables

pytplot.tplot_math.tinterp(tvar1, tvar2, replace=False)[source]

Interpolates one tplot variable to another one’s time cadence. This is done automatically by other processing routines.

Parameters:
  • tvar1 (str) – Name of first tplot variable whose times will be used to interpolate tvar2’s data.

  • tvar2 (str) – Name of second tplot variable whose data will be interpolated.

  • replace (bool, optional) – If true, the data in the original tplot variable is replaced. Otherwise, a variable is created.

Returns:

Name of the new tplot variable

Return type:

str

Examples

>>> pyspedas.store_data('a', data={'x':[0,4,8,12,16], 'y':[1,2,3,4,5]})
>>> pyspedas.store_data('c', data={'x':[0,4,8,12,16,19,21], 'y':[1,4,1,7,1,9,1]})
>>> pyspedas.tinterp('a','c')

Km to/from Earth Radii unit conversion

pytplot.tplot_math.tkm2re(name, km=False, newname=None, suffix='')[source]

Converts a tplot variable to Re or Km

Parameters:
  • name (str or list of str) – Names of tplot variables to convert (wildcards accepted)

  • km (bool, optional) – If True, convert Re to Km. If False, convert Km to Re. Default: False

  • newname (str or list of str, optional) – Output variable names; if not set, the names of the input variables are used + ‘_re’ or ‘_km’ Default: None

  • suffix (str, optional) – Specify to append a suffix to each variable (only valid if newname is not specified) Default: ‘’

Return type:

List of the tplot variables created

Examples

>>> import pyspedas
>>> pyspedas.store_data('a', data={'x':[0,4,8,12,16], 'y':[1,2,3,4,5]})
>>> pyspedas.tkm2re('a', km = True)

Normalize to unit vectors

pytplot.tplot_math.tnormalize(variable, newname=None, return_data=False)[source]

Normalize all the vectors stored in a tplot variable

Parameters:
  • variable (str or np.ndarray) – tplot variable (or numpy array) containing the vectors to be normalized

  • newname (str, optional) – name of the output variable; If not set, output name will be variable + ‘_normalize’ Default: None

  • return_data (bool, optional) – If true, return the normalized vectors instead of creating a tplot variable Default: False

Returns:

name of the tplot variable created or normalized vectors if return_data is set

Return type:

str or ndarray of float

Examples

>>> import pyspedas
>>> pyspedas.store_data('a', data={'x':[1, 2, 3], 'y':[[1,2,3],[4,5,6],[7,8,9]]})
>>> pyspedas.tnormalize('a')

Power Spectrum of Tplot variable

pytplot.tplot_math.tpwrspc(varname, newname=None, overwrite=False, noline=False, nohanning=False, bin=3, notperhz=False, trange=None, axis=0)[source]

This function is a wrapper for pwrspc. It applies pwrspc to a tplot variable and stores the result in a new tplot variable.

Parameters:
  • varname (str) – Name of the tplot variable.

  • newname (str, optional) – New name for the output tplot variable. If not set, default output variable will be Default: None

  • overwrite (bool, optional) – If True, overwrite the existing tplot variable. If True, then the newname keyword has no effect. Default: False

  • noline (bool, optional) – If True, straight line is not subtracted from the data. Default: False

  • nohanning (bool, optional) – If True, no Hanning window is applied to the data. Default: False

  • bin (int, optional) – Bin size for binning the data. Default: 3

  • notperhz (bool, optional) – If True, the output units are the square of the input units. Default: False

  • trange (list, optional) – Time range for the data extraction. Default: None

  • axis (int, optional) – If the input variable is multi-dimensional, this specifies the axis to operate along. Default: 0, first axis.

Returns:

Name of the new tplot variable created by this function. The output variable contains a single data point, frequency as v, and power as y.

Return type:

str

Examples

>>> import pyspedas
>>> import numpy as np
>>> pyspedas.store_data('a', data={'x': range(100), 'y': np.random.random(100)})
>>> pyspedas.tpwrspc('a')
>>> pyspedas.get_data('a_pwrspc')

Smooth Data

pytplot.tplot_math.tsmooth(names, width=10, median=None, preserve_nans=None, newname=None, new_names=None, suffix=None, overwrite=None)[source]

Smooths a tplot variable.

Parameters:
  • names (str/list of str) – List of tplot variable names to be smoothed (wildcards accepted)

  • width (int, optional) – Data window to use for smoothing. The default is 10.

  • median (bool, optional) – Apply the median as well. The default is None.

  • preserve_nans (bool, optional) – If None, then replace NaNs. The default is None.

  • newname (str/list of str, optional) – List of new names for tplot variables. If not given, then a suffix is applied. The default is None.

  • new_names (str/list of str, optional (deprecated)) – List of new names for tplot variables. If not given, then a suffix is applied. The default is None.

  • suffix (str, optional) – A suffix to apply. Default is ‘-s’. The default is None.

  • overwrite (bool, optional) – Replace the existing tplot name. The default is None.

Returns:

Returns list of tplot variables created or changed

Return type:

list of str

Example

>>> import pyspedas
>>> import numpy as np
>>> pyspedas.store_data('a', data={'x': range(100), 'y': np.random.random(100)})
>>> pyspedas.tsmooth('a')

Spectrum Multiplication

pytplot.tplot_math.spec_mult(tvar, newname=None, new_tvar=None)[source]

Multiplies the data by the stored spectrogram bins and created a new tplot variable

Note::

This analysis routine assumes the data is no more than 2 dimensions. If there are more, they may become flattened!

Parameters:
  • tvar (str) – Name of tplot variable

  • newname (str) – Name of new tvar in which to store interpolated data. Default: If none is specified, a name will be created of the form tvar_specmult.

  • new_tvar (str (Deprecated)) – Name of new tvar in which to store interpolated data. If none is specified, a name will be created.

Return type:

None

Examples

>>> pyspedas.store_data('h', data={'x':[0,4,8,12,16,19,21], 'y':[[8,1,1],[100,2,3],[4,2,47],[4,39,5],[5,5,99],[6,6,25],[7,-2,-5]],'v':[[1,1,50],[2,2,3],[100,4,47],[4,90,5],[5,5,99],[6,6,25],[7,7,-5]]})
>>> pyspedas.spec_mult('h')