Coverage for C:\src\imod-python\imod\select\layers.py: 92%

1 def upper_active_layer(da, is_ibound=True, include_constant_head=False):

2 """

3 Function to get the upper active layer from ibound xarray.DataArray

4

5 Parameters

6 ----------

7 da : xarray.DataArray

8 A 3D DataArray

9 is_ibound: bool, optional

10 If True, ``da`` is interpreted as ibound, with values 0: inactive, 1: active, -1 constant head.

11 If False, ``upper_active_layer`` is interpreted as first layer that has data.

12 Default is True.

13 include_constant_head : bool, optional

14 If True and ``is_ibound``, also include constant head cells.

15 Default is False.

16

17 Returns

18 -------

19 2d xr.DataArray of layernumber of upper active model layer

20 """

21 da = da.load()

22 if is_ibound:

23 # check if indeed ibound: convertible to int

24 if not da.astype(int).equals(da):

25 raise ValueError(

26 "Passed DataArray is no ibound, while is_bound was set to True"

27 )

28 # include constant head cells (?)

29 if include_constant_head:

30 is_active = da.fillna(0) != 0 # must be filled for argmax

31 else:

32 is_active = da.fillna(0) > 0

33 else:

34 is_active = ~da.isnull()

35

36 # get layer of upper active cell

37 da = is_active.layer.isel(layer=is_active.argmax(dim="layer"))

38 da = da.drop_vars("layer")

39

40 # skip where no active cells

41 return da.where(is_active.sum(dim="layer") > 0)