source: sasview/src/sas/sascalc/dataloader/readers/cansas_reader.py @ a3f1c11e

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 a3f1c11e was 5f26aa4, checked in by krzywon, 8 years ago

Fix cansas XML reader issues to resolve broken unit tests.

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