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

ESS_GUIESS_GUI_DocsESS_GUI_batch_fittingESS_GUI_bumps_abstractionESS_GUI_iss1116ESS_GUI_iss879ESS_GUI_iss959ESS_GUI_openclESS_GUI_orderingESS_GUI_sync_sascalc
Last change on this file since a40be8c was a40be8c, checked in by Piotr Rozyczko <rozyczko@…>, 8 years ago

Closes #596: Major rewrite to canSAS XML reader to create separate Data1D objects for each SASdata. Also fixed a few bugs that were present in the reader, that were not caught until now.

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