def test_no_wraps(self, tmp_netcdf_filename, vectorize): ds = xr.open_dataset(self.nc_file) with pytest.warns(None) as record: sub = subset.subset_shape(ds, self.poslons_geojson, vectorize=vectorize) self.compare_vals(ds, sub, "tas") # No time subsetting should occur. assert len(sub.tas) == 12 # Average temperature at surface for region in January (time=0) np.testing.assert_array_almost_equal( float(np.mean(sub.tas.isel(time=0))), 276.732483 ) # Check that no warnings are raised for meridian crossing assert ( '"Geometry crosses the Greenwich Meridian. Proceeding to split polygon at Greenwich."' '" This feature is experimental. Output might not be accurate."' not in [str(q.message) for q in record] ) assert sub.crs.prime_meridian_name == "Greenwich" assert sub.crs.grid_mapping_name == "latitude_longitude" sub.to_netcdf(tmp_netcdf_filename) assert tmp_netcdf_filename.exists() with xr.open_dataset(filename_or_obj=tmp_netcdf_filename) as f: assert {"tas", "crs"}.issubset(set(f.data_vars)) subset.subset_shape(ds, self.poslons_geojson, vectorize=vectorize)
def test_wraps(self, tmp_netcdf_filename, vectorize): ds = xr.open_dataset(self.nc_file) # Polygon crosses meridian, a warning should be raised with pytest.warns(UserWarning): sub = subset.subset_shape(ds, self.meridian_geojson) # No time subsetting should occur. assert len(sub.tas) == 12 # Average temperature at surface for region in January (time=0) np.testing.assert_array_almost_equal( float(np.mean(sub.tas.isel(time=0))), 285.064453 ) self.compare_vals(ds, sub, "tas") poly = gpd.read_file(self.meridian_multi_geojson) subtas = subset.subset_shape(ds.tas, poly, vectorize=vectorize) np.testing.assert_array_almost_equal( float(np.mean(subtas.isel(time=0))), 281.091553 ) assert sub.crs.prime_meridian_name == "Greenwich" assert sub.crs.grid_mapping_name == "latitude_longitude" sub.to_netcdf(tmp_netcdf_filename) assert tmp_netcdf_filename.exists() with xr.open_dataset(filename_or_obj=tmp_netcdf_filename) as f: assert {"tas", "crs"}.issubset(set(f.data_vars)) subset.subset_shape(ds, self.meridian_multi_geojson, vectorize=vectorize)
def test_small_poly_buffer(self): ds = xr.open_dataset(self.nc_file) with pytest.raises(ValueError): subset.subset_shape(ds, self.small_geojson) with pytest.raises(ValueError): subset.subset_shape(ds, self.small_geojson, buffer=0.6) sub = subset.subset_shape(ds, self.small_geojson, buffer=5) self.compare_vals(ds, sub, "tas") assert len(sub.lon.values) == 3 assert len(sub.lat.values) == 3
def test_rotated_pole_with_time(self): ds = xr.open_dataset(self.lons_2d_nc_file) with pytest.warns(None) as record: sub = subset.subset_shape( ds, self.eastern_canada_geojson, start_date="1984-06-01", end_date="1984-06-15", ) self.compare_vals(ds.sel(time=slice("1984-06-01", "1984-06-15")), sub, "tasmax", flag_2d=True) # Should only have 15 days of data. assert len(sub.tasmax) == 15 # Average max temperature at surface for region on June 1st, 1984 (time=0) np.testing.assert_allclose(float(np.mean(sub.tasmax.isel(time=0))), 289.634968) # Check that no warnings are raised for meridian crossing assert ( '"Geometry crosses the Greenwich Meridian. Proceeding to split polygon at Greenwich."' '" This feature is experimental. Output might not be accurate."' not in [q.message for q in record])
def test_wraps(self): ds = xr.open_dataset(self.nc_file) # Polygon crosses meridian, a warning should be raised with pytest.warns(UserWarning): sub = subset.subset_shape(ds, self.meridian_geojson) # No time subsetting should occur. assert len(sub.tas) == 12 # Average temperature at surface for region in January (time=0) np.testing.assert_array_almost_equal( float(np.mean(sub.tas.isel(time=0))), 285.064453) self.compare_vals(ds, sub, "tas") poly = gpd.read_file(self.meridian_multi_geojson) subtas = subset.subset_shape(ds.tas, poly) np.testing.assert_array_almost_equal( float(np.mean(subtas.isel(time=0))), 281.091553)
def test_small_poly_buffer(self, tmp_netcdf_filename): ds = xr.open_dataset(self.nc_file) with pytest.raises(ValueError): subset.subset_shape(ds, self.small_geojson) with pytest.raises(ValueError): subset.subset_shape(ds, self.small_geojson, buffer=0.6) sub = subset.subset_shape(ds, self.small_geojson, buffer=5) self.compare_vals(ds, sub, "tas") assert len(sub.lon.values) == 3 assert len(sub.lat.values) == 3 assert sub.crs.prime_meridian_name == "Greenwich" assert sub.crs.grid_mapping_name == "latitude_longitude" sub.to_netcdf(tmp_netcdf_filename) assert tmp_netcdf_filename.exists() with xr.open_dataset(filename_or_obj=tmp_netcdf_filename) as f: assert {"tas", "crs"}.issubset(set(f.data_vars))
def test_all_neglons(self): ds = xr.open_dataset(self.nc_file_neglons) with pytest.warns(None) as record: sub = subset.subset_shape(ds, self.southern_qc_geojson) self.compare_vals(ds, sub, "tasmax") # Average temperature at surface for region in January (time=0) np.testing.assert_array_almost_equal( float(np.mean(sub.tasmax.isel(time=0))), 269.2540588378906) # Check that no warnings are raised for meridian crossing assert ( '"Geometry crosses the Greenwich Meridian. Proceeding to split polygon at Greenwich."' '" This feature is experimental. Output might not be accurate."' not in [q.message for q in record])
def test_no_wraps(self): ds = xr.open_dataset(self.nc_file) with pytest.warns(None) as record: sub = subset.subset_shape(ds, self.poslons_geojson) self.compare_vals(ds, sub, "tas") # No time subsetting should occur. assert len(sub.tas) == 12 # Average temperature at surface for region in January (time=0) np.testing.assert_array_almost_equal( float(np.mean(sub.tas.isel(time=0))), 276.732483) # Check that no warnings are raised for meridian crossing assert ( '"Geometry crosses the Greenwich Meridian. Proceeding to split polygon at Greenwich."' '" This feature is experimental. Output might not be accurate."' not in [q.message for q in record])
def test_subset_multiregions(self): ds = xr.open_dataset(self.nc_file) regions = gpd.read_file(self.multi_regions_geojson) regions.set_index("id") ds_sub = subset.subset_shape(ds, shape=regions) assert ds_sub.notnull().sum() == 58 + 250 + 22