Home / Market / xarray coordinate alignment

xarray coordinate alignment

Diagnose xarray coordinate mismatches and choose explicit alignment before comparing or merging labeled scientific datasets, including weather and climate grids.

skill★ 507610 dlgithubCC-BY-4.0 content; MIT code; upstream exceptions applyInstall in CosmosUpstream Source

Documentation & Preview

--- name: xarray-coordinate-alignment description: "Diagnose xarray coordinate mismatches and choose explicit alignment before comparing or merging labeled scientific datasets, including weather and climate grids." category: data risk: safe source: self source_type: self date_added: "2026-11-30" author: ShianMike tags: [xarray, scientific-data, coordinates, weather, netcdf]

---

Xarray Coordinate Alignment

When to Use

Use weather-model-data-fetching to obtain model inputs and weather-data-reproducibility to record provenance. This workflow starts with loaded or lazily opened xarray objects.

Inspect the coordinates

  1. Identify the variables, intended calculation, and dimensions that should match. Check .dims, .sizes, .coords, and .xindexes on each input. Confirm that names such as time, valid_time, and step represent the intended axes before renaming. Different dimension names can broadcast and create extra axes.
  2. For each intended shared index, check uniqueness, label order, and the labels present on only one side. An unlabeled dimension needs an independently verified positional mapping. Equal dimension lengths alone are insufficient.
  3. Check value units and coordinate semantics. For time, verify the reference time, calendar, and whether a label denotes an instant or interval. For a grid, verify coordinate units, longitude convention, CRS, and any auxiliary latitude and longitude arrays. On a curvilinear grid, identical y and x indexes can still correspond to different locations.

Choose the alignment policy

| Intended coverage | Operation | What to verify | | --- | --- | --- | | Same indexed labels in the same order | xr.align(a, b, join="exact", copy=False) | Inspect the mismatch if alignment raises. | | Shared labels only | xr.align(a, b, join="inner", copy=False) | Count labels dropped from each input and flag an empty overlap. | | All labels from both inputs | xr.align(a, b, join="outer", copy=False) | Count introduced gaps and retain missing values unless the calculation defines a fill. | | One input supplies the target labels | xr.align(a, b, join="left", copy=False) for a | Identify any target labels missing from the other input. |

Use explicit alignment before arithmetic. For adding variables from same-grid datasets, pass join="exact" to xr.merge and choose compat and combine_attrs explicitly. Use compat="no_conflicts" for complementary missing values only after confirming that overlapping variables have the same meaning and units. Conflicting nonmissing values require a resolution supported by the source data.

For different grids or sampling times, establish the intended interpolation, regridding, or calendar conversion method before proceeding. Record the target, tolerance or method, and resulting coverage.

Example

Compare two synthetic daily series. The requested result retains all days from both inputs and calculates simulated minus observed only where both values exist.

import numpy as np
import xarray as xr

observed = xr.DataArray(
    [10.0, 12.0], dims="time",
    coords={"time": np.array(["2026-01-01", "2026-01-02"], dtype="datetime64[D]")},
)
simulated = xr.DataArray(
    [11.0, 13.0], dims="time",
    coords={"time": np.array(["2026-01-02", "2026-01-03"], dtype="datetime64[D]")},
)

observed, simulated = xr.align(observed, simulated, join="outer", copy=False)
difference = simulated - observed

np.testing.assert_array_equal(
    difference["time"].values.astype("datetime64[D]"),
    np.array(["2026-01-01", "2026-01-02", "2026-01-03"], dtype="datetime64[D]"),
)
np.testing.assert_allclose(difference.values, [np.nan, -1.0, np.nan])

Verify and report

Limitations

Safety and side effects

Use xarray in the existing Python environment with authorized inputs. The example uses NumPy and xarray in memory, with no accounts, network calls, or file writes. Accessing lazy values can read from the original backend, so use a bounded slice from the authorized source.

Sources