-
Notifications
You must be signed in to change notification settings - Fork 0
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 #15 from NHFLO/bodemlagen_pwn_2024
Refactor missing botms handling and layer thickness validation into a…
- Loading branch information
Showing
3 changed files
with
51 additions
and
43 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import logging | ||
|
||
import numpy as np | ||
import xarray as xr | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
def fix_missings_botms_and_min_layer_thickness(ds): | ||
""" | ||
Fix missing botms and ensure all layers have a positive thickness. | ||
Parameters | ||
---------- | ||
ds : xarray Dataset | ||
Dataset containing the layer model with top and botm. | ||
Raises | ||
------ | ||
ValueError | ||
If top contains nan values. | ||
""" | ||
if ds["top"].isnull().any(): | ||
msg = "Top should not contain nan values" | ||
raise ValueError(msg) | ||
|
||
out = xr.concat((ds["top"].expand_dims(dim={"layer": ["mv"]}, axis=0), ds["botm"]), dim="layer") | ||
|
||
# Use ffill here to fill the nan's with the previous layer. Layer thickness is zero for non existing layers | ||
out = out.ffill(dim="layer") | ||
out.values = np.minimum.accumulate(out.values, axis=out.dims.index("layer")) | ||
topbotm_fixed = out.isel(layer=slice(1, None)).transpose("layer", "icell2d") | ||
ds["botm"].values = topbotm_fixed.values | ||
|
||
# inform | ||
ncell, nisnull = ds["botm"].size, ds["botm"].isnull().sum() | ||
nfixed = (~np.isclose(ds.botm, topbotm_fixed)).sum() | ||
|
||
logger.info( | ||
"Fixed %.1f%% missing botms using downward fill. Shifted %.1f%% botms to ensure all layers have a positive thickness, assuming more info is in the upper layer.", | ||
nisnull / ncell * 100.0, | ||
(nfixed - nisnull) / ncell * 100.0 | ||
) |
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