• 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

78.57
/pyspedas/tplot_tools/tplot_math/split_vec.py
1
"""
2
Splits up 2D data into many 1D tplot variables.
3

4
Notes
5
-----
6
Similar to split_vec.pro in IDL SPEDAS.
7

8
"""
9

10
import copy
3✔
11
import logging
3✔
12
import pyspedas
3✔
13
from pyspedas.tplot_tools import store_data, get_data
3✔
14
import numpy as np
3✔
15
import logging
3✔
16

17
def split_vec(
3✔
18
        tvar,
19
        polar=False,
20
        newname=None,
21
        new_name=None,
22
        columns='all',
23
        suffix=None
24
):
25
    """
26
    Splits up 2D data into many 1D tplot variables. Takes a stored tplot vector like Vp
27
    and stores tplot variables Vp_x, Vp_y, Vp_z
28

29
    Parameters
30
    ----------
31
        tvar : str
32
            Name of tplot variable to split up
33
        polar : bool, optional
34
            If True, the input data is in polar coordinates.
35
            Suffix will be set to ['_mag', '_th', '_phi'].
36
            Default: False
37
        new_name : int/list, optional (Deprecated)
38
            The names of the new tplot variables.
39
            This must be the same length as the number of variables created.
40
        newname : int/list, optional
41
            The names of the new tplot variables.
42
            This must be the same length as the number of variables created.
43
            Default: None
44
        columns : list of ints, optional
45
            The specific column numbers to grab from the data.
46
            Default: 'all' (splits all columns)
47
        suffix: str
48
            Suffix str to be added to end of tplot variable name
49
            Default: None
50

51
    Returns
52
    -------
53
    list[str]
54
        List of variables created
55

56
    Examples
57
    --------
58
        >>> pyspedas.store_data('b', data={'x':[2,5,8,11,14,17,20], 'y':[[1,1,1,1,1,1],[2,2,5,4,1,1],[100,100,3,50,1,1],[4,4,8,58,1,1],[5,5,9,21,1,1],[6,6,2,2,1,1],[7,7,1,6,1,1]]})
59
        >>> pyspedas.split_vec('b')
60

61

62

63
    """
64
    # new_name is deprecated in favor of newname
UNCOV
65
    if new_name is not None:
2✔
66
        logging.info("split_vec: The new_name parameter is deprecated. Please use newname instead.")
×
67
        newname = new_name
×
68

69
    # Make sure the tvar is found
UNCOV
70
    if tvar not in pyspedas.tplot_tools.data_quants:
2✔
71
        logging.error(f"Error: {tvar} not found in memory.")
×
72
        return None
×
73

74
    # Give a default to the new name
UNCOV
75
    if newname is None:
2✔
UNCOV
76
        newname = tvar
2✔
77

78
    # Gather data from the tvar
UNCOV
79
    alldata = get_data(tvar)
2✔
UNCOV
80
    time = alldata[0]
2✔
UNCOV
81
    data = alldata[1]
2✔
UNCOV
82
    dim = data.shape
2✔
UNCOV
83
    metadata = get_data(tvar, metadata=True)
2✔
84

UNCOV
85
    has_v = False
2✔
UNCOV
86
    combined_v = None
2✔
UNCOV
87
    time_varying_v = False
2✔
88

89
    # If already size one, simply return
UNCOV
90
    if len(dim) == 1:
2✔
UNCOV
91
        return [tvar]
1✔
92
    # Is there depend_1 data to split?
UNCOV
93
    elif len(dim) == 2:
2✔
UNCOV
94
        if len(alldata) == 3:
2✔
UNCOV
95
            has_v = True
1✔
UNCOV
96
            combined_v = alldata[2]
1✔
UNCOV
97
            if len(combined_v.shape) == 1:
1✔
UNCOV
98
                time_varying_v = False
1✔
99
            else:
UNCOV
100
                time_varying_v = True
1✔
101

102
    elif len(dim) > 2:
×
103
        logging.error(f"split_vec:data array for {tvar} has {len(dim)} dimensions, too many to split")
×
104
        return None
×
105

106

UNCOV
107
    vec_length = dim[1]
2✔
108

109
    # Determine what the suffix list will be
UNCOV
110
    if suffix is not None:
2✔
111
        if vec_length > len(suffix):
×
112
            logging.error(f"split_vec error: number of columns ({vec_length}) is greater than the number of suffix entered")
×
113
    else:
UNCOV
114
        if vec_length == 3:
2✔
UNCOV
115
            if polar:
2✔
116
                suffix = ['_mag', '_th', '_phi']
×
117
            else:
UNCOV
118
                suffix = ["_x", "_y", "_z"]
2✔
119
        else:
120
            suffix = []
×
121
            for i in range(vec_length):
×
122
                suffix.append("_"+str(i))
×
123

UNCOV
124
    created_variables = []
2✔
125

126
    # grab column data
UNCOV
127
    if columns == 'all':
2✔
UNCOV
128
        columns = range(vec_length)
2✔
129

UNCOV
130
    v_idx = 0
2✔
UNCOV
131
    for i in columns:
2✔
132
        # if not a list
UNCOV
133
        if isinstance(i, list):
2✔
134
            range_start = i[0]
×
135
            range_end = i[1]
×
136
        else:
UNCOV
137
            range_start = i
2✔
UNCOV
138
            range_end = i
2✔
UNCOV
139
        split_col = list(range(range_start, range_end+1))
2✔
UNCOV
140
        split_name = newname + suffix[i]
2✔
UNCOV
141
        created_variables = created_variables + [split_name]
2✔
142

UNCOV
143
        if has_v and time_varying_v:
2✔
UNCOV
144
            data_for_tplot = {'x': time, 'y': data[:, split_col].squeeze(), 'v': combined_v[:,v_idx]}
1✔
UNCOV
145
        elif has_v and not time_varying_v:
2✔
UNCOV
146
            data_for_tplot = {'x': time, 'y': data[:, split_col].squeeze(), 'v': np.array([combined_v[v_idx]])}
1✔
147
        else:
UNCOV
148
            data_for_tplot = {'x': time, 'y': data[:, split_col].squeeze()}
1✔
149

UNCOV
150
        plot_options = metadata.get('plot_options', {})
2✔
UNCOV
151
        yaxis_opt = plot_options.get('yaxis_opt', {})
2✔
UNCOV
152
        labels = yaxis_opt.get('legend_names', None)
2✔
UNCOV
153
        if labels is None:
2✔
UNCOV
154
            cdf = metadata.get('CDF', {})
1✔
UNCOV
155
            labels = cdf.get('LABELS', None)
1✔
156

UNCOV
157
        if labels is None:
2✔
UNCOV
158
            attrs = metadata
1✔
159
        else:
160
            # Make a deep copy to avoid modifying original metadata
UNCOV
161
            attrs = copy.deepcopy(metadata)
1✔
UNCOV
162
            if 'plot_options' not in attrs:
1✔
163
                attrs['plot_options'] = {}
×
UNCOV
164
            if 'yaxis_opt' not in attrs['plot_options']:
1✔
165
                attrs['plot_options']['yaxis_opt'] = {}
×
UNCOV
166
            attrs['plot_options']['yaxis_opt']['legend_names'] = []
1✔
167

UNCOV
168
            if v_idx < len(labels):
1✔
UNCOV
169
                attrs['plot_options']['yaxis_opt']['legend_names'].append(labels[v_idx])
1✔
170

UNCOV
171
        if not store_data(split_name, data=data_for_tplot, attr_dict=attrs):
2✔
172
            raise Exception(f"Failed to store {split_name} in pyspedas.")
×
UNCOV
173
        v_idx += 1
2✔
174

UNCOV
175
    return created_variables
2✔
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