generated from NOAA-OWP/owp-open-source-project-template
-
Notifications
You must be signed in to change notification settings - Fork 16
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
feat: ngen_cal_model_observations
model plugin hook and default implementation
#155
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
ca18264
feat: ngen_cal_model_observation hook
aaraney 57a8f5f
feat: ngen_cal_model_observations plugin, NwisObservations
aaraney ab6a83e
feat: CalibrationSet gets observations from ngen_cal_model_observations
aaraney 3deac49
refactor: register default ngen plugins in one place
aaraney 50700f0
feat: register UsgsObservations plugin
aaraney 230e213
test: ngen_cal_model_observations registration
aaraney File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
from __future__ import annotations | ||
|
||
import typing | ||
|
||
import pandas as pd | ||
from hypy.hydrolocation.nwis_location import NWISLocation | ||
|
||
from ngen.cal import hookimpl | ||
|
||
if typing.TYPE_CHECKING: | ||
from datetime import datetime | ||
|
||
from hypy.nexus import Nexus | ||
|
||
|
||
class UsgsObservations: | ||
CFS_TO_CSM = 0.028316847 | ||
"""ft**3/s to m**3/s""" | ||
|
||
@hookimpl(trylast=True) | ||
def ngen_cal_model_observations( | ||
self, | ||
nexus: Nexus, | ||
start_time: datetime, | ||
end_time: datetime, | ||
simulation_interval: pd.Timedelta, | ||
) -> pd.Series: | ||
# use the nwis location to get observation data | ||
location = nexus._hydro_location | ||
assert isinstance(location, NWISLocation), f"expected hypy.hydrolocation.NWISLocation instance, got {type(location)}. cannot retrieve observations" | ||
|
||
try: | ||
df = location.get_data(start=start_time, end=end_time) | ||
except BaseException as e: | ||
raise RuntimeError(f"failed to retrieve observations for usgs gage: {location.station_id}") from e | ||
|
||
df.set_index("value_time", inplace=True) | ||
ds = df["value"].resample(simulation_interval).nearest() | ||
ds.rename("obs_flow", inplace=True) | ||
|
||
# convert from CFS to CMS observations | ||
ds = ds * UsgsObservations.CFS_TO_CSM | ||
return ds |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
In reality I think the caller should pass
start_time + 1 dt
(e.g.3600s
) since the model outputs will not contain values for the actualstart_time
(left exclusive).I don't think this is a problem now, but just wanted to note 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.
It does matter and things will break if we don't properly account for this. This only works right now b.c.
csv_output
'sdt
is300s
which means the first value isstart_time
+5min
. When we resample the simulation output to the hour using.resample("h").first()
the simulated value at5min
is backfilled tostart_time
and lines up with the nwis observations.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.
This likely heavily depends on how the data is "merged", in search.py
_objective_function
. Currently this done with a pandasmerge
withleft_index=True, right_index=True
which should result in a data frame of the overlapping indicies.