|
2 | 2 | import unittest |
3 | 3 | from typing import Set |
4 | 4 |
|
| 5 | +import fsspec |
5 | 6 | import numpy as np |
6 | 7 | import pyproj |
7 | 8 | import xarray as xr |
8 | 9 |
|
| 10 | +from test.s3test import MOTO_SERVER_ENDPOINT_URL |
| 11 | +from test.s3test import S3Test |
| 12 | +from xcube.core.gridmapping import GridMapping |
9 | 13 | from xcube.core.gridmapping.cfconv import GridCoords |
10 | 14 | from xcube.core.gridmapping.cfconv import GridMappingProxy |
| 15 | +from xcube.core.gridmapping.cfconv import add_spatial_ref |
11 | 16 | from xcube.core.gridmapping.cfconv import get_dataset_grid_mapping_proxies |
| 17 | +from xcube.core.new import new_cube |
| 18 | +from xcube.core.store.fs.registry import new_fs_data_store |
12 | 19 |
|
13 | 20 | CRS_WGS84 = pyproj.crs.CRS(4326) |
14 | 21 | CRS_CRS84 = pyproj.crs.CRS.from_string("urn:ogc:def:crs:OGC:1.3:CRS84") |
@@ -192,3 +199,98 @@ def _gen_2d(self): |
192 | 199 | self.assertEqual(('y', 'x'), lon.dims) |
193 | 200 | self.assertEqual(('y', 'x'), lat.dims) |
194 | 201 | return noise, crs, lon, lat |
| 202 | + |
| 203 | + |
| 204 | +class AddSpatialRefTest(S3Test): |
| 205 | + |
| 206 | + def test_add_spatial_ref(self): |
| 207 | + self.assert_add_spatial_ref_ok(None, None) |
| 208 | + self.assert_add_spatial_ref_ok(None, ('cx', 'cy')) |
| 209 | + self.assert_add_spatial_ref_ok('crs', None) |
| 210 | + self.assert_add_spatial_ref_ok('crs', ('cx', 'cy')) |
| 211 | + |
| 212 | + def assert_add_spatial_ref_ok(self, crs_var_name, xy_dim_names): |
| 213 | + |
| 214 | + root = 'eurodatacube-test/xcube-eea' |
| 215 | + data_id = 'test.zarr' |
| 216 | + crs = pyproj.CRS.from_string("EPSG:3035") |
| 217 | + |
| 218 | + if xy_dim_names: |
| 219 | + x_name, y_name = xy_dim_names |
| 220 | + else: |
| 221 | + x_name, y_name = 'x', 'y' |
| 222 | + |
| 223 | + cube = new_cube(x_name=x_name, |
| 224 | + y_name=y_name, |
| 225 | + x_start=0, |
| 226 | + y_start=0, |
| 227 | + x_res=10, |
| 228 | + y_res=10, |
| 229 | + x_units='metres', |
| 230 | + y_units='metres', |
| 231 | + drop_bounds=True, |
| 232 | + width=100, |
| 233 | + height=100, |
| 234 | + variables=dict(A=1.3, B=8.3)) |
| 235 | + |
| 236 | + storage_options = dict( |
| 237 | + anon=False, |
| 238 | + client_kwargs=dict( |
| 239 | + endpoint_url=MOTO_SERVER_ENDPOINT_URL, |
| 240 | + ) |
| 241 | + ) |
| 242 | + |
| 243 | + fs: fsspec.AbstractFileSystem = fsspec.filesystem('s3', |
| 244 | + **storage_options) |
| 245 | + if fs.isdir(root): |
| 246 | + fs.rm(root, recursive=True) |
| 247 | + fs.mkdirs(root, exist_ok=True) |
| 248 | + |
| 249 | + data_store = new_fs_data_store('s3', |
| 250 | + root=root, |
| 251 | + storage_options=storage_options) |
| 252 | + |
| 253 | + data_store.write_data(cube, data_id=data_id) |
| 254 | + cube = data_store.open_data(data_id) |
| 255 | + self.assertEqual({'A', 'B', 'time', x_name, y_name}, |
| 256 | + set(cube.variables)) |
| 257 | + |
| 258 | + with self.assertRaises(ValueError) as cm: |
| 259 | + GridMapping.from_dataset(cube) |
| 260 | + self.assertEqual( |
| 261 | + ('cannot find any grid mapping in dataset',), |
| 262 | + cm.exception.args |
| 263 | + ) |
| 264 | + |
| 265 | + path = f"{root}/{data_id}" |
| 266 | + group_store = fs.get_mapper(path, create=True) |
| 267 | + |
| 268 | + expected_crs_var_name = crs_var_name or 'spatial_ref' |
| 269 | + |
| 270 | + self.assertTrue(fs.exists(path)) |
| 271 | + self.assertFalse(fs.exists(f"{path}/{expected_crs_var_name}")) |
| 272 | + self.assertFalse(fs.exists(f"{path}/{expected_crs_var_name}/.zarray")) |
| 273 | + self.assertFalse(fs.exists(f"{path}/{expected_crs_var_name}/.zattrs")) |
| 274 | + |
| 275 | + kwargs = {} |
| 276 | + if crs_var_name is not None: |
| 277 | + kwargs.update(crs_var_name=crs_var_name) |
| 278 | + if xy_dim_names is not None: |
| 279 | + kwargs.update(xy_dim_names=xy_dim_names) |
| 280 | + add_spatial_ref(group_store, crs, **kwargs) |
| 281 | + |
| 282 | + self.assertTrue(fs.exists(f"{path}/{expected_crs_var_name}")) |
| 283 | + self.assertTrue(fs.exists(f"{path}/{expected_crs_var_name}/.zarray")) |
| 284 | + self.assertTrue(fs.exists(f"{path}/{expected_crs_var_name}/.zattrs")) |
| 285 | + |
| 286 | + cube = data_store.open_data(data_id) |
| 287 | + self.assertEqual({'A', 'B', 'time', |
| 288 | + x_name, y_name, expected_crs_var_name}, |
| 289 | + set(cube.variables)) |
| 290 | + self.assertEqual(expected_crs_var_name, |
| 291 | + cube.A.attrs.get('grid_mapping')) |
| 292 | + self.assertEqual(expected_crs_var_name, |
| 293 | + cube.B.attrs.get('grid_mapping')) |
| 294 | + |
| 295 | + gm = GridMapping.from_dataset(cube) |
| 296 | + self.assertIn("LAEA Europe", gm.crs.srs) |
0 commit comments