Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BUG: Handle CF CRS export errors in 'rio.write_crs' #595

Merged
merged 1 commit into from
Oct 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/history.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ History

Latest
------
- BUG: Handle CF CRS export errors in `rio.write_crs` (discussion #591)

0.12.2
------
Expand Down
8 changes: 5 additions & 3 deletions rioxarray/rioxarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -489,10 +489,12 @@ def write_crs(
)
# add grid mapping coordinate
data_obj.coords[grid_mapping_name] = xarray.Variable((), 0)
grid_map_attrs = {}
if get_option(EXPORT_GRID_MAPPING):
grid_map_attrs = pyproj.CRS.from_user_input(data_obj.rio.crs).to_cf()
else:
grid_map_attrs = {}
try:
grid_map_attrs = pyproj.CRS.from_user_input(data_obj.rio.crs).to_cf()
except KeyError:
pass
# spatial_ref is for compatibility with GDAL
crs_wkt = data_obj.rio.crs.to_wkt()
grid_map_attrs["spatial_ref"] = crs_wkt
Expand Down
36 changes: 36 additions & 0 deletions test/integration/test_integration_rioxarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -2115,6 +2115,42 @@ def test_write_crs_cf__disable_grid_mapping():
assert "grid_mapping_name" not in test_da.spatial_ref.attrs


def test_write_crs_cf__grib_wkt():
# https://github.com/corteva/rioxarray/discussions/591
grib_rotated_wkt = """GEOGCRS["Coordinate System imported from GRIB file",
BASEGEOGCRS["Coordinate System imported from GRIB file",
DATUM["unnamed",
ELLIPSOID["Sphere",6371229,0,
LENGTHUNIT["metre",1,
ID["EPSG",9001]]]],
PRIMEM["Greenwich",0,
ANGLEUNIT["degree",0.0174532925199433,
ID["EPSG",9122]]]],
DERIVINGCONVERSION["Pole rotation (GRIB convention)",
METHOD["Pole rotation (GRIB convention)"],
PARAMETER["Latitude of the southern pole (GRIB convention)",-33.443381,
ANGLEUNIT["degree",0.0174532925199433,
ID["EPSG",9122]]],
PARAMETER["Longitude of the southern pole (GRIB convention)",-93.536426,
ANGLEUNIT["degree",0.0174532925199433,
ID["EPSG",9122]]],
PARAMETER["Axis rotation (GRIB convention)",0,
ANGLEUNIT["degree",0.0174532925199433,
ID["EPSG",9122]]]],
CS[ellipsoidal,2],
AXIS["latitude",north,
ORDER[1],
ANGLEUNIT["degree",0.0174532925199433,
ID["EPSG",9122]]],
AXIS["longitude",east,
ORDER[2],
ANGLEUNIT["degree",0.0174532925199433,
ID["EPSG",9122]]]]"""

test_da = xarray.DataArray(1)
test_da.rio.write_crs(grib_rotated_wkt, inplace=True)


def test_write_crs__missing_geospatial_dims():
test_da = xarray.DataArray(
[1],
Expand Down