Skip to content

Commit

Permalink
add setup file to create alfalfa folder
Browse files Browse the repository at this point in the history
  • Loading branch information
tanushree04 committed Jan 22, 2024
1 parent 3f681df commit 92daa9b
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 12 deletions.
36 changes: 27 additions & 9 deletions tests/models/test_models.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,26 @@
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


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])
Expand All @@ -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)
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'))
2 changes: 2 additions & 0 deletions workflow/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Buildstock Alfalfa workflow

4 changes: 3 additions & 1 deletion workflow/lib/inputs.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 2 additions & 2 deletions workflow/lib/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down
79 changes: 79 additions & 0 deletions workflow/lib/setup.py
Original file line number Diff line number Diff line change
@@ -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)









0 comments on commit 92daa9b

Please sign in to comment.