Math Routines¶
Arithmetic¶
-
pytplot.tplot_math.
add
(tvar1, tvar2, 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
- new_tvar – str 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','a+c')
-
pytplot.tplot_math.
subtract
(tvar1, tvar2, 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
- new_tvar – str 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','a-c')
-
pytplot.tplot_math.
multiply
(tvar1, tvar2, 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
- new_tvar – str Name of new tplot variable. 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.multiply('a','c','a_x_c')
-
pytplot.tplot_math.
divide
(tvar1, tvar2, 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 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')
-
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')
-
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)
Add Across Columns¶
-
pytplot.tplot_math.
add_across
(tvar, column_range=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.
- new_tvar – str 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',new_tvar='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]],new_tvar='b_aap') >>> print(pytplot.data_quants['b_aap'].data)
Average over time¶
-
pytplot.tplot_math.
avg_res_data
(tvar, res, 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 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)
Clip Data¶
-
pytplot.tplot_math.
clip
(tvar, ymin, ymax, 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)
- newtvar – str Name of new tvar for clipped data storage. If not specified, tvar will be replaced
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')
Deflag Data¶
-
pytplot.tplot_math.
deflag
(tvar, flag, new_tvar=None)[source]¶ Change specified ‘flagged’ data to NaN.
Parameters: - tvar1 – str Name of tplot variable to use for data clipping.
- flag – int,list Flagged data will be converted to NaNs.
- newtvar – str Name of new tvar for deflagged data storage. If not specified, then the data in tvar1 will be replaced.
Returns: None
Examples
>>> # Remove any instances of [100,90,7,2,57] from 'd', store in 'e'. >>> 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]]}) >>> pytplot.deflag('d',[100,90,7,2,57],'e')
Degap Data¶
-
pytplot.tplot_math.
degap
(tvar, dt, margin, func='nan', new_tvar=None)[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
- margin – int/float, optional 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.
- new_tvar – str, optional The new tplot variable name to store the data into. If None, then the data is overwritten.
Returns: None
Examples
>>> # TODO
Derivative¶
-
pytplot.tplot_math.
derive
(tvar, new_tvar=None)[source]¶ Takes the derivative of the tplot variable.
Parameters: - tvar – str Name of tplot variable.
- new_tvar – 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)
Flatten Data¶
-
pytplot.tplot_math.
flatten
(tvar, range=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.
- new_tvar – str 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, 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.
- 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.
- new_tvar – str Name of new tvar for added data. If not set, then the original tvar is replaced.
Returns: None
Examples
>>> # Interpolate through the np.NaN values >>> 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, 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
- new_tvar – str, optional 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
Returns: None
Examples
>>> 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'],'deg') >>> print(pytplot.data_quants['deg'].values)
-
pytplot.tplot_math.
split_vec
(tvar, new_name=None, columns='all', suffix=None)[source]¶ Splits up 2D data into many 1D tplot variables.
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 to split up
- newtvars – int/list, optional The names of the new tplot variables. This must be the same length as the number of variables created.
- columns – list of ints, optional The specific column numbers to grab from the data. The default is to split all columns.
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',['b1','b2','b3'],[0,[1,3],4]) >>> print(pytplot.data_quants['b2'].values)
Power Spectrum¶
-
pytplot.tplot_math.
pwr_spec
(tvar, nbp=256, nsp=128, name=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
- name – str, optional The name of the new tplot variable created,
Returns: None
Examples
>>> pytplot.cdf_to_tplot("/path/to/pytplot/testfiles/mvn_euv_l2_bands_20170619_v09_r03.cdf") >>> pytplot.tplot_math.split_vec('data') >>> pytplot.pwr_spec('data_0') >>> pytplot.tplot('data_0_pwrspec')
Resample Data¶
-
pytplot.tplot_math.
resample
(tvar, times, new_tvar=None)[source]¶ Linearly interpolates data to user-specified values. To interpolate one tplot variable to another, use tinterp.
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 tvar whose data will be interpolated to specified times.
- times – int/list Desired times for interpolation.
- new_tvar – str Name of new tvar in which to store interpolated data. If none is specified, tvar will be overwritten
Returns: None
Examples
>>> # Interpolate data for 'd' to values [3,4,5,6,7,18]. >>> 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.tplot_resample('d',[3,4,5,6,7,18],'d_resampled')
Spectrum Multiplication¶
-
pytplot.tplot_math.
spec_mult
(tvar, 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
- times – int/list Desired times for interpolation.
- new_tvar – str 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','h_specmult') >>> print(pytplot.data_quants['h_specmult'].data)