Lab 6#
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 the Sea Surface Temperature Dataset#
Load the sea surface temperature dataset from the NetCDF file (
sea_surface_temperature.nc
).Inspect the
Dataset
object and list all the available variables and dimensions in the dataset.Select the
sst
variable (sea surface temperature).Print the attributes, dimensions, and coordinates of the
sst
variable to understand the metadata.
This exercise allows you to practice selecting specific subsets of data and visualizing SST patterns over a specified period.
Exercise 2: Data Selection and Indexing#
Select a subset of the
sst
data for a specific time (2010-07-01
) and latitude (0.0
), which represents the Equator.Create a time slice for the SST data between January and March 2010 for all latitudes and longitudes.
Plot the time slice as a line plot, showing the latitude-averaged SST over time.
This exercise allows you to practice selecting specific subsets of data and visualizing SST patterns over a specified period.
Exercise 3: Performing Arithmetic Operations#
Compute the mean SST over the entire time range (2010-2015) to obtain the average sea surface temperature for each spatial location.
Calculate the temperature anomalies by subtracting the computed mean from the original SST values. This helps understand how SST deviates from the mean during the time period.
Plot both the mean SST and the anomalies on separate plots to visualize spatial temperature patterns and deviations. You can select a specific time to display the anomalies.
This exercise introduces arithmetic operations on the dataset, focusing on the concept of temperature anomalies.
Exercise 4: GroupBy and Resampling#
Use
groupby
to calculate the seasonal mean SST. Group the data by season (DJF
,MAM
,JJA
, andSON
) and compute the average SST for each season.Resample the dataset to compute the monthly mean SST. This aggregates the data on a monthly basis.
Plot the seasonal mean SST and the monthly mean SST to visualize how sea surface temperature varies by season and by month.
This exercise demonstrates how to group and resample time-series data, commonly used in climate data analysis.
Exercise 5: Writing Data to NetCDF#
Select the SST anomalies calculated in Exercise 3 for further analysis and export.
Convert the
sst
variable tofloat32
to optimize file size before writing the data to a NetCDF file.Write the anomalies data to a new NetCDF file named
sst_anomalies.nc
for storage and future use.Load the saved NetCDF file back into memory and print its contents to verify the saved data.
This exercise teaches how to export processed geospatial data to NetCDF, a widely-used file format in climate data analysis.
Exercise 6: Load and Inspect a Raster Dataset#
Use
rioxarray
to load the GeoTIFF raster file at opengeos/datasets.Inspect the dataset by printing its dimensions, coordinates, and attributes.
Check and print the CRS and affine transformation of the dataset.
Exercise 7: Reproject the Raster to a New CRS#
Reproject the loaded raster dataset from its original CRS to EPSG:4326 (WGS84).
Print the new CRS and check the dimensions and coordinates of the reprojected data.
Plot the original and reprojected datasets for comparison.
Exercise 8: Clip the Raster Using a Bounding Box#
Define a bounding box (e.g.,
xmin
,ymin
,xmax
,ymax
) that covers the land area of Libya.Clip the raster dataset using this bounding box.
Plot the clipped data to visualize the result.
Exercise 9: Mask the Raster Using a Vector Dataset#
Load the GeoJSON file at opengeos/datasets using
geopandas
.Use the GeoJSON to mask the reprojected raster dataset, keeping only the data within the GeoJSON boundaries.
Plot the masked raster data.
Exercise 10: Resample the Raster to a Different Resolution#
Resample the raster dataset to a 3m resolution, using an average resampling method.
Check the new dimensions and coordinates after resampling.
Save the resampled raster dataset as a new GeoTIFF file.