Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Addresses #5214, EpwFile getTimeSeries fails for leap year weather file w/no leap day #5217

Merged
merged 21 commits into from
Oct 24, 2024

Conversation

joseph-robertson
Copy link
Collaborator

@joseph-robertson joseph-robertson commented Jun 4, 2024

Pull request overview

As far as I can tell, the Bad Date: year = 2009, month = Feb(2), day = 29. error is thrown for each of the following scenarios:

  • AMY leap year missing 2/29 -- This will just be considered a TMY (file should contain Feb 29 for leap years...)
  • TMY leap Feb w/out 2/29 -- This is what the issue wants to support
  • TMY leap Feb with 2/29 -- getTimeSeries will still fail (the assumed 2009 year will be used, and so Feb 29 would never be supported...)

Possible solutions:

  • optional isActualOverride bool supplied to EpwFile::getTimeSeries (demonstrated in this PR)
  • increase the 1 day delta tolerance here so, e.g., Feb28 - Mar1 doesn't trigger
  • specifically except the Feb28 - Mar1 1 day delta here for leap years
  • intercept loading of m_data to change xxxx-Feb-29 00:00:00 to xxxx-Mar-01 00:00:00 for TMY with leap Feb

Pull Request Author

  • Model API Changes / Additions
  • Any new or modified fields have been implemented in the EnergyPlus ForwardTranslator (and ReverseTranslator as appropriate)
  • Model API methods are tested (in src/model/test)
  • EnergyPlus ForwardTranslator Tests (in src/energyplus/Test)
  • If a new object or method, added a test in NREL/OpenStudio-resources: Add Link
  • If needed, added VersionTranslation rules for the objects (src/osversion/VersionTranslator.cpp)
  • Verified that C# bindings built fine on Windows, partial classes used as needed, etc.
  • All new and existing tests passes
  • If methods have been deprecated, update rest of code to use the new methods

Labels:

  • If change to an IDD file, add the label IDDChange
  • If breaking existing API, add the label APIChange
  • If deemed ready, add label Pull Request - Ready for CI so that CI builds your PR

Review Checklist

This will not be exhaustively relevant to every PR.

  • Perform a Code Review on GitHub
  • Code Style, strip trailing whitespace, etc.
  • All related changes have been implemented: model changes, model tests, FT changes, FT tests, VersionTranslation, OS App
  • Labeling is ok
  • If defect, verify by running develop branch and reproducing defect, then running PR and reproducing fix
  • If feature, test running new feature, try creative ways to break it
  • CI status: all green or justified

@joseph-robertson joseph-robertson added component - Utilities Other Pull Request - Ready for CI This pull request if finalized and is ready for continuous integration verification prior to merge. labels Jun 4, 2024
@joseph-robertson joseph-robertson self-assigned this Jun 4, 2024
@joseph-robertson
Copy link
Collaborator Author

After a bit of digging, it appears that realYear (and therefore epw.isActual) gets set to false here since we have a delta greater than 1 day in the epw file. I'm assuming this works as intended for TMY where months jump years.

Therefore we fall into this block instead of this one.

If epw.isActual were set to true, is the issue resolved here? If so, perhaps we could provide some type of override on isActual (either in the epw ctor, or as a setter somewhere)?

@joseph-robertson
Copy link
Collaborator Author

joseph-robertson commented Jun 4, 2024

Ah ha, here's the core issue. For all 3 of the above scenarios, m_data has dateTimes that look like:

2012-Feb-28 20:00:00
2012-Feb-28 21:00:00
2012-Feb-28 22:00:00
2012-Feb-28 23:00:00
2012-Feb-29 00:00:00 => Bad Date: year = 2009, month = Feb(2), day = 29.
2012-Mar-01 01:00:00
2012-Mar-01 02:00:00
2012-Mar-01 03:00:00
2012-Mar-01 04:00:00

This is the line that fails. The assumedYear in the Data ctor here is 2009, and so fails on the line above.

Why doesn't the EpwDataPoint have a dateTime of 2012-Mar-01 00:00:00 instead of 2012-Feb-29 00:00:00?

Answer:

pt = OpenStudio::EpwDataPoint.fromEpwStrings(2012, 2, 28, 24, 0, ["foo"] * 35)
pt.dateTime => 2012-Feb-29 00:00:00

@@ -3967,6 +3974,31 @@ bool EpwFile::parse(std::istream& ifs, bool storeData) {
}
}

for (unsigned int i = 0; i < epw_strings.size(); i++) {
Copy link
Collaborator Author

@joseph-robertson joseph-robertson Jun 7, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do this after the previous loop, instead of inside it, because we first need to get realYear.

@ckirney
Copy link

ckirney commented Jun 10, 2024

The fix looks good. I tried it on Windows with a couple of different weather files that had the issue before and everything seems to work. Thanks for resolving this.

@joseph-robertson joseph-robertson requested review from jmarrec and removed request for kbenne October 3, 2024 16:37
Copy link
Contributor

@kbenne kbenne left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @joseph-robertson. I left a few pedantic comments about coding nitpicks. You can take them or leave them.

I like the additional unit test coverage, otherwise I'd have a hard time knowing for sure that the approach works.

@@ -3887,6 +3887,17 @@ bool EpwFile::parse(std::istream& ifs, bool storeData) {
return false;
}

struct epw_string
{
int lineNumber;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps these can be const based on your use case.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed int to const int.

@@ -3887,6 +3887,17 @@ bool EpwFile::parse(std::istream& ifs, bool storeData) {
return false;
}

struct epw_string
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this is a type, wouldn't it be more aligned with our conventions to call it EPWString, or otherwise start with an uppercase letter?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed epw_string to EPWString.

return false;
}
epw_string epw_s = {lineNumber, year, month, day, hour, currentMinute, strings};
epw_strings.push_back(epw_s);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you could save a copy by doing the construction in place. push_back will perform move and save a copy. Here it doesn't matter that much because the copy is presumably super cheap. Something like this...

epw_strings.push_back({lineNumber, year, month, day, hour, currentMinute, strings});

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

epw_strings.emplace_back(lineNumber, year, month, day, hour, currentMinute, std::move(strings));

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would probably do epw_strings.reserve(8760) before looping?

Copy link
Collaborator

@jmarrec jmarrec Oct 29, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A benchmark: https://github.com/jmarrec/CppBenchmarks/blob/main/bench_EPWStrings.cpp

For the 8760 records case (most common), time in nanoseconds:

image

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The full range:

image

@@ -3967,6 +3974,31 @@ bool EpwFile::parse(std::istream& ifs, bool storeData) {
}
}

for (unsigned int i = 0; i < epw_strings.size(); i++) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wouldn't

for (const auto &epw_string: epw_strings) {
...
...
}

be a bit more modern? I don't believe you need the index, unless I missed it.

int month = epw_strings[i].month;
int day = epw_strings[i].day;
int hour = epw_strings[i].hour;
int currentMinute = epw_strings[i].currentMinute;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Making these copies seems superfluous. Perhaps you could just use epw_string.currentMinute when you need to access these members. At minimum they could be const.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moving all epw_string.month, epw_string.day, etc to where they are used below.


boost::optional<EpwDataPoint> pt = EpwDataPoint::fromEpwStrings(year, month, day, hour, currentMinute, epw_strings[i].strings);
if (pt) {
m_data.push_back(pt.get());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here the temporary pt makes sense to me since you have the push_back protected by a conditional.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No action.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

m_data.emplace_back(std::move(*pt))

Comment on lines 3986 to 3991
if ((!realYear) && (month == 2) && (day == 28) && (hour == 24) && (currentMinute == 0)) {
Date date(month, day, year);
if (date.isLeapYear()) {
day = 29;
}
}
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the main fix, right in here.

@@ -3967,6 +3974,31 @@ bool EpwFile::parse(std::istream& ifs, bool storeData) {
}
}

for (unsigned int i = 0; i < epw_strings.size(); i++) {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that this PR might increase slightly the EpwFile::parse time. This is because we now essentially have 2 loops that run through epw lines. I'm open to suggestions here for a better approach...

@@ -3887,6 +3887,17 @@ bool EpwFile::parse(std::istream& ifs, bool storeData) {
return false;
}

struct epw_string
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed epw_string to EPWString.

@@ -3887,6 +3887,17 @@ bool EpwFile::parse(std::istream& ifs, bool storeData) {
return false;
}

struct epw_string
{
int lineNumber;
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed int to const int.


boost::optional<EpwDataPoint> pt = EpwDataPoint::fromEpwStrings(year, month, day, hour, currentMinute, epw_strings[i].strings);
if (pt) {
m_data.push_back(pt.get());
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No action.

int month = epw_strings[i].month;
int day = epw_strings[i].day;
int hour = epw_strings[i].hour;
int currentMinute = epw_strings[i].currentMinute;
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moving all epw_string.month, epw_string.day, etc to where they are used below.

@ci-commercialbuildings
Copy link
Collaborator

ci-commercialbuildings commented Oct 23, 2024

CI Results for 15e17d3:

@joseph-robertson joseph-robertson merged commit 91b49f0 into develop Oct 24, 2024
1 of 5 checks passed
@joseph-robertson joseph-robertson deleted the epw-leapyear-no-leapday branch October 24, 2024 04:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
component - Utilities Other Pull Request - Ready for CI This pull request if finalized and is ready for continuous integration verification prior to merge.
Projects
None yet
5 participants