Coverage for C:\src\imod-python\imod\msw\output_control.py: 97%

39 statements  

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

1import pandas as pd 

2 

3from imod.msw.fixed_format import VariableMetaData 

4from imod.msw.pkgbase import MetaSwapPackage 

5from imod.msw.timeutil import to_metaswap_timeformat 

6 

7 

8# I did not use long variable names here (e.g. "precipitation"), as MetaSWAP 

9# uses these 2 to 4 character names to print its output to. This also has the 

10# benefit that the user is able to set additional variable names via kwargs 

11# (there are more than 130 possible variable names to choose from in MetaSWAP) 

12class VariableOutputControl(MetaSwapPackage): 

13 """ 

14 Control which variables will be created as output. The variable names used 

15 in this class provide a condensed water balance. You can use additional 

16 keyword arguments to set more variables by using their specific name, e.g. 

17 `vcr = True` for the water balance error. For all possibilities see the 

18 SIMGRO Input and Output description. 

19 

20 All budgets will be written in m unit to in `.idf` files and to mm unit in 

21 `.csv` files. 

22 

23 Parameters 

24 ---------- 

25 Pm: bool 

26 Write measured precipitation 

27 Psgw: bool 

28 Write sprinkling precipitation, from groundwater 

29 Pssw: bool 

30 Write sprinkling precipitation, from surface water 

31 qrun: bool 

32 Write runon 

33 qdr: bool 

34 Write net infiltration of surface water 

35 qspgw: bool 

36 Groundwater extraction for sprinkling from layer 

37 qmodf: bool 

38 Sum of all MODFLOW stresses on groundwater 

39 ETact: bool 

40 Write total actual evapotranspiration, which is the sum of the 

41 sprinkling evaporation (Esp), interception evaporation (Eic), ponding 

42 evaporation (Epd) bare soil evaporation (Ebs), and actual transpiration 

43 (Tact). 

44 **kwargs: bool 

45 Additional variables to let MetaSWAP write 

46 """ 

47 

48 _file_name = "sel_key_svat_per.inp" 

49 _settings: dict = {} 

50 _metadata_dict = { 

51 "variable": VariableMetaData(10, None, None, str), 

52 "option": VariableMetaData(10, 0, 3, int), 

53 } 

54 

55 def __init__( 

56 self, 

57 Pm=True, 

58 Psgw=True, 

59 Pssw=True, 

60 qrun=True, 

61 qdr=True, 

62 qspgw=True, 

63 qmodf=True, 

64 ETact=True, 

65 **kwargs, 

66 ): 

67 super().__init__() 

68 

69 # Convert to integer, as MetaSWAP expects its values as integers. 

70 self.dataset["Pm"] = int(Pm) 

71 self.dataset["Psgw"] = int(Psgw) 

72 self.dataset["Pssw"] = int(Pssw) 

73 self.dataset["qrun"] = int(qrun) 

74 self.dataset["qdr"] = int(qdr) 

75 self.dataset["qspgw"] = int(qspgw) 

76 self.dataset["qmodf"] = int(qmodf) 

77 self.dataset["ETact"] = int(ETact) 

78 

79 # Set additional settings 

80 for key, value in kwargs.items(): 

81 self.dataset[key] = int(value) 

82 

83 def _render(self, file, *args): 

84 variable, option = zip( 

85 *[(var, self.dataset[var].values) for var in self.dataset.data_vars] 

86 ) 

87 

88 dataframe = pd.DataFrame(data={"variable": variable, "option": option}) 

89 

90 self._check_range(dataframe) 

91 

92 return self.write_dataframe_fixed_width(file, dataframe) 

93 

94 

95class TimeOutputControl(MetaSwapPackage): 

96 """ 

97 Specify the accumulation periods which will be used to write output. For 

98 example, say the model computes on a daily timestep, but timesteps two days 

99 apart are specified, the summed fluxes of each two days are written by 

100 MetaSWAP. 

101 

102 Parameters 

103 ---------- 

104 time: xr.DataArray 

105 Timesteps at which to write output. 

106 """ 

107 

108 _file_name = "tiop_sim.inp" 

109 _settings: dict = {} 

110 _metadata_dict = { 

111 "time_since_start_year": VariableMetaData(15, 0.0, 366.0, float), 

112 "year": VariableMetaData(6, 1, 9999, int), 

113 "option": VariableMetaData(6, 1, 7, int), 

114 } 

115 

116 def __init__(self, time): 

117 super().__init__() 

118 

119 self.dataset["times"] = time 

120 

121 def _render(self, file, *args): 

122 year, time_since_start_year = to_metaswap_timeformat(self.dataset["times"]) 

123 

124 dataframe = pd.DataFrame( 

125 data={"time_since_start_year": time_since_start_year, "year": year} 

126 ) 

127 

128 dataframe["time_since_start_year"] += 1 

129 dataframe["option"] = 7 

130 

131 self._check_range(dataframe) 

132 

133 return self.write_dataframe_fixed_width(file, dataframe)