Beispiel #1
0
    def export_to_xml(self, path='cross_sections.xml'):
        """Export cross section data library to an XML file.

        Parameters
        ----------
        path : str
            Path to file to write. Defaults to 'cross_sections.xml'.

        """
        root = ET.Element('cross_sections')

        # Determine common directory for library paths
        common_dir = os.path.dirname(
            os.path.commonprefix([lib['path'] for lib in self.libraries]))
        if common_dir == '':
            common_dir = '.'

        if os.path.relpath(common_dir, os.path.dirname(str(path))) != '.':
            dir_element = ET.SubElement(root, "directory")
            dir_element.text = os.path.realpath(common_dir)

        for library in self.libraries:
            if library['type'] == "depletion_chain":
                lib_element = ET.SubElement(root, "depletion_chain")
            else:
                lib_element = ET.SubElement(root, "library")
                lib_element.set('materials', ' '.join(library['materials']))
            lib_element.set('path', os.path.relpath(library['path'],
                                                    common_dir))
            lib_element.set('type', library['type'])

        # Clean the indentation to be user-readable
        clean_indentation(root)

        # Write XML file
        reorder_attributes(root)  # TODO: Remove when support is Python 3.8+
        tree = ET.ElementTree(root)
        tree.write(str(path),
                   xml_declaration=True,
                   encoding='utf-8',
                   method='xml')
Beispiel #2
0
    def export_to_xml(self, path='geometry.xml', remove_surfs=False):
        """Export geometry to an XML file.

        Parameters
        ----------
        path : str
            Path to file to write. Defaults to 'geometry.xml'.
        remove_surfs : bool
            Whether or not to remove redundant surfaces from the geometry when
            exporting

            .. versionadded:: 0.12

        """
        # Find and remove redundant surfaces from the geometry
        if remove_surfs:
            self.remove_redundant_surfaces()

        # Create XML representation
        root_element = ET.Element("geometry")
        self.root_universe.create_xml_subelement(root_element, memo=set())

        # Sort the elements in the file
        root_element[:] = sorted(root_element,
                                 key=lambda x: (x.tag, int(x.get('id'))))

        # Clean the indentation in the file to be user-readable
        xml.clean_indentation(root_element)

        # Check if path is a directory
        p = Path(path)
        if p.is_dir():
            p /= 'geometry.xml'

        # Write the XML Tree to the geometry.xml file
        xml.reorder_attributes(
            root_element)  # TODO: Remove when support is Python 3.8+
        tree = ET.ElementTree(root_element)
        tree.write(str(p), xml_declaration=True, encoding='utf-8')