{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Xarray" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "[](https://colab.research.google.com/github/davemlz/spyndex/blob/main/docs/tutorials/xarray.ipynb)\n", "" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Welcome to one of the best levels: Level 4 - `spyndex + xarray`!\n", "\n", "Remember to install `spyndex`!" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "!pip install -U spyndex" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now, let's start!\n", "\n", "First, import `spyndex` and `xarray`:" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "import spyndex\n", "import xarray as xr" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## `xarray.DataArray`\n", "\n", "If you're a Remote Sensing scientist, you have probably worked with `xarray` and `Datasets` or `DataArrays`.\n", "\n", "Well, well, well... You wnat to use `spyndex` with `xarray`, don't you?\n", "\n", "The answer: YES, it is possible!\n", "\n", "Let's use a `xarray.DataArray` that is stored in the `spyndex` datasets: `sentinel`:" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "da = spyndex.datasets.open(\"sentinel\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This data array is very simple. We have 3 dimensions: `band`, `x` and `y`. Each band is one of the 10 m spectral bands of a Sentinel-2 image." ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
<xarray.DataArray (band: 4, x: 300, y: 300)>\n",
"array([[[ 299, 276, 280, ..., 510, 516, 521],\n",
" [ 287, 285, 284, ..., 503, 476, 469],\n",
" [ 287, 292, 288, ..., 454, 411, 337],\n",
" ...,\n",
" [ 502, 508, 520, ..., 683, 670, 791],\n",
" [ 486, 518, 532, ..., 688, 696, 693],\n",
" [ 486, 506, 515, ..., 659, 671, 664]],\n",
"\n",
" [[ 469, 446, 466, ..., 695, 711, 728],\n",
" [ 469, 437, 469, ..., 683, 694, 666],\n",
" [ 460, 460, 460, ..., 628, 595, 527],\n",
" ...,\n",
" [ 804, 808, 832, ..., 920, 872, 1023],\n",
" [ 787, 803, 822, ..., 890, 882, 871],\n",
" [ 787, 799, 822, ..., 893, 832, 834]],\n",
"\n",
" [[ 319, 293, 328, ..., 1054, 1090, 1110],\n",
" [ 327, 318, 345, ..., 1044, 1004, 952],\n",
" [ 339, 355, 323, ..., 922, 784, 652],\n",
" ...,\n",
" [1528, 1516, 1516, ..., 1250, 1246, 1420],\n",
" [1470, 1502, 1498, ..., 1316, 1200, 1162],\n",
" [1394, 1480, 1472, ..., 1288, 1144, 1122]],\n",
"\n",
" [[2164, 2128, 2206, ..., 1796, 1837, 1816],\n",
" [2110, 2017, 2228, ..., 1795, 1839, 1788],\n",
" [2050, 2112, 2062, ..., 1816, 1789, 1864],\n",
" ...,\n",
" [1910, 1942, 1942, ..., 2105, 1898, 2102],\n",
" [1836, 1874, 1916, ..., 2075, 1792, 1747],\n",
" [1778, 1844, 1870, ..., 2087, 1830, 1675]]])\n",
"Coordinates:\n",
" * band (band) <U3 'B02' 'B03' 'B04' 'B08'\n",
"Dimensions without coordinates: x, yarray([[[ 299, 276, 280, ..., 510, 516, 521],\n",
" [ 287, 285, 284, ..., 503, 476, 469],\n",
" [ 287, 292, 288, ..., 454, 411, 337],\n",
" ...,\n",
" [ 502, 508, 520, ..., 683, 670, 791],\n",
" [ 486, 518, 532, ..., 688, 696, 693],\n",
" [ 486, 506, 515, ..., 659, 671, 664]],\n",
"\n",
" [[ 469, 446, 466, ..., 695, 711, 728],\n",
" [ 469, 437, 469, ..., 683, 694, 666],\n",
" [ 460, 460, 460, ..., 628, 595, 527],\n",
" ...,\n",
" [ 804, 808, 832, ..., 920, 872, 1023],\n",
" [ 787, 803, 822, ..., 890, 882, 871],\n",
" [ 787, 799, 822, ..., 893, 832, 834]],\n",
"\n",
" [[ 319, 293, 328, ..., 1054, 1090, 1110],\n",
" [ 327, 318, 345, ..., 1044, 1004, 952],\n",
" [ 339, 355, 323, ..., 922, 784, 652],\n",
" ...,\n",
" [1528, 1516, 1516, ..., 1250, 1246, 1420],\n",
" [1470, 1502, 1498, ..., 1316, 1200, 1162],\n",
" [1394, 1480, 1472, ..., 1288, 1144, 1122]],\n",
"\n",
" [[2164, 2128, 2206, ..., 1796, 1837, 1816],\n",
" [2110, 2017, 2228, ..., 1795, 1839, 1788],\n",
" [2050, 2112, 2062, ..., 1816, 1789, 1864],\n",
" ...,\n",
" [1910, 1942, 1942, ..., 2105, 1898, 2102],\n",
" [1836, 1874, 1916, ..., 2075, 1792, 1747],\n",
" [1778, 1844, 1870, ..., 2087, 1830, 1675]]])array(['B02', 'B03', 'B04', 'B08'], dtype='<U3')