66import pytest
77import xarray as xr
88
9- from tests .conftest import EXAMPLE_DATA
10- from xarray_subset_grid import utils as xsg_utils
11- from xarray_subset_grid .utils import (
12- asdatetime ,
13- compute_2d_subset_mask ,
14- format_bytes ,
15- normalize_bbox_x_coords ,
16- normalize_polygon_x_coords ,
17- ray_tracing_numpy ,
18- )
9+ import xarray_subset_grid .utils as xsg_utils
1910
2011# normalize_polygon_x_coords tests.
2112
@@ -77,12 +68,7 @@ def get_test_file_dir():
7768)
7869def test_normalize_x_coords (lons , poly , norm_poly ):
7970 lons = np .array (lons )
80- normalized_polygon = normalize_polygon_x_coords (lons , np .array (poly ))
81- print (f"{ lons = } " )
82- print (f"{ poly = } " )
83- print (f"{ norm_poly = } " )
84- print (f"{ normalized_polygon = } " )
85-
71+ normalized_polygon = xsg_utils .normalize_polygon_x_coords (lons , np .array (poly ))
8672 assert np .allclose (normalized_polygon , norm_poly )
8773
8874
@@ -105,7 +91,7 @@ def test_normalize_x_coords(lons, poly, norm_poly):
10591)
10692def test_normalize_x_coords_bbox (lons , bbox , norm_bbox ):
10793 lons = np .array (lons )
108- normalized_polygon = normalize_bbox_x_coords (lons , bbox )
94+ normalized_polygon = xsg_utils . normalize_bbox_x_coords (lons , bbox )
10995 assert np .allclose (normalized_polygon , norm_bbox )
11096
11197
@@ -130,7 +116,7 @@ def test_ray_tracing_numpy():
130116 ]
131117 )
132118
133- result = ray_tracing_numpy (points [:, 0 ], points [:, 1 ], poly )
119+ result = xsg_utils . ray_tracing_numpy (points [:, 0 ], points [:, 1 ], poly )
134120
135121 assert np .array_equal (result , [False , True , False ])
136122
@@ -144,25 +130,25 @@ def test_ray_tracing_numpy():
144130 ],
145131)
146132def test_format_bytes (num , unit ):
147- assert unit in format_bytes (num )
133+ assert unit in xsg_utils . format_bytes (num )
148134
149135
150136def test_asdatetime_none ():
151- assert asdatetime (None ) is None
137+ assert xsg_utils . asdatetime (None ) is None
152138
153139
154140def test_asdatetime_datetime_passthrough ():
155141 dt = datetime (2020 , 6 , 15 , 12 , 30 , 0 )
156- assert asdatetime (dt ) is dt
142+ assert xsg_utils . asdatetime (dt ) is dt
157143
158144
159145def test_asdatetime_cftime_passthrough ():
160146 dt = cftime .datetime (2020 , 6 , 15 , 12 )
161- assert asdatetime (dt ) is dt
147+ assert xsg_utils . asdatetime (dt ) is dt
162148
163149
164150def test_asdatetime_parse_string ():
165- dt = asdatetime ("2020-06-15T12:30:00" )
151+ dt = xsg_utils . asdatetime ("2020-06-15T12:30:00" )
166152 assert dt .year == 2020 and dt .month == 6 and dt .day == 15
167153
168154
@@ -174,31 +160,38 @@ def test_compute_2d_subset_mask_all_inside():
174160 lat_da = xr .DataArray (lat2d , dims = ("y" , "x" ))
175161 lon_da = xr .DataArray (lon2d , dims = ("y" , "x" ))
176162 poly = np .array ([(- 75.0 , 39.0 ), (- 69.0 , 39.0 ), (- 69.0 , 45.0 ), (- 75.0 , 45.0 )])
177- mask = compute_2d_subset_mask (lat_da , lon_da , poly )
163+ mask = xsg_utils . compute_2d_subset_mask (lat_da , lon_da , poly )
178164 assert mask .dims == ("y" , "x" )
179- assert bool ( mask .all () )
165+ assert mask .all ()
180166
181167
182168def test_compute_2d_subset_mask_partial ():
183- ny , nx = 7 , 7
184- lat = np .linspace (40.0 , 46.0 , ny )
185- lon = np .linspace (- 74.0 , - 68.0 , nx )
169+ # Include explicit lon/lat nodes inside the polygon so the mask can be checked at a
170+ # non-boundary grid point (ray-casting is ambiguous on polygon edges).
171+ lat = np .array ([40.0 , 40.5 , 41.0 , 43.0 , 46.0 ])
172+ lon = np .array ([- 74.5 , - 73.75 , - 73.0 , - 71.0 , - 68.0 ])
186173 lat2d , lon2d = np .meshgrid (lat , lon , indexing = "ij" )
187174 lat_da = xr .DataArray (lat2d , dims = ("y" , "x" ))
188175 lon_da = xr .DataArray (lon2d , dims = ("y" , "x" ))
189- # Small polygon over the south-west corner only
190176 poly = np .array ([(- 74.5 , 40.0 ), (- 73.0 , 40.0 ), (- 73.0 , 41.0 ), (- 74.5 , 41.0 )])
191- mask = compute_2d_subset_mask (lat_da , lon_da , poly )
177+ mask = xsg_utils . compute_2d_subset_mask (lat_da , lon_da , poly )
192178 assert mask .dims == ("y" , "x" )
193- assert bool (mask .any ())
194- assert not bool (mask .all ())
195-
196-
197- def test_assign_ugrid_topology_utils_deprecation_wrapper ():
198- nc = EXAMPLE_DATA / "SFBOFS_subset1.nc"
199- if not nc .is_file ():
200- pytest .skip ("example NetCDF not present" )
201- ds = xr .open_dataset (nc )
202- with pytest .warns (DeprecationWarning , match = "assign_ugrid_topology" ):
203- ds2 = xsg_utils .assign_ugrid_topology (ds , face_node_connectivity = "nv" )
204- assert "mesh" in ds2 .variables
179+ assert mask .any ()
180+ assert not mask .all ()
181+ i_inside = int (np .where (lat == 40.5 )[0 ][0 ])
182+ j_inside = int (np .where (lon == - 73.75 )[0 ][0 ])
183+ assert mask .values [i_inside , j_inside ]
184+ assert not mask .values [- 1 , - 1 ]
185+
186+
187+ def test_compute_2d_subset_mask_list_polygon_coerced ():
188+ """list/tuple polygon vertices are accepted (coerced via normalize_polygon_x_coords)."""
189+ ny , nx = 5 , 5
190+ lat = np .linspace (40.0 , 44.0 , ny )
191+ lon = np .linspace (- 74.0 , - 70.0 , nx )
192+ lat2d , lon2d = np .meshgrid (lat , lon , indexing = "ij" )
193+ lat_da = xr .DataArray (lat2d , dims = ("y" , "x" ))
194+ lon_da = xr .DataArray (lon2d , dims = ("y" , "x" ))
195+ poly = [(- 75.0 , 39.0 ), (- 69.0 , 39.0 ), (- 69.0 , 45.0 ), (- 75.0 , 45.0 )]
196+ mask = xsg_utils .compute_2d_subset_mask (lat_da , lon_da , poly )
197+ assert mask .all ()
0 commit comments