Changeset e090c624 in sasview


Ignore:
Timestamp:
Jan 27, 2014 1:35:28 PM (10 years ago)
Author:
Jeff Krzywon <jeffery.krzywon@…>
Branches:
master, ESS_GUI, ESS_GUI_Docs, ESS_GUI_batch_fitting, ESS_GUI_bumps_abstraction, ESS_GUI_iss1116, ESS_GUI_iss879, ESS_GUI_iss959, ESS_GUI_opencl, ESS_GUI_ordering, ESS_GUI_sync_sascalc, costrafo411, magnetic_scatt, release-4.1.1, release-4.1.2, release-4.2.2, release_4.0.1, ticket-1009, ticket-1094-headless, ticket-1242-2d-resolution, ticket-1243, ticket-1249, ticket885, unittest-saveload
Children:
64beda4
Parents:
16bd5ca
Message:

Fix for ticket #203 and moved a function from cansas_reader to cansas_constants because it was more relevant there.

Location:
src/sans/dataloader/readers
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • src/sans/dataloader/readers/cansas_constants.py

    r76cd1ae re090c624  
    88    format = '' 
    99     
     10     
    1011    def __init__(self): 
    1112        self.ns = self.CANSAS_NS 
    1213        self.format = self.CANSAS_FORMAT 
     14     
     15     
     16    def _iterate_namespace(self, namespace): 
     17        """ 
     18        Method to iterate through a cansas constants tree based on a list of 
     19        names 
     20         
     21        :param namespace: A list of names that match the tree structure of 
     22            cansas_constants 
     23        """ 
     24        # The current level to look through in cansas_constants. 
     25        return_me = currentLevel() 
     26        return_me.current_level = self.CANSAS_FORMAT.get("SASentry") 
     27        # Defaults for variable and datatype 
     28        return_me.ns_variable = "{0}.meta_data[\"{2}\"] = \"{1}\"" 
     29        return_me.ns_datatype = "content" 
     30        return_me.ns_optional = True 
     31        for name in namespace: 
     32            if name != "SASentry": 
     33                return_me.current_level = \ 
     34                        return_me.current_level.get("children").get(name, "") 
     35                if return_me.current_level == "": 
     36                    return_me.current_level = \ 
     37                            return_me.current_level.get("<any>", "") 
     38                cl_variable = return_me.current_level.get("variable", "") 
     39                cl_datatype = return_me.current_level.get("storeas", "") 
     40                cl_units_optional = \ 
     41                            return_me.current_level.get("units_required", "") 
     42                # Where are how to store the variable for the given namespace 
     43                # CANSAS_CONSTANTS tree is hierarchical, so is no value, inherit 
     44                return_me.ns_variable = cl_variable if cl_variable != "" else \ 
     45                                    return_me.ns_variable 
     46                return_me.ns_datatype = cl_datatype if cl_datatype != "" else \ 
     47                                    return_me.ns_datatype 
     48                return_me.ns_optional = cl_units_optional if \ 
     49                                    cl_units_optional != return_me.ns_optional \ 
     50                                    else return_me.ns_optional 
     51        return return_me     
     52     
     53     
    1354    """ 
    1455    CANSAS_NS holds the base namespace and the default schema file information 
     
    2465                          } 
    2566                 } 
     67     
    2668     
    2769    """ 
     
    697739                                   } 
    698740                     } 
    699      
     741    
     742class currentLevel: 
     743      
     744    current_level = '' 
     745    ns_variable = '' 
     746    ns_datatype = '' 
     747    ns_optional = True 
     748      
     749    def __init__(self): 
     750        self.current_level = '' 
     751        self.ns_variable = '' 
     752        self.ns_datatype = "content" 
     753        self.ns_optional = True 
  • src/sans/dataloader/readers/cansas_reader.py

    r76cd1ae re090c624  
    4040 
    4141 
    42 def write_node(doc, parent, name, value, attr={}): 
    43     """ 
    44     :param doc: document DOM 
    45     :param parent: parent node 
    46     :param name: tag of the element 
    47     :param value: value of the child text node 
    48     :param attr: attribute dictionary 
    49      
    50     :return: True if something was appended, otherwise False 
    51     """ 
    52     if value is not None: 
    53         node = doc.createElement(name) 
    54         node.appendChild(doc.createTextNode(str(value))) 
    55         for item in attr: 
    56             node.setAttribute(item, attr[item]) 
    57         parent.appendChild(node) 
    58         return True 
    59     return False 
    60                  
    61  
    6242def get_content(location, node): 
    6343    """ 
     
    10181 
    10282 
    103  
    104 class CANSASError(Exception): 
    105     """Base class all CANSAS reader exceptions are derived""" 
    106     pass 
    107  
    108 class NotCANSASFileError(CANSASError): 
    109     def __init__(self): 
    110         self.value = "This is not a proper CanSAS file." 
    111     def __str__(self): 
    112         return repr(self.value) 
     83def write_node(doc, parent, name, value, attr={}): 
     84    """ 
     85    :param doc: document DOM 
     86    :param parent: parent node 
     87    :param name: tag of the element 
     88    :param value: value of the child text node 
     89    :param attr: attribute dictionary 
     90     
     91    :return: True if something was appended, otherwise False 
     92    """ 
     93    if value is not None: 
     94        node = doc.createElement(name) 
     95        node.appendChild(doc.createTextNode(str(value))) 
     96        for item in attr: 
     97            node.setAttribute(item, attr[item]) 
     98        parent.appendChild(node) 
     99        return True 
     100    return False 
     101                 
    113102 
    114103class Reader(): 
     
    274263        return name 
    275264     
    276     def _iterate_namespace(self, namespace): 
    277         """ 
    278         Method to iterate through a cansas constants tree based on a list of 
    279         names 
    280          
    281         :param namespace: A list of names that match the tree structure of 
    282             cansas_constants 
    283         """ 
    284         # The current level to look through in cansas_constants. 
    285         current_level = CANSAS_FORMAT.get("SASentry") 
    286         # Defaults for variable and datatype 
    287         ns_variable = "{0}.meta_data[\"{2}\"] = \"{1}\"" 
    288         ns_datatype = "content" 
    289         ns_optional = True 
    290         for name in namespace: 
    291             if name != "SASentry": 
    292                 current_level = current_level.get("children").get(name, "") 
    293                 if current_level == "": 
    294                     current_level = current_level.get("<any>", "") 
    295                 cl_variable = current_level.get("variable", "") 
    296                 cl_datatype = current_level.get("storeas", "") 
    297                 cl_units_optional = current_level.get("units_required", "") 
    298                 # Where are how to store the variable for the given namespace 
    299                 # CANSAS_CONSTANTS tree is hierarchical, so is no value, inherit 
    300                 ns_variable = cl_variable if cl_variable != "" else ns_variable 
    301                 ns_datatype = cl_datatype if cl_datatype != "" else ns_datatype 
    302                 ns_optional = cl_units_optional if cl_units_optional != \ 
    303                                     ns_optional else ns_optional 
    304         return current_level, ns_variable, ns_datatype, ns_optional 
    305      
    306265     
    307266    def _unit_conversion(self, new_current_level, attr, data1d, \ 
     
    397356                ns.append(tagname) 
    398357                attr = node.attrib 
     358                children = node.getchildren() 
     359                save_data1d = data1d 
    399360                 
    400361                # Look for special cases 
    401                 save_data1d = data1d 
    402362                if tagname == "SASdetector": 
    403363                    data1d = Detector() 
     
    420380                elif tagname == "aperture": 
    421381                    data1d = Aperture() 
    422                  
     382                if tagname == "Idata" and children is not None: 
     383                    dql = 0 
     384                    dqw = 0 
     385                    for child in children: 
     386                        tag = child.tag.replace(base_ns, "") 
     387                        if tag == "dQl": 
     388                            dql = 1 
     389                        if tag == "dQw": 
     390                            dqw = 1 
     391                    if dqw == 1 and dql == 0: 
     392                        data1d.dxl = numpy.append(data1d.dxl, 0.0) 
     393                    elif dql == 1 and dqw == 0: 
     394                        data1d.dxw = numpy.append(data1d.dxw, 0.0) 
     395                                 
    423396                # Get where to store content 
    424                 new_current_level, ns_var, ns_datatype, \ 
    425                                     optional = self._iterate_namespace(ns) 
     397                cs_values = constants._iterate_namespace(ns) 
    426398                # If the element is a child element, recurse 
    427                 if node.getchildren() is not None: 
    428                     # Returned value is new Data1D object with all previous and new values in it. 
     399                if children is not None: 
     400                    # Returned value is new Data1D object with all previous and  
     401                    # new values in it. 
    429402                    data1d, extras = self._parse_entry(node, ns, data1d, extras) 
    430403                     
     
    437410                 
    438411                # If the value is a float, compile with units. 
    439                 if ns_datatype == "float": 
     412                if cs_values.ns_datatype == "float": 
    440413                    # If an empty value is given, store as zero. 
    441414                    if node_value is None or node_value.isspace() \ 
    442415                                            or node_value.lower() == "nan": 
    443416                        node_value = "0.0" 
    444                     node_value, unit = self._unit_conversion(new_current_level,\ 
    445                                              attr, data1d, node_value, optional) 
     417                    node_value, unit = self._unit_conversion(\ 
     418                                cs_values.current_level, attr, data1d, \ 
     419                                node_value, cs_values.ns_optional) 
    446420                     
    447                 # If appending to a dictionary (meta_data | run_name), name sure the key is unique 
    448                 if ns_var == "{0}.meta_data[\"{2}\"] = \"{1}\"": 
    449                     # If we are within a Process, Detector, Collimation or Aperture instance, pull out old data1d 
    450                     tagname = self._create_unique_key(data1d.meta_data, tagname, 0) 
     421                # If appending to a dictionary (meta_data | run_name) 
     422                # make sure the key is unique 
     423                if cs_values.ns_variable == "{0}.meta_data[\"{2}\"] = \"{1}\"": 
     424                    # If we are within a Process, Detector, Collimation or  
     425                    # Aperture instance, pull out old data1d 
     426                    tagname = self._create_unique_key(data1d.meta_data, \ 
     427                                                      tagname, 0) 
    451428                    if isinstance(data1d, Data1D) == False: 
    452                         store_me = ns_var.format("data1d", node_value, tagname) 
     429                        store_me = cs_values.ns_variable.format("data1d", \ 
     430                                                            node_value, tagname) 
    453431                        extras.append(store_me) 
    454                         ns_var = None 
    455                 if ns_var == "{0}.run_name[\"{2}\"] = \"{1}\"": 
    456                     tagname = self._create_unique_key(data1d.run_name, tagname, 0) 
     432                        cs_values.ns_variable = None 
     433                if cs_values.ns_variable == "{0}.run_name[\"{2}\"] = \"{1}\"": 
     434                    tagname = self._create_unique_key(data1d.run_name, \ 
     435                                                      tagname, 0) 
    457436                 
    458437                # Check for Data1D object and any extra commands to save 
     
    461440                        exec item 
    462441                # Don't bother saving empty information unless it is a float 
    463                 if ns_var is not None and node_value is not None and \ 
     442                if cs_values.ns_variable is not None and node_value is not None and \ 
    464443                            node_value.isspace() == False: 
    465444                    # Format a string and then execute it. 
    466                     store_me = ns_var.format("data1d", node_value, tagname) 
     445                    store_me = cs_values.ns_variable.format("data1d", node_value, tagname) 
    467446                    exec store_me 
    468447                # Get attributes and process them 
     
    470449                    for key in node.keys(): 
    471450                        try: 
    472                             cansas_attrib = new_current_level.get("attributes").get(key) 
     451                            cansas_attrib = \ 
     452                            cs_values.current_level.get("attributes").get(key) 
    473453                            attrib_variable = cansas_attrib.get("variable") 
    474454                            if key == 'unit' and unit != '': 
     
    525505        main_node.setAttribute("xmlns:xsi", 
    526506                               "http://www.w3.org/2001/XMLSchema-instance") 
    527         main_node.setAttribute("xsi:schemaLocation", 
    528                                "{0} http://svn.smallangles.net/svn/canSAS/1dwg/trunk/cansas1d.xsd".format(ns)) 
     507        main_node.setAttribute("xsi:schemaLocation", "{0} http://svn.smallangles.net/svn/canSAS/1dwg/trunk/cansas1d.xsd".format(ns)) 
    529508         
    530509        doc.appendChild(main_node) 
Note: See TracChangeset for help on using the changeset viewer.