@@ -204,24 +204,35 @@ def _gen_2d(self):
204204class AddSpatialRefTest (S3Test ):
205205
206206 def test_add_spatial_ref (self ):
207- original_dataset = new_cube (x_name = 'x' ,
208- y_name = 'y' ,
209- x_start = 0 ,
210- y_start = 0 ,
211- x_res = 10 ,
212- y_res = 10 ,
213- x_units = 'metres' ,
214- y_units = 'metres' ,
215- drop_bounds = True ,
216- width = 100 ,
217- height = 100 ,
218- variables = dict (A = 1.3 , B = 8.3 ))
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 ):
219213
220214 root = 'eurodatacube-test/xcube-eea'
221215 data_id = 'test.zarr'
222- crs_var_name = 'spatial_ref'
223216 crs = pyproj .CRS .from_string ("EPSG:3035" )
224217
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+
225236 storage_options = dict (
226237 anon = False ,
227238 client_kwargs = dict (
@@ -239,13 +250,13 @@ def test_add_spatial_ref(self):
239250 root = root ,
240251 storage_options = storage_options )
241252
242- data_store .write_data (original_dataset , data_id = data_id )
243- opened_dataset = data_store .open_data (data_id )
244- self .assertEqual ({'A' , 'B' , 'x ' , 'y' , 'time' },
245- set (opened_dataset .variables ))
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 ))
246257
247258 with self .assertRaises (ValueError ) as cm :
248- GridMapping .from_dataset (opened_dataset )
259+ GridMapping .from_dataset (cube )
249260 self .assertEqual (
250261 ('cannot find any grid mapping in dataset' ,),
251262 cm .exception .args
@@ -254,21 +265,32 @@ def test_add_spatial_ref(self):
254265 path = f"{ root } /{ data_id } "
255266 group_store = fs .get_mapper (path , create = True )
256267
257- add_spatial_ref (group_store ,
258- crs ,
259- crs_var_name = crs_var_name )
260-
261- self .assertTrue (fs .exists (f"{ path } /{ crs_var_name } " ))
262- self .assertTrue (fs .exists (f"{ path } /{ crs_var_name } /.zarray" ))
263- self .assertTrue (fs .exists (f"{ path } /{ crs_var_name } /.zattrs" ))
264-
265- opened_dataset = data_store .open_data (data_id )
266- self .assertEqual ({'A' , 'B' , 'x' , 'y' , 'time' , 'spatial_ref' },
267- set (opened_dataset .variables ))
268- self .assertEqual (crs_var_name ,
269- opened_dataset .A .attrs .get ('grid_mapping' ))
270- self .assertEqual (crs_var_name ,
271- opened_dataset .B .attrs .get ('grid_mapping' ))
272-
273- gm = GridMapping .from_dataset (opened_dataset )
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 )
274296 self .assertIn ("LAEA Europe" , gm .crs .srs )
0 commit comments