55import xarray as xr
66from numpy .testing import assert_allclose
77
8- from parcels import xgcm
98from parcels ._datasets .structured .generic import X , Y , Z , datasets
10- from parcels .xgrid import XGrid , _search_1d_array
9+ from parcels .xgrid import XGrid , _drop_field_data , _search_1d_array
1110
12- GridTestCase = namedtuple ("GridTestCase" , ["Grid " , "attr" , "expected" ])
11+ GridTestCase = namedtuple ("GridTestCase" , ["ds " , "attr" , "expected" ])
1312
1413test_cases = [
1514 GridTestCase (datasets ["ds_2d_left" ], "lon" , datasets ["ds_2d_left" ].XG .values ),
@@ -34,25 +33,25 @@ def assert_equal(actual, expected):
3433
3534@pytest .mark .parametrize ("ds, attr, expected" , test_cases )
3635def test_xgrid_properties_ground_truth (ds , attr , expected ):
37- grid = XGrid ( xgcm . Grid (ds , periodic = False ) )
36+ grid = XGrid . from_dataset (ds )
3837 actual = getattr (grid , attr )
3938 assert_equal (actual , expected )
4039
4140
4241@pytest .mark .parametrize ("ds" , [pytest .param (ds , id = key ) for key , ds in datasets .items ()])
43- def test_xgrid_init_on_generic_datasets (ds ):
44- XGrid ( xgcm . Grid (ds , periodic = False ) )
42+ def test_xgrid_from_dataset_on_generic_datasets (ds ):
43+ XGrid . from_dataset (ds )
4544
4645
4746@pytest .mark .parametrize ("ds" , [datasets ["ds_2d_left" ]])
4847def test_xgrid_axes (ds ):
49- grid = XGrid ( xgcm . Grid (ds , periodic = False ) )
48+ grid = XGrid . from_dataset (ds )
5049 assert grid .axes == ["Z" , "Y" , "X" ]
5150
5251
5352@pytest .mark .parametrize ("ds" , [datasets ["ds_2d_left" ]])
5453def test_xgrid_get_axis_dim (ds ):
55- grid = XGrid ( xgcm . Grid (ds , periodic = False ) )
54+ grid = XGrid . from_dataset (ds )
5655 assert grid .get_axis_dim ("Z" ) == Z - 1
5756 assert grid .get_axis_dim ("Y" ) == Y - 1
5857 assert grid .get_axis_dim ("X" ) == X - 1
@@ -72,15 +71,15 @@ def test_invalid_lon_lat():
7271 ValueError ,
7372 match = ".*is defined on the center of the grid, but must be defined on the F points\." ,
7473 ):
75- XGrid ( xgcm . Grid (ds , periodic = False ) )
74+ XGrid . from_dataset (ds )
7675
7776 ds = datasets ["ds_2d_left" ].copy ()
7877 ds ["lon" ], _ = xr .broadcast (ds ["YG" ], ds ["XG" ])
7978 with pytest .raises (
8079 ValueError ,
8180 match = ".*have different dimensionalities\." ,
8281 ):
83- XGrid ( xgcm . Grid (ds , periodic = False ) )
82+ XGrid . from_dataset (ds )
8483
8584 ds = datasets ["ds_2d_left" ].copy ()
8685 ds ["lon" ], ds ["lat" ] = xr .broadcast (ds ["YG" ], ds ["XG" ])
@@ -90,7 +89,7 @@ def test_invalid_lon_lat():
9089 ValueError ,
9190 match = ".*must be defined on the X and Y axes and transposed to have dimensions in order of Y, X\." ,
9291 ):
93- XGrid ( xgcm . Grid (ds , periodic = False ) )
92+ XGrid . from_dataset (ds )
9493
9594
9695@pytest .mark .parametrize (
@@ -101,7 +100,7 @@ def test_invalid_lon_lat():
101100 ],
102101) # for key, ds in datasets.items()])
103102def test_xgrid_search_cpoints (ds ):
104- grid = XGrid ( xgcm . Grid (ds , periodic = False ) )
103+ grid = XGrid . from_dataset (ds )
105104 lat_array , lon_array = get_2d_fpoint_mesh (grid )
106105 lat_array , lon_array = corner_to_cell_center_points (lat_array , lon_array )
107106
@@ -148,10 +147,10 @@ def test_search_1d_array(array, x, expected_xi, expected_xsi):
148147
149148
150149@pytest .mark .parametrize (
151- "grid , da_name, expected" ,
150+ "ds , da_name, expected" ,
152151 [
153152 pytest .param (
154- XGrid ( xgcm . Grid ( datasets ["ds_2d_left" ], periodic = False )) ,
153+ datasets ["ds_2d_left" ],
155154 "U (C grid)" ,
156155 {
157156 "XG" : (np .int64 (0 ), np .float64 (0.0 )),
@@ -161,7 +160,7 @@ def test_search_1d_array(array, x, expected_xi, expected_xsi):
161160 id = "MITgcm indexing style U (C grid)" ,
162161 ),
163162 pytest .param (
164- XGrid ( xgcm . Grid ( datasets ["ds_2d_left" ], periodic = False )) ,
163+ datasets ["ds_2d_left" ],
165164 "V (C grid)" ,
166165 {
167166 "XC" : (np .int64 (- 1 ), np .float64 (0.5 )),
@@ -171,7 +170,7 @@ def test_search_1d_array(array, x, expected_xi, expected_xsi):
171170 id = "MITgcm indexing style V (C grid)" ,
172171 ),
173172 pytest .param (
174- XGrid ( xgcm . Grid ( datasets ["ds_2d_right" ], periodic = False )) ,
173+ datasets ["ds_2d_right" ],
175174 "U (C grid)" ,
176175 {
177176 "XG" : (np .int64 (0 ), np .float64 (0.0 )),
@@ -181,7 +180,7 @@ def test_search_1d_array(array, x, expected_xi, expected_xsi):
181180 id = "NEMO indexing style U (C grid)" ,
182181 ),
183182 pytest .param (
184- XGrid ( xgcm . Grid ( datasets ["ds_2d_right" ], periodic = False )) ,
183+ datasets ["ds_2d_right" ],
185184 "V (C grid)" ,
186185 {
187186 "XC" : (np .int64 (0 ), np .float64 (0.5 )),
@@ -192,10 +191,11 @@ def test_search_1d_array(array, x, expected_xi, expected_xsi):
192191 ),
193192 ],
194193)
195- def test_xgrid_localize_zero_position (grid , da_name , expected ):
194+ def test_xgrid_localize_zero_position (ds , da_name , expected ):
196195 """Test localize function using left and right datasets."""
196+ grid = XGrid .from_dataset (ds )
197+ da = ds [da_name ]
197198 position = grid .search (0 , 0 , 0 )
198- da = grid .xgcm_grid ._ds [da_name ]
199199
200200 local_position = grid .localize (position , da .dims )
201201 assert local_position == expected , f"Expected { expected } , got { local_position } "
0 commit comments