16
16
__copyright__ = __author__
17
17
18
18
__link__ = 'https://b-schwertfeger.de'
19
- __github__ = 'https://github.com/btschwertfeger/Bias-Adjustment-Python' ;
20
- __description__ = ' \
21
- Class / Script / Methods to apply bias corrections on temperature climate data \
22
- \
23
- T = Temperatures ($T$)\
24
- X = Some climate variable ($X$)\
25
- h = historical \
26
- p = scenario; future; predicted \
27
- obs = observed data ($T_{obs,h}$) \
28
- simh = modeled data with same time period as obs ($T_{sim,h}$) \
29
- simp = data to correct (predicted simulated data) ($T_{sim,p}$) \
30
- \
31
- \
32
- F = Cummulative Distribution Function\
33
- \mu = mean\
34
- \sigma = standard deviation\
35
- i = index\
36
- _{m} = long-term monthly interval\
19
+ __github__ = 'https://github.com/btschwertfeger/Bias-Adjustment-Python'
20
+ __description__ = ' \
21
+ Class / Script / Methods to apply bias corrections on temperature climate data \
22
+ \
23
+ T = Temperatures ($T$) \
24
+ X = Some climate variable ($X$) \
25
+ h = historical \
26
+ p = scenario; future; predicted \
27
+ obs = observed data ($T_{obs,h}$) \
28
+ simh = modeled data with same time period as obs ($T_{sim,h}$) \
29
+ simp = data to correct (predicted simulated data) ($T_{sim,p}$) \
30
+ \
31
+ \
32
+ F = Cummulative Distribution Function \
33
+ \mu = mean \
34
+ \sigma = standard deviation \
35
+ i = index \
36
+ _{m} = long-term monthly interval \
37
37
'
38
38
39
39
class CMethods (object ):
@@ -227,11 +227,7 @@ def pool_adjust(cls, params) -> xr.core.dataarray.DataArray:
227
227
228
228
func_adjustment = None
229
229
if method in cls .XCLIM_SDBA_METHODS :
230
- if method == 'xclim_dqm' : func_adjustment = sdba .adjustment .DetrendedQuantileMapping .train
231
- elif method == 'xclim_eqm' : func_adjustment = sdba .adjustment .EmpiricalQuantileMapping .train
232
- elif method == 'xclim_qdm' : func_adjustment = sdba .adjustment .QuantileDeltaMapping .train
233
- else : raise ValueError (f'Method { method } not implemented yet.' )
234
-
230
+ func_adjustment = cls .get_function (method )
235
231
for lon in range (len_lon ):
236
232
result [lon ] = cls .xclim_sdba_adjustment (
237
233
method = func_adjustment ,
@@ -249,14 +245,7 @@ def pool_adjust(cls, params) -> xr.core.dataarray.DataArray:
249
245
return result
250
246
251
247
elif method in cls .CUSTOM_METHODS :
252
- if method == 'linear_scaling' : func_adjustment = cls .linear_scaling
253
- elif method == 'variance_scaling' : func_adjustment = cls .variance_scaling
254
- elif method == 'delta_method' : func_adjustment = cls .delta_method
255
- elif method == 'quantile_mapping' : func_adjustment = cls .quantile_mapping
256
- elif method == 'empirical_quantile_mapping' : func_adjustment = cls .empirical_quantile_mapping
257
- elif method == 'quantile_delta_mapping' : func_adjustment = cls .quantile_delta_mapping
258
- else : raise ValueError (f'Method { method } not implemented yet.' )
259
-
248
+ func_adjustment = cls .get_function (method )
260
249
kwargs ['n_quantiles' ] = n_quantiles
261
250
kwargs ['kind' ] = kind
262
251
for lon in range (len_lon ):
@@ -342,35 +331,25 @@ def grouped_correction(cls,
342
331
xarray.core.dataarray.DataArray: Adjusted data
343
332
344
333
'''
345
-
346
- def compute (
347
- method : str ,
348
- obs : xr .core .dataarray .DataArray ,
349
- simh : xr .core .dataarray .DataArray ,
350
- simp : xr .core .dataarray .DataArray ,
351
- kind : str = '+' ,
352
- ** kwargs
353
- ):
354
- # TODO: (maybe) match case for python3.10
355
- if method == 'linear_scaling' : return cls .linear_scaling (obs = obs , simh = simh , simp = simp , kind = kind , ** kwargs )
356
- elif method == 'variance_scaling' : return cls .variance_scaling (obs = obs , simh = simh , simp = simp , ** kwargs )
357
- elif method == 'delta_method' : return cls .delta_method (obs = obs , simh = simh , simp = simp , kind = kind , ** kwargs )
358
- elif method == 'quantile_mapping' : return cls .quantile_mapping (obs = obs , simh = simh , simp = simp , ** kwargs )
359
- elif method == 'empirical_quantile_mapping' : return cls .empirical_quantile_mapping (obs = obs , simh = simh , simp = simp , ** kwargs )
360
- elif method == 'quantile_delta_mapping' : return cls .quantile_delta_mapping (obs = obs , simh = simh , simp = simp , ** kwargs )
361
- else : raise UnknownMethodError (method , cls .METHODS )
362
-
334
+
335
+ func_adjustment = cls .get_function (method )
363
336
result = simp .copy (deep = True ).load ()
364
337
groups = simh .groupby (group ).groups
365
- scen_groups = simp .groupby (group ).groups
366
-
367
- for group , scen_group in zip (groups .keys (), scen_groups .keys ()):
368
- obs_values_by_group = np .array ([obs [i ] for i in groups [group ]])
369
- contr_values_by_group = np .array ([simh [i ] for i in groups [group ]])
370
- scen_values_by_group = np .array ([simp [i ] for i in scen_groups [group ]])
338
+ simp_groups = simp .groupby (group ).groups
339
+
340
+ for month , simp_group in zip (groups .keys (), simp_groups .keys ()):
341
+ m_obs , m_simh , m_simp = [], [], []
342
+
343
+ for i in groups [month ]:
344
+ m_obs .append (obs [i ])
345
+ m_simh .append (simh [i ])
346
+ m_simp .append (simp [i ])
347
+ # m_obs = np.array([obs[i] for i in groups[group]])
348
+ # m_simh = np.array([simh[i] for i in groups[group]])
349
+ # m_simp = np.array([simp[i] for i in scen_groups[group]])
371
350
372
- computed_result = compute ( method = method , obs = obs_values_by_group , simh = contr_values_by_group , simp = scen_values_by_group , kind = kind , ** kwargs )
373
- for i , index in enumerate (scen_groups [ group ]): result [index ] = computed_result [i ]
351
+ computed_result = func_adjustment ( obs = m_obs , simh = m_simh , simp = m_simp , kind = kind , ** kwargs )
352
+ for i , index in enumerate (simp_groups [ month ]): result [index ] = computed_result [i ]
374
353
375
354
return result
376
355
@@ -579,7 +558,7 @@ def quantile_mapping(cls,
579
558
580
559
cdf_obs = cls .get_cdf (obs , xbins )
581
560
cdf_simh = cls .get_cdf (simh , xbins )
582
- epsilon = np .interp (simp , xbins , cdf_simh ) # Eq. 1
561
+ epsilon = np .interp (simp , xbins , cdf_simh ) # Eq. 1
583
562
584
563
return cls .get_inverse_of_cdf (cdf_obs , epsilon , xbins ) # Eq. 1
585
564
elif kind == '*' :
0 commit comments