forked from nikosarcevic/CosmOracle
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconversion_functions.py
More file actions
51 lines (47 loc) · 1.58 KB
/
conversion_functions.py
File metadata and controls
51 lines (47 loc) · 1.58 KB
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
import numpy as np
def convert_unit(value, from_unit, to_unit):
"""
Converts quantity value in from_unit to value' in to_unit
"""
conversion_table = {
"meter" : {
"meter" : 1,
"parsec" : 1/(3.0857e16),
"kiloparsec" : 1/(3.0857e16 * 1e3),
"megaparsec" : 1/(3.0857e16 * 1e6),
"gigaparsec" : 1/(3.0857e16 * 1e6),
"lightyear" : 1/(9.4607e15)
},
"parsec" : {
"meter" : 3.0857e16,
"kilometer" : 3.0857e16 * 1e-3,
"parsec" : 1,
"lightyear" : 3.26156
},
"megaparsec" : {
"meter" : 3.0857e16 * 1e6,
"megaparsec": 1,
"lightyear" : 3.26156e6
},
"lightyear" : {
"meter" : 9.4607e15,
"parsec" : 1/3.26156,
"megaparsec" : 1/3.26156e6,
"lightyear" : 1
},
"arcsec" : {
"arcsec" : 1,
"radian" : 1/(180 / np.pi * 3600)
},
"radian" : {
"arcsec" : 180 / np.pi * 3600,
"radian" : 1
}
}
if not (isinstance(value, int) or isinstance(value, float)):
raise TypeError("Invalid numerical value.")
if not (isinstance(from_unit, str) and isinstance(to_unit, str)):
raise TypeError("Ensure that your units are valid strings.")
elif not all(unit in conversion_table.keys() for unit in [from_unit, to_unit]):
raise TypeError("Some units are unavailable.")
return value * conversion_table[from_unit][to_unit]