Skip to content

Commit b253fd4

Browse files
adjusted requirements.txt; disabled other adjustment kinds except '+' for variance_scaling; minor changes in readme
1 parent 36f42cd commit b253fd4

File tree

4 files changed

+39
-41
lines changed

4 files changed

+39
-41
lines changed

README.md

+13-11
Original file line numberDiff line numberDiff line change
@@ -58,14 +58,14 @@ In this way, for example, modeled data, which on average represent values that a
5858

5959
All methods except the `adjust_3d` function requires the application on one time series.
6060

61-
| Function name | Description |
62-
| ------------------------ | -------------------------------------------------------------------------------------------- |
63-
| `linear_scaling` | Linear Scaling (additive and multiplicative) |
64-
| `variance_scaling` | Variance Scaling (additive) |
65-
| `delta_method` | Delta (Change) Method (additive and multiplicative) |
66-
| `quantile_mapping` | Quantile Mapping (additive) and Detrended Quantile Mapping (additive and multiplicative) |
67-
| `quantile_delta_mapping` | Quantile Delta Mapping (additive and multiplicative) |
68-
| `adjust_3d` | requires a method name and the respective parameters to adjust all time series of a data set |
61+
| Function name | Description |
62+
| ------------------------ | ---------------------------------------------------------------------------------------------------------- |
63+
| `linear_scaling` | Linear Scaling (additive and multiplicative) |
64+
| `variance_scaling` | Variance Scaling (additive) |
65+
| `delta_method` | Delta (Change) Method (additive and multiplicative) |
66+
| `quantile_mapping` | Quantile Mapping (additive) and Detrended Quantile Mapping (additive and multiplicative) |
67+
| `quantile_delta_mapping` | Quantile Delta Mapping (additive and multiplicative) |
68+
| `adjust_3d` | requires a method name and the respective parameters to adjust all time series of a 3-dimensional data set |
6969

7070
---
7171

@@ -105,9 +105,9 @@ qdm_result = cm.adjust_3d( # 3d = 2 spatial and 1 time dimension
105105
simh = simh['tas'],
106106
simp = simp['tas'],
107107
n_quaniles = 1000,
108-
kind = '+' # *
108+
kind = '+'
109109
)
110-
# * to calculate the relative rather than the absolute change,
110+
# to calculate the relative rather than the absolute change,
111111
# '*' can be used instead of '+' (this is prefered when adjusting
112112
# ratio based variables like precipitation)
113113
```
@@ -136,7 +136,7 @@ python3 do_bias_correction.py \
136136
```
137137

138138
- Linear and variance, as well as delta change method require `--group time.month` as argument.
139-
- Adjustment methods that apply changes in distributional biasses (QM, QDM, DQM, ...) need the `--nquantiles` argument set to some integer.
139+
- Adjustment methods that apply changes in distributional biasses (QM, QDM, DQM, ...) require the `--nquantiles` argument set to some integer.
140140
- Data sets must have the same spatial resolutions.
141141

142142
---
@@ -163,3 +163,5 @@ Since the scaling methods implemented so far scale by default over the mean valu
163163
- Delta Method based on: Beyer, R. and Krapp, M. and Manica, A.: An empirical evaluation of bias correction methods for palaeoclimate simulations (https://doi.org/10.5194/cp-16-1493-2020)
164164
- Quantile and Detrended Quantile Mapping based on: Alex J. Cannon and Stephen R. Sobie and Trevor Q. Murdock Bias Correction of GCM Precipitation by Quantile Mapping: How Well Do Methods Preserve Changes in Quantiles and Extremes? (https://doi.org/10.1175/JCLI-D-14-00754.1)
165165
- Quantile Delta Mapping based on: Tong, Y., Gao, X., Han, Z. et al. Bias correction of temperature and precipitation over China for RCM simulations using the QM and QDM methods. Clim Dyn 57, 1425–1443 (2021). (https://doi.org/10.1007/s00382-020-05447-4)
166+
167+
---

cmethods/CMethods.py

+22-26
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ def __init__(self, method: str, available_methods: list):
4949

5050
CUSTOM_METHODS = SCALING_METHODS + DISTRIBUTION_METHODS
5151

52-
METHODS = CUSTOM_METHODS #+ XCLIM_SDBA_METHODS
52+
METHODS = CUSTOM_METHODS
5353

5454
ADDITIVE = ['+', 'add']
5555
MULTIPLICATIVE = ['*', 'mult']
@@ -124,8 +124,8 @@ def adjust_3d(cls,
124124
)
125125
'''
126126

127-
obs = obs.transpose('lat', 'lon', 'time')#.load()
128-
simh = simh.transpose('lat', 'lon', 'time')#.load()
127+
obs = obs.transpose('lat', 'lon', 'time')
128+
simh = simh.transpose('lat', 'lon', 'time')
129129
simp = simp.transpose('lat', 'lon', 'time').load()
130130

131131
if group == None and method in cls.SCALING_METHODS: group = 'time.month'
@@ -169,31 +169,28 @@ def pool_adjust(cls, params) -> xr.core.dataarray.DataArray:
169169
''' Adjustment along longitude for one specific latitude
170170
used by cls.adjust_3d as callbackfunction for multiprocessing.Pool
171171
'''
172-
173-
method = params['method']
174-
obs = params['obs']
175-
simh = params['simh']
176-
simp = params['simp']
177-
n_quantiles = params.get('n_quantiles', 100)
178-
kind = params.get('kind', '+')
179-
group = params.get('group', None)
172+
for key in ['method', 'obs', 'simh', 'simp']: assert params[key]
180173
kwargs = params.get('kwargs', {})
181174

182-
result = simp.copy(deep=True).load()
183-
len_lon = len(obs.lon)
175+
result = params['simp'].copy(deep=True).load()
176+
len_lon = len(params['obs'].lon)
184177

185178
func_adjustment = None
186-
if method in cls.CUSTOM_METHODS:
187-
func_adjustment = cls.get_function(method)
188-
kwargs['n_quantiles'] = n_quantiles
189-
kwargs['kind'] = kind
179+
if params['method'] in cls.CUSTOM_METHODS:
180+
func_adjustment = cls.get_function(params['method'])
181+
kwargs['n_quantiles'] = params.get('n_quantiles', 100)
182+
kwargs['kind'] = params.get('kind', '+')
190183
for lon in range(len_lon):
191184
result[lon] = func_adjustment(
192-
obs=obs[lon], simh=simh[lon], simp=simp[lon], group=group, **kwargs
185+
obs=params['obs'][lon],
186+
simh=params['simh'][lon],
187+
simp=params['simp'][lon],
188+
group=params.get('group', None),
189+
**kwargs
193190
)
194191
return result
195192

196-
else: raise UnknownMethodError(method, cls.METHODS)
193+
else: raise UnknownMethodError(params['method'], cls.METHODS)
197194

198195
@classmethod
199196
def grouped_correction(cls,
@@ -276,11 +273,10 @@ def linear_scaling(cls,
276273
>)
277274
278275
----- E Q U A T I O N S -----
279-
T = temperature; d = daily; m = monthly
280276
Add ('+'):
281-
(1.) X^{*LS}_{sim,p}(i) = X_{sim,p}(i) + \mu_{m}(X_{obs,h}(i)) - \mu_{m}(X_{sim,h}(i))
277+
(1.) X^{*LS}_{sim,p}(i) = X_{sim,p}(i) + \mu_{m}(X_{obs,h}(i)) - \mu_{m}(X_{sim,h}(i))
282278
Mult ('*'):
283-
(2.) X^{*LS}_{sim,h}(i) = X_{sim,h}(i) + \mu_{m}(X_{obs,h}(i)) - \mu_{m}(X_{sim,h}(i))
279+
(2.) X^{*LS}_{sim,h}(i) = X_{sim,h}(i) + \mu_{m}(X_{obs,h}(i)) - \mu_{m}(X_{sim,h}(i))
284280
285281
----- R E F E R E N C E S -----
286282
@@ -333,8 +329,6 @@ def variance_scaling(cls,
333329
334330
------ E Q U A T I O N S -----
335331
336-
T = temperature; d = daily; m = monthly
337-
338332
(1.) X^{*LS}_{sim,h}(i) = X_{sim,h}(i) + \mu_{m}(X_{obs,h}(i)) - \mu_{m}(X_{sim,h}(i))
339333
(2.) X^{*LS}_{sim,p}(i) = X_{sim,p}(i) + \mu_{m}(X_{obs,h}(i)) - \mu_{m}(X_{sim,h}(i))
340334
@@ -351,7 +345,7 @@ def variance_scaling(cls,
351345
https://doi.org/10.1016/j.jhydrol.2012.05.052
352346
'''
353347
if group != None: return cls.grouped_correction(method='variance_scaling', obs=obs, simh=simh, simp=simp, group=group, kind=kind, **kwargs)
354-
else:
348+
elif kind in cls.ADDITIVE:
355349
LS_simh = cls.linear_scaling(obs, simh, simh, group=None) # Eq. 1
356350
LS_simp = cls.linear_scaling(obs, simh, simp, group=None) # Eq. 2
357351

@@ -366,6 +360,8 @@ def variance_scaling(cls,
366360
VS_2_simp = VS_1_simp * adj_scaling_factor # Eq. 5
367361
return VS_2_simp + np.nanmean(LS_simp) # Eq. 6
368362

363+
else: raise ValueError(f'"{kind}" not available or variance scaling!')
364+
369365
# ? -----========= D E L T A - M E T H O D =========------
370366
@classmethod
371367
def delta_method(cls,
@@ -561,7 +557,7 @@ def empirical_quantile_mapping(cls,
561557
https://svn.oss.deltares.nl/repos/openearthtools/trunk/python/applications/hydrotools/hydrotools/statistics/bias_correction.py
562558
563559
'''
564-
raise ValueError('idk if it is allowed to use this so please have a look at: https://svn.oss.deltares.nl/repos/openearthtools/trunk/python/applications/hydrotools/hydrotools/statistics/bias_correction.py ')
560+
raise ValueError('not implemented; please have a look at: https://svn.oss.deltares.nl/repos/openearthtools/trunk/python/applications/hydrotools/hydrotools/statistics/bias_correction.py ')
565561
# if group != None:
566562
# return cls.grouped_correction(
567563
# method = 'empirical_quantile_mapping',

cmethods/__version__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
VERSION = (0,5,4,1)
1+
VERSION = (0,5,4,2)
22
__version__ = '.'.join(map(str, VERSION))

cmethods/requirements.txt

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1+
xarray>=2022.11.0
2+
netCDF4>=1.6.1
13
numpy
2-
xarray
3-
tqdm
4-
netCDF4
4+
tqdm

0 commit comments

Comments
 (0)