The climpact stand alone package

climapct is both a freva plugin and a stand alone library that can be used to process climate data in order to select regions defined by coordinates stored in geo reference data, like shape or geojson files.

Installation

The best method to install the library is cloning the repository and creating a conda environment:

git clone https://gitlab.dkrz.de/ch1187/plugins4freva/climpact.git
cd climpact
conda env create -f conda-env.yml -n climpact

These commands would create a fresh conda environment named climpact after activating the environment

conda activate climpact

You can make use of the stand alone library.

The RunDirectory class is a class for reading data.

It offers easy control over selecting complex regions defined in geo-reference datasets like shape or geojson files. See also the Api Reference section for more details.

Parameters

files: list[str]

List of files that should be opened by the reader

variables: list[str]

list variables that should be considered

shape_file: str, default: None

Path to the shape file that is used to define the mask region. If None given (default) no masking will be applied.

region: str, default: “”

Select the name of a sub region within the shape file. If None given (default) the whole geometry defined in the shape file is taken.

mask_method: str, default: centres

String representing the method how the masked region (if given) should be applied. The string can either be centres or corners. Where centres selects only those grid-boxes that have the grid-box centres within the coordinates of the mask region. corners will select grid-boxes that have at least one of the four corners of the grid-boxes within the coordinates of the mask region. This means that corners, compared to centres will slightly increase the selected area.

mask: tuple[str, int], default: None

If additionally a land or sea region should be mask this variable can be used to set the path to a land-sea mask file (first entry in the tuple) and the type of the mask (second entry - 0: land, 1: sea).

abort_on_error: bool, default: False

Exit if something goes wrong while loading the dataset

reindex: bool, default: True

Apply nearest neighbor re-indexing to a larger grid in order to achieve a more precise region selection.

kwargs:

Additional information about the run

Examples

First we read a dataset without applying a mask at all

from climpact import RunDirectory
rd = RundDirectory(["~/orog.nc"], ["orog"])
print(rd.variables)
['orog']
print(type(rd.dataset))
<class 'xarray.core.dataset.Dataset'>
print(rd.dataset["orog"])
<xarray.DataArray 'orog' (rlat: 412, rlon: 424)>
dask.array<mul, shape=(412, 424), dtype=float64, chunksize=(412, 424), chunktype=numpy.ndarray>
Coordinates:
    lon      (rlat, rlon) float64 dask.array<chunksize=(412, 424), meta=np.ndarray>
    lat      (rlat, rlon) float64 dask.array<chunksize=(412, 424), meta=np.ndarray>
  * rlon     (rlon) float64 -28.38 -28.26 -28.16 -28.05 ... 17.93 18.05 18.16
  * rlat     (rlat) float64 -23.38 -23.26 -23.16 -23.05 ... 21.61 21.73 21.83
    Y        (rlat, rlon) float64 21.99 22.03 22.07 22.11 ... 66.81 66.75 66.69
    X        (rlat, rlon) float64 -10.06 -9.964 -9.864 ... 64.55 64.76 64.96
Attributes:
    standard_name:  surface_altitude
    long_name:      Surface Altitude
    units:          m
    grid_mapping:   rotated_pole

Now let’s apply a mask defined in a shape file

from climpact import RunDirectory
rd = RundDirectory(["~/orog.nc"], ["orog"], shape_file="Germany.shp")
print(rd.dataset["orog"])
<xarray.DataArray 'orog' (rlat: 365, rlon: 275)>
dask.array<mul, shape=(365, 275), dtype=float64, chunksize=(365, 275), chunktype=numpy.ndarray>
Coordinates:
  * rlon     (rlon) float64 -7.695 -7.673 -7.652 -7.63 ... -1.798 -1.777 -1.755
  * rlat     (rlat) float64 -3.245 -3.223 -3.201 -3.18 ... 4.631 4.653 4.675
    lon      (rlat, rlon) float64 dask.array<chunksize=(365, 275), meta=np.ndarray>
    lat      (rlat, rlon) float64 dask.array<chunksize=(365, 275), meta=np.ndarray>
    Y        (rlat, rlon) float64 46.92 46.92 46.92 46.93 ... 55.39 55.39 55.39
    X        (rlat, rlon) float64 6.713 6.745 6.776 6.807 ... 14.84 14.88 14.92
Attributes:
    standard_name:  surface_altitude
    long_name:      Surface Altitude
    units:          m
    grid_mapping:   rotated_pole

Finally we can only select a sub region in the shape file by giving the key or index to the sub region.

from climpact import RunDirectory
rd = RundDirectory(["~/orog.nc"],
                   ["orog"],
                   shape_file="Germany.shp",
                   region="Schaumburg",
                   )
print(rd.dataset["orog"])
<xarray.DataArray 'orog' (rlat: 25, rlon: 25)>
dask.array<mul, shape=(25, 25), dtype=float64, chunksize=(25, 25), chunktype=numpy.ndarray>
Coordinates:
  * rlon     (rlon) float64 -5.605 -5.587 -5.568 -5.55 ... -5.202 -5.183 -5.165
  * rlat     (rlat) float64 1.595 1.613 1.632 1.65 ... 1.98 1.998 2.017 2.035
    lon      (rlat, rlon) float64 dask.array<chunksize=(25, 25), meta=np.ndarray>
    lat      (rlat, rlon) float64 dask.array<chunksize=(25, 25), meta=np.ndarray>
    Y        (rlat, rlon) float64 52.0 52.0 52.0 52.01 ... 52.48 52.49 52.49
    X        (rlat, rlon) float64 8.876 8.905 8.935 8.964 ... 9.444 9.474 9.504
Attributes:
    standard_name:  surface_altitude
    long_name:      Surface Altitude
    units:          m
    grid_mapping:   rotated_pole