Question-and-Answer Resource for the Building Energy Modeling Community
Get started with the Help page
Ask Your Question

Revision history [back]

The code @shorowit gave you is fine. What you are really asking how do load an EPW in the python bindings so the OpenStudioApplication can find it.

Do the same as what the OpenStudioApplication does:

  • When loading the EPW: https://github.com/openstudiocoalition/OpenStudioApplication/blob/d93d710b2b7be34dd61ff374f8b5deb770384729/src/openstudio_lib/LocationTabView.cpp#L545-L588
  • When saving the model and workflow OSW: https://github.com/NREL/OpenStudio/blob/5a1550e3b02167e998386f3a7f3dc8297dc3ee3c/src/model/FileOperations.cpp#L367-L398

Here's a full implementation. Note that I choose to use Python's native pathlib.Path whenever possible.

from pathlib import Path
import shutil

import openstudio

# These are the two paths you need to change locally
model_path = Path('./model.osm').resolve()
epw_path = Path('/usr/local/EnergyPlus-22-2-0/WeatherData/USA_IL_Chicago-OHare.Intl.AP.725300_TMY3.epw')

# The companion directory is next to the OSM and is named like it (minus the extension)
companion_directory = model_path.parent / model_path.stem
companion_directory.mkdir(exist_ok=True)
osw_path = companion_directory / "workflow.osw"


model = openstudio.model.Model()
model.workflowJSON().setOswPath(str(osw_path))   # When we save it'll go there
model.workflowJSON().setSeedFile(str(Path("..") / model_path.name))  # Relative path

# Copy the EPW to the companion directory, subfolder `files`
new_epw_path = companion_directory / "files" / epw_path.name
new_epw_path.parent.mkdir(parents=True, exist_ok=True)
shutil.copy(epw_path, new_epw_path)

epwFile = openstudio.openstudioutilitiesfiletypes.EpwFile(str(new_epw_path))
weatherFile_ = openstudio.model.WeatherFile.setWeatherFile(model, epwFile)
if not weatherFile_.is_initialized():
    raise ValueError("Failed to set weather file")
weatherFile = weatherFile_.get()
weatherFile.makeUrlRelative(str(companion_directory / "files"))
model.workflowJSON().setWeatherFile(new_epw_path.name)

# UPDATE OTHER THINGS WHILE WE'RE AT IT
# Set calendar year or start day of week
yearDescription = model.getYearDescription()
startDateActualYear_ = epwFile.startDateActualYear()
if startDateActualYear_.is_initialized():
    yearDescription.resetDayofWeekforStartDay()
    yearDescription.setCalendarYear(startDateActualYear_.get())
else:
    yearDescription.resetCalendarYear()
    yearDescription.setDayofWeekforStartDay(epwFile.startDayOfWeek().valueName())

# set run period based on weather file
runPeriod = model.getRunPeriod()
runPeriod.setBeginMonth(epwFile.startDate().monthOfYear().value())
runPeriod.setBeginDayOfMonth(epwFile.startDate().dayOfMonth())
runPeriod.setEndMonth(epwFile.endDate().monthOfYear().value())
runPeriod.setEndDayOfMonth(epwFile.endDate().dayOfMonth())

# update site info
site = model.getSite()
site.setName(weatherFile.city())
site.setLatitude(weatherFile.latitude())
site.setLongitude(weatherFile.longitude())
site.setElevation(weatherFile.elevation())
site.setTimeZone(weatherFile.timeZone())

# Save the workflowJSON and the model
model.workflowJSON().save()
model.save(str(model_path), True)