Coverage for src/CSET/loaders/histograms.py: 36%

23 statements  

« prev     ^ index     » next       coverage.py v7.10.6, created at 2025-09-05 21:08 +0000

1# © Crown copyright, Met Office (2022-2025) and CSET contributors. 

2# 

3# Licensed under the Apache License, Version 2.0 (the "License"); 

4# you may not use this file except in compliance with the License. 

5# You may obtain a copy of the License at 

6# 

7# http://www.apache.org/licenses/LICENSE-2.0 

8# 

9# Unless required by applicable law or agreed to in writing, software 

10# distributed under the License is distributed on an "AS IS" BASIS, 

11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 

12# See the License for the specific language governing permissions and 

13# limitations under the License. 

14 

15"""Load histogram recipes.""" 

16 

17import itertools 

18 

19from CSET.recipes import Config, RawRecipe, get_models 

20 

21 

22def load(conf: Config): 

23 """Yield recipes from the given workflow configuration.""" 

24 # Load a list of model detail dictionaries. 

25 models = get_models(conf.asdict()) 

26 

27 # Surface (2D) fields. 

28 if conf.HISTOGRAM_SURFACE_FIELD: 28 ↛ 29line 28 didn't jump to line 29 because the condition on line 28 was never true

29 for field in conf.SURFACE_FIELDS: 

30 yield RawRecipe( 

31 recipe="generic_surface_histogram_series.yaml", 

32 variables={ 

33 "VARNAME": field, 

34 "MODEL_NAME": [model["name"] for model in models], 

35 "SEQUENCE": "time" 

36 if conf.HISTOGRAM_SURFACE_FIELD_SEQUENCE 

37 else "realization", 

38 "SUBAREA_TYPE": conf.SUBAREA_TYPE if conf.SELECT_SUBAREA else None, 

39 "SUBAREA_EXTENT": conf.SUBAREA_EXTENT 

40 if conf.SELECT_SUBAREA 

41 else None, 

42 }, 

43 model_ids=[model["id"] for model in models], 

44 aggregation=False, 

45 ) 

46 

47 # Pressure level fields. 

48 if conf.HISTOGRAM_PLEVEL_FIELD: 48 ↛ 49line 48 didn't jump to line 49 because the condition on line 48 was never true

49 for field, plevel in itertools.product( 

50 conf.PRESSURE_LEVEL_FIELDS, 

51 conf.PRESSURE_LEVELS, 

52 ): 

53 yield RawRecipe( 

54 recipe="generic_level_histogram_series.yaml", 

55 variables={ 

56 "VARNAME": field, 

57 "LEVELTYPE": "pressure", 

58 "LEVEL": plevel, 

59 "MODEL_NAME": [model["name"] for model in models], 

60 "SEQUENCE": "time" 

61 if conf.HISTOGRAM_PLEVEL_FIELD_SEQUENCE 

62 else "realization", 

63 "SUBAREA_TYPE": conf.SUBAREA_TYPE if conf.SELECT_SUBAREA else None, 

64 "SUBAREA_EXTENT": conf.SUBAREA_EXTENT 

65 if conf.SELECT_SUBAREA 

66 else None, 

67 }, 

68 model_ids=[model["id"] for model in models], 

69 aggregation=False, 

70 ) 

71 

72 # Model level fields 

73 if conf.HISTOGRAM_MLEVEL_FIELD: 73 ↛ 74line 73 didn't jump to line 74 because the condition on line 73 was never true

74 for field, mlevel in itertools.product( 

75 conf.MODEL_LEVEL_FIELDS, 

76 conf.MODEL_LEVELS, 

77 ): 

78 yield RawRecipe( 

79 recipe="generic_level_histogram_series.yaml", 

80 variables={ 

81 "VARNAME": field, 

82 "LEVELTYPE": "model_level_number", 

83 "LEVEL": mlevel, 

84 "MODEL_NAME": [model["name"] for model in models], 

85 "SEQUENCE": "time" 

86 if conf.HISTOGRAM_MLEVEL_FIELD_SEQUENCE 

87 else "realization", 

88 "SUBAREA_TYPE": conf.SUBAREA_TYPE if conf.SELECT_SUBAREA else None, 

89 "SUBAREA_EXTENT": conf.SUBAREA_EXTENT 

90 if conf.SELECT_SUBAREA 

91 else None, 

92 }, 

93 model_ids=[model["id"] for model in models], 

94 aggregation=False, 

95 ) 

96 

97 # Create a list of case aggregation types. 

98 AGGREGATION_TYPES = ["lead_time", "hour_of_day", "validity_time", "all"] 

99 

100 # Surface (2D) fields. 

101 for atype, field in itertools.product(AGGREGATION_TYPES, conf.SURFACE_FIELDS): 101 ↛ 102line 101 didn't jump to line 102 because the loop on line 101 never started

102 if conf.HISTOGRAM_SURFACE_FIELD_AGGREGATION[AGGREGATION_TYPES.index(atype)]: 

103 yield RawRecipe( 

104 recipe=f"generic_surface_histogram_series_case_aggregation_{atype}.yaml", 

105 variables={ 

106 "VARNAME": field, 

107 "MODEL_NAME": [model["name"] for model in models], 

108 "SUBAREA_TYPE": conf.SUBAREA_TYPE if conf.SELECT_SUBAREA else None, 

109 "SUBAREA_EXTENT": conf.SUBAREA_EXTENT 

110 if conf.SELECT_SUBAREA 

111 else None, 

112 }, 

113 model_ids=[model["id"] for model in models], 

114 aggregation=True, 

115 ) 

116 

117 # Pressure level fields. 

118 for atype, field, plevel in itertools.product( 118 ↛ 121line 118 didn't jump to line 121 because the loop on line 118 never started

119 AGGREGATION_TYPES, conf.PRESSURE_LEVEL_FIELDS, conf.PRESSURE_LEVELS 

120 ): 

121 if conf.HISTOGRAM_PLEVEL_FIELD_AGGREGATION[AGGREGATION_TYPES.index(atype)]: 

122 yield RawRecipe( 

123 recipe=f"generic_level_histogram_series_case_aggregation_{atype}.yaml", 

124 variables={ 

125 "VARNAME": field, 

126 "LEVELTYPE": "pressure", 

127 "LEVEL": plevel, 

128 "MODEL_NAME": [model["name"] for model in models], 

129 "SUBAREA_TYPE": conf.SUBAREA_TYPE if conf.SELECT_SUBAREA else None, 

130 "SUBAREA_EXTENT": conf.SUBAREA_EXTENT 

131 if conf.SELECT_SUBAREA 

132 else None, 

133 }, 

134 model_ids=[model["id"] for model in models], 

135 aggregation=True, 

136 ) 

137 

138 # Model level fields. 

139 for atype, field, mlevel in itertools.product( 139 ↛ 142line 139 didn't jump to line 142 because the loop on line 139 never started

140 AGGREGATION_TYPES, conf.MODEL_LEVEL_FIELDS, conf.MODEL_LEVELS 

141 ): 

142 if conf.HISTOGRAM_MLEVEL_FIELD_AGGREGATION[AGGREGATION_TYPES.index(atype)]: 

143 yield RawRecipe( 

144 recipe=f"generic_level_histogram_series_case_aggregation_{atype}.yaml", 

145 variables={ 

146 "VARNAME": field, 

147 "LEVELTYPE": "model_level_number", 

148 "LEVEL": mlevel, 

149 "MODEL_NAME": [model["name"] for model in models], 

150 "SUBAREA_TYPE": conf.SUBAREA_TYPE if conf.SELECT_SUBAREA else None, 

151 "SUBAREA_EXTENT": conf.SUBAREA_EXTENT 

152 if conf.SELECT_SUBAREA 

153 else None, 

154 }, 

155 model_ids=[model["id"] for model in models], 

156 aggregation=True, 

157 )