Exemple #1
0
    def test_no_wraps(self, tmp_netcdf_filename, vectorize):
        ds = 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)
Exemple #2
0
    def test_wraps(self, tmp_netcdf_filename, vectorize):
        ds = 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)
Exemple #3
0
    def _subset(resource):
        nonlocal count

        # if not subsetting by time, it's not necessary to decode times
        time_subset = start_date is not None or end_date is not None
        dataset = try_opendap(resource, decode_times=time_subset)

        with lock:
            count += 1
            write_log(
                process,
                f"Subsetting file {count} of {n_files} ({getattr(resource, resource.prop)})",
                subtask_percentage=(count - 1) * 100 // n_files,
            )

        dataset = dataset[variables] if variables else dataset

        subsetted = subset_shape(
            dataset,
            shape=shp,
            start_date=start_date,
            end_date=end_date,
        )

        if not all(subsetted.dims.values()):
            LOGGER.warning(f"Subset is empty for dataset: {resource.url}")
            return

        p = make_subset_file_name(resource)
        output_filename = Path(process.workdir) / p

        dataset_to_netcdf(subsetted, output_filename)

        output_files.append(output_filename)
Exemple #4
0
    def test_rotated_pole_with_time(self):
        ds = 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 [str(q.message) for q in record])
Exemple #5
0
    def test_small_poly_buffer(self, tmp_netcdf_filename):
        ds = 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))
Exemple #6
0
    def test_all_neglons(self):
        ds = 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])
Exemple #7
0
 def test_subset_multiregions(self):
     ds = 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