• 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

80.61
/pyspedas/tplot_tools/get_data.py
1
# Copyright 2018 Regents of the University of Colorado. All Rights Reserved.
2
# Released under the MIT license.
3
# This software was developed at the University of Colorado's Laboratory for Atmospheric and Space Physics.
4
# Verify current version before use at: https://github.com/MAVENSDC/PyTplot
5
import numpy as np
3✔
6
import pyspedas
3✔
7
from collections import namedtuple
3✔
8
import logging
3✔
9
from astropy import units as u
3✔
10

11

12
def get_data(name, xarray=False, metadata=False, dt=False, units=False, data_quant_in=None, ensure_writeable=False):
3✔
13
    """
14
    This function extracts the data from the tplot Variables stored in memory.
15
    
16
    Parameters
17
    ----------
18
        name : str 
19
            Name of the tplot variable
20
        xarray : bool, optional
21
            Keep the variable as an xarray object
22
        metadata : bool, optional
23
            Return the metadata of the object (the attr dictionary) instead of the actual data
24
        dt: bool, optional
25
            Return the times as np.datetime64[ns] objects instead of unix times
26
            (significantly faster)
27
        units: bool, optional
28
            Attach the astropy units to the data and dependencies prior to returning
29
        ensure_writeable: bool, optional
30
            Ensure that returned arrays are writeable, rather than (for example) read-only views of pandas
31
            data frame indices. Specify 'True' if you need to modify the returned arrays. Defaults to 'False'
32
            for efficiency.
33
         
34
    Returns
35
    --------
36
    times: ndarray[float]
37
        numpy array of seconds since 1970
38
    y: ndarray
39
        n-dimensional numpy array of the data values
40
    v: ndarray
41
        If exists, an array of bin values for 1-D data types
42
    spec_bins: ndarray
43
        If exists, an array of the spectrogram bins for the bin values
44
    v1: ndarray
45
        If exists, numpy array of the v1 dimension coordinates
46
    v2: ndarray
47
        If exists, numpy array of the v2 dimension coordinates
48
    v3: ndarray
49
        If exists, numpy array of the v3 dimension coordinates
50

51

52

53
    Notes:
54
    ------
55

56
    If metadata==True, the return value is a dict containing variable attributes and plot options.
57
    Otherwise, the return value is a named tuple with the fields described above.
58

59
    Examples
60
    --------
61
        >>> # Retrieve the data from Variable 1
62
        >>> import pyspedas
63
        >>> x_data = [1,2,3,4,5]
64
        >>> y_data = [1,2,3,4,5]
65
        >>> pyspedas.store_data("Variable1", data={'x':x_data, 'y':y_data})
66
        >>> time, data = pyspedas.get_data("Variable1")
67
        >>> metadata = pyspedas.get_data("Variable1", metadata=True)
68

69
    """
70

71
    #check for an input data_quant object
72
    if data_quant_in is not None:
3✔
UNCOV
73
        temp_data_quant = data_quant_in
×
74
    else:
75
        if name not in pyspedas.tplot_tools.data_quants.keys():
3✔
76
            logging.info("The name " + str(name) + " is currently not in pyspedas")
3✔
77
            return
3✔
78
    
79
        temp_data_quant = pyspedas.tplot_tools.data_quants[name]
3✔
80

81
    if isinstance(temp_data_quant, dict):
3✔
82
        # non-record varying variables are stored as dicts
83
        return temp_data_quant['data']
3✔
84

85
    if xarray:
3✔
86
        return temp_data_quant
3✔
87

88
    if metadata:
3✔
89
        return temp_data_quant.attrs
3✔
90

91
    error = temp_data_quant.attrs['plot_options']['error']
3✔
92

93
    if not dt:
3✔
94
        # TODO: Is this always at ns resolution?  ERG LEPE CDFs seem to be in microseconds
95
        times = np.int64(temp_data_quant.time.values)/1e9
3✔
96
        #times = np.array([int(time)/1e9 for time in temp_data_quant.time.values])
97
        #times = np.array([(time - np.datetime64('1970-01-01T00:00:00'))/np.timedelta64(1, 's') for time in temp_data_quant.time.values])
98
    else:
99
        times = temp_data_quant.time.values
3✔
100

101
    coord_names = temp_data_quant.coords.keys()
3✔
102
    data_values = temp_data_quant.data
3✔
103

104
    # Temporary patch for Pandas 3.0.0
105
    # Arguably, this is dangerous and should not be allowed...
106
    if ensure_writeable and not times.flags['WRITEABLE']:
3✔
107
        times=times.copy()
×
108
    if ensure_writeable and not data_values.flags['WRITEABLE']:
3✔
109
        data_values=data_values.copy()
×
110

111
    v1_values = None
3✔
112
    v2_values = None
3✔
113
    v3_values = None
3✔
114

115
    data_att_set = 'data_att' in temp_data_quant.attrs
3✔
116

117
    if 'v' in coord_names:
3✔
118
        v1_values = temp_data_quant.coords['v'].values
3✔
119
    if 'spec_bins' in coord_names:
3✔
120
        v1_values = temp_data_quant.coords['spec_bins'].values
3✔
121
    if 'v1' in coord_names:
3✔
122
        v1_values = temp_data_quant.coords['v1'].values
3✔
123
    if 'v2' in coord_names:
3✔
124
        v2_values = temp_data_quant.coords['v2'].values
3✔
125
    if 'v3' in coord_names:
3✔
126
        v3_values = temp_data_quant.coords['v3'].values
3✔
127

128
    if data_att_set and units:
3✔
129
        data_units = temp_data_quant.attrs['data_att'].get('units')
3✔
130
        v1_units = temp_data_quant.attrs['data_att'].get('depend_1_units')
3✔
131
        v2_units = temp_data_quant.attrs['data_att'].get('depend_2_units')
3✔
132
        v3_units = temp_data_quant.attrs['data_att'].get('depend_3_units')
3✔
133

134
        try:
3✔
135
            if data_units is not None:
3✔
136
                data_values = data_values * u.Unit(data_units)
3✔
137
            if v1_values is not None and v1_units is not None:
3✔
138
                v1_values = v1_values * u.Unit(v1_units)
1✔
139
            if v2_values is not None and v2_units is not None:
3✔
140
                v2_values = v2_values * u.Unit(v2_units)
×
141
            if v3_values is not None and v3_units is not None:
3✔
142
                v3_values = v3_values * u.Unit(v3_units)
×
143
        except ValueError:
3✔
144
            # occurs when there's a problem converting the units string
145
            # to astropy units
146
            pass
3✔
147

148
    if 'v1' in coord_names and 'v2' in coord_names and 'v3' in coord_names:
3✔
149
        if ensure_writeable and not v1_values.flags['WRITEABLE']:
3✔
150
            v1_values = v1_values.copy()
×
151
        if ensure_writeable and not v2_values.flags['WRITEABLE']:
3✔
152
            v2_values = v2_values.copy()
×
153
        if ensure_writeable and not v3_values.flags['WRITEABLE']:
3✔
154
            v3_values = v3_values.copy()
×
155
        variable = namedtuple('variable', ['times', 'y', 'v1', 'v2', 'v3'])
3✔
156
        return variable(times, data_values, v1_values, v2_values, v3_values)
3✔
157
    elif 'v1' in coord_names and 'v2' in coord_names:
3✔
158
        if ensure_writeable and not v1_values.flags['WRITEABLE']:
3✔
159
            v1_values = v1_values.copy()
×
160
        if ensure_writeable and not v2_values.flags['WRITEABLE']:
3✔
161
            v2_values = v2_values.copy()
×
162

163
        variable = namedtuple('variable', ['times', 'y', 'v1', 'v2'])
3✔
164
        return variable(times, data_values, v1_values, v2_values)
3✔
165
    elif 'v1' in coord_names:
3✔
166
        if ensure_writeable and not v1_values.flags['WRITEABLE']:
3✔
167
            v1_values = v1_values.copy()
×
168

169
        variable = namedtuple('variable', ['times', 'y', 'v1'])
3✔
170
        return variable(times, data_values, v1_values)
3✔
171
    elif 'v' in coord_names:
3✔
172
        if ensure_writeable and not v1_values.flags['WRITEABLE']:
3✔
173
            v1_values = v1_values.copy()
3✔
174

175
        variable = namedtuple('variable', ['times', 'y', 'v'])
3✔
176
        return variable(times, data_values, v1_values)
3✔
177
    elif 'spec_bins' in coord_names:
3✔
UNCOV
178
        if ensure_writeable and not v1_values.flags['WRITEABLE']:
×
179
            v1_values = v1_values.copy()
×
180

UNCOV
181
        variable = namedtuple('variable', ['times', 'y', 'v'])
×
UNCOV
182
        return variable(times, data_values, v1_values)
×
183

184
    if error is not None:
3✔
UNCOV
185
        if ensure_writeable and not error.flags['WRITEABLE']:
×
186
            error = error.copy()
×
UNCOV
187
        variable = namedtuple('variable', ['times', 'y', 'dy'])
×
UNCOV
188
        return variable(times, data_values, error)
×
189
    else:
190
        variable = namedtuple('variable', ['times', 'y'])
3✔
191
        return variable(times, data_values)
3✔
192

193

194
def get(name, xarray=False, metadata=False, dt=True, units=True, ensure_writeable=False):
3✔
195
    """
196
    This function extracts the data from the tplot Variables stored in memory.
197

198
    Parameters:
199
        name : str
200
            Name of the tplot variable
201
        xarray : bool
202
            Keep the variable as an xarray object
203
        metadata : bool
204
            Return the metadata of the object (the attr dictionary) instead of the actual data
205
        dt : bool
206
            Return the times as np.datetime64[ns] objects instead of unix times
207
            (significantly faster); defaults to True for pyspedas.get
208
        units: bool
209
            Attach the astropy units to the data and dependencies prior to returning
210
            defaults to True for pyspedas.get
211
        ensure_writeable: bool, optional
212
            Ensure that returned arrays are writeable, rather than (for example) read-only views of pandas
213
            data frame indices. Specify 'True' if you need to modify the returned arrays. Defaults to 'False'
214
            for efficiency.
215

216

217
    Returns: tuple of data/dimensions/metadata stored in pyspedas
218
        time_val : numpy array of seconds since 1970
219
        data_val : n-dimensional array of data
220
        spec_bins_val (if exists) : spectral bins if the plot is a spectrogram
221
        v1_val (if exists) : numpy array of v1 dimension coordinates
222
        v2_val {if exists} : numpy array of v2 dimension coordinates
223
        v3_val (if exists) : numpy array of v3 dimension coordinates
224

225

226
    Examples:
227
        >>> # Retrieve the data from Variable 1
228
        >>> import pyspedas
229
        >>> x_data = [1,2,3,4,5]
230
        >>> y_data = [1,2,3,4,5]
231
        >>> pyspedas.store("Variable1", data={'x':x_data, 'y':y_data})
232
        >>> time, data = pyspedas.get("Variable1")
233

234
    """
235
    return get_data(name, xarray=xarray, metadata=metadata, dt=dt, units=units, ensure_writeable=ensure_writeable)
3✔
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