@@ -554,7 +554,7 @@ def _parse_grid_mapping_attribute(grid_mapping_attr: str) -> dict[str, list[Hash
554
554
555
555
def _create_grid_mapping (
556
556
var_name : str ,
557
- obj_dataset : Dataset ,
557
+ ds : Dataset ,
558
558
grid_mapping_dict : dict [str , list [Hashable ]],
559
559
) -> GridMapping :
560
560
"""
@@ -576,41 +576,40 @@ def _create_grid_mapping(
576
576
577
577
Notes
578
578
-----
579
- Assumes pyproj is available (should be checked by caller) .
579
+ Assumes pyproj is available.
580
580
"""
581
- from pyproj import (
582
- CRS , # Safe to import since grid_mappings property checks availability
583
- )
581
+ import pyproj
584
582
585
- var = obj_dataset ._variables [var_name ]
583
+ var = ds ._variables [var_name ]
586
584
587
585
# 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 )
593
587
594
588
# Get the CF grid mapping name from the variable's attributes
595
589
cf_name = var .attrs .get ("grid_mapping_name" , var_name )
596
590
597
591
# 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 )
603
593
604
594
# Get associated coordinate variables, fallback to dimension names
605
595
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 ))
614
613
615
614
return GridMapping (name = cf_name , crs = crs , array = da , coordinates = tuple (coordinates ))
616
615
0 commit comments