{ "cells": [ { "cell_type": "markdown", "id": "205a15d4-e3b1-420d-aa95-0b64e829e297", "metadata": {}, "source": [ "# Effect of rounding on coordinates with Xarray\n", "**Claire Carouge, CLEX CMS**\n", "\n", "This notebook demonstrates how to work with two datasets that should have the same dimensional coordinates, but for whatever reason don’t.\n", "\n", "When doing operations involving the 2 DataArrays, Xarray will either return the results on the common locations only or return an indexing error. The solution is then to replace one set of coordinates with the other set.\n", "\n", "## The problem" ] }, { "cell_type": "markdown", "id": "4e53cc1f-2500-4576-8713-8368151a21cc", "metadata": {}, "source": [ "To demonstrate, we create two DataArrays that have a slightly different coordinate array. Note how the difference is so small that Jupyter’s default presentation obscures it by rounding." ] }, { "cell_type": "code", "execution_count": 1, "id": "4cec3123-ef3f-424f-80a4-f1eb7d9335fc", "metadata": {}, "outputs": [], "source": [ "import xarray as xr\n", "import numpy as np" ] }, { "cell_type": "code", "execution_count": 2, "id": "8451bb26-afd0-4914-8508-284b68b136b5", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
<xarray.DataArray (x: 3)>\n", "array([1, 2, 3])\n", "Coordinates:\n", " * x (x) float64 1.1 2.1 3.1
array([1, 2, 3])
array([1.1, 2.1, 3.1])
<xarray.DataArray (x: 3)>\n", "array([1, 2, 3])\n", "Coordinates:\n", " * x (x) float64 1.1 2.1 3.1
array([1, 2, 3])
array([1.1, 2.1, 3.1])
<xarray.DataArray (x: 2)>\n", "array([0, 0])\n", "Coordinates:\n", " * x (x) float64 1.1 3.1
array([0, 0])
array([1.1, 3.1])
<xarray.DataArray (x: 3)>\n", "array([0, 0, 0])\n", "Coordinates:\n", " * x (x) float64 1.1 2.1 3.1
array([0, 0, 0])
array([1.1, 2.1, 3.1])
<xarray.DataArray (x: 3)>\n", "array([1., 2., 3.])\n", "Coordinates:\n", " * x (x) float64 1.1 2.1 3.1
array([1., 2., 3.])
array([1.1, 2.1, 3.1])
<xarray.DataArray (x: 3)>\n", "array([ 0.00000000e+00, -1.00000008e-09, 0.00000000e+00])\n", "Coordinates:\n", " * x (x) float64 1.1 2.1 3.1
array([ 0.00000000e+00, -1.00000008e-09, 0.00000000e+00])
array([1.1, 2.1, 3.1])