Skip to content

Commit d627b8c

Browse files
added example in notebook
1 parent 6a25bae commit d627b8c

File tree

3 files changed

+489
-88
lines changed

3 files changed

+489
-88
lines changed

.DS_Store

0 Bytes
Binary file not shown.

CMethods.py

Lines changed: 37 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -16,24 +16,24 @@
1616
__copyright__ = __author__
1717
__email__ = '[email protected]'
1818
__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 \
3737
'
3838

3939
class CMethods(object):
@@ -227,11 +227,7 @@ def pool_adjust(cls, params) -> xr.core.dataarray.DataArray:
227227

228228
func_adjustment = None
229229
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)
235231
for lon in range(len_lon):
236232
result[lon] = cls.xclim_sdba_adjustment(
237233
method = func_adjustment,
@@ -249,14 +245,7 @@ def pool_adjust(cls, params) -> xr.core.dataarray.DataArray:
249245
return result
250246

251247
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)
260249
kwargs['n_quantiles'] = n_quantiles
261250
kwargs['kind'] = kind
262251
for lon in range(len_lon):
@@ -342,35 +331,25 @@ def grouped_correction(cls,
342331
xarray.core.dataarray.DataArray: Adjusted data
343332
344333
'''
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)
363336
result = simp.copy(deep=True).load()
364337
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]])
371350

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]
374353

375354
return result
376355

@@ -579,7 +558,7 @@ def quantile_mapping(cls,
579558

580559
cdf_obs = cls.get_cdf(obs, xbins)
581560
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
583562

584563
return cls.get_inverse_of_cdf(cdf_obs, epsilon, xbins) # Eq. 1
585564
elif kind == '*':

examples.ipynb

Lines changed: 452 additions & 30 deletions
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)