Coverage for C:\src\imod-python\imod\msw\vegetation.py: 93%

29 statements  

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

1import numpy as np 

2import xarray as xr 

3 

4from imod.msw.fixed_format import VariableMetaData 

5from imod.msw.pkgbase import MetaSwapPackage 

6 

7 

8class AnnualCropFactors(MetaSwapPackage): 

9 """ 

10 For each vegetation type specify a yearly trend in vegetation factors and 

11 interception characteristics. These are used if WOFOST is not used. 

12 

13 This class is responsible for the file `fact_svat.inp`. 

14 

15 Parameters 

16 ---------- 

17 soil_cover: array of floats (xr.DataArray) 

18 Soil cover in m2/m2. Must have a "vegetation_index" and "day_of_year" a 

19 coordinates. 

20 leaf_area_index: array of floats (xr.DataArray) 

21 Leaf area index in m2/m2. Must have a "vegetation_index" and 

22 "day_of_year" a coordinates. 

23 interception_capacity: array of floats (xr.DataArray) 

24 Interception capacity in m3/m2. Must have a "vegetation_index" and 

25 "day_of_year" a coordinates. 

26 vegetation_factor: array of floats (xr.DataArray) 

27 Vegetation factor. Must have a "vegetation_index" and "day_of_year" a 

28 coordinates. 

29 interception_factor: array of floats (xr.DataArray) 

30 Interception evaporation factor. Must have a "vegetation_index" and 

31 "day_of_year" a coordinates. 

32 bare_soil_factor: array of floats (xr.DataArray) 

33 Bare soil evaporation factor. Must have a "vegetation_index" and 

34 "day_of_year" a coordinates. 

35 ponding_factor: array of floats (xr.DataArray) 

36 Ponding factor. Must have a "vegetation_index" and "day_of_year" a 

37 coordinates. 

38 """ 

39 

40 _file_name = "fact_svat.inp" 

41 _metadata_dict = { 

42 "vegetation_index": VariableMetaData(6, 0, 999, int), 

43 "day_of_year": VariableMetaData(6, 1, 366, int), 

44 "soil_cover": VariableMetaData(8, 0.0, 1.0, float), 

45 "leaf_area_index": VariableMetaData(8, 0.0, 10.0, float), 

46 "interception_capacity": VariableMetaData(8, 0.0, 0.1, float), 

47 # io manual: min value vegetation_factor = 0.1, but example file has 0. 

48 # and works 

49 "vegetation_factor": VariableMetaData(8, 0.0, 10.0, float), 

50 "interception_factor": VariableMetaData(8, 0.01, 10.0, float), 

51 "bare_soil_factor": VariableMetaData(8, 0.01, 10.0, float), 

52 "ponding_factor": VariableMetaData(8, 0.01, 10.0, float), 

53 } 

54 

55 def __init__( 

56 self, 

57 soil_cover: xr.DataArray, 

58 leaf_area_index: xr.DataArray, 

59 interception_capacity: xr.DataArray, 

60 vegetation_factor: xr.DataArray, 

61 interception_factor: xr.DataArray, 

62 bare_soil_factor: xr.DataArray, 

63 ponding_factor: xr.DataArray, 

64 ): 

65 super().__init__() 

66 self.dataset["soil_cover"] = soil_cover 

67 self.dataset["leaf_area_index"] = leaf_area_index 

68 self.dataset["interception_capacity"] = interception_capacity 

69 self.dataset["vegetation_factor"] = vegetation_factor 

70 self.dataset["interception_factor"] = interception_factor 

71 self.dataset["bare_soil_factor"] = bare_soil_factor 

72 self.dataset["ponding_factor"] = ponding_factor 

73 

74 self._pkgcheck() 

75 

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

77 dataframe = self.dataset.to_dataframe( 

78 dim_order=("vegetation_index", "day_of_year") 

79 ).reset_index() 

80 

81 self._check_range(dataframe) 

82 

83 return self.write_dataframe_fixed_width(file, dataframe) 

84 

85 def _pkgcheck(self): 

86 dims = self.dataset.dims 

87 dims_expected = ("day_of_year", "vegetation_index") 

88 if len(set(dims) - set(dims_expected)) > 0: 

89 raise ValueError( 

90 f"Please provide DataArrays with dimensions {dims_expected}" 

91 ) 

92 

93 day_of_year = self.dataset.coords["day_of_year"].values 

94 if not np.all(day_of_year == np.arange(1, 367)): 

95 raise ValueError(r"Not all days of the year included in data.")