-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathlut.py
148 lines (120 loc) · 3.69 KB
/
lut.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
"""
Module to help manage mapping between quantity names and quantity codes
"""
from __future__ import print_function
# if custom mapping exists, use it
try:
from lut_mapping_custom import code_given_name, name_given_code, tex_given_code
except ImportError:
from lut_mapping import code_given_name, name_given_code, tex_given_code
# include user defined shortcuts, if they exist
try:
from lut_shortcuts import shortcuts
except ImportError:
shortcuts = {}
def lookup(quantity, __check_shortcuts=True):
"""
Search the look up table for the quantity. If the quantity code was
provided, then return the name. If the name was given, return the code.
__check_shortcuts is an internally used keyword and should always be set to True
"""
code_given = False
name_given = False
try: # assume integer-like
code = int(quantity)
code_given = True
except:
name = quantity
name_given = True
if (code_given):
if (code in name_given_code.keys()): # code given, grab name
return name_given_code[code]
else:
return None
if (name_given):
if (__check_shortcuts): # check shortcuts first
ind = shortcut_lookup(name)
if (ind is not None): # found in shortcuts
return ind
name = name.lower()
if (name in code_given_name.keys()): # name give, grab code
return code_given_name[name]
else:
return None
return None
def shortcut_lookup(name):
"""
Search the user defined shortcuts for the given quantity name, return the code
"""
if (name not in shortcuts.keys()):
return None
qname = shortcuts[name]
ind = lookup(qname, __check_shortcuts=False)
return ind
def latex_formula(qcode):
"""
Return the LaTeX formula for the given quantity code
"""
if (qcode in tex_given_code.keys()):
return tex_given_code[qcode]
return None
def parse_quantity(quantity):
"""
Given a quantity, return the index and the name
Examples:
parse_quantity("v_r") returns (1, "v_r")
parse_quantity( 1 ) returns (1, "v_r")
parse_quantity( "1" ) returns (1, "v_r")
"""
try:
code = int(quantity)
name = lookup(code)
except:
name = quantity
code = lookup(name)
return code, name
def parse_quantities(quantities):
"""
Given a list of quantities, return the indices and the names
Examples:
parse_quantity(["v_r", 2, "3"]) returns [1,2,3], ["v_r", "v_theta", "v_phi"]
"""
codes = []; names = []
for q in quantities:
c, n = parse_quantity(q)
codes.append(c)
names.append(n)
return codes, names
def find_possible(search_string):
"""
Search the quantity names for the given string. Return possible codes & names
"""
codes = []; names = []
search_string = search_string.lower()
for c,n in name_given_code.items():
if (search_string in n):
codes.append(c)
names.append(n)
return codes, names
def quantity_available(quantity):
"""
Check if the quantity is available, returns True/False
"""
available = False
try:
code = int(quantity)
if (code in name_given_code.keys()):
available = True
except:
name = quantity.lower()
if (name in code_given_name.keys()):
available = True
return available
def quantities_available(quantities):
"""
Check if multiple quantities are available, returns list of True/False
"""
available = []
for q in quantities:
available.append(quantity_available(q))
return available