{ "cells": [ { "cell_type": "markdown", "id": "5293f5c5", "metadata": {}, "source": [ "# Load Data from the eodc STAC catalogue into Xarray\n" ] }, { "cell_type": "code", "execution_count": null, "id": "0282890e", "metadata": {}, "outputs": [], "source": [ "try:\n", " from odc import stac as odc_stac\n", "except ImportError:\n", " %pip install odc-stac\n", " from odc import stac as odc_stac\n", "\n", "try:\n", " from netCDF4 import Dataset\n", "except ImportError:\n", " %pip install netCDF4\n", " from netCDF4 import Dataset\n", "\n", "import xarray as xr\n", "import matplotlib.pyplot as plt\n", "\n", "from pystac_client import Client\n", "\n", "import os\n", "from datetime import datetime\n", "\n", "import numpy as np" ] }, { "cell_type": "markdown", "id": "aa7db18f", "metadata": {}, "source": [ "## Access the catalog and desired collection\n", "\n", "First we load the data, we want to load as a Xarray. This is described in the [Data Discorvery Notebook](https://github.com/eodcgmbh/eodc-examples/blob/main/demos/python-stac_DataDiscovery.ipynb).\n" ] }, { "cell_type": "code", "execution_count": null, "id": "e9903a2f", "metadata": { "lines_to_next_cell": 2 }, "outputs": [], "source": [ "eodc_catalog = Client.open(\n", " \"https://stac.eodc.eu/api/v1\",\n", ")" ] }, { "cell_type": "code", "execution_count": null, "id": "f8699d67", "metadata": { "lines_to_next_cell": 2 }, "outputs": [], "source": [ "time_range = \"2024-03-01/2024-05-01\"\n", "\n", "# Bounding box of Austria\n", "bbox_aut = [9.25, 46.31, 17.46, 49.18]" ] }, { "cell_type": "code", "execution_count": null, "id": "c0989187", "metadata": {}, "outputs": [], "source": [ "search = eodc_catalog.search(\n", " collections=[\"SENTINEL2_L1C\"],\n", " bbox = bbox_aut,\n", " datetime=time_range\n", ")\n", "\n", "print(\"We found \", search.matched(), \" items, that match our filter criteria.\")\n", "\n", "items = search.items()\n" ] }, { "cell_type": "markdown", "id": "01e3b7fb", "metadata": {}, "source": [ " We found 14 items, that match our filter criteria." ] }, { "cell_type": "markdown", "id": "8bf987bf", "metadata": {}, "source": [ "### Specify Dataset parameters and load into Xarray\n" ] }, { "cell_type": "code", "execution_count": null, "id": "f9c30f02", "metadata": { "lines_to_next_cell": 2 }, "outputs": [], "source": [ "crs = \"EPSG:4312\"\n", "res = 0.00018 # 20 meters in degree \n", "bands = [\"red\", \"nir\"]" ] }, { "cell_type": "code", "execution_count": null, "id": "22a6b730", "metadata": {}, "outputs": [], "source": [ "#This step can take time\n", "print (\"loading data started : \", datetime.now())\n", "ds = odc_stac.load(items,\n", " crs=crs,\n", " resolution=res,\n", " bbox=bbox_aut,\n", " bands = bands,\n", " )\n", "print (\"loading data completed: \", datetime.now())" ] }, { "cell_type": "markdown", "id": "fb9d85c8", "metadata": {}, "source": [ " loading data started : 2024-07-04 13:19:36.876273\n", " loading data completed: 2024-07-04 13:49:39.271929" ] }, { "cell_type": "markdown", "id": "e89b765f", "metadata": {}, "source": [ "### Save results as netCDF\n" ] }, { "cell_type": "code", "execution_count": null, "id": "1b9171f3", "metadata": {}, "outputs": [], "source": [ "ds.to_netcdf(\"./data/stac_xarray.nc\")" ] }, { "cell_type": "markdown", "id": "617a40f0", "metadata": {}, "source": [ "## Load netCDF\n", "\n", "Open the saved NetCDF file.\n" ] }, { "cell_type": "code", "execution_count": null, "id": "cacc87bf", "metadata": { "lines_to_next_cell": 2 }, "outputs": [], "source": [ "nc_file = xr.open_dataset(\"./data/stac_xarray.nc\")" ] }, { "cell_type": "code", "execution_count": null, "id": "7f6d20ca", "metadata": { "lines_to_next_cell": 2 }, "outputs": [], "source": [ "red = nc_file['red']\n", "nir = nc_file['nir']" ] }, { "cell_type": "code", "execution_count": null, "id": "36ce8f71", "metadata": { "lines_to_next_cell": 2 }, "outputs": [], "source": [ "def normalized_difference(x, y):\n", " nd = (x - y) / (x + y)\n", " return nd" ] }, { "cell_type": "code", "execution_count": null, "id": "e78d8791", "metadata": {}, "outputs": [], "source": [ "ndvi = normalized_difference(nir, red)" ] }, { "cell_type": "markdown", "id": "d362ccb3", "metadata": {}, "source": [ "### Plot the NetCDF Data\n", "\n", "Select data for a specific time step and create a plot.\n" ] }, { "cell_type": "code", "execution_count": null, "id": "ab2e0b1e", "metadata": { "lines_to_next_cell": 2 }, "outputs": [], "source": [ "time_data = ndvi.isel(time = 0)" ] }, { "cell_type": "code", "execution_count": null, "id": "f91fa1b4", "metadata": { "lines_to_next_cell": 2 }, "outputs": [], "source": [ "p = plt.imshow(time_data, vmin = -1, vmax = 1, cmap = \"inferno\")\n", "plt.colorbar(p)\n", "plt.xlabel(\"Longitude\")\n", "plt.ylabel(\"Latitude\")\n", "plt.title(\"NetCDF Data NDVI\")\n", "plt.show()" ] }, { "cell_type": "markdown", "id": "4d085ef7", "metadata": {}, "source": [ "\n", "To create different plots, you need to run the notebook yourself.\n" ] }, { "cell_type": "code", "execution_count": null, "id": "6ea92f63", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "jupytext": { "cell_metadata_filter": "-all", "main_language": "python", "notebook_metadata_filter": "-all" }, "kernelspec": { "display_name": "probe", "language": "python", "name": "python3" }, "language_info": { "name": "python", "version": "3.11.9" } }, "nbformat": 4, "nbformat_minor": 5 }