Changeset aa720c4 in sasview


Ignore:
Timestamp:
Mar 3, 2015 10:26:26 AM (9 years ago)
Author:
Doucet, Mathieu <doucetm@…>
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:
226ce0b
Parents:
7425bcf
Message:

pylint fixes

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/sas/dataloader/readers/cansas_reader.py

    rb3efb7d raa720c4  
    55#This software was developed by the University of Tennessee as part of the 
    66#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  
     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 
    1010#following sentence: 
    11 #This work benefited from DANSE software developed under NSF award DMR-0520547.  
     11#This work benefited from DANSE software developed under NSF award DMR-0520547. 
    1212#copyright 2008,2009 University of Tennessee 
    1313############################################################################# 
     
    4242    HAS_CONVERTER = False 
    4343 
    44 CONSTANTS = CansasConstants()     
     44CONSTANTS = CansasConstants() 
    4545CANSAS_FORMAT = CONSTANTS.format 
    4646CANSAS_NS = CONSTANTS.names 
    4747ALLOW_ALL = True 
    4848 
    49  
    50 # minidom used in functions called by outside classes 
    51 import xml.dom.minidom 
    5249# DO NOT REMOVE 
    5350# Called by outside packages: 
     
    5754    """ 
    5855    Get the first instance of the content of a xpath location. 
    59      
     56 
    6057    :param location: xpath location 
    6158    :param node: node to start at 
    62      
     59 
    6360    :return: Element, or None 
    6461    """ 
    65     nodes = node.xpath(location,  
     62    nodes = node.xpath(location, 
    6663                       namespaces={'ns': CANSAS_NS.get("1.0").get("ns")}) 
    67      
     64 
    6865    if len(nodes) > 0: 
    6966        return nodes[0] 
     
    8279    :param value: value of the child text node 
    8380    :param attr: attribute dictionary 
    84      
     81 
    8582    :return: True if something was appended, otherwise False 
    8683    """ 
     
    9592        return True 
    9693    return False 
    97                  
     94 
    9895 
    9996class Reader(XMLreader): 
    10097    """ 
    10198    Class to load cansas 1D XML files 
    102      
     99 
    103100    :Dependencies: 
    104101        The CanSAS reader requires PyXML 0.8.4 or later. 
     
    107104    cansas_version = "1.0" 
    108105    base_ns = "{cansas1d/1.0}" 
    109      
     106 
    110107    logging = [] 
    111108    errors = [] 
    112      
     109 
    113110    type_name = "canSAS" 
    114111    ## Wildcards 
     
    116113    ## List of allowed extensions 
    117114    ext = ['.xml', '.XML', '.svs', '.SVS'] 
    118      
     115 
    119116    ## Flag to bypass extension check 
    120117    allow_all = True 
    121      
    122      
     118 
     119 
    123120    def __init__(self): 
    124121        ## List of errors 
    125122        self.errors = [] 
    126123        self.encoding = None 
    127          
    128          
     124 
     125 
    129126    def is_cansas(self, ext="xml"): 
    130127        """ 
    131128        Checks to see if the xml file is a CanSAS file 
    132          
     129 
    133130        :param ext: The file extension of the data file 
    134131        """ 
     
    142139            return True 
    143140        return False 
    144      
    145      
     141 
     142 
    146143    def load_file_and_schema(self, xml_file): 
    147144        """ 
    148145        Loads the file and associates a schema, if a known schema exists 
    149          
     146 
    150147        :param xml_file: The xml file path sent to Reader.read 
    151148        """ 
    152149        base_name = xml_reader.__file__ 
    153         base_name = base_name.replace("\\","/") 
     150        base_name = base_name.replace("\\", "/") 
    154151        base = base_name.split("/sas/")[0] 
    155          
     152 
    156153        # Load in xml file and get the cansas version from the header 
    157154        self.set_xml_file(xml_file) 
    158155        self.cansas_version = self.xmlroot.get("version", "1.0") 
    159          
     156 
    160157        # Generic values for the cansas file based on the version 
    161158        cansas_defaults = CANSAS_NS.get(self.cansas_version, "1.0") 
    162159        schema_path = "{0}/sas/dataloader/readers/schema/{1}".format\ 
    163160                (base, cansas_defaults.get("schema")).replace("\\", "/") 
    164          
     161 
    165162        # Link a schema to the XML file. 
    166163        self.set_schema(schema_path) 
    167164        return cansas_defaults 
    168                  
    169      
     165 
     166 
    170167    def read(self, xml_file): 
    171168        """ 
    172169        Validate and read in an xml_file file in the canSAS format. 
    173          
     170 
    174171        :param xml_file: A canSAS file path in proper XML format 
    175172        """ 
    176          
     173 
    177174        # output - Final list of Data1D objects 
    178175        output = [] 
    179176        # ns - Namespace hierarchy for current xml_file object 
    180177        ns_list = [] 
    181          
     178 
    182179        # Check that the file exists 
    183180        if os.path.isfile(xml_file): 
     
    186183            # If the file type is not allowed, return nothing 
    187184            if extension in self.ext or self.allow_all: 
    188                 # Get the file location of  
     185                # Get the file location of 
    189186                cansas_defaults = self.load_file_and_schema(xml_file) 
    190                  
     187 
    191188                # Try to load the file, but raise an error if unable to. 
    192189                # Check the file matches the XML schema 
     
    195192                        # Get each SASentry from XML file and add it to a list. 
    196193                        entry_list = self.xmlroot.xpath( 
    197                                 '/ns:SASroot/ns:SASentry',  
    198                                 namespaces = {'ns': cansas_defaults.get("ns")}) 
     194                                '/ns:SASroot/ns:SASentry', 
     195                                namespaces={'ns': cansas_defaults.get("ns")}) 
    199196                        ns_list.append("SASentry") 
    200                          
     197 
    201198                        # If multiple files, modify the name for each is unique 
    202199                        increment = 0 
    203200                        # Parse each SASentry item 
    204201                        for entry in entry_list: 
    205                             # Define a new Data1D object with zeroes for  
     202                            # Define a new Data1D object with zeroes for 
    206203                            # x_vals and y_vals 
    207                             data1d = Data1D(numpy.empty(0), numpy.empty(0), \ 
     204                            data1d = Data1D(numpy.empty(0), numpy.empty(0), 
    208205                                            numpy.empty(0), numpy.empty(0)) 
    209206                            data1d.dxl = numpy.empty(0) 
    210207                            data1d.dxw = numpy.empty(0) 
    211                              
     208 
    212209                            # If more than one SASentry, increment each in order 
    213210                            name = basename 
     
    215212                                name += "_{0}".format(increment) 
    216213                                increment += 1 
    217                              
    218                             # Set the Data1D name and then parse the entry.  
     214 
     215                            # Set the Data1D name and then parse the entry. 
    219216                            # The entry is appended to a list of entry values 
    220217                            data1d.filename = name 
    221218                            data1d.meta_data["loader"] = "CanSAS 1D" 
    222                              
     219 
    223220                            # Get all preprocessing events and encoding 
    224221                            self.set_processing_instructions() 
    225222                            data1d.meta_data[PREPROCESS] = \ 
    226223                                    self.processing_instructions 
    227                              
     224 
    228225                            # Parse the XML file 
    229226                            return_value, extras = \ 
    230227                                self._parse_entry(entry, ns_list, data1d) 
    231228                            del extras[:] 
    232                              
     229 
    233230                            return_value = self._final_cleanup(return_value) 
    234231                            output.append(return_value) 
     
    242239        # Return a list of parsed entries that dataloader can manage 
    243240        return None 
    244      
    245      
     241 
     242 
    246243    def _final_cleanup(self, data1d): 
    247244        """ 
    248245        Final cleanup of the Data1D object to be sure it has all the 
    249246        appropriate information needed for perspectives 
    250          
     247 
    251248        :param data1d: Data1D object that has been populated 
    252249        """ 
     
    272269            numpy.trim_zeros(data1d.dxw) 
    273270        return data1d 
    274                              
    275      
    276     def _create_unique_key(self, dictionary, name, numb = 0): 
     271 
     272    def _create_unique_key(self, dictionary, name, numb=0): 
    277273        """ 
    278274        Create a unique key value for any dictionary to prevent overwriting 
    279275        Recurses until a unique key value is found. 
    280          
     276 
    281277        :param dictionary: A dictionary with any number of entries 
    282278        :param name: The index of the item to be added to dictionary 
     
    289285            name = self._create_unique_key(dictionary, name, numb) 
    290286        return name 
    291      
    292      
     287 
     288 
    293289    def _unit_conversion(self, node, new_current_level, data1d, \ 
    294290                                                tagname, node_value): 
     
    296292        A unit converter method used to convert the data included in the file 
    297293        to the default units listed in data_info 
    298          
    299         :param new_current_level: cansas_constants level as returned by  
     294 
     295        :param new_current_level: cansas_constants level as returned by 
    300296            iterate_namespace 
    301297        :param attr: The attributes of the node 
     
    318314                    if HAS_CONVERTER == True: 
    319315                        ## Check local units - bad units raise KeyError 
    320                         data_conv_q = Converter(local_unit) 
    321316                        value_unit = default_unit 
    322317                        i_string = "node_value = data_conv_q" 
     
    350345        node_value = "float({0})".format(node_value) 
    351346        return node_value, value_unit 
    352      
    353      
     347 
     348 
    354349    def _check_for_empty_data(self, data1d): 
    355350        """ 
    356351        Creates an empty data set if no data is passed to the reader 
    357          
     352 
    358353        :param data1d: presumably a Data1D object 
    359354        """ 
     
    370365            data1d.dxw = dxw 
    371366        return data1d 
    372      
    373      
     367 
    374368    def _handle_special_cases(self, tagname, data1d, children): 
    375369        """ 
    376370        Handle cases where the data type in Data1D is a dictionary or list 
    377          
     371 
    378372        :param tagname: XML tagname in use 
    379373        :param data1d: The original Data1D object 
     
    404398            data1d = self._check_for_empty_resolution(data1d, children) 
    405399        return data1d 
    406      
    407      
     400 
    408401    def _check_for_empty_resolution(self, data1d, children): 
    409402        """ 
     
    434427            data1d.dy = numpy.append(data1d.dy, 0.0) 
    435428        return data1d 
    436      
    437      
    438     def _restore_original_case(self,  
    439                                tagname_original,  
    440                                tagname,  
    441                                save_data1d,  
     429 
     430    def _restore_original_case(self, 
     431                               tagname_original, 
     432                               tagname, 
     433                               save_data1d, 
    442434                               data1d): 
    443435        """ 
    444436        Save the special case data to the appropriate location and restore 
    445437        the original Data1D object 
    446          
     438 
    447439        :param tagname_original: Unmodified tagname for the node 
    448440        :param tagname: modified tagname for the node 
     
    463455            save_data1d = data1d 
    464456        return save_data1d 
    465      
    466      
     457 
    467458    def _handle_attributes(self, node, data1d, cs_values, tagname): 
    468459        """ 
     
    473464            for key in node.keys(): 
    474465                try: 
    475                     node_value, unit = self._get_node_value(node, cs_values, \ 
    476                                                         data1d, tagname) 
     466                    _, unit = self._get_node_value(node, cs_values, \ 
     467                                                   data1d, tagname) 
    477468                    cansas_attrib = \ 
    478469                        cs_values.current_level.get("attributes").get(key) 
     
    486477                    exec store_attr 
    487478                except AttributeError: 
    488                     pass     
     479                    pass 
    489480        return data1d 
    490      
    491      
     481 
    492482    def _get_node_value(self, node, cs_values, data1d, tagname): 
    493483        """ 
    494484        Get the value of a node and any applicable units 
    495          
     485 
    496486        :param node: The XML node to get the value of 
    497487        :param cs_values: A CansasConstants.CurrentLevel object 
     
    507497        if node_value is not None: 
    508498            node_value = ' '.join(node_value.split()) 
    509         
     499 
    510500        # If the value is a float, compile with units. 
    511501        if cs_values.ns_datatype == "float": 
     
    517507            node_value, units = self._unit_conversion(node, \ 
    518508                        cs_values.current_level, data1d, tagname, node_value) 
    519              
     509 
    520510        # If the value is a timestamp, convert to a datetime object 
    521511        elif cs_values.ns_datatype == "timestamp": 
     
    529519                    node_value = None 
    530520        return node_value, units 
    531      
    532      
     521 
    533522    def _parse_entry(self, dom, names=None, data1d=None, extras=None): 
    534523        """ 
     
    536525            the CanSAS data format. This will allow multiple data files 
    537526            and extra nodes to be read in simultaneously. 
    538          
     527 
    539528        :param dom: dom object with a namespace base of names 
    540529        :param names: A list of element names that lead up to the dom object 
     
    543532            is not a Data1D object 
    544533        """ 
    545          
     534 
    546535        if extras is None: 
    547536            extras = [] 
    548537        if names is None or names == []: 
    549538            names = ["SASentry"] 
    550          
     539 
    551540        data1d = self._check_for_empty_data(data1d) 
    552                              
     541 
    553542        self.base_ns = "{0}{1}{2}".format("{", \ 
    554543                            CANSAS_NS.get(self.cansas_version).get("ns"), "}") 
    555544        tagname = '' 
    556545        tagname_original = '' 
    557          
     546 
    558547        # Go through each child in the parent element 
    559548        for node in dom: 
     
    570559                    children = None 
    571560                save_data1d = data1d 
    572                  
     561 
    573562                # Look for special cases 
    574563                data1d = self._handle_special_cases(tagname, data1d, children) 
    575                  
     564 
    576565                # Get where to store content 
    577566                cs_values = CONSTANTS.iterate_namespace(names) 
    578567                # If the element is a child element, recurse 
    579568                if children is not None: 
    580                     # Returned value is new Data1D object with all previous and  
     569                    # Returned value is new Data1D object with all previous and 
    581570                    # new values in it. 
    582                     data1d, extras = self._parse_entry(node,  
     571                    data1d, extras = self._parse_entry(node, 
    583572                                                       names, data1d, extras) 
    584                      
     573 
    585574                #Get the information from the node 
    586575                node_value, _ = self._get_node_value(node, cs_values, \ 
    587576                                                            data1d, tagname) 
    588                  
     577 
    589578                # If appending to a dictionary (meta_data | run_name) 
    590579                # make sure the key is unique 
    591580                if cs_values.ns_variable == "{0}.meta_data[\"{2}\"] = \"{1}\"": 
    592                     # If we are within a Process, Detector, Collimation or  
     581                    # If we are within a Process, Detector, Collimation or 
    593582                    # Aperture instance, pull out old data1d 
    594583                    tagname = self._create_unique_key(data1d.meta_data, \ 
     
    602591                    tagname = self._create_unique_key(data1d.run_name, \ 
    603592                                                      tagname, 0) 
    604                  
     593 
    605594                # Check for Data1D object and any extra commands to save 
    606595                if isinstance(data1d, Data1D): 
     
    618607                data1d = self._handle_attributes(node, data1d, cs_values, \ 
    619608                                                 tagname) 
    620                  
     609 
    621610            except TypeError: 
    622611                pass 
     
    640629                    names.remove(tagname_original) 
    641630        return data1d, extras 
    642          
    643      
     631 
    644632    def _get_pi_string(self): 
    645633        """ 
     
    656644            pi_string = "" 
    657645        return pi_string 
    658      
    659      
     646 
    660647    def _create_main_node(self): 
    661648        """ 
     
    673660                  "version" : version} 
    674661        nsmap = {'xsi' : xsi, None: n_s} 
    675          
    676         main_node = self.create_element("{" + n_s + "}SASroot", \ 
    677                                                attrib = attrib, \ 
    678                                                nsmap = nsmap) 
     662 
     663        main_node = self.create_element("{" + n_s + "}SASroot", 
     664                                        attrib=attrib, nsmap=nsmap) 
    679665        return main_node 
    680      
    681      
     666 
    682667    def _write_run_names(self, datainfo, entry_node): 
    683668        """ 
    684669        Writes the run names to the XML file 
    685          
     670 
    686671        :param datainfo: The Data1D object the information is coming from 
    687672        :param entry_node: lxml node ElementTree object to be appended to 
     
    696681                runname = {'name': datainfo.run_name[item]} 
    697682            self.write_node(entry_node, "Run", item, runname) 
    698              
    699      
     683 
    700684    def _write_data(self, datainfo, entry_node): 
    701685        """ 
    702686        Writes the I and Q data to the XML file 
    703          
     687 
    704688        :param datainfo: The Data1D object the information is coming from 
    705689        :param entry_node: lxml node ElementTree object to be appended to 
     
    707691        node = self.create_element("SASdata") 
    708692        self.append(node, entry_node) 
    709          
     693 
    710694        for i in range(len(datainfo.x)): 
    711695            point = self.create_element("Idata") 
    712696            node.append(point) 
    713             self.write_node(point, "Q", datainfo.x[i], \ 
    714                              {'unit': datainfo.x_unit}) 
     697            self.write_node(point, "Q", datainfo.x[i], 
     698                            {'unit': datainfo.x_unit}) 
    715699            if len(datainfo.y) >= i: 
    716700                self.write_node(point, "I", datainfo.y[i], 
    717                             {'unit': datainfo.y_unit}) 
     701                                {'unit': datainfo.y_unit}) 
    718702            if datainfo.dy != None and len(datainfo.dy) > i: 
    719703                self.write_node(point, "Idev", datainfo.dy[i], 
    720                             {'unit': datainfo.y_unit}) 
     704                                {'unit': datainfo.y_unit}) 
    721705            if datainfo.dx != None and len(datainfo.dx) > i: 
    722706                self.write_node(point, "Qdev", datainfo.dx[i], 
    723                             {'unit': datainfo.x_unit}) 
     707                                {'unit': datainfo.x_unit}) 
    724708            if datainfo.dxw != None and len(datainfo.dxw) > i: 
    725709                self.write_node(point, "dQw", datainfo.dxw[i], 
    726                             {'unit': datainfo.x_unit}) 
     710                                {'unit': datainfo.x_unit}) 
    727711            if datainfo.dxl != None and len(datainfo.dxl) > i: 
    728712                self.write_node(point, "dQl", datainfo.dxl[i], 
    729                             {'unit': datainfo.x_unit}) 
    730          
    731      
    732      
     713                                {'unit': datainfo.x_unit}) 
     714 
    733715    def _write_trans_spectrum(self, datainfo, entry_node): 
    734716        """ 
    735717        Writes the transmission spectrum data to the XML file 
    736          
     718 
    737719        :param datainfo: The Data1D object the information is coming from 
    738720        :param entry_node: lxml node ElementTree object to be appended to 
     
    741723            spectrum = datainfo.trans_spectrum[i] 
    742724            node = self.create_element("SAStransmission_spectrum", 
    743                                               {"name" : spectrum.name}) 
     725                                       {"name" : spectrum.name}) 
    744726            self.append(node, entry_node) 
    745727            if isinstance(spectrum.timestamp, datetime.datetime): 
     
    748730                point = self.create_element("Tdata") 
    749731                node.append(point) 
    750                 self.write_node(point, "Lambda", spectrum.wavelength[i],  
    751                            {'unit': spectrum.wavelength_unit}) 
    752                 self.write_node(point, "T", spectrum.transmission[i],  
    753                            {'unit': spectrum.transmission_unit}) 
     732                self.write_node(point, "Lambda", spectrum.wavelength[i], 
     733                                {'unit': spectrum.wavelength_unit}) 
     734                self.write_node(point, "T", spectrum.transmission[i], 
     735                                {'unit': spectrum.transmission_unit}) 
    754736                if spectrum.transmission_deviation != None \ 
    755737                and len(spectrum.transmission_deviation) >= i: 
    756                     self.write_node(point, "Tdev", \ 
    757                                spectrum.transmission_deviation[i], \ 
    758                                {'unit': spectrum.transmission_deviation_unit}) 
    759      
    760      
     738                    self.write_node(point, "Tdev", 
     739                                    spectrum.transmission_deviation[i], 
     740                                    {'unit': 
     741                                     spectrum.transmission_deviation_unit}) 
     742 
    761743    def _write_sample_info(self, datainfo, entry_node): 
    762744        """ 
    763745        Writes the sample information to the XML file 
    764          
     746 
    765747        :param datainfo: The Data1D object the information is coming from 
    766748        :param entry_node: lxml node ElementTree object to be appended to 
     
    768750        sample = self.create_element("SASsample") 
    769751        if datainfo.sample.name is not None: 
    770             self.write_attribute(sample,  
    771                                         "name",  
    772                                         str(datainfo.sample.name)) 
     752            self.write_attribute(sample, "name", 
     753                                 str(datainfo.sample.name)) 
    773754        self.append(sample, entry_node) 
    774755        self.write_node(sample, "ID", str(datainfo.sample.ID)) 
    775756        self.write_node(sample, "thickness", datainfo.sample.thickness, 
    776                    {"unit": datainfo.sample.thickness_unit}) 
     757                        {"unit": datainfo.sample.thickness_unit}) 
    777758        self.write_node(sample, "transmission", datainfo.sample.transmission) 
    778759        self.write_node(sample, "temperature", datainfo.sample.temperature, 
    779                    {"unit": datainfo.sample.temperature_unit}) 
    780          
     760                        {"unit": datainfo.sample.temperature_unit}) 
     761 
    781762        pos = self.create_element("position") 
    782         written = self.write_node(pos,  
    783                                   "x",  
     763        written = self.write_node(pos, 
     764                                  "x", 
    784765                                  datainfo.sample.position.x, 
    785766                                  {"unit": datainfo.sample.position_unit}) 
    786         written = written | self.write_node(pos,  
    787                                             "y",  
    788                                             datainfo.sample.position.y, 
    789                                        {"unit": datainfo.sample.position_unit}) 
    790         written = written | self.write_node(pos,  
    791                                             "z", 
    792                                             datainfo.sample.position.z, 
    793                                        {"unit": datainfo.sample.position_unit}) 
     767        written = written | self.write_node( \ 
     768            pos, "y", datainfo.sample.position.y, 
     769            {"unit": datainfo.sample.position_unit}) 
     770        written = written | self.write_node( \ 
     771            pos, "z", datainfo.sample.position.z, 
     772            {"unit": datainfo.sample.position_unit}) 
    794773        if written == True: 
    795774            self.append(pos, sample) 
    796          
     775 
    797776        ori = self.create_element("orientation") 
    798777        written = self.write_node(ori, "roll", 
    799778                                  datainfo.sample.orientation.x, 
    800779                                  {"unit": datainfo.sample.orientation_unit}) 
    801         written = written | self.write_node(ori, "pitch", 
    802                                       datainfo.sample.orientation.y, 
    803                                     {"unit": datainfo.sample.orientation_unit}) 
    804         written = written | self.write_node(ori, "yaw", 
    805                                       datainfo.sample.orientation.z, 
    806                                     {"unit": datainfo.sample.orientation_unit}) 
     780        written = written | self.write_node( \ 
     781            ori, "pitch", datainfo.sample.orientation.y, 
     782            {"unit": datainfo.sample.orientation_unit}) 
     783        written = written | self.write_node( \ 
     784            ori, "yaw", datainfo.sample.orientation.z, 
     785            {"unit": datainfo.sample.orientation_unit}) 
    807786        if written == True: 
    808787            self.append(ori, sample) 
    809          
     788 
    810789        for item in datainfo.sample.details: 
    811790            self.write_node(sample, "details", item) 
    812      
    813      
     791 
    814792    def _write_instrument(self, datainfo, entry_node): 
    815793        """ 
    816794        Writes the instrumental information to the XML file 
    817          
     795 
    818796        :param datainfo: The Data1D object the information is coming from 
    819797        :param entry_node: lxml node ElementTree object to be appended to 
     
    823801        self.write_node(instr, "name", datainfo.instrument) 
    824802        return instr 
    825      
    826      
     803 
    827804    def _write_source(self, datainfo, instr): 
    828805        """ 
    829806        Writes the source information to the XML file 
    830          
     807 
    831808        :param datainfo: The Data1D object the information is coming from 
    832809        :param instr: instrument node  to be appended to 
     
    834811        source = self.create_element("SASsource") 
    835812        if datainfo.source.name is not None: 
    836             self.write_attribute(source, 
    837                                         "name", 
    838                                         str(datainfo.source.name)) 
     813            self.write_attribute(source, "name", 
     814                                 str(datainfo.source.name)) 
    839815        self.append(source, instr) 
    840816        if datainfo.source.radiation == None or datainfo.source.radiation == '': 
    841817            datainfo.source.radiation = "neutron" 
    842818        self.write_node(source, "radiation", datainfo.source.radiation) 
    843          
     819 
    844820        size = self.create_element("beam_size") 
    845821        if datainfo.source.beam_size_name is not None: 
    846             self.write_attribute(size, 
    847                                         "name", 
    848                                         str(datainfo.source.beam_size_name)) 
    849         written = self.write_node(size, "x", datainfo.source.beam_size.x, 
    850                              {"unit": datainfo.source.beam_size_unit}) 
    851         written = written | self.write_node(size, "y", 
    852                                       datainfo.source.beam_size.y, 
    853                                        {"unit": datainfo.source.beam_size_unit}) 
    854         written = written | self.write_node(size, "z", 
    855                                       datainfo.source.beam_size.z, 
    856                                        {"unit": datainfo.source.beam_size_unit}) 
     822            self.write_attribute(size, "name", 
     823                                 str(datainfo.source.beam_size_name)) 
     824        written = self.write_node( \ 
     825            size, "x", datainfo.source.beam_size.x, 
     826            {"unit": datainfo.source.beam_size_unit}) 
     827        written = written | self.write_node( \ 
     828            size, "y", datainfo.source.beam_size.y, 
     829            {"unit": datainfo.source.beam_size_unit}) 
     830        written = written | self.write_node( \ 
     831            size, "z", datainfo.source.beam_size.z, 
     832            {"unit": datainfo.source.beam_size_unit}) 
    857833        if written == True: 
    858834            self.append(size, source) 
    859              
     835 
    860836        self.write_node(source, "beam_shape", datainfo.source.beam_shape) 
    861837        self.write_node(source, "wavelength", 
    862                    datainfo.source.wavelength, 
    863                    {"unit": datainfo.source.wavelength_unit}) 
     838                        datainfo.source.wavelength, 
     839                        {"unit": datainfo.source.wavelength_unit}) 
    864840        self.write_node(source, "wavelength_min", 
    865                    datainfo.source.wavelength_min, 
    866                    {"unit": datainfo.source.wavelength_min_unit}) 
     841                        datainfo.source.wavelength_min, 
     842                        {"unit": datainfo.source.wavelength_min_unit}) 
    867843        self.write_node(source, "wavelength_max", 
    868                    datainfo.source.wavelength_max, 
    869                    {"unit": datainfo.source.wavelength_max_unit}) 
     844                        datainfo.source.wavelength_max, 
     845                        {"unit": datainfo.source.wavelength_max_unit}) 
    870846        self.write_node(source, "wavelength_spread", 
    871                    datainfo.source.wavelength_spread, 
    872                    {"unit": datainfo.source.wavelength_spread_unit}) 
    873      
    874      
    875     def _write_collimation(self, datainfo, instr):     
     847                        datainfo.source.wavelength_spread, 
     848                        {"unit": datainfo.source.wavelength_spread_unit}) 
     849 
     850    def _write_collimation(self, datainfo, instr): 
    876851        """ 
    877852        Writes the collimation information to the XML file 
    878          
     853 
    879854        :param datainfo: The Data1D object the information is coming from 
    880855        :param instr: lxml node ElementTree object to be appended to 
     
    888863                self.write_attribute(coll, "name", str(item.name)) 
    889864            self.append(coll, instr) 
    890              
     865 
    891866            self.write_node(coll, "length", item.length, 
    892                        {"unit": item.length_unit}) 
    893              
     867                            {"unit": item.length_unit}) 
     868 
    894869            for aperture in item.aperture: 
    895870                apert = self.create_element("aperture") 
     
    899874                    self.write_attribute(apert, "type", str(aperture.type)) 
    900875                self.append(apert, coll) 
    901                  
     876 
    902877                size = self.create_element("size") 
    903878                if aperture.size_name is not None: 
    904                     self.write_attribute(size,  
    905                                                 "name",  
    906                                                 str(aperture.size_name)) 
     879                    self.write_attribute(size, "name", 
     880                                         str(aperture.size_name)) 
    907881                written = self.write_node(size, "x", aperture.size.x, 
    908                                      {"unit": aperture.size_unit}) 
    909                 written = written | self.write_node(size, "y", aperture.size.y, 
    910                                                {"unit": aperture.size_unit}) 
    911                 written = written | self.write_node(size, "z", aperture.size.z, 
    912                                                {"unit": aperture.size_unit}) 
     882                                          {"unit": aperture.size_unit}) 
     883                written = written | self.write_node( \ 
     884                    size, "y", aperture.size.y, 
     885                    {"unit": aperture.size_unit}) 
     886                written = written | self.write_node( \ 
     887                    size, "z", aperture.size.z, 
     888                    {"unit": aperture.size_unit}) 
    913889                if written == True: 
    914890                    self.append(size, apert) 
    915                  
     891 
    916892                self.write_node(apert, "distance", aperture.distance, 
    917                            {"unit": aperture.distance_unit}) 
    918      
    919      
     893                                {"unit": aperture.distance_unit}) 
     894 
     895 
    920896    def _write_detectors(self, datainfo, instr): 
    921897        """ 
    922898        Writes the detector information to the XML file 
    923          
     899 
    924900        :param datainfo: The Data1D object the information is coming from 
    925901        :param inst: lxml instrument node to be appended to 
     
    929905            det.name = "" 
    930906            datainfo.detector.append(det) 
    931                  
     907 
    932908        for item in datainfo.detector: 
    933909            det = self.create_element("SASdetector") 
    934910            written = self.write_node(det, "name", item.name) 
    935911            written = written | self.write_node(det, "SDD", item.distance, 
    936                                            {"unit": item.distance_unit}) 
     912                                                {"unit": item.distance_unit}) 
    937913            if written == True: 
    938914                self.append(det, instr) 
    939              
     915 
    940916            off = self.create_element("offset") 
    941917            written = self.write_node(off, "x", item.offset.x, 
    942                                  {"unit": item.offset_unit}) 
     918                                      {"unit": item.offset_unit}) 
    943919            written = written | self.write_node(off, "y", item.offset.y, 
    944                                            {"unit": item.offset_unit}) 
     920                                                {"unit": item.offset_unit}) 
    945921            written = written | self.write_node(off, "z", item.offset.z, 
    946                                            {"unit": item.offset_unit}) 
     922                                                {"unit": item.offset_unit}) 
    947923            if written == True: 
    948924                self.append(off, det) 
    949                  
     925 
    950926            ori = self.create_element("orientation") 
    951927            written = self.write_node(ori, "roll", item.orientation.x, 
    952                                  {"unit": item.orientation_unit}) 
     928                                      {"unit": item.orientation_unit}) 
    953929            written = written | self.write_node(ori, "pitch", 
    954                                            item.orientation.y, 
    955                                            {"unit": item.orientation_unit}) 
     930                                                item.orientation.y, 
     931                                                {"unit": item.orientation_unit}) 
    956932            written = written | self.write_node(ori, "yaw", 
    957                                            item.orientation.z, 
    958                                            {"unit": item.orientation_unit}) 
     933                                                item.orientation.z, 
     934                                                {"unit": item.orientation_unit}) 
    959935            if written == True: 
    960936                self.append(ori, det) 
    961              
     937 
    962938            center = self.create_element("beam_center") 
    963939            written = self.write_node(center, "x", item.beam_center.x, 
    964                                  {"unit": item.beam_center_unit}) 
     940                                      {"unit": item.beam_center_unit}) 
    965941            written = written | self.write_node(center, "y", 
    966                                            item.beam_center.y, 
    967                                            {"unit": item.beam_center_unit}) 
     942                                                item.beam_center.y, 
     943                                                {"unit": item.beam_center_unit}) 
    968944            written = written | self.write_node(center, "z", 
    969                                            item.beam_center.z, 
    970                                            {"unit": item.beam_center_unit}) 
     945                                                item.beam_center.z, 
     946                                                {"unit": item.beam_center_unit}) 
    971947            if written == True: 
    972948                self.append(center, det) 
    973                  
     949 
    974950            pix = self.create_element("pixel_size") 
    975951            written = self.write_node(pix, "x", item.pixel_size.x, 
    976                                  {"unit": item.pixel_size_unit}) 
     952                                      {"unit": item.pixel_size_unit}) 
    977953            written = written | self.write_node(pix, "y", item.pixel_size.y, 
    978                                            {"unit": item.pixel_size_unit}) 
     954                                                {"unit": item.pixel_size_unit}) 
    979955            written = written | self.write_node(pix, "z", item.pixel_size.z, 
    980                                            {"unit": item.pixel_size_unit}) 
     956                                                {"unit": item.pixel_size_unit}) 
    981957            written = written | self.write_node(det, "slit_length", 
    982                                            item.slit_length, 
    983                                            {"unit": item.slit_length_unit}) 
     958                                                item.slit_length, 
     959                                                {"unit": item.slit_length_unit}) 
    984960            if written == True: 
    985961                self.append(pix, det) 
    986      
    987      
     962 
    988963    def _write_process_notes(self, datainfo, entry_node): 
    989964        """ 
    990965        Writes the process notes to the XML file 
    991          
     966 
    992967        :param datainfo: The Data1D object the information is coming from 
    993968        :param entry_node: lxml node ElementTree object to be appended to 
    994          
     969 
    995970        """ 
    996971        for item in datainfo.process: 
    997972            node = self.create_element("SASprocess") 
    998973            self.append(node, entry_node) 
    999  
    1000974            self.write_node(node, "name", item.name) 
    1001975            self.write_node(node, "date", item.date) 
     
    1009983            if len(item.notes) == 0: 
    1010984                self.write_node(node, "SASprocessnote", "") 
    1011      
    1012      
     985 
    1013986    def _write_notes(self, datainfo, entry_node): 
    1014987        """ 
    1015988        Writes the notes to the XML file and creates an empty note if none 
    1016989        exist 
    1017          
     990 
    1018991        :param datainfo: The Data1D object the information is coming from 
    1019992        :param entry_node: lxml node ElementTree object to be appended to 
    1020          
     993 
    1021994        """ 
    1022995        if len(datainfo.notes) == 0: 
     
    10281001                self.write_text(node, item) 
    10291002                self.append(node, entry_node) 
    1030      
    1031      
     1003 
    10321004    def _check_origin(self, entry_node, doc): 
    10331005        """ 
     
    10361008        If the calling function was not the cansas reader, return a minidom 
    10371009        object rather than an lxml object. 
    1038          
     1010 
    10391011        :param entry_node: lxml node ElementTree object to be appended to 
    10401012        :param doc: entire xml tree 
     
    10521024            entry_node = node_list.item(0) 
    10531025        return entry_node 
    1054      
    1055      
     1026 
    10561027    def _to_xml_doc(self, datainfo): 
    10571028        """ 
    10581029        Create an XML document to contain the content of a Data1D 
    1059          
     1030 
    10601031        :param datainfo: Data1D object 
    10611032        """ 
    10621033        if not issubclass(datainfo.__class__, Data1D): 
    10631034            raise RuntimeError, "The cansas writer expects a Data1D instance" 
    1064          
     1035 
    10651036        # Get PIs and create root element 
    10661037        pi_string = self._get_pi_string() 
    1067          
     1038 
    10681039        # Define namespaces and create SASroot object 
    10691040        main_node = self._create_main_node() 
    1070          
     1041 
    10711042        # Create ElementTree, append SASroot and apply processing instructions 
    10721043        base_string = pi_string + self.to_string(main_node) 
    10731044        base_element = self.create_element_from_string(base_string) 
    10741045        doc = self.create_tree(base_element) 
    1075          
     1046 
    10761047        # Create SASentry Element 
    10771048        entry_node = self.create_element("SASentry") 
    10781049        root = doc.getroot() 
    10791050        root.append(entry_node) 
    1080          
     1051 
    10811052        # Add Title to SASentry 
    10821053        self.write_node(entry_node, "Title", datainfo.title) 
    1083          
     1054 
    10841055        # Add Run to SASentry 
    10851056        self._write_run_names(datainfo, entry_node) 
    1086          
     1057 
    10871058        # Add Data info to SASEntry 
    10881059        self._write_data(datainfo, entry_node) 
    1089          
     1060 
    10901061        # Transmission Spectrum Info 
    10911062        self._write_trans_spectrum(datainfo, entry_node) 
    1092          
     1063 
    10931064        # Sample info 
    10941065        self._write_sample_info(datainfo, entry_node) 
    1095          
     1066 
    10961067        # Instrument info 
    10971068        instr = self._write_instrument(datainfo, entry_node) 
    1098          
     1069 
    10991070        #   Source 
    11001071        self._write_source(datainfo, instr) 
    1101          
     1072 
    11021073        #   Collimation 
    11031074        self._write_collimation(datainfo, instr) 
     
    11051076        #   Detectors 
    11061077        self._write_detectors(datainfo, instr) 
    1107              
     1078 
    11081079        # Processes info 
    11091080        self._write_process_notes(datainfo, entry_node) 
    1110                  
     1081 
    11111082        # Note info 
    1112         self._write_notes(datainfo, entry_node)  
    1113          
     1083        self._write_notes(datainfo, entry_node) 
     1084 
    11141085        # Return the document, and the SASentry node associated with 
    11151086        #      the data we just wrote 
    11161087        # If the calling function was not the cansas reader, return a minidom 
    1117         #      object rather than an lxml object.         
     1088        #      object rather than an lxml object. 
    11181089        entry_node = self._check_origin(entry_node, doc) 
    1119          
     1090 
    11201091        return doc, entry_node 
    1121      
    1122      
     1092 
    11231093    def write_node(self, parent, name, value, attr=None): 
    11241094        """ 
     
    11281098        :param value: value of the child text node 
    11291099        :param attr: attribute dictionary 
    1130          
     1100 
    11311101        :return: True if something was appended, otherwise False 
    11321102        """ 
     
    11351105            return True 
    11361106        return False 
    1137      
    1138              
     1107 
    11391108    def write(self, filename, datainfo): 
    11401109        """ 
    11411110        Write the content of a Data1D as a CanSAS XML file 
    1142          
     1111 
    11431112        :param filename: name of the file to write 
    11441113        :param datainfo: Data1D object 
     
    11531122                  pretty_print=True, xml_declaration=True) 
    11541123        file_ref.close() 
    1155      
    1156      
     1124 
    11571125    # DO NOT REMOVE - used in saving and loading panel states. 
    11581126    def _store_float(self, location, node, variable, storage, optional=True): 
     
    11621130        with the destination. The value is expected to 
    11631131        be a float. 
    1164          
     1132 
    11651133        The xpath location might or might not exist. 
    11661134        If it does not exist, nothing is done 
    1167         
     1135 
    11681136        :param location: xpath location to fetch 
    11691137        :param node: node to read the data from 
     
    11801148        except: 
    11811149            value = None 
    1182              
     1150 
    11831151        if value is not None: 
    11841152            # If the entry has units, check to see that they are 
     
    11931161                        try: 
    11941162                            conv = Converter(units) 
    1195                             exec "storage.%s = %g" % (variable, 
    1196                                           conv(value, units=local_unit)) 
     1163                            exec "storage.%s = %g" % \ 
     1164                                (variable, conv(value, units=local_unit)) 
    11971165                        except: 
    11981166                            _, exc_value, _ = sys.exc_info() 
     
    12181186            else: 
    12191187                exec "storage.%s = value" % variable 
    1220                  
    1221      
     1188 
    12221189    # DO NOT REMOVE - used in saving and loading panel states. 
    12231190    def _store_content(self, location, node, variable, storage): 
     
    12251192        Get the content of a xpath location and store 
    12261193        the result. The value is treated as a string. 
    1227          
     1194 
    12281195        The xpath location might or might not exist. 
    12291196        If it does not exist, nothing is done 
    1230          
     1197 
    12311198        :param location: xpath location to fetch 
    12321199        :param node: node to read the data from 
    12331200        :param variable: name of the data member to store it in [string] 
    12341201        :param storage: data object that has the 'variable' data member 
    1235          
     1202 
    12361203        :return: return a list of errors 
    12371204        """ 
Note: See TracChangeset for help on using the changeset viewer.