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 __authors__ = [ "Jérôme Kieffer" ]
29 __contact__ = "jerome.kieffer@esrf.fr"
30 __license__ = "LGPLv3+"
31 __copyright__ = "European Synchrotron Radiation Facility, Grenoble, France"
32
33
34 import math
35 from EDVerbose import EDVerbose
36 from XSDataCommon import XSDataString
40 """
41 This is a static utility class for handling unit (meter ... and subunit) in XSData / XML data structures.
42 """
43
44 dictLength = {("m", "meter", None): 1.0,
45 ("cm", "centimeter"): 1.0e-2,
46 ("mm", "millimeter"): 1.0e-3,
47 ("um", "µm", "micron", "micrometer"):1.0e-6,
48 ("nm", "nanometer"):1.0e-9,
49 ("pm", "picometer"): 1.0e-12,
50 ("A", "Angstrom", "angstrom"):1.0e-10,
51 ("km", "kilometer"): 1.e3,
52 }
53
54 dictAngle = {("rad", "r", None):1.0,
55 ("deg", "°"): math.pi / 180.0,
56 ("grad", "g"): math.pi / 200.0,
57 }
58
59 dictTime = { ("s", "sec", "second", None):1.0,
60 ("ms", "millisec", "millisecond"):1.0e-3,
61 ("mn", "min", "minute"):60.0,
62 ("h", "hour", "heure"):3600.0
63 }
64
65 @staticmethod
88
89
90 @staticmethod
115
116
117 @staticmethod
118 - def toXSD(_classXSData, _strObject,):
119 """Convert a string or possibly any object to an XSD object
120
121 @param _classXSData: the XSDataClass to be forced into
122 @type _classXSData: class derived from XSData
123 @param _strObject: the string representation of a physical object like "1.54 A"
124 @type _strObject: string (floats are accepted)
125 """
126 xsd = _classXSData()
127 if isinstance(_strObject, (unicode, str)):
128 listWords = _strObject.split(None, 1)
129 if len(listWords) > 0:
130 try:
131 fValue = float(listWords[0])
132 except Exception:
133 EDVerbose.ERROR("Trying to create XSData object from %s; fValue not a float !" % _strObject)
134
135 else:
136 xsd.setValue(fValue)
137 if len(listWords) == 2:
138 xsd.setUnit(XSDataString(listWords[1]))
139 else:
140 EDVerbose.WARNING("Trying to create XSData object from %s " % _strObject)
141 elif isinstance(_strObject, (float, int)):
142 xsd.setValue(_strObject)
143 return xsd
144
145
146 @staticmethod
148 """
149 Retrieves the name of the class
150 @return: the name of the class
151 @rtype: string
152 """
153 return str(_object.__class__).replace("<class '", "").replace("'>", "").split(".")[-1]
154
155
156 @staticmethod
158 """
159 Return the fScale factor of an unit versus SI unit (meter)
160 @param _unit: the name of the unit
161 @return : the numeric fScale factor (0.001 for mm)
162 """
163 fScale = None
164 for oneUnit in EDUtilsUnit.dictLength:
165 if _unit in oneUnit:
166 fScale = EDUtilsUnit.dictLength[oneUnit]
167 break
168 if fScale is None:
169 EDVerbose.WARNING("Unrecognized length unit: %s" % _unit)
170 fScale = 1.0
171 return fScale
172
173
174 @staticmethod
176 """
177 Return the fScale factor of an unit versus SI unit (radian)
178 @param _unit: the name of the unit
179 @return : the numeric fScale factor (0.001 for mm)
180 """
181 fScale = None
182 for oneUnit in EDUtilsUnit.dictAngle:
183 if _unit in oneUnit:
184 fScale = EDUtilsUnit.dictAngle[oneUnit]
185 break
186 if fScale is None:
187 EDVerbose.WARNING("Unrecognized Angle unit: %s" % _unit)
188 fScale = 1.0
189 return fScale
190
191
192 @staticmethod
194 """
195 Return the fScale factor of an unit versus SI unit (seconds)
196 @param _unit: the name of the unit
197 @return : the numeric fScale factor (60 for mn)
198 """
199 fScale = None
200 for oneUnit in EDUtilsUnit.dictTime:
201 if _unit in oneUnit:
202 fScale = EDUtilsUnit.dictTime[oneUnit]
203 break
204 if fScale is None:
205 EDVerbose.WARNING("Unrecognized Time unit: %s" % _unit)
206 fScale = 1.0
207 return fScale
208
209
210 @staticmethod
212 """
213 Convert a length like fValue to meter (SI)
214 @param _value: the fValue in unit
215 @type _value: float
216 @param _unit: the name of the unit
217 @type _unit: string
218 """
219 return _value * EDUtilsUnit.getUnitScaleLength(_unit)
220
221
222 @staticmethod
224 """
225 Convert an angle like fValue to radian (SI)
226 @param _value: the fValue in unit
227 @type _value: float
228 @param _unit: the name of the unit
229 @type _unit: string
230 """
231 return _value * EDUtilsUnit.getUnitScaleAngle(_unit)
232
233
234 @staticmethod
236 """
237 Convert an time like fValue to seconds (SI)
238 @param _value: the fValue in unit
239 @type _value: float
240 @param _unit: the name of the unit
241 @type _unit: string
242 """
243 return _value * EDUtilsUnit.getUnitScaleTime(_unit)
244