• 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.44
/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
        new_names=None,
12
        suffix=None,
13
        overwrite=None,
14
        median=None
15
):
16
    """
17
    Subtracts the average or median from data.
18

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

40
    Returns
41
    -------
42
    list of str
43
        List of new tplot variables created
44

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

51
    """
52

53
    # new_names is deprecated in favor of newname
54
    if new_names is not None:
1✔
55
        logging.info("subtract_average: The new_names parameter is deprecated. Please use newname instead.")
1✔
56
        newname = new_names
1✔
57

58
    old_names = tnames(names)
1✔
59

60
    if len(old_names) == 0:
1✔
61
        return
1✔
62
    elif len(old_names) < 1:
1✔
NEW
63
        logging.error('Subtract Average error: No tplot names were provided.')
×
NEW
64
        return
×
65

66
    if suffix is None:
1✔
67
        if median:
1✔
68
            suffix = '-m'
1✔
69
        else:
70
            suffix = '-d'
1✔
71

72
    if overwrite is not None:
1✔
73
        n_names = old_names
1✔
74
    elif newname is None:
1✔
75
        n_names = [s + suffix for s in old_names]
1✔
76
    else:
77
        n_names = newname
1✔
78

79
    if isinstance(n_names, str):
1✔
80
        n_names = [n_names]
1✔
81

82
    if len(n_names) != len(old_names):
1✔
83
        n_names = [s + suffix for s in old_names]
1✔
84

85
    old_names = tnames(names)
1✔
86

87
    for old_idx, old in enumerate(old_names):
1✔
88
        new = n_names[old_idx]
1✔
89

90
        if new != old:
1✔
91
            tplot_copy(old, new)
1✔
92

93
        data = pyspedas.tplot_tools.data_quants[new].values
1✔
94
        # Subtracting the average will fail if data is not a floating point type
95
        if data.dtype.kind != 'f':
1✔
NEW
96
            data=numpy.float64(data)
×
97
        dim = data.shape
1✔
98
        if median:
1✔
99
            if len(dim) == 1:
1✔
100
                if not numpy.isnan(data).all():
1✔
101
                        data -= numpy.nanmedian(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.nanmedian(data[:, i], axis=0)
1✔
106
            ptype = 'Median'
1✔
107
        else:
108
            if len(dim) == 1:
1✔
109
                if not numpy.isnan(data).all():
1✔
110
                    data -= numpy.nanmean(data, axis=0)
1✔
111
            else:
112
                for i in range(dim[1]):
1✔
113
                    if not numpy.isnan(data[:, i]).all():
1✔
114
                        data[:, i] -= numpy.nanmean(data[:, i], axis=0)
1✔
115
            ptype = 'Mean'
1✔
116

117
        pyspedas.tplot_tools.data_quants[new].values = data
1✔
118

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

121
    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