Skip to content

Commit 93511ee

Browse files
dcherianclaude
andcommitted
Memoize CRS construction in grid_mappings
pyproj.CRS.from_cf re-parses the datum/ellipsoid on every call, which is expensive for grid mappings carrying explicit ellipsoid parameters (e.g. geostationary). A dataset references the same grid mapping from many variables and .cf.grid_mappings may be accessed repeatedly, so cache the constructed CRS on the grid-mapping variable's attrs. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
1 parent 87fa240 commit 93511ee

2 files changed

Lines changed: 70 additions & 1 deletion

File tree

‎cf_xarray/accessor.py‎

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -556,6 +556,37 @@ def _parse_grid_mapping_attribute(
556556
return Frozen(result)
557557

558558

559+
def _hashable_attrs(attrs: Mapping[Any, Any]) -> tuple:
560+
"""Return a hashable, order-independent representation of an attrs mapping.
561+
562+
List- and array-valued attributes (e.g. ``standard_parallel``) are coerced
563+
to tuples so the result can be used as an ``lru_cache`` key.
564+
"""
565+
frozen = []
566+
for key, value in attrs.items():
567+
if hasattr(value, "tolist"): # numpy scalars/arrays
568+
value = value.tolist()
569+
if isinstance(value, list | tuple):
570+
value = tuple(value)
571+
frozen.append((key, value))
572+
frozen.sort(key=lambda kv: repr(kv[0]))
573+
return tuple(frozen)
574+
575+
576+
@functools.lru_cache(maxsize=256)
577+
def _crs_from_cf_attrs(attrs_items: tuple) -> Any:
578+
"""Build a ``pyproj.CRS`` from frozen CF grid-mapping attrs (memoized).
579+
580+
``pyproj.CRS.from_cf`` re-parses the datum/ellipsoid on every call, which is
581+
expensive for grid mappings carrying explicit ellipsoid parameters (e.g.
582+
geostationary). A dataset routinely references the same grid mapping from
583+
many variables, so cache on the attribute items.
584+
"""
585+
import pyproj
586+
587+
return pyproj.CRS.from_cf(dict(attrs_items))
588+
589+
559590
def _create_grid_mapping(
560591
var_name: str,
561592
ds: Dataset,
@@ -669,7 +700,7 @@ def _create_grid_mapping(
669700
}
670701
)
671702
else:
672-
crs = pyproj.CRS.from_cf(var.attrs)
703+
crs = _crs_from_cf_attrs(_hashable_attrs(var.attrs))
673704

674705
# Get associated coordinate variables, fallback to dimension names
675706
coordinates: list[Hashable] = grid_mapping_dict.get(var_name, [])

‎cf_xarray/tests/test_accessor.py‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1254,6 +1254,44 @@ def test_grid_mappings_property():
12541254
assert gm.coordinates == ("x", "y")
12551255

12561256

1257+
@requires_pyproj
1258+
def test_grid_mappings_crs_construction_is_cached(monkeypatch):
1259+
"""``pyproj.CRS.from_cf`` is memoized per grid-mapping attrs.
1260+
1261+
Building the CRS re-parses the datum/ellipsoid on every call. A dataset
1262+
references the same grid mapping from many variables and the property may
1263+
be accessed repeatedly, so each distinct grid mapping should be built once.
1264+
"""
1265+
import pyproj
1266+
1267+
from ..accessor import _crs_from_cf_attrs
1268+
1269+
_crs_from_cf_attrs.cache_clear()
1270+
1271+
from ..datasets import hrrrds
1272+
1273+
ds = hrrrds
1274+
1275+
calls = {"n": 0}
1276+
orig = pyproj.CRS.from_cf
1277+
1278+
def counting_from_cf(*args, **kwargs):
1279+
calls["n"] += 1
1280+
return orig(*args, **kwargs)
1281+
1282+
monkeypatch.setattr(pyproj.CRS, "from_cf", staticmethod(counting_from_cf))
1283+
1284+
# Repeated property accesses, including via a DataArray, must not rebuild
1285+
# the same CRS: hrrrds has 3 distinct grid mappings, each built once.
1286+
ds.cf.grid_mappings
1287+
ds.cf.grid_mappings
1288+
ds.foo.cf.grid_mappings
1289+
1290+
assert calls["n"] == 3
1291+
1292+
_crs_from_cf_attrs.cache_clear()
1293+
1294+
12571295
@requires_pyproj
12581296
def test_grid_mappings_coordinates_attribute():
12591297
"""Test that coordinates attribute is always populated correctly for DataArray grid mappings."""

0 commit comments

Comments
 (0)