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

spedas / pyspedas / 21464256675

29 Jan 2026 03:16AM UTC coverage: 90.019% (+1.3%) from 88.681%
21464256675

push

github

jameswilburlewis
Add some 'writeable' flags to get_data calls where test code writes to arrays returned by get_data

10 of 10 new or added lines in 1 file covered. (100.0%)

118 existing lines in 5 files now uncovered.

41622 of 46237 relevant lines covered (90.02%)

0.9 hits per line

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

87.76
/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
1✔
6
import pyspedas
1✔
7
from collections import namedtuple
1✔
8
import logging
1✔
9
from astropy import units as u
1✔
10

11

12
def get_data(name, xarray=False, metadata=False, dt=False, units=False, data_quant_in=None, writeable=False):
1✔
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 dependencioes prior to returning
29
        writeable: bool, optional
30
            EXPERIMENTAL, DO NOT USE IN PRODUCTION YET!   Return copies of output arrays, rather than
31
            views of the pandas dataframe, to avoid exceptions due to read-only arrays returned by pandas>=3.0.0
32
         
33
    Returns
34
    --------
35
    times: ndarray[float]
36
        numpy array of seconds since 1970
37
    y: ndarray
38
        n-dimensional numpy array of the data values
39
    v: ndarray
40
        If exists, an array of bin values for 1-D data types
41
    spec_bins: ndarray
42
        If exists, an array of the spectrogram bins for the bin values
43
    v1: ndarray
44
        If exists, numpy array of the v1 dimension coordinates
45
    v2: ndarray
46
        If exists, numpy array of the v2 dimension coordinates
47
    v3: ndarray
48
        If exists, numpy array of the v3 dimension coordinates
49

50

51

52
    Notes:
53
    ------
54

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

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

68
    """
69

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

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

84
    if xarray:
1✔
85
        return temp_data_quant
1✔
86

87
    if metadata:
1✔
88
        return temp_data_quant.attrs
1✔
89

90
    error = temp_data_quant.attrs['plot_options']['error']
1✔
91

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

100
    coord_names = temp_data_quant.coords.keys()
1✔
101
    data_values = temp_data_quant.data
1✔
102

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

110
    v1_values = None
1✔
111
    v2_values = None
1✔
112
    v3_values = None
1✔
113

114
    data_att_set = 'data_att' in temp_data_quant.attrs
1✔
115

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

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

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

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

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

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

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

180
        variable = namedtuple('variable', ['times', 'y', 'v'])
1✔
181
        return variable(times, data_values, v1_values)
1✔
182

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

192

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

197
    Parameters:
198
        name : str
199
            Name of the tplot variable
200
        xarray : bool
201
            Keep the variable as an xarray object
202
        metadata : bool
203
            Return the metadata of the object (the attr dictionary) instead of the actual data
204
        dt : bool
205
            Return the times as np.datetime64[ns] objects instead of unix times
206
            (significantly faster); defaults to True for pyspedas.get
207
        units: bool
208
            Attach the astropy units to the data and dependencioes prior to returning
209
            defaults to True for pyspedas.get
210

211
    Returns: tuple of data/dimensions/metadata stored in pyspedas
212
        time_val : numpy array of seconds since 1970
213
        data_val : n-dimensional array of data
214
        spec_bins_val (if exists) : spectral bins if the plot is a spectrogram
215
        v1_val (if exists) : numpy array of v1 dimension coordinates
216
        v2_val {if exists} : numpy array of v2 dimension coordinates
217
        v3_val (if exists) : numpy array of v3 dimension coordinates
218

219

220
    Examples:
221
        >>> # Retrieve the data from Variable 1
222
        >>> import pyspedas
223
        >>> x_data = [1,2,3,4,5]
224
        >>> y_data = [1,2,3,4,5]
225
        >>> pyspedas.store("Variable1", data={'x':x_data, 'y':y_data})
226
        >>> time, data = pyspedas.get("Variable1")
227

228
    """
229
    return get_data(name, xarray=xarray, metadata=metadata, dt=dt, units=units)
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