-
Notifications
You must be signed in to change notification settings - Fork 906
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
refactor: upgrade CA-NS with event classes #6050
refactor: upgrade CA-NS with event classes #6050
Conversation
Upgrade the parser to use the ExchangeList, ProductionBreakdownList, ProductionMix, and ZoneKey classes. This should yield no functional change. Additionally: - Comply with Black's 88-column default line length limit. - Define global constants to replace a number of local variables and literals. - Try to use more consistent names throughout. Refs: electricitymaps#6011, electricitymaps#6050
The JSON arrays a and b returned by the two endpoints appear to be parallel in the sense that element a[i] corresponds with element b[i] for all i of interest. Zip these arrays together and iterate over the resulting pairs instead of iterating over one and performing a linear search to find the corresponding element in the other. Additionally: - Check whether we've been bitten by a race condition while requesting the above array data and bail out if so. - Skip the first element of each array because the reported base load is always 0 MW. Refs: electricitymaps#6011, electricitymaps#6050
a0d61d6
to
0de7079
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A few suggestions, but otherwise this looks good!
- When a pair of requests have failed, retry them until they succeed or the maximum number of attempts is exceeded. - Simplify the validation logic by making better use of the event classes instead of introducing extraneous data structures. Refs: electricitymaps#6011, electricitymaps#6050
Reliably merge the source's base load array with the corresponding electricity mix array by correlating events based on their timestamps. Refs: electricitymaps#6011, electricitymaps#6050
parsers/CA_NS.py
Outdated
# In this source, imports are positive. In the expected result for | ||
# CA-NB->CA-NS, "net" represents a flow from NB to NS, i.e., an import | ||
# to NS, so the value can be used directly. Note that this API only | ||
# specifies imports; when NS is exporting energy, the API returns 0. | ||
exchanges.append( | ||
datetime=timestamp, | ||
netFlow=load * mix["Imports"] / 100, | ||
source=SOURCE, | ||
zoneKey=ZoneKey("CA-NB->CA-NS"), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not strictly related to this refactor, but just to mention. The assumption that all imports are from NB is now problematic because Nova Scotia's interconnection to Newfoundland is now live. But I've never seen live data on its operation, the closest thing was that it amounted to 14% of NS supply in 2023H1, per https://www.nspower.ca/cleanandgreen/clean-energy "Our Energy Stats" chart
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For reference related to your comment: the daily generation reports for Newfoundland might be usable as a source for the interconnector data, see #2541 (comment)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey, look: I found a page on the NSPower website that has the information you need (Maritime Link interconnector) and more, updated roughly every 5 minutes, with more granular data than the current source: it's called Daily Report, but under the heading "Current System Conditions" you'll find "10 minute averages" of e.g. load, wind, and imports and exports through various interconnectors that are at most five minutes old.
Edit: well, turns out you found it at some point as well: #3206 (comment)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So I dug deeper into this, and it turns out we aren't actually using this exchange data since we get the data on the NS-NB interconnection from New Brunswick Power, not from NSPower. So it might be best to just delete the exchanges-related part, or, alternatively, switch to obtaining exchange info from the source in #6317 (and perhaps implement the NS-Newfoundland interconnector as well). But that could be done in a separate issue.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey @VIKTORVAV99, any thoughts on this? Should we simply delete the exchange parser if it's not used?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To clarify, my idea was to delete the exchange parser now so we are more certain we don't accidentally fetch incorrect data, and then re-add an exchange parser based on the new source from #6317 in a separate pull request.
But doing both in this PR is also acceptable, if you've got the time for it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, gotcha! It's not entirely clear to me what needs to be done to implement the new exchange parser (there are a lot of links to follow and I'm a bit lost), so I'll just delete it here and we can address it separately. Would those changes have any impact on the production parser?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The exchange stuff shouldn't. The "Current System Conditions" also has wind generation, but as the page has data updated roughly every 5 minutes I think it'll be hard to integrate with the hourly data provided by the current source – and probably not that useful either, if the current source stays online.
As for implementing the exchange parser, the short summary is: from https://www.nspower.ca/oasis/system-reports-messages/daily-report, under "Current System Conditions", grab:
- The timestamp, e.g. "Last Updated: 10-Jan-24 20:38:10";
- "NS Export" for (most likely) the New Brunswick interconnector (optional, already provided by the New Brunswick parser);
- "Maritime Link Import" for the Newfoundland interconnector.
The rest of the stuff in the issue is useful for exchange configuration and historical data.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Awesome, thanks for clarifying! That doesn't sound as arduous as I thought; maybe I can roll it into this PR. I'll try to carve out some time to work on it this weekend.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Alright, that wasn't so bad! A few notes:
- I created the
CA-NL-NF->CA-NS
exchange, but this is my first time doing so, so I very well could have done something wrong. In particular, I haven't tested how it will look because I don't have the front-end set up on my machine (I used Google Maps for the coordinates and a protractor for the rotation 😁). Further, I wasn't sure whether to useCA-NL-LB
orCA-NL-NF
for the other zone (I chose the latter based on the physical location of the link). - I also implemented the
CA-NB->CA-NS
exchange, but the back-end is still configured to use the NB parser for this pair.
- Stop discarding events whose production mix percentages exceed percentage thresholds, as these imposed limits may not be sound. - Provide defaults when getting attributes from the production mix to avoid comparing numbers with Nones. Refs: electricitymaps#6011, electricitymaps#6050
- New Brunswick and Newfoundland and Labrador exchange data is available at https://www.nspower.ca/oasis/system-reports-messages/daily-report; upgrade the exchange parser to leverage this new source. - Since it no longer reduces duplication, eliminate the _get_info function and roll its logic into fetch_production. Refs: electricitymaps#2541, electricitymaps#3206, electricitymaps#6011, electricitymaps#6050, electricitymaps#6317
Can this five-month-old PR be reviewed and merged? Current JSON endpoints aren't sending current data (#5587), if this persists, we would probably want to switch to the OASIS report being implemented in this PR, so having it merged would be a useful base. |
Apply the following corrections and merge conflic resolutions: - Fix an incorrect zone in a call to fetch_exchange. - Treat CT generation as oil instead of gas. - Update the comment regarding hydro capacities. - Update the production mix thresholds for hydro and oil.
Gracefully handle errors that might occur while navigating the HTML document containing exchange data. Refs: electricitymaps#6011
- Re-throw caught errors to provide better diagnostic information. - Format log messages lazily. Refs: electricitymaps#6011
Hi, we will merge a quick fix today to make sure that the data stops being overwritten with 0s. This PR looks great and should be merged after as the quality of the refactoring is really good. :) |
Add a check for 0 MW base loads to resolve the merge conflict. Refs: electricitymaps#6011, electricitymaps#6440
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good to me!
Thanks for bearing with me on this PR and sorry for some reviews taking longer than they should.
I'm approving this now but will let @mathilde-daugy have the final say if we should remove the in parser validation for this parser.
PS: I left some suggestions that have some tiny performance improvements as it avoid a reverse walk in the string for the value replacement.
All good to merge? |
Issue
Refs: #6011, #6050
Description
Upgrade the parser to use the
ExchangeList
,ProductionBreakdownList
,ProductionMix
, andZoneKey
classes. This should yield no functional change. Additionally:Double check
poetry run test_parser "zone_key"
pnpx prettier --write .
andpoetry run format
to format my changes.