Skip to content

Commit

Permalink
update files and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tanushree04 committed Feb 6, 2024
1 parent 525a428 commit 958f5d2
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 51 deletions.
38 changes: 27 additions & 11 deletions tests/models/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,19 @@

def test_model_downloads_comstock():

folder_name = Path(os.path.join(os.getcwd(), 'comsstock'))
alfalfa_folder = Path(os.path.join(os.getcwd(), 'alfalfa_folder'))
current_directory = Path(__file__).resolve().parent

# folder to download models
folder_name = Path(os.path.join(current_directory, 'comsstock'))
# folder to configure and upload to alfalfa
alfalfa_folder = Path(os.path.join(current_directory, 'alfalfa_folder'))

id = ['bldg0000001','bldg0000002']

objA = Inputs
objA.__init__(Inputs, '2023','comstock_amy2018_release_2','18', id, folder_name)


# remove if folder exists
shutil.rmtree(folder_name, ignore_errors=True)

objB = Models
Expand All @@ -26,20 +31,27 @@ def test_model_downloads_comstock():
save_folder_1 = os.path.join(folder_name, id[0])
save_folder_2 = os.path.join(folder_name, id[1])

# check if models are downloaded
assert os.path.exists(folder_name)
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'))
# Test Model Setup
objC = Setup
objC.create_folder(objA, alfalfa_folder)

assert os.path.exists(os.path.join(alfalfa_folder, 'files'))
assert os.path.exists(os.path.join(alfalfa_folder, 'alfalfa_upload', id[0], 'models', f'{id[0]}.osm'))


def test_model_downloads_resstock():

folder_name = Path(os.path.join(os.getcwd(), 'resstock'))
alfalfa_folder = Path(os.path.join(os.getcwd(), 'alfalfa_folder_res'))
current_directory = Path(__file__).resolve().parent

folder_name = Path(os.path.join(current_directory, 'resstock'))
alfalfa_folder = Path(os.path.join(current_directory, 'alfalfa_folder_res'))
id = ['bldg0000003','bldg0000004']

objA = Inputs
Expand All @@ -58,7 +70,11 @@ def test_model_downloads_resstock():
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'))
# Test Model Setup
objC = Setup
objC.create_folder(objA, alfalfa_folder)

assert os.path.exists(os.path.join(alfalfa_folder, 'files'))
assert os.path.exists(os.path.join(alfalfa_folder, 'alfalfa_upload',id[0], 'models', f'{id[0]}.osm'))

102 changes: 62 additions & 40 deletions workflow/lib/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,70 +10,92 @@ 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_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)
alfalfa_upload = Path(os.path.join(alfalfa_folder, 'alfalfa_upload'))
alfalfa_upload.mkdir(parents=True, exist_ok=True)

# create a files folder. This is done so users can add more measures and modify osw file
files = Path(os.path.join(alfalfa_folder, 'files'))
# Remove the destination folder if it exists
if os.path.exists(files):
shutil.rmtree(files)
#files.mkdir(parents=True, exist_ok=True)

current_directory = Path(__file__).resolve().parent



base_files = os.path.join(current_directory.parent,'base_files')
# copy base files to folder
shutil.copytree(base_files, files)

# copy downloaded models to alfalfa folder
for file in folder_path.iterdir():
for file in Path(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 = Path(os.path.join(alfalfa_upload, file.name))
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
# Copy default measures
#(model_filepath / 'measures').mkdir(parents=True, exist_ok=True)
shutil.copytree(os.path.join(files, 'measures'), os.path.join(model_filepath/ 'measures'))

# Write OSW file
# copy osw file
osw_path = Path(os.path.join(model_filepath, 'workflow.osw'))
shutil.copy(os.path.join(files, 'workflow.osw'), osw_path)

#TO DO pass weather file
with open(osw_path, 'w') as osw:
json.dump(Setup.create_osw(file.name, ''), osw, indent=2)

(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
with open(osw_path, 'r') as osw:
data = json.load(osw)
data['seed_file'] = f"{file.name}.osm"
# TO DO add weather
# osw['weather_file'] = ""

# TO DO add better way to do this
data['steps'][0]['arguments']['model'] = f"{file.name}.osm"

with open(osw_path, 'w') as osw:
json.dump(data, osw, indent=2)



0 comments on commit 958f5d2

Please sign in to comment.