Skip to content

Commit 27d0cbf

Browse files
committed
add tests
1 parent e90d40e commit 27d0cbf

3 files changed

Lines changed: 79 additions & 7 deletions

File tree

check_pygrib_vs_herbie_crs_extraction.ipynb

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
},
1616
{
1717
"cell_type": "code",
18-
"execution_count": 1,
18+
"execution_count": 2,
1919
"metadata": {},
2020
"outputs": [],
2121
"source": [
@@ -129,15 +129,16 @@
129129
" elif model[\"model\"] in (\"hrdps\",): # no historical data\n",
130130
" date = pd.Timestamp(\"now\").floor(\"6h\") - pd.Timedelta(\"6h\")\n",
131131
" else:\n",
132-
" date = \"2025-01-01\"\n",
132+
" date = \"2025-07-05T06:00:00\"\n",
133133
"\n",
134-
" ds = Herbie(date, verbose=False, **model).xarray(\n",
135-
" search, remove_grib=False, _use_pygrib_for_crs=False\n",
136-
" )\n",
134+
" # ds = Herbie(date, verbose=False, **model).xarray(\n",
135+
" # search, remove_grib=False, _use_pygrib_for_crs=False\n",
136+
" # )\n",
137+
" ds = Herbie(date, verbose=False, **model).download()\n",
137138
"\n",
138139
" print()\n",
139140
" print(f\"Model: {model['model'].upper()}\")\n",
140-
" with pygrib.open(str(ds.local_grib)) as grb:\n",
141+
" with pygrib.open(str(ds)) as grb:\n",
141142
" msg = grb.message(1)\n",
142143
" projparams_dict_pygrib = dict(sorted(msg.projparams.items()))\n",
143144
" print(\" pygrib\", projparams_dict_pygrib)\n",

pyproject.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,12 @@ keywords = [
3232
dependencies = [
3333
"cfgrib>=0.9.15",
3434
"eccodes>=2.37.0",
35+
"jupyter>=1.1.1",
3536
"numpy>=1.24",
3637
"pandas>=2.1",
3738
"pyproj>=3.7.0",
3839
"requests>=2.23.3",
39-
"toml>=0.10.2", # TODO: Drop in favor of tomllib when Python >=3.11 is required.
40+
"toml>=0.10.2", # TODO: Drop in favor of tomllib when Python >=3.11 is required.
4041
"xarray>=2025.1.1",
4142
]
4243
dynamic = ["version"]
@@ -56,6 +57,7 @@ build-backend = "hatchling.build"
5657
dev = [
5758
"ipykernel>=6.29.5",
5859
"jupyter>=1.1.1",
60+
"netcdf4>=1.7.2",
5961
"pygrib>=2.1.6",
6062
"pytest>=8.3.5",
6163
"pytest-cov>=6.1.1",

tests/test_hrdps.py

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
## Nikhil Shankar
2+
## August 10, 2025
3+
4+
"""Tests for downloading HRDPS model."""
5+
6+
from datetime import datetime
7+
8+
import pandas as pd
9+
10+
from herbie import Herbie, config
11+
12+
now = datetime.now()
13+
latest = pd.Timestamp("now").floor("6h") - pd.Timedelta("6h")
14+
15+
save_dir = config["default"]["save_dir"] / "Herbie-Tests-Data/"
16+
17+
# Remove all previous test data
18+
for i in (save_dir / "hrdps").rglob("*"):
19+
if i.is_file():
20+
i.unlink()
21+
22+
23+
def test_hrdps_download():
24+
H = Herbie(
25+
latest,
26+
model="hrdps",
27+
product="continental/2.5km",
28+
variable="TMP",
29+
level="AGL-2m",
30+
overwrite=True,
31+
save_dir=save_dir,
32+
)
33+
f = H.download()
34+
assert H.get_localFilePath().exists()
35+
f.unlink()
36+
37+
38+
def test_hrdps_xarray():
39+
H = Herbie(
40+
latest,
41+
model="hrdps",
42+
product="continental/2.5km",
43+
variable="TMP",
44+
level="AGL-2m",
45+
overwrite=True,
46+
save_dir=save_dir,
47+
)
48+
H.xarray(remove_grib=False)
49+
assert H.get_localFilePath().exists()
50+
H.get_localFilePath().unlink()
51+
52+
53+
def test_hrdps_to_netcdf():
54+
"""Check that a xarray Dataset can be written to a NetCDF file.
55+
56+
It is important that I have haven't put any python objects in the
57+
xarray Dataset attributes.
58+
"""
59+
H = Herbie(
60+
latest,
61+
model="hrdps",
62+
product="continental/2.5km",
63+
variable="TMP",
64+
level="AGL-2m",
65+
overwrite=True,
66+
save_dir=save_dir,
67+
)
68+
ds = H.xarray(remove_grib=False)
69+
ds.to_netcdf(save_dir / "test_hrdps_to_netcdf.nc")

0 commit comments

Comments
 (0)