[7d6351e] | 1 | """ |
---|
[76cd1ae] | 2 | CanSAS data reader - new recursive cansas_version. |
---|
[7d6351e] | 3 | """ |
---|
[0997158f] | 4 | ############################################################################ |
---|
| 5 | #This software was developed by the University of Tennessee as part of the |
---|
| 6 | #Distributed Data Analysis of Neutron Scattering Experiments (DANSE) |
---|
| 7 | #project funded by the US National Science Foundation. |
---|
| 8 | #If you use DANSE applications to do scientific research that leads to |
---|
| 9 | #publication, we ask that you acknowledge the use of the software with the |
---|
| 10 | #following sentence: |
---|
| 11 | #This work benefited from DANSE software developed under NSF award DMR-0520547. |
---|
| 12 | #copyright 2008,2009 University of Tennessee |
---|
| 13 | ############################################################################# |
---|
| 14 | |
---|
[8780e9a] | 15 | import logging |
---|
| 16 | import numpy |
---|
[a7a5886] | 17 | import os |
---|
| 18 | import sys |
---|
[9e2bc6c] | 19 | import datetime |
---|
[ac5b69d] | 20 | import inspect |
---|
| 21 | # For saving individual sections of data |
---|
[ad8034f] | 22 | from sans.dataloader.data_info import Data1D |
---|
| 23 | from sans.dataloader.data_info import Collimation |
---|
[76cd1ae] | 24 | from sans.dataloader.data_info import TransmissionSpectrum |
---|
[ad8034f] | 25 | from sans.dataloader.data_info import Detector |
---|
| 26 | from sans.dataloader.data_info import Process |
---|
| 27 | from sans.dataloader.data_info import Aperture |
---|
[ac5b69d] | 28 | # Both imports used. Do not remove either. |
---|
| 29 | from xml.dom.minidom import parseString |
---|
[76cd1ae] | 30 | import sans.dataloader.readers.xml_reader as xml_reader |
---|
[ac5b69d] | 31 | from sans.dataloader.readers.xml_reader import XMLreader |
---|
| 32 | from sans.dataloader.readers.cansas_constants import CansasConstants |
---|
[19b628f] | 33 | |
---|
[da96629] | 34 | _ZERO = 1e-16 |
---|
[ea67541] | 35 | PREPROCESS = "xmlpreprocess" |
---|
[2e3b055] | 36 | ENCODING = "encoding" |
---|
[5a0dac1f] | 37 | HAS_CONVERTER = True |
---|
[b39c817] | 38 | try: |
---|
[ffbe487] | 39 | from sans.data_util.nxsunit import Converter |
---|
[b39c817] | 40 | except: |
---|
[5a0dac1f] | 41 | HAS_CONVERTER = False |
---|
[76cd1ae] | 42 | |
---|
[ac5b69d] | 43 | CONSTANTS = CansasConstants() |
---|
| 44 | CANSAS_FORMAT = CONSTANTS.format |
---|
| 45 | CANSAS_NS = CONSTANTS.names |
---|
[5a0dac1f] | 46 | ALLOW_ALL = True |
---|
[b0d0723] | 47 | |
---|
[7d6351e] | 48 | |
---|
[ac5b69d] | 49 | # minidom used in functions called by outside classes |
---|
| 50 | import xml.dom.minidom |
---|
| 51 | # DO NOT REMOVE |
---|
| 52 | # Called by outside packages: |
---|
| 53 | # sans.perspectives.invariant.invariant_state |
---|
| 54 | # sans.perspectives.fitting.pagestate |
---|
[8780e9a] | 55 | def get_content(location, node): |
---|
| 56 | """ |
---|
[0997158f] | 57 | Get the first instance of the content of a xpath location. |
---|
| 58 | |
---|
| 59 | :param location: xpath location |
---|
| 60 | :param node: node to start at |
---|
| 61 | |
---|
| 62 | :return: Element, or None |
---|
[8780e9a] | 63 | """ |
---|
[ac5b69d] | 64 | nodes = node.xpath(location, |
---|
| 65 | namespaces={'ns': CANSAS_NS.get("1.0").get("ns")}) |
---|
[b0d0723] | 66 | |
---|
[7d6351e] | 67 | if len(nodes) > 0: |
---|
[b0d0723] | 68 | return nodes[0] |
---|
| 69 | else: |
---|
| 70 | return None |
---|
[8780e9a] | 71 | |
---|
[7d6351e] | 72 | |
---|
[ac5b69d] | 73 | # DO NOT REMOVE |
---|
| 74 | # Called by outside packages: |
---|
| 75 | # sans.perspectives.fitting.pagestate |
---|
[e090c624] | 76 | def write_node(doc, parent, name, value, attr={}): |
---|
| 77 | """ |
---|
| 78 | :param doc: document DOM |
---|
| 79 | :param parent: parent node |
---|
| 80 | :param name: tag of the element |
---|
| 81 | :param value: value of the child text node |
---|
| 82 | :param attr: attribute dictionary |
---|
| 83 | |
---|
| 84 | :return: True if something was appended, otherwise False |
---|
| 85 | """ |
---|
| 86 | if value is not None: |
---|
| 87 | node = doc.createElement(name) |
---|
| 88 | node.appendChild(doc.createTextNode(str(value))) |
---|
| 89 | for item in attr: |
---|
| 90 | node.setAttribute(item, attr[item]) |
---|
| 91 | parent.appendChild(node) |
---|
| 92 | return True |
---|
| 93 | return False |
---|
| 94 | |
---|
[19b628f] | 95 | |
---|
[ac5b69d] | 96 | class Reader(XMLreader): |
---|
[8780e9a] | 97 | """ |
---|
[0997158f] | 98 | Class to load cansas 1D XML files |
---|
| 99 | |
---|
| 100 | :Dependencies: |
---|
[19b628f] | 101 | The CanSAS reader requires PyXML 0.8.4 or later. |
---|
[8780e9a] | 102 | """ |
---|
[19b628f] | 103 | ##CanSAS version - defaults to version 1.0 |
---|
[76cd1ae] | 104 | cansas_version = "1.0" |
---|
[ac5b69d] | 105 | |
---|
| 106 | logging = [] |
---|
[19b628f] | 107 | errors = [] |
---|
| 108 | |
---|
| 109 | type_name = "canSAS" |
---|
[28caa03] | 110 | ## Wildcards |
---|
[ac5b69d] | 111 | type = ["XML files (*.xml)|*.xml", "SasView Save Files (*.svs)|*.svs"] |
---|
[8780e9a] | 112 | ## List of allowed extensions |
---|
[ac5b69d] | 113 | ext = ['.xml', '.XML', '.svs', '.SVS'] |
---|
[19b628f] | 114 | |
---|
| 115 | ## Flag to bypass extension check |
---|
| 116 | allow_all = True |
---|
[8780e9a] | 117 | |
---|
[fe78c7b] | 118 | def __init__(self): |
---|
| 119 | ## List of errors |
---|
| 120 | self.errors = [] |
---|
[19b628f] | 121 | |
---|
[ac5b69d] | 122 | def is_cansas(self, ext="xml"): |
---|
[19b628f] | 123 | """ |
---|
| 124 | Checks to see if the xml file is a CanSAS file |
---|
| 125 | """ |
---|
[ac5b69d] | 126 | if self.validate_xml(): |
---|
[2e3b055] | 127 | name = "{http://www.w3.org/2001/XMLSchema-instance}schemaLocation" |
---|
[ac5b69d] | 128 | value = self.xmlroot.get(name) |
---|
| 129 | if CANSAS_NS.get(self.cansas_version).get("ns") == \ |
---|
| 130 | value.rsplit(" ")[0]: |
---|
[19b628f] | 131 | return True |
---|
[ac5b69d] | 132 | if ext == "svs": |
---|
| 133 | return True |
---|
[19b628f] | 134 | return False |
---|
[fe78c7b] | 135 | |
---|
[19b628f] | 136 | def read(self, xml): |
---|
[7d6351e] | 137 | """ |
---|
[19b628f] | 138 | Validate and read in an xml file in the canSAS format. |
---|
[0997158f] | 139 | |
---|
[19b628f] | 140 | :param xml: A canSAS file path in proper XML format |
---|
[8780e9a] | 141 | """ |
---|
[19b628f] | 142 | # X - Q value; Y - Intensity (Abs) |
---|
[ac5b69d] | 143 | x_vals = numpy.empty(0) |
---|
| 144 | y_vals = numpy.empty(0) |
---|
| 145 | dx_vals = numpy.empty(0) |
---|
| 146 | dy_vals = numpy.empty(0) |
---|
[19b628f] | 147 | dxl = numpy.empty(0) |
---|
| 148 | dxw = numpy.empty(0) |
---|
| 149 | |
---|
| 150 | # output - Final list of Data1D objects |
---|
[8780e9a] | 151 | output = [] |
---|
[19b628f] | 152 | # ns - Namespace hierarchy for current xml object |
---|
[ac5b69d] | 153 | ns_list = [] |
---|
[19b628f] | 154 | |
---|
| 155 | # Check that the file exists |
---|
| 156 | if os.path.isfile(xml): |
---|
| 157 | basename = os.path.basename(xml) |
---|
| 158 | _, extension = os.path.splitext(basename) |
---|
[ac5b69d] | 159 | # If the file type is not allowed, return nothing |
---|
[19b628f] | 160 | if extension in self.ext or self.allow_all: |
---|
[ac5b69d] | 161 | # Get the file location of |
---|
[19b628f] | 162 | base_name = xml_reader.__file__ |
---|
[1ce36f37] | 163 | base_name = base_name.replace("\\","/") |
---|
| 164 | base = base_name.split("/sans/")[0] |
---|
[19b628f] | 165 | |
---|
[76cd1ae] | 166 | # Load in xml file and get the cansas version from the header |
---|
[ac5b69d] | 167 | self.set_xml_file(xml) |
---|
| 168 | root = self.xmlroot |
---|
[19b628f] | 169 | if root is None: |
---|
| 170 | root = {} |
---|
[76cd1ae] | 171 | self.cansas_version = root.get("version", "1.0") |
---|
[19b628f] | 172 | |
---|
| 173 | # Generic values for the cansas file based on the version |
---|
[76cd1ae] | 174 | cansas_defaults = CANSAS_NS.get(self.cansas_version, "1.0") |
---|
| 175 | schema_path = "{0}/sans/dataloader/readers/schema/{1}".format\ |
---|
| 176 | (base, cansas_defaults.get("schema")).replace("\\", "/") |
---|
[19b628f] | 177 | |
---|
| 178 | # Link a schema to the XML file. |
---|
[ac5b69d] | 179 | self.set_schema(schema_path) |
---|
[2e3b055] | 180 | |
---|
[19b628f] | 181 | # Try to load the file, but raise an error if unable to. |
---|
| 182 | # Check the file matches the XML schema |
---|
[0a5c8f5] | 183 | try: |
---|
[ac5b69d] | 184 | if self.is_cansas(extension): |
---|
[76cd1ae] | 185 | # Get each SASentry from XML file and add it to a list. |
---|
[19b628f] | 186 | entry_list = root.xpath('/ns:SASroot/ns:SASentry', |
---|
[76cd1ae] | 187 | namespaces={'ns': cansas_defaults.get("ns")}) |
---|
[ac5b69d] | 188 | ns_list.append("SASentry") |
---|
[0a5c8f5] | 189 | |
---|
[76cd1ae] | 190 | # If multiple files, modify the name for each is unique |
---|
| 191 | multiple_files = len(entry_list) - 1 |
---|
| 192 | increment = 0 |
---|
[19b628f] | 193 | name = basename |
---|
| 194 | # Parse each SASentry item |
---|
| 195 | for entry in entry_list: |
---|
[ac5b69d] | 196 | # Define a new Data1D object with zeroes for |
---|
| 197 | # x_vals and y_vals |
---|
| 198 | data1d = Data1D(x_vals, y_vals, dx_vals, dy_vals) |
---|
[76cd1ae] | 199 | data1d.dxl = dxl |
---|
| 200 | data1d.dxw = dxw |
---|
[19b628f] | 201 | |
---|
[76cd1ae] | 202 | # If more than one SASentry, increment each in order |
---|
| 203 | if multiple_files: |
---|
| 204 | name += "_{0}".format(increment) |
---|
| 205 | increment += 1 |
---|
[19b628f] | 206 | |
---|
[76cd1ae] | 207 | # Set the Data1D name and then parse the entry. |
---|
| 208 | # The entry is appended to a list of entry values |
---|
| 209 | data1d.filename = name |
---|
| 210 | data1d.meta_data["loader"] = "CanSAS 1D" |
---|
[ea67541] | 211 | |
---|
[2e3b055] | 212 | # Get all preprocessing events and encoding |
---|
[ac5b69d] | 213 | self.set_processing_instructions() |
---|
[ea67541] | 214 | data1d.meta_data[PREPROCESS] = \ |
---|
[ac5b69d] | 215 | self.processing_instructions |
---|
[ea67541] | 216 | |
---|
| 217 | # Parse the XML file |
---|
[76cd1ae] | 218 | return_value, extras = \ |
---|
[ac5b69d] | 219 | self._parse_entry(entry, ns_list, data1d) |
---|
[19b628f] | 220 | del extras[:] |
---|
| 221 | |
---|
[76cd1ae] | 222 | # Final cleanup |
---|
| 223 | # Remove empty nodes, verify array sizes are correct |
---|
[19b628f] | 224 | for error in self.errors: |
---|
| 225 | return_value.errors.append(error) |
---|
| 226 | del self.errors[:] |
---|
| 227 | numpy.trim_zeros(return_value.x) |
---|
| 228 | numpy.trim_zeros(return_value.y) |
---|
| 229 | numpy.trim_zeros(return_value.dy) |
---|
| 230 | size_dx = return_value.dx.size |
---|
| 231 | size_dxl = return_value.dxl.size |
---|
| 232 | size_dxw = return_value.dxw.size |
---|
| 233 | if size_dxl == 0 and size_dxw == 0: |
---|
| 234 | return_value.dxl = None |
---|
| 235 | return_value.dxw = None |
---|
| 236 | numpy.trim_zeros(return_value.dx) |
---|
| 237 | elif size_dx == 0: |
---|
| 238 | return_value.dx = None |
---|
| 239 | size_dx = size_dxl |
---|
| 240 | numpy.trim_zeros(return_value.dxl) |
---|
| 241 | numpy.trim_zeros(return_value.dxw) |
---|
| 242 | output.append(return_value) |
---|
| 243 | else: |
---|
[ac5b69d] | 244 | value = self.find_invalid_xml() |
---|
[19b628f] | 245 | output.append("Invalid XML at: {0}".format(value)) |
---|
[0a5c8f5] | 246 | except: |
---|
[19b628f] | 247 | # If the file does not match the schema, raise this error |
---|
[ac5b69d] | 248 | raise RuntimeError, "%s cannot be read" % xml |
---|
[19b628f] | 249 | return output |
---|
| 250 | # Return a list of parsed entries that dataloader can manage |
---|
| 251 | return None |
---|
| 252 | |
---|
[ac5b69d] | 253 | def _create_unique_key(self, dictionary, name, numb = 0): |
---|
[76cd1ae] | 254 | """ |
---|
| 255 | Create a unique key value for any dictionary to prevent overwriting |
---|
[ea67541] | 256 | Recurses until a unique key value is found. |
---|
[76cd1ae] | 257 | |
---|
| 258 | :param dictionary: A dictionary with any number of entries |
---|
| 259 | :param name: The index of the item to be added to dictionary |
---|
[ac5b69d] | 260 | :param numb: The number to be appended to the name, starts at 0 |
---|
[76cd1ae] | 261 | """ |
---|
[19b628f] | 262 | if dictionary.get(name) is not None: |
---|
[ac5b69d] | 263 | numb += 1 |
---|
[19b628f] | 264 | name = name.split("_")[0] |
---|
[ac5b69d] | 265 | name += "_{0}".format(numb) |
---|
| 266 | name = self._create_unique_key(dictionary, name, numb) |
---|
[19b628f] | 267 | return name |
---|
| 268 | |
---|
[76cd1ae] | 269 | |
---|
| 270 | def _unit_conversion(self, new_current_level, attr, data1d, \ |
---|
[92c8fec] | 271 | tagname, node_value, optional = True): |
---|
[76cd1ae] | 272 | """ |
---|
| 273 | A unit converter method used to convert the data included in the file |
---|
| 274 | to the default units listed in data_info |
---|
| 275 | |
---|
| 276 | :param new_current_level: cansas_constants level as returned by |
---|
[ac5b69d] | 277 | iterate_namespace |
---|
[76cd1ae] | 278 | :param attr: The attributes of the node |
---|
| 279 | :param data1d: Where the values will be saved |
---|
| 280 | :param node_value: The value of the current dom node |
---|
| 281 | :param optional: Boolean that says if the units are required |
---|
| 282 | """ |
---|
[19b628f] | 283 | value_unit = '' |
---|
| 284 | if 'unit' in attr and new_current_level.get('unit') is not None: |
---|
[8780e9a] | 285 | try: |
---|
[19b628f] | 286 | if isinstance(node_value, float) is False: |
---|
| 287 | exec("node_value = float({0})".format(node_value)) |
---|
| 288 | default_unit = None |
---|
| 289 | unitname = new_current_level.get("unit") |
---|
[76cd1ae] | 290 | exec "default_unit = data1d.{0}".format(unitname) |
---|
[19b628f] | 291 | local_unit = attr['unit'] |
---|
[76cd1ae] | 292 | if local_unit.lower() != default_unit.lower() and \ |
---|
| 293 | local_unit is not None and local_unit.lower() != "none" and\ |
---|
| 294 | default_unit is not None: |
---|
[19b628f] | 295 | if HAS_CONVERTER == True: |
---|
| 296 | try: |
---|
[ea67541] | 297 | ## Check local units - bad units raise KeyError |
---|
[92c8fec] | 298 | Converter(local_unit) |
---|
[19b628f] | 299 | data_conv_q = Converter(attr['unit']) |
---|
| 300 | value_unit = default_unit |
---|
[76cd1ae] | 301 | exec "node_value = data_conv_q(node_value, units=data1d.{0})".format(unitname) |
---|
[92c8fec] | 302 | except KeyError as e: |
---|
[19b628f] | 303 | err_msg = "CanSAS reader: could not convert " |
---|
[92c8fec] | 304 | err_msg += "{0} unit {1}; ".format(tagname, local_unit) |
---|
[76cd1ae] | 305 | intermediate = "err_msg += \"expecting [{1}] {2}\".format(data1d.{0}, sys.exc_info()[1])".format(unitname, "{0}", "{1}") |
---|
[19b628f] | 306 | exec intermediate |
---|
| 307 | self.errors.append(err_msg) |
---|
[92c8fec] | 308 | raise ValueError(err_msg) |
---|
| 309 | return |
---|
| 310 | except: |
---|
[ac5b69d] | 311 | err_msg = \ |
---|
| 312 | "CanSAS reader: could not convert the units" |
---|
[b65ae90] | 313 | self.errors.append(err_msg) |
---|
[92c8fec] | 314 | return |
---|
[19b628f] | 315 | else: |
---|
| 316 | value_unit = local_unit |
---|
| 317 | err_msg = "CanSAS reader: unrecognized %s unit [%s];"\ |
---|
| 318 | % (node_value, default_unit) |
---|
| 319 | err_msg += " expecting [%s]" % local_unit |
---|
| 320 | self.errors.append(err_msg) |
---|
[92c8fec] | 321 | raise ValueError, err_msg |
---|
| 322 | return |
---|
[19b628f] | 323 | else: |
---|
| 324 | value_unit = local_unit |
---|
[8780e9a] | 325 | except: |
---|
[19b628f] | 326 | err_msg = "CanSAS reader: could not convert " |
---|
| 327 | err_msg += "Q unit [%s]; " % attr['unit'], |
---|
[76cd1ae] | 328 | exec "err_msg += \"expecting [%s]\n %s\" % (data1d.{0}, sys.exc_info()[1])".format(unitname) |
---|
[19b628f] | 329 | self.errors.append(err_msg) |
---|
[92c8fec] | 330 | raise ValueError, err_msg |
---|
| 331 | return |
---|
[19b628f] | 332 | elif 'unit' in attr: |
---|
| 333 | value_unit = attr['unit'] |
---|
| 334 | node_value = "float({0})".format(node_value) |
---|
| 335 | return node_value, value_unit |
---|
| 336 | |
---|
[ac5b69d] | 337 | def _parse_entry(self, dom, names=["SASentry"], data1d=None, extras=[]): |
---|
[19b628f] | 338 | """ |
---|
| 339 | Parse a SASEntry - new recursive method for parsing the dom of |
---|
| 340 | the CanSAS data format. This will allow multiple data files |
---|
| 341 | and extra nodes to be read in simultaneously. |
---|
[8780e9a] | 342 | |
---|
[ac5b69d] | 343 | :param dom: dom object with a namespace base of names |
---|
| 344 | :param names: A list of element names that lead up to the dom object |
---|
[76cd1ae] | 345 | :param data1d: The data1d object that will be modified |
---|
| 346 | :param extras: Any values that should go into meta_data when data1d |
---|
| 347 | is not a Data1D object |
---|
[19b628f] | 348 | """ |
---|
[ac5b69d] | 349 | |
---|
[19b628f] | 350 | # A portion of every namespace entry |
---|
[ac5b69d] | 351 | if data1d == None: |
---|
| 352 | x_vals = numpy.empty(0) |
---|
| 353 | y_vals = numpy.empty(0) |
---|
| 354 | dx_vals = numpy.empty(0) |
---|
| 355 | dy_vals = numpy.empty(0) |
---|
| 356 | dxl = numpy.empty(0) |
---|
| 357 | dxw = numpy.empty(0) |
---|
| 358 | data1d = Data1D(x_vals, y_vals, dx_vals, dy_vals) |
---|
| 359 | data1d.dxl = dxl |
---|
| 360 | data1d.dxw = dxw |
---|
| 361 | |
---|
[76cd1ae] | 362 | base_ns = "{0}{1}{2}".format("{", \ |
---|
| 363 | CANSAS_NS.get(self.cansas_version).get("ns"), "}") |
---|
[19b628f] | 364 | unit = '' |
---|
[ea67541] | 365 | tagname = '' |
---|
| 366 | tagname_original = '' |
---|
[b0d0723] | 367 | |
---|
[19b628f] | 368 | # Go through each child in the parent element |
---|
| 369 | for node in dom: |
---|
[8780e9a] | 370 | try: |
---|
[ac5b69d] | 371 | # Get the element name and set the current names level |
---|
[19b628f] | 372 | tagname = node.tag.replace(base_ns, "") |
---|
| 373 | tagname_original = tagname |
---|
[92a2ecd] | 374 | if tagname == "fitting_plug_in" or tagname == "pr_inversion" or\ |
---|
| 375 | tagname == "invariant": |
---|
[ac5b69d] | 376 | continue |
---|
| 377 | names.append(tagname) |
---|
[19b628f] | 378 | attr = node.attrib |
---|
[e090c624] | 379 | children = node.getchildren() |
---|
[ac5b69d] | 380 | if len(children) == 0: |
---|
| 381 | children = None |
---|
[e090c624] | 382 | save_data1d = data1d |
---|
[579ba85] | 383 | |
---|
[19b628f] | 384 | # Look for special cases |
---|
| 385 | if tagname == "SASdetector": |
---|
[76cd1ae] | 386 | data1d = Detector() |
---|
[19b628f] | 387 | elif tagname == "SAScollimation": |
---|
[76cd1ae] | 388 | data1d = Collimation() |
---|
| 389 | elif tagname == "SAStransmission_spectrum": |
---|
| 390 | data1d = TransmissionSpectrum() |
---|
[19b628f] | 391 | elif tagname == "SASprocess": |
---|
[76cd1ae] | 392 | data1d = Process() |
---|
[19b628f] | 393 | for child in node: |
---|
| 394 | if child.tag.replace(base_ns, "") == "term": |
---|
| 395 | term_attr = {} |
---|
| 396 | for attr in child.keys(): |
---|
[76cd1ae] | 397 | term_attr[attr] = \ |
---|
| 398 | ' '.join(child.get(attr).split()) |
---|
[19b628f] | 399 | if child.text is not None: |
---|
[76cd1ae] | 400 | term_attr['value'] = \ |
---|
| 401 | ' '.join(child.text.split()) |
---|
| 402 | data1d.term.append(term_attr) |
---|
[19b628f] | 403 | elif tagname == "aperture": |
---|
[76cd1ae] | 404 | data1d = Aperture() |
---|
[e090c624] | 405 | if tagname == "Idata" and children is not None: |
---|
| 406 | dql = 0 |
---|
| 407 | dqw = 0 |
---|
| 408 | for child in children: |
---|
| 409 | tag = child.tag.replace(base_ns, "") |
---|
| 410 | if tag == "dQl": |
---|
| 411 | dql = 1 |
---|
| 412 | if tag == "dQw": |
---|
| 413 | dqw = 1 |
---|
| 414 | if dqw == 1 and dql == 0: |
---|
| 415 | data1d.dxl = numpy.append(data1d.dxl, 0.0) |
---|
| 416 | elif dql == 1 and dqw == 0: |
---|
| 417 | data1d.dxw = numpy.append(data1d.dxw, 0.0) |
---|
| 418 | |
---|
[19b628f] | 419 | # Get where to store content |
---|
[ac5b69d] | 420 | cs_values = CONSTANTS.iterate_namespace(names) |
---|
[19b628f] | 421 | # If the element is a child element, recurse |
---|
[e090c624] | 422 | if children is not None: |
---|
| 423 | # Returned value is new Data1D object with all previous and |
---|
| 424 | # new values in it. |
---|
[ac5b69d] | 425 | data1d, extras = self._parse_entry(node, |
---|
| 426 | names, data1d, extras) |
---|
[19b628f] | 427 | |
---|
| 428 | #Get the information from the node |
---|
| 429 | node_value = node.text |
---|
| 430 | if node_value == "": |
---|
| 431 | node_value = None |
---|
| 432 | if node_value is not None: |
---|
| 433 | node_value = ' '.join(node_value.split()) |
---|
[8780e9a] | 434 | |
---|
[19b628f] | 435 | # If the value is a float, compile with units. |
---|
[e090c624] | 436 | if cs_values.ns_datatype == "float": |
---|
[19b628f] | 437 | # If an empty value is given, store as zero. |
---|
[76cd1ae] | 438 | if node_value is None or node_value.isspace() \ |
---|
| 439 | or node_value.lower() == "nan": |
---|
[19b628f] | 440 | node_value = "0.0" |
---|
[b65ae90] | 441 | node_value, unit = self._unit_conversion(\ |
---|
[e090c624] | 442 | cs_values.current_level, attr, data1d, \ |
---|
[92c8fec] | 443 | tagname, node_value, cs_values.ns_optional) |
---|
[9e2bc6c] | 444 | # If the value is a timestamp, convert to a datetime object |
---|
| 445 | elif cs_values.ns_datatype == "timestamp": |
---|
| 446 | if node_value is None or node_value.isspace(): |
---|
| 447 | pass |
---|
| 448 | else: |
---|
| 449 | try: |
---|
| 450 | node_value = \ |
---|
| 451 | datetime.datetime.fromtimestamp(node_value) |
---|
| 452 | except ValueError: |
---|
| 453 | node_value = None |
---|
[e090c624] | 454 | # If appending to a dictionary (meta_data | run_name) |
---|
| 455 | # make sure the key is unique |
---|
| 456 | if cs_values.ns_variable == "{0}.meta_data[\"{2}\"] = \"{1}\"": |
---|
| 457 | # If we are within a Process, Detector, Collimation or |
---|
| 458 | # Aperture instance, pull out old data1d |
---|
| 459 | tagname = self._create_unique_key(data1d.meta_data, \ |
---|
| 460 | tagname, 0) |
---|
[76cd1ae] | 461 | if isinstance(data1d, Data1D) == False: |
---|
[e090c624] | 462 | store_me = cs_values.ns_variable.format("data1d", \ |
---|
| 463 | node_value, tagname) |
---|
[19b628f] | 464 | extras.append(store_me) |
---|
[e090c624] | 465 | cs_values.ns_variable = None |
---|
| 466 | if cs_values.ns_variable == "{0}.run_name[\"{2}\"] = \"{1}\"": |
---|
| 467 | tagname = self._create_unique_key(data1d.run_name, \ |
---|
| 468 | tagname, 0) |
---|
[8780e9a] | 469 | |
---|
[19b628f] | 470 | # Check for Data1D object and any extra commands to save |
---|
[76cd1ae] | 471 | if isinstance(data1d, Data1D): |
---|
[19b628f] | 472 | for item in extras: |
---|
| 473 | exec item |
---|
| 474 | # Don't bother saving empty information unless it is a float |
---|
[ea67541] | 475 | if cs_values.ns_variable is not None and \ |
---|
| 476 | node_value is not None and \ |
---|
[76cd1ae] | 477 | node_value.isspace() == False: |
---|
[19b628f] | 478 | # Format a string and then execute it. |
---|
[ea67541] | 479 | store_me = cs_values.ns_variable.format("data1d", \ |
---|
| 480 | node_value, tagname) |
---|
[19b628f] | 481 | exec store_me |
---|
| 482 | # Get attributes and process them |
---|
| 483 | if attr is not None: |
---|
| 484 | for key in node.keys(): |
---|
| 485 | try: |
---|
[e090c624] | 486 | cansas_attrib = \ |
---|
| 487 | cs_values.current_level.get("attributes").get(key) |
---|
[19b628f] | 488 | attrib_variable = cansas_attrib.get("variable") |
---|
| 489 | if key == 'unit' and unit != '': |
---|
| 490 | attrib_value = unit |
---|
| 491 | else: |
---|
| 492 | attrib_value = node.attrib[key] |
---|
[76cd1ae] | 493 | store_attr = attrib_variable.format("data1d", \ |
---|
| 494 | attrib_value, key) |
---|
[19b628f] | 495 | exec store_attr |
---|
| 496 | except AttributeError as e: |
---|
[7d6351e] | 497 | pass |
---|
[b65ae90] | 498 | |
---|
[ac5b69d] | 499 | except TypeError as e: |
---|
[b65ae90] | 500 | pass |
---|
[19b628f] | 501 | except Exception as e: |
---|
| 502 | exc_type, exc_obj, exc_tb = sys.exc_info() |
---|
| 503 | fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1] |
---|
| 504 | print(e, exc_type, fname, exc_tb.tb_lineno, tagname, exc_obj) |
---|
| 505 | finally: |
---|
[76cd1ae] | 506 | # Save special cases in original data1d object |
---|
| 507 | # then restore the data1d |
---|
[19b628f] | 508 | if tagname_original == "SASdetector": |
---|
[76cd1ae] | 509 | save_data1d.detector.append(data1d) |
---|
[19b628f] | 510 | elif tagname_original == "SAScollimation": |
---|
[76cd1ae] | 511 | save_data1d.collimation.append(data1d) |
---|
| 512 | elif tagname == "SAStransmission_spectrum": |
---|
| 513 | save_data1d.trans_spectrum.append(data1d) |
---|
[19b628f] | 514 | elif tagname_original == "SASprocess": |
---|
[76cd1ae] | 515 | save_data1d.process.append(data1d) |
---|
[19b628f] | 516 | elif tagname_original == "aperture": |
---|
[76cd1ae] | 517 | save_data1d.aperture.append(data1d) |
---|
[e390933] | 518 | else: |
---|
[76cd1ae] | 519 | save_data1d = data1d |
---|
[92a2ecd] | 520 | if tagname_original == "fitting_plug_in" or \ |
---|
| 521 | tagname_original == "invariant" or \ |
---|
| 522 | tagname_original == "pr_inversion": |
---|
| 523 | pass |
---|
| 524 | else: |
---|
[ac5b69d] | 525 | data1d = save_data1d |
---|
| 526 | # Remove tagname from names to restore original base |
---|
| 527 | names.remove(tagname_original) |
---|
[76cd1ae] | 528 | return data1d, extras |
---|
[d6513cd] | 529 | |
---|
[ac5b69d] | 530 | |
---|
[b3de3a45] | 531 | def _to_xml_doc(self, datainfo): |
---|
[4c00964] | 532 | """ |
---|
[0997158f] | 533 | Create an XML document to contain the content of a Data1D |
---|
| 534 | |
---|
| 535 | :param datainfo: Data1D object |
---|
[4c00964] | 536 | """ |
---|
[7d8094b] | 537 | if not issubclass(datainfo.__class__, Data1D): |
---|
[4c00964] | 538 | raise RuntimeError, "The cansas writer expects a Data1D instance" |
---|
| 539 | |
---|
[2e3b055] | 540 | # Get PIs and create root element |
---|
[ac5b69d] | 541 | pis = self.return_processing_instructions() |
---|
| 542 | if len(pis) > 0: |
---|
| 543 | pi_tree = self.create_tree(pis[0]) |
---|
| 544 | i = 1 |
---|
| 545 | for i in range(1,len(pis) - 1): |
---|
| 546 | pi_tree = self.append(pis[i], pi_tree) |
---|
| 547 | pi_string = self.to_string(pi_tree) |
---|
| 548 | else: |
---|
| 549 | pi_string = "" |
---|
[2e3b055] | 550 | |
---|
| 551 | # Define namespaces and create SASroot object |
---|
| 552 | xsi = "http://www.w3.org/2001/XMLSchema-instance" |
---|
| 553 | version = self.cansas_version |
---|
| 554 | ns = CANSAS_NS.get(version).get("ns") |
---|
| 555 | if version == "1.1": |
---|
| 556 | url = "http://www.cansas.org/formats/1.1/" |
---|
| 557 | else: |
---|
| 558 | url = "http://svn.smallangles.net/svn/canSAS/1dwg/trunk/" |
---|
[ac5b69d] | 559 | schema_location = "{0} {1}cansas1d.xsd".format(ns, url) |
---|
| 560 | attrib = {"{" + xsi + "}schemaLocation" : schema_location, |
---|
[2e3b055] | 561 | "version" : version} |
---|
| 562 | nsmap = {'xsi' : xsi, None: ns} |
---|
[c6ca23d] | 563 | |
---|
[ac5b69d] | 564 | main_node = self.create_element("{" + ns + "}SASroot", \ |
---|
[2e3b055] | 565 | attrib = attrib, \ |
---|
| 566 | nsmap = nsmap) |
---|
[ea67541] | 567 | |
---|
[2e3b055] | 568 | # Create ElementTree, append SASroot and apply processing instructions |
---|
[ac5b69d] | 569 | base_string = pi_string + self.to_string(main_node) |
---|
| 570 | base_element = self.create_element_from_string(base_string) |
---|
| 571 | doc = self.create_tree(base_element) |
---|
[fee780b] | 572 | |
---|
[2e3b055] | 573 | # Create SASentry Element |
---|
[ac5b69d] | 574 | entry_node = self.create_element("SASentry") |
---|
[2e3b055] | 575 | root = doc.getroot() |
---|
| 576 | root.append(entry_node) |
---|
[4c00964] | 577 | |
---|
[2e3b055] | 578 | # Add Title to SASentry |
---|
| 579 | self.write_node(entry_node, "Title", datainfo.title) |
---|
[4c00964] | 580 | |
---|
[2e3b055] | 581 | # Add Run to SASentry |
---|
[9e2bc6c] | 582 | if datainfo.run == None or datainfo.run == []: |
---|
| 583 | RUN_NAME_DEFAULT = "None" |
---|
| 584 | datainfo.run.append(RUN_NAME_DEFAULT) |
---|
| 585 | datainfo.run_name[RUN_NAME_DEFAULT] = RUN_NAME_DEFAULT |
---|
[579ba85] | 586 | for item in datainfo.run: |
---|
| 587 | runname = {} |
---|
[7d6351e] | 588 | if item in datainfo.run_name and \ |
---|
| 589 | len(str(datainfo.run_name[item])) > 1: |
---|
| 590 | runname = {'name': datainfo.run_name[item]} |
---|
[2e3b055] | 591 | self.write_node(entry_node, "Run", item, runname) |
---|
[4c00964] | 592 | |
---|
| 593 | # Data info |
---|
[ac5b69d] | 594 | node = self.create_element("SASdata") |
---|
| 595 | self.append(node, entry_node) |
---|
[4c00964] | 596 | |
---|
[579ba85] | 597 | for i in range(len(datainfo.x)): |
---|
[ac5b69d] | 598 | pt = self.create_element("Idata") |
---|
[2e3b055] | 599 | node.append(pt) |
---|
| 600 | self.write_node(pt, "Q", datainfo.x[i], {'unit': datainfo.x_unit}) |
---|
[7d6351e] | 601 | if len(datainfo.y) >= i: |
---|
[2e3b055] | 602 | self.write_node(pt, "I", datainfo.y[i], |
---|
[7d6351e] | 603 | {'unit': datainfo.y_unit}) |
---|
[76cd1ae] | 604 | if datainfo.dy != None and len(datainfo.dy) > i: |
---|
[2e3b055] | 605 | self.write_node(pt, "Idev", datainfo.dy[i], |
---|
[19b628f] | 606 | {'unit': datainfo.y_unit}) |
---|
[76cd1ae] | 607 | if datainfo.dx != None and len(datainfo.dx) > i: |
---|
[2e3b055] | 608 | self.write_node(pt, "Qdev", datainfo.dx[i], |
---|
[7d6351e] | 609 | {'unit': datainfo.x_unit}) |
---|
[76cd1ae] | 610 | if datainfo.dxw != None and len(datainfo.dxw) > i: |
---|
[2e3b055] | 611 | self.write_node(pt, "dQw", datainfo.dxw[i], |
---|
[7d6351e] | 612 | {'unit': datainfo.x_unit}) |
---|
[76cd1ae] | 613 | if datainfo.dxl != None and len(datainfo.dxl) > i: |
---|
[2e3b055] | 614 | self.write_node(pt, "dQl", datainfo.dxl[i], |
---|
[19b628f] | 615 | {'unit': datainfo.x_unit}) |
---|
[ac5b69d] | 616 | |
---|
[19b628f] | 617 | # Transmission Spectrum Info |
---|
[76cd1ae] | 618 | for i in range(len(datainfo.trans_spectrum)): |
---|
| 619 | spectrum = datainfo.trans_spectrum[i] |
---|
[ac5b69d] | 620 | node = self.create_element("SAStransmission_spectrum", |
---|
[2e3b055] | 621 | {"name" : spectrum.name}) |
---|
[ac5b69d] | 622 | self.append(node, entry_node) |
---|
[9e2bc6c] | 623 | if isinstance(spectrum.timestamp, datetime.datetime): |
---|
| 624 | node.setAttribute("timestamp", spectrum.timestamp) |
---|
[76cd1ae] | 625 | for i in range(len(spectrum.wavelength)): |
---|
[ac5b69d] | 626 | pt = self.create_element("Tdata") |
---|
[2e3b055] | 627 | node.append(pt) |
---|
| 628 | self.write_node(pt, "Lambda", spectrum.wavelength[i], |
---|
[76cd1ae] | 629 | {'unit': spectrum.wavelength_unit}) |
---|
[2e3b055] | 630 | self.write_node(pt, "T", spectrum.transmission[i], |
---|
[76cd1ae] | 631 | {'unit': spectrum.transmission_unit}) |
---|
| 632 | if spectrum.transmission_deviation != None \ |
---|
| 633 | and len(spectrum.transmission_deviation) >= i: |
---|
[2e3b055] | 634 | self.write_node(pt, "Tdev", \ |
---|
[76cd1ae] | 635 | spectrum.transmission_deviation[i], \ |
---|
| 636 | {'unit': spectrum.transmission_deviation_unit}) |
---|
[579ba85] | 637 | |
---|
[4c00964] | 638 | # Sample info |
---|
[ac5b69d] | 639 | sample = self.create_element("SASsample") |
---|
[579ba85] | 640 | if datainfo.sample.name is not None: |
---|
[ac5b69d] | 641 | self.write_attribute(sample, |
---|
[2e3b055] | 642 | "name", |
---|
| 643 | str(datainfo.sample.name)) |
---|
[ac5b69d] | 644 | self.append(sample, entry_node) |
---|
[2e3b055] | 645 | self.write_node(sample, "ID", str(datainfo.sample.ID)) |
---|
| 646 | self.write_node(sample, "thickness", datainfo.sample.thickness, |
---|
[7d6351e] | 647 | {"unit": datainfo.sample.thickness_unit}) |
---|
[2e3b055] | 648 | self.write_node(sample, "transmission", datainfo.sample.transmission) |
---|
| 649 | self.write_node(sample, "temperature", datainfo.sample.temperature, |
---|
[7d6351e] | 650 | {"unit": datainfo.sample.temperature_unit}) |
---|
[4c00964] | 651 | |
---|
[ac5b69d] | 652 | pos = self.create_element("position") |
---|
[2e3b055] | 653 | written = self.write_node(pos, |
---|
| 654 | "x", |
---|
| 655 | datainfo.sample.position.x, |
---|
| 656 | {"unit": datainfo.sample.position_unit}) |
---|
| 657 | written = written | self.write_node(pos, |
---|
| 658 | "y", |
---|
| 659 | datainfo.sample.position.y, |
---|
[7d6351e] | 660 | {"unit": datainfo.sample.position_unit}) |
---|
[2e3b055] | 661 | written = written | self.write_node(pos, |
---|
| 662 | "z", |
---|
| 663 | datainfo.sample.position.z, |
---|
[7d6351e] | 664 | {"unit": datainfo.sample.position_unit}) |
---|
[4c00964] | 665 | if written == True: |
---|
[ac5b69d] | 666 | self.append(pos, sample) |
---|
[4c00964] | 667 | |
---|
[ac5b69d] | 668 | ori = self.create_element("orientation") |
---|
[2e3b055] | 669 | written = self.write_node(ori, "roll", |
---|
| 670 | datainfo.sample.orientation.x, |
---|
| 671 | {"unit": datainfo.sample.orientation_unit}) |
---|
| 672 | written = written | self.write_node(ori, "pitch", |
---|
[a7a5886] | 673 | datainfo.sample.orientation.y, |
---|
[7d6351e] | 674 | {"unit": datainfo.sample.orientation_unit}) |
---|
[2e3b055] | 675 | written = written | self.write_node(ori, "yaw", |
---|
[a7a5886] | 676 | datainfo.sample.orientation.z, |
---|
[7d6351e] | 677 | {"unit": datainfo.sample.orientation_unit}) |
---|
[4c00964] | 678 | if written == True: |
---|
[ac5b69d] | 679 | self.append(ori, sample) |
---|
[4c00964] | 680 | |
---|
[19b628f] | 681 | for item in datainfo.sample.details: |
---|
[2e3b055] | 682 | self.write_node(sample, "details", item) |
---|
[19b628f] | 683 | |
---|
[4c00964] | 684 | # Instrument info |
---|
[ac5b69d] | 685 | instr = self.create_element("SASinstrument") |
---|
| 686 | self.append(instr, entry_node) |
---|
[4c00964] | 687 | |
---|
[2e3b055] | 688 | self.write_node(instr, "name", datainfo.instrument) |
---|
[4c00964] | 689 | |
---|
| 690 | # Source |
---|
[ac5b69d] | 691 | source = self.create_element("SASsource") |
---|
[579ba85] | 692 | if datainfo.source.name is not None: |
---|
[ac5b69d] | 693 | self.write_attribute(source, |
---|
[2e3b055] | 694 | "name", |
---|
| 695 | str(datainfo.source.name)) |
---|
[ac5b69d] | 696 | self.append(source, instr) |
---|
[9e2bc6c] | 697 | if datainfo.source.radiation == None or datainfo.source.radiation == '': |
---|
| 698 | datainfo.source.radiation = "neutron" |
---|
[2e3b055] | 699 | self.write_node(source, "radiation", datainfo.source.radiation) |
---|
[19b628f] | 700 | |
---|
[ac5b69d] | 701 | size = self.create_element("beam_size") |
---|
[579ba85] | 702 | if datainfo.source.beam_size_name is not None: |
---|
[ac5b69d] | 703 | self.write_attribute(size, |
---|
[2e3b055] | 704 | "name", |
---|
| 705 | str(datainfo.source.beam_size_name)) |
---|
| 706 | written = self.write_node(size, "x", datainfo.source.beam_size.x, |
---|
[7d6351e] | 707 | {"unit": datainfo.source.beam_size_unit}) |
---|
[2e3b055] | 708 | written = written | self.write_node(size, "y", |
---|
[a7a5886] | 709 | datainfo.source.beam_size.y, |
---|
[7d6351e] | 710 | {"unit": datainfo.source.beam_size_unit}) |
---|
[2e3b055] | 711 | written = written | self.write_node(size, "z", |
---|
[a7a5886] | 712 | datainfo.source.beam_size.z, |
---|
[7d6351e] | 713 | {"unit": datainfo.source.beam_size_unit}) |
---|
[579ba85] | 714 | if written == True: |
---|
[ac5b69d] | 715 | self.append(size, source) |
---|
[579ba85] | 716 | |
---|
[2e3b055] | 717 | self.write_node(source, "beam_shape", datainfo.source.beam_shape) |
---|
| 718 | self.write_node(source, "wavelength", |
---|
[a7a5886] | 719 | datainfo.source.wavelength, |
---|
[7d6351e] | 720 | {"unit": datainfo.source.wavelength_unit}) |
---|
[2e3b055] | 721 | self.write_node(source, "wavelength_min", |
---|
[a7a5886] | 722 | datainfo.source.wavelength_min, |
---|
[7d6351e] | 723 | {"unit": datainfo.source.wavelength_min_unit}) |
---|
[2e3b055] | 724 | self.write_node(source, "wavelength_max", |
---|
[a7a5886] | 725 | datainfo.source.wavelength_max, |
---|
[7d6351e] | 726 | {"unit": datainfo.source.wavelength_max_unit}) |
---|
[2e3b055] | 727 | self.write_node(source, "wavelength_spread", |
---|
[a7a5886] | 728 | datainfo.source.wavelength_spread, |
---|
[7d6351e] | 729 | {"unit": datainfo.source.wavelength_spread_unit}) |
---|
[4c00964] | 730 | |
---|
| 731 | # Collimation |
---|
[9e2bc6c] | 732 | if datainfo.collimation == [] or datainfo.collimation == None: |
---|
| 733 | coll = Collimation() |
---|
| 734 | datainfo.collimation.append(coll) |
---|
[4c00964] | 735 | for item in datainfo.collimation: |
---|
[ac5b69d] | 736 | coll = self.create_element("SAScollimation") |
---|
[579ba85] | 737 | if item.name is not None: |
---|
[ac5b69d] | 738 | self.write_attribute(coll, "name", str(item.name)) |
---|
| 739 | self.append(coll, instr) |
---|
[4c00964] | 740 | |
---|
[2e3b055] | 741 | self.write_node(coll, "length", item.length, |
---|
[7d6351e] | 742 | {"unit": item.length_unit}) |
---|
[4c00964] | 743 | |
---|
| 744 | for apert in item.aperture: |
---|
[ac5b69d] | 745 | ap = self.create_element("aperture") |
---|
[579ba85] | 746 | if apert.name is not None: |
---|
[ac5b69d] | 747 | self.write_attribute(ap, "name", str(apert.name)) |
---|
[579ba85] | 748 | if apert.type is not None: |
---|
[ac5b69d] | 749 | self.write_attribute(ap, "type", str(apert.type)) |
---|
| 750 | self.append(ap, coll) |
---|
[4c00964] | 751 | |
---|
[ac5b69d] | 752 | size = self.create_element("size") |
---|
[579ba85] | 753 | if apert.size_name is not None: |
---|
[ac5b69d] | 754 | self.write_attribute(size, |
---|
[2e3b055] | 755 | "name", |
---|
| 756 | str(apert.size_name)) |
---|
| 757 | written = self.write_node(size, "x", apert.size.x, |
---|
[7d6351e] | 758 | {"unit": apert.size_unit}) |
---|
[2e3b055] | 759 | written = written | self.write_node(size, "y", apert.size.y, |
---|
[7d6351e] | 760 | {"unit": apert.size_unit}) |
---|
[2e3b055] | 761 | written = written | self.write_node(size, "z", apert.size.z, |
---|
[7d6351e] | 762 | {"unit": apert.size_unit}) |
---|
[579ba85] | 763 | if written == True: |
---|
[ac5b69d] | 764 | self.append(size, ap) |
---|
[19b628f] | 765 | |
---|
[2e3b055] | 766 | self.write_node(ap, "distance", apert.distance, |
---|
[19b628f] | 767 | {"unit": apert.distance_unit}) |
---|
[4c00964] | 768 | |
---|
| 769 | # Detectors |
---|
[9e2bc6c] | 770 | if datainfo.detector == None or datainfo.detector == []: |
---|
| 771 | det = Detector() |
---|
| 772 | det.name = "" |
---|
| 773 | datainfo.detector.append(det) |
---|
| 774 | |
---|
[4c00964] | 775 | for item in datainfo.detector: |
---|
[ac5b69d] | 776 | det = self.create_element("SASdetector") |
---|
[2e3b055] | 777 | written = self.write_node(det, "name", item.name) |
---|
| 778 | written = written | self.write_node(det, "SDD", item.distance, |
---|
[7d6351e] | 779 | {"unit": item.distance_unit}) |
---|
[579ba85] | 780 | if written == True: |
---|
[ac5b69d] | 781 | self.append(det, instr) |
---|
[4c00964] | 782 | |
---|
[ac5b69d] | 783 | off = self.create_element("offset") |
---|
[2e3b055] | 784 | written = self.write_node(off, "x", item.offset.x, |
---|
[7d6351e] | 785 | {"unit": item.offset_unit}) |
---|
[2e3b055] | 786 | written = written | self.write_node(off, "y", item.offset.y, |
---|
[7d6351e] | 787 | {"unit": item.offset_unit}) |
---|
[2e3b055] | 788 | written = written | self.write_node(off, "z", item.offset.z, |
---|
[7d6351e] | 789 | {"unit": item.offset_unit}) |
---|
[579ba85] | 790 | if written == True: |
---|
[ac5b69d] | 791 | self.append(off, det) |
---|
[19b628f] | 792 | |
---|
[ac5b69d] | 793 | ori = self.create_element("orientation") |
---|
[2e3b055] | 794 | written = self.write_node(ori, "roll", item.orientation.x, |
---|
[19b628f] | 795 | {"unit": item.orientation_unit}) |
---|
[2e3b055] | 796 | written = written | self.write_node(ori, "pitch", |
---|
[19b628f] | 797 | item.orientation.y, |
---|
| 798 | {"unit": item.orientation_unit}) |
---|
[2e3b055] | 799 | written = written | self.write_node(ori, "yaw", |
---|
[19b628f] | 800 | item.orientation.z, |
---|
| 801 | {"unit": item.orientation_unit}) |
---|
| 802 | if written == True: |
---|
[ac5b69d] | 803 | self.append(ori, det) |
---|
[4c00964] | 804 | |
---|
[ac5b69d] | 805 | center = self.create_element("beam_center") |
---|
[2e3b055] | 806 | written = self.write_node(center, "x", item.beam_center.x, |
---|
[7d6351e] | 807 | {"unit": item.beam_center_unit}) |
---|
[2e3b055] | 808 | written = written | self.write_node(center, "y", |
---|
[a7a5886] | 809 | item.beam_center.y, |
---|
[7d6351e] | 810 | {"unit": item.beam_center_unit}) |
---|
[2e3b055] | 811 | written = written | self.write_node(center, "z", |
---|
[a7a5886] | 812 | item.beam_center.z, |
---|
[7d6351e] | 813 | {"unit": item.beam_center_unit}) |
---|
[579ba85] | 814 | if written == True: |
---|
[ac5b69d] | 815 | self.append(center, det) |
---|
[579ba85] | 816 | |
---|
[ac5b69d] | 817 | pix = self.create_element("pixel_size") |
---|
[2e3b055] | 818 | written = self.write_node(pix, "x", item.pixel_size.x, |
---|
[7d6351e] | 819 | {"unit": item.pixel_size_unit}) |
---|
[2e3b055] | 820 | written = written | self.write_node(pix, "y", item.pixel_size.y, |
---|
[7d6351e] | 821 | {"unit": item.pixel_size_unit}) |
---|
[2e3b055] | 822 | written = written | self.write_node(pix, "z", item.pixel_size.z, |
---|
[7d6351e] | 823 | {"unit": item.pixel_size_unit}) |
---|
[2e3b055] | 824 | written = written | self.write_node(det, "slit_length", |
---|
[19b628f] | 825 | item.slit_length, |
---|
| 826 | {"unit": item.slit_length_unit}) |
---|
[2e3b055] | 827 | if written == True: |
---|
[ac5b69d] | 828 | self.append(pix, det) |
---|
[19b628f] | 829 | |
---|
[579ba85] | 830 | # Processes info |
---|
[4c00964] | 831 | for item in datainfo.process: |
---|
[ac5b69d] | 832 | node = self.create_element("SASprocess") |
---|
| 833 | self.append(node, entry_node) |
---|
[4c00964] | 834 | |
---|
[2e3b055] | 835 | self.write_node(node, "name", item.name) |
---|
| 836 | self.write_node(node, "date", item.date) |
---|
| 837 | self.write_node(node, "description", item.description) |
---|
[579ba85] | 838 | for term in item.term: |
---|
| 839 | value = term['value'] |
---|
| 840 | del term['value'] |
---|
[2e3b055] | 841 | self.write_node(node, "term", value, term) |
---|
[579ba85] | 842 | for note in item.notes: |
---|
[2e3b055] | 843 | self.write_node(node, "SASprocessnote", note) |
---|
[19b628f] | 844 | if len(item.notes) == 0: |
---|
[2e3b055] | 845 | self.write_node(node, "SASprocessnote", "") |
---|
[19b628f] | 846 | |
---|
| 847 | # Note info |
---|
| 848 | if len(datainfo.notes) == 0: |
---|
[ac5b69d] | 849 | node = self.create_element("SASnote") |
---|
| 850 | self.append(node, entry_node) |
---|
[19b628f] | 851 | else: |
---|
| 852 | for item in datainfo.notes: |
---|
[ac5b69d] | 853 | node = self.create_element("SASnote") |
---|
| 854 | self.write_text(node, item) |
---|
| 855 | self.append(node, entry_node) |
---|
[19b628f] | 856 | |
---|
[2e3b055] | 857 | |
---|
[b3de3a45] | 858 | # Return the document, and the SASentry node associated with |
---|
[a3b635b] | 859 | # the data we just wrote |
---|
| 860 | # If the calling function was not the cansas reader, return a minidom |
---|
[bb1b892] | 861 | # object rather than an lxml object. |
---|
[ac5b69d] | 862 | frm = inspect.stack()[1] |
---|
[bb1b892] | 863 | mod_name = frm[1].replace("\\", "/").replace(".pyc", "") |
---|
[a3b635b] | 864 | mod_name = mod_name.replace(".py", "") |
---|
[51af54b] | 865 | mod = mod_name.split("sans/") |
---|
[bb1b892] | 866 | mod_name = mod[1] |
---|
[51af54b] | 867 | if mod_name != "dataloader/readers/cansas_reader": |
---|
[a3b635b] | 868 | string = self.to_string(doc, pp=False) |
---|
[ac5b69d] | 869 | doc = parseString(string) |
---|
| 870 | node_name = entry_node.tag |
---|
| 871 | node_list = doc.getElementsByTagName(node_name) |
---|
| 872 | entry_node = node_list.item(0) |
---|
[a3b635b] | 873 | |
---|
[b3de3a45] | 874 | return doc, entry_node |
---|
[2e3b055] | 875 | |
---|
| 876 | |
---|
[ac5b69d] | 877 | def write_node(self, parent, name, value, attr=None): |
---|
[2e3b055] | 878 | """ |
---|
| 879 | :param doc: document DOM |
---|
| 880 | :param parent: parent node |
---|
| 881 | :param name: tag of the element |
---|
| 882 | :param value: value of the child text node |
---|
| 883 | :param attr: attribute dictionary |
---|
| 884 | |
---|
| 885 | :return: True if something was appended, otherwise False |
---|
| 886 | """ |
---|
| 887 | if value is not None: |
---|
[ac5b69d] | 888 | parent = self.ebuilder(parent, name, value, attr) |
---|
[2e3b055] | 889 | return True |
---|
| 890 | return False |
---|
| 891 | |
---|
[b3de3a45] | 892 | |
---|
| 893 | def write(self, filename, datainfo): |
---|
| 894 | """ |
---|
[0997158f] | 895 | Write the content of a Data1D as a CanSAS XML file |
---|
| 896 | |
---|
| 897 | :param filename: name of the file to write |
---|
| 898 | :param datainfo: Data1D object |
---|
[b3de3a45] | 899 | """ |
---|
| 900 | # Create XML document |
---|
[7d6351e] | 901 | doc, _ = self._to_xml_doc(datainfo) |
---|
[4c00964] | 902 | # Write the file |
---|
| 903 | fd = open(filename, 'w') |
---|
[ac5b69d] | 904 | if self.encoding == None: |
---|
| 905 | self.encoding = "UTF-8" |
---|
| 906 | doc.write(fd, encoding=self.encoding, |
---|
[2e3b055] | 907 | pretty_print=True, xml_declaration=True) |
---|
[4c00964] | 908 | fd.close() |
---|
[ea67541] | 909 | |
---|
[ac5b69d] | 910 | |
---|
| 911 | # DO NOT REMOVE - used in saving and loading panel states. |
---|
| 912 | def _store_float(self, location, node, variable, storage, optional=True): |
---|
| 913 | """ |
---|
| 914 | Get the content of a xpath location and store |
---|
| 915 | the result. Check that the units are compatible |
---|
| 916 | with the destination. The value is expected to |
---|
| 917 | be a float. |
---|
| 918 | |
---|
| 919 | The xpath location might or might not exist. |
---|
| 920 | If it does not exist, nothing is done |
---|
| 921 | |
---|
| 922 | :param location: xpath location to fetch |
---|
| 923 | :param node: node to read the data from |
---|
| 924 | :param variable: name of the data member to store it in [string] |
---|
| 925 | :param storage: data object that has the 'variable' data member |
---|
| 926 | :param optional: if True, no exception will be raised |
---|
| 927 | if unit conversion can't be done |
---|
| 928 | |
---|
| 929 | :raise ValueError: raised when the units are not recognized |
---|
| 930 | """ |
---|
| 931 | entry = get_content(location, node) |
---|
| 932 | try: |
---|
| 933 | value = float(entry.text) |
---|
| 934 | except: |
---|
| 935 | value = None |
---|
| 936 | |
---|
| 937 | if value is not None: |
---|
| 938 | # If the entry has units, check to see that they are |
---|
| 939 | # compatible with what we currently have in the data object |
---|
| 940 | units = entry.get('unit') |
---|
| 941 | if units is not None: |
---|
| 942 | toks = variable.split('.') |
---|
| 943 | local_unit = None |
---|
| 944 | exec "local_unit = storage.%s_unit" % toks[0] |
---|
| 945 | if local_unit != None and units.lower() != local_unit.lower(): |
---|
| 946 | if HAS_CONVERTER == True: |
---|
| 947 | try: |
---|
| 948 | conv = Converter(units) |
---|
| 949 | exec "storage.%s = %g" % (variable, |
---|
| 950 | conv(value, units=local_unit)) |
---|
| 951 | except: |
---|
[a3b635b] | 952 | exc_type, exc_value, exc_traceback = sys.exc_info() |
---|
[ac5b69d] | 953 | err_mess = "CanSAS reader: could not convert" |
---|
| 954 | err_mess += " %s unit [%s]; expecting [%s]\n %s" \ |
---|
[a3b635b] | 955 | % (variable, units, local_unit, exc_value) |
---|
[ac5b69d] | 956 | self.errors.append(err_mess) |
---|
| 957 | if optional: |
---|
| 958 | logging.info(err_mess) |
---|
| 959 | else: |
---|
| 960 | raise ValueError, err_mess |
---|
| 961 | else: |
---|
| 962 | err_mess = "CanSAS reader: unrecognized %s unit [%s];"\ |
---|
| 963 | % (variable, units) |
---|
| 964 | err_mess += " expecting [%s]" % local_unit |
---|
| 965 | self.errors.append(err_mess) |
---|
| 966 | if optional: |
---|
| 967 | logging.info(err_mess) |
---|
| 968 | else: |
---|
| 969 | raise ValueError, err_mess |
---|
| 970 | else: |
---|
| 971 | exec "storage.%s = value" % variable |
---|
| 972 | else: |
---|
| 973 | exec "storage.%s = value" % variable |
---|
| 974 | |
---|
| 975 | |
---|
| 976 | # DO NOT REMOVE - used in saving and loading panel states. |
---|
| 977 | def _store_content(self, location, node, variable, storage): |
---|
| 978 | """ |
---|
| 979 | Get the content of a xpath location and store |
---|
| 980 | the result. The value is treated as a string. |
---|
| 981 | |
---|
| 982 | The xpath location might or might not exist. |
---|
| 983 | If it does not exist, nothing is done |
---|
| 984 | |
---|
| 985 | :param location: xpath location to fetch |
---|
| 986 | :param node: node to read the data from |
---|
| 987 | :param variable: name of the data member to store it in [string] |
---|
| 988 | :param storage: data object that has the 'variable' data member |
---|
| 989 | |
---|
| 990 | :return: return a list of errors |
---|
| 991 | """ |
---|
| 992 | entry = get_content(location, node) |
---|
| 993 | if entry is not None and entry.text is not None: |
---|
| 994 | exec "storage.%s = entry.text.strip()" % variable |
---|