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

spedas / pyspedas / 17001196665

15 Aug 2025 11:12PM UTC coverage: 89.516% (+0.7%) from 88.849%
17001196665

push

github

web-flow
Merge branch 'pyspedas_2_0_dev' into master

5072 of 6199 new or added lines in 413 files covered. (81.82%)

8 existing lines in 2 files now uncovered.

40061 of 44753 relevant lines covered (89.52%)

0.9 hits per line

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

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

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

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

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

55
    Examples
56
    --------
57
        >>> 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]]})
58
        >>> pyspedas.split_vec('b')
59

60

61

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

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

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

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

84
    # If already size one, simply return
85
    if len(dim) == 1:
1✔
86
        return [tvar]
1✔
87

88
    vec_length = dim[1]
1✔
89

90
    # Determine what the suffix list will be
91
    if suffix is not None:
1✔
NEW
92
        if vec_length > len(suffix):
×
NEW
93
            logging.error(f"split_vec error: number of columns ({vec_length}) is greater than the number of suffix entered")
×
94
    else:
95
        if vec_length == 3:
1✔
96
            if polar:
1✔
NEW
97
                suffix = ['_mag', '_th', '_phi']
×
98
            else:
99
                suffix = ["_x", "_y", "_z"]
1✔
100
        else:
NEW
101
            suffix = []
×
NEW
102
            for i in range(vec_length):
×
NEW
103
                suffix.append("_"+str(i))
×
104

105
    created_variables = []
1✔
106

107
    # grab column data
108
    if columns == 'all':
1✔
109
        columns = range(vec_length)
1✔
110

111
    for i in columns:
1✔
112

113
        # if not a list
114
        if isinstance(i, list):
1✔
NEW
115
            range_start = i[0]
×
NEW
116
            range_end = i[1]
×
117
        else:
118
            range_start = i
1✔
119
            range_end = i
1✔
120
        split_col = list(range(range_start, range_end+1))
1✔
121
        split_name = newname + suffix[i]
1✔
122
        created_variables = created_variables + [split_name]
1✔
123

124
        data_for_tplot = {'x': time, 'y': data[:, split_col].squeeze()}
1✔
125

126
        if not store_data(split_name, data=data_for_tplot, attr_dict=metadata):
1✔
NEW
127
            raise Exception(f"Failed to store {split_name} in pyspedas.")
×
128

129
    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