Skip to content

Commit 309e18d

Browse files
committed
one more
1 parent 74c4c61 commit 309e18d

File tree

1 file changed

+2
-25
lines changed

1 file changed

+2
-25
lines changed

cf_xarray/accessor.py

Lines changed: 2 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -492,11 +492,9 @@ def _get_grid_mapping_name(obj: DataArray | Dataset, key: str) -> list[str]:
492492
results = set()
493493
for var in variables.values():
494494
attrs_or_encoding = ChainMap(var.attrs, var.encoding)
495-
if "grid_mapping" in attrs_or_encoding:
496-
grid_mapping_attr = attrs_or_encoding["grid_mapping"]
495+
if grid_mapping_attr := attrs_or_encoding.get("grid_mapping"):
497496
# Parse potentially multiple grid mappings
498497
grid_mapping_var_names = _parse_grid_mapping_attribute(grid_mapping_attr)
499-
500498
for grid_mapping_var_name in grid_mapping_var_names:
501499
if grid_mapping_var_name not in variables:
502500
raise ValueError(
@@ -1979,28 +1977,7 @@ def get_associated_variable_names(
19791977
if grid_mapping_attr := attrs_or_encoding.get("grid_mapping", None):
19801978
# Parse grid mapping variables using the same function
19811979
grid_mapping_vars = _parse_grid_mapping_attribute(grid_mapping_attr)
1982-
coords["grid_mapping"] = grid_mapping_vars
1983-
1984-
# Extract coordinate variables using regex
1985-
if ":" in grid_mapping_attr:
1986-
# Pattern to find coordinate variables: words that come after ":" but before next grid mapping variable
1987-
# This captures coordinate variables between grid mapping sections
1988-
coord_pattern = r":\s+([^:]+?)(?=\s+[a-zA-Z_][a-zA-Z0-9_]*\s*:|$)"
1989-
coord_matches = re.findall(coord_pattern, grid_mapping_attr)
1990-
1991-
for coord_section in coord_matches:
1992-
# Split each coordinate section and add valid coordinate names
1993-
coord_vars = coord_section.split()
1994-
# Filter out grid mapping variable names that might have been captured
1995-
coord_vars = [
1996-
var
1997-
for var in coord_vars
1998-
if not (
1999-
var.startswith(("crs_", "spatial_", "proj_"))
2000-
and var in grid_mapping_vars
2001-
)
2002-
]
2003-
coords["coordinates"].extend(coord_vars)
1980+
coords["grid_mapping"] = cast(list[Hashable], grid_mapping_vars)
20041981

20051982
more: Sequence[Hashable] = ()
20061983
if geometry_var := attrs_or_encoding.get("geometry", None):

0 commit comments

Comments
 (0)