From c19319dae87e79f5f1808cc0a6087997667a4368 Mon Sep 17 00:00:00 2001 From: konstntokas Date: Tue, 14 Jan 2025 16:39:05 +0100 Subject: [PATCH] Tonios comment addressed --- test/sampledata.py | 6 +++++- xcube/core/resampling/spatial.py | 11 +++++++---- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/test/sampledata.py b/test/sampledata.py index ca7e375cf..0f702f155 100644 --- a/test/sampledata.py +++ b/test/sampledata.py @@ -376,7 +376,11 @@ def new_5x5_dataset_regular_utm(cls): spatial_ref = np.array(0) band_1 = np.arange(25).reshape((5, 5)) ds = xr.Dataset( - dict(band_1=xr.DataArray(band_1, dims=("y", "x"))), + dict( + band_1=xr.DataArray( + band_1, dims=("y", "x"), attrs=dict(grid_mapping="spatial_ref") + ) + ), coords=dict(x=x, y=y, spatial_ref=spatial_ref), ) ds.spatial_ref.attrs = pyproj.CRS.from_epsg("32632").to_cf() diff --git a/xcube/core/resampling/spatial.py b/xcube/core/resampling/spatial.py index f00c8c905..d671eae2a 100644 --- a/xcube/core/resampling/spatial.py +++ b/xcube/core/resampling/spatial.py @@ -208,15 +208,18 @@ def resample_in_space( # If CRSes are not both geographic and their CRSes are different # transform the source_gm so its CRS matches the target CRS: transformed_source_gm = source_gm.transform(target_gm.crs, xy_res=target_gm.xy_res) - transformed_x, transformed_y = transformed_source_gm.xy_coords - attrs = dict(grid_mapping="spatial_ref") - transformed_x.attrs = attrs - transformed_y.attrs = attrs source_ds = source_ds.drop_vars(source_gm.xy_dim_names) if "crs" in source_ds: source_ds = source_ds.drop_vars("crs") if "spatial_ref" in source_ds: source_ds = source_ds.drop_vars("spatial_ref") + for var in source_ds.data_vars: + if "grid_mapping" in source_ds[var].attrs: + del source_ds[var].attrs["grid_mapping"] + transformed_x, transformed_y = transformed_source_gm.xy_coords + attrs = dict(grid_mapping="spatial_ref") + transformed_x.attrs = attrs + transformed_y.attrs = attrs source_ds = source_ds.assign_coords( spatial_ref=xr.DataArray(0, attrs=transformed_source_gm.crs.to_cf()), transformed_x=transformed_x,