Skip to content

Commit e999f4f

Browse files
adjusted docstrings; removed unused variables
1 parent fca91c0 commit e999f4f

File tree

2 files changed

+54
-33
lines changed

2 files changed

+54
-33
lines changed

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ A C++ implementation that works way faster can be found here: [https://github.co
1313
- Linear Scaling
1414
- Variance Scaling
1515
- Delta (Change) Method
16-
- Quantile Mapping
16+
- Quantile and Detrended Quantile Mapping
1717
- Quantile Delta Mapping
1818
____
1919
## Usage
@@ -39,7 +39,7 @@ ls_result = cm.linear_scaling(
3939
kind = '+' # *
4040
)
4141

42-
qdm_result = cm.adjust_2d(
42+
qdm_result = cm.adjust_2d( # 2d = 2 spatial and 1 time dimension
4343
method = 'quantile_delta_mapping',
4444
obs = obsh['tas'],
4545
simh = simh['tas'],
@@ -55,7 +55,7 @@ ____
5555

5656
`/examples/examples.ipynb`: Notebook containing different methods and plots
5757

58-
`/examples/do_bias_correctino.py`: Example script for adjusting climate data
58+
`/examples/do_bias_correction.py`: Example script for adjusting climate data
5959
```bash
6060
python3 do_bias_correction.py \
6161
--obs input_data/obs.nc \
@@ -73,4 +73,4 @@ python3 do_bias_correction.py \
7373
- Data sets should have the same spatial resolutions.
7474
____
7575
## Notes:
76-
- Computation in Python takes some time, so this is only for demonstration. When adjusting large datasets, its best to the C++ implementation mentioned above.
76+
- Computation in Python takes some time, so this is only for demonstration. When adjusting large datasets, its best to the C++ implementation mentioned above.

cmethods/CMethods.py

Lines changed: 50 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -74,11 +74,9 @@ def adjust_2d(cls,
7474
obs: xr.core.dataarray.DataArray,
7575
simh: xr.core.dataarray.DataArray,
7676
simp: xr.core.dataarray.DataArray,
77-
tslice_adjust: slice=None,
7877
n_quantiles: int=100,
7978
kind: str='+',
8079
group: str=None,
81-
window: int=1,
8280
n_jobs: int=1,
8381
**kwargs
8482
) -> xr.core.dataarray.Dataset:
@@ -94,11 +92,9 @@ def adjust_2d(cls,
9492
simh (xarray.core.dataarray.DataArray): simulated historical Data
9593
simp (xarray.core.dataarray.DataArray): future simulated Data to adjust
9694
97-
tslice_adjust (slice): Timespan to adjust, default: None
9895
n_quantiles (int): Number of quantiles to involve
9996
kind (str): Kind of adjustment ('+' or '*'), default: '+' (always use '+' for temperature)
10097
group (str): Group data by (e.g.: 'time.month', 'time.dayofyear')
101-
window (int): Grouping window, default: 1
10298
n_jobs (int): Use n processes, default: 1
10399
104100
----- R E T U R N -----
@@ -116,7 +112,6 @@ def adjust_2d(cls,
116112
obs = obs[variable],
117113
simh = simh[variable],
118114
simp = simp[variable],
119-
tslice_adjust = slice('2071-01-01', '2100-12-31'),
120115
n_quantiles = 100,
121116
group = 'time.month',
122117
n_jobs = 4
@@ -171,11 +166,9 @@ def pool_adjust(cls, params) -> xr.core.dataarray.DataArray:
171166
obs = params['obs']
172167
simh = params['simh']
173168
simp = params['simp']
174-
tslice_adjust = params.get('tslice_adjust', None)
175169
n_quantiles = params.get('n_quantiles', 100)
176170
kind = params.get('kind', '+')
177171
group = params.get('group', None)
178-
window = params.get('window', 1)
179172
save_model = params.get('save_model', None)
180173
kwargs = params.get('kwargs', {})
181174

@@ -208,7 +201,7 @@ def grouped_correction(cls,
208201
'''Method to adjust 1 dimensional climate data while respecting adjustment groups.
209202
210203
----- P A R A M E T E R S -----
211-
204+
method (str): adjustment method name
212205
obs (xarray.core.dataarray.DataArray): observed / obserence Data
213206
simh (xarray.core.dataarray.DataArray): simulated historical Data
214207
simp (xarray.core.dataarray.DataArray): future simulated Data
@@ -256,7 +249,7 @@ def linear_scaling(cls,
256249
simh (xarray.core.dataarray.DataArray): simulated historical Data
257250
simp (xarray.core.dataarray.DataArray): future simulated Data
258251
group (str): [optional] Group / Period (e.g.: 'time.month')
259-
method (str): '+' or '*', default: '+'
252+
kind (str): '+' or '*', default: '+'
260253
261254
----- R E T U R N -----
262255
@@ -313,6 +306,7 @@ def variance_scaling(cls,
313306
simh (xarray.core.dataarray.DataArray): simulated historical Data
314307
simp (xarray.core.dataarray.DataArray): future simulated Data
315308
group (str): [optional] Group / Period (e.g.: 'time.month')
309+
kind (str): '+' or '*', default: '+'
316310
317311
----- R E T U R N -----
318312
@@ -375,7 +369,8 @@ def delta_method(cls,
375369
simh (xarray.core.dataarray.DataArray): simulated historical Data
376370
simp (xarray.core.dataarray.DataArray): future simulated Data
377371
group (str): [optional] Group / Period (e.g.: 'time.month')
378-
372+
kind (str): '+' or '*', default: '+'
373+
379374
----- R E T U R N -----
380375
381376
xarray.core.dataarray.DataArray: Adjusted data
@@ -418,17 +413,30 @@ def quantile_mapping(cls,
418413
**kwargs
419414
) -> xr.core.dataarray.DataArray:
420415
''' Quantile Mapping Bias Correction
421-
422-
------ E Q U A T I O N S -----
423-
Add (+):
424-
(1.) X^{*QM}_{sim,p}(i) = F^{-1}_{obs,h} \left\{F_{sim,h}\left[X_{sim,p}(i)\right]\right\}
425-
Mult (*):
426-
(1.) --//--
427416
428-
----- R E F E R E N C E S -----
429-
430-
Multiplicative implementeation by 'deleted profile' OR Adrian Tompkins [email protected], posted on November 8, 2016 at
431-
https://www.researchgate.net/post/Does-anyone-know-about-bias-correction-and-quantile-mapping-in-PYTHON
417+
----- P A R A M E T E R S -----
418+
419+
obs (xarray.core.dataarray.DataArray): observed / obserence Data
420+
simh (xarray.core.dataarray.DataArray): simulated historical Data
421+
simp (xarray.core.dataarray.DataArray): future simulated Data
422+
n_quantiles (int): number of quantiles to use
423+
group (str): [optional] Group / Period (e.g.: 'time.month')
424+
kind (str): '+' or '*', default: '+'
425+
426+
----- R E T U R N -----
427+
428+
xarray.core.dataarray.DataArray: Adjusted data
429+
430+
------ E Q U A T I O N S -----
431+
Add (+):
432+
(1.) X^{*QM}_{sim,p}(i) = F^{-1}_{obs,h} \left\{F_{sim,h}\left[X_{sim,p}(i)\right]\right\}
433+
Mult (*):
434+
(1.) --//--
435+
436+
----- R E F E R E N C E S -----
437+
438+
Multiplicative implementeation by 'deleted profile' OR Adrian Tompkins [email protected], posted on November 8, 2016 at
439+
https://www.researchgate.net/post/Does-anyone-know-about-bias-correction-and-quantile-mapping-in-PYTHON
432440
'''
433441

434442
if group != None: return cls.grouped_correction(method='quantile_mapping', obs=obs, simh=simh, simp=simp, group=group, n_quantiles=n_quantiles, kind=kind, **kwargs)
@@ -556,16 +564,29 @@ def quantile_delta_mapping(cls,
556564
**kwargs
557565
) -> xr.core.dataarray.DataArray:
558566
''' Quantile Delta Mapping bias adjustment
559-
560-
------ E Q U A T I O N S -----
567+
568+
----- P A R A M E T E R S -----
569+
570+
obs (xarray.core.dataarray.DataArray): observed / obserence Data
571+
simh (xarray.core.dataarray.DataArray): simulated historical Data
572+
simp (xarray.core.dataarray.DataArray): future simulated Data
573+
n_quantiles (int): number of quantiles to use
574+
group (str): [optional] Group / Period (e.g.: 'time.month')
575+
kind (str): '+' or '*', default: '+'
576+
577+
----- R E T U R N -----
561578
579+
xarray.core.dataarray.DataArray: Adjusted data
580+
581+
------ E Q U A T I O N S -----
582+
562583
Add (+):
563584
(1.1) \varepsilon(i) = F_{sim,p}\left[X_{sim,p}(i)\right], \hspace{1em} \varepsilon(i)\in\{0,1\}
564585
565586
(1.2) X^{QDM(1)}_{sim,p}(i) = F^{-1}_{obs,h}\left[\varepsilon(i)\right]
566587
567588
(1.3) \Delta(i) & = F^{-1}_{sim,p}\left[\varepsilon(i)\right] - F^{-1}_{sim,h}\left[\varepsilon(i)\right] \\[1pt]
568-
& = X_{sim,p}(i) - F^{-1}_{sim,h}\left\{F^{}_{sim,p}\left[X_{sim,p}(i)\right]\right\}
589+
& = X_{sim,p}(i) - F^{-1}_{sim,h}\left\{F^{}_{sim,p}\left[X_{sim,p}(i)\right]\right\}
569590
570591
(1.4) X^{*QDM}_{sim,p}(i) = X^{QDM(1)}_{sim,p}(i) + \Delta(i)
571592
@@ -575,17 +596,17 @@ def quantile_delta_mapping(cls,
575596
(1.2) --//--
576597
577598
(2.3) \Delta(i) & = \frac{ F^{-1}_{sim,p}\left[\varepsilon(i)\right] }{ F^{-1}_{sim,h}\left[\varepsilon(i)\right] } \\[1pt]
578-
& = \frac{ X_{sim,p}(i) }{ F^{-1}_{sim,h}\left\{F^{}_{sim,p}\left[X_{sim,p}(i)\right]\right\} }
599+
& = \frac{ X_{sim,p}(i) }{ F^{-1}_{sim,h}\left\{F^{}_{sim,p}\left[X_{sim,p}(i)\right]\right\} }
579600
580601
(2.4) X^{*QDM}_{sim,p}(i) = X^{QDM(1)}_{sim,p}(i) \cdot \Delta(i)
581602
582-
----- R E F E R E N C E S -----
583-
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).
584-
https://doi.org/10.1007/s00382-020-05447-4
603+
----- R E F E R E N C E S -----
604+
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).
605+
https://doi.org/10.1007/s00382-020-05447-4
585606
586-
----- N O T E S -----
607+
----- N O T E S -----
587608
588-
@param global_min: float | this parameter can be set when kind == '*' to define a custom lower limit. Otherwise 0.0 is used.
609+
@param global_min: float | this parameter can be set when kind == '*' to define a custom lower limit. Otherwise 0.0 is used.
589610
590611
'''
591612

0 commit comments

Comments
 (0)