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

spedas / pyspedas / 23096825121

14 Mar 2026 09:34PM UTC coverage: 10.65% (-79.8%) from 90.486%
23096825121

push

github

jameswilburlewis
Allow uploading of hidden files

5208 of 48900 relevant lines covered (10.65%)

0.11 hits per line

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

12.96
/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
1✔
13
import math
1✔
14
import numpy as np
1✔
15
import pyspedas
1✔
16
from pyspedas.tplot_tools import tnames, tplot_copy
1✔
17

18

19
def smooth(data, width=10, preserve_nans=None):
1✔
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()
×
45
    N = len(data)
×
46

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

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

66

67
def tsmooth(names, width=10, median=None, preserve_nans=None,
1✔
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)
×
107

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

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

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

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

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

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

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

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

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

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

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

148
    return n_names
×
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