Changeset b0d0723 in sasview
- Timestamp:
- Aug 15, 2009 8:17:01 PM (15 years ago)
- Branches:
- master, ESS_GUI, ESS_GUI_Docs, ESS_GUI_batch_fitting, ESS_GUI_bumps_abstraction, ESS_GUI_iss1116, ESS_GUI_iss879, ESS_GUI_iss959, ESS_GUI_opencl, ESS_GUI_ordering, ESS_GUI_sync_sascalc, costrafo411, magnetic_scatt, release-4.1.1, release-4.1.2, release-4.2.2, release_4.0.1, ticket-1009, ticket-1094-headless, ticket-1242-2d-resolution, ticket-1243, ticket-1249, ticket885, unittest-saveload
- Children:
- 2cb89e7
- Parents:
- 379e15b
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
DataLoader/readers/cansas_reader.py
r7d8094b rb0d0723 6 6 See the license text in license.txt 7 7 8 copyright 2008, University of Tennessee8 copyright 2008, 2009, University of Tennessee 9 9 """ 10 10 # Known issue: reader not compatible with multiple SASdata entries … … 24 24 import os, sys 25 25 from DataLoader.data_info import Data1D, Collimation, Detector, Process, Aperture 26 from xml import xpath 27 import xml.dom.minidom 28 26 from lxml import etree 27 import xml.dom.minidom 29 28 30 29 has_converter = True … … 33 32 except: 34 33 has_converter = False 34 35 CANSAS_NS = "cansas1d/1.0" 35 36 36 37 def write_node(doc, parent, name, value, attr={}): … … 52 53 return False 53 54 54 def get_node_text(node):55 """56 Get the text context of a node57 58 @param node: node to read from59 @return: content, attribute list60 """61 content = None62 attr = {}63 for item in node.childNodes:64 if item.nodeName.find('text')>=0 \65 and len(item.nodeValue.strip())>0:66 content = item.nodeValue.strip()67 break68 69 if node.hasAttributes():70 for i in range(node.attributes.length):71 attr[node.attributes.item(i).nodeName] \72 = node.attributes.item(i).nodeValue73 74 return content, attr75 76 55 def get_content(location, node): 77 56 """ … … 80 59 @param location: xpath location 81 60 @param node: node to start at 82 """ 61 @return: Element, or None 62 """ 63 nodes = node.xpath(location, namespaces={'ns': CANSAS_NS}) 64 65 if len(nodes)>0: 66 return nodes[0] 67 else: 68 return None 69 70 def get_float(location, node): 71 """ 72 Get the content of a node as a float 73 74 @param location: xpath location 75 @param node: node to start at 76 """ 77 nodes = node.xpath(location, namespaces={'ns': CANSAS_NS}) 78 83 79 value = None 84 attr 85 nodes = xpath.Evaluate(location, node)80 attr = {} 81 86 82 if len(nodes)>0: 87 83 try: 88 # Skip comments and empty lines 89 for item in nodes[0].childNodes: 90 if item.nodeName.find('text')>=0 \ 91 and len(item.nodeValue.strip())>0: 92 value = item.nodeValue.strip() 93 break 94 95 if nodes[0].hasAttributes(): 96 for i in range(nodes[0].attributes.length): 97 attr[nodes[0].attributes.item(i).nodeName] \ 98 = nodes[0].attributes.item(i).nodeValue 99 except: 100 # problem reading the node. Skip it and return that 101 # nothing was found 102 logging.error("cansas_reader.get_content: %s\n %s" % (location, sys.exc_value)) 103 104 return value, attr 105 106 def get_float(location, node): 107 """ 108 Get the content of a node as a float 109 110 @param location: xpath location 111 @param node: node to start at 112 """ 113 value = None 114 attr = {} 115 content, attr = get_content(location, node) 116 if content is not None: 117 try: 118 value = float(content) 84 value = float(nodes[0].text) 119 85 except: 120 86 # Could not pass, skip and return None 121 logging.error("cansas_reader.get_float: could not convert '%s' to float" % content) 122 87 logging.error("cansas_reader.get_float: could not convert '%s' to float" % nodes[0].text) 88 89 if nodes[0].get('unit') is not None: 90 attr['unit'] = nodes[0].get('unit') 91 123 92 return value, attr 124 93 … … 140 109 @raise ValueError: raised when the units are not recognized 141 110 """ 142 value, attr = get_float(location, node) 111 entry = get_content(location, node) 112 try: 113 value = float(entry.text) 114 except: 115 value = None 116 143 117 if value is not None: 144 118 # If the entry has units, check to see that they are 145 119 # compatible with what we currently have in the data object 146 if attr.has_key('unit'): 120 units = entry.get('unit') 121 if units is not None: 147 122 toks = variable.split('.') 148 123 exec "local_unit = storage.%s_unit" % toks[0] 149 if attr['unit'].lower()!=local_unit.lower():124 if units.lower()!=local_unit.lower(): 150 125 if has_converter==True: 151 126 try: 152 conv = Converter( attr['unit'])127 conv = Converter(units) 153 128 exec "storage.%s = %g" % (variable, conv(value, units=local_unit)) 154 129 except: 155 #Below three lines were added for the unit = 1/A. local unit is defined in 'mm'. Need to check!!! 156 if variable == 'slit_length' and attr['unit'] !=local_unit: 157 pass 158 else: 159 raise ValueError, "CanSAS reader: could not convert %s unit [%s]; expecting [%s]\n %s" \ 160 % (variable, attr['unit'], local_unit, sys.exc_value) 130 raise ValueError, "CanSAS reader: could not convert %s unit [%s]; expecting [%s]\n %s" \ 131 % (variable, units, local_unit, sys.exc_value) 161 132 else: 162 133 raise ValueError, "CanSAS reader: unrecognized %s unit [%s]; expecting [%s]" \ 163 % (variable, attr['unit'], local_unit)134 % (variable, units, local_unit) 164 135 else: 165 136 exec "storage.%s = value" % variable … … 181 152 @param storage: data object that has the 'variable' data member 182 153 """ 183 value, attr= get_content(location, node)184 if valueis not None:185 exec "storage.%s = value" % variable154 entry = get_content(location, node) 155 if entry is not None and entry.text is not None: 156 exec "storage.%s = entry.text.strip()" % variable 186 157 187 158 … … 213 184 @raise ValueError: when the length of the data vectors are inconsistent 214 185 """ 215 from xml.dom.minidom import parse216 217 186 output = [] 218 187 … … 222 191 if extension.lower() in self.ext: 223 192 224 dom = parse(path) 225 193 tree = etree.parse(path, parser=etree.ETCompatXMLParser()) 226 194 # Check the format version number 227 nodes = xpath.Evaluate('SASroot', dom) 228 if nodes[0].hasAttributes(): 229 for i in range(nodes[0].attributes.length): 230 if nodes[0].attributes.item(i).nodeName=='version': 231 if nodes[0].attributes.item(i).nodeValue != self.version: 232 raise ValueError, "cansas_reader: unrecognized version number %s" % \ 233 nodes[0].attributes.item(i).nodeValue 234 235 entry_list = xpath.Evaluate('SASroot/SASentry', dom) 195 # Specifying the namespace will take care of the file format version 196 root = tree.getroot() 197 198 entry_list = root.xpath('/ns:SASroot/ns:SASentry', namespaces={'ns': CANSAS_NS}) 199 236 200 for entry in entry_list: 237 201 sas_entry = self._parse_entry(entry) … … 262 226 data_info = Data1D(x, y) 263 227 264 # Look up title 265 _store_content('Title', dom, 'title', data_info) 228 # Look up title 229 _store_content('ns:Title', dom, 'title', data_info) 230 266 231 # Look up run number 267 nodes = xpath.Evaluate('Run', dom)232 nodes = dom.xpath('ns:Run', namespaces={'ns': CANSAS_NS}) 268 233 for item in nodes: 269 value, attr = get_node_text(item) 270 if value is not None: 271 data_info.run.append(value) 272 if attr.has_key('name'): 273 data_info.run_name[value] = attr['name'] 234 if item.text is not None: 235 value = item.text.strip() 236 if len(value) > 0: 237 data_info.run.append(value) 238 if item.get('name') is not None: 239 data_info.run_name[value] = item.get('name') 274 240 275 241 # Look up instrument name 276 _store_content('SASinstrument/name', dom, 'instrument', data_info) 277 #value, attr = get_content('SASinstrument', dom) 278 #if attr.has_key('name'): 279 # data_info.instrument = attr['name'] 280 281 note_list = xpath.Evaluate('SASnote', dom) 242 _store_content('ns:SASinstrument/ns:name', dom, 'instrument', data_info) 243 244 # Notes 245 note_list = dom.xpath('ns:SASnote', namespaces={'ns': CANSAS_NS}) 282 246 for note in note_list: 283 247 try: 284 note_value, note_attr = get_node_text(note) 285 if note_value is not None: 286 data_info.notes.append(note_value) 248 if note.text is not None: 249 note_value = note.text.strip() 250 if len(note_value) > 0: 251 data_info.notes.append(note_value) 287 252 except: 288 253 logging.error("cansas_reader.read: error processing entry notes\n %s" % sys.exc_value) 289 290 254 291 255 # Sample info ################### 292 value, attr = get_content('SASsample', dom)293 if attr.has_key('name'):294 data_info.sample.name = attr['name']295 296 _store_content(' SASsample/ID',256 entry = get_content('ns:SASsample', dom) 257 if entry is not None: 258 data_info.sample.name = entry.get('name') 259 260 _store_content('ns:SASsample/ns:ID', 297 261 dom, 'ID', data_info.sample) 298 _store_float(' SASsample/thickness',262 _store_float('ns:SASsample/ns:thickness', 299 263 dom, 'thickness', data_info.sample) 300 _store_float(' SASsample/transmission',264 _store_float('ns:SASsample/ns:transmission', 301 265 dom, 'transmission', data_info.sample) 302 _store_float(' SASsample/temperature',266 _store_float('ns:SASsample/ns:temperature', 303 267 dom, 'temperature', data_info.sample) 304 nodes = xpath.Evaluate('SASsample/details', dom) 268 269 nodes = dom.xpath('ns:SASsample/ns:details', namespaces={'ns': CANSAS_NS}) 305 270 for item in nodes: 306 271 try: 307 detail_value, detail_attr = get_node_text(item) 308 if detail_value is not None: 309 data_info.sample.details.append(detail_value) 272 if item.text is not None: 273 detail_value = item.text.strip() 274 if len(detail_value) > 0: 275 data_info.sample.details.append(detail_value) 310 276 except: 311 277 logging.error("cansas_reader.read: error processing sample details\n %s" % sys.exc_value) 312 278 313 279 # Position (as a vector) 314 _store_float(' SASsample/position/x',280 _store_float('ns:SASsample/ns:position/ns:x', 315 281 dom, 'position.x', data_info.sample) 316 _store_float(' SASsample/position/y',282 _store_float('ns:SASsample/ns:position/ns:y', 317 283 dom, 'position.y', data_info.sample) 318 _store_float(' SASsample/position/z',284 _store_float('ns:SASsample/ns:position/ns:z', 319 285 dom, 'position.z', data_info.sample) 320 286 321 287 # Orientation (as a vector) 322 _store_float(' SASsample/orientation/roll',288 _store_float('ns:SASsample/ns:orientation/ns:roll', 323 289 dom, 'orientation.x', data_info.sample) 324 _store_float(' SASsample/orientation/pitch',290 _store_float('ns:SASsample/ns:orientation/ns:pitch', 325 291 dom, 'orientation.y', data_info.sample) 326 _store_float(' SASsample/orientation/yaw',292 _store_float('ns:SASsample/ns:orientation/ns:yaw', 327 293 dom, 'orientation.z', data_info.sample) 328 294 329 295 # Source info ################### 330 value, attr = get_content('SASinstrument/SASsource', dom)331 if attr.has_key('name'):332 data_info.source.name = attr['name']333 334 _store_content(' SASinstrument/SASsource/radiation',296 entry = get_content('ns:SASinstrument/ns:SASsource', dom) 297 if entry is not None: 298 data_info.source.name = entry.get('name') 299 300 _store_content('ns:SASinstrument/ns:SASsource/ns:radiation', 335 301 dom, 'radiation', data_info.source) 336 _store_content(' SASinstrument/SASsource/beam_shape',302 _store_content('ns:SASinstrument/ns:SASsource/ns:beam_shape', 337 303 dom, 'beam_shape', data_info.source) 338 _store_float(' SASinstrument/SASsource/wavelength',304 _store_float('ns:SASinstrument/ns:SASsource/ns:wavelength', 339 305 dom, 'wavelength', data_info.source) 340 _store_float(' SASinstrument/SASsource/wavelength_min',306 _store_float('ns:SASinstrument/ns:SASsource/ns:wavelength_min', 341 307 dom, 'wavelength_min', data_info.source) 342 _store_float(' SASinstrument/SASsource/wavelength_max',308 _store_float('ns:SASinstrument/ns:SASsource/ns:wavelength_max', 343 309 dom, 'wavelength_max', data_info.source) 344 _store_float(' SASinstrument/SASsource/wavelength_spread',310 _store_float('ns:SASinstrument/ns:SASsource/ns:wavelength_spread', 345 311 dom, 'wavelength_spread', data_info.source) 346 312 347 313 # Beam size (as a vector) 348 value, attr = get_content('SASinstrument/SASsource/beam_size', dom)349 if attr.has_key('name'):350 data_info.source.beam_size_name = attr['name']351 352 _store_float(' SASinstrument/SASsource/beam_size/x',314 entry = get_content('ns:SASinstrument/ns:SASsource/ns:beam_size', dom) 315 if entry is not None: 316 data_info.source.beam_size_name = entry.get('name') 317 318 _store_float('ns:SASinstrument/ns:SASsource/ns:beam_size/ns:x', 353 319 dom, 'beam_size.x', data_info.source) 354 _store_float(' SASinstrument/SASsource/beam_size/y',320 _store_float('ns:SASinstrument/ns:SASsource/ns:beam_size/ns:y', 355 321 dom, 'beam_size.y', data_info.source) 356 _store_float(' SASinstrument/SASsource/beam_size/z',322 _store_float('ns:SASinstrument/ns:SASsource/ns:beam_size/ns:z', 357 323 dom, 'beam_size.z', data_info.source) 358 324 359 325 # Collimation info ################### 360 nodes = xpath.Evaluate('SASinstrument/SAScollimation', dom)326 nodes = dom.xpath('ns:SASinstrument/ns:SAScollimation', namespaces={'ns': CANSAS_NS}) 361 327 for item in nodes: 362 328 collim = Collimation() 363 value, attr = get_node_text(item) 364 if attr.has_key('name'): 365 collim.name = attr['name'] 366 _store_float('length', item, 'length', collim) 329 if item.get('name') is not None: 330 collim.name = item.get('name') 331 _store_float('ns:length', item, 'length', collim) 367 332 368 333 # Look for apertures 369 apert_list = xpath.Evaluate('aperture', item)334 apert_list = item.xpath('ns:aperture', namespaces={'ns': CANSAS_NS}) 370 335 for apert in apert_list: 371 336 aperture = Aperture() 372 337 373 338 # Get the name and type of the aperture 374 ap_value, ap_attr = get_node_text(apert) 375 if ap_attr.has_key('name'): 376 aperture.name = ap_attr['name'] 377 if ap_attr.has_key('type'): 378 aperture.type = ap_attr['type'] 339 aperture.name = apert.get('name') 340 aperture.type = apert.get('type') 379 341 380 _store_float(' distance', apert, 'distance', aperture)381 382 value, attr = get_content('size', apert)383 if attr.has_key('name'):384 aperture.size_name = attr['name']385 386 _store_float(' size/x', apert, 'size.x', aperture)387 _store_float(' size/y', apert, 'size.y', aperture)388 _store_float(' size/z', apert, 'size.z', aperture)342 _store_float('ns:distance', apert, 'distance', aperture) 343 344 entry = get_content('ns:size', apert) 345 if entry is not None: 346 aperture.size_name = entry.get('name') 347 348 _store_float('ns:size/ns:x', apert, 'size.x', aperture) 349 _store_float('ns:size/ns:y', apert, 'size.y', aperture) 350 _store_float('ns:size/ns:z', apert, 'size.z', aperture) 389 351 390 352 collim.aperture.append(aperture) … … 393 355 394 356 # Detector info ###################### 395 nodes = xpath.Evaluate('SASinstrument/SASdetector', dom)357 nodes = dom.xpath('ns:SASinstrument/ns:SASdetector', namespaces={'ns': CANSAS_NS}) 396 358 for item in nodes: 397 359 398 360 detector = Detector() 399 361 400 _store_content('n ame', item, 'name', detector)401 _store_float(' SDD', item, 'distance', detector)362 _store_content('ns:name', item, 'name', detector) 363 _store_float('ns:SDD', item, 'distance', detector) 402 364 403 365 # Detector offset (as a vector) 404 _store_float(' offset/x', item, 'offset.x', detector)405 _store_float(' offset/y', item, 'offset.y', detector)406 _store_float(' offset/z', item, 'offset.z', detector)366 _store_float('ns:offset/ns:x', item, 'offset.x', detector) 367 _store_float('ns:offset/ns:y', item, 'offset.y', detector) 368 _store_float('ns:offset/ns:z', item, 'offset.z', detector) 407 369 408 370 # Detector orientation (as a vector) 409 _store_float(' orientation/roll', item, 'orientation.x', detector)410 _store_float(' orientation/pitch', item, 'orientation.y', detector)411 _store_float(' orientation/yaw', item, 'orientation.z', detector)371 _store_float('ns:orientation/ns:roll', item, 'orientation.x', detector) 372 _store_float('ns:orientation/ns:pitch', item, 'orientation.y', detector) 373 _store_float('ns:orientation/ns:yaw', item, 'orientation.z', detector) 412 374 413 375 # Beam center (as a vector) 414 _store_float(' beam_center/x', item, 'beam_center.x', detector)415 _store_float(' beam_center/y', item, 'beam_center.y', detector)416 _store_float(' beam_center/z', item, 'beam_center.z', detector)376 _store_float('ns:beam_center/ns:x', item, 'beam_center.x', detector) 377 _store_float('ns:beam_center/ns:y', item, 'beam_center.y', detector) 378 _store_float('ns:beam_center/ns:z', item, 'beam_center.z', detector) 417 379 418 380 # Pixel size (as a vector) 419 _store_float(' pixel_size/x', item, 'pixel_size.x', detector)420 _store_float(' pixel_size/y', item, 'pixel_size.y', detector)421 _store_float(' pixel_size/z', item, 'pixel_size.z', detector)422 423 _store_float(' slit_length', item, 'slit_length', detector)381 _store_float('ns:pixel_size/ns:x', item, 'pixel_size.x', detector) 382 _store_float('ns:pixel_size/ns:y', item, 'pixel_size.y', detector) 383 _store_float('ns:pixel_size/ns:z', item, 'pixel_size.z', detector) 384 385 _store_float('ns:slit_length', item, 'slit_length', detector) 424 386 425 387 data_info.detector.append(detector) 426 388 427 389 # Processes info ###################### 428 nodes = xpath.Evaluate('SASprocess', dom)390 nodes = dom.xpath('ns:SASprocess', namespaces={'ns': CANSAS_NS}) 429 391 for item in nodes: 430 392 process = Process() 431 _store_content('n ame', item, 'name', process)432 _store_content(' date', item, 'date', process)433 _store_content(' description', item, 'description', process)434 435 term_list = xpath.Evaluate('term', item)393 _store_content('ns:name', item, 'name', process) 394 _store_content('ns:date', item, 'date', process) 395 _store_content('ns:description', item, 'description', process) 396 397 term_list = item.xpath('ns:term', namespaces={'ns': CANSAS_NS}) 436 398 for term in term_list: 437 399 try: 438 term_value, term_attr = get_node_text(term) 439 term_attr['value'] = term_value 440 if term_value is not None: 400 term_attr = {} 401 for attr in term.keys(): 402 term_attr[attr] = term.get(attr).strip() 403 if term.text is not None: 404 term_attr['value'] = term.text.strip() 441 405 process.term.append(term_attr) 442 406 except: 443 407 logging.error("cansas_reader.read: error processing process term\n %s" % sys.exc_value) 444 408 445 note_list = xpath.Evaluate('SASprocessnote', item)409 note_list = item.xpath('ns:SASprocessnote', namespaces={'ns': CANSAS_NS}) 446 410 for note in note_list: 447 try: 448 note_value, note_attr = get_node_text(note) 449 if note_value is not None: 450 process.notes.append(note_value) 451 except: 452 logging.error("cansas_reader.read: error processing process notes\n %s" % sys.exc_value) 453 411 if note.text is not None: 412 process.notes.append(note.text.strip()) 454 413 455 414 data_info.process.append(process) … … 457 416 458 417 # Data info ###################### 459 nodes = xpath.Evaluate('SASdata', dom)418 nodes = dom.xpath('ns:SASdata', namespaces={'ns': CANSAS_NS}) 460 419 if len(nodes)>1: 461 420 raise RuntimeError, "CanSAS reader is not compatible with multiple SASdata entries" 462 421 463 nodes = xpath.Evaluate('SASdata/Idata', dom) 422 nodes = dom.xpath('ns:SASdata/ns:Idata', namespaces={'ns': CANSAS_NS}) 423 464 424 x = numpy.zeros(0) 465 425 y = numpy.zeros(0) … … 470 430 471 431 for item in nodes: 472 _x, attr = get_float(' Q', item)473 _dx, attr_d = get_float(' Qdev', item)474 _dxl, attr_l = get_float(' dQl', item)475 _dxw, attr_w = get_float(' dQw', item)432 _x, attr = get_float('ns:Q', item) 433 _dx, attr_d = get_float('ns:Qdev', item) 434 _dxl, attr_l = get_float('ns:dQl', item) 435 _dxw, attr_w = get_float('ns:dQw', item) 476 436 if _dx == None: 477 437 _dx = 0.0 … … 529 489 % (attr['unit'], data_info.x_unit) 530 490 531 _y, attr = get_float(' I', item)532 _dy, attr_d = get_float(' Idev', item)491 _y, attr = get_float('ns:I', item) 492 _dy, attr_d = get_float('ns:Idev', item) 533 493 if _dy == None: 534 494 _dy = 0.0 … … 804 764 reader = Reader() 805 765 print reader.read("../test/cansas1d.xml") 766 #print reader.read("../test/latex_smeared.xml") 806 767 807 768
Note: See TracChangeset
for help on using the changeset viewer.