Skip to content

Commit

Permalink
Merge pull request #23 from pnnl/add_osstd_path
Browse files Browse the repository at this point in the history
Add openstudio-standards repository path to `create_openstudio_standards_data_json_ashrae_90_1`
  • Loading branch information
lymereJ authored Jan 5, 2024
2 parents 6aeb8df + e8f044b commit 040b987
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
fetch_table,
fetch_records_from_table_by_key_values,
)
from building_energy_standards_data.database_engine.assertions import check_path


def create_openstudio_standards_space_data_json_ashrae_90_1(
Expand Down Expand Up @@ -302,44 +303,59 @@ def create_openstudio_standards_space_data_json(


def create_openstudio_standards_data_json_ashrae_90_1(
conn: sqlite3.Connection, version_90_1: str
conn: sqlite3.Connection,
version_90_1: str,
osstd_repository_path: str,
prm: bool = False,
) -> None:
"""
Create and export data for a specific version of ASHRAE 90.1 to be used by OpenStudio Standards
:param conn (sqlite3.Connection): database connection
:param version_90_1 (str): code version of ASHRAE 90.1, e.g. "2004", "2007", etc.
:param osstd_repository_path (str): path of the local openstudio-standards repository
:param prm (bool): indicates if the prm data for the code version of 90.1 should be generated.
"""
check_path(osstd_repository_path)

# Dictionary that maps OpenStudio Standards JSON data file names to database table(s)
# The mapping defined here covers data that varies based on code version
prm_suffix = "_prm" if prm else ""
tables_to_export_90_1 = {
"chillers": ["hvac_minimum_requirement_chillers_90_1"],
"boilers": ["hvac_minimum_requirement_boilers_90_1"],
"furnaces": ["hvac_minimum_requirement_furnaces_90_1"],
"heat_rejection": ["hvac_minimum_requirement_heat_rejection_90_1"],
"motors": ["hvac_minimum_requirement_motors_90_1"],
"unitary_acs": ["hvac_minimum_requirement_unitary_air_conditioners_90_1"],
"chillers": [f"hvac_minimum_requirement_chillers_90_1{prm_suffix}"],
"boilers": [f"hvac_minimum_requirement_boilers_90_1{prm_suffix}"],
"furnaces": [f"hvac_minimum_requirement_furnaces_90_1{prm_suffix}"],
"heat_rejection": [f"hvac_minimum_requirement_heat_rejection_90_1{prm_suffix}"],
"motors": [f"hvac_minimum_requirement_motors_90_1{prm_suffix}"],
"unitary_acs": [
f"hvac_minimum_requirement_unitary_air_conditioners_90_1{prm_suffix}"
],
"water_source_heat_pumps_heating": [
"hvac_minimum_requirement_water_source_heat_pumps_heating_90_1"
f"hvac_minimum_requirement_water_source_heat_pumps_heating_90_1{prm_suffix}"
],
"water_source_heat_pumps": [
"hvac_minimum_requirement_water_source_heat_pumps_cooling_90_1"
f"hvac_minimum_requirement_water_source_heat_pumps_cooling_90_1{prm_suffix}"
],
"water_heaters": ["hvac_minimum_requirement_water_heaters_90_1"],
"heat_pumps": ["hvac_minimum_requirement_heat_pump_cooling_90_1"],
"heat_pumps_heating": ["hvac_minimum_requirement_heat_pump_heating_90_1"],
"economizers": ["system_requirement_economizer_90_1"],
"energy_recovery": ["system_requirement_energy_recovery_90_1"],
"construction_properties": ["envelope_requirement"],
"water_heaters": [f"hvac_minimum_requirement_water_heaters_90_1{prm_suffix}"],
"heat_pumps": [f"hvac_minimum_requirement_heat_pump_cooling_90_1{prm_suffix}"],
"heat_pumps_heating": [
f"hvac_minimum_requirement_heat_pump_heating_90_1{prm_suffix}"
],
"economizers": [f"system_requirement_economizer_90_1{prm_suffix}"],
"energy_recovery": [f"system_requirement_energy_recovery_90_1{prm_suffix}"],
"construction_properties": [f"envelope_requirement{prm_suffix}"],
}

# Generate and "export" the data to the correct location within the OpenStudio Standards repository
code = "ashrae_90_1_prm" if prm else "ashrae_90_1"
template = f"90.1-PRM-{version_90_1}" if prm else f"90.1-PRM-{version_90_1}"
create_openstudio_standards_code_version_data_json(
conn,
code="ashrae_90_1",
code=code,
code_version=version_90_1,
template=f"90.1-{version_90_1}",
template=template,
tables_to_export=tables_to_export_90_1,
osstd_repository_path=osstd_repository_path,
)

# The mapping defined here covers data that does NOT vary based on code version
Expand All @@ -352,7 +368,10 @@ def create_openstudio_standards_data_json_ashrae_90_1(

# Generate and "export" the data to the correct location within the OpenStudio Standards repository
create_openstudio_standards_code_data_json(
conn, code="ashrae_90_1", tables_to_export=tables_to_export_90_1
conn,
code="ashrae_90_1",
tables_to_export=tables_to_export_90_1,
osstd_repository_path=osstd_repository_path,
)


Expand All @@ -362,14 +381,18 @@ def create_openstudio_standards_code_version_data_json(
code_version: str,
template: str,
tables_to_export: dict,
osstd_repository_path: str = "./",
) -> None:
"""Extract code- and code version-specific OpenStudio Standards data from the database and export it to JSON files
:param conn (sqlite3.Connection): database connection
:param code (str): name of the building energy code, e.g. "ashrae_90_1"
:param code_version (str): verion of the code, e.g. "2004", "2007", etc.
:param template (str): template corresponding to the code and code version, e.g. "90.1-2004", or "90.1-2007"
:param tables_to_export (dict): mapping of name of OpenStudio Standards JSON file name to corresponding tables from the database that contains the data for the code and code version data
:param osstd_repository_path (str): path of the local openstudio-standards repository
"""
check_path(osstd_repository_path)

for table_type, tables in tables_to_export.items():
logging.info(f"Creating {table_type} data")

Expand All @@ -393,7 +416,7 @@ def create_openstudio_standards_code_version_data_json(
# Export retrieved data
if len(file_content[table_type]) > 0:
with open(
f"../../lib/openstudio-standards/standards/{code}/{code}_{code_version}/data/{code}_{code_version}.{table_type}.json",
f"{osstd_repository_path}/lib/openstudio-standards/standards/{code}/{code}_{code_version}/data/{code}_{code_version}.{table_type}.json",
"w+",
) as output_report:
output_report.write(json.dumps(file_content, indent=2))
Expand All @@ -404,13 +427,17 @@ def create_openstudio_standards_code_version_data_json(


def create_openstudio_standards_code_data_json(
conn: sqlite3.Connection, code: str, tables_to_export: dict
conn: sqlite3.Connection,
code: str,
tables_to_export: dict,
osstd_repository_path: str,
) -> None:
"""
Extract code version-specific OpenStudio Standards data from the database and export it to JSON files
:param conn (sqlite3.Connection): database connection
:param code (str): name of the building energy code, e.g. "ashrae_90_1"
:param tables_to_export (dict): mapping of name of OpenStudio Standards JSON file name to corresponding tables from the database that contains the data for the code and code version data
:param osstd_repository_path (str): path of the local openstudio-standards repository
"""
for table_type, table in tables_to_export.items():
# Store the retrieved content from the database
Expand All @@ -429,7 +456,7 @@ def create_openstudio_standards_code_data_json(
# Export retrieved data
if len(file_content[table_type]) > 0:
with open(
f"../../lib/openstudio-standards/standards/{code}/data/{code}.{table_type}.json",
f"{osstd_repository_path}/lib/openstudio-standards/standards/{code}/data/{code}.{table_type}.json",
"w+",
) as output_report:
output_report.write(json.dumps(file_content, indent=2))
Expand Down
31 changes: 30 additions & 1 deletion building_energy_standards_data/database_engine/assertions.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import logging
import logging, os


class OpenStudioStandardsDataException(Exception):
Expand All @@ -23,6 +23,35 @@ def __init__(self, object_name, first_key):
super().__init__(message)


class PathNotFound(OpenStudioStandardsDataException):
def __init__(self, path):
message = f"The path {path} cannot be found"
super().__init__(message)


def check_path(path):
"""Check that a path exists
Parameters
----------
path : str
String that represent a path to a directory
Returns
-------
True: if the path is valid
Raises:
------
PathNotFound: the path is not valid
"""
if path is None:
raise PathNotFound(path)
if not os.path.exists(path):
raise PathNotFound(path)
return True


def getattr_(obj, obj_name: str, first_key, *remaining_keys):
"""Gets the value inside a dictionary described by a key path or raises an expection
Expand Down
2 changes: 1 addition & 1 deletion docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ import sqlite3
conn = sqlite3.connect('openstudio_standards.db')
from building_energy_standards_data.applications.create_openstudio_standards_json import create_openstudio_standards_space_data_json_ashrae_90_1
for t in ["2004", "2007", "2010", "2013", "2016", "2019"]:
create_openstudio_standards_space_data_json_ashrae_90_1(conn=conn, version_90_1=t)
create_openstudio_standards_space_data_json_ashrae_90_1(conn=conn, version_90_1=t, osstd_repository_path="./")
conn.close()
```
## Future Enhancements
Expand Down

0 comments on commit 040b987

Please sign in to comment.