easyclimate.interp.mesh2point

Interpolating mesh/grid data to point locations

This module provides functionality to interpolate values from a regular grid/mesh (e.g., climate model output, remote sensing data) to specific point locations (e.g., weather stations, observation points) using various interpolation methods.

Functions

interp_mesh2point(data_input, df[, lon_dim_mesh, ...])

Interpolate values from a regular grid/mesh to specific point locations.

interp_mesh2point_withtime(→ xarray.DataArray)

Interpolate gridded data to specific point locations with time series preservation.

Module Contents

easyclimate.interp.mesh2point.interp_mesh2point(data_input: xarray.DataArray, df: pandas.DataFrame, lon_dim_mesh: str = 'lon', lat_dim_mesh: str = 'lat', lon_dim_df: str = 'lon', lat_dim_df: str = 'lat', method: Literal['linear', 'nearest', 'slinear', 'cubic', 'quintic', 'pchip'] = 'linear')

Interpolate values from a regular grid/mesh to specific point locations.

Parameters

data_inputxarray.DataArray.

Input grid data with latitude and longitude dimensions.

dfpandas.DataFrame.

DataFrame containing point locations with latitude and longitude columns.

lon_dim_meshstr, optional

Name of the longitude dimension in the input grid, by default "lon".

lat_dim_meshstr, optional

Name of the latitude dimension in the input grid, by default "lat".

lon_dim_dfstr, optional

Name of the longitude column in the DataFrame, by default "lon".

lat_dim_dfstr, optional

Name of the latitude column in the DataFrame, by default "lat".

methodstr, optional

Interpolation method to use. Options are:

  • "linear": bilinear interpolation (default)

  • "nearest": nearest neighbor

  • "slinear": spline linear

  • "cubic": spline cubic

  • "quintic": spline quintic

  • "pchip": piecewise cubic Hermite interpolating polynomial

Returns

pandas.DataFrame.

Original DataFrame with an additional column "interpolated_value" containing the interpolated values. Points outside the grid range will have NaN values.

Raises

ValueError

If no points in the DataFrame fall within the grid’s spatial extent.

Examples

>>> import xarray as xr
>>> import pandas as pd
>>> # Create sample grid data
>>> lats = np.linspace(-90, 90, 181)
>>> lons = np.linspace(-180, 180, 361)
>>> data = xr.DataArray(np.random.rand(181, 361), dims=['lat', 'lon'],
...                     coords={'lat': lats, 'lon': lons})
>>> # Create sample points
>>> points = pd.DataFrame({'lat': [45.5, 30.2], 'lon': [-120.3, 150.7]})
>>> # Interpolate
>>> result = interp_mesh2point(data, points)
easyclimate.interp.mesh2point.interp_mesh2point_withtime(data_input: xarray.DataArray, stations_df: pandas.DataFrame, lon_dim_df: str, lat_dim_df: str, station_dim_df: str, lon_dim_mesh: str = 'lon', lat_dim_mesh: str = 'lat', time_dim_mesh: str = 'time', method: Literal['linear', 'nearest', 'slinear', 'cubic', 'quintic', 'pchip'] = 'linear') xarray.DataArray

Interpolate gridded data to specific point locations with time series preservation.

Parameters

data_inputxarray.DataArray.

Input grid data with time, latitude and longitude dimensions.

stations_dfpandas.DataFrame.

DataFrame containing station locations with ID, latitude and longitude.

lon_dim_dfstr.

Name of the longitude column in the DataFrame.

lat_dim_dfstr.

Name of the latitude column in the DataFrame.

station_dim_dfstr.

Name of the station ID column in the DataFrame.

lon_dim_meshstr, default: “lon”

Name of the longitude dimension in the input grid.

lat_dim_meshstr, default: “lat”

Name of the latitude dimension in the input grid.

time_dim_meshstr, default: “time”

Name of the time dimension in the input grid.

methodstr, optional

Interpolation method:

  • “linear”: bilinear interpolation (default)

  • “nearest”: nearest neighbor

  • “slinear”: spline linear

  • “cubic”: spline cubic

  • “quintic”: spline quintic

  • “pchip”: piecewise cubic Hermite interpolating polynomial

Returns

xarray.Dataset.

xarray.Dataset with dimensions (station, time) containing:

  • Interpolated values

  • Station coordinates

Examples

>>> import xarray as xr
>>> import pandas as pd
>>> import numpy as np
>>> # Create sample grid
>>> times = pd.date_range("2020-01-01", periods=5)
>>> lats = np.linspace(-45, -10, 100)
>>> lons = np.linspace(110, 156, 120)
>>> data = xr.DataArray(
...     np.random.rand(5, 100, 120),
...     dims=["time", "lat", "lon"],
...     coords={"time": times, "lat": lats, "lon": lons}
... )
>>> # Create stations
>>> stations = pd.DataFrame({
...     "station_id_col": [1001, 1002],
...     "lat_col": [-15.5, -20.3],
...     "lon_col": [125.5, 130.2]
... })
>>> # Interpolate
>>> result = interp_mesh2point_withtime(
...     data, stations,
...     lon_dim_df="lon_col", lat_dim_df="lat_col", station_dim_df="station_id_col"
... )