• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

spedas / pyspedas / 25194239086

26 Apr 2026 08:07PM UTC coverage: 61.697% (-28.8%) from 90.54%
25194239086

push

github

jameswilburlewis
Added test for loading Cluster CODIF differential energy flux

0 of 7 new or added lines in 1 file covered. (0.0%)

19460 existing lines in 418 files now uncovered.

30204 of 48955 relevant lines covered (61.7%)

1.44 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

8.62
/pyspedas/tplot_tools/tplot_math/tpwrspc.py
1
import logging
3✔
2
import numpy as np
3✔
3
from pyspedas.tplot_tools import tnames, get_data, store_data, options
3✔
4
from pyspedas.tplot_tools import pwrspc
3✔
5

6

7
def tpwrspc(varname, newname=None, overwrite=False, noline=False, nohanning=False, bin=3, notperhz=False, trange=None, axis=0):
3✔
8
    """
9
    This function is a wrapper for pwrspc.
10
    It applies pwrspc to a tplot variable and stores the result in a new tplot variable.
11

12
    Parameters
13
    ----------
14
        varname: str
15
            Name of the tplot variable.
16

17
        newname: str, optional
18
            New name for the output tplot variable. If not set, default output variable will be
19
            Default: None
20

21
        overwrite: bool, optional
22
            If True, overwrite the existing tplot variable.
23
            If True, then the newname keyword has no effect.
24
            Default: False
25

26
        noline: bool, optional
27
            If True, straight line is not subtracted from the data.
28
            Default: False
29

30
        nohanning: bool, optional
31
            If True, no Hanning window is applied to the data.
32
            Default: False
33
        
34
        bin: int, optional
35
            Bin size for binning the data. 
36
            Default: 3
37

38
        notperhz: bool, optional
39
            If True, the output units are the square of the input units.
40
            Default: False
41

42
        trange: list, optional
43
            Time range for the data extraction.
44
            Default: None
45

46
        axis: int, optional
47
            If the input variable is multi-dimensional, this specifies the axis to operate along.
48
            Default: 0, first axis.
49

50
    Returns
51
    --------
52
    str
53
            Name of the new tplot variable created by this function.
54
            The output variable contains a single data point, frequency as v, and power as y.
55
    
56
    Examples
57
    --------
58
        >>> import pyspedas
59
        >>> import numpy as np
60
        >>> pyspedas.store_data('a', data={'x': range(100), 'y': np.random.random(100)})
61
        >>> pyspedas.tpwrspc('a')
62
        >>> pyspedas.get_data('a_pwrspc')
63
    """
64

65
    # Check if the variable exists
UNCOV
66
    if tnames(varname) == []:
×
67
        logging.info('This tplot variable does not exist.')
×
68
        return ''
×
69

70
    # Check for conflicting arguments
UNCOV
71
    if overwrite:
×
72
        logging.info('Variable will be overwritten.')
×
73
        newname = varname
×
74
    else:
UNCOV
75
        if newname:
×
76
            if tnames(newname) != []:
×
77
                logging.info('This new tplot variable already exists. Please use overwrite=True to overwrite it.')
×
78
                return ''
×
79
        else:
UNCOV
80
            newname = varname + '_pwrspc'
×
81

82
    # Get data from tplot variable
UNCOV
83
    d = get_data(varname)
×
UNCOV
84
    t = np.array(d[0], dtype='float64')
×
UNCOV
85
    data = np.array(d[1], dtype='float64')
×
86

87
    # If the input variable is multi-dimensional, operate along the specified axis
UNCOV
88
    if data.ndim == 1:
×
UNCOV
89
        y = data
×
90
    elif data.ndim == 2:
×
91
        if axis < 1:
×
92
            axis = 0
×
93
        elif axis > data.shape[1]:
×
94
            axis = data.shape[1] - 1
×
95
        logging.info(f'Operating along axis {axis}')
×
96
        y = data[:, axis]
×
97
    else:
98
        logging.info('Cannot handle data with more than two dimensions.')
×
99
        return ''
×
100

101
    # Remove NaN points
UNCOV
102
    tav = np.mean(t)  # We are going to store f,p at the time average point
×
UNCOV
103
    goodpoints = ~np.isnan(t) & ~np.isnan(y)
×
UNCOV
104
    if np.any(goodpoints):
×
UNCOV
105
        t = t[goodpoints]
×
UNCOV
106
        y = y[goodpoints]
×
UNCOV
107
        logging.info('NaN points have been removed.')
×
108
    else:
109
        logging.info(f'No valid data points in {varname}')
×
110
        return ''
×
111

112
    # Restrict to a time range if specified
UNCOV
113
    if trange and len(trange) == 2:
×
114
        ok = (trange[0] <= t < trange[1])
×
115
        if not np.any(ok):
×
116
            logging.info('No data in time range for:', varname)
×
117
            logging.info('No Power spectrum for:', varname)
×
118
            return ''
×
119
        else:
120
            t = t[ok]
×
121
            y = y[ok]
×
122

123
    # Call the power spectrum function (previously defined)
UNCOV
124
    t = t-t[0]
×
UNCOV
125
    f, p = pwrspc(t, y, noline=noline, nohanning=nohanning, bin=bin, notperhz=notperhz)
×
126

UNCOV
127
    if f is not None and p is not None and len(f) > 0 and len(p) > 0 and len(f) == len(p):
×
128
        # Store the result in a new tplot variable
129
        # A single data point will be created, with frequency as v, and power as y
UNCOV
130
        pp = np.array([p,])
×
UNCOV
131
        ff = np.array([f,])
×
UNCOV
132
        tt = np.array([tav,], dtype='float64')
×
UNCOV
133
        store_data(newname, data={'x': tt, 'y':  pp, 'v':  ff})
×
UNCOV
134
        options(newname, 'spec', 1)
×
UNCOV
135
        options(newname, 'ylog', 1)
×
136
    else:
137
        print('No Power spectrum for:', varname)
×
138
        return ''
×
139

UNCOV
140
    return newname
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc