1- from collections import defaultdict
21import itertools
2+ from collections import defaultdict
33from pathlib import Path
44
55import click
@@ -90,10 +90,12 @@ def get_source_paths(
9090 source_paths = defaultdict (list )
9191 for source , variant in inclusion_meta .index .tolist ():
9292 source_paths [source ].append (
93- [cd_data .extracted_cmip6_path (v , cmip6_experiment , source , variant )
94- for v in source_variables ]
95- )
96-
93+ [
94+ cd_data .extracted_cmip6_path (v , cmip6_experiment , source , variant )
95+ for v in source_variables
96+ ]
97+ )
98+
9799 return source_paths
98100
99101
@@ -152,7 +154,7 @@ def compute_anomaly(
152154 return anomaly
153155
154156
155- def generate_scenario_daily_main ( # noqa: PLR0912
157+ def generate_scenario_daily_main ( # noqa: PLR0912, PLR0915, C901
156158 output_dir : str | Path ,
157159 year : str | int ,
158160 target_variable : str ,
@@ -177,7 +179,7 @@ def generate_scenario_daily_main( # noqa: PLR0912
177179 sid = f"Source { i + 1 } /{ len (source_paths )} : { source } "
178180
179181 source_anomalies : dict [str , tuple [int , xr .Dataset ]] = {}
180- for j , vps in enumerate (variant_paths ):
182+ for j , vps in enumerate (variant_paths ):
181183 vid = f"{ sid } , Variant { j + 1 } /{ len (variant_paths )} : { vps [0 ].stem .split ('_' )[- 1 ]} "
182184 try :
183185 print (f"{ vid } : Loading reference" )
@@ -187,20 +189,20 @@ def generate_scenario_daily_main( # noqa: PLR0912
187189 except KeyError :
188190 print (f"{ vid } : Bad formatting, skipping..." )
189191 continue
190-
192+
191193 print (f"{ vid } : computing anomaly" )
192194 v_anomaly = compute_anomaly (sref , target , anomaly_type )
193-
195+
194196 key = f"{ len (v_anomaly .latitude )} _{ len (v_anomaly .longitude )} "
195197
196198 if key in source_anomalies :
197199 old_count , old_anomaly = source_anomalies [key ]
198-
200+
199201 for coord in ["latitude" , "longitude" ]:
200202 old_c = old_anomaly [coord ].to_numpy ()
201203 new_c = v_anomaly [coord ].to_numpy ()
202204 tol = 1e-5
203-
205+
204206 if np .abs (old_c - new_c ).max () < tol :
205207 v_anomaly = v_anomaly .assign ({coord : old_c })
206208 else :
@@ -212,7 +214,7 @@ def generate_scenario_daily_main( # noqa: PLR0912
212214 if source_anomalies :
213215 anomalies [source ] = source_anomalies
214216
215- ensemble_anomaly = xr .Dataset ()
217+ ensemble_anomaly = xr .Dataset ()
216218 for i , (source , source_anomalies ) in enumerate (anomalies .items ()):
217219 sid = f"Source { i + 1 } /{ len (source_paths )} : { source } "
218220 print (f"Downscaling { i + 1 } /{ len (anomalies )} : { source } " )
@@ -222,19 +224,23 @@ def generate_scenario_daily_main( # noqa: PLR0912
222224 for j , (res , (count , v_anomaly )) in enumerate (source_anomalies .items ()):
223225 res_id = f"{ sid } , Resolution { j } / { len (source_anomalies )} : { res } "
224226 print (f"Downscaling { res_id } " )
225-
227+
226228 if source_ensemble_anomaly .nbytes :
227- source_ensemble_anomaly += utils .interpolate_to_target_latlon (v_anomaly , method = "linear" )
229+ source_ensemble_anomaly += utils .interpolate_to_target_latlon (
230+ v_anomaly , method = "linear"
231+ )
228232 else :
229- source_ensemble_anomaly = utils .interpolate_to_target_latlon (v_anomaly , method = "linear" )
233+ source_ensemble_anomaly = utils .interpolate_to_target_latlon (
234+ v_anomaly , method = "linear"
235+ )
230236 total_count += count
231237 source_ensemble_anomaly /= total_count
232238
233239 if ensemble_anomaly .nbytes :
234240 ensemble_anomaly += source_ensemble_anomaly
235241 else :
236242 ensemble_anomaly = source_ensemble_anomaly
237-
243+
238244 ensemble_anomaly /= len (anomalies )
239245
240246 print ("Computing scenario data" )
@@ -277,7 +283,7 @@ def generate_scenario_daily(
277283 target_variable : str ,
278284 cmip6_experiment : str ,
279285 queue : str ,
280- overwrite : bool , # noqa: FBT001
286+ overwrite : bool ,
281287) -> None :
282288 cd_data = ClimateDownscaleData (output_dir )
283289
0 commit comments