Skip to content

Commit 6696f04

Browse files
dcherianclaude
andcommitted
cleanup
Co-authored-by: Claude <[email protected]>
1 parent 87ccd42 commit 6696f04

File tree

1 file changed

+23
-24
lines changed

1 file changed

+23
-24
lines changed

cf_xarray/accessor.py

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -554,7 +554,7 @@ def _parse_grid_mapping_attribute(grid_mapping_attr: str) -> dict[str, list[Hash
554554

555555
def _create_grid_mapping(
556556
var_name: str,
557-
obj_dataset: Dataset,
557+
ds: Dataset,
558558
grid_mapping_dict: dict[str, list[Hashable]],
559559
) -> GridMapping:
560560
"""
@@ -576,41 +576,40 @@ def _create_grid_mapping(
576576
577577
Notes
578578
-----
579-
Assumes pyproj is available (should be checked by caller).
579+
Assumes pyproj is available.
580580
"""
581-
from pyproj import (
582-
CRS, # Safe to import since grid_mappings property checks availability
583-
)
581+
import pyproj
584582

585-
var = obj_dataset._variables[var_name]
583+
var = ds._variables[var_name]
586584

587585
# Create DataArray from Variable, preserving the name
588-
# Use reset_coords(drop=True) to avoid coordinate conflicts
589-
if var_name in obj_dataset.coords:
590-
da = obj_dataset.coords[var_name].reset_coords(drop=True)
591-
else:
592-
da = obj_dataset[var_name].reset_coords(drop=True)
586+
da = xr.DataArray(ds._variables[var_name], name=var_name)
593587

594588
# Get the CF grid mapping name from the variable's attributes
595589
cf_name = var.attrs.get("grid_mapping_name", var_name)
596590

597591
# Create CRS from the grid mapping variable
598-
try:
599-
crs = CRS.from_cf(var.attrs)
600-
except Exception:
601-
# If CRS creation fails, use None
602-
crs = None
592+
crs = pyproj.CRS.from_cf(var.attrs)
603593

604594
# Get associated coordinate variables, fallback to dimension names
605595
coordinates: list[Hashable] = grid_mapping_dict.get(var_name, [])
606-
if not coordinates:
607-
# For DataArrays, find the data variable that references this grid mapping
608-
for _data_var_name, data_var in obj_dataset.data_vars.items():
609-
if "grid_mapping" in data_var.attrs:
610-
gm_attr = data_var.attrs["grid_mapping"]
611-
if var_name in gm_attr:
612-
coordinates = list(data_var.dims)
613-
break
596+
# """
597+
# In order to make use of a grid mapping to directly calculate latitude and longitude values
598+
# it is necessary to associate the coordinate variables with the independent variables of the mapping.
599+
# This is done by assigning a standard_name to the coordinate variable.
600+
# The appropriate values of the standard_name depend on the grid mapping and are given in Appendix F, Grid Mappings.
601+
# """
602+
if not coordinates and len(grid_mapping_dict) == 1:
603+
if crs.to_cf().get("grid_mapping_name") == "rotated_latitude_longitude":
604+
xname, yname = "grid_longitude", "grid_latitude"
605+
elif crs.is_geographic:
606+
xname, yname = "longitude", "latitude"
607+
elif crs.is_projected:
608+
xname, yname = "projection_x_coordinate", "projection_y_coordinate"
609+
610+
x = apply_mapper(_get_with_standard_name, ds, xname, error=False, default=[[]])
611+
y = apply_mapper(_get_with_standard_name, ds, yname, error=False, default=[[]])
612+
coordinates = tuple(itertools.chain(x, y))
614613

615614
return GridMapping(name=cf_name, crs=crs, array=da, coordinates=tuple(coordinates))
616615

0 commit comments

Comments
 (0)