• 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

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

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

7

8
def subtract_average(
3✔
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

UNCOV
49
    old_names = tnames(names)
1✔
50

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

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

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

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

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

UNCOV
76
    old_names = tnames(names)
1✔
77

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

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

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

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

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

UNCOV
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