source: sasview/src/sas/dataloader/readers/cansas_reader.py @ 682c432

ESS_GUIESS_GUI_DocsESS_GUI_batch_fittingESS_GUI_bumps_abstractionESS_GUI_iss1116ESS_GUI_iss879ESS_GUI_iss959ESS_GUI_openclESS_GUI_orderingESS_GUI_sync_sascalccostrafo411magnetic_scattrelease-4.1.1release-4.1.2release-4.2.2release_4.0.1ticket-1009ticket-1094-headlessticket-1242-2d-resolutionticket-1243ticket-1249ticket885unittest-saveload
Last change on this file since 682c432 was 682c432, checked in by krzywon, 9 years ago

Fixed the focus bug associated with #275 where focus wasn't being set on
the fitting pages. Made some cahnges to the cansas reader to be more
explicit and (attempt to) lower the pylint scores.

  • Property mode set to 100644
File size: 49.0 KB
Line 
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#############################################################################
14
15import logging
16import numpy
17import os
18import sys
19import datetime
20import inspect
21# For saving individual sections of data
22from sas.dataloader.data_info import Data1D
23from sas.dataloader.data_info import Collimation
24from sas.dataloader.data_info import TransmissionSpectrum
25from sas.dataloader.data_info import Detector
26from sas.dataloader.data_info import Process
27from sas.dataloader.data_info import Aperture
28import sas.dataloader.readers.xml_reader as xml_reader
29from sas.dataloader.readers.xml_reader import XMLreader
30from sas.dataloader.readers.cansas_constants import CansasConstants
31
32# The following 2 imports *ARE* used. Do not remove either.
33import xml.dom.minidom
34from xml.dom.minidom import parseString
35
36_ZERO = 1e-16
37PREPROCESS = "xmlpreprocess"
38ENCODING = "encoding"
39RUN_NAME_DEFAULT = "None"
40HAS_CONVERTER = True
41try:
42    from sas.data_util.nxsunit import Converter
43except ImportError:
44    HAS_CONVERTER = False
45
46CONSTANTS = CansasConstants()
47CANSAS_FORMAT = CONSTANTS.format
48CANSAS_NS = CONSTANTS.names
49ALLOW_ALL = True
50
51# DO NOT REMOVE Called by outside packages:
52#    sas.perspectives.invariant.invariant_state
53#    sas.perspectives.fitting.pagestate
54def 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
70# DO NOT REMOVE Called by outside packages:
71#    sas.perspectives.fitting.pagestate
72def 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
93class 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")
151        schema_path = "{0}/sas/dataloader/readers/schema/{1}".format\
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:
223                        output.append("Invalid XML at: {0}".format(\
224                                                    self.find_invalid_xml()))
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
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
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
296        default_unit = None
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
311                        node_value = data_conv_q(node_value, units=default_unit)
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)
321                err_msg += "expecting [{0}]".format(default_unit)
322                value_unit = local_unit
323            except:
324                print sys.exc_info()
325                err_msg = "CanSAS reader: unknown error converting "
326                err_msg += "\"{0}\" unit [{1}]"
327                err_msg = err_msg.format(tagname, local_unit)
328                value_unit = local_unit
329        elif 'unit' in attr:
330            value_unit = attr['unit']
331        if err_msg:
332            self.errors.add(err_msg)
333        node_value = "float({0})".format(node_value)
334        return node_value, value_unit
335
336    def _check_for_empty_data(self, data1d):
337        """
338        Creates an empty data set if no data is passed to the reader
339
340        :param data1d: presumably a Data1D object
341        """
342        if data1d == None:
343            self.errors = set()
344            x_vals = numpy.empty(0)
345            y_vals = numpy.empty(0)
346            dx_vals = numpy.empty(0)
347            dy_vals = numpy.empty(0)
348            dxl = numpy.empty(0)
349            dxw = numpy.empty(0)
350            data1d = Data1D(x_vals, y_vals, dx_vals, dy_vals)
351            data1d.dxl = dxl
352            data1d.dxw = dxw
353        return data1d
354
355    def _handle_special_cases(self, tagname, data1d, children):
356        """
357        Handle cases where the data type in Data1D is a dictionary or list
358
359        :param tagname: XML tagname in use
360        :param data1d: The original Data1D object
361        :param children: Child nodes of node
362        :param node: existing node with tag name 'tagname'
363        """
364        if tagname == "SASdetector":
365            data1d = Detector()
366        elif tagname == "SAScollimation":
367            data1d = Collimation()
368        elif tagname == "SAStransmission_spectrum":
369            data1d = TransmissionSpectrum()
370        elif tagname == "SASprocess":
371            data1d = Process()
372            for child in children:
373                if child.tag.replace(self.base_ns, "") == "term":
374                    term_attr = {}
375                    for attr in child.keys():
376                        term_attr[attr] = \
377                            ' '.join(child.get(attr).split())
378                    if child.text is not None:
379                        term_attr['value'] = \
380                            ' '.join(child.text.split())
381                    data1d.term.append(term_attr)
382        elif tagname == "aperture":
383            data1d = Aperture()
384        if tagname == "Idata" and children is not None:
385            data1d = self._check_for_empty_resolution(data1d, children)
386        return data1d
387
388    def _check_for_empty_resolution(self, data1d, children):
389        """
390        A method to check all resolution data sets are the same size as I and Q
391        """
392        dql_exists = False
393        dqw_exists = False
394        dq_exists = False
395        di_exists = False
396        for child in children:
397            tag = child.tag.replace(self.base_ns, "")
398            if tag == "dQl":
399                dql_exists = True
400            if tag == "dQw":
401                dqw_exists = True
402            if tag == "Qdev":
403                dq_exists = True
404            if tag == "Idev":
405                di_exists = True
406        if dqw_exists and dql_exists == False:
407            data1d.dxl = numpy.append(data1d.dxl, 0.0)
408        elif dql_exists and dqw_exists == False:
409            data1d.dxw = numpy.append(data1d.dxw, 0.0)
410        elif dql_exists == False and dqw_exists == False \
411                                            and dq_exists == False:
412            data1d.dx = numpy.append(data1d.dx, 0.0)
413        if di_exists == False:
414            data1d.dy = numpy.append(data1d.dy, 0.0)
415        return data1d
416
417    def _restore_original_case(self,
418                               tagname_original,
419                               tagname,
420                               save_data1d,
421                               data1d):
422        """
423        Save the special case data to the appropriate location and restore
424        the original Data1D object
425
426        :param tagname_original: Unmodified tagname for the node
427        :param tagname: modified tagname for the node
428        :param save_data1d: The original Data1D object
429        :param data1d: If a special case was handled, an object of that type
430        """
431        if tagname_original == "SASdetector":
432            save_data1d.detector.append(data1d)
433        elif tagname_original == "SAScollimation":
434            save_data1d.collimation.append(data1d)
435        elif tagname == "SAStransmission_spectrum":
436            save_data1d.trans_spectrum.append(data1d)
437        elif tagname_original == "SASprocess":
438            save_data1d.process.append(data1d)
439        elif tagname_original == "aperture":
440            save_data1d.aperture.append(data1d)
441        else:
442            save_data1d = data1d
443        return save_data1d
444
445    def _handle_attributes(self, node, data1d, cs_values, tagname):
446        """
447        Process all of the attributes for a node
448        """
449        attr = node.attrib
450        if attr is not None:
451            for key in node.keys():
452                try:
453                    node_value, unit = self._get_node_value(node, cs_values, \
454                                                   data1d, tagname)
455                    cansas_attrib = \
456                        cs_values.current_level.get("attributes").get(key)
457                    attrib_variable = cansas_attrib.get("variable")
458                    if key == 'unit' and unit != '':
459                        attrib_value = unit
460                    else:
461                        attrib_value = node.attrib[key]
462                    store_attr = attrib_variable.format("data1d",
463                                                        attrib_value,
464                                                        key,
465                                                        node_value)
466                    exec store_attr
467                except AttributeError:
468                    pass
469        return data1d
470
471    def _get_node_value(self, node, cs_values, data1d, tagname):
472        """
473        Get the value of a node and any applicable units
474
475        :param node: The XML node to get the value of
476        :param cs_values: A CansasConstants.CurrentLevel object
477        :param attr: The node attributes
478        :param dataid: The working object to be modified
479        :param tagname: The tagname of the node
480        """
481        #Get the text from the node and convert all whitespace to spaces
482        units = ''
483        node_value = node.text
484        if node_value == "":
485            node_value = None
486        if node_value is not None:
487            node_value = ' '.join(node_value.split())
488
489        # If the value is a float, compile with units.
490        if cs_values.ns_datatype == "float":
491            # If an empty value is given, set as zero.
492            if node_value is None or node_value.isspace() \
493                                    or node_value.lower() == "nan":
494                node_value = "0.0"
495            #Convert the value to the base units
496            node_value, units = self._unit_conversion(node, \
497                        cs_values.current_level, data1d, tagname, node_value)
498
499        # If the value is a timestamp, convert to a datetime object
500        elif cs_values.ns_datatype == "timestamp":
501            if node_value is None or node_value.isspace():
502                pass
503            else:
504                try:
505                    node_value = \
506                        datetime.datetime.fromtimestamp(node_value)
507                except ValueError:
508                    node_value = None
509        return node_value, units
510
511    def _parse_entry(self, dom, names=None, data1d=None, extras=None):
512        """
513        Parse a SASEntry - new recursive method for parsing the dom of
514            the CanSAS data format. This will allow multiple data files
515            and extra nodes to be read in simultaneously.
516
517        :param dom: dom object with a namespace base of names
518        :param names: A list of element names that lead up to the dom object
519        :param data1d: The data1d object that will be modified
520        :param extras: Any values that should go into meta_data when data1d
521            is not a Data1D object
522        """
523
524        if extras is None:
525            extras = []
526        if names is None or names == []:
527            names = ["SASentry"]
528
529        data1d = self._check_for_empty_data(data1d)
530
531        self.base_ns = "{0}{1}{2}".format("{", \
532                            CANSAS_NS.get(self.cansas_version).get("ns"), "}")
533        tagname = ''
534        tagname_original = ''
535
536        # Go through each child in the parent element
537        for node in dom:
538            try:
539                # Get the element name and set the current names level
540                tagname = node.tag.replace(self.base_ns, "")
541                tagname_original = tagname
542                if tagname == "fitting_plug_in" or tagname == "pr_inversion" or\
543                    tagname == "invariant":
544                    continue
545                names.append(tagname)
546                children = node.getchildren()
547                if len(children) == 0:
548                    children = None
549                save_data1d = data1d
550
551                # Look for special cases
552                data1d = self._handle_special_cases(tagname, data1d, children)
553
554                # Get where to store content
555                cs_values = CONSTANTS.iterate_namespace(names)
556                # If the element is a child element, recurse
557                if children is not None:
558                    # Returned value is new Data1D object with all previous and
559                    # new values in it.
560                    data1d, extras = self._parse_entry(node,
561                                                       names, data1d, extras)
562
563                #Get the information from the node
564                node_value, _ = self._get_node_value(node, cs_values, \
565                                                            data1d, tagname)
566
567                # If appending to a dictionary (meta_data | run_name)
568                # make sure the key is unique
569                if cs_values.ns_variable == "{0}.meta_data[\"{2}\"] = \"{1}\"":
570                    # If we are within a Process, Detector, Collimation or
571                    # Aperture instance, pull out old data1d
572                    tagname = self._create_unique_key(data1d.meta_data, \
573                                                      tagname, 0)
574                    if isinstance(data1d, Data1D) == False:
575                        store_me = cs_values.ns_variable.format("data1d", \
576                                                            node_value, tagname)
577                        extras.append(store_me)
578                        cs_values.ns_variable = None
579                if cs_values.ns_variable == "{0}.run_name[\"{2}\"] = \"{1}\"":
580                    tagname = self._create_unique_key(data1d.run_name, \
581                                                      tagname, 0)
582
583                # Check for Data1D object and any extra commands to save
584                if isinstance(data1d, Data1D):
585                    for item in extras:
586                        exec item
587                # Don't bother saving empty information unless it is a float
588                if cs_values.ns_variable is not None and \
589                            node_value is not None and \
590                            node_value.isspace() == False:
591                    # Format a string and then execute it.
592                    store_me = cs_values.ns_variable.format("data1d", \
593                                                            node_value, tagname)
594                    exec store_me
595                # Get attributes and process them
596                data1d = self._handle_attributes(node, data1d, cs_values, \
597                                                 tagname)
598
599            except TypeError:
600                pass
601            except Exception as excep:
602                exc_type, exc_obj, exc_tb = sys.exc_info()
603                fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]
604                print(excep, exc_type, fname, exc_tb.tb_lineno, \
605                      tagname, exc_obj)
606            finally:
607                # Save special cases in original data1d object
608                # then restore the data1d
609                save_data1d = self._restore_original_case(tagname_original, \
610                                                tagname, save_data1d, data1d)
611                if tagname_original == "fitting_plug_in" or \
612                    tagname_original == "invariant" or \
613                    tagname_original == "pr_inversion":
614                    pass
615                else:
616                    data1d = save_data1d
617                    # Remove tagname from names to restore original base
618                    names.remove(tagname_original)
619        return data1d, extras
620
621    def _get_pi_string(self):
622        """
623        Creates the processing instructions header for writing to file
624        """
625        pis = self.return_processing_instructions()
626        if len(pis) > 0:
627            pi_tree = self.create_tree(pis[0])
628            i = 1
629            for i in range(1, len(pis) - 1):
630                pi_tree = self.append(pis[i], pi_tree)
631            pi_string = self.to_string(pi_tree)
632        else:
633            pi_string = ""
634        return pi_string
635
636    def _create_main_node(self):
637        """
638        Creates the primary xml header used when writing to file
639        """
640        xsi = "http://www.w3.org/2001/XMLSchema-instance"
641        version = self.cansas_version
642        n_s = CANSAS_NS.get(version).get("ns")
643        if version == "1.1":
644            url = "http://www.cansas.org/formats/1.1/"
645        else:
646            url = "http://svn.smallangles.net/svn/canSAS/1dwg/trunk/"
647        schema_location = "{0} {1}cansas1d.xsd".format(n_s, url)
648        attrib = {"{" + xsi + "}schemaLocation" : schema_location,
649                  "version" : version}
650        nsmap = {'xsi' : xsi, None: n_s}
651
652        main_node = self.create_element("{" + n_s + "}SASroot",
653                                        attrib=attrib, nsmap=nsmap)
654        return main_node
655
656    def _write_run_names(self, datainfo, entry_node):
657        """
658        Writes the run names to the XML file
659
660        :param datainfo: The Data1D object the information is coming from
661        :param entry_node: lxml node ElementTree object to be appended to
662        """
663        if datainfo.run == None or datainfo.run == []:
664            datainfo.run.append(RUN_NAME_DEFAULT)
665            datainfo.run_name[RUN_NAME_DEFAULT] = RUN_NAME_DEFAULT
666        for item in datainfo.run:
667            runname = {}
668            if item in datainfo.run_name and \
669            len(str(datainfo.run_name[item])) > 1:
670                runname = {'name': datainfo.run_name[item]}
671            self.write_node(entry_node, "Run", item, runname)
672
673    def _write_data(self, datainfo, entry_node):
674        """
675        Writes the I and Q data to the XML file
676
677        :param datainfo: The Data1D object the information is coming from
678        :param entry_node: lxml node ElementTree object to be appended to
679        """
680        node = self.create_element("SASdata")
681        self.append(node, entry_node)
682
683        for i in range(len(datainfo.x)):
684            point = self.create_element("Idata")
685            node.append(point)
686            self.write_node(point, "Q", datainfo.x[i],
687                            {'unit': datainfo.x_unit})
688            if len(datainfo.y) >= i:
689                self.write_node(point, "I", datainfo.y[i],
690                                {'unit': datainfo.y_unit})
691            if datainfo.dy != None and len(datainfo.dy) > i:
692                self.write_node(point, "Idev", datainfo.dy[i],
693                                {'unit': datainfo.y_unit})
694            if datainfo.dx != None and len(datainfo.dx) > i:
695                self.write_node(point, "Qdev", datainfo.dx[i],
696                                {'unit': datainfo.x_unit})
697            if datainfo.dxw != None and len(datainfo.dxw) > i:
698                self.write_node(point, "dQw", datainfo.dxw[i],
699                                {'unit': datainfo.x_unit})
700            if datainfo.dxl != None and len(datainfo.dxl) > i:
701                self.write_node(point, "dQl", datainfo.dxl[i],
702                                {'unit': datainfo.x_unit})
703
704    def _write_trans_spectrum(self, datainfo, entry_node):
705        """
706        Writes the transmission spectrum data to the XML file
707
708        :param datainfo: The Data1D object the information is coming from
709        :param entry_node: lxml node ElementTree object to be appended to
710        """
711        for i in range(len(datainfo.trans_spectrum)):
712            spectrum = datainfo.trans_spectrum[i]
713            node = self.create_element("SAStransmission_spectrum",
714                                       {"name" : spectrum.name})
715            self.append(node, entry_node)
716            if isinstance(spectrum.timestamp, datetime.datetime):
717                node.setAttribute("timestamp", spectrum.timestamp)
718            for i in range(len(spectrum.wavelength)):
719                point = self.create_element("Tdata")
720                node.append(point)
721                self.write_node(point, "Lambda", spectrum.wavelength[i],
722                                {'unit': spectrum.wavelength_unit})
723                self.write_node(point, "T", spectrum.transmission[i],
724                                {'unit': spectrum.transmission_unit})
725                if spectrum.transmission_deviation != None \
726                and len(spectrum.transmission_deviation) >= i:
727                    self.write_node(point, "Tdev",
728                                    spectrum.transmission_deviation[i],
729                                    {'unit':
730                                     spectrum.transmission_deviation_unit})
731
732    def _write_sample_info(self, datainfo, entry_node):
733        """
734        Writes the sample information to the XML file
735
736        :param datainfo: The Data1D object the information is coming from
737        :param entry_node: lxml node ElementTree object to be appended to
738        """
739        sample = self.create_element("SASsample")
740        if datainfo.sample.name is not None:
741            self.write_attribute(sample, "name",
742                                 str(datainfo.sample.name))
743        self.append(sample, entry_node)
744        self.write_node(sample, "ID", str(datainfo.sample.ID))
745        self.write_node(sample, "thickness", datainfo.sample.thickness,
746                        {"unit": datainfo.sample.thickness_unit})
747        self.write_node(sample, "transmission", datainfo.sample.transmission)
748        self.write_node(sample, "temperature", datainfo.sample.temperature,
749                        {"unit": datainfo.sample.temperature_unit})
750
751        pos = self.create_element("position")
752        written = self.write_node(pos,
753                                  "x",
754                                  datainfo.sample.position.x,
755                                  {"unit": datainfo.sample.position_unit})
756        written = written | self.write_node( \
757            pos, "y", datainfo.sample.position.y,
758            {"unit": datainfo.sample.position_unit})
759        written = written | self.write_node( \
760            pos, "z", datainfo.sample.position.z,
761            {"unit": datainfo.sample.position_unit})
762        if written == True:
763            self.append(pos, sample)
764
765        ori = self.create_element("orientation")
766        written = self.write_node(ori, "roll",
767                                  datainfo.sample.orientation.x,
768                                  {"unit": datainfo.sample.orientation_unit})
769        written = written | self.write_node( \
770            ori, "pitch", datainfo.sample.orientation.y,
771            {"unit": datainfo.sample.orientation_unit})
772        written = written | self.write_node( \
773            ori, "yaw", datainfo.sample.orientation.z,
774            {"unit": datainfo.sample.orientation_unit})
775        if written == True:
776            self.append(ori, sample)
777
778        for item in datainfo.sample.details:
779            self.write_node(sample, "details", item)
780
781    def _write_instrument(self, datainfo, entry_node):
782        """
783        Writes the instrumental information to the XML file
784
785        :param datainfo: The Data1D object the information is coming from
786        :param entry_node: lxml node ElementTree object to be appended to
787        """
788        instr = self.create_element("SASinstrument")
789        self.append(instr, entry_node)
790        self.write_node(instr, "name", datainfo.instrument)
791        return instr
792
793    def _write_source(self, datainfo, instr):
794        """
795        Writes the source information to the XML file
796
797        :param datainfo: The Data1D object the information is coming from
798        :param instr: instrument node  to be appended to
799        """
800        source = self.create_element("SASsource")
801        if datainfo.source.name is not None:
802            self.write_attribute(source, "name",
803                                 str(datainfo.source.name))
804        self.append(source, instr)
805        if datainfo.source.radiation == None or datainfo.source.radiation == '':
806            datainfo.source.radiation = "neutron"
807        self.write_node(source, "radiation", datainfo.source.radiation)
808
809        size = self.create_element("beam_size")
810        if datainfo.source.beam_size_name is not None:
811            self.write_attribute(size, "name",
812                                 str(datainfo.source.beam_size_name))
813        written = self.write_node( \
814            size, "x", datainfo.source.beam_size.x,
815            {"unit": datainfo.source.beam_size_unit})
816        written = written | self.write_node( \
817            size, "y", datainfo.source.beam_size.y,
818            {"unit": datainfo.source.beam_size_unit})
819        written = written | self.write_node( \
820            size, "z", datainfo.source.beam_size.z,
821            {"unit": datainfo.source.beam_size_unit})
822        if written == True:
823            self.append(size, source)
824
825        self.write_node(source, "beam_shape", datainfo.source.beam_shape)
826        self.write_node(source, "wavelength",
827                        datainfo.source.wavelength,
828                        {"unit": datainfo.source.wavelength_unit})
829        self.write_node(source, "wavelength_min",
830                        datainfo.source.wavelength_min,
831                        {"unit": datainfo.source.wavelength_min_unit})
832        self.write_node(source, "wavelength_max",
833                        datainfo.source.wavelength_max,
834                        {"unit": datainfo.source.wavelength_max_unit})
835        self.write_node(source, "wavelength_spread",
836                        datainfo.source.wavelength_spread,
837                        {"unit": datainfo.source.wavelength_spread_unit})
838
839    def _write_collimation(self, datainfo, instr):
840        """
841        Writes the collimation information to the XML file
842
843        :param datainfo: The Data1D object the information is coming from
844        :param instr: lxml node ElementTree object to be appended to
845        """
846        if datainfo.collimation == [] or datainfo.collimation == None:
847            coll = Collimation()
848            datainfo.collimation.append(coll)
849        for item in datainfo.collimation:
850            coll = self.create_element("SAScollimation")
851            if item.name is not None:
852                self.write_attribute(coll, "name", str(item.name))
853            self.append(coll, instr)
854
855            self.write_node(coll, "length", item.length,
856                            {"unit": item.length_unit})
857
858            for aperture in item.aperture:
859                apert = self.create_element("aperture")
860                if aperture.name is not None:
861                    self.write_attribute(apert, "name", str(aperture.name))
862                if aperture.type is not None:
863                    self.write_attribute(apert, "type", str(aperture.type))
864                self.append(apert, coll)
865
866                size = self.create_element("size")
867                if aperture.size_name is not None:
868                    self.write_attribute(size, "name",
869                                         str(aperture.size_name))
870                written = self.write_node(size, "x", aperture.size.x,
871                                          {"unit": aperture.size_unit})
872                written = written | self.write_node( \
873                    size, "y", aperture.size.y,
874                    {"unit": aperture.size_unit})
875                written = written | self.write_node( \
876                    size, "z", aperture.size.z,
877                    {"unit": aperture.size_unit})
878                if written == True:
879                    self.append(size, apert)
880
881                self.write_node(apert, "distance", aperture.distance,
882                                {"unit": aperture.distance_unit})
883
884    def _write_detectors(self, datainfo, instr):
885        """
886        Writes the detector information to the XML file
887
888        :param datainfo: The Data1D object the information is coming from
889        :param inst: lxml instrument node to be appended to
890        """
891        if datainfo.detector == None or datainfo.detector == []:
892            det = Detector()
893            det.name = ""
894            datainfo.detector.append(det)
895
896        for item in datainfo.detector:
897            det = self.create_element("SASdetector")
898            written = self.write_node(det, "name", item.name)
899            written = written | self.write_node(det, "SDD", item.distance,
900                                                {"unit": item.distance_unit})
901            if written == True:
902                self.append(det, instr)
903
904            off = self.create_element("offset")
905            written = self.write_node(off, "x", item.offset.x,
906                                      {"unit": item.offset_unit})
907            written = written | self.write_node(off, "y", item.offset.y,
908                                                {"unit": item.offset_unit})
909            written = written | self.write_node(off, "z", item.offset.z,
910                                                {"unit": item.offset_unit})
911            if written == True:
912                self.append(off, det)
913
914            ori = self.create_element("orientation")
915            written = self.write_node(ori, "roll", item.orientation.x,
916                                      {"unit": item.orientation_unit})
917            written = written | self.write_node(ori, "pitch",
918                                                item.orientation.y,
919                                                {"unit": item.orientation_unit})
920            written = written | self.write_node(ori, "yaw",
921                                                item.orientation.z,
922                                                {"unit": item.orientation_unit})
923            if written == True:
924                self.append(ori, det)
925
926            center = self.create_element("beam_center")
927            written = self.write_node(center, "x", item.beam_center.x,
928                                      {"unit": item.beam_center_unit})
929            written = written | self.write_node(center, "y",
930                                                item.beam_center.y,
931                                                {"unit": item.beam_center_unit})
932            written = written | self.write_node(center, "z",
933                                                item.beam_center.z,
934                                                {"unit": item.beam_center_unit})
935            if written == True:
936                self.append(center, det)
937
938            pix = self.create_element("pixel_size")
939            written = self.write_node(pix, "x", item.pixel_size.x,
940                                      {"unit": item.pixel_size_unit})
941            written = written | self.write_node(pix, "y", item.pixel_size.y,
942                                                {"unit": item.pixel_size_unit})
943            written = written | self.write_node(pix, "z", item.pixel_size.z,
944                                                {"unit": item.pixel_size_unit})
945            written = written | self.write_node(det, "slit_length",
946                                                item.slit_length,
947                                                {"unit": item.slit_length_unit})
948            if written == True:
949                self.append(pix, det)
950
951    def _write_process_notes(self, datainfo, entry_node):
952        """
953        Writes the process notes to the XML file
954
955        :param datainfo: The Data1D object the information is coming from
956        :param entry_node: lxml node ElementTree object to be appended to
957
958        """
959        for item in datainfo.process:
960            node = self.create_element("SASprocess")
961            self.append(node, entry_node)
962            self.write_node(node, "name", item.name)
963            self.write_node(node, "date", item.date)
964            self.write_node(node, "description", item.description)
965            for term in item.term:
966                value = term['value']
967                del term['value']
968                self.write_node(node, "term", value, term)
969            for note in item.notes:
970                self.write_node(node, "SASprocessnote", note)
971            if len(item.notes) == 0:
972                self.write_node(node, "SASprocessnote", "")
973
974    def _write_notes(self, datainfo, entry_node):
975        """
976        Writes the notes to the XML file and creates an empty note if none
977        exist
978
979        :param datainfo: The Data1D object the information is coming from
980        :param entry_node: lxml node ElementTree object to be appended to
981
982        """
983        if len(datainfo.notes) == 0:
984            node = self.create_element("SASnote")
985            self.append(node, entry_node)
986        else:
987            for item in datainfo.notes:
988                node = self.create_element("SASnote")
989                self.write_text(node, item)
990                self.append(node, entry_node)
991
992    def _check_origin(self, entry_node, doc):
993        """
994        Return the document, and the SASentry node associated with
995        the data we just wrote.
996        If the calling function was not the cansas reader, return a minidom
997        object rather than an lxml object.
998
999        :param entry_node: lxml node ElementTree object to be appended to
1000        :param doc: entire xml tree
1001        """
1002        frm = inspect.stack()[1]
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]
1007        if mod_name != "dataloader/readers/cansas_reader":
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)
1013        return entry_node
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.
1063        entry_node = self._check_origin(entry_node, doc)
1064        return doc, entry_node
1065
1066    def write_node(self, parent, name, value, attr=None):
1067        """
1068        :param doc: document DOM
1069        :param parent: parent node
1070        :param name: tag of the element
1071        :param value: value of the child text node
1072        :param attr: attribute dictionary
1073
1074        :return: True if something was appended, otherwise False
1075        """
1076        if value is not None:
1077            parent = self.ebuilder(parent, name, value, attr)
1078            return True
1079        return False
1080
1081    def write(self, filename, datainfo):
1082        """
1083        Write the content of a Data1D as a CanSAS XML file
1084
1085        :param filename: name of the file to write
1086        :param datainfo: Data1D object
1087        """
1088        # Create XML document
1089        doc, _ = self._to_xml_doc(datainfo)
1090        # Write the file
1091        file_ref = open(filename, 'w')
1092        if self.encoding == None:
1093            self.encoding = "UTF-8"
1094        doc.write(file_ref, encoding=self.encoding,
1095                  pretty_print=True, xml_declaration=True)
1096        file_ref.close()
1097
1098    # DO NOT REMOVE - used in saving and loading panel states.
1099    def _store_float(self, location, node, variable, storage, optional=True):
1100        """
1101        Get the content of a xpath location and store
1102        the result. Check that the units are compatible
1103        with the destination. The value is expected to
1104        be a float.
1105
1106        The xpath location might or might not exist.
1107        If it does not exist, nothing is done
1108
1109        :param location: xpath location to fetch
1110        :param node: node to read the data from
1111        :param variable: name of the data member to store it in [string]
1112        :param storage: data object that has the 'variable' data member
1113        :param optional: if True, no exception will be raised
1114            if unit conversion can't be done
1115
1116        :raise ValueError: raised when the units are not recognized
1117        """
1118        entry = get_content(location, node)
1119        try:
1120            value = float(entry.text)
1121        except:
1122            value = None
1123
1124        if value is not None:
1125            # If the entry has units, check to see that they are
1126            # compatible with what we currently have in the data object
1127            units = entry.get('unit')
1128            if units is not None:
1129                toks = variable.split('.')
1130                local_unit = None
1131                exec "local_unit = storage.%s_unit" % toks[0]
1132                if local_unit != None and units.lower() != local_unit.lower():
1133                    if HAS_CONVERTER == True:
1134                        try:
1135                            conv = Converter(units)
1136                            exec "storage.%s = %g" % \
1137                                (variable, conv(value, units=local_unit))
1138                        except:
1139                            _, exc_value, _ = sys.exc_info()
1140                            err_mess = "CanSAS reader: could not convert"
1141                            err_mess += " %s unit [%s]; expecting [%s]\n  %s" \
1142                                % (variable, units, local_unit, exc_value)
1143                            self.errors.add(err_mess)
1144                            if optional:
1145                                logging.info(err_mess)
1146                            else:
1147                                raise ValueError, err_mess
1148                    else:
1149                        err_mess = "CanSAS reader: unrecognized %s unit [%s];"\
1150                        % (variable, units)
1151                        err_mess += " expecting [%s]" % local_unit
1152                        self.errors.add(err_mess)
1153                        if optional:
1154                            logging.info(err_mess)
1155                        else:
1156                            raise ValueError, err_mess
1157                else:
1158                    exec "storage.%s = value" % variable
1159            else:
1160                exec "storage.%s = value" % variable
1161
1162    # DO NOT REMOVE - used in saving and loading panel states.
1163    def _store_content(self, location, node, variable, storage):
1164        """
1165        Get the content of a xpath location and store
1166        the result. The value is treated as a string.
1167
1168        The xpath location might or might not exist.
1169        If it does not exist, nothing is done
1170
1171        :param location: xpath location to fetch
1172        :param node: node to read the data from
1173        :param variable: name of the data member to store it in [string]
1174        :param storage: data object that has the 'variable' data member
1175
1176        :return: return a list of errors
1177        """
1178        entry = get_content(location, node)
1179        if entry is not None and entry.text is not None:
1180            exec "storage.%s = entry.text.strip()" % variable
Note: See TracBrowser for help on using the repository browser.