{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Generating a Spectral Index Data Cube with Cubo and Spyndex\n",
    "## Introduction\n",
    "Using the cubo and spyndex library by David Montero Loaiza (Github: [davemlz](https://github.com/davemlz)), we can easily create spectral index data cubes from coordinates and visualize them."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import lexcube\n",
    "import numpy as np\n",
    "import xarray as xr\n",
    "import spyndex\n",
    "import cubo"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Using [cubo](https://github.com/ESDS-Leipzig/cubo), we can create a data cube from Sentinel 2 using STAC:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "bands_da = cubo.create(\n",
    "    lat=51, # Central latitude of the cube\n",
    "    lon=10, # Central longitude of the cube\n",
    "    collection=\"sentinel-2-l2a\", # Name of the STAC collection\n",
    "    bands=[\"B01\",\"B02\",\"B03\",\"B04\",\"B05\",\"B06\",\"B07\",\"B08\",\"B11\",\"B12\"], # Bands to retrieve\n",
    "    start_date=\"2022-01-25\", # Start date of the cube\n",
    "    end_date=\"2022-12-31\", # End date of the cube\n",
    "    edge_size=1024, # Edge size of the cube (px)\n",
    "    resolution=10, # Pixel size of the cube (m)\n",
    "    query={\"eo:cloud_cover\": {\"lt\": 10} }\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Now, we can use that data cube to compute any spectral index (in this case NDVI) using the [spyndex](https://github.com/awesome-spectral-indices/spyndex) library:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "da = spyndex.computeIndex(\n",
    "    index = [\"NDVI\"],\n",
    "    params = {\n",
    "        \"N\": bands_da.sel(band = \"B08\"),\n",
    "        \"R\": bands_da.sel(band = \"B04\"),\n",
    "        \"L\": 0.5\n",
    "    }\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Finally, we can visualize the NDVI cube with Lexcube:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "w = lexcube.Cube3DWidget(da)\n",
    "w"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Afterwards, it is possible to parametrize the visualization further or save the figure as a PNG:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# ** Save the figure as PNG:\n",
    "# w.savefig()\n",
    "# ** Or adjust the colormap:\n",
    "# w.vmin = -0.1\n",
    "# w.vmax = 0.5\n",
    "# w.cmap = \"thermal\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example with much larger time frame\n",
    "You can alternatively load and visualize a much larger time frame (seven years instead of one), which may take 2-3 minutes to load:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from sen2nbar.nbar import nbar_cubo"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "lbands_da = cubo.create(\n",
    "    lat=51, # Central latitude of the cube\n",
    "    lon=10, # Central longitude of the cube\n",
    "    collection=\"sentinel-2-l2a\", # Name of the STAC collection\n",
    "    bands=[\"B01\",\"B02\",\"B03\",\"B04\",\"B05\",\"B06\",\"B07\",\"B08\",\"B11\",\"B12\"], # Bands to retrieve\n",
    "    start_date=\"2016-01-01\", # Start date of the cube\n",
    "    end_date=\"2022-12-31\", # End date of the cube\n",
    "    edge_size=1024, # Edge size of the cube (px)\n",
    "    resolution=10, # Pixel size of the cube (m)\n",
    "    query={\"eo:cloud_cover\": {\"lt\": 10} }\n",
    ")\n",
    "lbands_da = nbar_cubo(lbands_da)\n",
    "\n",
    "lda = spyndex.computeIndex(\n",
    "    index = [\"NDVI\"],\n",
    "    params = {\n",
    "        \"N\": lbands_da.sel(band = \"B08\"),\n",
    "        \"R\": lbands_da.sel(band = \"B04\"),\n",
    "        \"L\": 0.5\n",
    "    }\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "lw = lexcube.Cube3DWidget(lda)\n",
    "lw"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# ** Save the figure as PNG:\n",
    "# lw.savefig()\n",
    "# ** Or adjust the colormap:\n",
    "# lw.vmin = -0.1\n",
    "# lw.vmax = 0.5"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
