|
8 | 8 | "\n", |
9 | 9 | "\n", |
10 | 10 | "> *DS Python for GIS and Geoscience* \n", |
11 | | - "> *November, 2022*\n", |
| 11 | + "> *September, 2023*\n", |
12 | 12 | ">\n", |
13 | | - "> *© 2022, Joris Van den Bossche and Stijn Van Hoey. Licensed under [CC BY 4.0 Creative Commons](http://creativecommons.org/licenses/by/4.0/)*\n", |
| 13 | + "> *© 2023, Joris Van den Bossche and Stijn Van Hoey. Licensed under [CC BY 4.0 Creative Commons](http://creativecommons.org/licenses/by/4.0/)*\n", |
14 | 14 | "\n", |
15 | 15 | "---" |
16 | 16 | ] |
|
426 | 426 | "\n", |
427 | 427 | "**EXERCISE**:\n", |
428 | 428 | "\n", |
429 | | - "* Read in the file `./data/gent/raster/2020-09-17_Sentinel_2_L1C_True_color.tiff` with xarray and assign the data to a new variable `tc_data`. \n", |
| 429 | + "* Read in the file `./data/gent/raster/2020-09-17_Sentinel_2_L1C_True_color.tiff` with xarray in the original raw data dtype and assign the data to a new variable `tc_data`. \n", |
430 | 430 | "* Inspect the display of `tc_data`. What are the different dimensions of the array? \n", |
431 | 431 | "* Select only the *second* layer of `tc_data` and assign the output to a new variable `tc_g`.\n", |
432 | 432 | "* Plot the second (green) layer.\n", |
433 | 433 | "* Create a new array with each of the elements in the `tc_g` array with a value above 15000 set to a new value of 65535.\n", |
434 | 434 | " \n", |
435 | 435 | "<details><summary>Hints</summary>\n", |
436 | 436 | "\n", |
| 437 | + "* Use the `mask_and_scale` parameter in the reader function to make sure the data type of tc_data is the same as the raw data.\n", |
437 | 438 | "* To select a specific subset for a certain dimension of a DataArray, use the `.sel()` method. The argument name to use is the name of the dimension.\n", |
438 | 439 | "* You can mask an array and set the masked values to another value with the `.where()` method. Check the help of the method for information on the keyword arguments. \n", |
439 | 440 | "\n", |
|
595 | 596 | }, |
596 | 597 | "outputs": [], |
597 | 598 | "source": [ |
598 | | - "b4_data = xr.open_dataarray(\"./data/gent/raster/2020-09-17_Sentinel_2_L1C_B04.tiff\", \n", |
| 599 | + "b4_data = xr.open_dataarray(\"./data/gent/raster/2020-09-17_Sentinel_2_L1C_True_color.tiff\", \n", |
599 | 600 | " engine=\"rasterio\", mask_and_scale=False)" |
600 | 601 | ] |
601 | 602 | }, |
|
662 | 663 | "outputs": [], |
663 | 664 | "source": [ |
664 | 665 | "# Create the histogram plots\n", |
665 | | - "fig, (ax0, ax1) = plt.subplots(1, 2, sharey=True)\n", |
| 666 | + "fig, (ax0, ax1) = plt.subplots(1, 2, sharey=True, figsize=(12, 4))\n", |
666 | 667 | "b4_data.plot.hist(bins=30, log=True, ax=ax0)\n", |
667 | 668 | "b4_data_f.plot.hist(bins=30, log=True, ax=ax1);" |
668 | 669 | ] |
669 | 670 | }, |
| 671 | + { |
| 672 | + "cell_type": "markdown", |
| 673 | + "metadata": {}, |
| 674 | + "source": [ |
| 675 | + "This is what the `mask_and_scale` option also does: it reads the nodata value from the metadata in the tiff file and uses this to mask the no-data value with a Nan value." |
| 676 | + ] |
| 677 | + }, |
670 | 678 | { |
671 | 679 | "cell_type": "markdown", |
672 | 680 | "metadata": {}, |
|
1156 | 1164 | "\n", |
1157 | 1165 | "The data set `./data/herstappe/raster/2020-09-17_Sentinel_2_L1C_True_color.tiff` (assign to variable `herstappe_data`) has values ranging in between 0.11325, 0.8575. To improve the quality of the visualization, stretch __each of the layers individually__ to the values to the range 0. to 1. with a linear transformation: \n", |
1158 | 1166 | " \n", |
1159 | | - "$$z_i=\\frac{x_i-\\min(x)}{\\max(x)-\\min(x)}$$\n", |
| 1167 | + "$$z_{i,scaled}=\\frac{z_i-\\min(z)}{\\max(z)-\\min(z)}$$\n", |
1160 | 1168 | "\n", |
1161 | 1169 | "Make a plot of the end result and compare with a plot of the original data. \n", |
1162 | 1170 | "\n", |
|
1251 | 1259 | "- Read the data file and assign to a variable `gent`.\n", |
1252 | 1260 | "- Try to plot it with the `imshow()` method.\n", |
1253 | 1261 | "- Convert the array to a float array and call it `gent_f`.\n", |
1254 | | - "- Convert the max value of 65536 to np.nan (as we have done before).\n", |
1255 | 1262 | "- Now divide the array by 65536 to get our data in a [0-1] range.\n", |
1256 | 1263 | "- Plot the result with the `imshow()` method.\n", |
1257 | 1264 | "\n", |
|
1303 | 1310 | }, |
1304 | 1311 | "outputs": [], |
1305 | 1312 | "source": [ |
1306 | | - "# Convert to float and make 65535 equal to Nan\n", |
1307 | | - "gent_f = gent.astype(float)\n", |
1308 | | - "gent_f = gent_f.where(gent != 65535)" |
| 1313 | + "# Convert to float\n", |
| 1314 | + "gent_f = gent.astype(float)" |
1309 | 1315 | ] |
1310 | 1316 | }, |
1311 | 1317 | { |
|
1648 | 1654 | "metadata": { |
1649 | 1655 | "celltoolbar": "Nbtutor - export exercises", |
1650 | 1656 | "jupytext": { |
1651 | | - "cell_metadata_filter": "-run_control,-deletable,-editable,-jupyter,-slideshow" |
| 1657 | + "cell_metadata_filter": "-run_control,-deletable,-editable,-jupyter,-slideshow", |
| 1658 | + "notebook_metadata_filter": "-jupytext.cell_metadata_filter" |
1652 | 1659 | }, |
1653 | 1660 | "kernelspec": { |
1654 | 1661 | "display_name": "Python 3 (ipykernel)", |
|
1665 | 1672 | "name": "python", |
1666 | 1673 | "nbconvert_exporter": "python", |
1667 | 1674 | "pygments_lexer": "ipython3", |
1668 | | - "version": "3.10.6" |
| 1675 | + "version": "3.11.5" |
1669 | 1676 | }, |
1670 | 1677 | "widgets": { |
1671 | 1678 | "application/vnd.jupyter.widget-state+json": { |
|
0 commit comments