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

spedas / pyspedas / 25223435129

01 May 2026 04:46PM UTC coverage: 15.296% (-75.2%) from 90.54%
25223435129

push

github

jameswilburlewis
Temporarily disabling Cluster tests due to problems at CSA

7488 of 48955 relevant lines covered (15.3%)

0.15 hits per line

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

9.8
/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)
×
50

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

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

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

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

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

76
    old_names = tnames(names)
×
77

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

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

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

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

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

112
    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