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

spedas / pyspedas / 25391193746

05 May 2026 05:15PM UTC coverage: 76.027% (+19.6%) from 56.446%
25391193746

push

github

jameswilburlewis
Restore Cluster tests -- CSA seems to be back online

37244 of 48988 relevant lines covered (76.03%)

2.41 hits per line

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

98.15
/pyspedas/tplot_tools/tplot_math/tsmooth.py
1
"""
2
Smooths a tplot variable.
3

4
Uses a boxcar average of the specified width.
5

6
Notes
7
-----
8
Similar to tsmooth2.pro in IDL SPEDAS.
9
Also, see: https://www.harrisgeospatial.com/docs/SMOOTH.html
10

11
"""
12
import logging
4✔
13
import math
4✔
14
import numpy as np
4✔
15
import pyspedas
4✔
16
from pyspedas.tplot_tools import tnames, tplot_copy
4✔
17

18

19
def smooth(data, width=10, preserve_nans=None):
4✔
20
    """
21
    Boxcar average.
22

23
    Parameters
24
    ----------
25
    data : list of floats
26
        The data should be a one-dim array.
27
    width : float, optional
28
        Data window to use for smoothing. The default is 10.
29
    preserve_nans : bool, optional
30
        If None, then replace NaNs. The default is None.
31

32
    Returns
33
    -------
34
    list of float
35
        Smoothed data.
36
    
37
    Example
38
    -------
39
        >>> import pyspedas
40
        >>> import numpy as np
41
        >>> print(pyspedas.smooth(np.random.random(100)))
42

43
    """
44
    result = data.copy()
3✔
45
    N = len(data)
3✔
46

47
    if N <= width:
3✔
48
        logging.error("smooth: Not enough points.")
3✔
49
        return result
3✔
50

51
    for i, d in enumerate(data):
3✔
52
        if (i >= (width-1)/2) and (i <= N-(width+1)/2):
3✔
53
            if (preserve_nans is not None) and data[i] is np.nan:
3✔
54
                continue
×
55
            tsum = 0
3✔
56
            count = 0
3✔
57
            for j in range(int(width)):
3✔
58
                idx = math.ceil(i+j-width/2)
3✔
59
                if data[idx] is not np.nan:
3✔
60
                    tsum += data[idx]
3✔
61
                    count += 1
3✔
62
            if count > 0:  # otherwise, all NaN
3✔
63
                result[i] = (1/width) * tsum
3✔
64
    return result
3✔
65

66

67
def tsmooth(names, width=10, median=None, preserve_nans=None,
4✔
68
            newname=None, suffix=None, overwrite=None):
69
    """
70
    Smooths a tplot variable.
71

72
    Parameters
73
    ----------
74
    names: str/list of str
75
        List of tplot variable names to be smoothed (wildcards accepted)
76
    width: int, optional
77
        Data window to use for smoothing. The default is 10.
78
    median: bool, optional
79
        Apply the median as well. The default is None.
80
    preserve_nans: bool, optional
81
        If None, then replace NaNs. The default is None.
82
    newname: str/list of str, optional
83
        List of new names for tplot variables.
84
        If not given, then a suffix is applied. 
85
        The default is None.
86
    suffix: str, optional
87
        A suffix to apply. Default is '-s'.
88
        The default is None.
89
    overwrite: bool, optional
90
        Replace the existing tplot name.
91
        The default is None.
92

93
    Returns
94
    -------
95
    list of str
96
        Returns list of tplot variables created or changed
97

98
    Example
99
    -------
100
        >>> import pyspedas
101
        >>> import numpy as np
102
        >>> pyspedas.store_data('a', data={'x': range(100), 'y': np.random.random(100)})
103
        >>> pyspedas.tsmooth('a')
104

105
    """
106
    old_names = tnames(names)
3✔
107

108
    if len(old_names) < 1:
3✔
109
        logging.error('tsmooth: No valid tplot variable names were provided.')
3✔
110
        return
3✔
111

112
    if suffix is None:
3✔
113
        suffix = '-s'
3✔
114

115
    if overwrite is not None:
3✔
116
        n_names = old_names
3✔
117
    elif newname is None:
3✔
118
        n_names = [s + suffix for s in old_names]
3✔
119
    else:
120
        n_names = newname
3✔
121

122
    if isinstance(n_names, str):
3✔
123
        n_names = [n_names]
3✔
124

125
    if len(n_names) != len(old_names):
3✔
126
        n_names = [s + suffix for s in old_names]
3✔
127

128
    for i, old in enumerate(old_names):
3✔
129
        new = n_names[i]
3✔
130

131
        if new != old:
3✔
132
            tplot_copy(old, new)
3✔
133

134
        data = pyspedas.tplot_tools.data_quants[new].values
3✔
135

136
        dim = data.shape
3✔
137
        if len(dim) == 1:
3✔
138
            data = smooth(data, width=width, preserve_nans=preserve_nans)
3✔
139
        else:
140
            for k in range(dim[1]):
3✔
141
                data[:, k] = smooth(data[:, k], width=width,
3✔
142
                                    preserve_nans=preserve_nans)
143

144
        pyspedas.tplot_tools.data_quants[new].values = data
3✔
145

146
        logging.info('tsmooth was applied to: ' + new)
3✔
147

148
    return n_names
3✔
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