Skip to content

Commit d781272

Browse files
committed
Remove legacy netcdf test and supporting sanitization code
Per: #474 (comment)
1 parent a9494bf commit d781272

2 files changed

Lines changed: 1 addition & 73 deletions

File tree

src/herbie/core.py

Lines changed: 0 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
from urllib.parse import urlparse
2323

2424
import cfgrib
25-
import numpy as np
2625
import pandas as pd
2726
import requests
2827
import xarray as xr
@@ -52,58 +51,6 @@
5251
"Curl is not in system Path. Herbie won't be able to download GRIB files."
5352
)
5453

55-
56-
_INT32_MIN = np.iinfo(np.int32).min
57-
_INT32_MAX = np.iinfo(np.int32).max
58-
59-
60-
def _sanitize_attr_value(value):
61-
"""Convert attribute values so they are compatible with NetCDF3 writers."""
62-
63-
if isinstance(value, np.integer):
64-
value = int(value)
65-
if value < _INT32_MIN or value > _INT32_MAX:
66-
return float(value)
67-
return value
68-
if isinstance(value, np.floating):
69-
return float(value)
70-
if isinstance(value, np.ndarray):
71-
return [_sanitize_attr_value(v) for v in value.tolist()]
72-
if isinstance(value, list):
73-
return [_sanitize_attr_value(v) for v in value]
74-
if isinstance(value, tuple):
75-
return tuple(_sanitize_attr_value(v) for v in value)
76-
if isinstance(value, (np.datetime64, np.timedelta64)):
77-
if np.isnat(value):
78-
return None
79-
return value.item() if hasattr(value, "item") else value
80-
return value
81-
82-
83-
def _sanitize_attrs(obj):
84-
"""Ensure attributes are serialisable by NetCDF writers."""
85-
86-
obj.attrs = {k: _sanitize_attr_value(v) for k, v in obj.attrs.items()}
87-
88-
89-
def _sanitize_variable(obj):
90-
"""Sanitize both attributes and encoding for an xarray Variable."""
91-
92-
_sanitize_attrs(obj)
93-
94-
if not hasattr(obj, "dtype"):
95-
return
96-
97-
dtype = obj.dtype
98-
if dtype is None:
99-
return
100-
101-
if np.issubdtype(dtype, np.datetime64) or np.issubdtype(dtype, np.timedelta64):
102-
obj.attrs.pop("_FillValue", None)
103-
if hasattr(obj, "encoding"):
104-
obj.encoding["_FillValue"] = None
105-
106-
10754
def wgrib2_idx(grib2filepath: Union[Path, str]) -> str:
10855
"""
10956
Produce the GRIB2 inventory index with wgrib2.

tests/test_hrdps.py

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -81,23 +81,4 @@ def test_hrdps_xarray():
8181
)
8282
H.xarray(remove_grib=False)
8383
assert H.get_localFilePath().exists()
84-
H.get_localFilePath().unlink()
85-
86-
87-
def test_hrdps_to_netcdf():
88-
"""Check that a xarray Dataset can be written to a NetCDF file.
89-
90-
It is important that I have haven't put any python objects in the
91-
xarray Dataset attributes.
92-
"""
93-
H = Herbie(
94-
latest,
95-
model="hrdps",
96-
product="continental/2.5km",
97-
variable="TMP",
98-
level="AGL-2m",
99-
overwrite=True,
100-
save_dir=save_dir,
101-
)
102-
ds = H.xarray(remove_grib=False)
103-
ds.to_netcdf(save_dir / "test_hrdps_to_netcdf.nc")
84+
H.get_localFilePath().unlink()

0 commit comments

Comments
 (0)