-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathConvert_unitDimension_to_YTUnit.py
63 lines (51 loc) · 1.33 KB
/
Convert_unitDimension_to_YTUnit.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
"""
Programm: Convert_unitDimension_to_YTUnit
Author: Daniel Grassinger
License: Modified BSD License (also known as New or Revised BSD)
Date: 25.09.2015
Python: 2.7.6
Description: This little script converts a unitDimension array
from a openPMD Dataset into a unit string for yt
Usage: unitsToString(array with 7 float(unitDimension Attribut) )
"""
def unitsToString(units):
def getUnitString(num):
if(num==0):
return "m"
elif(num==1):
return "kg"
elif(num==2):
return "s"
elif(num==3):
return "A"
elif(num==4):
return "K"
elif(num==5):
return "mole"
elif(num==6):
return "cd"
divident = ""
divisor = ""
num = 0
for unit in units:
if(unit==0.0):
continue
elif(unit==1):
divident+=getUnitString(num) + " * "
elif(unit==-1):
divisor+=getUnitString(num) + " * "
elif(unit>0):
divident+=getUnitString(num) + "**" + str(unit) + " * "
else:
divisor+=getUnitString(num) + "**" + str(-1.0*unit) + " * "
num+=1
if(divisor==""):
return divident[:-3]
else:
return "( "+ divident[:-3] + " ) / ( " + divisor[:-3] + " )"
"""
example:
print unitsToString([-3., 0., 1., 1., 0., 0., 0.])
example output:
( kg * s ) / ( m**3.0 )
"""