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

spedas / pyspedas / 17192022881

24 Aug 2025 06:00PM UTC coverage: 89.279% (+0.09%) from 89.194%
17192022881

push

github

jameswilburlewis
Remove references to obsolete 'new_tvar' parameter

11 of 11 new or added lines in 10 files covered. (100.0%)

29 existing lines in 6 files now uncovered.

40356 of 45202 relevant lines covered (89.28%)

0.89 hits per line

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

94.12
/pyspedas/tplot_tools/tplot_math/subtract_average.py
1

2
import logging
1✔
3
import pyspedas
1✔
4
from pyspedas.tplot_tools import tnames, tplot_copy
1✔
5
import numpy
1✔
6

7

8
def subtract_average(
1✔
9
        names,
10
        newname=None,
11
        suffix=None,
12
        overwrite=None,
13
        median=None
14
):
15
    """
16
    Subtracts the average or median from data.
17

18
    Parameters
19
    ----------
20
    names: str/list of str
21
        List of tplot variable names to process (wildcards accepted)
22
    newname: str/list of str, optional
23
        List of new names for tplot variables.
24
        Default: None. If not given, then a suffix is applied.
25
    suffix: str, optional
26
        A suffix to apply.
27
        Default: '-d'.
28
    overwrite: bool, optional
29
        If set, then tplot variables are replaced.
30
        Default: None
31
    median: float, optional
32
        If it is 0 or not set, then it computes the mean.
33
        Otherwise, it computes the median.
34
        Default: None.
35

36
    Returns
37
    -------
38
    list of str
39
        List of new tplot variables created
40

41
    Examples
42
    --------
43
        >>> pyspedas.store_data('a', data={'x':[0,4,8,12,16], 'y':[1,2,3,4,5]})
44
        >>> pyspedas.subtract_average('a')
45
        >>> pyspedas.tplot(['a','a-d'])
46

47
    """
48

49
    old_names = tnames(names)
1✔
50

51
    if len(old_names) == 0:
1✔
52
        return
1✔
53
    elif len(old_names) < 1:
1✔
UNCOV
54
        logging.error('Subtract Average error: No tplot names were provided.')
×
UNCOV
55
        return
×
56

57
    if suffix is None:
1✔
58
        if median:
1✔
59
            suffix = '-m'
1✔
60
        else:
61
            suffix = '-d'
1✔
62

63
    if overwrite is not None:
1✔
64
        n_names = old_names
1✔
65
    elif newname is None:
1✔
66
        n_names = [s + suffix for s in old_names]
1✔
67
    else:
68
        n_names = newname
1✔
69

70
    if isinstance(n_names, str):
1✔
71
        n_names = [n_names]
1✔
72

73
    if len(n_names) != len(old_names):
1✔
74
        n_names = [s + suffix for s in old_names]
1✔
75

76
    old_names = tnames(names)
1✔
77

78
    for old_idx, old in enumerate(old_names):
1✔
79
        new = n_names[old_idx]
1✔
80

81
        if new != old:
1✔
82
            tplot_copy(old, new)
1✔
83

84
        data = pyspedas.tplot_tools.data_quants[new].values
1✔
85
        # Subtracting the average will fail if data is not a floating point type
86
        if data.dtype.kind != 'f':
1✔
UNCOV
87
            data=numpy.float64(data)
×
88
        dim = data.shape
1✔
89
        if median:
1✔
90
            if len(dim) == 1:
1✔
91
                if not numpy.isnan(data).all():
1✔
92
                        data -= numpy.nanmedian(data, axis=0)
1✔
93
            else:
94
                for i in range(dim[1]):
1✔
95
                    if not numpy.isnan(data[:, i]).all():
1✔
96
                        data[:, i] -= numpy.nanmedian(data[:, i], axis=0)
1✔
97
            ptype = 'Median'
1✔
98
        else:
99
            if len(dim) == 1:
1✔
100
                if not numpy.isnan(data).all():
1✔
101
                    data -= numpy.nanmean(data, axis=0)
1✔
102
            else:
103
                for i in range(dim[1]):
1✔
104
                    if not numpy.isnan(data[:, i]).all():
1✔
105
                        data[:, i] -= numpy.nanmean(data[:, i], axis=0)
1✔
106
            ptype = 'Mean'
1✔
107

108
        pyspedas.tplot_tools.data_quants[new].values = data
1✔
109

110
        logging.info('Subtract ' + ptype + ' was applied to: ' + new)
1✔
111

112
    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