earthkit.data.readers.xarray.patch

Attributes

Functions

patch_analysis_lead_to_valid_time(ds, time_coord_names)

Convert analysis time and lead time coordinates to valid time.

patch_attributes(ds, attributes)

Patch the attributes of the dataset.

patch_coordinates(ds, coordinates)

Patch the coordinates of the dataset.

patch_dataset(ds, patch)

Patch the dataset.

patch_rename(ds, renames)

Rename variables in the dataset.

patch_rolling_operation(ds, vars_operation_config)

Apply a rolling operation to specified variables in the dataset.

patch_sort_coordinate(ds, sort_coordinates)

Sort the coordinates of the dataset.

patch_subset_dataset(ds, selection)

Select a subset of the dataset using xarray's sel method.

Module Contents

earthkit.data.readers.xarray.patch.PATCHES
earthkit.data.readers.xarray.patch.patch_analysis_lead_to_valid_time(ds, time_coord_names)

Convert analysis time and lead time coordinates to valid time.

This function creates a new valid time coordinate by adding the analysis time and lead time coordinates, then stacks and reorganises the dataset to use valid time as the primary time dimension.

Parameters:
  • ds (xr.Dataset) – The dataset to patch.

  • time_coord_names (dict[str, str]) –

    Dictionary mapping required keys to coordinate names in the dataset:

    • ’analysis_time_coordinate’str

      Name of the analysis/initialisation time coordinate.

    • ’lead_time_coordinate’str

      Name of the forecast lead time coordinate.

    • ’valid_time_coordinate’str

      Name for the new valid time coordinate to create.

Returns:

The patched dataset with valid time as the primary time coordinate. The analysis and lead time coordinates are removed.

Return type:

xr.Dataset

Examples

>>> patch_analysis_lead_to_valid_time(
...     ds,
...     {
...         "analysis_time_coordinate": "forecast_reference_time",
...         "lead_time_coordinate": "step",
...         "valid_time_coordinate": "time",
...     },
... )
earthkit.data.readers.xarray.patch.patch_attributes(ds, attributes)

Patch the attributes of the dataset.

Parameters:
  • ds (xr.Dataset) – The dataset to patch.

  • attributes (Dict[str, Dict[str, Any]]) – The attributes to patch.

Returns:

The patched dataset.

Return type:

Any

earthkit.data.readers.xarray.patch.patch_coordinates(ds, coordinates)

Patch the coordinates of the dataset.

Parameters:
  • ds (xr.Dataset) – The dataset to patch.

  • coordinates (List[str]) – The coordinates to patch.

Returns:

The patched dataset.

Return type:

Any

earthkit.data.readers.xarray.patch.patch_dataset(ds, patch)

Patch the dataset.

Parameters:
  • ds (xr.Dataset) – The dataset to patch.

  • patch (Dict[str, Dict[str, Any]]) – The patch to apply.

Returns:

The patched dataset.

Return type:

Any

earthkit.data.readers.xarray.patch.patch_rename(ds, renames)

Rename variables in the dataset.

Parameters:
  • ds (xr.Dataset) – The dataset to patch.

  • renames (dict[str, str]) – Mapping from old variable names to new variable names.

Returns:

The patched dataset.

Return type:

Any

earthkit.data.readers.xarray.patch.patch_rolling_operation(ds, vars_operation_config)

Apply a rolling operation to specified variables in the dataset.

This function calculates a rolling operation over a specified dimension for selected variables. The rolling window requires all periods to be present (min_periods=steps).

Parameters:
  • ds (xr.Dataset) – The dataset to patch.

  • vars_operation_config (dict) –

    Configuration for the rolling operation with the following keys:

    • ’dim’str

      The dimension along which to apply the rolling operation (e.g., ‘time’).

    • ’steps’int

      The number of steps in the rolling window.

    • ’vars’list[str]

      List of variable names to apply the rolling operation to.

    • ’operation’str

      The operation to apply (‘sum’, ‘mean’, ‘min’, ‘max’, ‘std’, etc.).

Returns:

The patched dataset with rolling operations applied to the specified variables.

Return type:

xr.Dataset

Examples

>>> patch_rolling_operation(
...     ds,
...     {
...         "dim": "time",
...         "steps": 3,
...         "vars": ["precipitation", "radiation"],
...         "operation": "sum",
...     },
... )
earthkit.data.readers.xarray.patch.patch_sort_coordinate(ds, sort_coordinates)

Sort the coordinates of the dataset.

Parameters:
  • ds (xr.Dataset) – The dataset to patch.

  • sort_coordinates (List[str]) – The coordinates to sort.

Returns:

The patched dataset.

Return type:

Any

earthkit.data.readers.xarray.patch.patch_subset_dataset(ds, selection)

Select a subset of the dataset using xarray’s sel method.

Parameters:
  • ds (xr.Dataset) – The dataset to patch.

  • selection (dict[str, Any]) – Dictionary mapping dimension names to selection criteria. Keys must be existing dimension names in the dataset. Values can be any type accepted by xarray’s sel method, including: - Single values (int, float, str, datetime) - Lists or arrays of values - Slices (using slice() objects) - Boolean arrays

Returns:

The patched dataset containing only the selected subset.

Return type:

xr.Dataset

Examples

>>> # Select specific time and pressure level
>>> patch_subset_dataset(ds, {"time": "2020-01-01", "pressure": 500})
>>> # Select a range using slice
>>> patch_subset_dataset(ds, {"lat": slice(-90, 90), "lon": slice(0, 180)})