From 83892e588f149ef66270e166b1f52ff9ce8e5373 Mon Sep 17 00:00:00 2001 From: Qiusheng Wu Date: Thu, 3 Oct 2024 00:06:52 -0400 Subject: [PATCH] Add lab6 (#89) --- _toc.yml | 1 + book/geospatial/rioxarray.ipynb | 2 +- book/geospatial/rioxarray.md | 2 +- book/labs/lab_06.ipynb | 222 ++++++++++++++++++++++++++++++++ book/labs/lab_06.md | 129 +++++++++++++++++++ 5 files changed, 354 insertions(+), 2 deletions(-) create mode 100644 book/labs/lab_06.ipynb create mode 100644 book/labs/lab_06.md diff --git a/_toc.yml b/_toc.yml index 10b5b61..b676078 100644 --- a/_toc.yml +++ b/_toc.yml @@ -61,3 +61,4 @@ parts: - file: book/labs/lab_03 - file: book/labs/lab_04 - file: book/labs/lab_05 + - file: book/labs/lab_06 diff --git a/book/geospatial/rioxarray.ipynb b/book/geospatial/rioxarray.ipynb index 195739a..3566a1e 100644 --- a/book/geospatial/rioxarray.ipynb +++ b/book/geospatial/rioxarray.ipynb @@ -612,7 +612,7 @@ "\n", "### Exercise 1: Load and Inspect a Raster Dataset\n", "\n", - "1. Use `rioxarray` to load the GeoTIFF raster file .\n", + "1. Use `rioxarray` to load the GeoTIFF raster file.\n", "2. Inspect the dataset by printing its dimensions, coordinates, and attributes.\n", "3. Check and print the CRS and affine transformation of the dataset." ] diff --git a/book/geospatial/rioxarray.md b/book/geospatial/rioxarray.md index 6b204f6..1dc0299 100644 --- a/book/geospatial/rioxarray.md +++ b/book/geospatial/rioxarray.md @@ -324,7 +324,7 @@ https://github.com/opengeos/datasets/releases/download/raster/Libya-2023-09-13.t ### Exercise 1: Load and Inspect a Raster Dataset -1. Use `rioxarray` to load the GeoTIFF raster file . +1. Use `rioxarray` to load the GeoTIFF raster file. 2. Inspect the dataset by printing its dimensions, coordinates, and attributes. 3. Check and print the CRS and affine transformation of the dataset. diff --git a/book/labs/lab_06.ipynb b/book/labs/lab_06.ipynb new file mode 100644 index 0000000..059ba6b --- /dev/null +++ b/book/labs/lab_06.ipynb @@ -0,0 +1,222 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Lab 6\n", + "\n", + "[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/giswqs/geog-312/blob/main/book/labs/lab_06.ipynb)\n", + "\n", + "## Overview\n", + "\n", + "In this lab, you will practice handling multi-dimensional geospatial datasets using `Xarray` and `Rioxarray`. This includes working with NetCDF climate data and georeferenced raster datasets (GeoTIFF files). You will learn how to perform data selection, arithmetic operations, resampling, and reprojection, as well as saving your results back to disk.\n", + "\n", + "By the end of this lab, you will have a better understanding of how to:\n", + "\n", + "* Work with Xarray datasets (NetCDF format) and inspect geospatial raster data.\n", + "* Apply common data operations like slicing, resampling, and arithmetic analysis.\n", + "* Reproject, clip, mask, and resample raster datasets.\n", + "* Export processed data to various formats such as NetCDF and GeoTIFF.\n", + "\n", + "## Exercise 1: Exploring a New Dataset\n", + "\n", + "1. Load the Xarray tutorial dataset `rasm`.\n", + "2. Inspect the `Dataset` object and list all the variables and dimensions.\n", + "3. Select the `Tair` variable (air temperature).\n", + "4. Print the attributes, dimensions, and coordinates of `Tair`." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Exercise 2: Data Selection and Indexing\n", + "\n", + "1. Select a subset of the `Tair` data for the date `1980-07-01` and latitude `70.0`.\n", + "2. Create a time slice for the entire latitude range between January and March of 1980.\n", + "3. Plot the selected time slice as a line plot." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Exercise 3: Performing Arithmetic Operations\n", + "\n", + "1. Compute the mean of the `Tair` data over the `time` dimension.\n", + "2. Subtract the computed mean from the original `Tair` dataset to get the temperature anomalies.\n", + "3. Plot the mean temperature and the anomalies on separate plots." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Exercise 4: GroupBy and Resampling\n", + "\n", + "1. Use `groupby` to calculate the seasonal mean temperature (`Tair`).\n", + "2. Use `resample` to calculate the monthly mean temperature for 1980.\n", + "3. Plot the seasonal mean for each season and the monthly mean." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Exercise 5: Writing Data to netCDF\n", + "\n", + "1. Select the temperature anomalies calculated in Exercise 3.\n", + "2. Convert the `Tair` variable to `float32` to optimize file size.\n", + "3. Write the anomalies data to a new netCDF file named `tair_anomalies.nc`.\n", + "4. Load the data back from the file and print its contents." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Exercise 6: Load and Inspect a Raster Dataset\n", + "\n", + "1. Use `rioxarray` to load the GeoTIFF raster file at https://github.com/opengeos/datasets/releases/download/raster/Libya-2023-09-13.tif.\n", + "2. Inspect the dataset by printing its dimensions, coordinates, and attributes.\n", + "3. Check and print the CRS and affine transformation of the dataset." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Exercise 7: Reproject the Raster to a New CRS\n", + "\n", + "1. Reproject the loaded raster dataset from its original CRS to EPSG:4326 (WGS84).\n", + "2. Print the new CRS and check the dimensions and coordinates of the reprojected data.\n", + "3. Plot the original and reprojected datasets for comparison." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Exercise 8: Clip the Raster Using a Bounding Box\n", + "\n", + "1. Define a bounding box (e.g., `xmin`, `ymin`, `xmax`, `ymax`) that covers the land area of Libya.\n", + "2. Clip the raster dataset using this bounding box.\n", + "3. Plot the clipped data to visualize the result." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Exercise 9: Mask the Raster Using a Vector Dataset\n", + "\n", + "1. Load the GeoJSON file at https://github.com/opengeos/datasets/releases/download/raster/Derna_Libya.geojson using `geopandas`.\n", + "2. Use the GeoJSON to mask the raster dataset, keeping only the data within the GeoJSON boundaries.\n", + "3. Plot the masked raster data." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Exercise 10: Resample the Raster to a Different Resolution\n", + "\n", + "1. Resample the raster dataset to a 3m resolution, using an average resampling method.\n", + "2. Check the new dimensions and coordinates after resampling.\n", + "3. Save the resampled raster dataset as a new GeoTIFF file." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "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.2" + } + }, + "nbformat": 4, + "nbformat_minor": 4 +} diff --git a/book/labs/lab_06.md b/book/labs/lab_06.md new file mode 100644 index 0000000..20e93b6 --- /dev/null +++ b/book/labs/lab_06.md @@ -0,0 +1,129 @@ +--- +jupytext: + text_representation: + extension: .md + format_name: myst + format_version: 0.13 + jupytext_version: 1.16.4 +kernelspec: + display_name: Python 3 (ipykernel) + language: python + name: python3 +--- + +# Lab 6 + +[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/giswqs/geog-312/blob/main/book/labs/lab_06.ipynb) + +## Overview + +In this lab, you will practice handling multi-dimensional geospatial datasets using `Xarray` and `Rioxarray`. This includes working with NetCDF climate data and georeferenced raster datasets (GeoTIFF files). You will learn how to perform data selection, arithmetic operations, resampling, and reprojection, as well as saving your results back to disk. + +By the end of this lab, you will have a better understanding of how to: + +* Work with Xarray datasets (NetCDF format) and inspect geospatial raster data. +* Apply common data operations like slicing, resampling, and arithmetic analysis. +* Reproject, clip, mask, and resample raster datasets. +* Export processed data to various formats such as NetCDF and GeoTIFF. + +## Exercise 1: Exploring a New Dataset + +1. Load the Xarray tutorial dataset `rasm`. +2. Inspect the `Dataset` object and list all the variables and dimensions. +3. Select the `Tair` variable (air temperature). +4. Print the attributes, dimensions, and coordinates of `Tair`. + +```{code-cell} ipython3 + +``` + +## Exercise 2: Data Selection and Indexing + +1. Select a subset of the `Tair` data for the date `1980-07-01` and latitude `70.0`. +2. Create a time slice for the entire latitude range between January and March of 1980. +3. Plot the selected time slice as a line plot. + +```{code-cell} ipython3 + +``` + +## Exercise 3: Performing Arithmetic Operations + +1. Compute the mean of the `Tair` data over the `time` dimension. +2. Subtract the computed mean from the original `Tair` dataset to get the temperature anomalies. +3. Plot the mean temperature and the anomalies on separate plots. + +```{code-cell} ipython3 + +``` + +## Exercise 4: GroupBy and Resampling + +1. Use `groupby` to calculate the seasonal mean temperature (`Tair`). +2. Use `resample` to calculate the monthly mean temperature for 1980. +3. Plot the seasonal mean for each season and the monthly mean. + +```{code-cell} ipython3 + +``` + +## Exercise 5: Writing Data to netCDF + +1. Select the temperature anomalies calculated in Exercise 3. +2. Convert the `Tair` variable to `float32` to optimize file size. +3. Write the anomalies data to a new netCDF file named `tair_anomalies.nc`. +4. Load the data back from the file and print its contents. + +```{code-cell} ipython3 + +``` + +## Exercise 6: Load and Inspect a Raster Dataset + +1. Use `rioxarray` to load the GeoTIFF raster file at https://github.com/opengeos/datasets/releases/download/raster/Libya-2023-09-13.tif. +2. Inspect the dataset by printing its dimensions, coordinates, and attributes. +3. Check and print the CRS and affine transformation of the dataset. + +```{code-cell} ipython3 + +``` + +## Exercise 7: Reproject the Raster to a New CRS + +1. Reproject the loaded raster dataset from its original CRS to EPSG:4326 (WGS84). +2. Print the new CRS and check the dimensions and coordinates of the reprojected data. +3. Plot the original and reprojected datasets for comparison. + +```{code-cell} ipython3 + +``` + +## Exercise 8: Clip the Raster Using a Bounding Box + +1. Define a bounding box (e.g., `xmin`, `ymin`, `xmax`, `ymax`) that covers the land area of Libya. +2. Clip the raster dataset using this bounding box. +3. Plot the clipped data to visualize the result. + +```{code-cell} ipython3 + +``` + +## Exercise 9: Mask the Raster Using a Vector Dataset + +1. Load the GeoJSON file at https://github.com/opengeos/datasets/releases/download/raster/Derna_Libya.geojson using `geopandas`. +2. Use the GeoJSON to mask the raster dataset, keeping only the data within the GeoJSON boundaries. +3. Plot the masked raster data. + +```{code-cell} ipython3 + +``` + +## Exercise 10: Resample the Raster to a Different Resolution + +1. Resample the raster dataset to a 3m resolution, using an average resampling method. +2. Check the new dimensions and coordinates after resampling. +3. Save the resampled raster dataset as a new GeoTIFF file. + +```{code-cell} ipython3 + +```