diff --git a/honeybee_energy/cli/translate.py b/honeybee_energy/cli/translate.py index c13fe6233..50620383a 100644 --- a/honeybee_energy/cli/translate.py +++ b/honeybee_energy/cli/translate.py @@ -120,9 +120,7 @@ def ddy_from_epw(epw_file, sim_par): # load the project information if it exists if project_info is not None: - with open(project_info) as json_file: - data = json.load(json_file) - project_info = ProjectInfo.from_dict(data) + project_info = ProjectInfo.from_json(project_info) if sim_par_json is None or not os.path.isfile(sim_par_json): sim_par = SimulationParameter() @@ -344,9 +342,7 @@ def model_to_osm( # load the project information if it exists if project_info is not None: - with open(project_info) as json_file: - data = json.load(json_file) - project_info = ProjectInfo.from_dict(data) + project_info = ProjectInfo.from_json(project_info) if epw_file is None: epw_file = project_info.default_epw_file @@ -580,9 +576,7 @@ def model_to_idf( """ # load the project information if it exists if project_info is not None: - with open(project_info) as json_file: - data = json.load(json_file) - project_info = ProjectInfo.from_dict(data) + project_info = ProjectInfo.from_json(project_info) # load simulation parameters or generate default ones if sim_par_json is not None: @@ -637,7 +631,7 @@ def model_to_idf( exists=True, file_okay=True, dir_okay=False, resolve_path=True)) @click.option('--project-info', '-pi', help='Full path to a ladybug ProjectInfo ' 'JSON that describes criteria about the project site. When specified, ' - 'this will result in the location, north_angle and building type ' + 'this will result in the location, north and building type ' 'written into the gbXML.', default=None, show_default=True, type=click.Path(exists=True, file_okay=True, dir_okay=False, resolve_path=True)) @@ -814,7 +808,7 @@ def model_to_gbxml( model_file: Full path to a Honeybee Model file (HBJSON or HBpkl). project_info: Full path to a ladybug ProjectInfo JSON that describes criteria about the project site. When specified, this will result - in the location, north_angle and building type written into the gbXML. + in the location, north and building type written into the gbXML. ip_units: A boolean to note whether the geometry, space loads, and construction properties are reported in IP units (True) or SI units (False). (Default: False). @@ -931,9 +925,7 @@ def model_to_gbxml( if full_geometry: include_shell_geometry, include_space_boundaries = True, True if project_info is not None: - with open(project_info) as json_file: - data = json.load(json_file) - project_info = ProjectInfo.from_dict(data) + project_info = ProjectInfo.from_json(project_info) model = Model.from_file(model_file) gbxml_str = model.to_gbxml( project_info=project_info, ip_units=ip_units, @@ -1068,6 +1060,12 @@ def model_to_trace_gbxml( @translate.command('model-to-sdd') @click.argument('model-file', type=click.Path( exists=True, file_okay=True, dir_okay=False, resolve_path=True)) +@click.option('--project-info', '-pi', help='Full path to a ladybug ProjectInfo ' + 'JSON that describes criteria about the project site. When specified, ' + 'this will result in the north written into the SDD XML.', + default=None, show_default=True, + type=click.Path(exists=True, file_okay=True, dir_okay=False, + resolve_path=True)) @click.option('--geometry-ids/--geometry-names', ' /-gn', help='Flag to note whether a ' 'cleaned version of all geometry display names should be used instead ' 'of identifiers when translating the Model to IDF. Using this flag will ' @@ -1089,7 +1087,7 @@ def model_to_trace_gbxml( @click.option('--output-file', '-f', help='Optional SDD file to output the string ' 'of the translation. By default it printed out to stdout.', default='-', type=click.Path(file_okay=True, dir_okay=False, resolve_path=True)) -def model_to_sdd_cli(model_file, geometry_ids, resource_ids, output_file): +def model_to_sdd_cli(model_file, project_info, geometry_ids, resource_ids, output_file): """Translate a Honeybee Model file to a SDD file. \b @@ -1099,7 +1097,7 @@ def model_to_sdd_cli(model_file, geometry_ids, resource_ids, output_file): try: geo_names = not geometry_ids res_names = not resource_ids - model_to_sdd(model_file, geo_names, res_names, output_file) + model_to_sdd(model_file, project_info, geo_names, res_names, output_file) except Exception as e: _logger.exception('Model translation failed.\n{}'.format(e)) sys.exit(1) @@ -1108,13 +1106,17 @@ def model_to_sdd_cli(model_file, geometry_ids, resource_ids, output_file): def model_to_sdd( - model_file, geometry_names=False, resource_names=False, output_file=None, + model_file, project_info=None, geometry_names=False, resource_names=False, + output_file=None, geometry_ids=True, resource_ids=True ): """Translate a Honeybee Model file to a SDD file. Args: model_file: Full path to a Honeybee Model file (HBJSON or HBpkl). + project_info: Full path to a ladybug ProjectInfo JSON that describes + criteria about the project site. When specified, this will result + in the north written into the SDD XML. geometry_names: Boolean to note whether a cleaned version of all geometry display names should be used instead of identifiers when translating the Model to OSM and IDF. Using this flag will affect all Rooms, Faces, @@ -1137,7 +1139,8 @@ def model_to_sdd( """ # check that honeybee-openstudio is installed try: - from honeybee_openstudio.openstudio import openstudio + from honeybee_openstudio.openstudio import openstudio, OSModel + from honeybee_openstudio.simulation import simulation_parameter_to_openstudio from honeybee_openstudio.writer import model_to_openstudio except ImportError as e: # honeybee-openstudio is not installed raise ImportError('{}\n{}'.format(HB_OS_MSG, e)) @@ -1145,6 +1148,13 @@ def model_to_sdd( # translate the model to an OpenStudio Model model = Model.from_file(model_file) + # load the project information if it exists + sim_par = None + if project_info is not None: + project_info = ProjectInfo.from_json(project_info) + sim_par = SimulationParameter() + sim_par.assign_from_project_info(project_info, overwrite=True) + if geometry_names: # rename all face geometry so that it is easy to identify model.reset_ids() # sets the identifiers based on the display_name for room in model.rooms: @@ -1160,9 +1170,11 @@ def model_to_sdd( room.rename_doors_by_attribute() model.reset_ids() + os_model = OSModel() + if sim_par is not None: + simulation_parameter_to_openstudio(sim_par, os_model) os_model = model_to_openstudio( - model, - use_simple_window_constructions=True, + model, os_model, use_simple_window_constructions=True, use_resource_names=resource_names ) diff --git a/honeybee_energy/simulation/parameter.py b/honeybee_energy/simulation/parameter.py index e5712e489..ec9ef5067 100644 --- a/honeybee_energy/simulation/parameter.py +++ b/honeybee_energy/simulation/parameter.py @@ -42,6 +42,10 @@ class SimulationParameter(object): degrees. 90 is West and 270 is East (Default: 0). terrain_type: Text for the terrain type in which the model sits. Choose from: 'Ocean', 'Country', 'Suburbs', 'Urban', 'City'.(Default: 'City') + location: An optional Ladybug Location object to describe the exact latitude, + longitude, time zone and elevation of the project site. If None + the location specified within the EPW file will be used for the + simulation. (Default: None). Properties: * output @@ -54,16 +58,19 @@ class SimulationParameter(object): * north_angle * north_vector * terrain_type + * location """ __slots__ = ('_output', '_run_period', '_timestep', '_simulation_control', '_shadow_calculation', '_sizing_parameter', '_north_angle', - '_north_vector', '_terrain_type') + '_north_vector', '_terrain_type', '_location') VALIDTIMESTEPS = (1, 2, 3, 4, 5, 6, 10, 12, 15, 20, 30, 60) TERRAIN_TYPES = ('Ocean', 'Country', 'Suburbs', 'Urban', 'City') - def __init__(self, output=None, run_period=None, timestep=6, - simulation_control=None, shadow_calculation=None, sizing_parameter=None, - north_angle=0, terrain_type='City'): + def __init__( + self, output=None, run_period=None, timestep=6, + simulation_control=None, shadow_calculation=None, sizing_parameter=None, + north_angle=0, terrain_type='City', location=None + ): """Initialize SimulationParameter.""" self.output = output self.run_period = run_period @@ -73,6 +80,7 @@ def __init__(self, output=None, run_period=None, timestep=6, self.sizing_parameter = sizing_parameter self.north_angle = north_angle self.terrain_type = terrain_type + self.location = location @property def output(self): @@ -229,6 +237,18 @@ def terrain_type(self, value): else: self._terrain_type = 'City' + @property + def location(self): + """Get or set an optional Location object to override that within the EPW.""" + return self._location + + @location.setter + def location(self, value): + if value is not None: + assert isinstance(value, Location), 'Expected Location ' \ + 'for SimulationParameter location. Got {}.'.format(type(value)) + self._location = value + def building_idf(self, identifier='Building'): """Get an IDF string for an IDF Building object. @@ -275,6 +295,7 @@ def assign_from_project_info(self, project_info, overwrite=False): self.north_angle = project_info.north_angle self.sizing_parameter.design_days = project_info.default_design_days self.sizing_parameter.climate_zone = project_info.ashrae_climate_zone + self.location = project_info.location if len(project_info.building_type) == 1: self.sizing_parameter.building_type = project_info.building_type[0] elif len(project_info.building_type) > 1: @@ -287,6 +308,8 @@ def assign_from_project_info(self, project_info, overwrite=False): self.sizing_parameter.design_days = project_info.default_design_days if not self.sizing_parameter.climate_zone: self.sizing_parameter.climate_zone = project_info.ashrae_climate_zone + if not self.location: + self.location = project_info.location if not self.sizing_parameter.building_type: if len(project_info.building_type) == 1: self.sizing_parameter.building_type = project_info.building_type[0] @@ -420,12 +443,16 @@ def from_idf(cls, idf_string): loc_str = loc_pattern.findall(idf_string)[0] except IndexError: # No Site:Location in the file. loc_str = None - location = Location.from_idf(loc_str) if loc_str is not None else None + if loc_str is not None: + location = Location.from_idf(loc_str) + loc = location if parse_idf_string(loc_str)[-1].title() == 'Yes' else None + else: + location, loc = None, None sizing_par = SizingParameter.from_idf( [dy[0] for dy in ddy_p.findall(idf_string)], sizing_str, location) return cls(output, run_period, timestep, sim_control, shadow_calc, - sizing_par, north_angle, terrain) + sizing_par, north_angle, terrain, loc) @classmethod def from_dict(cls, data): @@ -444,6 +471,9 @@ def from_dict(cls, data): "simulation_control": {}, # Honeybee SimulationControl dictionary "shadow_calculation": {}, # Honeybee ShadowCalculation dictionary "sizing_parameter": {} # Honeybee SizingParameter dictionary + "north_angle": 15, # North angle in degrees + "terrain_type": "City", # Text for the terrain type in which the model sits + "location": {} # Ladybug Location dictionary } """ assert data['type'] == 'SimulationParameter', \ @@ -471,9 +501,14 @@ def from_dict(cls, data): terrain_type = 'City' if 'terrain_type' in data and data['terrain_type'] is not None: terrain_type = data['terrain_type'] + location = None + if 'location' in data and data['location'] is not None: + location = Location.from_dict(data['location']) - return cls(output, run_period, timestep, simulation_control, - shadow_calculation, sizing_parameter, north_angle, terrain_type) + return cls( + output, run_period, timestep, simulation_control, shadow_calculation, + sizing_parameter, north_angle, terrain_type, location + ) def to_idf(self, identifier='Building'): """Get an EnergyPlus string representation of the SimulationParameter. @@ -571,6 +606,13 @@ def to_idf(self, identifier='Building'): Site:WaterMainsTemperature, CorrelationFromWeatherFile; !- calculation method + Site:Location, + New York JFK, + 40.65, !Latitude + -73.8, !Longitude + -5.0, !Time Zone + 5.0, !Elevation + Yes; !Use location for simulation """ sim_param_str = ['!- ==========================================\n' @@ -616,10 +658,17 @@ def to_idf(self, identifier='Building'): sim_param_str.append(self.building_idf(identifier)) sim_param_str.append(self.water_mains_idf()) + # write the location object if specified + if self.location is not None: + last_line = '\n Yes; !Use location for simulation' + loc_str = self.location.to_idf().replace(';', ',') + last_line + sim_param_str.append(loc_str) + return '\n\n'.join(sim_param_str) def to_dict(self): """SimulationParameter dictionary representation.""" + loc = self.location.to_dict() if self.location is not None else None return { 'type': 'SimulationParameter', 'output': self.output.to_dict(), @@ -629,7 +678,8 @@ def to_dict(self): 'shadow_calculation': self.shadow_calculation.to_dict(), 'sizing_parameter': self.sizing_parameter.to_dict(), 'north_angle': self.north_angle, - 'terrain_type': self.terrain_type + 'terrain_type': self.terrain_type, + 'location': loc } def duplicate(self): @@ -644,14 +694,18 @@ def __copy__(self): return SimulationParameter( self.output.duplicate(), self.run_period.duplicate(), self.timestep, self.simulation_control.duplicate(), self.shadow_calculation.duplicate(), - self.sizing_parameter.duplicate(), self.north_angle, self.terrain_type) + self.sizing_parameter.duplicate(), + self.north_angle, self.terrain_type, self.location + ) def __key(self): """A tuple based on the object properties, useful for hashing.""" - return (hash(self.output), hash(self.run_period), self.timestep, - hash(self.simulation_control), - hash(self.shadow_calculation), hash(self.sizing_parameter), - self.north_angle, self.terrain_type) + return ( + hash(self.output), hash(self.run_period), self.timestep, + hash(self.simulation_control), + hash(self.shadow_calculation), hash(self.sizing_parameter), + self.north_angle, self.terrain_type, hash(self.location) + ) def __hash__(self): return hash(self.__key()) diff --git a/tests/json/project_info.json b/tests/json/project_info.json index 269f02cd5..30fee5d7d 100644 --- a/tests/json/project_info.json +++ b/tests/json/project_info.json @@ -1,6 +1,6 @@ { "type": "ProjectInfo", - "north_angle": 15.0, + "north": 15.0, "location": { "city": "New York JFK", "state": "-",