diff --git a/docs/source/science/NISAR/NISAR_DEM.ipynb b/docs/source/science/NISAR/NISAR_DEM.ipynb new file mode 100644 index 00000000..4fc80d4a --- /dev/null +++ b/docs/source/science/NISAR/NISAR_DEM.ipynb @@ -0,0 +1,335 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "8467047e-e36e-4037-b815-3485c29a5c4d", + "metadata": {}, + "source": [ + "# NISAR DEM" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "id": "bb09ce2f-225b-4217-ac6f-c751b324a3c3", + "metadata": {}, + "outputs": [], + "source": [ + "import os\n", + "import time\n", + "from pathlib import Path\n", + "\n", + "import requests\n", + "import numpy as np\n", + "import rasterio\n", + "from rasterio.windows import from_bounds\n", + "import matplotlib.pyplot as plt" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "7fd0dcea-0a39-4563-a8d5-cca321b35f50", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "('https://nisar.asf.earthdatacloud.nasa.gov/NISAR/DEM/v1.2/EPSG4326/EPSG4326.vrt',\n", + " 's3://sds-n-cumulus-prod-nisar-products/DEM/v1.2/EPSG4326/EPSG4326.vrt')" + ] + }, + "execution_count": 2, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# --- DEM version + projection directory ---\n", + "DEM_VERSION = \"v1.2\"\n", + "EPSG_DIR = \"EPSG4326\" # or \"EPSG3413\" (Arctic), \"EPSG3031\" (Antarctic)\n", + "\n", + "# --- AOI bbox ---\n", + "# For EPSG4326: lon/lat bbox (min_lon, min_lat, max_lon, max_lat)\n", + "minx, miny, maxx, maxy = -116.7, 35.9, -116.6, 36.0\n", + "BBOX = (minx, miny, maxx, maxy)\n", + "\n", + "# --- ASF HTTPS VRT ---\n", + "ASF_HTTP_BASE = \"https://nisar.asf.earthdatacloud.nasa.gov/NISAR/DEM\"\n", + "HTTP_VRT_URL = f\"{ASF_HTTP_BASE}/{DEM_VERSION}/{EPSG_DIR}/{EPSG_DIR}.vrt\"\n", + "HTTP_VRT_VSI = f\"/vsicurl/{HTTP_VRT_URL}\"\n", + "\n", + "# --- ASF S3 VRT ---\n", + "ASF_S3_BUCKET = \"sds-n-cumulus-prod-nisar-products\"\n", + "S3_KEY = f\"DEM/{DEM_VERSION}/{EPSG_DIR}/{EPSG_DIR}.vrt\"\n", + "S3_VRT_S3URI = f\"s3://{ASF_S3_BUCKET}/{S3_KEY}\"\n", + "S3_VRT_VSI = f\"/vsis3/{ASF_S3_BUCKET}/{S3_KEY}\"\n", + "\n", + "HTTP_VRT_URL, S3_VRT_S3URI" + ] + }, + { + "cell_type": "markdown", + "id": "a0c3e23e-922b-454d-8b8c-37c4cb505bc7", + "metadata": {}, + "source": [ + "# HTTP workflow" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "b78057f9-185b-4faa-a6ea-9442f173e244", + "metadata": {}, + "outputs": [], + "source": [ + "def subset_to_geotiff(dataset_path: str, bbox, out_tif: str):\n", + " minx, miny, maxx, maxy = bbox\n", + "\n", + " with rasterio.open(dataset_path) as src:\n", + " win = from_bounds(minx, miny, maxx, maxy, transform=src.transform)\n", + " data = src.read(1, window=win)\n", + " new_transform = src.window_transform(win)\n", + "\n", + " profile = src.profile.copy()\n", + " profile.update(\n", + " driver=\"GTiff\",\n", + " height=data.shape[0],\n", + " width=data.shape[1],\n", + " transform=new_transform,\n", + " tiled=True,\n", + " compress=\"deflate\",\n", + " BIGTIFF=\"IF_SAFER\",\n", + " )\n", + "\n", + " with rasterio.open(out_tif, \"w\", **profile) as dst:\n", + " dst.write(data, 1)\n", + "\n", + " return out_tif" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "fff0723b-34c0-4559-83b5-fface5109a84", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{'status': 302,\n", + " 'location': 'https://urs.earthdata.nasa.gov/oauth/authorize?client_id=qpa3q_b7OJ32gGjwA9DxHg&response_type=code&redirect_uri=https://nisar.asf.earthdatacloud.nasa.gov/login&state=%2FNISAR%2FDEM%2Fv1.2%2FEPSG4326%2FEPSG4326.vrt&app_type=401',\n", + " 'content_type': 'application/json',\n", + " 'head': ''}" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "def probe_http_no_redirect(url: str, timeout=20):\n", + " r = requests.get(url, allow_redirects=False, timeout=timeout)\n", + " return {\n", + " \"status\": r.status_code,\n", + " \"location\": r.headers.get(\"Location\"),\n", + " \"content_type\": r.headers.get(\"content-type\"),\n", + " \"head\": r.text[:120],\n", + " }\n", + "\n", + "probe = probe_http_no_redirect(HTTP_VRT_URL)\n", + "probe" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "id": "6d2067a0-e63b-4d27-b64d-6874d434c9e5", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "HTTP workflow not usable\n" + ] + } + ], + "source": [ + "out_http = \"dem_subset_http.tif\"\n", + "\n", + "if probe.get(\"status\") == 200 and str(probe.get(\"head\",\"\")).lstrip().startswith(\"" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "if Path(out_s3).exists():\n", + " with rasterio.open(out_s3) as src:\n", + " arr = src.read(1)\n", + " nodata = src.nodata\n", + "\n", + " if nodata is not None:\n", + " arr = np.where(arr == nodata, np.nan, arr)\n", + "\n", + " plt.figure(figsize=(7, 5))\n", + " plt.imshow(arr)\n", + " plt.title(out_s3)\n", + " plt.colorbar()\n", + " plt.tight_layout()\n", + " plt.show()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "3e121b4d-f091-474d-a449-a00ae780e62f", + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "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.12.7" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/docs/source/science_examples.rst b/docs/source/science_examples.rst index dcb11ac9..a2d3f28c 100644 --- a/docs/source/science_examples.rst +++ b/docs/source/science_examples.rst @@ -20,6 +20,7 @@ You can also find links to Open Source Science guidelines for the MAAP platform. science/ATL03/ATL03.ipynb science/ATL08/ATL08.ipynb science/NISAR/NISAR_access.ipynb + science/NISAR/NISAR_dem.ipynb science/ESA_BIOMASS/ESA_BIOMASS_Data_Access.ipynb science/ESA_BIOMASS/ESA_BIOMASS_Simulated_Data_Access.ipynb science/OPERA/OPERA_Surface_Displacement.ipynb