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

spedas / pyspedas / 25391193746

05 May 2026 05:15PM UTC coverage: 76.027% (+19.6%) from 56.446%
25391193746

push

github

jameswilburlewis
Restore Cluster tests -- CSA seems to be back online

37244 of 48988 relevant lines covered (76.03%)

2.41 hits per line

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

100.0
/pyspedas/tplot_tools/tplot_math/tnormalize.py
1
import numpy as np
4✔
2
from pyspedas.tplot_tools import get_data, store_data
4✔
3

4

5
def tnormalize(variable, newname=None, return_data=False):
4✔
6
    """
7
    Normalize all the vectors stored in a tplot variable
8

9
    Parameters
10
    ----------
11
        variable: str or np.ndarray
12
            tplot variable (or numpy array) containing the vectors to be normalized
13

14
        newname: str, optional
15
            name of the output variable; If not set, output name will be variable + '_normalize'
16
            Default: None
17

18
        return_data: bool, optional
19
            If true, return the normalized vectors instead of creating a tplot variable
20
            Default: False
21
    
22
    Returns
23
    -------
24
        str or ndarray of float
25
            name of the tplot variable created or normalized vectors if return_data is set
26
    
27
    Examples
28
    ---------
29
        >>> import pyspedas
30
        >>> pyspedas.store_data('a', data={'x':[1, 2, 3], 'y':[[1,2,3],[4,5,6],[7,8,9]]})
31
        >>> pyspedas.tnormalize('a')
32

33

34
    """
35
    metadata_in = {}
4✔
36
    if isinstance(variable, str):
4✔
37
        data_in = get_data(variable)
4✔
38
        metadata_in = get_data(variable, metadata=True)
4✔
39
        data = data_in[1]
4✔
40
        times = data_in[0]
4✔
41
    else:
42
        data = np.atleast_2d(variable)
4✔
43
        times = np.zeros(data.shape[0])
4✔
44

45
    n = np.sqrt(np.nansum(data**2, axis=1))
4✔
46

47
    # to do element-wise division, the magnitude needs to be repeated for each component
48
    norm_reshaped = np.reshape(n, [len(times), 1])
4✔
49
    norm_mag = np.repeat(norm_reshaped, len(data[0, :]), axis=1)
4✔
50

51
    data_norm = data/norm_mag
4✔
52

53
    if return_data:
4✔
54
        return data_norm
4✔
55
    else:
56
        if newname is None:
4✔
57
            newname = variable + '_normalized'
3✔
58
        store_data(newname, data={'x': times, 'y': data_norm}, attr_dict=metadata_in)
4✔
59
        return newname
4✔
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