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

>>> pytplot.store_data('a', data={'x':[0,4,8,12,16], 'y':[1,2,3,4,5]})
>>> pytplot.store_data('c', data={'x':[0,4,8,12,16,19,21], 'y':[1,4,1,7,1,9,1]})
>>> pytplot.add('a','c',newname='a+c')
>>> pytplot.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.

Returns:

None

Examples

>>> pytplot.store_data('a', data={'x':[0,4,8,12,16], 'y':[1,2,3,4,5]})
>>> pytplot.store_data('c', data={'x':[0,4,8,12,16,19,21], 'y':[1,4,1,7,1,9,1]})
>>> pytplot.subtract('a','c',new_tvar='a-c')
>>> print(pytplot.data_quants['a-c'].data)
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.

Returns:

None

Examples

>>> x1 = [0, 4, 8, 12, 16]
>>> x2 = [0, 4, 8, 12, 16, 19, 21]
>>> time1 = [pytplot.time_float("2020-01-01") + i for i in x1]
>>> time2 = [pytplot.time_float("2020-01-01") + i for i in x2]
>>> pytplot.store_data("a", data={"x": time1, "y": [1, 2, 3, 4, 5]})
>>> pytplot.store_data("c", data={"x": time2, "y": [1, 4, 1, 7, 1, 9, 1]})
>>> n = pytplot.multiply("a", "c", newname="a_x_c")
>>> print('new tplot variable:', n)
>>> ac = pytplot.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.

Returns:

None

Examples

>>> pytplot.store_data('a', data={'x':[0,4,8,12,16], 'y':[1,2,3,4,5]})
>>> pytplot.store_data('c', data={'x':[0,4,8,12,16,19,21], 'y':[1,4,1,7,1,9,1]})
>>> pytplot.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

Returns:

None

Examples

>>> #Add across every column in the data
>>> pytplot.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]]})
>>> pytplot.add_across('d',newname='d_aa')
>>> print(pytplot.data_quants['d_aa'].data)
>>> #Add across specific columns in the data
>>> pytplot.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]]})
>>> pytplot.add_across('b',column_range=[[1,2],[3,4]],newname='b_aap')
>>> print(pytplot.data_quants['b_aap'].data)

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:
  • tvar1 – 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.

Returns:

None

Examples

>>> #Average the data over every two seconds
>>> pytplot.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]]})
>>> pytplot.avg_res_data('d',2,'d2res')
>>> print(pytplot.data_quants['d'].values)

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.

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

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

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

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

  • 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:
  • tvar1 – str Name of tvar to use for data clipping.

  • 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.

Returns:

None

Examples

>>> Make any values below 2 and above 6 equal to NaN.
>>> pytplot.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]]})
>>> pytplot.clip('d',2,6,'e')
>>> print(pytplot.data_quants['e'].data)

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.

Returns:

None

Examples

>>> pytplot.store_data('a', data={'x':[0,4,8,12,16], 'y':[1,2,3,4,5]})
>>> pytplot.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]]})
>>> pytplot.crop('a','b')
>>> print(pytplot.data_quants['a'].data)
>>> print(pytplot.data_quants['b'].data)

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) – Name of tplot variable to use for data clipping.

  • 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

>>> pytplot.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'.
>>> pytplot.deflag('d',[100,90,7,2,57],newname='e')

Degap Data

pytplot.tplot_math.degap(tvar, dt=None, margin=0.25, 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) – Name of tplot variable to modify

  • 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.

  • 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 pytplot
>>> time = [pytplot.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]
>>> pytplot.store_data("a", data={"x": time, "y": y})
>>> degap("a", newname="b")
>>> b = pytplot.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.

Returns:

None

Examples

>>> pytplot.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]]})
>>> pytplot.derive('b','dbdt')
>>> print(pytplot.data_quants['dbdt'].values)

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.

Examples

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

Flatten Data

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

Divides the column by an average over specified time

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 first tplot variable.

  • range – [int, int], optional The time range to average over. The default is the whole range.

  • newname – str Name of new tvar for added data. If not set, then a name is made up.

  • new_tvar – str (Deprecated) Name of new tvar for added data. If not set, then a name is made up.

Returns:

None

Examples

>>> # Divide each column by the average of the data between times 8 and 14
>>> pytplot.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]]})
>>> pytplot.flatten('d',[8,14],'d_flatten')
>>> print(pytplot.data_quants['d_flatten'].values)

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 pytplot
>>> import numpy as np
>>> pytplot.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]]})
>>> pytplot.interp_nan('e','e_nonan',s_limit=5)
>>> print(pytplot.data_quants['e_nonan'].values)

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 pytplot
>>> import numpy as np
>>> pytplot.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]]})
>>> pytplot.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]]})
>>> pytplot.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]]})
>>> pytplot.join_vec(['d','e','g'],newname='deg')
>>> print(pytplot.data_quants['deg'].values)
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:

None

Examples

>>> pytplot.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]]})
>>> pytplot.tplot_math.split_vec('b')
>>> print(pytplot.data_quants['b_0'].values)
>>> print(pytplot.data_quants['b_1'].values)
>>> print(pytplot.data_quants['b_5'].values)

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.

Return type:

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

Examples

>>> import pytplot
>>> time = [pytplot.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]
>>> pytplot.store_data("a", data={"x": time, "y": y})
>>> a = pytplot.get_data("a")
>>> print(a)
>>> b = pytplot.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,

Returns:

None

Examples

>>> import pytplot
>>> import math
>>> time = [pytplot.time_float("2020-01-01") + i for i in range(10000)]
>>> quantity = [math.sin(i) for i in range(10000)]
>>> pytplot.store_data("dp", data={"x": time, "y": quantity})
>>> pytplot.pwr_spec("dp", name="dp_pwrspec")
>>> pytplot.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 pytplot names.

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

  • new_names (str/list of str, optional (Deprecated)) – List of new names for pytplot 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 pytplot 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 str – List of new tplot variables created

  • Examples – >>> pytplot.store_data(‘a’, data={‘x’:[0,4,8,12,16], ‘y’:[1,2,3,4,5]}) >>> pytplot.subtract_average(‘a’) >>> pytplot.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 pytplot variables. Default: None. If not given, then a suffix is applied.

  • new_names (str/list of str, optional (Deprecated)) – List of new names for pytplot 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 pytplot variables are replaced. Default: None

Returns:

  • list of str – Returns a list of new pytplot variables created

  • Examples – >>> from pytplot import subtract_median >>> pytplot.store_data(‘a’, data={‘x’:[0,4,8,12,16], ‘y’:[1.,2.,3.,4.,5.]}) >>> pytplot.tplot_math.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

Return type:

Name of the tplot variable created

Examples

>>> # Compute the dot products of a given time series
>>> import pytplot
>>> x1 = [0, 4, 8]
>>> x2 = [0, 4, 8]
>>> time1 = [pytplot.time_float("2020-01-01") + i for i in x1]
>>> time2 = [pytplot.time_float("2020-01-01") + i for i in x2]
>>> pytplot.store_data("a", data={"x": time1, "y": [[1, 2, 3],[2, 3, 4],[3, 4, 5]]})
>>> pytplot.store_data("c", data={"x": time2, "y": [[1, 4, 1],[2, 5, 2],[3, 5, 3]]})
>>> n = pytplot.tdotp("a", "c", newname="a_dot_c")
>>> print('new tplot variable:', n)
>>> ac = pytplot.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: None

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

Return type:

Name of the tplot variable

Example

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

  • newname (str, optional) – Name of new pytplot 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 pytplot variable.

Return type:

str

Example

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

Time Clip

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

Clip data from time_start to time_end.

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

  • time_start (float) – Start time.

  • time_end (float) – End time.

  • newname (str/list of str, optional) – List of new names for pytplot 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 pytplot 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

Returns:

Returns a list of pytplot variables created or changed

Return type:

list of str

Example

>>> # Clip time
>>> x1 = [0, 4, 8, 12, 16]
>>> time1 = [pytplot.time_float("2020-01-01") + i for i in x1]
>>> pytplot.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]
>>> pytplot.time_clip('a',time_start,time_end)
>>> ac = pytplot.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:

new_var2, the name of the new tplot variable

Examples

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

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

  • 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 pytplot
>>> pytplot.store_data('a', data={'x':[0,4,8,12,16], 'y':[1,2,3,4,5]})
>>> pytplot.tkm2re('a', km = True)
>>> print(pytplot.data_quants['a_km'].data)

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

Examples

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

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 pytplot variable and stores the result in a new pytplot 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:

newname – 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, string

Examples

>>> import pytplot
>>> import numpy as np
>>> pytplot.store_data('a', data={'x': range(100), 'y': np.random.random(100)})
>>> pytplot.tpwrspc('a')
>>> pytplot.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 pytplot names.

  • 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 pytplot 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 pytplot 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 pytplot
>>> import numpy as np
>>> pytplot.store_data('a', data={'x': range(100), 'y': np.random.random(100)})
>>> pytplot.tsmooth('a')
>>> print(pytplot.data_quants['a-s'].data)

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.

Returns:

None

Examples

>>> pytplot.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]]})
>>> pytplot.spec_mult('h')
>>> print(pytplot.data_quants['h_specmult'].data)