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

spedas / pyspedas / 19581095745

21 Nov 2025 07:20PM UTC coverage: 90.132% (-0.07%) from 90.199%
19581095745

push

github

jameswilburlewis
Removed check for dens_e (and reversed order of tests), fixed typo vthtime->vth_time

0 of 3 new or added lines in 1 file covered. (0.0%)

89 existing lines in 4 files now uncovered.

41420 of 45955 relevant lines covered (90.13%)

0.9 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
1✔
11
import logging
1✔
12
import pyspedas
1✔
13
from pyspedas.tplot_tools import store_data, get_data
1✔
14
import numpy as np
1✔
15
import logging
1✔
16

17
def split_vec(
1✔
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
65
    if new_name is not None:
1✔
66
        logging.info("split_vec: The new_name parameter is deprecated. Please use newname instead.")
×
UNCOV
67
        newname = new_name
×
68

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

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

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

85
    has_v = False
1✔
86
    combined_v = None
1✔
87
    time_varying_v = False
1✔
88

89
    # If already size one, simply return
90
    if len(dim) == 1:
1✔
91
        return [tvar]
1✔
92
    # Is there depend_1 data to split?
93
    elif len(dim) == 2:
1✔
94
        if len(alldata) == 3:
1✔
95
            has_v = True
1✔
96
            combined_v = alldata[2]
1✔
97
            if len(combined_v.shape) == 1:
1✔
98
                time_varying_v = False
1✔
99
            else:
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")
×
UNCOV
104
        return None
×
105

106

107
    vec_length = dim[1]
1✔
108

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

124
    created_variables = []
1✔
125

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

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

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

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

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

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

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

175
    return created_variables
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