Skip to content

Commit

Permalink
added uv.lock to gitignore for uv users, added test coverage and addr…
Browse files Browse the repository at this point in the history
…essed issue code
  • Loading branch information
aaTman committed Feb 14, 2025
1 parent a96cbf5 commit ac03318
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 6 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,4 @@ herbie/_version.py
.coverage

.ruff_cache/
uv.lock
10 changes: 5 additions & 5 deletions herbie/accessors.py
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,7 @@ def plant_tree(save_pickle: Optional[Union[Path, str]] = None):
print("INFO: 🌱 Growing new BallTree...", end="")
tree = BallTree(np.deg2rad(df_grid), metric="haversine")
print(
f"🌳 BallTree grew in {(pd.Timestamp('now')-timer).total_seconds():.2} seconds."
f"🌳 BallTree grew in {(pd.Timestamp('now') - timer).total_seconds():.2} seconds."
)
if save_pickle:
try:
Expand Down Expand Up @@ -512,7 +512,7 @@ def plant_tree(save_pickle: Optional[Union[Path, str]] = None):

a = pd.concat(
[
a,
a.reset_index(drop=True),
df_grid.iloc[a.grid_index]
.add_suffix("_grid")
.reset_index(drop=True),
Expand All @@ -534,8 +534,8 @@ def plant_tree(save_pickle: Optional[Union[Path, str]] = None):
# Get corresponding values from xarray
# https://docs.xarray.dev/en/stable/user-guide/indexing.html#more-advanced-indexing
ds_points = ds.sel(
x=a.x_grid.to_xarray(),
y=a.y_grid.to_xarray(),
x=a.x_grid.to_xarray().dropna("point").astype("int"),
y=a.y_grid.to_xarray().dropna("point").astype("int"),
)
ds_points.coords["point_grid_distance"] = a.point_grid_distance.to_xarray()
ds_points["point_grid_distance"].attrs["long_name"] = (
Expand Down Expand Up @@ -848,7 +848,7 @@ def plot(self, ax=None, common_features_kw={}, vars=None, **kwargs):

VALID = pd.to_datetime(ds.valid_time.data).strftime("%H:%M UTC %d %b %Y")
RUN = pd.to_datetime(ds.time.data).strftime("%H:%M UTC %d %b %Y")
FXX = f"F{pd.to_timedelta(ds.step.data).total_seconds()/3600:02.0f}"
FXX = f"F{pd.to_timedelta(ds.step.data).total_seconds() / 3600:02.0f}"

level_type = ds[var].GRIB_typeOfLevel
if level_type in _level_units:
Expand Down
55 changes: 54 additions & 1 deletion tests/test_pick_points.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import xarray as xr
import random
import string

import numpy as np
from herbie import Herbie

ds1 = Herbie("2024-03-01 00:00", model="hrrr").xarray("TMP:[5,6,7,8,9][0,5]0 mb")
Expand Down Expand Up @@ -60,6 +60,59 @@ def test_pick_points_simple():
)


def test_pick_points_nans_in_grid():
"""Tests when there are null values in the xarray dataset."""
ds = xr.Dataset(
{
"a": (
["latitude", "longitude"],
[[1, 0, np.nan], [0, 0, np.nan], [0, 1, np.nan]],
)
},
coords={
"latitude": (["latitude"], [45, 46, 47]),
"longitude": (["longitude"], [100, 101, 102]),
},
)
point = pd.DataFrame({"latitude": [45.25], "longitude": [100.25]})

p = ds.herbie.pick_points(point, method="nearest", k=1)
assert all(
[
p.latitude.item() == 45,
p.longitude.item() == 100,
p.point_grid_distance.round(2).item() == 34.02,
p.point_latitude.item() == 45.25,
p.point_longitude.item() == 100.25,
p.a.item() == 1,
]
)


def test_pick_points_pd_index_off():
"""Test a very simple grid."""
ds = xr.Dataset(
{"a": (["latitude", "longitude"], [[1, 0], [0, 0]])},
coords={
"latitude": (["latitude"], [45, 46]),
"longitude": (["longitude"], [100, 101]),
},
)
point = pd.DataFrame({"latitude": [45.25], "longitude": [100.25]}, index=[200])

p = ds.herbie.pick_points(point, method="nearest", k=1)
assert all(
[
p.latitude.item() == 45,
p.longitude.item() == 100,
p.point_grid_distance.round(2).item() == 34.02,
p.point_latitude.item() == 45.25,
p.point_longitude.item() == 100.25,
p.a.item() == 1,
]
)


def test_pick_points_self_points():
"""Test pick points with model's own grid points."""
H = Herbie("2024-03-01", model="hrrr")
Expand Down

0 comments on commit ac03318

Please sign in to comment.