import os
import glob
from pathlib import Path
import numpy as np
import pandas as pd
import geopandas as gpd
import matplotlib.pyplot as plt
import contextily as cx
import zipfile
import earthaccess
From the PO.DAAC Cookbook, to access the GitHub version of the notebook, follow this link.
SWOT Date/Time Transformation
Authored by Nicholas Tarpinian, PO.DAAC
Summary
The following workflow lets you create time series plots with various Geographical Information System (GIS) Desktop softwares by transforming SWOT_L2_HR_RiverSP_2.0 Shapefile vector datasets.
Requirements
1. Compute environment
- Local compute environment e.g. laptop, server: this tutorial can be run on your local machine.
2. Earthdata Login
An Earthdata Login account is required to access data, as well as discover restricted data, from the NASA Earthdata system. Thus, to access NASA data, you need Earthdata Login. Please visit https://urs.earthdata.nasa.gov to register and manage your Earthdata Login account. This account is free to create and only takes a moment to set up.
Learning Objectives:
- Accessing SWOT shapefile hydrology dataset through earthaccess and downloading it locally.
- Utilizing geoprocessing tools with GIS Desktop Softwares; ArcGIS Pro and QGIS.
- Transforming ‘time_str’ attribute/variable to a Date/Time data type.
- Utilizing the new data type variable to create a time series plot/chart.
Import libraries
Authentication with earthaccess
In this notebook, we will be calling the authentication in the below cell.
= earthaccess.login() auth
Search using earthaccess
for SWOT River Reaches
Each dataset has it’s own unique collection concept ID. For this dataset it is SWOT_L2_HR_RiverSP_2.0. SWOT files come in “reach” and “node” versions in this same collection, here we want the 10km reaches rather than the nodes. We will also only get files for North America, or ‘NA’.
= earthaccess.search_data(short_name = 'SWOT_L2_HR_RIVERSP_2.0',
results = ('2024-02-01 00:00:00', '2024-02-29 23:59:59'), # can also specify by time
temporal = '*Reach*_NA_*') # here we filter by Reach files (not node), continent code=NA granule_name
Granules found: 192
Download the Data into a folder
"../datasets/data_downloads/SWOT_files") earthaccess.download(results,
Unzip shapefiles to existing folder
= Path("../datasets/data_downloads/SWOT_files")
folder for item in os.listdir(folder): # loop through items in dir
if item.endswith(".zip"): # check for ".zip" extension
= zipfile.ZipFile(f"{folder}/{item}") # create zipfile object
zip_ref # extract file to dir
zip_ref.extractall(folder) # close file zip_ref.close()
Merging multiple reaches to a single shapefile
Since a time series plot will be created, merging all the shapefiles to one will be the better option.
# State filename extension to look for within folder, in this case .shp which is the shapefile
= folder.glob("*.shp")
shapefiles
# Merge/Combine multiple shapefiles in folder into one
= pd.concat([
gdf
gpd.read_file(shp)for shp in shapefiles
]).pipe(gpd.GeoDataFrame)
# Export merged geodataframe into shapefile
/ 'SWOTReaches.shp') gdf.to_file(folder
ArcGIS Pro
Esri’s ArcGIS Pro offers a geoproccesing tool called ‘Convert Time Field’.
This allows the transformation of date and time values to be created from one field to another.
In this case the date/time field ‘time_str’ is in a text data type format. It needs to be converted to a date/time data type in order to create a time series plot.
The ‘no_data’ values do not get converted; it keeps it as ‘NULL’
Once the new field is created, this allows for the creation of a time series plot.
This is a great way to observe an area with multiple days of measurements.
In this case, we will configure a line plot in the chart properties of the median of water surface elevation ‘wse’.
QGIS
QGIS offers a geoproccesing tool called ‘Refactor Fields’.
This modifies the date/time values of the current field and creates a new layer.
In this case the date/time field ‘time_str’ can be changed to a date/time data type.
In the ‘type’ section, you can select the data type of your choosing but for this case we will select ‘Date & Time’.
The new layer can be saved to memory as a temporary layer, but its best to save the layer locally.
The new layer created does not include the ‘no_data’ values.
As shown in the image below; the black symbology is the original layer and the red symbology is the newly created layer without the ‘no_data’ values.
Within the processing toolbox, to create a time series select the ‘Plots’ category to show the various ways of plotting data.
There also is the option of using the plugin DataPlotly, which utilizes the python library Plotly.
This offers a variety of ways to plot and customize your charts.
For this example, we will configure a box plot looking at both time and ‘wse’. Also, querying ‘wse’ to only include values greater than 0, so it does not include any negative values.
This plot showcases the data without the date/time transformation for a single month.
This plot is the same single month dataset but with the date/time transformation.
This plot showcases a single pass river reach as the same transformed image from above.