Compute climate index TG (‘Mean daily mean temperature’). The computation in the back is taken from the python module xclim

[1]:
%load_ext autoreload
%autoreload 2
[2]:
import warnings

warnings.filterwarnings("ignore")
[3]:
from pyhomogenize import open_xrdataset

import index_calculator as xcalc

Select one CORDEX tas file and open it with xarray.

[4]:
ifile = "tas_EUR-11_MPI-M-MPI-ESM-LR_historical_r3i1p1_GERICS-REMO2015_v1_day_20010101-20051231.nc"
[5]:
ds = open_xrdataset(ifile)
ds
[5]:
<xarray.Dataset>
Dimensions:                     (time: 1826, bnds: 2, rlat: 412, rlon: 424,
                                 vertices: 4)
Coordinates:
  * time                        (time) object 2001-01-01 12:00:00 ... 2005-12...
  * rlat                        (rlat) float64 -23.38 -23.27 ... 21.72 21.84
  * rlon                        (rlon) float64 -28.38 -28.27 ... 18.04 18.16
    lat                         (rlat, rlon) float32 dask.array<chunksize=(412, 424), meta=np.ndarray>
    lon                         (rlat, rlon) float32 dask.array<chunksize=(412, 424), meta=np.ndarray>
    height                      float64 ...
Dimensions without coordinates: bnds, vertices
Data variables:
    time_bnds                   (time, bnds) object dask.array<chunksize=(1, 2), meta=np.ndarray>
    rotated_latitude_longitude  int32 ...
    lat_vertices                (rlat, rlon, vertices) float32 dask.array<chunksize=(412, 424, 4), meta=np.ndarray>
    lon_vertices                (rlat, rlon, vertices) float32 dask.array<chunksize=(412, 424, 4), meta=np.ndarray>
    tas                         (time, rlat, rlon) float32 dask.array<chunksize=(1, 412, 424), meta=np.ndarray>
Attributes: (12/35)
    institution:                    Helmholtz-Zentrum Geesthacht, Climate Ser...
    institute_id:                   GERICS
    experiment_id:                  historical
    source:                         GERICS-REMO2015
    model_id:                       GERICS-REMO2015
    forcing:                        N/A
    ...                             ...
    parent_experiment:              N/A
    modeling_realm:                 atmos
    realization:                    3
    cmor_version:                   2.9.1
    tracking_id:                    hdl:21.14103/55a844fd-666d-4799-8563-8fbd...
    CF_variables:                   ['tas']

Now do a preprocessing of the dataset and create an index_calculator.PreProcessing object. Preprocessor tasks are:

  • select a user-given time range (parameter: time_range)

  • check time axis for missing, redundant and duplicated time steps

  • crop time axis to required left and right border

  • write available and requested time range to object

[6]:
preproc = xcalc.preprocessing(ds)
preproc.preproc
[6]:
<xarray.Dataset>
Dimensions:                     (time: 1826, bnds: 2, rlat: 412, rlon: 424,
                                 vertices: 4)
Coordinates:
  * time                        (time) object 2001-01-01 12:00:00 ... 2005-12...
  * rlat                        (rlat) float64 -23.38 -23.27 ... 21.72 21.84
  * rlon                        (rlon) float64 -28.38 -28.27 ... 18.04 18.16
    lat                         (rlat, rlon) float32 dask.array<chunksize=(412, 424), meta=np.ndarray>
    lon                         (rlat, rlon) float32 dask.array<chunksize=(412, 424), meta=np.ndarray>
    height                      float64 ...
Dimensions without coordinates: bnds, vertices
Data variables:
    time_bnds                   (time, bnds) object dask.array<chunksize=(1, 2), meta=np.ndarray>
    rotated_latitude_longitude  int32 ...
    lat_vertices                (rlat, rlon, vertices) float32 dask.array<chunksize=(412, 424, 4), meta=np.ndarray>
    lon_vertices                (rlat, rlon, vertices) float32 dask.array<chunksize=(412, 424, 4), meta=np.ndarray>
    tas                         (time, rlat, rlon) float32 dask.array<chunksize=(1, 412, 424), meta=np.ndarray>
Attributes: (12/35)
    institution:                    Helmholtz-Zentrum Geesthacht, Climate Ser...
    institute_id:                   GERICS
    experiment_id:                  historical
    source:                         GERICS-REMO2015
    model_id:                       GERICS-REMO2015
    forcing:                        N/A
    ...                             ...
    parent_experiment:              N/A
    modeling_realm:                 atmos
    realization:                    3
    cmor_version:                   2.9.1
    tracking_id:                    hdl:21.14103/55a844fd-666d-4799-8563-8fbd...
    CF_variables:                   ['tas']

Now, use the above object to compute the climate index TG.

[7]:
proc = xcalc.processing("TG", preproc_obj=preproc)
proc.proc
[7]:
<xarray.Dataset>
Dimensions:                     (rlat: 412, rlon: 424, vertices: 4, time: 5,
                                 bounds: 2)
Coordinates:
    height                      float64 2.0
  * rlat                        (rlat) float64 -23.38 -23.27 ... 21.72 21.84
  * rlon                        (rlon) float64 -28.38 -28.27 ... 18.04 18.16
    lat                         (rlat, rlon) float32 21.99 22.03 ... 66.75 66.69
    lon                         (rlat, rlon) float32 349.9 350.0 ... 64.76 64.96
  * time                        (time) object 2001-07-03 00:00:00 ... 2005-07...
Dimensions without coordinates: vertices, bounds
Data variables:
    rotated_latitude_longitude  int32 ...
    lat_vertices                (rlat, rlon, vertices) float32 dask.array<chunksize=(412, 424, 4), meta=np.ndarray>
    lon_vertices                (rlat, rlon, vertices) float32 dask.array<chunksize=(412, 424, 4), meta=np.ndarray>
    TG                          (time, rlat, rlon) float32 dask.array<chunksize=(1, 412, 424), meta=np.ndarray>
    time_bnds                   (time, bounds) object 2001-01-01 12:00:00 ......
Attributes: (12/35)
    institution:                    Helmholtz-Zentrum Geesthacht, Climate Ser...
    institute_id:                   GERICS
    experiment_id:                  historical
    source:                         GERICS-REMO2015
    model_id:                       GERICS-REMO2015
    forcing:                        N/A
    ...                             ...
    parent_experiment:              N/A
    modeling_realm:                 atmos
    realization:                    3
    cmor_version:                   2.9.1
    tracking_id:                    hdl:21.14103/55a844fd-666d-4799-8563-8fbd...
    CF_variables:                   ['tas']

Finally, do some postprocessing. The postprocessor writes some additional attributes to ds’ global attributes.

[8]:
postproc = xcalc.postprocessing(
    project="CORDEX",
    proc_obj=proc,
    institution="Helmholtz-Zentrum hereon GmbH, Climate Service Center Germany",
    institution_id="GERICS",
)
postproc.postproc
[8]:
<xarray.Dataset>
Dimensions:                     (rlat: 412, rlon: 424, vertices: 4, time: 5,
                                 bounds: 2)
Coordinates:
    height                      float64 2.0
  * rlat                        (rlat) float64 -23.38 -23.27 ... 21.72 21.84
  * rlon                        (rlon) float64 -28.38 -28.27 ... 18.04 18.16
    lat                         (rlat, rlon) float32 21.99 22.03 ... 66.75 66.69
    lon                         (rlat, rlon) float32 349.9 350.0 ... 64.76 64.96
  * time                        (time) object 2001-07-03 00:00:00 ... 2005-07...
Dimensions without coordinates: vertices, bounds
Data variables:
    rotated_latitude_longitude  int32 ...
    lat_vertices                (rlat, rlon, vertices) float32 dask.array<chunksize=(412, 424, 4), meta=np.ndarray>
    lon_vertices                (rlat, rlon, vertices) float32 dask.array<chunksize=(412, 424, 4), meta=np.ndarray>
    TG                          (time, rlat, rlon) float32 dask.array<chunksize=(1, 412, 424), meta=np.ndarray>
    time_bnds                   (time, bounds) object 2001-01-01 12:00:00 ......
Attributes: (12/45)
    institution:                    Helmholtz-Zentrum Geesthacht, Climate Ser...
    institute_id:                   GERICS
    experiment_id:                  historical
    source:                         GERICS-REMO2015
    model_id:                       GERICS-REMO2015
    forcing:                        N/A
    ...                             ...
    ci_name:                        TG
    ci_frequency:                   year
    ci_timerange_index:             2001-2005
    ci_timerange_source:            20010101-20051231
    ci_package_name:                xclim_0.37.0
    ci_package_reference:           xcalc_0.3.2

Optionaly, we can store the dataset as a netCDF file on disk. The output file naming depends on the user-given project.

[9]:
xcalc.outputwriter(postproc_obj=postproc)
File written: ./cordex/climdex/EUR-11/GERICS/GERICS/MPI-M-MPI-ESM-LR/historical/r3i1p1/GERICS-REMO2015/v1/year/TG/TG_EUR-11_MPI-M-MPI-ESM-LR_historical_r3i1p1_GERICS-REMO2015_v1_day_GERICS_year_2001-2005.nc
[9]:
<index_calculator.OutputWriter at 0x7fff8dd41d20>

For simplyfication, we can do preprocessing, processing, postprocessing and output file storing by calling just one function.

[10]:
idx_obj = xcalc.index_calculator(
    ds=ds,
    index="TG",
    project="CORDEX",
    institution_id="GERICS",
    institution="Helmholtz-Zentrum hereon GmbH, Climate Service Center Germany",
    write=True,
)
File written: ./cordex/climdex/EUR-11/GERICS/GERICS/MPI-M-MPI-ESM-LR/historical/r3i1p1/GERICS-REMO2015/v1/year/TG/TG_EUR-11_MPI-M-MPI-ESM-LR_historical_r3i1p1_GERICS-REMO2015_v1_day_GERICS_year_2001-2005.nc
[11]:
idx_obj.postproc
[11]:
<xarray.Dataset>
Dimensions:                     (rlat: 412, rlon: 424, vertices: 4, time: 5,
                                 bounds: 2)
Coordinates:
    height                      float64 2.0
  * rlat                        (rlat) float64 -23.38 -23.27 ... 21.72 21.84
  * rlon                        (rlon) float64 -28.38 -28.27 ... 18.04 18.16
    lat                         (rlat, rlon) float32 21.99 22.03 ... 66.75 66.69
    lon                         (rlat, rlon) float32 349.9 350.0 ... 64.76 64.96
  * time                        (time) object 2001-07-03 00:00:00 ... 2005-07...
Dimensions without coordinates: vertices, bounds
Data variables:
    rotated_latitude_longitude  int32 ...
    lat_vertices                (rlat, rlon, vertices) float32 dask.array<chunksize=(412, 424, 4), meta=np.ndarray>
    lon_vertices                (rlat, rlon, vertices) float32 dask.array<chunksize=(412, 424, 4), meta=np.ndarray>
    TG                          (time, rlat, rlon) float32 dask.array<chunksize=(1, 412, 424), meta=np.ndarray>
    time_bnds                   (time, bounds) object 2001-01-01 12:00:00 ......
Attributes: (12/45)
    institution:                    Helmholtz-Zentrum Geesthacht, Climate Ser...
    institute_id:                   GERICS
    experiment_id:                  historical
    source:                         GERICS-REMO2015
    model_id:                       GERICS-REMO2015
    forcing:                        N/A
    ...                             ...
    ci_name:                        TG
    ci_frequency:                   year
    ci_timerange_index:             2001-2005
    ci_timerange_source:            20010101-20051231
    ci_package_name:                xclim_0.37.0
    ci_package_reference:           xcalc_0.3.2
[ ]: