Changeset 4c00964 in sasview for DataLoader/readers
- Timestamp:
- Aug 28, 2008 4:53:26 PM (16 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:
- 579ba85
- Parents:
- 3c404d3
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
DataLoader/readers/cansas_reader.py
rd6513cd r4c00964 10 10 11 11 #TODO: Unit conversion 12 #TODO: missing aperture type: go through all entries and check for additional attributes 13 #TODO: check that all vectors are written only if they have at least one non-empty value 14 #TODO: multiple SASEntrys 12 15 #TODO: Store error list 13 16 #TODO: convert from pixel to mm for beam center... … … 22 25 from DataLoader.data_info import Data1D, Collimation, Detector, Process, Aperture 23 26 from xml import xpath 27 import xml.dom.minidom 28 24 29 25 30 has_converter = True … … 28 33 except: 29 34 has_converter = False 35 36 def write_node(doc, parent, name, value, attr={}): 37 """ 38 @param doc: document DOM 39 @param parent: parent node 40 @param name: tag of the element 41 @param value: value of the child text node 42 @param attr: attribute dictionary 43 @return: True if something was appended, otherwise False 44 """ 45 if value is not None: 46 node = doc.createElement(name) 47 node.appendChild(doc.createTextNode(str(value))) 48 for item in attr: 49 node.setAttribute(item, attr[item]) 50 parent.appendChild(node) 51 return True 52 return False 30 53 31 54 def get_node_text(node): … … 287 310 288 311 # Source info ################### 312 value, attr = get_content('SASinstrument/SASsource', dom) 313 if attr.has_key('name'): 314 data_info.source.name = attr['name'] 315 289 316 _store_content('SASinstrument/SASsource/radiation', 290 317 dom, 'radiation', data_info.source) … … 312 339 for item in nodes: 313 340 collim = Collimation() 341 value, attr = get_node_text(item) 342 if attr.has_key('name'): 343 collim.name = attr['name'] 314 344 _store_float('length', item, 'length', collim) 315 345 … … 318 348 for apert in apert_list: 319 349 aperture = Aperture() 350 351 # Get the name and type of the aperture 352 ap_value, ap_attr = get_node_text(item) 353 if ap_attr.has_key('name'): 354 aperture.name = ap_attr['name'] 355 if ap_attr.has_key('type'): 356 aperture.type = ap_attr['type'] 357 320 358 _store_float('distance', apert, 'distance', aperture) 321 359 _store_float('size/x', apert, 'size.x', aperture) … … 451 489 return data_info 452 490 453 491 def write(self, filename, datainfo): 492 """ 493 Write the content of a Data1D as a CanSAS XML file 494 495 @param filename: name of the file to write 496 @param datainfo: Data1D object 497 """ 498 499 if not datainfo.__class__ == Data1D: 500 raise RuntimeError, "The cansas writer expects a Data1D instance" 501 502 doc = xml.dom.minidom.Document() 503 main_node = doc.createElement("SASroot") 504 main_node.setAttribute("version", "1.0") 505 doc.appendChild(main_node) 506 507 entry_node = doc.createElement("SASentry") 508 main_node.appendChild(entry_node) 509 510 write_node(doc, entry_node, "title", datainfo.title) 511 write_node(doc, entry_node, "run", datainfo.run) 512 513 # Data info 514 node = doc.createElement("SASdata") 515 entry_node.appendChild(node) 516 517 # Sample info 518 sample = doc.createElement("SASsample") 519 entry_node.appendChild(sample) 520 write_node(doc, sample, "ID", datainfo.sample.ID) 521 write_node(doc, sample, "thickness", datainfo.sample.thickness, {"unit":datainfo.sample.thickness_unit}) 522 write_node(doc, sample, "transmission", datainfo.sample.transmission) 523 write_node(doc, sample, "temperature", datainfo.sample.temperature, {"unit":datainfo.sample.temperature_unit}) 524 525 for item in datainfo.sample.details: 526 write_node(doc, sample, "details", item) 527 528 pos = doc.createElement("position") 529 written = False 530 written = written or write_node(doc, pos, "x", datainfo.sample.position.x, {"unit":datainfo.sample.position_unit}) 531 written = written or write_node(doc, pos, "y", datainfo.sample.position.y, {"unit":datainfo.sample.position_unit}) 532 written = written or write_node(doc, pos, "z", datainfo.sample.position.z, {"unit":datainfo.sample.position_unit}) 533 if written == True: 534 sample.appendChild(pos) 535 536 ori = doc.createElement("orientation") 537 written = False 538 written = written or write_node(doc, ori, "roll", datainfo.sample.orientation.x, {"unit":datainfo.sample.orientation_unit}) 539 written = written or write_node(doc, ori, "pitch", datainfo.sample.orientation.y, {"unit":datainfo.sample.orientation_unit}) 540 written = written or write_node(doc, ori, "yaw", datainfo.sample.orientation.z, {"unit":datainfo.sample.orientation_unit}) 541 if written == True: 542 sample.appendChild(ori) 543 544 # Instrument info 545 instr = doc.createElement("SASinstrument") 546 entry_node.appendChild(instr) 547 548 write_node(doc, instr, "name", datainfo.instrument) 549 550 # Source 551 source = doc.createElement("SASsource") 552 source.setAttribute("name", str(datainfo.source.name)) 553 instr.appendChild(source) 554 555 write_node(doc, source, "radiation", datainfo.source.radiation) 556 write_node(doc, source, "beam_shape", datainfo.source.beam_shape) 557 write_node(doc, source, "wavelength", datainfo.source.wavelength, {"unit":datainfo.source.wavelength_unit}) 558 write_node(doc, source, "wavelength_min", datainfo.source.wavelength_min, {"unit":datainfo.source.wavelength_min_unit}) 559 write_node(doc, source, "wavelength_max", datainfo.source.wavelength_max, {"unit":datainfo.source.wavelength_max_unit}) 560 write_node(doc, source, "wavelength_spread", datainfo.source.wavelength_spread, {"unit":datainfo.source.wavelength_spread_unit}) 561 562 # Collimation 563 for item in datainfo.collimation: 564 coll = doc.createElement("SAScollimation") 565 coll.setAttribute("name", item.name) 566 instr.appendChild(coll) 567 568 write_node(doc, coll, "length", item.length, {"unit":item.length_unit}) 569 570 for apert in item.aperture: 571 ap = doc.createElement("SAScollimation") 572 ap.setAttribute("name", apert.name) 573 ap.setAttribute("type", apert.type) 574 instr.appendChild(ap) 575 576 write_node(doc, ap, "distance", apert.distance, {"unit":apert.distance_unit}) 577 578 size = doc.createElement("size") 579 ap.appendChild(size) 580 write_node(doc, size, "x", apert.size.x, {"unit":apert.size_unit}) 581 write_node(doc, size, "y", apert.size.y, {"unit":apert.size_unit}) 582 write_node(doc, size, "z", apert.size.z, {"unit":apert.size_unit}) 583 584 585 # Detectors 586 for item in datainfo.detector: 587 det = doc.createElement("SASdetector") 588 instr.appendChild(det) 589 590 write_node(doc, det, "name", item.name) 591 write_node(doc, det, "SDD", item.distance, {"unit":item.distance_unit}) 592 write_node(doc, det, "slit_length", item.slit_length, {"unit":item.slit_length_unit}) 593 594 off = doc.createElement("offset") 595 det.appendChild(off) 596 write_node(doc, off, "x", item.offset.x, {"unit":item.offset_unit}) 597 write_node(doc, off, "y", item.offset.y, {"unit":item.offset_unit}) 598 write_node(doc, off, "z", item.offset.z, {"unit":item.offset_unit}) 599 600 601 center = doc.createElement("beam_center") 602 det.appendChild(center) 603 write_node(doc, center, "x", item.beam_center.x, {"unit":item.beam_center_unit}) 604 write_node(doc, center, "y", item.beam_center.y, {"unit":item.beam_center_unit}) 605 write_node(doc, center, "z", item.beam_center.z, {"unit":item.beam_center_unit}) 606 607 pix = doc.createElement("pixel_size") 608 det.appendChild(pix) 609 write_node(doc, pix, "x", item.pixel_size.x, {"unit":item.pixel_size_unit}) 610 write_node(doc, pix, "y", item.pixel_size.y, {"unit":item.pixel_size_unit}) 611 write_node(doc, pix, "z", item.pixel_size.z, {"unit":item.pixel_size_unit}) 612 613 614 # Sample info 615 for item in datainfo.process: 616 node = doc.createElement("SASprocess") 617 entry_node.appendChild(node) 618 619 write_node(doc, entry_node, "run", item.name) 620 621 622 # Write the file 623 fd = open(filename, 'w') 624 fd.write(doc.toprettyxml()) 625 fd.close() 626 627 454 628 if __name__ == "__main__": 455 629 logging.basicConfig(level=logging.ERROR,
Note: See TracChangeset
for help on using the changeset viewer.