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

27 statements  

« prev     ^ index     » next       coverage.py v7.5.1, created at 2024-05-08 14:15 +0200

1""" 

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

3""" 

4 

5from imod.typing import GridDataArray 

6from imod.typing.grid import preserve_gridtype, zeros_like 

7 

8 

9@preserve_gridtype 

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

11 """ 

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

13 active cell. 

14 

15 Parameters 

16 ---------- 

17 active: {DataArray, UgridDataArray} 

18 Grid of booleans designating active cell. 

19 """ 

20 layer = active.coords["layer"] 

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

22 # float as np.nan forces float. 

23 nodata = layer.max() + 1 

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

25 

26 

27@preserve_gridtype 

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

29 """ 

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

31 cell. 

32 

33 Parameters 

34 ---------- 

35 active: {DataArray, UgridDataArray} 

36 Grid of booleans designating active cell. 

37 """ 

38 layer = active.coords["layer"] 

39 upper_active_layer = get_upper_active_layer_number(active) 

40 return layer == upper_active_layer 

41 

42 

43@preserve_gridtype 

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

45 """ 

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

47 most active cell. 

48 

49 Parameters 

50 ---------- 

51 active: {DataArray, UgridDataArray} 

52 Grid of booleans designating active cell. 

53 """ 

54 layer = active.coords["layer"] 

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

56 # float as np.nan forces float. 

57 nodata = layer.min() - 1 

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

59 

60 

61@preserve_gridtype 

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

63 """ 

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

65 cell. 

66 

67 Parameters 

68 ---------- 

69 active: {DataArray, UgridDataArray} 

70 Grid of booleans designating active cell. 

71 """ 

72 layer = active.coords["layer"] 

73 lower_active_layer = get_lower_active_layer_number(active) 

74 return layer == lower_active_layer 

75 

76 

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

78 """ 

79 Create a top array with a layer dimension, from a top array with no layer 

80 dimension and a bottom array with a layer dimension. The (output) top of 

81 layer n is assigned the bottom of layer n-1. 

82 

83 Parameters 

84 ---------- 

85 bottom: {DataArray, UgridDataArray} 

86 Bottoms with layer dimension 

87 top: {DataArray, UgridDataArray} 

88 Top, without layer dimension 

89 

90 Returns 

91 ------- 

92 new_top: {DataArray, UgridDataArray} 

93 Top with layer dimension. 

94 """ 

95 new_top = zeros_like(bottom) 

96 new_top[0] = top 

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

98 

99 return new_top