{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "23678f64-b06d-409b-992f-6eed8c035968",
   "metadata": {},
   "source": [
    "# Lexcube + Awesome Spectral Indices + OpenEO"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "62a9b9b7-ce4e-43b6-894e-fc4cb76ac1bf",
   "metadata": {},
   "source": [
    "Import libraries:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a0f30555-3ea8-4b78-9591-8a85b1d1d7d0",
   "metadata": {},
   "outputs": [],
   "source": [
    "import openeo\n",
    "import lexcube\n",
    "import xarray as xr\n",
    "\n",
    "from openeo.extra.spectral_indices import compute_index"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c85bb0f4-3e66-4c95-a29b-b092bc090f59",
   "metadata": {},
   "source": [
    "Connect to OpenEO (create an account if you do not have one):"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "cb3964bb-9e37-4c68-ac73-de9a14a65cb5",
   "metadata": {},
   "outputs": [],
   "source": [
    "con = openeo.connect(\"openeo.dataspace.copernicus.eu\")\n",
    "con.authenticate_oidc()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c73f9799-53e6-406b-ab25-1506ffae1e41",
   "metadata": {},
   "source": [
    "Load the Sentinel-2 collection:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "eba303e5-985f-483a-b967-3e739f11b5c5",
   "metadata": {},
   "outputs": [],
   "source": [
    "datacube = con.load_collection(\n",
    "    \"SENTINEL2_L2A\",\n",
    "    spatial_extent={\"west\": 5.14, \"south\": 51.17, \"east\": 5.17, \"north\": 51.19},\n",
    "    temporal_extent = [\"2021-02-01\", \"2021-04-30\"],\n",
    "    bands=[\"B02\", \"B04\", \"B08\"],\n",
    "    max_cloud_cover=85,\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "230d4031-7ab0-44e0-935c-4eb434e5dca7",
   "metadata": {},
   "source": [
    "Compute NDVI using Awesome Spectral Indices:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "105559fa-0f9c-421b-8171-ad6c7ef5a44c",
   "metadata": {},
   "outputs": [],
   "source": [
    "NDVI = compute_index(datacube, \"NDVI\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1ad249cd-abf9-4a53-861f-99a3f711a8ff",
   "metadata": {},
   "source": [
    "Download the data cube:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f8f59d6a-2098-45e7-ad5b-6a7f07b82e43",
   "metadata": {},
   "outputs": [],
   "source": [
    "NDVI.download(\"NDVI.nc\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7076a14a-1ba2-4b8b-b28e-45ef2b3a3415",
   "metadata": {},
   "source": [
    "Open the data cube with `xarray`:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c73437bc-14f6-40d5-ae20-52a3b9c84499",
   "metadata": {},
   "outputs": [],
   "source": [
    "da = xr.open_dataset(\"NDVI.nc\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b718c946-5e18-448a-b862-e246c4822fa1",
   "metadata": {},
   "source": [
    "Do the interactive visualization with Lexcube:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9774861d-4f3e-4327-8383-dfd03fa193cc",
   "metadata": {},
   "outputs": [],
   "source": [
    "w = lexcube.Cube3DWidget(da[\"NDVI\"], cmap=\"viridis\", vmin=0, vmax=1)\n",
    "w"
   ]
  }
 ],
 "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.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
