Coverage for C:\src\imod-python\imod\prepare\layer.py: 100%

1"""

2 This module contains all kinds of utilities to work with layers.

3"""

4

5 from imod.typing import GridDataArray

6 from imod.typing.grid import zeros_like

7

8

9 def get_upper_active_layer_number(active: GridDataArray) -> GridDataArray:

10 """

11 Returns planar grid of integers with the layer number of the upper most

12 active cell.

13

14 Parameters

15 ----------

16 active: {DataArray, UgridDataArray}

17 Grid of booleans designating active cell.

18 """

19 layer = active.coords["layer"]

20 # Set nodata to sentinel value to prevent a dtype shift from integer to

21 # float as np.nan forces float.

22 nodata = layer.max() + 1

23 return layer.where(active, nodata).min("layer")

24

25

26 def get_upper_active_grid_cells(active: GridDataArray) -> GridDataArray:

27 """

28 Returns grid of booleans designating location of the uppermost active grid

29 cell.

30

31 Parameters

32 ----------

33 active: {DataArray, UgridDataArray}

34 Grid of booleans designating active cell.

35 """

36 layer = active.coords["layer"]

37 upper_active_layer = get_upper_active_layer_number(active)

38 return layer == upper_active_layer

39

40

41 def get_lower_active_layer_number(active: GridDataArray) -> GridDataArray:

42 """

43 Returns two-dimensional grid of integers with the layer number of the lower

44 most active cell.

45

46 Parameters

47 ----------

48 active: {DataArray, UgridDataArray}

49 Grid of booleans designating active cell.

50 """

51 layer = active.coords["layer"]

52 # Set nodata to sentinel value to prevent a dtype shift from integer to

53 # float as np.nan forces float.

54 nodata = layer.min() - 1

55 return layer.where(active, nodata).max("layer")

56

57

58 def get_lower_active_grid_cells(active: GridDataArray) -> GridDataArray:

59 """

60 Returns grid of booleans designating location of the lowermost active grid

61 cell.

62

63 Parameters

64 ----------

65 active: {DataArray, UgridDataArray}

66 Grid of booleans designating active cell.

67 """

68 layer = active.coords["layer"]

69 lower_active_layer = get_lower_active_layer_number(active)

70 return layer == lower_active_layer

71

72

73 def create_layered_top(bottom: GridDataArray, top: GridDataArray) -> GridDataArray:

74 """

75 Create a top array with layers from a single top array and a full bottom array

76 """

77 new_top = zeros_like(bottom)

78 new_top[0] = top

79 new_top[1:] = bottom[0:-1].values

80

81 return new_top