• 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

15.79
/pyspedas/tplot_tools/tplot_math/pwr_spec.py
1
import numpy as np
3✔
2
import logging
3✔
3
import pyspedas
3✔
4
from pyspedas.tplot_tools import data_exists, store_data, get_data, options
3✔
5
from scipy import signal
3✔
6

7

8
# First pass at the power spectrum function.  This is still missing several features of the IDL power spectrum routine, such as
9
# bin, nohanning, notperhertz, and tm_sensativity.  The IDL routine is located in dpwrspc.pro.
10

11
# There is also the issue of this not quite having the same units as the plot I use as my reference.
12
# https://agupubs.onlinelibrary.wiley.com/doi/full/10.1002/2015GL065366#grl53372-bib-0016
13
# Interestingly enough, the output is the same if units of seconds are used in the periodogram instead of Hertz.
14
# Perhaps they calculated it differently?
15

16

17
def pwr_spec(tvar, nbp=256, nsp=128, newname=None):
3✔
18
    """
19
    Calculates the power spectrum of a line, and adds a tplot variable for this new spectrogram
20

21
    Parameters
22
    ----------
23
        tvar : str
24
            Name of tvar to use
25
        nbp : int, optional
26
            The number of points to use when calculating the FFT
27
        nsp : int, optional
28
            The number of points to shift over to calculate the next FFT
29
        newname : str, optional
30
            The name of the new tplot variable created,
31

32
    Returns
33
    -------
34
        None
35

36
    Examples
37
    --------
38
        >>> import pyspedas
39
        >>> import math
40
        >>> time = [pyspedas.time_float("2020-01-01") + i for i in range(10000)]
41
        >>> quantity = [math.sin(i) for i in range(10000)]
42
        >>> pyspedas.store_data("dp", data={"x": time, "y": quantity})
43
        >>> pyspedas.pwr_spec("dp", newname="dp_pwrspec")
44
        >>> pyspedas.tplot("dp_pwrspec")
45
    """
46

UNCOV
47
    if not data_exists(tvar):
×
48
        logging.error("Input variable %s does not exist", tvar)
×
49
        return
×
50

UNCOV
51
    d =get_data(tvar)
×
UNCOV
52
    x, y = d[0], d[1]
×
53

UNCOV
54
    if len(y.shape) > 1:
×
55
        logging.warning(
×
56
            "Cannot create pwr_spec for variable %s, must be a single line", tvar
57
        )
58

UNCOV
59
    l = len(x)
×
UNCOV
60
    x_new = []
×
UNCOV
61
    f_new = []
×
UNCOV
62
    pxx_new = []
×
UNCOV
63
    shift_lsp = np.arange(0, l - 1, nsp)
×
UNCOV
64
    for i in shift_lsp:
×
UNCOV
65
        x_n = x[i : i + nbp]
×
UNCOV
66
        y_n = y[i : i + nbp]
×
UNCOV
67
        if len(x_n) < nbp:
×
UNCOV
68
            continue
×
69

UNCOV
70
        median_diff_between_points = np.median(np.diff(x_n))
×
UNCOV
71
        w = signal.get_window("hamming", nbp)
×
UNCOV
72
        f, pxx = signal.periodogram(
×
73
            y_n, fs=(1 / median_diff_between_points), window=w, detrend="linear"
74
        )
UNCOV
75
        f = f[1:-1]
×
UNCOV
76
        pxx = pxx[1:-1]
×
UNCOV
77
        x_new.append((x_n[-1] + x_n[0]) / 2)
×
UNCOV
78
        f_new.append(f)
×
UNCOV
79
        pxx_new.append(pxx)
×
80

UNCOV
81
    if newname is None:
×
82
        newname = tvar + "_pwrspec"
×
83

UNCOV
84
    store_data(newname, data={"x": x_new, "y": pxx_new, "v": f_new})
×
UNCOV
85
    options(newname, "spec", 1)
×
UNCOV
86
    options(newname, "zlog", 1)
×
UNCOV
87
    options(newname, "ylog", 1)
×
88

UNCOV
89
    return
×
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