1 | |
---|
2 | |
---|
3 | ############################################################################ |
---|
4 | #This software was developed by the University of Tennessee as part of the |
---|
5 | #Distributed Data Analysis of Neutron Scattering Experiments (DANSE) |
---|
6 | #project funded by the US National Science Foundation. |
---|
7 | #If you use DANSE applications to do scientific research that leads to |
---|
8 | #publication, we ask that you acknowledge the use of the software with the |
---|
9 | #following sentence: |
---|
10 | #This work benefited from DANSE software developed under NSF award DMR-0520547. |
---|
11 | #copyright 2008,2009 University of Tennessee |
---|
12 | ############################################################################# |
---|
13 | |
---|
14 | # Known issue: reader not compatible with multiple SASdata entries |
---|
15 | # within a single SASentry. Will raise a runtime error. |
---|
16 | |
---|
17 | #TODO: check that all vectors are written only if they have at |
---|
18 | # least one non-empty value |
---|
19 | #TODO: Writing only allows one SASentry per file. |
---|
20 | # Would be best to allow multiple entries. |
---|
21 | #TODO: Store error list |
---|
22 | #TODO: Allow for additional meta data for each section |
---|
23 | #TODO: Notes need to be implemented. They can be any XML |
---|
24 | # structure in version 1.0 |
---|
25 | # Process notes have the same problem. |
---|
26 | #TODO: Unit conversion is not complete (temperature units are missing) |
---|
27 | |
---|
28 | |
---|
29 | import logging |
---|
30 | import numpy |
---|
31 | import os |
---|
32 | import sys |
---|
33 | from sans.dataloader.data_info import Data1D |
---|
34 | from sans.dataloader.data_info import Collimation |
---|
35 | from sans.dataloader.data_info import Detector |
---|
36 | from sans.dataloader.data_info import Process |
---|
37 | from sans.dataloader.data_info import Aperture |
---|
38 | from lxml import etree |
---|
39 | import xml.dom.minidom |
---|
40 | _ZERO = 1e-16 |
---|
41 | HAS_CONVERTER = True |
---|
42 | try: |
---|
43 | from data_util.nxsunit import Converter |
---|
44 | except: |
---|
45 | HAS_CONVERTER = False |
---|
46 | |
---|
47 | CANSAS_NS = "cansas1d/1.0" |
---|
48 | ALLOW_ALL = True |
---|
49 | |
---|
50 | def write_node(doc, parent, name, value, attr={}): |
---|
51 | """ |
---|
52 | :param doc: document DOM |
---|
53 | :param parent: parent node |
---|
54 | :param name: tag of the element |
---|
55 | :param value: value of the child text node |
---|
56 | :param attr: attribute dictionary |
---|
57 | |
---|
58 | :return: True if something was appended, otherwise False |
---|
59 | """ |
---|
60 | if value is not None: |
---|
61 | node = doc.createElement(name) |
---|
62 | node.appendChild(doc.createTextNode(str(value))) |
---|
63 | for item in attr: |
---|
64 | node.setAttribute(item, attr[item]) |
---|
65 | parent.appendChild(node) |
---|
66 | return True |
---|
67 | return False |
---|
68 | |
---|
69 | def get_content(location, node): |
---|
70 | """ |
---|
71 | Get the first instance of the content of a xpath location. |
---|
72 | |
---|
73 | :param location: xpath location |
---|
74 | :param node: node to start at |
---|
75 | |
---|
76 | :return: Element, or None |
---|
77 | """ |
---|
78 | nodes = node.xpath(location, namespaces={'ns': CANSAS_NS}) |
---|
79 | |
---|
80 | if len(nodes)>0: |
---|
81 | return nodes[0] |
---|
82 | else: |
---|
83 | return None |
---|
84 | |
---|
85 | def get_float(location, node): |
---|
86 | """ |
---|
87 | Get the content of a node as a float |
---|
88 | |
---|
89 | :param location: xpath location |
---|
90 | :param node: node to start at |
---|
91 | """ |
---|
92 | nodes = node.xpath(location, namespaces={'ns': CANSAS_NS}) |
---|
93 | |
---|
94 | value = None |
---|
95 | attr = {} |
---|
96 | if len(nodes) > 0: |
---|
97 | try: |
---|
98 | value = float(nodes[0].text) |
---|
99 | except: |
---|
100 | # Could not pass, skip and return None |
---|
101 | msg = "cansas_reader.get_float: could not " |
---|
102 | msg += " convert '%s' to float" % nodes[0].text |
---|
103 | logging.error(msg) |
---|
104 | if nodes[0].get('unit') is not None: |
---|
105 | attr['unit'] = nodes[0].get('unit') |
---|
106 | return value, attr |
---|
107 | |
---|
108 | |
---|
109 | class Reader: |
---|
110 | """ |
---|
111 | Class to load cansas 1D XML files |
---|
112 | |
---|
113 | :Dependencies: |
---|
114 | The CanSas reader requires PyXML 0.8.4 or later. |
---|
115 | """ |
---|
116 | ## CanSAS version |
---|
117 | version = '1.0' |
---|
118 | ## File type |
---|
119 | type_name = "CanSAS 1D" |
---|
120 | ## Wildcards |
---|
121 | type = ["CanSAS 1D files (*.xml)|*.xml", |
---|
122 | "CanSAS 1D AVE files (*.AVEx)|*.AVEx", |
---|
123 | "CanSAS 1D AVE files (*.ABSx)|*.ABSx"] |
---|
124 | |
---|
125 | ## List of allowed extensions |
---|
126 | ext = ['.xml', '.XML', '.avex', '.AVEx', '.absx', 'ABSx'] |
---|
127 | |
---|
128 | def __init__(self): |
---|
129 | ## List of errors |
---|
130 | self.errors = [] |
---|
131 | |
---|
132 | def read(self, path): |
---|
133 | """ |
---|
134 | Load data file |
---|
135 | |
---|
136 | :param path: file path |
---|
137 | |
---|
138 | :return: Data1D object if a single SASentry was found, |
---|
139 | or a list of Data1D objects if multiple entries were found, |
---|
140 | or None of nothing was found |
---|
141 | |
---|
142 | :raise RuntimeError: when the file can't be opened |
---|
143 | :raise ValueError: when the length of the data vectors are inconsistent |
---|
144 | """ |
---|
145 | output = [] |
---|
146 | if os.path.isfile(path): |
---|
147 | basename = os.path.basename(path) |
---|
148 | root, extension = os.path.splitext(basename) |
---|
149 | if ALLOW_ALL or extension.lower() in self.ext: |
---|
150 | try: |
---|
151 | tree = etree.parse(path, parser=etree.ETCompatXMLParser()) |
---|
152 | # Check the format version number |
---|
153 | # Specifying the namespace will take care of the file |
---|
154 | # format version |
---|
155 | root = tree.getroot() |
---|
156 | |
---|
157 | entry_list = root.xpath('/ns:SASroot/ns:SASentry', |
---|
158 | namespaces={'ns': CANSAS_NS}) |
---|
159 | |
---|
160 | for entry in entry_list: |
---|
161 | self.errors = [] |
---|
162 | sas_entry = self._parse_entry(entry) |
---|
163 | sas_entry.filename = basename |
---|
164 | |
---|
165 | # Store loading process information |
---|
166 | sas_entry.errors = self.errors |
---|
167 | sas_entry.meta_data['loader'] = self.type_name |
---|
168 | output.append(sas_entry) |
---|
169 | except: |
---|
170 | raise RuntimeError, "%s cannot be read \n" % path |
---|
171 | else: |
---|
172 | raise RuntimeError, "%s is not a file" % path |
---|
173 | # Return output consistent with the loader's api |
---|
174 | if len(output) == 0: |
---|
175 | #cannot return none when it cannot read |
---|
176 | #return None |
---|
177 | raise RuntimeError, "%s cannot be read \n" % path |
---|
178 | elif len(output) == 1: |
---|
179 | return output[0] |
---|
180 | else: |
---|
181 | return output |
---|
182 | |
---|
183 | def _parse_entry(self, dom): |
---|
184 | """ |
---|
185 | Parse a SASentry |
---|
186 | |
---|
187 | :param node: SASentry node |
---|
188 | |
---|
189 | :return: Data1D object |
---|
190 | """ |
---|
191 | x = numpy.zeros(0) |
---|
192 | y = numpy.zeros(0) |
---|
193 | |
---|
194 | data_info = Data1D(x, y) |
---|
195 | |
---|
196 | # Look up title |
---|
197 | self._store_content('ns:Title', dom, 'title', data_info) |
---|
198 | |
---|
199 | # Look up run number |
---|
200 | nodes = dom.xpath('ns:Run', namespaces={'ns': CANSAS_NS}) |
---|
201 | for item in nodes: |
---|
202 | if item.text is not None: |
---|
203 | value = item.text.strip() |
---|
204 | if len(value) > 0: |
---|
205 | data_info.run.append(value) |
---|
206 | if item.get('name') is not None: |
---|
207 | data_info.run_name[value] = item.get('name') |
---|
208 | |
---|
209 | # Look up instrument name |
---|
210 | self._store_content('ns:SASinstrument/ns:name', dom, 'instrument', |
---|
211 | data_info) |
---|
212 | |
---|
213 | # Notes |
---|
214 | note_list = dom.xpath('ns:SASnote', namespaces={'ns': CANSAS_NS}) |
---|
215 | for note in note_list: |
---|
216 | try: |
---|
217 | if note.text is not None: |
---|
218 | note_value = note.text.strip() |
---|
219 | if len(note_value) > 0: |
---|
220 | data_info.notes.append(note_value) |
---|
221 | except: |
---|
222 | err_mess = "cansas_reader.read: error processing" |
---|
223 | err_mess += " entry notes\n %s" % sys.exc_value |
---|
224 | self.errors.append(err_mess) |
---|
225 | logging.error(err_mess) |
---|
226 | |
---|
227 | # Sample info ################### |
---|
228 | entry = get_content('ns:SASsample', dom) |
---|
229 | if entry is not None: |
---|
230 | data_info.sample.name = entry.get('name') |
---|
231 | |
---|
232 | self._store_content('ns:SASsample/ns:ID', |
---|
233 | dom, 'ID', data_info.sample) |
---|
234 | self._store_float('ns:SASsample/ns:thickness', |
---|
235 | dom, 'thickness', data_info.sample) |
---|
236 | self._store_float('ns:SASsample/ns:transmission', |
---|
237 | dom, 'transmission', data_info.sample) |
---|
238 | self._store_float('ns:SASsample/ns:temperature', |
---|
239 | dom, 'temperature', data_info.sample) |
---|
240 | |
---|
241 | nodes = dom.xpath('ns:SASsample/ns:details', |
---|
242 | namespaces={'ns': CANSAS_NS}) |
---|
243 | for item in nodes: |
---|
244 | try: |
---|
245 | if item.text is not None: |
---|
246 | detail_value = item.text.strip() |
---|
247 | if len(detail_value) > 0: |
---|
248 | data_info.sample.details.append(detail_value) |
---|
249 | except: |
---|
250 | err_mess = "cansas_reader.read: error processing " |
---|
251 | err_mess += " sample details\n %s" % sys.exc_value |
---|
252 | self.errors.append(err_mess) |
---|
253 | logging.error(err_mess) |
---|
254 | |
---|
255 | # Position (as a vector) |
---|
256 | self._store_float('ns:SASsample/ns:position/ns:x', |
---|
257 | dom, 'position.x', data_info.sample) |
---|
258 | self._store_float('ns:SASsample/ns:position/ns:y', |
---|
259 | dom, 'position.y', data_info.sample) |
---|
260 | self._store_float('ns:SASsample/ns:position/ns:z', |
---|
261 | dom, 'position.z', data_info.sample) |
---|
262 | |
---|
263 | # Orientation (as a vector) |
---|
264 | self._store_float('ns:SASsample/ns:orientation/ns:roll', |
---|
265 | dom, 'orientation.x', data_info.sample) |
---|
266 | self._store_float('ns:SASsample/ns:orientation/ns:pitch', |
---|
267 | dom, 'orientation.y', data_info.sample) |
---|
268 | self._store_float('ns:SASsample/ns:orientation/ns:yaw', |
---|
269 | dom, 'orientation.z', data_info.sample) |
---|
270 | |
---|
271 | # Source info ################### |
---|
272 | entry = get_content('ns:SASinstrument/ns:SASsource', dom) |
---|
273 | if entry is not None: |
---|
274 | data_info.source.name = entry.get('name') |
---|
275 | |
---|
276 | self._store_content('ns:SASinstrument/ns:SASsource/ns:radiation', |
---|
277 | dom, 'radiation', data_info.source) |
---|
278 | self._store_content('ns:SASinstrument/ns:SASsource/ns:beam_shape', |
---|
279 | dom, 'beam_shape', data_info.source) |
---|
280 | self._store_float('ns:SASinstrument/ns:SASsource/ns:wavelength', |
---|
281 | dom, 'wavelength', data_info.source) |
---|
282 | self._store_float('ns:SASinstrument/ns:SASsource/ns:wavelength_min', |
---|
283 | dom, 'wavelength_min', data_info.source) |
---|
284 | self._store_float('ns:SASinstrument/ns:SASsource/ns:wavelength_max', |
---|
285 | dom, 'wavelength_max', data_info.source) |
---|
286 | self._store_float('ns:SASinstrument/ns:SASsource/ns:wavelength_spread', |
---|
287 | dom, 'wavelength_spread', data_info.source) |
---|
288 | |
---|
289 | # Beam size (as a vector) |
---|
290 | entry = get_content('ns:SASinstrument/ns:SASsource/ns:beam_size', dom) |
---|
291 | if entry is not None: |
---|
292 | data_info.source.beam_size_name = entry.get('name') |
---|
293 | |
---|
294 | self._store_float('ns:SASinstrument/ns:SASsource/ns:beam_size/ns:x', |
---|
295 | dom, 'beam_size.x', data_info.source) |
---|
296 | self._store_float('ns:SASinstrument/ns:SASsource/ns:beam_size/ns:y', |
---|
297 | dom, 'beam_size.y', data_info.source) |
---|
298 | self._store_float('ns:SASinstrument/ns:SASsource/ns:beam_size/ns:z', |
---|
299 | dom, 'beam_size.z', data_info.source) |
---|
300 | |
---|
301 | # Collimation info ################### |
---|
302 | nodes = dom.xpath('ns:SASinstrument/ns:SAScollimation', |
---|
303 | namespaces={'ns': CANSAS_NS}) |
---|
304 | for item in nodes: |
---|
305 | collim = Collimation() |
---|
306 | if item.get('name') is not None: |
---|
307 | collim.name = item.get('name') |
---|
308 | self._store_float('ns:length', item, 'length', collim) |
---|
309 | |
---|
310 | # Look for apertures |
---|
311 | apert_list = item.xpath('ns:aperture', namespaces={'ns': CANSAS_NS}) |
---|
312 | for apert in apert_list: |
---|
313 | aperture = Aperture() |
---|
314 | |
---|
315 | # Get the name and type of the aperture |
---|
316 | aperture.name = apert.get('name') |
---|
317 | aperture.type = apert.get('type') |
---|
318 | |
---|
319 | self._store_float('ns:distance', apert, 'distance', aperture) |
---|
320 | |
---|
321 | entry = get_content('ns:size', apert) |
---|
322 | if entry is not None: |
---|
323 | aperture.size_name = entry.get('name') |
---|
324 | |
---|
325 | self._store_float('ns:size/ns:x', apert, 'size.x', aperture) |
---|
326 | self._store_float('ns:size/ns:y', apert, 'size.y', aperture) |
---|
327 | self._store_float('ns:size/ns:z', apert, 'size.z', aperture) |
---|
328 | |
---|
329 | collim.aperture.append(aperture) |
---|
330 | |
---|
331 | data_info.collimation.append(collim) |
---|
332 | |
---|
333 | # Detector info ###################### |
---|
334 | nodes = dom.xpath('ns:SASinstrument/ns:SASdetector', |
---|
335 | namespaces={'ns': CANSAS_NS}) |
---|
336 | for item in nodes: |
---|
337 | |
---|
338 | detector = Detector() |
---|
339 | |
---|
340 | self._store_content('ns:name', item, 'name', detector) |
---|
341 | self._store_float('ns:SDD', item, 'distance', detector) |
---|
342 | |
---|
343 | # Detector offset (as a vector) |
---|
344 | self._store_float('ns:offset/ns:x', item, 'offset.x', detector) |
---|
345 | self._store_float('ns:offset/ns:y', item, 'offset.y', detector) |
---|
346 | self._store_float('ns:offset/ns:z', item, 'offset.z', detector) |
---|
347 | |
---|
348 | # Detector orientation (as a vector) |
---|
349 | self._store_float('ns:orientation/ns:roll', item, 'orientation.x', |
---|
350 | detector) |
---|
351 | self._store_float('ns:orientation/ns:pitch', item, 'orientation.y', |
---|
352 | detector) |
---|
353 | self._store_float('ns:orientation/ns:yaw', item, 'orientation.z', |
---|
354 | detector) |
---|
355 | |
---|
356 | # Beam center (as a vector) |
---|
357 | self._store_float('ns:beam_center/ns:x', item, 'beam_center.x', |
---|
358 | detector) |
---|
359 | self._store_float('ns:beam_center/ns:y', item, 'beam_center.y', |
---|
360 | detector) |
---|
361 | self._store_float('ns:beam_center/ns:z', item, 'beam_center.z', |
---|
362 | detector) |
---|
363 | |
---|
364 | # Pixel size (as a vector) |
---|
365 | self._store_float('ns:pixel_size/ns:x', item, 'pixel_size.x', |
---|
366 | detector) |
---|
367 | self._store_float('ns:pixel_size/ns:y', item, 'pixel_size.y', |
---|
368 | detector) |
---|
369 | self._store_float('ns:pixel_size/ns:z', item, 'pixel_size.z', |
---|
370 | detector) |
---|
371 | |
---|
372 | self._store_float('ns:slit_length', item, 'slit_length', detector) |
---|
373 | |
---|
374 | data_info.detector.append(detector) |
---|
375 | |
---|
376 | # Processes info ###################### |
---|
377 | nodes = dom.xpath('ns:SASprocess', namespaces={'ns': CANSAS_NS}) |
---|
378 | for item in nodes: |
---|
379 | process = Process() |
---|
380 | self._store_content('ns:name', item, 'name', process) |
---|
381 | self._store_content('ns:date', item, 'date', process) |
---|
382 | self._store_content('ns:description', item, 'description', process) |
---|
383 | |
---|
384 | term_list = item.xpath('ns:term', namespaces={'ns': CANSAS_NS}) |
---|
385 | for term in term_list: |
---|
386 | try: |
---|
387 | term_attr = {} |
---|
388 | for attr in term.keys(): |
---|
389 | term_attr[attr] = term.get(attr).strip() |
---|
390 | if term.text is not None: |
---|
391 | term_attr['value'] = term.text.strip() |
---|
392 | process.term.append(term_attr) |
---|
393 | except: |
---|
394 | err_mess = "cansas_reader.read: error processing " |
---|
395 | err_mess += " process term\n %s" % sys.exc_value |
---|
396 | self.errors.append(err_mess) |
---|
397 | logging.error(err_mess) |
---|
398 | |
---|
399 | note_list = item.xpath('ns:SASprocessnote', |
---|
400 | namespaces={'ns': CANSAS_NS}) |
---|
401 | for note in note_list: |
---|
402 | if note.text is not None: |
---|
403 | process.notes.append(note.text.strip()) |
---|
404 | |
---|
405 | data_info.process.append(process) |
---|
406 | |
---|
407 | |
---|
408 | # Data info ###################### |
---|
409 | nodes = dom.xpath('ns:SASdata', namespaces={'ns': CANSAS_NS}) |
---|
410 | if len(nodes) > 1: |
---|
411 | msg = "CanSAS reader is not compatible with multiple" |
---|
412 | msg += " SASdata entries" |
---|
413 | raise RuntimeError, msg |
---|
414 | |
---|
415 | nodes = dom.xpath('ns:SASdata/ns:Idata', namespaces={'ns': CANSAS_NS}) |
---|
416 | |
---|
417 | x = numpy.zeros(0) |
---|
418 | y = numpy.zeros(0) |
---|
419 | dx = numpy.zeros(0) |
---|
420 | dy = numpy.zeros(0) |
---|
421 | dxw = numpy.zeros(0) |
---|
422 | dxl = numpy.zeros(0) |
---|
423 | |
---|
424 | for item in nodes: |
---|
425 | _x, attr = get_float('ns:Q', item) |
---|
426 | _dx, attr_d = get_float('ns:Qdev', item) |
---|
427 | _dxl, attr_l = get_float('ns:dQl', item) |
---|
428 | _dxw, attr_w = get_float('ns:dQw', item) |
---|
429 | if _dx == None: |
---|
430 | _dx = 0.0 |
---|
431 | if _dxl == None: |
---|
432 | _dxl = 0.0 |
---|
433 | if _dxw == None: |
---|
434 | _dxw = 0.0 |
---|
435 | |
---|
436 | if attr.has_key('unit') and \ |
---|
437 | attr['unit'].lower() != data_info.x_unit.lower(): |
---|
438 | if HAS_CONVERTER == True: |
---|
439 | try: |
---|
440 | data_conv_q = Converter(attr['unit']) |
---|
441 | _x = data_conv_q(_x, units=data_info.x_unit) |
---|
442 | except: |
---|
443 | msg = "CanSAS reader: could not convert " |
---|
444 | msg += "Q unit [%s]; " % attr['unit'], |
---|
445 | msg += "expecting [%s]\n %s" % (data_info.x_unit, |
---|
446 | sys.exc_value) |
---|
447 | raise ValueError, msg |
---|
448 | |
---|
449 | else: |
---|
450 | msg = "CanSAS reader: unrecognized Q unit [%s]; "\ |
---|
451 | % attr['unit'] |
---|
452 | msg += "expecting [%s]" % data_info.x_unit |
---|
453 | raise ValueError, msg |
---|
454 | |
---|
455 | # Error in Q |
---|
456 | if attr_d.has_key('unit') and \ |
---|
457 | attr_d['unit'].lower() != data_info.x_unit.lower(): |
---|
458 | if HAS_CONVERTER == True: |
---|
459 | try: |
---|
460 | data_conv_q = Converter(attr_d['unit']) |
---|
461 | _dx = data_conv_q(_dx, units=data_info.x_unit) |
---|
462 | except: |
---|
463 | msg = "CanSAS reader: could not convert dQ unit [%s]; "\ |
---|
464 | % attr['unit'] |
---|
465 | msg += " expecting " |
---|
466 | msg += "[%s]\n %s" % (data_info.x_unit, sys.exc_value) |
---|
467 | raise ValueError, msg |
---|
468 | |
---|
469 | else: |
---|
470 | msg = "CanSAS reader: unrecognized dQ unit [%s]; "\ |
---|
471 | % attr['unit'] |
---|
472 | msg += "expecting [%s]" % data_info.x_unit |
---|
473 | raise ValueError, msg |
---|
474 | |
---|
475 | # Slit length |
---|
476 | if attr_l.has_key('unit') and \ |
---|
477 | attr_l['unit'].lower() != data_info.x_unit.lower(): |
---|
478 | if HAS_CONVERTER == True: |
---|
479 | try: |
---|
480 | data_conv_q = Converter(attr_l['unit']) |
---|
481 | _dxl = data_conv_q(_dxl, units=data_info.x_unit) |
---|
482 | except: |
---|
483 | msg = "CanSAS reader: could not convert dQl unit [%s];"\ |
---|
484 | % attr['unit'] |
---|
485 | msg += " expecting [%s]\n %s" % (data_info.x_unit, |
---|
486 | sys.exc_value) |
---|
487 | raise ValueError, msg |
---|
488 | |
---|
489 | else: |
---|
490 | msg = "CanSAS reader: unrecognized dQl unit [%s];"\ |
---|
491 | % attr['unit'] |
---|
492 | msg += " expecting [%s]" % data_info.x_unit |
---|
493 | raise ValueError, msg |
---|
494 | |
---|
495 | # Slit width |
---|
496 | if attr_w.has_key('unit') and \ |
---|
497 | attr_w['unit'].lower() != data_info.x_unit.lower(): |
---|
498 | if HAS_CONVERTER == True: |
---|
499 | try: |
---|
500 | data_conv_q = Converter(attr_w['unit']) |
---|
501 | _dxw = data_conv_q(_dxw, units=data_info.x_unit) |
---|
502 | except: |
---|
503 | msg = "CanSAS reader: could not convert dQw unit [%s];"\ |
---|
504 | % attr['unit'] |
---|
505 | msg += " expecting [%s]\n %s" % (data_info.x_unit, |
---|
506 | sys.exc_value) |
---|
507 | raise ValueError, msg |
---|
508 | |
---|
509 | else: |
---|
510 | msg = "CanSAS reader: unrecognized dQw unit [%s];"\ |
---|
511 | % attr['unit'] |
---|
512 | msg += " expecting [%s]" % data_info.x_unit |
---|
513 | raise ValueError, msg |
---|
514 | _y, attr = get_float('ns:I', item) |
---|
515 | _dy, attr_d = get_float('ns:Idev', item) |
---|
516 | if _dy == None: |
---|
517 | _dy = 0.0 |
---|
518 | if attr.has_key('unit') and \ |
---|
519 | attr['unit'].lower() != data_info.y_unit.lower(): |
---|
520 | if HAS_CONVERTER == True: |
---|
521 | try: |
---|
522 | data_conv_i = Converter(attr['unit']) |
---|
523 | _y = data_conv_i(_y, units=data_info.y_unit) |
---|
524 | except: |
---|
525 | if attr['unit'].lower() == 'count': |
---|
526 | pass |
---|
527 | else: |
---|
528 | msg = "CanSAS reader: could not" |
---|
529 | msg += " convert I(q) unit [%s];" % str(attr['unit']) |
---|
530 | msg += " expecting [%s]\n" % str(data_info.y_unit) |
---|
531 | msg += " %s" % str(sys.exc_value) |
---|
532 | raise ValueError, msg |
---|
533 | else: |
---|
534 | msg = "CanSAS reader: unrecognized I(q) unit [%s];"\ |
---|
535 | % attr['unit'] |
---|
536 | msg += " expecting [%s]" % data_info.y_unit |
---|
537 | raise ValueError, msg |
---|
538 | |
---|
539 | if attr_d.has_key('unit') and \ |
---|
540 | attr_d['unit'].lower() != data_info.y_unit.lower(): |
---|
541 | if HAS_CONVERTER == True: |
---|
542 | try: |
---|
543 | data_conv_i = Converter(attr_d['unit']) |
---|
544 | _dy = data_conv_i(_dy, units=data_info.y_unit) |
---|
545 | except: |
---|
546 | if attr_d['unit'].lower() == 'count': |
---|
547 | pass |
---|
548 | else: |
---|
549 | msg = "CanSAS reader: could not convert dI(q) unit " |
---|
550 | msg += "[%s]; expecting [%s]\n %s" % (attr_d['unit'], |
---|
551 | data_info.y_unit, sys.exc_value) |
---|
552 | raise ValueError, msg |
---|
553 | else: |
---|
554 | msg = "CanSAS reader: unrecognized dI(q) unit [%s]; "\ |
---|
555 | % attr_d['unit'] |
---|
556 | msg += "expecting [%s]" % data_info.y_unit |
---|
557 | raise ValueError, msg |
---|
558 | |
---|
559 | if _x is not None and _y is not None: |
---|
560 | x = numpy.append(x, _x) |
---|
561 | y = numpy.append(y, _y) |
---|
562 | dx = numpy.append(dx, _dx) |
---|
563 | dy = numpy.append(dy, _dy) |
---|
564 | dxl = numpy.append(dxl, _dxl) |
---|
565 | dxw = numpy.append(dxw, _dxw) |
---|
566 | # Zeros in dx, dy |
---|
567 | if not numpy.all(dx==0): |
---|
568 | dx[dx==0] = _ZERO |
---|
569 | if not numpy.all(dy==0): |
---|
570 | dy[dy==0] = _ZERO |
---|
571 | |
---|
572 | data_info.x = x[x!=0] |
---|
573 | data_info.y = y[x!=0] |
---|
574 | data_info.dx = dx[x!=0] |
---|
575 | |
---|
576 | data_info.dy = dy[x!=0] |
---|
577 | data_info.dxl = dxl[x!=0] |
---|
578 | data_info.dxw = dxw[x!=0] |
---|
579 | |
---|
580 | data_conv_q = None |
---|
581 | data_conv_i = None |
---|
582 | |
---|
583 | if HAS_CONVERTER == True and data_info.x_unit != '1/A': |
---|
584 | data_conv_q = Converter('1/A') |
---|
585 | # Test it |
---|
586 | data_conv_q(1.0, output.Q_unit) |
---|
587 | |
---|
588 | if HAS_CONVERTER == True and data_info.y_unit != '1/cm': |
---|
589 | data_conv_i = Converter('1/cm') |
---|
590 | # Test it |
---|
591 | data_conv_i(1.0, output.I_unit) |
---|
592 | |
---|
593 | if data_conv_q is not None: |
---|
594 | data_info.xaxis("\\rm{Q}", data_info.x_unit) |
---|
595 | else: |
---|
596 | data_info.xaxis("\\rm{Q}", 'A^{-1}') |
---|
597 | if data_conv_i is not None: |
---|
598 | data_info.yaxis("\\rm{Intensity}", data_info.y_unit) |
---|
599 | else: |
---|
600 | data_info.yaxis("\\rm{Intensity}","cm^{-1}") |
---|
601 | |
---|
602 | return data_info |
---|
603 | |
---|
604 | def _to_xml_doc(self, datainfo): |
---|
605 | """ |
---|
606 | Create an XML document to contain the content of a Data1D |
---|
607 | |
---|
608 | :param datainfo: Data1D object |
---|
609 | """ |
---|
610 | |
---|
611 | if not issubclass(datainfo.__class__, Data1D): |
---|
612 | raise RuntimeError, "The cansas writer expects a Data1D instance" |
---|
613 | |
---|
614 | doc = xml.dom.minidom.Document() |
---|
615 | main_node = doc.createElement("SASroot") |
---|
616 | main_node.setAttribute("version", self.version) |
---|
617 | main_node.setAttribute("xmlns", "cansas1d/%s" % self.version) |
---|
618 | main_node.setAttribute("xmlns:xsi", |
---|
619 | "http://www.w3.org/2001/XMLSchema-instance") |
---|
620 | main_node.setAttribute("xsi:schemaLocation", |
---|
621 | "cansas1d/%s http://svn.smallangles.net/svn/canSAS/1dwg/trunk/cansas1d.xsd" % self.version) |
---|
622 | |
---|
623 | doc.appendChild(main_node) |
---|
624 | |
---|
625 | entry_node = doc.createElement("SASentry") |
---|
626 | main_node.appendChild(entry_node) |
---|
627 | |
---|
628 | write_node(doc, entry_node, "Title", datainfo.title) |
---|
629 | for item in datainfo.run: |
---|
630 | runname = {} |
---|
631 | if datainfo.run_name.has_key(item) and \ |
---|
632 | len(str(datainfo.run_name[item]))>1: |
---|
633 | runname = {'name': datainfo.run_name[item] } |
---|
634 | write_node(doc, entry_node, "Run", item, runname) |
---|
635 | |
---|
636 | # Data info |
---|
637 | node = doc.createElement("SASdata") |
---|
638 | entry_node.appendChild(node) |
---|
639 | |
---|
640 | for i in range(len(datainfo.x)): |
---|
641 | pt = doc.createElement("Idata") |
---|
642 | node.appendChild(pt) |
---|
643 | write_node(doc, pt, "Q", datainfo.x[i], {'unit':datainfo.x_unit}) |
---|
644 | if len(datainfo.y)>=i: |
---|
645 | write_node(doc, pt, "I", datainfo.y[i], |
---|
646 | {'unit':datainfo.y_unit}) |
---|
647 | if datainfo.dx != None and len(datainfo.dx) >= i: |
---|
648 | write_node(doc, pt, "Qdev", datainfo.dx[i], |
---|
649 | {'unit':datainfo.x_unit}) |
---|
650 | if datainfo.dxl != None and len(datainfo.dxl) >= i: |
---|
651 | write_node(doc, pt, "dQl", datainfo.dxl[i], |
---|
652 | {'unit':datainfo.x_unit}) |
---|
653 | if datainfo.dxw != None and len(datainfo.dxw) >= i: |
---|
654 | write_node(doc, pt, "dQw", datainfo.dxw[i], |
---|
655 | {'unit':datainfo.x_unit}) |
---|
656 | if datainfo.dy != None and len(datainfo.dy) >= i: |
---|
657 | write_node(doc, pt, "Idev", datainfo.dy[i], |
---|
658 | {'unit':datainfo.y_unit}) |
---|
659 | |
---|
660 | |
---|
661 | # Sample info |
---|
662 | sample = doc.createElement("SASsample") |
---|
663 | if datainfo.sample.name is not None: |
---|
664 | sample.setAttribute("name", str(datainfo.sample.name)) |
---|
665 | entry_node.appendChild(sample) |
---|
666 | write_node(doc, sample, "ID", str(datainfo.sample.ID)) |
---|
667 | write_node(doc, sample, "thickness", datainfo.sample.thickness, |
---|
668 | {"unit":datainfo.sample.thickness_unit}) |
---|
669 | write_node(doc, sample, "transmission", datainfo.sample.transmission) |
---|
670 | write_node(doc, sample, "temperature", datainfo.sample.temperature, |
---|
671 | {"unit":datainfo.sample.temperature_unit}) |
---|
672 | |
---|
673 | for item in datainfo.sample.details: |
---|
674 | write_node(doc, sample, "details", item) |
---|
675 | |
---|
676 | pos = doc.createElement("position") |
---|
677 | written = write_node(doc, pos, "x", datainfo.sample.position.x, |
---|
678 | {"unit":datainfo.sample.position_unit}) |
---|
679 | written = written | write_node(doc, pos, "y", |
---|
680 | datainfo.sample.position.y, |
---|
681 | {"unit":datainfo.sample.position_unit}) |
---|
682 | written = written | write_node(doc, pos, "z", |
---|
683 | datainfo.sample.position.z, |
---|
684 | {"unit":datainfo.sample.position_unit}) |
---|
685 | if written == True: |
---|
686 | sample.appendChild(pos) |
---|
687 | |
---|
688 | ori = doc.createElement("orientation") |
---|
689 | written = write_node(doc, ori, "roll", |
---|
690 | datainfo.sample.orientation.x, |
---|
691 | {"unit":datainfo.sample.orientation_unit}) |
---|
692 | written = written | write_node(doc, ori, "pitch", |
---|
693 | datainfo.sample.orientation.y, |
---|
694 | {"unit":datainfo.sample.orientation_unit}) |
---|
695 | written = written | write_node(doc, ori, "yaw", |
---|
696 | datainfo.sample.orientation.z, |
---|
697 | {"unit":datainfo.sample.orientation_unit}) |
---|
698 | if written == True: |
---|
699 | sample.appendChild(ori) |
---|
700 | |
---|
701 | # Instrument info |
---|
702 | instr = doc.createElement("SASinstrument") |
---|
703 | entry_node.appendChild(instr) |
---|
704 | |
---|
705 | write_node(doc, instr, "name", datainfo.instrument) |
---|
706 | |
---|
707 | # Source |
---|
708 | source = doc.createElement("SASsource") |
---|
709 | if datainfo.source.name is not None: |
---|
710 | source.setAttribute("name", str(datainfo.source.name)) |
---|
711 | instr.appendChild(source) |
---|
712 | |
---|
713 | write_node(doc, source, "radiation", datainfo.source.radiation) |
---|
714 | write_node(doc, source, "beam_shape", datainfo.source.beam_shape) |
---|
715 | size = doc.createElement("beam_size") |
---|
716 | if datainfo.source.beam_size_name is not None: |
---|
717 | size.setAttribute("name", str(datainfo.source.beam_size_name)) |
---|
718 | written = write_node(doc, size, "x", datainfo.source.beam_size.x, |
---|
719 | {"unit":datainfo.source.beam_size_unit}) |
---|
720 | written = written | write_node(doc, size, "y", |
---|
721 | datainfo.source.beam_size.y, |
---|
722 | {"unit":datainfo.source.beam_size_unit}) |
---|
723 | written = written | write_node(doc, size, "z", |
---|
724 | datainfo.source.beam_size.z, |
---|
725 | {"unit":datainfo.source.beam_size_unit}) |
---|
726 | if written == True: |
---|
727 | source.appendChild(size) |
---|
728 | |
---|
729 | write_node(doc, source, "wavelength", |
---|
730 | datainfo.source.wavelength, |
---|
731 | {"unit":datainfo.source.wavelength_unit}) |
---|
732 | write_node(doc, source, "wavelength_min", |
---|
733 | datainfo.source.wavelength_min, |
---|
734 | {"unit":datainfo.source.wavelength_min_unit}) |
---|
735 | write_node(doc, source, "wavelength_max", |
---|
736 | datainfo.source.wavelength_max, |
---|
737 | {"unit":datainfo.source.wavelength_max_unit}) |
---|
738 | write_node(doc, source, "wavelength_spread", |
---|
739 | datainfo.source.wavelength_spread, |
---|
740 | {"unit":datainfo.source.wavelength_spread_unit}) |
---|
741 | |
---|
742 | # Collimation |
---|
743 | for item in datainfo.collimation: |
---|
744 | coll = doc.createElement("SAScollimation") |
---|
745 | if item.name is not None: |
---|
746 | coll.setAttribute("name", str(item.name)) |
---|
747 | instr.appendChild(coll) |
---|
748 | |
---|
749 | write_node(doc, coll, "length", item.length, |
---|
750 | {"unit":item.length_unit}) |
---|
751 | |
---|
752 | for apert in item.aperture: |
---|
753 | ap = doc.createElement("aperture") |
---|
754 | if apert.name is not None: |
---|
755 | ap.setAttribute("name", str(apert.name)) |
---|
756 | if apert.type is not None: |
---|
757 | ap.setAttribute("type", str(apert.type)) |
---|
758 | coll.appendChild(ap) |
---|
759 | |
---|
760 | write_node(doc, ap, "distance", apert.distance, |
---|
761 | {"unit":apert.distance_unit}) |
---|
762 | |
---|
763 | size = doc.createElement("size") |
---|
764 | if apert.size_name is not None: |
---|
765 | size.setAttribute("name", str(apert.size_name)) |
---|
766 | written = write_node(doc, size, "x", apert.size.x, |
---|
767 | {"unit":apert.size_unit}) |
---|
768 | written = written | write_node(doc, size, "y", apert.size.y, |
---|
769 | {"unit":apert.size_unit}) |
---|
770 | written = written | write_node(doc, size, "z", apert.size.z, |
---|
771 | {"unit":apert.size_unit}) |
---|
772 | if written == True: |
---|
773 | ap.appendChild(size) |
---|
774 | |
---|
775 | # Detectors |
---|
776 | for item in datainfo.detector: |
---|
777 | det = doc.createElement("SASdetector") |
---|
778 | written = write_node(doc, det, "name", item.name) |
---|
779 | written = written | write_node(doc, det, "SDD", item.distance, |
---|
780 | {"unit":item.distance_unit}) |
---|
781 | written = written | write_node(doc, det, "slit_length", |
---|
782 | item.slit_length, |
---|
783 | {"unit":item.slit_length_unit}) |
---|
784 | if written == True: |
---|
785 | instr.appendChild(det) |
---|
786 | |
---|
787 | off = doc.createElement("offset") |
---|
788 | written = write_node(doc, off, "x", item.offset.x, |
---|
789 | {"unit":item.offset_unit}) |
---|
790 | written = written | write_node(doc, off, "y", item.offset.y, |
---|
791 | {"unit":item.offset_unit}) |
---|
792 | written = written | write_node(doc, off, "z", item.offset.z, |
---|
793 | {"unit":item.offset_unit}) |
---|
794 | if written == True: |
---|
795 | det.appendChild(off) |
---|
796 | |
---|
797 | center = doc.createElement("beam_center") |
---|
798 | written = write_node(doc, center, "x", item.beam_center.x, |
---|
799 | {"unit":item.beam_center_unit}) |
---|
800 | written = written | write_node(doc, center, "y", |
---|
801 | item.beam_center.y, |
---|
802 | {"unit":item.beam_center_unit}) |
---|
803 | written = written | write_node(doc, center, "z", |
---|
804 | item.beam_center.z, |
---|
805 | {"unit":item.beam_center_unit}) |
---|
806 | if written == True: |
---|
807 | det.appendChild(center) |
---|
808 | |
---|
809 | pix = doc.createElement("pixel_size") |
---|
810 | written = write_node(doc, pix, "x", item.pixel_size.x, |
---|
811 | {"unit":item.pixel_size_unit}) |
---|
812 | written = written | write_node(doc, pix, "y", item.pixel_size.y, |
---|
813 | {"unit":item.pixel_size_unit}) |
---|
814 | written = written | write_node(doc, pix, "z", item.pixel_size.z, |
---|
815 | {"unit":item.pixel_size_unit}) |
---|
816 | if written == True: |
---|
817 | det.appendChild(pix) |
---|
818 | |
---|
819 | ori = doc.createElement("orientation") |
---|
820 | written = write_node(doc, ori, "roll", item.orientation.x, |
---|
821 | {"unit":item.orientation_unit}) |
---|
822 | written = written | write_node(doc, ori, "pitch", |
---|
823 | item.orientation.y, |
---|
824 | {"unit":item.orientation_unit}) |
---|
825 | written = written | write_node(doc, ori, "yaw", |
---|
826 | item.orientation.z, |
---|
827 | {"unit":item.orientation_unit}) |
---|
828 | if written == True: |
---|
829 | det.appendChild(ori) |
---|
830 | |
---|
831 | |
---|
832 | # Processes info |
---|
833 | for item in datainfo.process: |
---|
834 | node = doc.createElement("SASprocess") |
---|
835 | entry_node.appendChild(node) |
---|
836 | |
---|
837 | write_node(doc, node, "name", item.name) |
---|
838 | write_node(doc, node, "date", item.date) |
---|
839 | write_node(doc, node, "description", item.description) |
---|
840 | for term in item.term: |
---|
841 | value = term['value'] |
---|
842 | del term['value'] |
---|
843 | write_node(doc, node, "term", value, term) |
---|
844 | for note in item.notes: |
---|
845 | write_node(doc, node, "SASprocessnote", note) |
---|
846 | |
---|
847 | # Return the document, and the SASentry node associated with |
---|
848 | # the data we just wrote |
---|
849 | return doc, entry_node |
---|
850 | |
---|
851 | def write(self, filename, datainfo): |
---|
852 | """ |
---|
853 | Write the content of a Data1D as a CanSAS XML file |
---|
854 | |
---|
855 | :param filename: name of the file to write |
---|
856 | :param datainfo: Data1D object |
---|
857 | """ |
---|
858 | # Create XML document |
---|
859 | doc, sasentry = self._to_xml_doc(datainfo) |
---|
860 | # Write the file |
---|
861 | fd = open(filename, 'w') |
---|
862 | fd.write(doc.toprettyxml()) |
---|
863 | fd.close() |
---|
864 | |
---|
865 | def _store_float(self, location, node, variable, storage, optional=True): |
---|
866 | """ |
---|
867 | Get the content of a xpath location and store |
---|
868 | the result. Check that the units are compatible |
---|
869 | with the destination. The value is expected to |
---|
870 | be a float. |
---|
871 | |
---|
872 | The xpath location might or might not exist. |
---|
873 | If it does not exist, nothing is done |
---|
874 | |
---|
875 | :param location: xpath location to fetch |
---|
876 | :param node: node to read the data from |
---|
877 | :param variable: name of the data member to store it in [string] |
---|
878 | :param storage: data object that has the 'variable' data member |
---|
879 | :param optional: if True, no exception will be raised |
---|
880 | if unit conversion can't be done |
---|
881 | |
---|
882 | :raise ValueError: raised when the units are not recognized |
---|
883 | """ |
---|
884 | entry = get_content(location, node) |
---|
885 | try: |
---|
886 | value = float(entry.text) |
---|
887 | except: |
---|
888 | value = None |
---|
889 | |
---|
890 | if value is not None: |
---|
891 | # If the entry has units, check to see that they are |
---|
892 | # compatible with what we currently have in the data object |
---|
893 | units = entry.get('unit') |
---|
894 | if units is not None: |
---|
895 | toks = variable.split('.') |
---|
896 | exec "local_unit = storage.%s_unit" % toks[0] |
---|
897 | if units.lower()!=local_unit.lower(): |
---|
898 | if HAS_CONVERTER == True: |
---|
899 | try: |
---|
900 | conv = Converter(units) |
---|
901 | exec "storage.%s = %g" % (variable, |
---|
902 | conv(value, units=local_unit)) |
---|
903 | except: |
---|
904 | err_mess = "CanSAS reader: could not convert" |
---|
905 | err_mess += " %s unit [%s]; expecting [%s]\n %s" \ |
---|
906 | % (variable, units, local_unit, sys.exc_value) |
---|
907 | self.errors.append(err_mess) |
---|
908 | if optional: |
---|
909 | logging.info(err_mess) |
---|
910 | else: |
---|
911 | raise ValueError, err_mess |
---|
912 | else: |
---|
913 | err_mess = "CanSAS reader: unrecognized %s unit [%s];"\ |
---|
914 | % (variable, units) |
---|
915 | err_mess += " expecting [%s]" % local_unit |
---|
916 | self.errors.append(err_mess) |
---|
917 | if optional: |
---|
918 | logging.info(err_mess) |
---|
919 | else: |
---|
920 | raise ValueError, err_mess |
---|
921 | else: |
---|
922 | exec "storage.%s = value" % variable |
---|
923 | else: |
---|
924 | exec "storage.%s = value" % variable |
---|
925 | |
---|
926 | def _store_content(self, location, node, variable, storage): |
---|
927 | """ |
---|
928 | Get the content of a xpath location and store |
---|
929 | the result. The value is treated as a string. |
---|
930 | |
---|
931 | The xpath location might or might not exist. |
---|
932 | If it does not exist, nothing is done |
---|
933 | |
---|
934 | :param location: xpath location to fetch |
---|
935 | :param node: node to read the data from |
---|
936 | :param variable: name of the data member to store it in [string] |
---|
937 | :param storage: data object that has the 'variable' data member |
---|
938 | |
---|
939 | :return: return a list of errors |
---|
940 | """ |
---|
941 | entry = get_content(location, node) |
---|
942 | if entry is not None and entry.text is not None: |
---|
943 | exec "storage.%s = entry.text.strip()" % variable |
---|
944 | |
---|
945 | |
---|
946 | |
---|
947 | if __name__ == "__main__": |
---|
948 | logging.basicConfig(level=logging.ERROR, |
---|
949 | format='%(asctime)s %(levelname)s %(message)s', |
---|
950 | filename='cansas_reader.log', |
---|
951 | filemode='w') |
---|
952 | reader = Reader() |
---|
953 | print reader.read("../test/cansas1d.xml") |
---|
954 | #print reader.read("../test/latex_smeared.xml") |
---|
955 | |
---|
956 | |
---|
957 | |
---|