Exemple #1
0
def unzip_fmux(archive, path='.'):
    """
    Unzip an FMUX.
    
    Looks for a model description XML file and returns the result in a dict with 
    the key words: 'model_desc'. If the file is not found an exception will be 
    raised.
    
    Parameters::
        
        archive --
            The archive file name.
            
        path --
            The path to the archive file.
            Default: Current directory.
            
    Raises::
    
        IOError the model description XML file is missing in the FMU.
    """
    tmpdir = unzip_unit(archive, path)
    fmux_files = get_files_in_archive(tmpdir)
    
    # check if all files have been found during unzip
    if fmux_files['model_desc'] == None:
        raise IOError('ModelDescription.xml not found in FMUX archive: '+str(archive))
    
    return fmux_files
Exemple #2
0
    def _get_fmu_target(self):
        '''Get fmu target, either ME or CS.  Only works for fmu version 2.0.
        
        Returns
        -------
        target : string
            'me' or 'cs' for model exchange or cosimulation
        
        '''

        if self.fmu_version == '2.0':
            tmpdir = core.unzip_unit(self.fmupath)
            element_tree = xmlparser._parse_XML(tmpdir + os.sep +
                                                'modelDescription.xml')
            root = element_tree.getroot()
            try:
                tag = root.find('ModelExchange').tag
            except:
                pass
            try:
                tag = root.find('CoSimulation').tag
            except:
                pass
            if tag == 'ModelExchange':
                target = 'me'
            else:
                target = 'cs'
        else:
            target = None

        return target
def unzip_fmux(archive, path='.'):
    """
    Unzip an FMUX.
    
    Looks for a model description XML file and returns the result in a dict with 
    the key words: 'model_desc'. If the file is not found an exception will be 
    raised.
    
    Parameters::
        
        archive --
            The archive file name.
            
        path --
            The path to the archive file.
            Default: Current directory.
            
    Raises::
    
        IOError the model description XML file is missing in the FMU.
    """
    tmpdir = unzip_unit(archive, path)
    fmux_files = get_files_in_archive(tmpdir)
    
    # check if all files have been found during unzip
    if fmux_files['model_desc'] == None:
        raise IOError('ModelDescription.xml not found in FMUX archive: '+str(archive))
    
    return fmux_files
Exemple #4
0
    def _get_fmu_variable_units(self):
        '''Get fmu model variable units.
        
        Returns
        -------
        fmu_variable_units : dictionary
            Dictionary where the keys are variable names and values are unit
            strings.  These unit strings can be used by 
            ``_get_unit_class_from_fmu_variable_units`` to get the 
            corresponding mpcpy unit class.
        
        '''

        tmpdir = core.unzip_unit(self.fmupath)
        element_tree = xmlparser._parse_XML(tmpdir + os.sep +
                                            'modelDescription.xml')
        root = element_tree.getroot()
        model_variables = root.find('ModelVariables')
        type_definitions = root.find('TypeDefinitions')
        variables = model_variables.getchildren()
        if type_definitions is not None:
            types = type_definitions.getchildren()
        fmu_variable_units = {}
        for variable in variables:
            real = variable.find('Real')
            if real is not None:
                items = real.items()
                attributes = []
                for attribute in items:
                    attributes.append(attribute[0])
                if 'unit' in attributes:
                    unit = real.get('unit')
                elif 'declaredType' in attributes and 'unit' not in attributes:
                    variable_type = real.get('declaredType')
                    for type_instance in types:
                        if variable_type == type_instance.get('name'):
                            if self.fmu_version == '1.0':
                                sub_type = type_instance.find('RealType')
                            elif self.fmu_version == '2.0':
                                sub_type = type_instance.find('Real')
                            else:
                                raise TypeError(
                                    'Cannot get variable units for fmu {0}.'.
                                    format(self.fmu_version))
                            if sub_type is not None:
                                unit = sub_type.get('unit')
                else:
                    unit = None

                fmu_variable_units[variable.get('name')] = unit

        return fmu_variable_units
Exemple #5
0
 def _load_xml_root(self):
     '''Load the xml root.
     
     Returns
     -------
     _xml_root : xml root object
         From element_tree.getroot().
     
     '''
     
     tmpdir = core.unzip_unit(self.fmupath);
     element_tree = xmlparser._parse_XML(tmpdir+os.sep + 'modelDescription.xml');
     shutil.rmtree(tmpdir)
     _xml_root = element_tree.getroot();
     
     return _xml_root