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

60 observed flow and stage data #61

Merged
merged 25 commits into from
Nov 26, 2024
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
42a3e20
added functions for retrieving input observed data from the hdf
zherbz Oct 23, 2024
e4d9469
updated doc strings for new function
zherbz Oct 23, 2024
43b7523
added parameters within doc string
zherbz Oct 23, 2024
9b1f0ae
updated activation instructions for windows users
zherbz Nov 5, 2024
b581e9c
revised function observed_timeseries_input
zherbz Nov 5, 2024
c49baac
added tests for observed_timeseries_input
zherbz Nov 5, 2024
4be57a1
new test hdf and csv data for testing the observed_timeseries_input f…
zherbz Nov 5, 2024
9f2537a
updated for devcontainer users
zherbz Nov 7, 2024
6a7c443
added fiona==1.9.6 as a dependency in order for all plan tests to suc…
zherbz Nov 7, 2024
fb23af9
added a devcontainer folder for those using Windows OS systems needin…
zherbz Nov 7, 2024
a3dda6f
replaced black-formatted and pylint with charliermash.ruff
zherbz Nov 20, 2024
b19acc5
updated for documentation on venv activation across platforms
zherbz Nov 20, 2024
164911a
moved the pinned version of Fiona into the list of optional dependencies
zherbz Nov 20, 2024
ffc1f1b
removed redundant methods and updated bytes conversion to use utils …
zherbz Nov 20, 2024
728b661
updated test functions related to observed hdf data reading
zherbz Nov 20, 2024
6443edb
updated test csv datasets related to observed hdf function testing
zherbz Nov 20, 2024
f448399
updated mac/linux env install docs
zherbz Nov 25, 2024
93fdadd
removed parse_ras_datetime import, and instead defaulting to stage un…
zherbz Nov 25, 2024
1ef65f2
added tests for value error and rasplanhdf error related to observed …
zherbz Nov 25, 2024
fe0421a
updated observed_timeseries_input to instead return an xarray dataset…
zherbz Nov 25, 2024
6cd4371
updated tests with new function features
zherbz Nov 25, 2024
9d6ea25
updated test datasets now with added dims for columns
zherbz Nov 25, 2024
5c9be9c
renamed the col Date to time for consistency with other methods in li…
zherbz Nov 26, 2024
59b1a8f
renamed the col Date to time for consistency with other methods in li…
zherbz Nov 26, 2024
5086776
renamed the col Date to time for consistency with other methods in li…
zherbz Nov 26, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .devcontainer/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
FROM mcr.microsoft.com/devcontainers/base:jammy
28 changes: 28 additions & 0 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"name": "rashdf-devcontainer",
"build": {
"dockerfile": "Dockerfile",
"context": ".."
},
"features": {
"ghcr.io/devcontainers/features/git:1": {},
"ghcr.io/devcontainers/features/python:1":{"version":"3.12"}
},
"mounts": [],
"customizations": {
"vscode": {
"extensions": [
"ms-python.python",
"ms-python.pylint",
"GitHub.copilot",
"ms-python.black-formatter"
thwllms marked this conversation as resolved.
Show resolved Hide resolved
],
"settings": {
"python.defaultInterpreterPath": "/opt/conda/envs/rashdf/bin/python"
}
}
},
// avoid dubious ownership of the workspace folder https://www.kenmuse.com/blog/avoiding-dubious-ownership-in-dev-containers/
"postStartCommand": "git config --global --add safe.directory ${containerWorkspaceFolder}",
"postCreateCommand": "sudo chown -R vscode:vscode ${containerWorkspaceFolder}"
}
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,14 @@ $ python -m venv venv-rashdf

Activate the virtual environment:
```
# For macOS/Linux
$ source ./venv/bin/activate
(venv-rashdf) $
(venv-rashdf)
thwllms marked this conversation as resolved.
Show resolved Hide resolved
Or
$ source venv-rashdf/bin/activate
thwllms marked this conversation as resolved.
Show resolved Hide resolved

# For Windows
$ source venv-rashdf/Scripts/activate
thwllms marked this conversation as resolved.
Show resolved Hide resolved
```

Install dev dependencies:
Expand Down
4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ classifiers = [
"Programming Language :: Python :: 3.12",
]
version = "0.6.0"
dependencies = ["h5py", "geopandas>=1.0,<2.0", "pyarrow", "xarray"]
dependencies = ["h5py", "geopandas>=1.0,<2.0", "pyarrow", "xarray", "fiona==1.9.6"]
thwllms marked this conversation as resolved.
Show resolved Hide resolved

[project.optional-dependencies]
dev = ["pre-commit", "ruff", "pytest", "pytest-cov", "fiona", "kerchunk", "zarr", "dask", "fsspec", "s3fs"]
dev = ["pre-commit", "ruff", "pytest", "pytest-cov", "kerchunk", "zarr", "dask", "fsspec", "s3fs"]
docs = ["sphinx", "numpydoc", "sphinx_rtd_theme"]

[project.urls]
Expand Down
114 changes: 114 additions & 0 deletions src/rashdf/plan.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from .utils import (
df_datetimes_to_str,
ras_timesteps_to_datetimes,
parse_ras_datetime,
thwllms marked this conversation as resolved.
Show resolved Hide resolved
parse_ras_datetime_ms,
deprecated,
)
Expand Down Expand Up @@ -156,6 +157,7 @@
PLAN_INFO_PATH = "Plan Data/Plan Information"
PLAN_PARAMS_PATH = "Plan Data/Plan Parameters"
PRECIP_PATH = "Event Conditions/Meteorology/Precipitation"
OBS_DATA_PATH = "Event Conditions/Observed Data"
RESULTS_UNSTEADY_PATH = "Results/Unsteady"
RESULTS_UNSTEADY_SUMMARY_PATH = f"{RESULTS_UNSTEADY_PATH}/Summary"
VOLUME_ACCOUNTING_PATH = f"{RESULTS_UNSTEADY_PATH}/Volume Accounting"
Expand All @@ -166,6 +168,8 @@
UNSTEADY_TIME_SERIES_PATH = f"{BASE_OUTPUT_PATH}/Unsteady Time Series"
REFERENCE_LINES_OUTPUT_PATH = f"{UNSTEADY_TIME_SERIES_PATH}/Reference Lines"
REFERENCE_POINTS_OUTPUT_PATH = f"{UNSTEADY_TIME_SERIES_PATH}/Reference Points"
OBS_FLOW_OUTPUT_PATH = f"{OBS_DATA_PATH}/Flow"
OBS_STAGE_OUTPUT_PATH = f"{OBS_DATA_PATH}/Stage"

RESULTS_STEADY_PATH = "Results/Steady"
BASE_STEADY_PATH = f"{RESULTS_STEADY_PATH}/Output/Output Blocks/Base Output"
Expand Down Expand Up @@ -1117,6 +1121,106 @@
"""
return self.reference_timeseries_output(reftype="lines")

def observed_timeseries_input(self, vartype: str = "Flow") -> xr.Dataset:
"""Return observed timeseries input data for reference lines and points from a HEC-RAS HDF plan file.

Parameters
----------
vartype : str, optional
The type of observed data to retrieve. Must be either "Flow" or "Stage".
(default: "Flow")

Returns
-------
Dict[Site: str, xr.DataArray]
An xarray Dataset with observed timeseries data for both reference lines and reference points.

Coordinates:
- 'time': datetime64[ns]

Data variables:
- 'Flow' or 'Stage': float64
"""

# Decode the contents of the DataFrame from utf-8
def decode_bytes(val):
thwllms marked this conversation as resolved.
Show resolved Hide resolved
if isinstance(val, bytes):
return val.decode("utf-8")
return val

if vartype == "Flow":
output_path = self.OBS_FLOW_OUTPUT_PATH
elif vartype == "Stage":
output_path = self.OBS_STAGE_OUTPUT_PATH
else:
raise ValueError('vartype must be either "Flow" or "Stage".')

Check warning on line 1156 in src/rashdf/plan.py

View check run for this annotation

Codecov / codecov/patch

src/rashdf/plan.py#L1156

Added line #L1156 was not covered by tests

observed_group = self.get(output_path)
if observed_group is None:
raise RasPlanHdfError(

Check warning on line 1160 in src/rashdf/plan.py

View check run for this annotation

Codecov / codecov/patch

src/rashdf/plan.py#L1160

Added line #L1160 was not covered by tests
f"Could not find HDF group at path '{output_path}'."
f" Does the Plan HDF file contain reference {vartype[:-1]} output data?"
thwllms marked this conversation as resolved.
Show resolved Hide resolved
)
print(f"observed_group: {observed_group}")
thwllms marked this conversation as resolved.
Show resolved Hide resolved
if "Attributes" in observed_group.keys():
attr_path = observed_group["Attributes"]
print(f"attr_path: {attr_path}")
thwllms marked this conversation as resolved.
Show resolved Hide resolved
attrs_df = pd.DataFrame(attr_path[:])
# Apply the decoding function to each element in the DataFrame
attrs_df = attrs_df.map(decode_bytes)
if vartype == "Flow":
attrs_df["Units"] = "cfs"
elif vartype == "Stage":
attrs_df["Units"] = "ft"
else:
attrs_df["Units"] = "Unknown"

Check warning on line 1176 in src/rashdf/plan.py

View check run for this annotation

Codecov / codecov/patch

src/rashdf/plan.py#L1176

Added line #L1176 was not covered by tests
thwllms marked this conversation as resolved.
Show resolved Hide resolved
das = {}
thwllms marked this conversation as resolved.
Show resolved Hide resolved
for site in observed_group.keys():
if site != "Attributes":
# Site Ex: 'Ref Point: Grapevine_Lake_RP'
site_path = observed_group[site]
prefix = site.split(":")[0]
thwllms marked this conversation as resolved.
Show resolved Hide resolved
suffix = site.split(":")[1][1:]
data_df = pd.DataFrame(site_path[:])
# Apply the decoding function to each element in the DataFrame
data_df = data_df.map(decode_bytes)
# Assign data types to the columns
data_df["Date"] = data_df["Date"].apply(parse_ras_datetime)
data_df["Date"] = pd.to_datetime(
data_df["Date"], format="%d%b%Y %H:%M:%S"
)
data_df["Value"] = data_df["Value"].astype(float)
# Determine the site type
site_type = (
thwllms marked this conversation as resolved.
Show resolved Hide resolved
"reference_line" if "Ref Line" in site else "reference_point"
)
# Package into an xarray DataArray
da = xr.DataArray(
data_df["Value"].values,
name=vartype,
dims=["time"],
coords={
"time": data_df["Date"].values,
},
attrs={
"units": attrs_df["Units"][0],
"hdf_path": f"{output_path}/{site}",
},
)
das[suffix] = da

return das

def observed_data_timeseries_input(self) -> xr.Dataset:
thwllms marked this conversation as resolved.
Show resolved Hide resolved
"""Return observed data timeseries input data for reference lines or points from a HEC-RAS HDF plan file.

Returns
-------
xr.Dataset
An xarray Dataset with observed timeseries input data for reference lines or points.
"""
return self.observed_timeseries_input(vartype="Flow")

Check warning on line 1222 in src/rashdf/plan.py

View check run for this annotation

Codecov / codecov/patch

src/rashdf/plan.py#L1222

Added line #L1222 was not covered by tests

def reference_points_timeseries_output(self) -> xr.Dataset:
"""Return timeseries output data for reference points from a HEC-RAS HDF plan file.

Expand Down Expand Up @@ -1279,6 +1383,16 @@
"""
return self.get_attrs(self.PRECIP_PATH)

def get_obs_data_attrs(self) -> Dict:
thwllms marked this conversation as resolved.
Show resolved Hide resolved
"""Return observed data attributes from a HEC-RAS HDF plan file.

Returns
-------
dict
Dictionary of observed data attributes.
"""
return self.get_attrs(self.OBS_DATA_PATH)

Check warning on line 1394 in src/rashdf/plan.py

View check run for this annotation

Codecov / codecov/patch

src/rashdf/plan.py#L1394

Added line #L1394 was not covered by tests

def get_results_unsteady_attrs(self) -> Dict:
"""Return unsteady attributes from a HEC-RAS HDF plan file.

Expand Down
Loading