def test_mask_wrap(method): # create a test case where the outlines and the lon coordinates # are different # outline 0..359.9 outl1 = ((359, 0), (359, 1), (0, 1.0), (0, 0)) outl2 = ((359, 1), (359, 2), (0, 2.0), (0, 1)) outlines = [outl1, outl2] r = Regions(outlines) # lon -180..179.9 lon = [-1.5, -0.5] lat = [0.5, 1.5] result = r.mask(lon, lat, method=method, wrap_lon=False).values assert np.all(np.isnan(result)) # this is the wrong wrapping result = r.mask(lon, lat, method=method, wrap_lon=180).values assert np.all(np.isnan(result)) expected = expected_mask_2D() # determine the wrap automatically result = r.mask(lon, lat, method=method, wrap_lon=True).values assert np.allclose(result, expected, equal_nan=True) # determine the wrap by hand result = r.mask(lon, lat, method=method, wrap_lon=360).values assert np.allclose(result, expected, equal_nan=True)
def test_mask_autowrap(method): expected = expected_mask_2D() # create a test case where the outlines and the lon coordinates # are different - or the same - should work either way # 1. -180..180 regions and -180..180 lon lon = [0.5, 1.5] lat = [0.5, 1.5] result = dummy_region.mask(lon, lat, method=method).values assert np.allclose(result, expected, equal_nan=True) # 2. -180..180 regions and 0..360 lon # outline -180..180 outl1 = ((-180, 0), (-180, 1), (-1, 1.0), (-1, 0)) outl2 = ((-180, 1), (-180, 2), (-1, 2.0), (-1, 1)) outlines = [outl1, outl2] r = Regions(outlines) # lon -180..179.9 lon = [358.5, 359.5] lat = [0.5, 1.5] result = r.mask(lon, lat, method=method).values assert np.allclose(result, expected, equal_nan=True) # 3. 0..360 regions and -180..180 lon # outline 0..359.9 outl1 = ((359, 0), (359, 1), (0, 1.0), (0, 0)) outl2 = ((359, 1), (359, 2), (0, 2.0), (0, 1)) outlines = [outl1, outl2] r = Regions(outlines) # lon -180..179.9 lon = [-1.5, -0.5] lat = [0.5, 1.5] result = r.mask(lon, lat, method=method).values assert np.allclose(result, expected, equal_nan=True) # 3. 0..360 regions and 0..360 lon # lon 0..359.9 lon = [0.5, 359.5] lat = [0.5, 1.5] result = r.mask(lon, lat, method=method).values assert np.allclose(result, expected, equal_nan=True)
def test_mask_poly_z_value(method): outl1 = Polygon(((0, 0, 1), (0, 1, 1), (1, 1.0, 1), (1, 0, 1))) outl2 = Polygon(((0, 1, 1), (0, 2, 1), (1, 2.0, 1), (1, 1, 1))) outlines = [outl1, outl2] r_z = Regions(outlines) expected = expected_mask_2D() result = r_z.mask(dummy_lon, dummy_lat, method=method) assert isinstance(result, xr.DataArray) assert np.allclose(result, expected, equal_nan=True) assert np.all(np.equal(result.lat.values, dummy_lat)) assert np.all(np.equal(result.lon.values, dummy_lon))
def test_mask_poly_z_value(method): if method == "legacy": pytest.xfail("legacy does not support z-coordinates") outl1 = Polygon(((0, 0, 1), (0, 1, 1), (1, 1.0, 1), (1, 0, 1))) outl2 = Polygon(((0, 1, 1), (0, 2, 1), (1, 2.0, 1), (1, 1, 1))) outlines = [outl1, outl2] r_z = Regions(outlines) expected = expected_mask() result = r_z.mask(lon, lat, method=method, xarray=True) assert isinstance(result, xr.DataArray) assert np.allclose(result, expected, equal_nan=True) assert np.all(np.equal(result.lat.values, lat)) assert np.all(np.equal(result.lon.values, lon))