import xarray as xr
import pandas as pd
import geopandas as gpd
import numpy as np
import matplotlib.pyplot as plt
import hvplot.xarray
import earthaccess
import zipfile
import warnings
warnings.filterwarnings("ignore", category=FutureWarning, module="earthaccess")
pd.set_option('display.max_columns', None) #make sure all columns displayed for shapefiles
From the PO.DAAC Cookbook, to access the GitHub version of the notebook, follow this link.
SWOT Hydrology Science Application Tutorial on Local Machine
Retrieving SWOT attributes (WSE, width, slope) and plotting a longitudinal profile along a river or over a basin
Requirement
This tutorial can be run on a local environment. For the AWS cloud version of this notebook, click here.
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.
This code runs using SWOT Level 2 Data Products (Version D).
Notebook Authors: Arnaud Cerbelaud, Jeffrey Wade, & Cassie Nickles, NASA Jet Propulsion Laboratory - California Institute of Technology
Last updated: November 2026
Learning Objectives
- Retrieve SWOT hydrological attributes on river reaches. Query reaches by:
- River name
- Downstream tracing from reach id (e.g. headwater to outlet) for river longitudinal profiles
- Upstream tracing from reach id (e.g. outlet to full river network) for watershed analysis
- Plot a time series of WSE, width, slope data on the filtered data
- Visualize an interactive map of WSE, width, slope data on the filtered data
Import Packages
Authenticate
Authenticate your Earthdata Login (EDL) information using the earthaccess python package as follows:
earthaccess.login() # Login with your EDL credentials if asked<earthaccess.auth.Auth at 0x7f7e8cf6de80>
1. Retrieve SWOT hydrological attributes on river reaches within the AWS cloud (Cal/Val data)
What data should we download?
- Optional step: Get the .kmz file of SWOT passes/swaths for the and import it into Google Earth for visualization
- Determine which pass number corresponds to the river/basin you want to look at! #### Search for multiple days of data
# Enter pass number(s)
pass_number = ["354", "091"] #Susquehanna River in NA
# e.g. 341, 576, 298 for Connecticut in NA, "236", "514", "542", "085", "363", "057", "335", "029" for Rhine in EU
# Enter continent code
continent_code = "NA" # e.g. "AF", "NA", "EU", "SI", "AS", "AU", "SA", "AR", "GR"
# Retrieves granulev and links list from the passes we want, in this case by passing to `earthdata.search_data` function the data collection shortname and temporal bounds
links_list = []
for p in range(len(pass_number)):
river_results = earthaccess.search_data(short_name = 'SWOT_L2_HR_RiverSP_D',
temporal = ('2024-01-25 00:00:00', '2024-05-16 23:59:59'),
granule_name = "*Reach*_" + pass_number[p] + "_" + continent_code + "*")
for r in range(len(river_results)):
river_link = earthaccess.results.DataGranule.data_links(river_results[r], access='external')[0]
links_list.append(river_link)
print(f"Found {len(river_results)} granules")Found 6 granules
#links_listDownload files to new data_downloads folder
earthaccess.download(links_list, "./data_downloads")[PosixPath('data_downloads/SWOT_L2_HR_RiverSP_Reach_010_354_NA_20240206T150945_20240206T152044_PGD0_01.zip'),
PosixPath('data_downloads/SWOT_L2_HR_RiverSP_Reach_011_354_NA_20240227T115450_20240227T120549_PGD0_01.zip'),
PosixPath('data_downloads/SWOT_L2_HR_RiverSP_Reach_012_354_NA_20240319T083953_20240319T085052_PGD0_01.zip'),
PosixPath('data_downloads/SWOT_L2_HR_RiverSP_Reach_013_354_NA_20240409T052714_20240409T053605_PGD0_01.zip'),
PosixPath('data_downloads/SWOT_L2_HR_RiverSP_Reach_014_354_NA_20240430T020958_20240430T022110_PGD0_01.zip'),
PosixPath('data_downloads/SWOT_L2_HR_RiverSP_Reach_010_091_NA_20240128T055759_20240128T061253_PGD0_01.zip'),
PosixPath('data_downloads/SWOT_L2_HR_RiverSP_Reach_011_091_NA_20240218T024304_20240218T025758_PGD0_01.zip'),
PosixPath('data_downloads/SWOT_L2_HR_RiverSP_Reach_012_091_NA_20240309T232808_20240309T234301_PGD0_01.zip'),
PosixPath('data_downloads/SWOT_L2_HR_RiverSP_Reach_013_091_NA_20240330T201303_20240330T202814_PGD0_01.zip'),
PosixPath('data_downloads/SWOT_L2_HR_RiverSP_Reach_014_091_NA_20240420T165809_20240420T171320_PGD0_01.zip'),
PosixPath('data_downloads/SWOT_L2_HR_RiverSP_Reach_015_091_NA_20240511T134322_20240511T135815_PGD0_01.zip')]
Unzip selected files
The native format for this data is a .zip file, and we want the .shp file within the .zip file, so we must first extract the data to open it. First, we’ll programmatically get the filename we just downloaded, and then extract all data to the data_downloads folder.
filenames = []
for l in range(len(links_list)):
filenames.append(links_list[l].split("/")[-1])for f in range(len(filenames)):
with zipfile.ZipFile(f'data_downloads/{filenames[f]}', 'r') as zip_ref:
zip_ref.extractall('data_downloads')# replace zip file names with .shp ending
filename_shps = [filename.replace('zip', 'shp') for filename in filenames]#initialize opened list of files
SWOT_HR_shps = []
# Loop through queried granules to open and stack all acquisition dates
for j in range(len(filename_shps)):
SWOT_HR_shps.append(gpd.read_file(f'data_downloads/{filename_shps[j]}')) Aggregate unzipped files into dataframe
# Combine granules from all acquisition dates into one dataframe
SWOT_HR_df = gpd.GeoDataFrame(pd.concat(SWOT_HR_shps, ignore_index=True))
# Sort dataframe by reach_id and time
SWOT_HR_df = SWOT_HR_df.sort_values(['reach_id', 'time'])
SWOT_HR_df| reach_id | time | time_tai | time_str | p_lat | p_lon | river_name | wse | wse_u | wse_r_u | wse_c | wse_c_u | slope | slope_u | slope_r_u | slope2 | slope2_u | slope2_r_u | width | width_u | width_c | width_c_u | area_total | area_tot_u | area_detct | area_det_u | area_wse | d_x_area | d_x_area_u | layovr_val | node_dist | loc_offset | xtrk_dist | dschg_c | dschg_c_u | dschg_csf | dschg_c_q | dschg_gc | dschg_gc_u | dschg_gcsf | dschg_gc_q | dschg_m | dschg_m_u | dschg_msf | dschg_m_q | dschg_gm | dschg_gm_u | dschg_gmsf | dschg_gm_q | dschg_b | dschg_b_u | dschg_bsf | dschg_b_q | dschg_gb | dschg_gb_u | dschg_gbsf | dschg_gb_q | dschg_h | dschg_h_u | dschg_hsf | dschg_h_q | dschg_gh | dschg_gh_u | dschg_ghsf | dschg_gh_q | dschg_o | dschg_o_u | dschg_osf | dschg_o_q | dschg_go | dschg_go_u | dschg_gosf | dschg_go_q | dschg_s | dschg_s_u | dschg_ssf | dschg_s_q | dschg_gs | dschg_gs_u | dschg_gssf | dschg_gs_q | dschg_i | dschg_i_u | dschg_isf | dschg_i_q | dschg_gi | dschg_gi_u | dschg_gisf | dschg_gi_q | dschg_q_b | dschg_gq_b | reach_q | reach_q_b | dark_frac | ice_clim_f | ice_dyn_f | partial_f | n_good_nod | obs_frac_n | xovr_cal_q | geoid_hght | geoid_slop | solid_tide | load_tidef | load_tideg | pole_tide | dry_trop_c | wet_trop_c | iono_c | xovr_cal_c | n_reach_up | n_reach_dn | rch_id_up | rch_id_dn | p_wse | p_wse_var | p_width | p_wid_var | p_n_nodes | p_dist_out | p_length | p_maf | p_dam_id | p_n_ch_max | p_n_ch_mod | p_low_slp | geometry | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | 71353200011 | 7.605474e+08 | 7.605474e+08 | 2024-02-06T15:09:45Z | 55.175425 | -83.807379 | no_data | 12.3940 | 0.72636 | 0.72076 | -1.000000e+12 | -1.000000e+12 | 0.000715 | 0.000096 | 0.000096 | 0.001167 | 0.000011 | 1.006598e-05 | 647.575718 | 6.364634 | -1.000000e+12 | -1.000000e+12 | 9771345.3 | 20672.4890 | 1904578.8 | 20672.5 | 1118046.6 | -1.000000e+12 | -1.000000e+12 | 1.9125 | 218.390544 | 5864.42120 | 9806.92773 | -1.000000e+12 | -1.000000e+12 | -1.000000e+12 | -999 | -1.000000e+12 | -1.000000e+12 | -1.000000e+12 | -999 | -1.000000e+12 | -1.000000e+12 | -1.000000e+12 | -999 | -1.000000e+12 | -1.000000e+12 | -1.000000e+12 | -999 | -1.000000e+12 | -1.000000e+12 | -1.000000e+12 | -999 | -1.000000e+12 | -1.000000e+12 | -1.000000e+12 | -999 | -1.000000e+12 | -1.000000e+12 | -1.000000e+12 | -999 | -1.000000e+12 | -1.000000e+12 | -1.000000e+12 | -999 | -1.000000e+12 | -1.000000e+12 | -1.000000e+12 | -999 | -1.000000e+12 | -1.000000e+12 | -1.000000e+12 | -999 | -1.000000e+12 | -1.000000e+12 | -1.000000e+12 | -999 | -1.000000e+12 | -1.000000e+12 | -1.000000e+12 | -999 | -1.000000e+12 | -1.000000e+12 | -1.000000e+12 | -999 | -1.000000e+12 | -1.000000e+12 | -1.000000e+12 | -999 | 8389049 | 8389049 | 1 | 49166 | 0.094499 | 2 | -999 | 1 | 16 | 0.213333 | 0 | -45.578178 | -0.000002 | -0.087027 | -0.008323 | -0.007471 | 0.005244 | -2.307279 | -0.079216 | -0.005576 | 0.069262 | 1 | 1 | 71353200021, no_data, no_data, no_data | 71353300023, no_data, no_data, no_data | 6.30 | 13.561736 | 105.0 | 2821.089 | 75 | 23931.846 | 15089.116311 | -1.000000e+12 | 0 | 3 | 1 | 0 | LINESTRING (-83.72202 55.19853, -83.72202 55.1... |
| 623 | 71353200011 | 7.623501e+08 | 7.623501e+08 | 2024-02-27T11:54:50Z | 55.175425 | -83.807379 | no_data | 12.4376 | 0.72625 | 0.72065 | -1.000000e+12 | -1.000000e+12 | 0.000727 | 0.000096 | 0.000096 | 0.001191 | 0.000010 | 9.307460e-06 | 780.639040 | 6.542280 | -1.000000e+12 | -1.000000e+12 | 11779153.3 | 21249.4887 | 2351712.5 | 21249.5 | 1071891.3 | -1.000000e+12 | -1.000000e+12 | 1.7425 | 247.494404 | 5865.10192 | 9205.79346 | -1.000000e+12 | -1.000000e+12 | -1.000000e+12 | -999 | -1.000000e+12 | -1.000000e+12 | -1.000000e+12 | -999 | -1.000000e+12 | -1.000000e+12 | -1.000000e+12 | -999 | -1.000000e+12 | -1.000000e+12 | -1.000000e+12 | -999 | -1.000000e+12 | -1.000000e+12 | -1.000000e+12 | -999 | -1.000000e+12 | -1.000000e+12 | -1.000000e+12 | -999 | -1.000000e+12 | -1.000000e+12 | -1.000000e+12 | -999 | -1.000000e+12 | -1.000000e+12 | -1.000000e+12 | -999 | -1.000000e+12 | -1.000000e+12 | -1.000000e+12 | -999 | -1.000000e+12 | -1.000000e+12 | -1.000000e+12 | -999 | -1.000000e+12 | -1.000000e+12 | -1.000000e+12 | -999 | -1.000000e+12 | -1.000000e+12 | -1.000000e+12 | -999 | -1.000000e+12 | -1.000000e+12 | -1.000000e+12 | -999 | -1.000000e+12 | -1.000000e+12 | -1.000000e+12 | -999 | 8389049 | 8389049 | 1 | 49166 | 0.072498 | 2 | -999 | 1 | 16 | 0.213333 | 0 | -45.577788 | -0.000002 | -0.056391 | -0.009737 | -0.010486 | 0.004354 | -2.296100 | -0.011410 | -0.002305 | -0.328151 | 1 | 1 | 71353200021, no_data, no_data, no_data | 71353300023, no_data, no_data, no_data | 6.30 | 13.561736 | 105.0 | 2821.089 | 75 | 23931.846 | 15089.116311 | -1.000000e+12 | 0 | 3 | 1 | 0 | LINESTRING (-83.72202 55.19853, -83.72202 55.1... |
| 1251 | 71353200011 | 7.641528e+08 | 7.641528e+08 | 2024-03-19T08:39:53Z | 55.175425 | -83.807379 | no_data | 11.8920 | 0.73169 | 0.72614 | -1.000000e+12 | -1.000000e+12 | 0.000805 | 0.000097 | 0.000096 | 0.001298 | 0.000024 | 2.358635e-05 | 741.757893 | 7.071239 | -1.000000e+12 | -1.000000e+12 | 11192471.1 | 22967.5585 | 2195898.8 | 22967.6 | 1151614.4 | -1.000000e+12 | -1.000000e+12 | 1.6187 | 209.020435 | 5857.69764 | 9559.22412 | -1.000000e+12 | -1.000000e+12 | -1.000000e+12 | -999 | -1.000000e+12 | -1.000000e+12 | -1.000000e+12 | -999 | -1.000000e+12 | -1.000000e+12 | -1.000000e+12 | -999 | -1.000000e+12 | -1.000000e+12 | -1.000000e+12 | -999 | -1.000000e+12 | -1.000000e+12 | -1.000000e+12 | -999 | -1.000000e+12 | -1.000000e+12 | -1.000000e+12 | -999 | -1.000000e+12 | -1.000000e+12 | -1.000000e+12 | -999 | -1.000000e+12 | -1.000000e+12 | -1.000000e+12 | -999 | -1.000000e+12 | -1.000000e+12 | -1.000000e+12 | -999 | -1.000000e+12 | -1.000000e+12 | -1.000000e+12 | -999 | -1.000000e+12 | -1.000000e+12 | -1.000000e+12 | -999 | -1.000000e+12 | -1.000000e+12 | -1.000000e+12 | -999 | -1.000000e+12 | -1.000000e+12 | -1.000000e+12 | -999 | -1.000000e+12 | -1.000000e+12 | -1.000000e+12 | -999 | 8389049 | 8389049 | 1 | 49166 | 0.088554 | 2 | -999 | 1 | 16 | 0.213333 | 0 | -45.577948 | -0.000003 | -0.038541 | -0.002927 | -0.002592 | 0.003038 | -2.284283 | -0.021854 | -0.002521 | -0.226372 | 1 | 1 | 71353200021, no_data, no_data, no_data | 71353300023, no_data, no_data, no_data | 6.30 | 13.561736 | 105.0 | 2821.089 | 75 | 23931.846 | 15089.116311 | -1.000000e+12 | 0 | 3 | 1 | 0 | LINESTRING (-83.72202 55.19853, -83.72202 55.1... |
| 2293 | 71353200011 | 7.677582e+08 | 7.677582e+08 | 2024-04-30T02:10:02Z | 55.175425 | -83.807379 | no_data | 9.5508 | 0.15799 | 0.12984 | -1.000000e+12 | -1.000000e+12 | 0.000932 | 0.000067 | 0.000066 | 0.001438 | 0.000005 | 4.080360e-06 | 286.068050 | 4.407441 | -1.000000e+12 | -1.000000e+12 | 4316514.1 | 66504.3906 | 4088749.4 | 66504.4 | 1430929.3 | -1.000000e+12 | -1.000000e+12 | 4.1801 | 143.625158 | -1.50156 | 8151.92773 | -1.000000e+12 | -1.000000e+12 | -1.000000e+12 | -999 | -1.000000e+12 | -1.000000e+12 | -1.000000e+12 | -999 | -1.000000e+12 | -1.000000e+12 | -1.000000e+12 | -999 | -1.000000e+12 | -1.000000e+12 | -1.000000e+12 | -999 | -1.000000e+12 | -1.000000e+12 | -1.000000e+12 | -999 | -1.000000e+12 | -1.000000e+12 | -1.000000e+12 | -999 | -1.000000e+12 | -1.000000e+12 | -1.000000e+12 | -999 | -1.000000e+12 | -1.000000e+12 | -1.000000e+12 | -999 | -1.000000e+12 | -1.000000e+12 | -1.000000e+12 | -999 | -1.000000e+12 | -1.000000e+12 | -1.000000e+12 | -999 | -1.000000e+12 | -1.000000e+12 | -1.000000e+12 | -999 | -1.000000e+12 | -1.000000e+12 | -1.000000e+12 | -999 | -1.000000e+12 | -1.000000e+12 | -1.000000e+12 | -999 | -1.000000e+12 | -1.000000e+12 | -1.000000e+12 | -999 | 8389049 | 8389049 | 1 | 16398 | 0.052766 | 2 | -999 | 0 | 46 | 0.613333 | 0 | -45.588418 | -0.000002 | 0.058387 | -0.005001 | -0.005464 | 0.000305 | -2.334146 | -0.024098 | -0.004880 | -0.036721 | 1 | 1 | 71353200021, no_data, no_data, no_data | 71353300023, no_data, no_data, no_data | 6.30 | 13.561736 | 105.0 | 2821.089 | 75 | 23931.846 | 15089.116311 | -1.000000e+12 | 0 | 3 | 1 | 0 | LINESTRING (-83.72202 55.19853, -83.72202 55.1... |
| 1 | 71353200021 | 7.605474e+08 | 7.605474e+08 | 2024-02-06T15:09:46Z | 55.065099 | -83.912437 | no_data | 25.5365 | 0.09256 | 0.02164 | -1.000000e+12 | -1.000000e+12 | 0.000759 | 0.000014 | 0.000013 | 0.000729 | 0.000003 | 7.487900e-07 | 732.445770 | 2.172724 | -1.000000e+12 | -1.000000e+12 | 12040432.8 | 35716.6923 | 11142131.1 | 35716.7 | 8884726.6 | -1.000000e+12 | -1.000000e+12 | 1.4562 | 235.225570 | -2.24822 | 18082.59863 | -1.000000e+12 | -1.000000e+12 | -1.000000e+12 | -999 | -1.000000e+12 | -1.000000e+12 | -1.000000e+12 | -999 | -1.000000e+12 | -1.000000e+12 | -1.000000e+12 | -999 | -1.000000e+12 | -1.000000e+12 | -1.000000e+12 | -999 | -1.000000e+12 | -1.000000e+12 | -1.000000e+12 | -999 | -1.000000e+12 | -1.000000e+12 | -1.000000e+12 | -999 | -1.000000e+12 | -1.000000e+12 | -1.000000e+12 | -999 | -1.000000e+12 | -1.000000e+12 | -1.000000e+12 | -999 | -1.000000e+12 | -1.000000e+12 | -1.000000e+12 | -999 | -1.000000e+12 | -1.000000e+12 | -1.000000e+12 | -999 | -1.000000e+12 | -1.000000e+12 | -1.000000e+12 | -999 | -1.000000e+12 | -1.000000e+12 | -1.000000e+12 | -999 | -1.000000e+12 | -1.000000e+12 | -1.000000e+12 | -999 | -1.000000e+12 | -1.000000e+12 | -1.000000e+12 | -999 | 8389049 | 8389049 | 1 | 14 | 0.074607 | 2 | -999 | 0 | 75 | 0.914634 | 0 | -45.633779 | -0.000007 | -0.087093 | -0.008324 | -0.007440 | 0.005254 | -2.304890 | -0.078703 | -0.005580 | 0.131516 | 1 | 1 | 71353200031, no_data, no_data, no_data | 71353200011, no_data, no_data, no_data | 18.60 | 14.102569 | 78.0 | 521.854 | 82 | 40370.514 | 16438.667968 | -1.000000e+12 | 0 | 2 | 1 | 0 | LINESTRING (-83.82561 55.11666, -83.82606 55.1... |
| ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... |
| 5321 | 77111400141 | 7.615394e+08 | 7.615394e+08 | 2024-02-18T02:43:18Z | 9.250436 | -83.596271 | no_data | 464.9978 | 0.15103 | 0.12128 | -1.000000e+12 | -1.000000e+12 | 0.006302 | 0.000081 | 0.000081 | 0.006622 | 0.000017 | 1.700523e-05 | 234.973830 | 30.974627 | -1.000000e+12 | -1.000000e+12 | 2139717.6 | 203443.2232 | 1360229.4 | 203443.2 | 397062.2 | -1.000000e+12 | -1.000000e+12 | 5.5029 | 118.053166 | -242.43730 | -5242.19629 | -1.000000e+12 | -1.000000e+12 | -1.000000e+12 | -999 | -1.000000e+12 | -1.000000e+12 | -1.000000e+12 | -999 | -1.000000e+12 | -1.000000e+12 | -1.000000e+12 | -999 | -1.000000e+12 | -1.000000e+12 | -1.000000e+12 | -999 | -1.000000e+12 | -1.000000e+12 | -1.000000e+12 | -999 | -1.000000e+12 | -1.000000e+12 | -1.000000e+12 | -999 | -1.000000e+12 | -1.000000e+12 | -1.000000e+12 | -999 | -1.000000e+12 | -1.000000e+12 | -1.000000e+12 | -999 | -1.000000e+12 | -1.000000e+12 | -1.000000e+12 | -999 | -1.000000e+12 | -1.000000e+12 | -1.000000e+12 | -999 | -1.000000e+12 | -1.000000e+12 | -1.000000e+12 | -999 | -1.000000e+12 | -1.000000e+12 | -1.000000e+12 | -999 | -1.000000e+12 | -1.000000e+12 | -1.000000e+12 | -999 | -1.000000e+12 | -1.000000e+12 | -1.000000e+12 | -999 | 8651192 | 8651192 | 2 | 280590 | 0.118636 | 0 | -999 | 0 | 25 | 0.543478 | 0 | 13.933358 | -0.000009 | 0.081260 | -0.018044 | -0.017949 | 0.001682 | -2.179477 | -0.330250 | -0.014243 | 0.168808 | 1 | 1 | 77111400156, no_data, no_data, no_data | 77111400131, no_data, no_data, no_data | 465.75 | 310.206781 | 42.0 | 305.147 | 46 | 144085.138 | 9106.195214 | -1.000000e+12 | 0 | 1 | 1 | 0 | LINESTRING (-83.56668 9.2337, -83.56695 9.2338... |
| 6524 | 77111400141 | 7.633421e+08 | 7.633421e+08 | 2024-03-09T23:28:21Z | 9.250436 | -83.596271 | no_data | 472.5356 | 0.17605 | 0.15130 | -1.000000e+12 | -1.000000e+12 | 0.004579 | 0.000089 | 0.000089 | 0.004630 | 0.000022 | 2.180628e-05 | 95.371337 | 2.110136 | -1.000000e+12 | -1.000000e+12 | 868470.0 | 16359.5116 | 704979.7 | 16359.5 | 184529.9 | -1.000000e+12 | -1.000000e+12 | 4.8068 | 162.159965 | 231.81118 | -7043.45898 | -1.000000e+12 | -1.000000e+12 | -1.000000e+12 | -999 | -1.000000e+12 | -1.000000e+12 | -1.000000e+12 | -999 | -1.000000e+12 | -1.000000e+12 | -1.000000e+12 | -999 | -1.000000e+12 | -1.000000e+12 | -1.000000e+12 | -999 | -1.000000e+12 | -1.000000e+12 | -1.000000e+12 | -999 | -1.000000e+12 | -1.000000e+12 | -1.000000e+12 | -999 | -1.000000e+12 | -1.000000e+12 | -1.000000e+12 | -999 | -1.000000e+12 | -1.000000e+12 | -1.000000e+12 | -999 | -1.000000e+12 | -1.000000e+12 | -1.000000e+12 | -999 | -1.000000e+12 | -1.000000e+12 | -1.000000e+12 | -999 | -1.000000e+12 | -1.000000e+12 | -1.000000e+12 | -999 | -1.000000e+12 | -1.000000e+12 | -1.000000e+12 | -999 | -1.000000e+12 | -1.000000e+12 | -1.000000e+12 | -999 | -1.000000e+12 | -1.000000e+12 | -1.000000e+12 | -999 | 8389049 | 8389049 | 1 | 51214 | 0.046548 | 0 | -999 | 1 | 14 | 0.304348 | 0 | 13.917987 | -0.000018 | -0.246538 | 0.001378 | 0.001583 | 0.001243 | -2.177829 | -0.264017 | -0.016274 | 0.267522 | 1 | 1 | 77111400156, no_data, no_data, no_data | 77111400131, no_data, no_data, no_data | 465.75 | 310.206781 | 42.0 | 305.147 | 46 | 144085.138 | 9106.195214 | -1.000000e+12 | 0 | 1 | 1 | 0 | LINESTRING (-83.56668 9.2337, -83.56695 9.2338... |
| 7744 | 77111400141 | 7.651448e+08 | 7.651448e+08 | 2024-03-30T20:13:25Z | 9.250436 | -83.596271 | no_data | 465.2092 | 0.16858 | 0.14255 | -1.000000e+12 | -1.000000e+12 | 0.006993 | 0.000082 | 0.000082 | 0.007234 | 0.000008 | 7.560180e-06 | 153.785862 | 11.243329 | -1.000000e+12 | -1.000000e+12 | 1400404.1 | 69280.2168 | 785793.7 | 69280.2 | 235104.4 | -1.000000e+12 | -1.000000e+12 | 8.6177 | 133.815390 | -264.15388 | -4548.62793 | -1.000000e+12 | -1.000000e+12 | -1.000000e+12 | -999 | -1.000000e+12 | -1.000000e+12 | -1.000000e+12 | -999 | -1.000000e+12 | -1.000000e+12 | -1.000000e+12 | -999 | -1.000000e+12 | -1.000000e+12 | -1.000000e+12 | -999 | -1.000000e+12 | -1.000000e+12 | -1.000000e+12 | -999 | -1.000000e+12 | -1.000000e+12 | -1.000000e+12 | -999 | -1.000000e+12 | -1.000000e+12 | -1.000000e+12 | -999 | -1.000000e+12 | -1.000000e+12 | -1.000000e+12 | -999 | -1.000000e+12 | -1.000000e+12 | -1.000000e+12 | -999 | -1.000000e+12 | -1.000000e+12 | -1.000000e+12 | -999 | -1.000000e+12 | -1.000000e+12 | -1.000000e+12 | -999 | -1.000000e+12 | -1.000000e+12 | -1.000000e+12 | -999 | -1.000000e+12 | -1.000000e+12 | -1.000000e+12 | -999 | -1.000000e+12 | -1.000000e+12 | -1.000000e+12 | -999 | 8651192 | 8651192 | 2 | 837646 | 0.170765 | 0 | -999 | 1 | 17 | 0.369565 | 0 | 13.926575 | -0.000016 | 0.124483 | 0.014989 | 0.012868 | 0.000746 | -2.171614 | -0.193215 | -0.020247 | 0.022794 | 1 | 1 | 77111400156, no_data, no_data, no_data | 77111400131, no_data, no_data, no_data | 465.75 | 310.206781 | 42.0 | 305.147 | 46 | 144085.138 | 9106.195214 | -1.000000e+12 | 0 | 1 | 1 | 0 | LINESTRING (-83.56668 9.2337, -83.56695 9.2338... |
| 8964 | 77111400141 | 7.669475e+08 | 7.669475e+08 | 2024-04-20T16:58:31Z | 9.250436 | -83.596271 | no_data | 443.7862 | 0.44310 | 0.43386 | -1.000000e+12 | -1.000000e+12 | -0.016440 | 0.000168 | 0.000168 | -0.010069 | 0.000023 | 2.326612e-05 | 154.393204 | 2.933652 | -1.000000e+12 | -1.000000e+12 | 1405934.7 | 20851.6278 | 870789.3 | 20851.6 | 195645.8 | -1.000000e+12 | -1.000000e+12 | 2.9014 | 140.171417 | 153.74930 | -4713.04761 | -1.000000e+12 | -1.000000e+12 | -1.000000e+12 | -999 | -1.000000e+12 | -1.000000e+12 | -1.000000e+12 | -999 | -1.000000e+12 | -1.000000e+12 | -1.000000e+12 | -999 | -1.000000e+12 | -1.000000e+12 | -1.000000e+12 | -999 | -1.000000e+12 | -1.000000e+12 | -1.000000e+12 | -999 | -1.000000e+12 | -1.000000e+12 | -1.000000e+12 | -999 | -1.000000e+12 | -1.000000e+12 | -1.000000e+12 | -999 | -1.000000e+12 | -1.000000e+12 | -1.000000e+12 | -999 | -1.000000e+12 | -1.000000e+12 | -1.000000e+12 | -999 | -1.000000e+12 | -1.000000e+12 | -1.000000e+12 | -999 | -1.000000e+12 | -1.000000e+12 | -1.000000e+12 | -999 | -1.000000e+12 | -1.000000e+12 | -1.000000e+12 | -999 | -1.000000e+12 | -1.000000e+12 | -1.000000e+12 | -999 | -1.000000e+12 | -1.000000e+12 | -1.000000e+12 | -999 | 25428408 | 25428408 | 2 | 314382 | 0.206488 | 0 | -999 | 1 | 8 | 0.173913 | 0 | 13.969287 | 0.000028 | 0.164779 | -0.012810 | -0.010803 | 0.000268 | -2.183164 | -0.214158 | -0.020495 | 0.158683 | 1 | 1 | 77111400156, no_data, no_data, no_data | 77111400131, no_data, no_data, no_data | 465.75 | 310.206781 | 42.0 | 305.147 | 46 | 144085.138 | 9106.195214 | -1.000000e+12 | 0 | 1 | 1 | 0 | LINESTRING (-83.56668 9.2337, -83.56695 9.2338... |
| 10167 | 77111400141 | 7.687502e+08 | 7.687503e+08 | 2024-05-11T13:43:35Z | 9.250436 | -83.596271 | no_data | 443.3540 | 0.45298 | 0.44395 | -1.000000e+12 | -1.000000e+12 | 0.001568 | 0.000150 | 0.000150 | 0.002577 | 0.000023 | 2.250824e-05 | 164.826504 | 13.177444 | -1.000000e+12 | -1.000000e+12 | 1500942.3 | 83409.9421 | 966086.9 | 83409.9 | 222724.4 | -1.000000e+12 | -1.000000e+12 | 2.1682 | 94.512951 | -138.66408 | -5875.23999 | -1.000000e+12 | -1.000000e+12 | -1.000000e+12 | -999 | -1.000000e+12 | -1.000000e+12 | -1.000000e+12 | -999 | -1.000000e+12 | -1.000000e+12 | -1.000000e+12 | -999 | -1.000000e+12 | -1.000000e+12 | -1.000000e+12 | -999 | -1.000000e+12 | -1.000000e+12 | -1.000000e+12 | -999 | -1.000000e+12 | -1.000000e+12 | -1.000000e+12 | -999 | -1.000000e+12 | -1.000000e+12 | -1.000000e+12 | -999 | -1.000000e+12 | -1.000000e+12 | -1.000000e+12 | -999 | -1.000000e+12 | -1.000000e+12 | -1.000000e+12 | -999 | -1.000000e+12 | -1.000000e+12 | -1.000000e+12 | -999 | -1.000000e+12 | -1.000000e+12 | -1.000000e+12 | -999 | -1.000000e+12 | -1.000000e+12 | -1.000000e+12 | -999 | -1.000000e+12 | -1.000000e+12 | -1.000000e+12 | -999 | -1.000000e+12 | -1.000000e+12 | -1.000000e+12 | -999 | 8651192 | 8651192 | 2 | 311310 | 0.074018 | 0 | -999 | 1 | 6 | 0.130435 | 0 | 13.936392 | 0.000015 | -0.160424 | -0.004390 | -0.003783 | -0.000135 | -2.179108 | -0.338441 | -0.006660 | -0.115766 | 1 | 1 | 77111400156, no_data, no_data, no_data | 77111400131, no_data, no_data, no_data | 465.75 | 310.206781 | 42.0 | 305.147 | 46 | 144085.138 | 9106.195214 | -1.000000e+12 | 0 | 1 | 1 | 0 | LINESTRING (-83.56668 9.2337, -83.56695 9.2338... |
10168 rows × 127 columns
Exploring the dataset
What acquisition dates and rivers do our downloaded files cover?
print('Available dates are:')
print(np.unique([i[:10] for i in SWOT_HR_df['time_str']]))
print('Available rivers are:')
print(np.unique([i for i in SWOT_HR_df['river_name']]))Available dates are:
['2024-01-28' '2024-02-06' '2024-02-18' '2024-02-27' '2024-03-09'
'2024-03-19' '2024-03-30' '2024-04-09' '2024-04-20' '2024-04-30'
'2024-05-11' 'no_data']
Available rivers are:
['Abitibi River' 'Albany River' 'Allegheny River' 'Anacostia River'
'Appomattox River' 'Attawapiskat River' 'Back River' 'Black River'
'Blanche River' 'Caloosahatchee River' 'Cape Fear River' 'Cashie River'
'Catawba River' 'Cattaraugus Creek' 'Chenango River' 'Chickahominy River'
'Chuckatuck Creek' 'Chuckatuck Creek; Elizabeth River'
'Chuckatuck Creek; Jones Creek; Pagan River' 'Conodoguinet Creek'
'Cooper River' 'Crowe River' 'Dan River' 'Deep River' 'Elizabeth River'
'Frederick House River' 'Genesee River' 'Goose Creek' 'Grasse River'
'Great Pee Dee River' 'Great Pee Dee River; Waccamaw River' 'Haw River'
'James River' 'Jones Creek' 'Juniata River' 'Koksoak River'
'Kwetabohigan River' 'Lower Potomac River' 'Madawaska River'
'Mattagami River' 'Mattawa River' 'Missinabi River' 'Mohawk River'
'Monocacy River' 'Monocacy River; Upper Potomac River' 'Moose River'
'Nanjemoy Creek' 'Nansemond River' 'Neuse River' 'Niagara River'
'Nomini Creek' 'North Branch Susquehanna River' 'North French River'
'North Santee River' 'Northeast Cape Fear River' 'Oneida River'
'Oswegatchie River' 'Oswego River' 'Otonabee River' 'Pagan River'
'Pagan River; Warwick River' 'Racquette River' 'Rappahannock River'
'Raystown Branch Juniata River' 'Roanoke River' 'Saint Lawrence River'
'Saint Regis River' 'Santee River' 'Seneca River'
'South Fork Shenandoah River' 'South Nation River' 'South Santee River'
'St. Johns River' 'Susquehanna River' 'Swatara Creek' 'Trent River'
'Upper Potomac River' 'Waccamaw River' 'Wando River' 'Warwick River'
'West Branch Oswegatchie River' 'West Branch Susquehanna River'
'West Branch of the Saint Regis R' 'Western Branch Elizabeth River'
'no_data']
Filter dataframe by river name of interest and plot selected reaches:
Note: Some rivers have multiple names, hence using the contains function
# Enter river name
river = "Susquehanna River" # e.g. "Rhine", "Connecticut River"
## Filter dataframe
SWOT_HR_df_river = SWOT_HR_df[(SWOT_HR_df.river_name.str.contains(river))]
# Plot geopandas dataframe with 'explore' by reach id
SWOT_HR_df_river[['reach_id','river_name','geometry']].explore('reach_id', style_kwds=dict(weight=6))2. River longitudinal profile: trace reaches downstream of given starting reach using rch_id_dn field
WARNING: This works as long as the data is exhaustive (no missing SWORD reaches)
First, let’s set up a dictionary relating all reaches in the dataset to their downstream neighbor
Note: rch_dn_dict[rch_id] gives a list of all the reaches directly downstream from rch_id
# Format rch_id_dn for dictionary. Rch_id_dn allows for multiple downstream reaches to be stored
# Also removes spaces in attribute field
rch_id_dn = [[x.strip() for x in SWOT_HR_df.rch_id_dn[j].split(',')] for j in range(0,len(SWOT_HR_df.rch_id_dn))]
# Filter upstream reach ids to remove 'no_data'
rch_id_dn_filter = [[x for x in dn_id if x.isnumeric()] for dn_id in rch_id_dn]
# Create lookup dictionary for river network topology: Downstream
rch_dn_dict = {SWOT_HR_df.reach_id[i]: rch_id_dn_filter[i] for i in range(len(SWOT_HR_df))}Then, starting from a given reach, let’s trace all connected downstream reaches
Find the Reach IDs on www.swordexplorer.com
# Enter reach_id from which we will trace downstream (e.g. headwaters of the Connecticut River)
rch_dn_st = {"Connecticut River": '73120000691',
"Rhine": '23267000651',
"Susquehanna River": '73160300071'
}
# Initialize list to store downstream reaches, including starting reach
rch_dn_list = [rch_dn_st[river]]
# Retrieve first downstream id of starting reach and add to list
rch_dn_next = rch_dn_dict[rch_dn_st[river]][0]
# Trace next downstream reach until we hit the outlet (or here the last reach on file)
while len(rch_dn_next) != 0:
# Add reach to list if value exists
if len(rch_dn_next) != 0:
rch_dn_list.append(rch_dn_next)
# Recursively retrieve first downstream id of next reach
# Catch error if reach isn't in downloaded data
try:
rch_dn_next = rch_dn_dict[rch_dn_next][0]
except:
breakFinally, we filtered our downloaded data by the traced reaches to create a plot
# Filter downloaded data by downstream traced reaches
SWOT_dn_trace = SWOT_HR_df[SWOT_HR_df.reach_id.isin(rch_dn_list)]
# Remove reaches from rch_dn_list that are not present in SWOT data
rch_dn_list = [rch for rch in rch_dn_list if rch in SWOT_HR_df.reach_id.values]
SWOT_dn_trace[['reach_id','river_name','geometry']].explore('river_name', style_kwds=dict(weight=6))Note: If not all of your data appears, there might be a SWORD database error
Let’s check by singling out the reach it left off on.
For example, in the Connecticut River, it looks like reach ‘73120000531’ at the time of this run (4/1/24) has ‘no_data’ in the rch_id_up and rch_id_dn fields when there should be values. To help out the SWORD database creators, we can report this discrepancy by clicking the Report Reach button at https://www.swordexplorer.com/. SWORD should be updated quarterly.
SWOT_HR_df[SWOT_HR_df.reach_id == '73120000531']3. Watershed analysis: trace reaches upstream of starting reach using rch_id_up field
WARNING: This works as long as the data is exhaustive (no missing SWORD reaches)
First, let’s set up a dictionary relating all reaches in the dataset to their upstream neighbor
Note: rch_up_dict[rch_id] gives a list of all the reaches directly upstream from rch_id
# Format rch_id_up for dictionary. Rch_id_up allows for multiple upstream reaches to be stored
# Also removes spaces in attribute field
rch_id_up = [[x.strip() for x in SWOT_HR_df.rch_id_up[j].split(',')] for j in range(0,len(SWOT_HR_df.rch_id_up))]
# Filter upstream reach ids to remove 'no_data'
rch_id_up_fil = [[x for x in ups_id if x.isnumeric()] for ups_id in rch_id_up]
# Create lookup dictionary for river network topology: Upstream
rch_up_dict = {SWOT_HR_df.reach_id[i]: rch_id_up_fil[i] for i in range(len(SWOT_HR_df))}Then, starting from a given reach, let’s trace all connected upstream reaches
This adds a bit of complexity, as we need to keep track of multiple branches upstream of the starting reach. Find outlet Reach IDs on www.swordexplorer.com
# Enter reach_id from which we will trace upstream (e.g. outlet of the River)
rch_up_st = {"Connecticut River": '73120000021',
"Rhine": '23265000051',
"Susquehanna River": '73160100011'
}
# Initialize list to store traced upstream reaches, including starting reach
rch_up_list = [rch_up_st[river]]
# Retrieve ids of reaches upstream of starting reach and add to list
rch_up_next = rch_up_dict[rch_up_st[river]]
# For upstream tracing, we need to set a list of next upstream ids to start while loop
rch_next_id = rch_up_next
# Loop until no more reaches to trace
while len(rch_next_id) != 0:
# Initialize list to store next upstream ids
rch_next_id = []
# Loop through next upstream ids for given reach
for rch_up_sel in rch_up_next:
# Get values of existing upstream ids of rch_up_next reaches
# If reach isn't in SWOT data (usually ghost reaches), continue to next reach
try:
rch_next_id = rch_next_id + rch_up_dict[rch_up_sel]
except:
continue
# Append id to list
rch_up_list.append(rch_up_sel)
# If reaches exist, add to list for next cycle of tracing
if len(rch_next_id) != 0:
rch_up_next = rch_next_idFinally, we filtered our downloaded data by the traced reaches to create a plot
# Filter downloaded data by upstream traced reaches
SWOT_up_trace = SWOT_HR_df[SWOT_HR_df.reach_id.isin(rch_up_list)]
# Remove reaches from rch_up_list that are not present in SWOT data
rch_up_list = [rch for rch in rch_up_list if rch in SWOT_HR_df.reach_id.values]
SWOT_up_trace[['reach_id','river_name','geometry']].explore('river_name', style_kwds=dict(weight=6))Note: If not all of your data appears, there might be a SWORD database error
Let’s check by singling out the reach it left off on.
For example, in the Connecticut River, it looks like reach ‘73120000131’ at the time of this run (4/1/24) has ‘no_data’ in the rch_id_up field when there should be values. To help out the SWORD database creators, we can report this discrepancy by clicking the Report Reach button at https://www.swordexplorer.com/. SWORD should be updated quarterly.
SWOT_HR_df[SWOT_HR_df.reach_id == '73120000131']4. Visualize and plot a time series of WSE/width/slope longitudinal profiles
Let’s create a time series dataframe from the downstream filtered database
# Retrieve all possible acquisition dates (keeping only YYYY-MM-DD)
dates = np.unique([i[:10] for i in [x for x in SWOT_HR_df['time_str'] if x!='no_data']])
# Create a new database for time series analysis with unique reach_ids
SWOT_dn_trace_time = SWOT_dn_trace.set_index('reach_id').groupby(level=0) \
.apply(lambda df: df.reset_index(drop=True)) \
.unstack().sort_index(axis=1, level=1)
SWOT_dn_trace_time.columns = ['{}_{}'.format(x[0],dates[x[1]]) for x in SWOT_dn_trace_time.columns]Plot a longitudinal profile for selected SWOT variable
# Explore variables you could choose to plot
for var in ["wse","slope","width","len"]:
print(SWOT_dn_trace.columns[SWOT_dn_trace.columns.str.contains(var)])Index(['wse', 'wse_u', 'wse_r_u', 'wse_c', 'wse_c_u', 'area_wse', 'p_wse',
'p_wse_var'],
dtype='str')
Index(['slope', 'slope_u', 'slope_r_u', 'slope2', 'slope2_u', 'slope2_r_u'], dtype='str')
Index(['width', 'width_u', 'width_c', 'width_c_u', 'p_width'], dtype='str')
Index(['p_length'], dtype='str')
# Enter variable of interest for plotting
varstr = "wse"The error that outputs the following cell is because of the SWORD database discrepancy noted above. When this is corrected, only the graph should appear without the error.
# Find cumulative length on the longitudinal profile
length_list = np.nan_to_num([SWOT_dn_trace.p_length[SWOT_dn_trace.reach_id == rch].mean()/1000 for rch in rch_dn_list])
cumlength_list = np.cumsum(length_list)
## Plot a longitudinal profile from the downstream tracing database
## Plot a longitudinal profile from the downstream tracing database
plt.figure(figsize=(12,8))
for t in dates:
# Store the quantity of interest (wse, width etc.) at time t
value = SWOT_dn_trace_time.loc[rch_dn_list,varstr+'_'+t]
# Remove set negative values (bad observations) to NaN and forward fill NaNs
value[value < 0] = np.nan
value = value.ffill()
# Plot the data
plt.plot(cumlength_list, value, label = varstr+'_'+t)
plt.xlabel('Downstream Distance (km)')
plt.ylabel(varstr)
plt.legend()
Map the longitudinal profile of selected SWOT variable
# Choose a date
date = dates[1]#Set one column as the active geometry in the new database
SWOT_dn_trace_time = SWOT_dn_trace_time.set_geometry("geometry_"+date)
#Set cleaner colorbar bounds for better visualization
vmin = np.percentile([i for i in SWOT_dn_trace_time[varstr+'_'+date] if i>0],5)
vmax = np.percentile([i for i in SWOT_dn_trace_time[varstr+'_'+date] if i>0],95)
# Interactive map
SWOT_dn_trace_time.explore(varstr+'_'+date,
vmin = vmin,
vmax = vmax,
cmap = "Blues", #"Blues",
control_scale = True,
tooltip = varstr+'_'+date, # show "varstr+'_'+dates[0]" value in tooltip (on hover)
popup = True, # show all values in popup (on click)
#tiles = "CartoDB positron", # use "CartoDB positron" tiles
style_kwds=dict(weight=10)
)Supplemental
We can also map the upstream traced database
SWOT_up_trace_time = SWOT_up_trace.set_index('reach_id').groupby(level=0) \
.apply(lambda df: df.reset_index(drop=True)) \
.unstack().sort_index(axis=1, level=1)
SWOT_up_trace_time.columns = ['{}_{}'.format(x[0],dates[x[1]]) for x in SWOT_up_trace_time.columns]
SWOT_up_trace_time = SWOT_up_trace_time.set_geometry("geometry_"+date)
#Set cleaner colorbar bounds for better visualization
vmin = np.percentile([i for i in SWOT_up_trace_time[varstr+'_'+date] if i>0],5)
vmax = np.percentile([i for i in SWOT_up_trace_time[varstr+'_'+date] if i>0],95)
# Interactive map
SWOT_up_trace_time.explore(varstr+'_'+date,
vmin = vmin,
vmax = vmax,
cmap = "Blues", #"Blues",
control_scale = True,
tooltip = varstr+'_'+date, # show "varstr+'_'+dates[0]" value in tooltip (on hover)
popup = True, # show all values in popup (on click)
tiles = "OpenStreetMap", # use "CartoDB positron" tiles
style_kwds=dict(weight=5)
)