Skip to content

Commit 09caa30

Browse files
reduce run time
1 parent 75c2e71 commit 09caa30

File tree

1 file changed

+29
-63
lines changed

1 file changed

+29
-63
lines changed

notebooks/courses/microwave-remote-sensing/unit_01/02_in_class_exercise.ipynb

Lines changed: 29 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,11 @@
55
"id": "0",
66
"metadata": {},
77
"source": [
8-
"# Unit Conversion\n",
8+
"---\n",
9+
"title: Unit Conversion\n",
10+
"jupyter: \"microwave-remote-sensing\"\n",
11+
"---\n",
12+
"\n",
913
"\n",
1014
"\n",
1115
"In this notebook, we are going to have a look at the conversion of units. Sentinel-1 data, and most other SAR data, is usually provided in decibels (dB). In this notebook, we will discover the advantages of displaying SAR data in decibels and why we need to convert the data to a linear scale in order to make meaningful calculations. Let's start with importing some libraries.\n",
@@ -15,8 +19,7 @@
1519
"$$\n",
1620
"$$\n",
1721
"[\\text{dB}] \\longleftrightarrow [\\text{m}^2 \\cdot \\text{m}^{-2}]\n",
18-
"$$\n",
19-
"\n"
22+
"$$\n"
2023
]
2124
},
2225
{
@@ -30,8 +33,7 @@
3033
"import numpy as np\n",
3134
"import odc.stac\n",
3235
"import pystac_client\n",
33-
"import rioxarray # noqa: F401\n",
34-
"import xarray as xr"
36+
"import rioxarray # noqa: F401"
3537
]
3638
},
3739
{
@@ -89,14 +91,12 @@
8991
"outputs": [],
9092
"source": [
9193
"bands = \"VV\"\n",
92-
"crs = \"EPSG:27704\" # Coordinate Reference System: EQUI7 Grid of Europe\n",
93-
"res = 20 # 20 meter\n",
9494
"\n",
9595
"sig0_dc = odc.stac.stac_load(\n",
9696
" items,\n",
9797
" bands=bands,\n",
9898
" bbox=bounds,\n",
99-
" chunks={\"time\": 5, \"x\": 600, \"y\": 600},\n",
99+
" chunks={\"time\": 5, \"x\": 1000, \"y\": 1000},\n",
100100
")\n",
101101
"\n",
102102
"nodata = items[0].assets[\"VV\"].extra_fields[\"raster:bands\"][0][\"nodata\"]\n",
@@ -335,7 +335,7 @@
335335
"id": "15",
336336
"metadata": {},
337337
"source": [
338-
"The dataset has now only 12 timestamps, one for each month. Next, we want to calculate the average $\\sigma^0$ value across the scene for one month. We will do this again by converting the data to linear scale first and then calculating the average and converting it back to dB.\n"
338+
"The dataset has now only 12 timestamps, one for each month. Next, we want to calculate the average $\\sigma^0$ value across a subset of the scene for one month. We will do this again by converting the data to linear scale first and then calculating the average and converting it back to dB.\n"
339339
]
340340
},
341341
{
@@ -346,7 +346,11 @@
346346
"outputs": [],
347347
"source": [
348348
"# Lets take a data array with db values\n",
349-
"db_array = sig0_monthly.sel(time=\"2022-07-30\", method=\"nearest\")\n",
349+
"db_array = (\n",
350+
" sig0_monthly.sel(time=\"2022-07-30\", method=\"nearest\")\n",
351+
" .isel(x=slice(300, 400), y=slice(500, 600))\n",
352+
" .compute()\n",
353+
")\n",
350354
"\n",
351355
"# Compute the linear values\n",
352356
"lin_array = db2lin(db_array)"
@@ -375,65 +379,27 @@
375379
"id": "18",
376380
"metadata": {},
377381
"source": [
378-
"As you can see in the example, the mean values across the scene are different in dB and linear scale. Therefore, it is important to be aware in which scale the data is stored to perform the correct type of mathematical operation or always convert the data to linear scale before doing any calculations.\n",
379-
"\n",
380-
"## Save Mean Mosaic as Tif File\n",
381-
"\n",
382-
"Often we want to store the output of a computation permanently on a file system. The most common file format for this is a GeoTIFF (TIF file with additional information on the georeference). The following cell indicates how this can be easily done with Xarray. When we want to store the data as a GeoTIFF we need to make sure to provide a spatial reference to geolocate the data. The best way to check whether the Xarray has a coordinate reference system (CRS) is by using the rioxarray `rio.crs` accessor. More about this in notebook \"Datacubes\".\n"
383-
]
384-
},
385-
{
386-
"cell_type": "code",
387-
"execution_count": null,
388-
"id": "19",
389-
"metadata": {},
390-
"outputs": [],
391-
"source": [
392-
"# Select some data which we want to save\n",
393-
"data_2_save = sig0_monthly.sel(time=\"2022-07-30\", method=\"nearest\")\n",
394-
"data_2_save.rio.crs"
395-
]
396-
},
397-
{
398-
"cell_type": "markdown",
399-
"id": "20",
400-
"metadata": {},
401-
"source": [
402-
"In this case, the spatial reference is the EPSG Code `EPSG:27704`, which is the `Equi7Grid Europe`.\n",
403-
"As the output data array already has a spatial reference we now save it as a raster file\n"
404-
]
405-
},
406-
{
407-
"cell_type": "code",
408-
"execution_count": null,
409-
"id": "21",
410-
"metadata": {},
411-
"outputs": [],
412-
"source": [
413-
"# Save the data\n",
414-
"data_2_save.rio.to_raster(\n",
415-
" \"sig0_mean_mosaic_july.tif\", tiled=True, driver=\"GTiff\", compress=\"LZW\"\n",
416-
")"
417-
]
418-
},
419-
{
420-
"cell_type": "code",
421-
"execution_count": null,
422-
"id": "22",
423-
"metadata": {},
424-
"outputs": [],
425-
"source": [
426-
"# Load the data again (for demonstration purposes)\n",
427-
"loaded_data = xr.open_dataset(\"sig0_mean_mosaic_july.tif\", engine=\"rasterio\")\n",
428-
"loaded_data"
382+
"As you can see in the example, the mean values across the scene are different in dB and linear scale. Therefore, it is important to be aware in which scale the data is stored to perform the correct type of mathematical operation or always convert the data to linear scale before doing any calculations."
429383
]
430384
}
431385
],
432386
"metadata": {
433387
"kernelspec": {
434-
"display_name": "Python 3 (ipykernel)",
388+
"display_name": "microwave-remote-sensing",
435389
"language": "python",
436-
"name": "python3"
390+
"name": "microwave-remote-sensing"
391+
},
392+
"language_info": {
393+
"codemirror_mode": {
394+
"name": "ipython",
395+
"version": 3
396+
},
397+
"file_extension": ".py",
398+
"mimetype": "text/x-python",
399+
"name": "python",
400+
"nbconvert_exporter": "python",
401+
"pygments_lexer": "ipython3",
402+
"version": "3.11.13"
437403
}
438404
},
439405
"nbformat": 4,

0 commit comments

Comments
 (0)