Exemplo n.º 1
0
    def from_dict(cls, data):
        """Create a ApertureModifierSet from a dictionary.

        Note that the dictionary must be a non-abridged version for this
        classmethod to work.

        Args:
            data: Dictionary describing the ApertureModifierSet.
        """
        assert data['type'] == cls.__name__, \
            'Expected {}. Got {}.'.format(cls.__name__, data['type'])
        extc = dict_to_modifier(data['exterior_modifier']) \
            if 'exterior_modifier' in data and data['exterior_modifier'] \
            is not None else None
        intc = dict_to_modifier(data['interior_modifier']) \
            if 'interior_modifier' in data and data['interior_modifier'] \
            is not None else None
        egc = dict_to_modifier(data['exterior_glass_modifier']) \
            if 'exterior_glass_modifier' in data and data['exterior_glass_modifier'] \
            is not None else None
        igc = dict_to_modifier(data['interior_glass_modifier']) \
            if 'interior_glass_modifier' in data and data['interior_glass_modifier'] \
            is not None else None
        ohc = dict_to_modifier(data['overhead_modifier']) \
            if 'overhead_modifier' in data and data['overhead_modifier'] \
            is not None else None
        return cls(extc, intc, egc, igc, ohc)
Exemplo n.º 2
0
def modifier_from_rad(modifier_rad, output_file):
    """Translate a Modifier JSON file to a honeybee JSON as an array of modifiers.
    \n
    Args:
        modifier_rad: Full path to a Modifier .rad or .mat file. Only the modifiers
            and materials in this file will be extracted.
    """
    try:
        # re-serialize the Modifiers to Python
        mod_objs = []
        with open(modifier_rad) as f:
            rad_dicts = string_to_dicts(f.read())
            for mod_dict in rad_dicts:
                mod_objs.append(dict_to_modifier(mod_dict))

        # create the honeybee dictionaries
        json_dicts = [mod.to_dict() for mod in mod_objs]

        # write out the JSON file
        output_file.write(json.dumps(json_dicts))
    except Exception as e:
        _logger.exception('Modifier translation failed.\n{}'.format(e))
        sys.exit(1)
    else:
        sys.exit(0)
def load_modifiers_from_folder(modifier_lib_folder):
    """Load all of the material layer objects from a modifier standards folder.

    Args:
        modifier_lib_folder: Path to a modifiers sub-folder within a
            honeybee standards folder.
    """
    user_modifiers = {}
    for f in os.listdir(modifier_lib_folder):
        f_path = os.path.join(modifier_lib_folder, f)
        if os.path.isfile(f_path):
            if f_path.endswith('.mat') or f_path.endswith('.rad'):
                with open(f_path) as f:
                    try:
                        rad_dicts = string_to_dicts(f.read())
                        for mod_dict in rad_dicts:
                            mod = dict_to_modifier(mod_dict)
                            mod.lock()
                            user_modifiers[mod.identifier] = mod
                    except ValueError:
                        pass  # empty rad file with no modifiers in them
            if f_path.endswith('.json'):
                with open(f_path) as json_file:
                    data = json.load(json_file)
                if 'type' in data:  # single object
                    load_modifier_object(data, user_modifiers)
                else:  # a collection of several objects
                    for mod_identifier in data:
                        load_modifier_object(data[mod_identifier],
                                             user_modifiers)
    return user_modifiers
Exemplo n.º 4
0
    def from_primitive_dict(cls, primitive_dict):
        """Initialize mirror from a primitive dict.

        Args:
            data: A dictionary in the format below.

        .. code-block:: python

            {
            "modifier": {},  # primitive modifier (Default: None)
            "type": "mirror",  # primitive type
            "identifier": "",  # primitive identifier
            "display_name": "",  # primitive display name
            "values": [],  # values
            "dependencies": []
            }
        """
        cls._dict_type_check(cls.__name__, primitive_dict)
        modifier, dependencies = cls.filter_dict_input(primitive_dict)
        if len(primitive_dict['values'][0]) == 1:
            # find name
            alt_id = primitive_dict['values'][0][0]
            if alt_id == 'void':
                alternate_material = VOID
            elif isinstance(alt_id, dict):
                try:  # see if the mutil module has already been imported
                    mutil
                except NameError:
                    # import the module here to avoid a circular import
                    import honeybee_radiance.mutil as mutil
                alternate_material = mutil.dict_to_modifier(alt_id)
            else:
                alt_mats = [d for d in dependencies if d.identifier == alt_id]

                assert len(alt_mats) == 1, \
                    'Failed to find alternate material for mirror: "{}" in ' \
                    'dependencies.'.format(alt_id)

                # remove it from dependencies
                alternate_material = alt_mats[0]
                dependencies.remove(alternate_material)
        else:
            alternate_material = None

        values = primitive_dict['values'][2]
        cls_ = cls(identifier=primitive_dict["identifier"],
                   r_reflectance=values[0],
                   g_reflectance=values[1],
                   b_reflectance=values[2],
                   modifier=modifier,
                   alternate_material=alternate_material,
                   dependencies=dependencies)
        if 'display_name' in primitive_dict \
                and primitive_dict['display_name'] is not None:
            cls_.display_name = primitive_dict['display_name']

        # this might look redundant but it is NOT. see glass for explanation.
        cls_.values = primitive_dict['values']
        return cls_
Exemplo n.º 5
0
    def from_dict(cls, data):
        """Create a ModifierSet from a dictionary.

        Note that the dictionary must be a non-abridged version for this
        classmethod to work.

        Args:
            data: Dictionary describing the ModifierSet in the following format.

        .. code-block:: python

            {
            'type': 'ModifierSet',
            'identifier': str,  # ModifierSet identifier
            "display_name": str,  # ModifierSet display name
            'wall_set': {},  # WallModifierSet dictionary
            'floor_set': {},  # FloorSet dictionary
            'roof_ceiling_set': {},  # RoofCeilingModifierSet dictionary
            'aperture_set': {},  # ApertureModifierSet dictionary
            'door_set': {},  # DoorModifierSet dictionary
            'shade_set': {},  # ShadeModifierSet dictionary
            'air_boundary_modifier': {},  # AirBoundary dictionary
            }
        """
        assert data['type'] == 'ModifierSet', \
            'Expected ModifierSet. Got {}.'.format(data['type'])

        # build each of the sub-construction sets
        wall_set = WallModifierSet.from_dict(data['wall_set']) if 'wall_set' \
            in data and data['wall_set'] is not None else None
        floor_set = FloorModifierSet.from_dict(data['floor_set']) if 'floor_set' \
            in data and data['floor_set'] is not None else None
        roof_ceiling_set = RoofCeilingModifierSet.from_dict(data['roof_ceiling_set']) \
            if 'roof_ceiling_set' in data and \
            data['roof_ceiling_set'] is not None else None
        aperture_set = ApertureModifierSet.from_dict(data['aperture_set']) if \
            'aperture_set' in data and data['aperture_set'] is not None else None
        door_set = DoorModifierSet.from_dict(data['door_set']) if \
            'door_set' in data and data['door_set'] is not None else None
        shade_set = ShadeModifierSet.from_dict(data['shade_set']) if \
            'shade_set' in data and data['shade_set'] is not None else None
        air_mod = dict_to_modifier(data['air_boundary_modifier']) \
            if 'air_boundary_modifier' in data and \
            data['air_boundary_modifier'] is not None else None

        new_obj = cls(data['identifier'], wall_set, floor_set,
                      roof_ceiling_set, aperture_set, door_set, shade_set,
                      air_mod)
        if 'display_name' in data and data['display_name'] is not None:
            new_obj.display_name = data['display_name']
        return new_obj
Exemplo n.º 6
0
    def filter_dict_input(input_dict):
        """Filter a dictionary of a Primitive to get modifier and dependency objects."""
        try:  # see if the mutil module has already been imported
            mutil
        except NameError:
            # import the module here (instead of at top) to avoid a circular import
            import honeybee_radiance.mutil as mutil  # imports all modifiers classes

        # try to get modifier
        if 'modifier' not in input_dict or input_dict['modifier'] is None or \
                input_dict['modifier'] == 'void':
            modifier = VOID
        else:
            modifier = mutil.dict_to_modifier(input_dict['modifier'])

        if 'dependencies' not in input_dict:
            dependencies = []
        else:
            dependencies = [
                mutil.dict_to_modifier(dep) for dep in input_dict['dependencies']
            ]

        return modifier, dependencies
Exemplo n.º 7
0
    def from_dict(cls, data):
        """Initialize Mirror from a dictionary.

        Args:
            data: A dictionary in the format below.

        .. code-block:: python

            {
            "type": "mirror",  # Material type
            "identifier": "",  # Material identifier
            "display_name": string  # Material display name
            "r_reflectance": float,  # Reflectance for red
            "g_reflectance": float,  # Reflectance for green
            "b_reflectance": float,  # Reflectance for blue
            "modifier": {},  # Material modifier (Default: None)
            "alternate_material": {},  # optional alternate material
            "dependencies": []
            }
        """
        assert 'type' in data, 'Input dictionary is missing "type".'
        if data['type'] != cls.__name__.lower():
            raise ValueError(
                'Type must be %s not %s.' % (cls.__name__.lower(), data['type'])
            )
        modifier, dependencies = Material.filter_dict_input(data)
        if 'alternate_material' in data and data['alternate_material']:
            # alternate material
            if data['alternate_material'] == 'void':
                alternate_material = VOID
            else:
                try:  # see if the mutil module has already been imported
                    mutil
                except NameError:
                    # import the module here to avoid a circular import
                    import honeybee_radiance.mutil as mutil
                alternate_material = mutil.dict_to_modifier(data['alternate_material'])
        else:
            alternate_material = None

        new_obj = cls(identifier=data["identifier"],
                      r_reflectance=data["r_reflectance"],
                      g_reflectance=data["g_reflectance"],
                      b_reflectance=data["b_reflectance"],
                      modifier=modifier,
                      alternate_material=alternate_material,
                      dependencies=dependencies)
        if 'display_name' in data and data['display_name'] is not None:
            new_obj.display_name = data['display_name']
        return new_obj
Exemplo n.º 8
0
        mod = m_class.from_dict(mod_dict)
        mod.lock()
        assert mod_dict['identifier'] not in _default_mods, 'Cannot overwrite ' \
            'default modifier "{}".'.format(mod_dict['identifier'])
        _loaded_modifiers[mod_dict['identifier']] = mod
    except (TypeError, KeyError):
        pass  # not a Honeybee Modifier JSON; possibly a comment


for f in os.listdir(folders.modifier_lib):
    f_path = os.path.join(folders.modifier_lib, f)
    if os.path.isfile(f_path):
        if f_path.endswith('.mat') or f_path.endswith('.rad'):
            with open(f_path) as f:
                try:
                    rad_dicts = string_to_dicts(f.read())
                    for mod_dict in rad_dicts:
                        mod = dict_to_modifier(mod_dict)
                        mod.lock()
                        _loaded_modifiers[mod.identifier] = mod
                except ValueError:
                    pass  # empty rad file with no modifiers in them
        if f_path.endswith('.json'):
            with open(f_path) as json_file:
                data = json.load(json_file)
            if 'type' in data:  # single object
                load_modifier_object(data)
            else:  # a collection of several objects
                for mod_identifier in data:
                    load_modifier_object(data[mod_identifier])