Skip to content

nearest_neighbor

Classes:

Name Description
NearestNeighbor

Maps data onto the specified coordinate grid using nearest-neighbor.

Classes#

NearestNeighbor #

Bases: DataConverter

Maps data onto the specified coordinate grid using nearest-neighbor.

Methods:

Name Description
convert

Attributes:

Name Type Description
coord str

The coordinate axis this converter should be applied on. Defaults to 'time'.

Attributes#

coord class-attribute instance-attribute #
coord: str = 'time'

The coordinate axis this converter should be applied on. Defaults to 'time'.

Functions#

convert #
convert(
    data: xr.DataArray,
    variable_name: str,
    dataset_config: DatasetConfig,
    retrieved_dataset: RetrievedDataset,
    **kwargs: Any
) -> Optional[xr.DataArray]
Source code in tsdat/io/converters/nearest_neighbor.py
def convert(
    self,
    data: xr.DataArray,
    variable_name: str,
    dataset_config: DatasetConfig,
    retrieved_dataset: RetrievedDataset,
    **kwargs: Any,
) -> Optional[xr.DataArray]:
    # Assume that the coord index in the output matches coord index in the retrieved
    # structure.
    target_coord = retrieved_dataset.coords[self.coord]
    coord_index = dataset_config[variable_name].dims.index(self.coord)
    current_coord_name = tuple(data.coords.keys())[coord_index]

    # Create an empty DataArray with the shape we want to achieve
    new_coords = {
        k: v.data if k != current_coord_name else target_coord.data
        for k, v in data.coords.items()
    }
    tmp_data = xr.DataArray(coords=new_coords, dims=tuple(new_coords))  # type: ignore

    # Resample the data using nearest neighbor
    new_data = data.reindex_like(other=tmp_data, method="nearest")

    return new_data