{ "cells": [ { "cell_type": "markdown", "id": "e3117169", "metadata": {}, "source": [ "# S1 SAR Backscatter Basic Usage" ] }, { "cell_type": "markdown", "id": "8ef3e5d2", "metadata": {}, "source": [ "> **Note:** \n", "> This notebook includes **interactive map visualizations** using the `geemap` package. \n", "> Additionally, you will likely need to install `ipykernel` to utilize the interactive elements within a Jupyter Notebook.\n", "> The `geemap` and `ipykernel` packages are **not installed automatically** with `RadGEEToolbox` since they are optional dependencies focused on visualizations. \n", "> If you have not already installed these packages, you can do so with:\n", ">\n", "> ```bash\n", "> pip install geemap ipykernel\n", "> ```\n", ">\n", "> Alternatively, using conda:\n", ">\n", "> ```bash\n", "> conda install conda-forge::geemap anaconda::ipykernel\n", "> ```\n", "> \n", "> If you do not install them, the map-based portions of this notebook will not work." ] }, { "cell_type": "markdown", "id": "62a36def", "metadata": {}, "source": [ "**Initialization and Setup**" ] }, { "cell_type": "code", "execution_count": 1, "id": "8ccbf201", "metadata": {}, "outputs": [], "source": [ "import ee\n", "from RadGEEToolbox import Sentinel1Collection" ] }, { "cell_type": "code", "execution_count": null, "id": "7a556efe", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Earth Engine initialized successfully.\n" ] } ], "source": [ "# Store name of Google Cloud Project assosiated with Earth Engine - replace with your project ID/name\n", "PROJECT_ID = 'your-cloud-project-id'\n", "\n", "# Attempt to initialize Earth Engine\n", "try:\n", " ee.Initialize(project=PROJECT_ID)\n", " print(\"Earth Engine initialized successfully.\")\n", "except Exception as e:\n", " print(\"Initialization failed, attempting authentication...\")\n", " try:\n", " ee.Authenticate()\n", " ee.Initialize(project=PROJECT_ID)\n", " print(\"Authentication and initialization successful.\")\n", " except Exception as auth_error:\n", " print(\"Authentication failed. Error details:\", auth_error)" ] }, { "cell_type": "markdown", "id": "a2ec8525", "metadata": {}, "source": [ "**Quickly defining an ROI for filtering imagery - can be any ee.Geometry()**\n", "\n", "Note: You may filter based on relative orbit numbers in addition to a geometry" ] }, { "cell_type": "code", "execution_count": 15, "id": "1a561b53", "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", " \n", " " ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "# First we will define a region of interest (ROI) for the analysis, in this case, Salt Lake County, Utah.\n", "counties = ee.FeatureCollection('TIGER/2018/Counties')\n", "salt_lake_county = counties.filter(ee.Filter.And(\n", " ee.Filter.eq('NAME', 'Salt Lake'),\n", " ee.Filter.eq('STATEFP', '49')))\n", "salt_lake_geometry = salt_lake_county.geometry()" ] }, { "cell_type": "markdown", "id": "8b1dc6b1", "metadata": {}, "source": [ "________\n", "\n", "**Image collection definition and filtering**\n", "\n", "Define the desired parameters to initialize the Sentinel-1 SAR collection using the `Sentinel1Collection()` class. \n", "\n", "**In this case, filtering to dual-polarized (VV/VH) descending IW scenes during May of 2024** " ] }, { "cell_type": "code", "execution_count": 16, "id": "17e3048c", "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", " \n", " " ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "SAR_collection = Sentinel1Collection(\n", " start_date='2024-05-01',\n", " end_date='2024-05-31',\n", " instrument_mode='IW',\n", " polarization=['VV', 'VH'],\n", " orbit_direction='DESCENDING',\n", " boundary=salt_lake_geometry,\n", " resolution_meters=10\n", ")" ] }, { "cell_type": "markdown", "id": "b577cdec", "metadata": {}, "source": [ "The image will be much larger than the boundary of the study area, so let's mask the data using the `salt_lake_geometry` shape and `.mask_to_polygon()` to reduce processing time moving forward" ] }, { "cell_type": "code", "execution_count": 17, "id": "f1ca682a", "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", " \n", " " ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "SAR_collection = SAR_collection.mask_to_polygon(salt_lake_geometry)" ] }, { "cell_type": "markdown", "id": "b89f28d3", "metadata": {}, "source": [ "Now let's print the dates of the collection to validate" ] }, { "cell_type": "code", "execution_count": 18, "id": "2a4fcd91", "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", " \n", " " ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "SAR Collection Dates: ['2024-05-04', '2024-05-04', '2024-05-11', '2024-05-11', '2024-05-23', '2024-05-23', '2024-05-28', '2024-05-28']\n" ] } ], "source": [ "dates = SAR_collection.dates\n", "print(\"SAR Collection Dates:\", dates)" ] }, { "cell_type": "markdown", "id": "a6bb20b2", "metadata": {}, "source": [ "______\n", "\n", "**Multilooking and speckle filtering are very common workflows for SAR data, and `RadGEEToolbox` provides functionality for both**\n", "\n", "S1 SAR data hosted by GEE is in units of decibels (dB, logarithmic) but units of σ⁰ (backscatter coefficient, linear) are more suitable for multilooking and speckle filtering.\n", "\n", "**First, we convert from dB to σ⁰ using `.sigma0FromDb`**" ] }, { "cell_type": "code", "execution_count": null, "id": "f89136bf", "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", " \n", " " ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "SAR_collection_sigma0 = SAR_collection.sigma0FromDb" ] }, { "cell_type": "markdown", "id": "3d6c3ba4", "metadata": {}, "source": [ "**Then Perform Multilooking using `.multilook()` - in this case using 4 looks**" ] }, { "cell_type": "code", "execution_count": 20, "id": "ee8256a1", "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", " \n", " " ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "SAR_collection_multilooked = SAR_collection_sigma0.multilook(looks=4)" ] }, { "cell_type": "markdown", "id": "a15380be", "metadata": {}, "source": [ "**Now perform speckle filtering on the multilooked collection using `.speckle_filter()` **\n", "\n", "**When speckle filtering you must provide the kernel size (odd integer between 3-9).** You may optionally tweak the speckle filter process by adjusting the geometry used for windowed statistics, the Tk value, sigma value, and number of looks of the input collection. \n", "\n", "*In this case we choose a kernel size of 5x5, use the geometry of our ROI, and specify that the input collection is multilooked by 4 looks (important for the correct LUT usage)*" ] }, { "cell_type": "code", "execution_count": 21, "id": "52e728a9", "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", " \n", " " ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "SAR_collection_multilooked_and_filtered = SAR_collection_multilooked.speckle_filter(5, geometry=salt_lake_geometry, looks=4)" ] }, { "cell_type": "markdown", "id": "830650f6", "metadata": {}, "source": [ "**Print the dates of the multilooked and speckle filtered SAR collection using `.dates` to verify the filtering worked successfully**" ] }, { "cell_type": "code", "execution_count": 22, "id": "11d93e02", "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", " \n", " " ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "['2024-05-04', '2024-05-04', '2024-05-11', '2024-05-11', '2024-05-23', '2024-05-23', '2024-05-28', '2024-05-28']\n" ] } ], "source": [ "print(SAR_collection_multilooked_and_filtered.dates)" ] }, { "cell_type": "markdown", "id": "da3b46d0", "metadata": {}, "source": [ "**Now we convert back to dB for analysis and visualization using `.dbFromSigma0`**" ] }, { "cell_type": "code", "execution_count": null, "id": "432e005b", "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", " \n", " " ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "SAR_collection_multilooked_and_filtered_db = SAR_collection_multilooked_and_filtered.dbFromSigma0" ] }, { "cell_type": "markdown", "id": "b4ceb24f", "metadata": {}, "source": [ "__________\n", "\n", "**Example of how to temporally reduce the results by processing a mean composite using `.mean`**" ] }, { "cell_type": "code", "execution_count": 24, "id": "292bca69", "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", " \n", " " ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "mean_SAR_image = SAR_collection_multilooked_and_filtered_db.mean" ] }, { "cell_type": "markdown", "id": "0d5ce020", "metadata": {}, "source": [ "___________\n", "**If you wish to visualize the results follow the code below. You will need to have the `geemap` package installed**" ] }, { "cell_type": "code", "execution_count": 25, "id": "83cfd1fe", "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", " \n", " " ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "import geemap\n", "from RadGEEToolbox import GetPalette\n", "from RadGEEToolbox import VisParams" ] }, { "cell_type": "markdown", "id": "e04c9479", "metadata": {}, "source": [ "**Run the cell below to view the results on the map**\n", "\n", "#### Note: the raster is very detailed, so it may take a few seconds once done processing to display the image on the map" ] }, { "cell_type": "code", "execution_count": 26, "id": "aec94952", "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", " \n", " " ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "0c2ba93846bd42b1900949ca75f0a1a9", "version_major": 2, "version_minor": 0 }, "text/plain": [ "Map(center=[40.7514, -111.9064], controls=(WidgetControl(options=['position', 'transparent_bg'], widget=Search…" ] }, "execution_count": 26, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Map = geemap.Map(center=(40.7514, -111.9064), zoom=10)\n", "Map.addLayer(mean_SAR_image.select('VV'), vis_params={'min': -50, 'max': 0, 'palette': GetPalette.get_palette('inferno')}, name='NDWI')\n", "Map.add_colorbar(vis_params={'min': -50, 'max': 0, 'palette': GetPalette.get_palette('inferno')})\n", "Map" ] }, { "cell_type": "markdown", "id": "6982ebf6", "metadata": {}, "source": [ "Example of What Map Should Look Like\n", "\n", "![Example of What Map Should Look Like](images/SAR_Map_Example.png)" ] }, { "cell_type": "markdown", "id": "0d566f2b", "metadata": {}, "source": [ "### *Please refer to the [RadGEEToolbox documentation](https://radgeetoolbox.readthedocs.io/en/latest/) for more information and a comprehensive list of available functionality*" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "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.12.3" } }, "nbformat": 4, "nbformat_minor": 5 }