-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
116 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import cartopy.crs as ccrs | ||
import numpy as np | ||
import xarray as xr | ||
|
||
from ilamb3 import post | ||
|
||
|
||
def generate_test_dset(seed: int = 1, shift: bool = False): | ||
rs = np.random.RandomState(seed) | ||
lat = [-67.5, -22.5, 22.5, 67.5] | ||
lon = [-135.0 + 360 * shift, -45.0 + 360 * shift, 45.0, 135.0] | ||
ds = xr.Dataset( | ||
data_vars={ | ||
"da": xr.DataArray( | ||
rs.rand(len(lat), len(lon)) * 1e-8, | ||
coords=[lat, lon], | ||
dims=["lat", "lon"], | ||
), | ||
} | ||
) | ||
ds["da"].attrs["units"] = "kg m-2 s-1" | ||
return ds | ||
|
||
|
||
def generate_test_site_dset(seed: int = 1): | ||
rs = np.random.RandomState(seed) | ||
lat = xr.DataArray(data=[-67.5, -22.5, 22.5, 67.5], dims=["site"]) | ||
lon = xr.DataArray(data=[-135.0, -45.0, 45.0, 135.0], dims=["site"]) | ||
ds = xr.Dataset( | ||
data_vars={ | ||
"da": xr.DataArray( | ||
rs.rand(lat.size) * 1e-8, | ||
dims=["time", "site"], | ||
), | ||
} | ||
) | ||
ds["da"] = ds["da"].assign_coords({"lat": lat, "lon": lon}) | ||
ds["da"].attrs["units"] = "kg m-2 s-1" | ||
ds["da"].attrs["coordinates"] = "lat lon" | ||
return ds | ||
|
||
|
||
def test_get_plot_limits(): | ||
limits = post.get_plot_limits( | ||
generate_test_dset(seed=1).rename_vars({"da": "a"}), | ||
{ | ||
"m": generate_test_dset(seed=1).rename_vars({"da": "b"}), | ||
"n": generate_test_dset(seed=1).rename_vars({"da": "c"}), | ||
}, | ||
) | ||
assert not (set(limits) - set(["a", "b", "c"])) | ||
|
||
|
||
def test_extents(): | ||
ds = generate_test_dset() | ||
ext = post._plot_extents(ds["da"]) | ||
assert np.allclose(ext, [-135.0, 135.0, -67.5, 67.5]) | ||
|
||
|
||
def test_proj(): | ||
ds = generate_test_dset() | ||
proj = post._plot_projection(post._plot_extents(ds["da"])) | ||
assert isinstance(proj[0], ccrs.PlateCarree) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import numpy as np | ||
import xarray as xr | ||
|
||
from ilamb3.analysis.runoff_sensitivity import compute_runoff_sensitivity | ||
from ilamb3.regions import Regions | ||
from ilamb3.tests.test_compare import generate_test_dset | ||
|
||
|
||
def test_runoff_sensitivity(): | ||
ilamb_regions = Regions() | ||
ilamb_regions.add_latlon_bounds("test", "test", [-80, 80], [-170, 170]) | ||
ds = xr.Dataset( | ||
{ | ||
"pr": 1e3 * generate_test_dset(seed=1, ntime=240, nlat=10, nlon=20)["da"], | ||
"tas": 273 | ||
+ 1e9 * generate_test_dset(seed=2, ntime=240, nlat=10, nlon=20)["da"], | ||
"mrro": generate_test_dset(seed=3, ntime=240, nlat=10, nlon=20)["da"], | ||
} | ||
) | ||
ds["mrro"] = ( | ||
ds["mrro"].mean() | ||
+ 1e2 * (ds["pr"] - ds["pr"].mean()) | ||
+ 1e-3 * (ds["tas"] - ds["tas"].mean()) | ||
) | ||
df = compute_runoff_sensitivity(ds, ["test"]) | ||
assert np.allclose(df.loc["test"]["tas Sensitivity"], 28006.878361) | ||
assert np.allclose(df.loc["test"]["pr Sensitivity"], 139.850275) |