• 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

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

18

19
def smooth(data, width=10, preserve_nans=None):
3✔
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
    """
UNCOV
44
    result = data.copy()
1✔
UNCOV
45
    N = len(data)
1✔
46

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

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

66

67
def tsmooth(names, width=10, median=None, preserve_nans=None,
3✔
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
    """
UNCOV
106
    old_names = tnames(names)
1✔
107

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

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

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

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

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

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

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

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

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

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

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

UNCOV
148
    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