Skip to content

Only use distance dimensions by default in laplacian for DataArrays - #4163

Open
bhaskargurram-ai wants to merge 1 commit into
Unidata:mainfrom
bhaskargurram-ai:fix/laplacian-length-dims
Open

bhaskargurram-ai wants to merge 1 commit into
Unidata:mainfrom
bhaskargurram-ai:fix/laplacian-length-dims

Conversation

@bhaskargurram-ai

Copy link
Copy Markdown

Description Of Changes

Called on a DataArray with no axes, coordinates or deltas, laplacian adds up the second derivative along every dimension. On a typical model or analysis field with a pressure or time dimension this fails with an error that is hard to read, e.g. for (isobaric, lat, lon) data:

DimensionalityError: Cannot convert from 'kelvin / hectopascal ** 2' ... to 'kelvin / meter ** 2'

Following the proposal in #3105, when f is a DataArray and none of axes, coordinates and deltas are given, laplacian now only uses the dimensions along which positions are distances:

  • latitude and longitude, which already go through grid deltas in second_derivative
  • dimensions whose coordinate has units of length (projection x/y, or a height vertical coordinate)

Time, pressure and other dimensions are skipped. If no dimension qualifies, it uses all of them as before, so anything that worked before gives the same result (it could only work if all the dimensions had the same units). Explicit axes are used as given. I chose to skip pressure as well as time, since a sum of second derivatives in hPa and in m isn't meaningful. The docstring describes the new default.

Tests added in tests/calc/test_calc_tools.py:

  • (isobaric, lat, lon) and (time, isobaric, y, x) DataArrays with no axes now match axes=('lat', 'lon') / axes=('y', 'x'). Both fail on main with the error above.
  • a height (km) vertical coordinate is still included, and a DataArray without any distance dimension still uses all of them (both pass before and after, as guards).

tests/calc passes with xarray 2025.10.1. Ruff and flake8 are clean.

Checklist

When given a DataArray without axes, coordinates or deltas, laplacian
summed the second derivative along every dimension. For data with a
time or pressure dimension, as is usual for model and analysis fields,
this raised a hard to decipher DimensionalityError.

By default, only use the dimensions along which positions are
distances: latitude and longitude (converted with grid deltas) and
dimensions whose coordinate has units of length. If there are none,
keep using all dimensions as before.

Fixes Unidata#3105
@bhaskargurram-ai
bhaskargurram-ai requested a review from a team as a code owner October 18, 2026 03:57
@bhaskargurram-ai
bhaskargurram-ai requested review from dcamron and removed request for a team October 18, 2026 03:57
@CLAassistant

CLAassistant commented Oct 18, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

laplacian should skip non-length dimensions

2 participants