-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #40 from paigem/handle-land-mask
Handle land mask
- Loading branch information
Showing
5 changed files
with
159 additions
and
87 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
from typing import Dict, Tuple | ||
|
||
import numpy as np | ||
import xarray as xr | ||
from numpy.random import default_rng | ||
|
||
|
||
def create_data( | ||
shape: Tuple[int, ...], | ||
chunks: Dict[str, int] = {}, | ||
skin_correction: bool = False, | ||
order: str = "F", | ||
use_xr=True, | ||
land_mask=False, | ||
): | ||
size = shape[0] * shape[1] | ||
shape2d = (shape[0], shape[1]) | ||
rng = default_rng() | ||
indices = rng.choice(size, size=int(size * 0.3), replace=False) | ||
multi_indices = np.unravel_index(indices, shape2d) | ||
|
||
def _arr(value, chunks, order): | ||
arr = np.full(shape, value, order=order) | ||
# adds random noise scaled by a percentage of the value | ||
randomize_factor = 0.001 | ||
randomize_range = value * randomize_factor | ||
noise = np.random.rand(*shape) * randomize_range | ||
arr = arr + noise | ||
|
||
if land_mask: | ||
arr[ | ||
multi_indices[0], multi_indices[1], : | ||
] = np.nan # add NaNs to mimic land mask | ||
if use_xr: | ||
arr = xr.DataArray(arr) | ||
if chunks: | ||
arr = arr.chunk(chunks) | ||
return arr | ||
|
||
sst = _arr(290.0, chunks, order) | ||
t_zt = _arr(280.0, chunks, order) | ||
hum_zt = _arr(0.001, chunks, order) | ||
u_zu = _arr(1.0, chunks, order) | ||
v_zu = _arr(-1.0, chunks, order) | ||
slp = _arr(101000.0, chunks, order) | ||
rad_sw = _arr(0.000001, chunks, order) | ||
rad_lw = _arr(350.0, chunks, order) | ||
if skin_correction: | ||
return sst, t_zt, hum_zt, u_zu, v_zu, rad_sw, rad_lw, slp | ||
else: | ||
return sst, t_zt, hum_zt, u_zu, v_zu, slp |
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,52 @@ | ||
import numpy as np | ||
import pytest | ||
from aerobulk.flux import noskin_np, skin_np | ||
from create_test_data import create_data | ||
|
||
"""Tests for the numpy land_mask wrapper""" | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"algo, skin_correction", | ||
[ | ||
("ecmwf", True), | ||
("ecmwf", False), | ||
("coare3p0", True), | ||
("coare3p0", False), | ||
("coare3p6", True), | ||
("coare3p6", False), | ||
("andreas", False), | ||
("ncar", False), | ||
], | ||
) | ||
def test_land_mask(skin_correction, algo): | ||
shape = (2, 3, 4) | ||
size = shape[0] * shape[1] * shape[2] | ||
|
||
if skin_correction: | ||
func = skin_np | ||
else: | ||
func = noskin_np | ||
|
||
args = create_data( | ||
shape, | ||
chunks=False, | ||
skin_correction=skin_correction, | ||
use_xr=False, | ||
land_mask=True, | ||
) | ||
out_data = func(*args, algo, 2, 10, 6) | ||
|
||
# Check the location of all NaNs is correct | ||
for o in out_data: | ||
np.testing.assert_allclose(np.isnan(args[0]), np.isnan(o)) | ||
|
||
# Check that values of the unshrunk array are correct | ||
for i in range(size): | ||
index = np.unravel_index(i, shape) | ||
if not np.isnan(out_data[0][index]): | ||
single_inputs = tuple(np.atleast_3d(i[index]) for i in args) | ||
|
||
single_outputs = func(*single_inputs, algo, 2, 10, 6) | ||
for so, o in zip(single_outputs, out_data): | ||
assert so == o[index] |
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