3

how to add .epw file in openstudio sdk for weather data using python bindings

I want to add .epw file to my model to add weather data but i am not able to do so

This is the code snippet i am using

epw_path = os.path("file path")
epw = os.openstudioutilities.openstudioutilitiesfiletypes.EpwFile(epw_path)
weather = os.model.WeatherFile.setWeatherFile(model, epw)

Thanks

Rocker's avatar
41
Rocker
asked 2023-02-24 06:42:50 -0500
Aaron Boranian's avatar
14.1k
Aaron Boranian
updated 2023-03-07 10:12:44 -0500
edit flag offensive 0 remove flag close merge delete

Comments

add a comment see more comments

2 Answers

3

This works for me:

import openstudio
model = openstudio.model.Model()
epw_path = openstudio.path('//path//to//weather//USA_CO_Boulder-Broomfield-Jefferson.County.AP.724699_TMY3.epw')
epw_file = openstudio.openstudioutilitiesfiletypes.EpwFile(epw_path)
openstudio.model.WeatherFile.setWeatherFile(model, epw_file)
print(model)

It produces an OSM with:

OS:Version,
  {93be9bc6-f569-4f5c-9804-7f01878e7175}, !- Handle
  3.2.1;                                  !- Version Identifier

OS:WeatherFile,
  {b21bb643-6d07-4e17-9e14-31c2171765da}, !- Handle
  Broomfield Jeffco  Boulder   S,         !- City
  CO,                                     !- State Province Region
  USA,                                    !- Country
  TMY3,                                   !- Data Source
  724699,                                 !- WMO Number
  40.13,                                  !- Latitude {deg}
  -105.24,                                !- Longitude {deg}
  -7,                                     !- Time Zone {hr}
  1689,                                   !- Elevation {m}
  //path//to//weather//USA_CO_Boulder-Broomfield-Jefferson.County.AP.724699_TMY3.epw, !- Url
  AC900369;                               !- Checksum
shorowit's avatar
11.8k
shorowit
answered 2023-02-27 12:40:29 -0500
edit flag offensive 0 remove flag delete link

Comments

Yes this works, But when save the file as .osm and open it again in openstudio application it says "invalid weather file object"

Rocker's avatar Rocker (2023-02-27 23:07:17 -0500) edit

@Rocker Use an absolute path instead of a relative path and the OS Application should be able to find it.

shorowit's avatar shorowit (2023-02-27 23:23:56 -0500) edit

I checked for it, I am using an absolute path Also i am able to get the latitude, city etc from code but when i open it in application the issue arises

Rocker's avatar Rocker (2023-02-28 22:34:26 -0500) edit
add a comment see more comments
0

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:

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)
Julien Marrec's avatar
29.7k
Julien Marrec
answered 2023-03-06 03:46:21 -0500
edit flag offensive 0 remove flag delete link

Comments

add a comment see more comments