diff --git a/tests/models/test_models.py b/tests/models/test_models.py index 86a355e0..5f901c4d 100644 --- a/tests/models/test_models.py +++ b/tests/models/test_models.py @@ -1,5 +1,7 @@ from workflow.lib.inputs import Inputs from workflow.lib.models import Models +from workflow.lib.setup import Setup +from pathlib import Path import pytest import os import shutil @@ -7,16 +9,18 @@ def test_model_downloads_comstock(): - folder_name = os.path.join(os.getcwd(), 'comsstock') + folder_name = Path(os.path.join(os.getcwd(), 'comsstock')) + alfalfa_folder = Path(os.path.join(os.getcwd(), 'alfalfa_folder')) + id = ['bldg0000001','bldg0000002'] + objA = Inputs - objA.__init__(Inputs, '2023','comstock_amy2018_release_2','18', folder_name) + objA.__init__(Inputs, '2023','comstock_amy2018_release_2','18', id, folder_name) - id = ['bldg0000001','bldg0000002'] shutil.rmtree(folder_name, ignore_errors=True) objB = Models - objB.download_models(id, objA) + objB.download_models(objA) save_folder_1 = os.path.join(folder_name, id[0]) @@ -26,21 +30,35 @@ def test_model_downloads_comstock(): assert os.path.exists(save_folder_1) assert os.path.exists(save_folder_2) + shutil.rmtree(alfalfa_folder, ignore_errors=True) + Setup.create_folder(objA, alfalfa_folder) + + assert os.path.exists(alfalfa_folder) + assert os.path.exists(os.path.join(alfalfa_folder, id[0], 'models', f'{id[0]}.osm')) + def test_model_downloads_resstock(): - folder_name = os.path.join(os.getcwd(), 'resstock') + folder_name = Path(os.path.join(os.getcwd(), 'resstock')) + alfalfa_folder = Path(os.path.join(os.getcwd(), 'alfalfa_folder_res')) + id = ['bldg0000003','bldg0000004'] + objA = Inputs - objA.__init__(Inputs, '2021','resstock_tmy3_release_1','00', folder_name) + objA.__init__(Inputs, '2021','resstock_tmy3_release_1','00', id, folder_name) - id = ['bldg0000003','bldg0000004'] shutil.rmtree(folder_name, ignore_errors=True) objB = Models - objB.download_models(id, objA) + objB.download_models(objA) save_folder_1 = os.path.join(folder_name, id[0]) save_folder_2 = os.path.join(folder_name, id[1]) assert os.path.exists(folder_name) assert os.path.exists(save_folder_1) - assert os.path.exists(save_folder_2) \ No newline at end of file + assert os.path.exists(save_folder_2) + + shutil.rmtree(alfalfa_folder, ignore_errors=True) + Setup.create_folder(objA, alfalfa_folder) + + assert os.path.exists(alfalfa_folder) + assert os.path.exists(os.path.join(alfalfa_folder, id[0], 'models', f'{id[0]}.osm')) diff --git a/workflow/README.md b/workflow/README.md new file mode 100644 index 00000000..ffb33b76 --- /dev/null +++ b/workflow/README.md @@ -0,0 +1,2 @@ +# Buildstock Alfalfa workflow + diff --git a/workflow/lib/inputs.py b/workflow/lib/inputs.py index cda9ec38..3fd009c2 100644 --- a/workflow/lib/inputs.py +++ b/workflow/lib/inputs.py @@ -4,8 +4,10 @@ class Inputs: """Class to assign inputs for running buildstock models in Alfalfa.""" - def __init__(self, year, dataset_name, upgrade, folder): + def __init__(self, year, dataset_name, upgrade, id, folder): self.year = year self.dataset_name = dataset_name self.upgrade = upgrade + self.id = id + # Folder to download models self.folder = folder diff --git a/workflow/lib/models.py b/workflow/lib/models.py index 111123bd..bffed91c 100644 --- a/workflow/lib/models.py +++ b/workflow/lib/models.py @@ -12,13 +12,13 @@ class Models: """Class to download models from defined inputs""" - def download_models(id, obj): + def download_models(obj): folder_path = obj.folder + id = obj.id os.makedirs(folder_path, exist_ok=True) # Create folder if it doesn't exist for i in id: - # TO DO: check if this url is different for other years/resstock url1 = f'https://oedi-data-lake.s3.amazonaws.com/nrel-pds-building-stock/end-use-load-profiles-for-us-building-stock/{obj.year}/{obj.dataset_name}/building_energy_models/upgrade={obj.upgrade}/{i}-up{obj.upgrade}.osm.gz' url2 = f'https://oedi-data-lake.s3.amazonaws.com/nrel-pds-building-stock/end-use-load-profiles-for-us-building-stock/{obj.year}/{obj.dataset_name}/building_energy_models/{i}-up{obj.upgrade}.osm.gz' diff --git a/workflow/lib/setup.py b/workflow/lib/setup.py new file mode 100644 index 00000000..5bfba41a --- /dev/null +++ b/workflow/lib/setup.py @@ -0,0 +1,79 @@ +import pandas as pd +import requests +import os +import shutil +import json +from workflow.lib.inputs import Inputs +from pathlib import Path + +class Setup: + + """Class to create folder to upload to Alfalfa""" + + def create_osw(filename, weather): + + osw_content = {"seed_file": f"{filename}.osm", + "weather_file": f"{weather}", + "measure_paths": ["./measures"], + "run_directory": "./run/", + "file_paths": [ + "./weather/", + "./models/" + ], + "steps" : [ + { + "measure_dir_name" : "alfalfa_vars", + "name" : "Alfalfa Variables", + "description" : "Add custom variables for Alfalfa", + "modeler_description" : "Add EMS global variables required by Alfalfa", + "arguments" : { + "model" : f"{filename}.osm" + } + } + ] + } + + return osw_content + + + def create_folder(obj, alfalfa_folder): + + # folder where models are downloaded + folder_path = obj.folder + + print(folder_path) + + # create folder to upload to alfalfa + os.makedirs(alfalfa_folder, exist_ok=True) + + # copy downloaded models to alfalfa folder + for file in folder_path.iterdir(): + if file.is_dir() and (file/ f'{file.name}.osm').exists: + + # Make folder for model + model_filepath = Path(os.path.join(alfalfa_folder, file.name)) + print(model_filepath) + model_filepath.mkdir(parents=True, exist_ok=True) + (model_filepath / 'models').mkdir(parents=True, exist_ok=True) + (model_filepath / 'measures').mkdir(parents=True, exist_ok=True) + (model_filepath / 'weather').mkdir(parents=True, exist_ok=True) + + # Copy model + shutil.copy((file / f'{file.name}.osm'), (model_filepath / 'models' / f'{file.name}.osm')) + # TO DO Copy weather + + # Write OSW file + osw_path = Path(os.path.join(model_filepath, 'workflow.osw')) + + #TO DO pass weather file + with open(osw_path, 'w') as osw: + json.dump(Setup.create_osw(file.name, ''), osw, indent=2) + + + + + + + + +