• 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

67.27
/pyspedas/tplot_tools/tplot_math/tkm2re.py
1
import logging
3✔
2
import pyspedas
3✔
3
from pyspedas.tplot_tools import store_data, get_data, tnames, get_units, set_units
3✔
4

5

6
def tkm2re(name, km=False, newname=None, suffix=''):
3✔
7
    """
8
    Converts a tplot variable to Re or Km
9

10
    Parameters
11
    -----------
12
        name: str or list of str
13
            Names of tplot variables to convert (wildcards accepted)
14

15
        km: bool, optional
16
            If True, convert Re to Km. If False, convert Km to Re.
17
            Default: False
18

19
        newname: str or list of str, optional
20
            Output variable names; if not set, the names of the
21
            input variables are used + '_re' or '_km'
22
            Default: None
23

24
        suffix: str, optional
25
            Specify to append a suffix to each variable 
26
            (only valid if newname is not specified)
27
            Default: ''
28

29
    Returns
30
    --------
31
        List of the tplot variables created
32

33
    Examples
34
    --------
35
        >>> import pyspedas
36
        >>> pyspedas.store_data('a', data={'x':[0,4,8,12,16], 'y':[1,2,3,4,5]})
37
        >>> pyspedas.tkm2re('a', km = True)
38

39

40
    """
UNCOV
41
    km_in_re = 6371.2
2✔
42

UNCOV
43
    names = tnames(name)
2✔
44

UNCOV
45
    if names == []:
2✔
UNCOV
46
        logging.error('tkm2re: No valid tplot variables found: ' + name)
×
UNCOV
47
        return
×
48

UNCOV
49
    if newname is None:
2✔
UNCOV
50
        newname = [n + suffix for n in names]
2✔
51

UNCOV
52
        if km == False:
2✔
UNCOV
53
            newname = [n + '_re' for n in newname]
2✔
54
        else:
UNCOV
55
            newname = [n + '_km' for n in newname]
×
56
    else:
UNCOV
57
        if not isinstance(newname, list):
2✔
UNCOV
58
            newname = [newname]
2✔
59

UNCOV
60
        if len(newname) != len(names):
2✔
UNCOV
61
            logging.error('Number of output variable names (newname) should match the number of input variables.')
×
UNCOV
62
            return
×
63

UNCOV
64
    out = []
2✔
65

UNCOV
66
    for in_tvar, out_tvar in zip(names, newname):
2✔
UNCOV
67
        data = get_data(in_tvar)
2✔
UNCOV
68
        metadata = get_data(in_tvar, metadata=True)
2✔
69

UNCOV
70
        if data is None:
2✔
71
            logging.error('Problem reading variable: ' + in_tvar)
×
72
            continue
×
UNCOV
73
        data_in_units = get_units(in_tvar)
2✔
UNCOV
74
        if data_in_units is None:
2✔
UNCOV
75
            data_in_units = 'None'
×
UNCOV
76
        if km == False:
2✔
UNCOV
77
            new_units = 'Re'
2✔
UNCOV
78
            if 're' in data_in_units.lower():
2✔
UNCOV
79
                logging.warning(f'Tplot variable {in_tvar} is already in units of Re!')
×
UNCOV
80
                data_out = data.y
×
81
            else:
UNCOV
82
                data_out = data.y/km_in_re
2✔
83
        else:
UNCOV
84
            new_units = 'km'
×
UNCOV
85
            if 'km' in data_in_units.lower():
×
UNCOV
86
                logging.warning(f'Tplot variable {in_tvar} is already in units of km!')
×
UNCOV
87
                data_out = data.y
×
88
            else:
UNCOV
89
                data_out = data.y*km_in_re
×
90

UNCOV
91
        saved = store_data(out_tvar, data={'x': data.times, 'y': data_out}, attr_dict=metadata)
2✔
92

UNCOV
93
        if not saved:
2✔
94
            logging.error('Problem creating tplot variable.')
×
95
            continue
×
96
        else:
UNCOV
97
            set_units(out_tvar, new_units)
2✔
98

99
        # update the subtitle, if needed
UNCOV
100
        yaxis_opt = pyspedas.tplot_tools.data_quants[out_tvar].attrs['plot_options'].get('yaxis_opt')
2✔
101

UNCOV
102
        if yaxis_opt is not None:
2✔
UNCOV
103
            subtitle = yaxis_opt.get('axis_subtitle')
2✔
UNCOV
104
            if subtitle is not None:
2✔
UNCOV
105
                if km == False:
2✔
UNCOV
106
                    new_subtitle = pyspedas.tplot_tools.data_quants[out_tvar].attrs['plot_options']['yaxis_opt']['axis_subtitle'].lower().replace('km', 'Re')
2✔
107
                else:
UNCOV
108
                    new_subtitle = pyspedas.tplot_tools.data_quants[out_tvar].attrs['plot_options']['yaxis_opt']['axis_subtitle'].lower().replace('re', 'km')
×
UNCOV
109
                pyspedas.tplot_tools.data_quants[out_tvar].attrs['plot_options']['yaxis_opt']['axis_subtitle'] = new_subtitle
2✔
110

UNCOV
111
        out.append(out_tvar)
2✔
112

UNCOV
113
    return out
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