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