From 98c1e10976c4fe887ae551d60547ff2634d3e774 Mon Sep 17 00:00:00 2001 From: Alex Leith Date: Thu, 25 Apr 2024 10:47:23 +1000 Subject: [PATCH 1/2] Add first draft of geomad notebook --- S1_GeoMAD_Mosaic_Test.ipynb | 212 ++++++++++++++++++++++++++++++++++++ 1 file changed, 212 insertions(+) create mode 100644 S1_GeoMAD_Mosaic_Test.ipynb diff --git a/S1_GeoMAD_Mosaic_Test.ipynb b/S1_GeoMAD_Mosaic_Test.ipynb new file mode 100644 index 0000000..639a0b4 --- /dev/null +++ b/S1_GeoMAD_Mosaic_Test.ipynb @@ -0,0 +1,212 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Sentinel-1 GeoMAD\n", + "\n", + "This notebook runs a process to load Sentinel-1 data and produce\n", + "a geometric median and median absolute deviations. Two test regions\n", + "are currently available, one in Tasmania and one in Moretons Bay.\n", + "\n", + "Key requirements include Datacube and a specific branch of\n", + "[odc-algo](https://github.com/opendatacube/odc-algo/tree/add-rust-geomedian-impl)." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "tags": [] + }, + "outputs": [], + "source": [ + "import odc.geo.xr # noqa: F401\n", + "from datacube import Datacube\n", + "from distributed import Client\n", + "from dask import config\n", + "\n", + "from odc.algo import geomedian_with_mads\n", + "from odc.stac import configure_rio\n", + "\n", + "from collections import namedtuple\n", + "\n", + "from easi_tools.notebook_utils import localcluster_dashboard\n", + "from easi_tools import EasiDefaults" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "tags": [] + }, + "outputs": [], + "source": [ + "# Connect to the datacube and set up some config\n", + "dc = Datacube()\n", + "\n", + "easi = EasiDefaults()\n", + "configure_rio(cloud_defaults=True)\n", + "study_site = namedtuple(\"study_site\", [\"name\", \"bbox\"])" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "tags": [] + }, + "outputs": [], + "source": [ + "# Configure Dask using defaults\n", + "client = Client()\n", + "\n", + "print(localcluster_dashboard(client=client, server=easi.hub))" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "tags": [] + }, + "outputs": [], + "source": [ + "# Change process parameters here\n", + "\n", + "# Study site in Tasmania\n", + "site = study_site(\"southern_tasmania\", [146.2357, -43.6796, 147.147, -42.9305])\n", + "\n", + "# Study site in Moreton Bay\n", + "site = study_site(\"moreton_bay\", [152.00, -27.50, 153.20, -26.50])\n", + "\n", + "year = \"2023\"\n", + "\n", + "data = dc.load(\n", + " product=\"sentinel1_grd_gamma0_beta\",\n", + " lon=(site.bbox[0], site.bbox[2]),\n", + " lat=(site.bbox[1], site.bbox[3]),\n", + " time=year,\n", + " resolution=(-20, 20),\n", + " output_crs=\"EPSG:3577\",\n", + " dask_chunks=dict(x=2048, y=2048),\n", + " group_by=\"solar_day\",\n", + " measurements=[\"vv\", \"vh\"],\n", + ")\n", + "\n", + "# Mask out nodata values\n", + "data = data.where(data.vv > 0)\n", + "\n", + "data" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "tags": [] + }, + "outputs": [], + "source": [ + "# Get the geomedian and mad bands\n", + "geomad = geomedian_with_mads(data, work_chunks=(2048, 2048), num_threads=32)\n", + "\n", + "# Calculate means\n", + "for band in [\"vv\", \"vh\"]:\n", + " geomad[f\"{band}_mean\"] = data[band].mean(\"time\")\n", + "\n", + "geomad" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "tags": [] + }, + "outputs": [], + "source": [ + "# This step run the computation on Dask. Open the dashboard\n", + "# link above to watch it process.\n", + "computed = geomad.compute()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "tags": [] + }, + "outputs": [], + "source": [ + "computed[\"vv\"].plot.imshow(robust=True)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "tags": [] + }, + "outputs": [], + "source": [ + "computed[\"vv_mean\"].plot.imshow(robust=True)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "(computed[\"vv\"] - computed[\"vv_mean\"]).plot.imshow(robust=True)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "tags": [] + }, + "outputs": [], + "source": [ + "computed[\"vv\"].odc.explore(robust=True)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "tags": [] + }, + "outputs": [], + "source": [ + "for var in computed.data_vars:\n", + " computed[var].odc.write_cog(f\"{site.name}_beta_{var}.tif\")" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Alex Test Environment", + "language": "python", + "name": "test" + }, + "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.10.12" + } + }, + "nbformat": 4, + "nbformat_minor": 4 +} From c41ef6c443e175d298d3be5e4b1c79967793d7d7 Mon Sep 17 00:00:00 2001 From: Alex Leith Date: Thu, 25 Apr 2024 01:51:29 +0000 Subject: [PATCH 2/2] Update notebook after testing on EASI --- .gitignore | 2 ++ S1_GeoMAD_Mosaic_Test.ipynb | 34 +++++++++++++++++----------------- 2 files changed, 19 insertions(+), 17 deletions(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..176af7e --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +.ipynb_checkpoints/ + diff --git a/S1_GeoMAD_Mosaic_Test.ipynb b/S1_GeoMAD_Mosaic_Test.ipynb index 639a0b4..15546ac 100644 --- a/S1_GeoMAD_Mosaic_Test.ipynb +++ b/S1_GeoMAD_Mosaic_Test.ipynb @@ -26,14 +26,15 @@ "from datacube import Datacube\n", "from distributed import Client\n", "from dask import config\n", + "from numpy import log10\n", "\n", "from odc.algo import geomedian_with_mads\n", "from odc.stac import configure_rio\n", "\n", "from collections import namedtuple\n", "\n", - "from easi_tools.notebook_utils import localcluster_dashboard\n", - "from easi_tools import EasiDefaults" + "# from easi_tools.notebook_utils import localcluster_dashboard\n", + "# from easi_tools import EasiDefaults" ] }, { @@ -47,8 +48,13 @@ "# Connect to the datacube and set up some config\n", "dc = Datacube()\n", "\n", - "easi = EasiDefaults()\n", + "# Rasterio defaults\n", "configure_rio(cloud_defaults=True)\n", + "\n", + "# Easi defaults\n", + "# easi = EasiDefaults()\n", + "\n", + "# A simple data structure for our study sites\n", "study_site = namedtuple(\"study_site\", [\"name\", \"bbox\"])" ] }, @@ -63,7 +69,7 @@ "# Configure Dask using defaults\n", "client = Client()\n", "\n", - "print(localcluster_dashboard(client=client, server=easi.hub))" + "# print(localcluster_dashboard(client=client, server=easi.hub))" ] }, { @@ -80,7 +86,7 @@ "site = study_site(\"southern_tasmania\", [146.2357, -43.6796, 147.147, -42.9305])\n", "\n", "# Study site in Moreton Bay\n", - "site = study_site(\"moreton_bay\", [152.00, -27.50, 153.20, -26.50])\n", + "# site = study_site(\"moreton_bay\", [152.00, -27.50, 153.20, -26.50])\n", "\n", "year = \"2023\"\n", "\n", @@ -155,15 +161,6 @@ "computed[\"vv_mean\"].plot.imshow(robust=True)" ] }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "(computed[\"vv\"] - computed[\"vv_mean\"]).plot.imshow(robust=True)" - ] - }, { "cell_type": "code", "execution_count": null, @@ -172,7 +169,7 @@ }, "outputs": [], "source": [ - "computed[\"vv\"].odc.explore(robust=True)" + "(computed[\"vv\"] - computed[\"vv_mean\"]).plot.imshow(robust=True)" ] }, { @@ -183,8 +180,11 @@ }, "outputs": [], "source": [ - "for var in computed.data_vars:\n", - " computed[var].odc.write_cog(f\"{site.name}_beta_{var}.tif\")" + "computed[\"vv_log\"] = 10 * log10(computed.vv)\n", + "computed[\"vh_log\"] = 10 * log10(computed.vh)\n", + "computed[\"vh_vv_log\"] = 0.5 * (computed.vh_log / computed.vv_log)\n", + "\n", + "computed.odc.explore(bands=[\"vv_log\", \"vh_log\", \"vh_vv_log\"], vmin=-30, vmax=0)" ] } ],