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

spedas / pyspedas / 17001196665

15 Aug 2025 11:12PM UTC coverage: 89.516% (+0.7%) from 88.849%
17001196665

push

github

web-flow
Merge branch 'pyspedas_2_0_dev' into master

5072 of 6199 new or added lines in 413 files covered. (81.82%)

8 existing lines in 2 files now uncovered.

40061 of 44753 relevant lines covered (89.52%)

0.9 hits per line

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

94.74
/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()
1✔
45
    N = len(data)
1✔
46

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

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

66

67
def tsmooth(names, width=10, median=None, preserve_nans=None,
1✔
68
            newname=None, new_names=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
    new_names: str/list of str, optional (deprecated)
87
        List of new names for tplot variables.
88
        If not given, then a suffix is applied.
89
        The default is None.
90
    suffix: str, optional
91
        A suffix to apply. Default is '-s'.
92
        The default is None.
93
    overwrite: bool, optional
94
        Replace the existing tplot name.
95
        The default is None.
96

97
    Returns
98
    -------
99
    list of str
100
        Returns list of tplot variables created or changed
101

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

109
    """
110
    old_names = tnames(names)
1✔
111

112
    if len(old_names) < 1:
1✔
113
        logging.error('tsmooth: No valid tplot variable names were provided.')
1✔
114
        return
1✔
115

116
    # new_names is deprecated
117
    if new_names is not None:
1✔
NEW
118
        logging.info("tsmooth: The new_names parameter is deprecated. Please use newname instead.")
×
NEW
119
        newname = new_names
×
120

121
    if suffix is None:
1✔
122
        suffix = '-s'
1✔
123

124
    if overwrite is not None:
1✔
125
        n_names = old_names
1✔
126
    elif newname is None:
1✔
127
        n_names = [s + suffix for s in old_names]
1✔
128
    else:
129
        n_names = newname
1✔
130

131
    if isinstance(n_names, str):
1✔
132
        n_names = [n_names]
1✔
133

134
    if len(n_names) != len(old_names):
1✔
135
        n_names = [s + suffix for s in old_names]
1✔
136

137
    for i, old in enumerate(old_names):
1✔
138
        new = n_names[i]
1✔
139

140
        if new != old:
1✔
141
            tplot_copy(old, new)
1✔
142

143
        data = pyspedas.tplot_tools.data_quants[new].values
1✔
144

145
        dim = data.shape
1✔
146
        if len(dim) == 1:
1✔
147
            data = smooth(data, width=width, preserve_nans=preserve_nans)
1✔
148
        else:
149
            for k in range(dim[1]):
1✔
150
                data[:, k] = smooth(data[:, k], width=width,
1✔
151
                                    preserve_nans=preserve_nans)
152

153
        pyspedas.tplot_tools.data_quants[new].values = data
1✔
154

155
        logging.info('tsmooth was applied to: ' + new)
1✔
156

157
    return n_names
1✔
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