Example #1
0
def test_mae():
    sim = (
        open_dataset("sdba/CanESM2_1950-2100.nc").sel(time=slice("1950", "1953")).tasmax
    )
    ref = open_dataset("sdba/nrcan_1950-2013.nc").sel(time=slice("1950", "1953")).tasmax
    test = sdba.measures.mae(sim, ref).values
    np.testing.assert_array_almost_equal(test, [4.159672, 14.2148, 9.768536], 4)
Example #2
0
    def test_3d_data_with_nans(self):
        # test with 3d data
        pr = open_dataset(self.nc_file).pr
        prMM = open_dataset(self.nc_file).pr
        prMM.values *= 86400.0
        prMM.attrs["units"] = "mm/day"
        # put a nan somewhere
        prMM.values[10, 1, 0] = np.nan
        pr.values[10, 1, 0] = np.nan
        pr_min = "5 mm/d"
        out1 = atmos.wetdays(pr, thresh=pr_min, freq="MS")
        out2 = atmos.wetdays(prMM, thresh=pr_min, freq="MS")

        # test kg m-2 s-1
        pr.attrs["units"] = "kg m-2 s-1"
        out3 = atmos.wetdays(pr, thresh=pr_min, freq="MS")

        np.testing.assert_array_equal(out1, out2)
        np.testing.assert_array_equal(out1, out3)

        # check some vector with and without a nan
        x1 = prMM[:31, 0, 0].values

        wd1 = (x1 >= int(pr_min.split(" ")[0])).sum()

        assert wd1 == out1.values[0, 0, 0]

        assert np.isnan(out1.values[0, 1, 0])

        # make sure that vector with all nans gives nans whatever skipna
        assert np.isnan(out1.values[0, -1, -1])
Example #3
0
    def test_DTR_3d_data_with_nans(self):
        tasmax = open_dataset(self.nc_tasmax).tasmax
        tasmax_C = open_dataset(self.nc_tasmax).tasmax
        tasmax_C -= K2C
        tasmax_C.attrs["units"] = "C"
        tasmin = open_dataset(self.nc_tasmin).tasmin
        tasmin_C = open_dataset(self.nc_tasmin).tasmin
        tasmin_C -= K2C
        tasmin_C.attrs["units"] = "C"
        # put a nan somewhere
        tasmin.values[32, 1, 0] = np.nan
        tasmin_C.values[32, 1, 0] = np.nan
        dtr = atmos.daily_temperature_range(tasmin, tasmax, freq="MS")
        dtrC = atmos.daily_temperature_range(tasmin_C, tasmax_C, freq="MS")
        min1 = tasmin.values[:, 0, 0]
        max1 = tasmax.values[:, 0, 0]

        dtr1 = max1 - min1

        np.testing.assert_array_equal(dtr, dtrC)
        assert dtr.attrs["units"] == "K"
        assert np.allclose(dtr1[0:31].mean(), dtr.values[0, 0, 0])

        assert np.isnan(dtr.values[1, 1, 0])

        assert np.isnan(dtr.values[0, -1, -1])

        dtr = atmos.max_daily_temperature_range(tasmin, tasmax, freq="MS")
        dtrC = atmos.max_daily_temperature_range(tasmin_C, tasmax_C, freq="MS")
        np.testing.assert_array_equal(dtr, dtrC)
        assert dtr.attrs["units"] == "K"
        assert np.allclose(dtr1[0:31].max(), dtr.values[0, 0, 0])
        assert np.isnan(dtr.values[1, 1, 0])
        assert np.isnan(dtr.values[0, -1, -1])
    def test_3d_data_with_nans(self):
        tasmax = open_dataset(self.nc_tasmax).tasmax
        tasmin = open_dataset(self.nc_tasmin).tasmin

        tasmaxC = open_dataset(self.nc_tasmax).tasmax
        tasminC = open_dataset(self.nc_tasmin).tasmin
        tasmaxC -= K2C
        tasmaxC.attrs["units"] = "C"
        tasminC -= K2C
        tasminC.attrs["units"] = "C"

        # put a nan somewhere
        tasmin.values[180, 1, 0] = np.nan
        tasminC.values[180, 1, 0] = np.nan

        out = atmos.tx_tn_days_above(tasmin,
                                     tasmax,
                                     thresh_tasmax="25 C",
                                     thresh_tasmin="18 C")
        outC = atmos.tx_tn_days_above(tasminC,
                                      tasmaxC,
                                      thresh_tasmax="25 C",
                                      thresh_tasmin="18 C")
        np.testing.assert_array_equal(out, outC)

        min1 = tasmin.values[:, 53, 76]
        max1 = tasmax.values[:, 53, 76]

        out1 = ((min1 > (K2C + 18)) * (max1 > (K2C + 25)) * 1.0).sum()

        assert np.allclose(out1, out.values[0, 53, 76])

        assert np.isnan(out.values[0, 1, 0])

        assert np.isnan(out.values[0, -1, -1])
Example #5
0
    def test_with_different_phases(self):
        # test with different phases
        pr = open_dataset(self.nc_pr).pr  # mm/s
        tasmin = open_dataset(self.nc_tasmin).tasmin  # K

        out_tot = atmos.precip_accumulation(pr, freq="MS")
        out_sol = atmos.solid_precip_accumulation(pr, tas=tasmin, freq="MS")
        out_liq = atmos.liquid_precip_accumulation(pr, tas=tasmin, freq="MS")

        np.testing.assert_array_almost_equal(out_liq + out_sol, out_tot, 4)

        assert "solid" in out_sol.long_name
        assert "liquid" in out_liq.long_name
        assert out_sol.standard_name == "lwe_thickness_of_snowfall_amount"

        # With a non-default threshold
        out_sol = atmos.solid_precip_accumulation(pr,
                                                  tas=tasmin,
                                                  thresh="40 degF",
                                                  freq="MS")
        out_liq = atmos.liquid_precip_accumulation(pr,
                                                   tas=tasmin,
                                                   thresh="40 degF",
                                                   freq="MS")

        np.testing.assert_array_almost_equal(out_liq + out_sol, out_tot, 4)
Example #6
0
def test_rmse():
    sim = (
        open_dataset("sdba/CanESM2_1950-2100.nc").sel(time=slice("1950", "1953")).tasmax
    )
    ref = open_dataset("sdba/nrcan_1950-2013.nc").sel(time=slice("1950", "1953")).tasmax
    test = sdba.measures.rmse(sim, ref).values
    np.testing.assert_array_almost_equal(test, [5.4499755, 18.124086, 12.387193], 4)
Example #7
0
    def test_3d_data_with_nans(self, prunits, prfac, tasunits, tasoffset, chunks):
        pr = open_dataset(self.nc_pr).pr
        pr2 = pr.copy()
        pr2.values *= prfac
        pr2.attrs["units"] = prunits

        tasmax = open_dataset(self.nc_tasmax, chunks=chunks).tasmax
        tasmin = open_dataset(self.nc_tasmin, chunks=chunks).tasmin
        tas = 0.5 * (tasmax + tasmin)
        tas.attrs = tasmax.attrs
        tas2 = tas.copy()
        if chunks:
            tas2 = tas2.chunk(chunks)
        if tasunits:
            tas2.values -= tasoffset
            tas2.attrs["units"] = tasunits

        pr2.values[10, 1, 0] = np.nan
        pr.values[10, 1, 0] = np.nan

        outref = atmos.rain_on_frozen_ground_days(pr, tas, freq="MS")
        out21 = atmos.rain_on_frozen_ground_days(pr2, tas, freq="MS")
        out22 = atmos.rain_on_frozen_ground_days(pr2, tas2, freq="MS")
        out12 = atmos.rain_on_frozen_ground_days(pr, tas2, freq="MS")

        np.testing.assert_array_equal(outref, out21)
        np.testing.assert_array_equal(outref, out22)
        np.testing.assert_array_equal(outref, out12)

        assert np.isnan(out22.values[0, 1, 0])
        assert np.isnan(out22.values[0, -1, -1])
Example #8
0
    def test_3d_data_with_nans(self):
        # test with 3d data
        pr = open_dataset(self.nc_file).pr
        prMM = open_dataset(self.nc_file).pr
        prMM.values *= 86400.0
        prMM.attrs["units"] = "mm/day"
        # put a nan somewhere
        prMM.values[10, 1, 0] = np.nan
        pr.values[10, 1, 0] = np.nan

        out1 = atmos.max_1day_precipitation_amount(pr, freq="MS")
        out2 = atmos.max_1day_precipitation_amount(prMM, freq="MS")

        # test kg m-2 s-1
        pr.attrs["units"] = "kg m-2 s-1"
        out3 = atmos.max_1day_precipitation_amount(pr, freq="MS")

        np.testing.assert_array_almost_equal(out1, out2, 3)
        np.testing.assert_array_almost_equal(out1, out3, 3)

        x1 = prMM[:31, 0, 0].values
        rx1 = x1.max()

        assert np.allclose(rx1, out1.values[0, 0, 0])
        # assert (np.allclose(di1, dis.values[0, 0, 0]))
        assert np.isnan(out1.values[0, 1, 0])
        # assert (np.allclose(di2, dis.values[0, 1, 0]))
        assert np.isnan(out1.values[0, -1, -1])
    def test_3d_data_with_nans(self):
        # test with 3d data
        tas = open_dataset(self.nc_file).tasmax
        tasC = open_dataset(self.nc_file).tasmax
        tasC -= K2C
        tasC.attrs["units"] = "C"
        # put a nan somewhere
        tas.values[180, 1, 0] = np.nan
        tasC.values[180, 1, 0] = np.nan
        # compute with both skipna options
        thresh = 273.16 + 25
        fd = atmos.tx_days_above(tas, freq="YS")
        fdC = atmos.tx_days_above(tasC, freq="YS")

        x1 = tas.values[:, 0, 0]

        fd1 = (x1[x1 > thresh]).size

        np.testing.assert_array_equal(fd, fdC)

        assert np.allclose(fd1, fd.values[0, 0, 0])

        assert np.isnan(fd.values[0, 1, 0])

        assert np.isnan(fd.values[0, -1, -1])
Example #10
0
    def test_3d_data_with_nans(self):
        # test with 3d data
        pr = open_dataset(self.nc_file).pr
        prMM = open_dataset(self.nc_file).pr
        prMM.values *= 86400.0
        prMM.attrs["units"] = "mm/day"
        # put a nan somewhere
        prMM.values[10, 1, 0] = np.nan
        pr.values[10, 1, 0] = np.nan
        pr_min = "5 mm/d"
        out1 = atmos.maximum_consecutive_dry_days(pr, thresh=pr_min, freq="MS")
        out2 = atmos.maximum_consecutive_dry_days(prMM,
                                                  thresh=pr_min,
                                                  freq="MS")

        # test kg m-2 s-1
        pr.attrs["units"] = "kg m-2 s-1"
        out3 = atmos.maximum_consecutive_dry_days(pr, thresh=pr_min, freq="MS")

        np.testing.assert_array_equal(out1, out2)
        np.testing.assert_array_equal(out1, out3)

        # check some vector with and without a nan
        x1 = prMM[:31, 0, 0] * 0.0 + 50.0
        x1[5:10] = 0
        x1.attrs["units"] = "mm/day"
        cdd1 = atmos.maximum_consecutive_dry_days(x1, freq="MS")

        assert cdd1 == 5

        assert np.isnan(out1.values[0, 1, 0])

        # make sure that vector with all nans gives nans whatever skipna
        assert np.isnan(out1.values[0, -1, -1])
Example #11
0
    def test_3d_data_with_nans(self):
        # test with 3d data
        tasmin = open_dataset(self.nc_file).tasmin
        tasminC = open_dataset(self.nc_file).tasmin
        tasminC -= K2C
        tasminC.attrs["units"] = "C"
        # put a nan somewhere
        tasmin.values[180, 1, 0] = np.nan
        tasminC.values[180, 1, 0] = np.nan
        # compute with both skipna options
        thresh = 273.16
        fd = atmos.frost_days(tasmin, freq="YS")
        fdC = atmos.frost_days(tasminC, freq="YS")
        # fds = xci.frost_days(tasmin, thresh=thresh, freq='YS', skipna=True)

        x1 = tasmin.values[:, 0, 0]

        fd1 = (x1[x1 < thresh]).size

        np.testing.assert_array_equal(fd, fdC)

        assert np.allclose(fd1, fd.values[0, 0, 0])
        # assert (np.allclose(fd1, fds.values[0, 0, 0]))
        assert np.isnan(fd.values[0, 1, 0])
        # assert (np.allclose(fd2, fds.values[0, 1, 0]))
        assert np.isnan(fd.values[0, -1, -1])
Example #12
0
    def test_convert_units(self):
        tasmax = open_dataset(self.nc_tasmax).tasmax
        tasmin = open_dataset(self.nc_tasmin).tasmin
        tasmax.values -= K2C
        tasmax.attrs["units"] = "C"
        tasmin.values -= K2C
        tasmin.attrs["units"] = "C"
        # put a nan somewhere
        tasmin.values[180, 1, 0] = np.nan

        with pytest.warns(None) as record:
            frzthw = atmos.daily_freezethaw_cycles(
                tasmin,
                tasmax,
                thresh_tasmax="0 degC",
                thresh_tasmin="0 degC",
                freq="YS",
            )

        min1 = tasmin.values[:, 0, 0]
        max1 = tasmax.values[:, 0, 0]

        frzthw1 = ((min1 < 0) * (max1 > 0) * 1.0).sum()

        assert (
            "This index calculation will soon require user-specified thresholds."
            not in [str(q.message) for q in record])

        assert np.allclose(frzthw1, frzthw.values[0, 0, 0])

        assert np.isnan(frzthw.values[0, 1, 0])

        assert np.isnan(frzthw.values[0, -1, -1])
Example #13
0
    def test_Tmean_3d_data(self):
        ds_tmax = open_dataset(self.nc_files[0])
        ds_tmin = open_dataset(self.nc_files[1])
        tas = atmos.tg(ds_tmin.tasmin, ds_tmax.tasmax)
        tas_C = atmos.tg(ds_tmin.tasmin, ds_tmax.tasmax)
        tas_C.values -= K2C
        tas_C.attrs["units"] = "C"
        # put a nan somewhere
        tas.values[180, 1, 0] = np.nan
        tas_C.values[180, 1, 0] = np.nan
        tmmean = atmos.tg_mean(tas)
        tmmeanC = atmos.tg_mean(tas_C)
        x1 = tas.values[:, 0, 0]
        tmmean1 = x1.mean()

        # TODO: Investigate the differences between the two outputs.
        # The conversion to K is done after / before the mean.
        np.testing.assert_array_almost_equal(tmmeanC, tmmean, 3)
        # test single point vs manual
        assert np.allclose(tmmean1, tmmean.values[0, 0, 0],
                           tmmeanC.values[0, 0, 0])
        # test single nan point
        assert np.isnan(tmmean.values[0, 1, 0])
        # test all nan point
        assert np.isnan(tmmean.values[0, -1, -1])
Example #14
0
    def test_dtr_var_3d_data_with_nans(self):
        tasmax = open_dataset(self.nc_tasmax).tasmax
        tasmax_C = open_dataset(self.nc_tasmax).tasmax
        tasmax_C -= K2C
        tasmax_C.attrs["units"] = "C"
        tasmin = open_dataset(self.nc_tasmin).tasmin
        tasmin_C = open_dataset(self.nc_tasmin).tasmin
        tasmin_C -= K2C
        tasmin_C.attrs["units"] = "C"
        # put a nan somewhere
        tasmin.values[32, 1, 0] = np.nan
        tasmin_C.values[32, 1, 0] = np.nan

        etr = atmos.extreme_temperature_range(tasmin, tasmax, freq="MS")
        etrC = atmos.extreme_temperature_range(tasmin_C, tasmax_C, freq="MS")
        min1 = tasmin.values[:, 0, 0]
        max1 = tasmax.values[:, 0, 0]

        np.testing.assert_array_equal(etr, etrC)

        etr1 = max1[0:31].max() - min1[0:31].min()
        assert np.allclose(etr1, etr.values[0, 0, 0], etrC.values[0, 0, 0])

        assert np.isnan(etr.values[1, 1, 0])

        assert np.isnan(etr.values[0, -1, -1])
Example #15
0
    def test_dtr_var_3d_data_with_nans(self):
        tasmax = open_dataset(self.nc_tasmax).tasmax
        tasmax_C = open_dataset(self.nc_tasmax).tasmax
        tasmax_C -= K2C
        tasmax_C.attrs["units"] = "C"
        tasmin = open_dataset(self.nc_tasmin).tasmin
        tasmin_C = open_dataset(self.nc_tasmin).tasmin
        tasmin_C -= K2C
        tasmin_C.attrs["units"] = "C"
        # put a nan somewhere
        tasmin.values[32, 1, 0] = np.nan
        tasmin_C.values[32, 1, 0] = np.nan
        dtr = atmos.daily_temperature_range_variability(tasmin,
                                                        tasmax,
                                                        freq="MS")
        dtrC = atmos.daily_temperature_range_variability(tasmin_C,
                                                         tasmax_C,
                                                         freq="MS")
        min1 = tasmin.values[:, 0, 0]
        max1 = tasmax.values[:, 0, 0]
        assert dtr.attrs["units"] == "K"
        dtr1a = max1 - min1
        dtr1 = abs(np.diff(dtr1a))
        np.testing.assert_array_equal(dtr, dtrC)

        # first month jan use 0:30 (n==30) because of day to day diff
        assert np.allclose(dtr1[0:30].mean(), dtr.values[0, 0, 0],
                           dtrC.values[0, 0, 0])

        assert np.isnan(dtr.values[1, 1, 0])

        assert np.isnan(dtr.values[0, -1, -1])
Example #16
0
 def get_snowfall(self):
     dnr = xr.merge(
         (open_dataset(self.pr_file), open_dataset(self.tasmin_file)))
     return atmos.snowfall_approximation(dnr.pr,
                                         tas=dnr.tasmin,
                                         thresh="-0.5 degC",
                                         method="binary")
Example #17
0
    def test_3d_data_with_nans(self):
        # test with 3d data
        tas = open_dataset(self.nc_file).tasmin
        tasC = open_dataset(self.nc_file).tasmin
        tasC -= K2C
        tasC.attrs["units"] = "C"
        # put a nan somewhere
        tas.values[180, 1, 0] = np.nan
        tasC.values[180, 1, 0] = np.nan
        # compute with both skipna options
        thresh = 273.16 + 20
        out = atmos.tropical_nights(tas, freq="YS")
        outC = atmos.tropical_nights(tasC, freq="YS")
        # fds = xci.frost_days(tasmin, thresh=thresh, freq='YS', skipna=True)

        x1 = tas.values[:, 0, 0]

        out1 = (x1[x1 > thresh]).size

        np.testing.assert_array_equal(out, outC)

        assert np.allclose(out1, out.values[0, 0, 0])

        assert np.isnan(out.values[0, 1, 0])

        assert np.isnan(out.values[0, -1, -1])
Example #18
0
    def test_3d_data_with_nans(self):
        # test with 3d data
        pr = open_dataset(self.nc_pr).pr  # mm/s
        prMM = open_dataset(self.nc_pr).pr
        prMM *= 86400
        prMM.attrs["units"] = "mm/day"
        # put a nan somewhere
        prMM.values[10, 1, 0] = np.nan
        pr.values[10, 1, 0] = np.nan

        out1 = atmos.precip_accumulation(pr, freq="MS")
        out2 = atmos.precip_accumulation(prMM, freq="MS")

        # test kg m-2 s-1
        pr.attrs["units"] = "kg m-2 s-1"
        out3 = atmos.precip_accumulation(pr, freq="MS")

        np.testing.assert_array_almost_equal(out1, out2, 3)
        np.testing.assert_array_almost_equal(out1, out3, 5)

        # check some vector with and without a nan
        x1 = prMM[:31, 0, 0].values

        prTot = x1.sum()

        np.testing.assert_almost_equal(prTot, out1.values[0, 0, 0], 4)

        assert np.isnan(out1.values[0, 1, 0])

        assert np.isnan(out1.values[0, -1, -1])
Example #19
0
    def test_add_dims(self, use_dask):
        if use_dask:
            chunks = {"location": -1}
        else:
            chunks = None
        ref = (open_dataset(
            "sdba/ahccd_1950-2013.nc",
            chunks=chunks,
            drop_variables=["lat",
                            "lon"]).sel(time=slice("1981", "2010")).tasmax)
        ref = convert_units_to(ref, "K")
        ref = ref.isel(location=1, drop=True).expand_dims(location=["Amos"])

        dsim = open_dataset("sdba/CanESM2_1950-2100.nc",
                            chunks=chunks,
                            drop_variables=["lat", "lon"]).tasmax
        hist = dsim.sel(time=slice("1981", "2010"))
        sim = dsim.sel(time=slice("2041", "2070"))

        # With add_dims, "does it run" test
        group = Grouper("time.dayofyear", window=5, add_dims=["location"])
        EQM = EmpiricalQuantileMapping.train(ref, hist, group=group)
        EQM.adjust(sim).load()

        # Without, sanity test.
        group = Grouper("time.dayofyear", window=5)
        EQM2 = EmpiricalQuantileMapping.train(ref, hist, group=group)
        scen2 = EQM2.adjust(sim).load()
        assert scen2.sel(location=["Kugluktuk", "Vancouver"]).isnull().all()
Example #20
0
    def test_3d_data_with_nans(self, units, factor, chunks):
        # test with 3d data
        pr1 = open_dataset(self.nc_file).pr
        pr2 = open_dataset(self.nc_file, chunks=chunks).pr
        pr2.values *= factor
        pr2.attrs["units"] = units
        # put a nan somewhere
        pr2.values[10, 1, 0] = np.nan
        pr1.values[10, 1, 0] = np.nan
        wind = 3
        out1 = atmos.max_n_day_precipitation_amount(pr1,
                                                    window=wind,
                                                    freq="MS")
        out2 = atmos.max_n_day_precipitation_amount(pr2,
                                                    window=wind,
                                                    freq="MS")

        np.testing.assert_array_almost_equal(out1, out2, 3)

        x1 = pr1[:31, 0, 0].values * 86400
        df = pd.DataFrame({"pr": x1})
        rx3 = df.rolling(wind).sum().max()

        assert np.allclose(rx3, out1.values[0, 0, 0])
        assert np.isnan(out1.values[0, 1, 0])
        assert np.isnan(out1.values[0, -1, -1])
Example #21
0
    def test_real_data(self):

        dsim = open_dataset("sdba/CanESM2_1950-2100.nc").chunk()
        dref = open_dataset("sdba/ahccd_1950-2013.nc").chunk()

        ref = convert_units_to(dref.sel(time=slice("1950", "2009")).pr, "mm/d")
        hist = convert_units_to(
            dsim.sel(time=slice("1950", "2009")).pr, "mm/d")

        quantiles = np.linspace(0.01, 0.99, num=50)

        with xr.set_options(keep_attrs=True):
            ref = ref + uniform_noise_like(ref, low=1e-6, high=1e-3)
            hist = hist + uniform_noise_like(hist, low=1e-6, high=1e-3)

        EQM = EmpiricalQuantileMapping.train(ref,
                                             hist,
                                             group=Grouper("time.dayofyear",
                                                           window=31),
                                             nquantiles=quantiles)

        scen = EQM.adjust(hist, interp="linear", extrapolation="constant")

        EX = ExtremeValues.train(ref,
                                 hist,
                                 cluster_thresh="1 mm/day",
                                 q_thresh=0.97)
        new_scen = EX.adjust(scen, hist, frac=0.000000001)
        new_scen.load()
Example #22
0
    def test_3d_data_with_nans(self):
        # test with 3d data
        pr = open_dataset(self.nc_file).pr
        prMM = open_dataset(self.nc_file).pr
        prMM.values *= 86400.0
        prMM.attrs["units"] = "mm/day"
        # put a nan somewhere
        prMM.values[10, 1, 0] = np.nan
        pr.values[10, 1, 0] = np.nan

        # compute with both skipna options
        pr_min = "2 mm/d"
        # dis = daily_pr_intensity(pr, pr_min=pr_min, freq='MS', skipna=True)

        out1 = atmos.daily_pr_intensity(pr, thresh=pr_min, freq="MS")
        out2 = atmos.daily_pr_intensity(prMM, thresh=pr_min, freq="MS")

        # test kg m-2 s-1
        pr.attrs["units"] = "kg m-2 s-1"
        out3 = atmos.daily_pr_intensity(pr, thresh=pr_min, freq="MS")

        np.testing.assert_array_almost_equal(out1, out2, 3)
        np.testing.assert_array_almost_equal(out1, out3, 3)

        x1 = prMM[:31, 0, 0].values

        di1 = x1[x1 >= int(pr_min.split(" ")[0])].mean()
        # buffer = np.ma.masked_invalid(x2)
        # di2 = buffer[buffer >= pr_min].mean()

        assert np.allclose(di1, out1.values[0, 0, 0])
        # assert (np.allclose(di1, dis.values[0, 0, 0]))
        assert np.isnan(out1.values[0, 1, 0])
        # assert (np.allclose(di2, dis.values[0, 1, 0]))
        assert np.isnan(out1.values[0, -1, -1])
Example #23
0
def test_corn_heat_units():
    tn = open_dataset("ERA5/daily_surface_cancities_1990-1993.nc").tasmin
    tx = open_dataset("ERA5/daily_surface_cancities_1990-1993.nc").tasmax

    with xr.set_options(keep_attrs=True):
        tnC = tn - K2C
        tnC.attrs["units"] = "C"

    chu = atmos.corn_heat_units(tasmin=tn,
                                tasmax=tx,
                                thresh_tasmin="4.44 degC",
                                thresh_tasmax="10 degC")
    chuC = atmos.corn_heat_units(tasmin=tnC,
                                 tasmax=tx,
                                 thresh_tasmin="4.44 degC",
                                 thresh_tasmax="10 degC")

    np.testing.assert_allclose(chu, chuC, rtol=1e-3)

    np.testing.assert_allclose(chu[0, 180:185],
                               np.array(
                                   [13.777, 12.368, 11.966, 14.674, 16.797]),
                               rtol=1e-4)

    assert ("specific thresholds : tmin > 4.44 degc and tmax > 10 degc."
            in chu.description)
Example #24
0
def test_annual_cycle_correlation():
    sim = (
        open_dataset("sdba/CanESM2_1950-2100.nc").sel(time=slice("1950", "1953")).tasmax
    )
    ref = open_dataset("sdba/nrcan_1950-2013.nc").sel(time=slice("1950", "1953")).tasmax
    test = (
        sdba.measures.annual_cycle_correlation(sim, ref, window=31)
        .sel(location="Vancouver")
        .values
    )
    np.testing.assert_array_almost_equal(test, [0.94580488], 4)
Example #25
0
def test_spatial_analogs(method):
    if method == "skezely_rizzo":
        pytest.skip("Method not implemented.")
    diss = open_dataset("SpatialAnalogs/dissimilarity")
    data = open_dataset("SpatialAnalogs/indicators")

    target = data.sel(lat=46.1875, lon=-72.1875, time=slice("1970", "1990"))
    candidates = data.sel(time=slice("1970", "1990"))

    out = xca.spatial_analogs(target, candidates, method=method)

    np.testing.assert_allclose(diss[method], out, rtol=1e-3, atol=1e-3)
Example #26
0
def test_spatial_analogs(method):
    if method in [
            "nearest_neighbor", "kldiv"
    ] and parse_version(__scipy_version__) < parse_version("1.6.0"):
        pytest.skip("Method not supported in scipy<1.6.0")

    diss = open_dataset("SpatialAnalogs/dissimilarity")
    data = open_dataset("SpatialAnalogs/indicators")

    target = data.sel(lat=46.1875, lon=-72.1875, time=slice("1970", "1990"))
    candidates = data.sel(time=slice("1970", "1990"))

    out = xca.spatial_analogs(target, candidates, method=method)
    np.testing.assert_allclose(diss[method], out, rtol=1e-3, atol=1e-3)
Example #27
0
    def test_raise(self):
        da = open_dataset(self.nc_poslons).tas
        with pytest.raises(ValueError):
            subset.subset_bbox(
                da,
                lon_bnds=self.lonGCM,
                lat_bnds=self.latGCM,
                start_date="2056",
                end_date="2055",
            )

        da = open_dataset(
            self.nc_2dlonlat).tasmax.drop_vars(names=["lon", "lat"])
        with pytest.raises(Exception):
            subset.subset_bbox(da, lon_bnds=self.lon, lat_bnds=self.lat)
Example #28
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)
Example #29
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)
Example #30
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])