Ignore:
Timestamp:
Jul 21, 2016 10:47:25 AM (8 years ago)
Author:
lewis
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, ticket-1009, ticket-1094-headless, ticket-1242-2d-resolution, ticket-1243, ticket-1249, ticket885, unittest-saveload
Children:
94f4518
Parents:
8976865
Message:

Support loading BSL/OTOKO files with multiple frames

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/sas/sasgui/perspectives/file_converter/cansas_writer.py

    r8976865 reb8da5f  
    66class CansasWriter(CansasReader): 
    77 
    8     def write(self, filename, datainfo, sasentry_attrs=None): 
     8    def write(self, filename, frame_data, sasentry_attrs=None): 
    99        """ 
    1010        Write the content of a Data1D as a CanSAS XML file 
     
    1414        """ 
    1515        # Create XML document 
    16         doc, _ = self._to_xml_doc(datainfo, sasentry_attrs) 
     16        doc, _ = self._to_xml_doc(frame_data, sasentry_attrs) 
    1717        # Write the file 
    1818        file_ref = open(filename, 'w') 
     
    2424 
    2525 
    26     def _to_xml_doc(self, datainfo, sasentry_attrs=None): 
     26    def _to_xml_doc(self, frame_data, sasentry_attrs=None): 
    2727        """ 
    28         Create an XML document to contain the content of a Data1D 
     28        Create an XML document to contain the content of an array of Data1Ds 
    2929 
    30         :param datainfo: Data1D object 
     30        :param frame_data: An array of Data1D objects 
    3131        """ 
    32         if not issubclass(datainfo.__class__, Data1D): 
    33             raise RuntimeError, "The cansas writer expects a Data1D instance" 
     32        valid_class = all([issubclass(data.__class__, Data1D) for data in frame_data]) 
     33        if not valid_class: 
     34            raise RuntimeError, ("The cansas writer expects an array of " 
     35                "Data1D instances") 
    3436 
    3537        # Get PIs and create root element 
     
    4648        root.append(entry_node) 
    4749 
     50        # Use the first element in the array for writing metadata 
     51        datainfo = frame_data[0] 
    4852        # Add Title to SASentry 
    4953        self.write_node(entry_node, "Title", datainfo.title) 
     
    5155        self._write_run_names(datainfo, entry_node) 
    5256        # Add Data info to SASEntry 
    53         self._write_data(datainfo, entry_node) 
     57        self._write_data(frame_data, entry_node) 
    5458        # Transmission Spectrum Info 
    5559        self._write_trans_spectrum(datainfo, entry_node) 
     
    7074        # Return the document, and the SASentry node associated with 
    7175        #      the data we just wrote 
    72          
     76 
    7377        return doc, entry_node 
     78 
     79    def _write_data(self, frame_data, entry_node): 
     80        """ 
     81        Writes the I and Q data to the XML file 
     82 
     83        :param datainfo: The Data1D object the information is coming from 
     84        :param entry_node: lxml node ElementTree object to be appended to 
     85        """ 
     86        for datainfo in frame_data: 
     87            node = self.create_element("SASdata") 
     88            self.append(node, entry_node) 
     89 
     90            for i in range(len(datainfo.x)): 
     91                point = self.create_element("Idata") 
     92                node.append(point) 
     93                self.write_node(point, "Q", datainfo.x[i], 
     94                                {'unit': datainfo.x_unit}) 
     95                if len(datainfo.y) >= i: 
     96                    self.write_node(point, "I", datainfo.y[i], 
     97                                    {'unit': datainfo.y_unit}) 
     98                if datainfo.dy != None and len(datainfo.dy) > i: 
     99                    self.write_node(point, "Idev", datainfo.dy[i], 
     100                                    {'unit': datainfo.y_unit}) 
     101                if datainfo.dx != None and len(datainfo.dx) > i: 
     102                    self.write_node(point, "Qdev", datainfo.dx[i], 
     103                                    {'unit': datainfo.x_unit}) 
     104                if datainfo.dxw != None and len(datainfo.dxw) > i: 
     105                    self.write_node(point, "dQw", datainfo.dxw[i], 
     106                                    {'unit': datainfo.x_unit}) 
     107                if datainfo.dxl != None and len(datainfo.dxl) > i: 
     108                    self.write_node(point, "dQl", datainfo.dxl[i], 
     109                                    {'unit': datainfo.x_unit}) 
Note: See TracChangeset for help on using the changeset viewer.