Skip to content

Commit

Permalink
Merge pull request #5011 from NREL/5009-osw-weather
Browse files Browse the repository at this point in the history
Improve weather file handling in OS Workflow
  • Loading branch information
DavidGoldwasser authored Nov 2, 2023
2 parents d8026ec + 100167e commit 50ad104
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 46 deletions.
14 changes: 3 additions & 11 deletions src/workflow/ApplyMeasure.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,8 @@ void OSWorkflow::applyMeasures(MeasureType measureType, bool energyplus_output_r
if (!sqlPath.empty()) {
runner.setLastEnergyPlusSqlFilePath(sqlPath);
}
if (!epwPath.empty()) {
runner.setLastEpwFilePath(epwPath);
}

updateLastWeatherFileFromModel();

const auto measureDirName = step.measureDirName();
if (openstudio::filesystem::path(measureDirName).is_absolute()) {
Expand Down Expand Up @@ -213,14 +212,7 @@ void OSWorkflow::applyMeasures(MeasureType measureType, bool energyplus_output_r
}

if (measureType == MeasureType::ModelMeasure) {
if (auto weatherFile_ = model.weatherFile()) {
if (auto p_ = weatherFile_->path()) {
// Probably a workflowJSON.findFile() call...
// m_epwPath_ = p_;
} else {
LOG(Warn, "Weather file object found in model but no path is given");
}
}
updateLastWeatherFileFromModel();
}

if (m_add_timings && m_detailed_timings) {
Expand Down
50 changes: 50 additions & 0 deletions src/workflow/OSWorkflow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,56 @@ OSWorkflow::OSWorkflow(const WorkflowRunOptions& t_workflowRunOptions, ScriptEng
}
}

void OSWorkflow::initializeWeatherFileFromOSW() {
LOG(Debug, "Initialize the weather file from osw");
auto epwPath_ = workflowJSON.weatherFile();

if (epwPath_) {
LOG(Debug, "Search for weather file defind by osw " << epwPath_.get());
auto epwFullPath_ = workflowJSON.findFile(epwPath_.get());
if (!epwFullPath_) {
auto epwFullPath_ = workflowJSON.findFile(epwPath_->filename());
}
if (!epwFullPath_) {
throw std::runtime_error(fmt::format("Weather file {} specified but cannot be found", epwPath_->string()));
}

epwPath = epwFullPath_.get();

if (auto epwFile_ = openstudio::EpwFile::load(epwPath)) {
model::WeatherFile::setWeatherFile(model, epwFile_.get());
runner.setLastEpwFilePath(epwPath);
} else {
LOG(Warn, "Could not load weather file from " << epwPath_.get());
}
} else {
LOG(Debug, "Weather file is not defined by the osw");
}
}

void OSWorkflow::updateLastWeatherFileFromModel() {
LOG(Debug, "Find model's weather file and update LastEpwFilePath");
if (auto epwFile_ = model.weatherFile()) {
if (auto epwPath_ = epwFile_->path()) {
LOG(Debug, "Search for weather file " << epwPath_.get());
auto epwFullPath_ = workflowJSON.findFile(epwPath_.get());
if (!epwFullPath_) {
auto epwFullPath_ = workflowJSON.findFile(epwPath_->filename());
}
if (!epwFullPath_) {
throw std::runtime_error(fmt::format("Weather file {} specified but cannot be found", epwPath_->string()));
}

epwPath = epwFullPath_.get();
runner.setLastEpwFilePath(epwPath);

return;
}
}

LOG(Debug, "Weather file is not defined by the model");
}

void OSWorkflow::applyArguments(measure::OSArgumentMap& argumentMap, const std::string& argumentName, const openstudio::Variant& argumentValue) {
LOG(Info, "Setting argument value '" << argumentName << "' to '" << argumentValue << "'");

Expand Down
2 changes: 2 additions & 0 deletions src/workflow/OSWorkflow.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,8 @@ class OSWorkflow

//@}

void initializeWeatherFileFromOSW();
void updateLastWeatherFileFromModel();
void applyMeasures(MeasureType measureType, bool energyplus_output_requests = false);
static void applyArguments(measure::OSArgumentMap& argumentMap, const std::string& argumentName, const openstudio::Variant& argumentValue);
void saveOSMToRootDirIfDebug();
Expand Down
36 changes: 1 addition & 35 deletions src/workflow/RunInitialization.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,41 +83,7 @@ void OSWorkflow::runInitialization() {
openstudio::model::initializeModelObjects(model);
}

LOG(Debug, "Getting the initial weather file");
// Initialize the weather file
auto epwPath_ = workflowJSON.weatherFile();
bool alreadySetInModel = false;
if (!epwPath_) {
LOG(Debug, "No weather file specified in OSW, looking in model");
if (auto epwFile_ = model.weatherFile()) {
epwPath_ = epwFile_->path();
alreadySetInModel = true;
}
}

if (epwPath_) {
LOG(Debug, "Search for weather file " << epwPath_.get());
auto epwFullPath_ = workflowJSON.findFile(epwPath_.get());
if (!epwFullPath_) {
auto epwFullPath_ = workflowJSON.findFile(epwPath_->filename());
}
if (!epwFullPath_) {
throw std::runtime_error(fmt::format("Weather file {} specified but cannot be found", epwPath_->string()));
}

epwPath = epwFullPath_.get();

if (!alreadySetInModel) {
if (auto epwFile_ = openstudio::EpwFile::load(epwPath)) {
model::WeatherFile::setWeatherFile(model, epwFile_.get());
} else {
LOG(Warn, "Could not load weather file from " << epwPath_.get());
}
}

} else {
LOG(Debug, "No valid weather file defined in either the osm or osw");
}
initializeWeatherFileFromOSW();

// Set a clone of the WorkflowJSON for the model, so that it finds the filePaths (such as generated_files we added above)
model.setWorkflowJSON(workflowJSON.clone());
Expand Down

0 comments on commit 50ad104

Please sign in to comment.