Getting Started

Installation

The python-cmethods module can be installed using the package manager pip:

python3 -m pip install python-cmethods

Usage and Examples

The python-cmethods module can be imported and applied as showing in the following examples. For more detailed description of the methods, please have a look at the method specific documentation.

Apply the Linear Scaling bias correction technique on 1-dimensional data
 1import xarray as xr
 2from cmethods import adjust
 3
 4obsh = xr.open_dataset("input_data/observations.nc")
 5simh = xr.open_dataset("input_data/control.nc")
 6simp = xr.open_dataset("input_data/scenario.nc")
 7
 8ls_result = adjust(
 9    method="linear_scaling",
10    obs=obsh["tas"][:, 0, 0],
11    simh=simh["tas"][:, 0, 0],
12    simp=simp["tas"][:, 0, 0],
13    kind="+",
14)
Apply the Quantile Delta Mapping bias correction technique on 3-dimensional data
 1import xarray as xr
 2from cmethods import adjust
 3
 4obsh = xr.open_dataset("input_data/observations.nc")
 5simh = xr.open_dataset("input_data/control.nc")
 6simp = xr.open_dataset("input_data/scenario.nc")
 7
 8qdm_result = adjust(
 9    method="quantile_delta_mapping",
10    obs=obsh["tas"],
11    simh=simh["tas"],
12    simp=simp["tas"],
13    n_quaniles=1000,
14    kind="+",
15)

Advanced Usage

In some cases the time dimension of input data sets have different sizes. In such case, the hidden parameter input_core_dims must be passed to the adjust call.

It defines the dimension names of the input data sets, i.e. if the time dimensions of obs and simp have the length, but the time dimension of simh is somewhat smaller, you have to define this as follows:

Bias Adjustments for data sets with different time dimension lengths pt. 1
 1from cmethods import adjust
 2import xarray as xr
 3
 4obs = xr.open_dataset("examples/input_data/observations.nc")["tas"]
 5simp = xr.open_dataset("examples/input_data/control.nc")["tas"]
 6simh = simp.copy(deep=True)[3650:]
 7
 8bc = adjust(
 9    method="quantile_mapping",
10    obs=obs,
11    simh=simh.rename({"time": "t_simh"}),
12    simp=simh,
13    kind="+",
14    input_core_dims={"obs": "time", "simh": "t_simh", "simp": "time"}
15)

In case you are applying a scaling based technique using grouping, you have to adjust the group names accordingly to the time dimension names.

Bias Adjustments for data sets with different time dimension lengths pt. 2
 1from cmethods import adjust
 2import xarray as xr
 3
 4obs = xr.open_dataset("examples/input_data/observations.nc")["tas"]
 5simp = xr.open_dataset("examples/input_data/control.nc")["tas"]
 6simh = simp.copy(deep=True)[3650:]
 7
 8bc = adjust(
 9    method="linear_scaling",
10    obs=obs,
11    simh=simh.rename({"time": "t_simh"}),
12    simp=simh,
13    kind="+",
14    group={"obs": "time.month", "simh": "t_simh.month", "simp": "time.month"},
15    input_core_dims={"obs": "time", "simh": "t_simh", "simp": "time"}
16)