import os
import glob
import h5netcdf
import xarray as xr
import pandas as pd
import geopandas as gpd
import contextily as cx
import numpy as np
import matplotlib.pyplot as plt
import hvplot.xarray
import zipfile
import earthaccess
import warnings
warnings.filterwarnings("ignore", category=FutureWarning)From the PO.DAAC Cookbook, to access the GitHub version of the notebook, follow this link.
SWOT Hydrology Dataset Exploration on a local machine
Accessing and Visualizing SWOT Datasets
Requirement:
Local compute environment e.g. laptop, server: this tutorial can be run on your local machine.
Learning Objectives:
- Access SWOT HR data products (archived in NASA Earthdata Cloud) by downloading to local machine
- Visualize accessed data for a quick check
SWOT Level 2 KaRIn High Rate Version D Datasets:
River Vector Shapefile - SWOT_L2_HR_RIVERSP_D
Lake Vector Shapefile - SWOT_L2_HR_LAKESP_D
Water Mask Pixel Cloud NetCDF - SWOT_L2_HR_PIXC_D
Water Mask Pixel Cloud Vector Attribute NetCDF - SWOT_L2_HR_PIXCVec_D
Raster NetCDF - SWOT_L2_HR_Raster_D
Single Look Complex Data product - SWOT_L1B_HR_SLC_D
Notebook Author: Cassie Nickles, NASA PO.DAAC (Feb 2024) || Other Contributors: Zoe Walschots (PO.DAAC Summer Intern 2023), Catalina Taglialatela (NASA PO.DAAC), Luis Lopez (NASA NSIDC DAAC), Brent Williams (NASA JPL), Nicholas Tarpinian (NASA PO.DAAC) (Nov 2026)
Last updated: November 2026
Libraries Needed
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. If you don’t already have one, 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. We use earthaccess to authenticate your login credentials below.
auth = earthaccess.login()Single File Access
1. River Vector Shapefiles
The https access link can be found using earthaccess data search. Since this collection consists of Reach and Node files, we need to extract only the granule for the Reach file. We do this by filtering for the ‘Reach’ title in the data link.
Alternatively, Earthdata Search (see tutorial) can be used to manually search in a GUI interface.
For additional tips on spatial searching of SWOT HR L2 data, see also PO.DAAC Cookbook - SWOT Chapter tips section.
Search for the data of interest
#Retrieves granule from the day we want, in this case by passing to `earthdata.search_data` function the data collection shortname, temporal bounds, and filter by wildcards
river_results = earthaccess.search_data(short_name = 'SWOT_L2_HR_RiverSP_D',
temporal = ('2024-02-01 00:00:00', '2024-07-15 23:59:59'), # can also specify by time
granule_name = '*Reach*_287_NA*') # here we filter by Reach files (not node), pass=287, continent code=NA
print(f"Found {len(river_results)} granules")Found 9 granules
Dowload, unzip, read the data
Let’s download the first data file! earthaccess.download has a list as the input format, so we need to put brackets around the single file we pass.
earthaccess.download([river_results[0]], "./data_downloads")[PosixPath('data_downloads/SWOT_L2_HR_RiverSP_Reach_010_287_NA_20240204T060400_20240204T061541_PGD0_01.zip')]
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.
filename = earthaccess.results.DataGranule.data_links(river_results[0], access='external')
filename = filename[0].split("/")[-1]
filename'SWOT_L2_HR_RiverSP_Reach_010_287_NA_20240204T060400_20240204T061541_PGD0_01.zip'
with zipfile.ZipFile(f'data_downloads/{filename}', 'r') as zip_ref:
zip_ref.extractall('data_downloads')Open the shapefile using geopandas
filename_shp = filename.replace('.zip','.shp')SWOT_HR_shp1 = gpd.read_file(f'data_downloads/{filename_shp}')
#view the attribute table
SWOT_HR_shp1 | reach_id | time | time_tai | time_str | p_lat | p_lon | river_name | wse | wse_u | wse_r_u | ... | 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 | 71224800073 | -1.000000e+12 | -1.000000e+12 | no_data | 48.721718 | -92.406994 | no_data | -1.000000e+12 | -1.000000e+12 | -1.000000e+12 | ... | 232341.227 | 90 | 1768199.245 | 18019.914551 | -1.000000e+12 | 0 | 2 | 1 | 0 | LINESTRING (-92.51093 48.70847, -92.51052 48.7... |
| 1 | 71224800081 | -1.000000e+12 | -1.000000e+12 | no_data | 48.739149 | -92.290107 | no_data | -1.000000e+12 | -1.000000e+12 | -1.000000e+12 | ... | 767.700 | 6 | 1769349.955 | 1150.710141 | -1.000000e+12 | 0 | 2 | 1 | 0 | LINESTRING (-92.29723 48.73905, -92.29682 48.7... |
| 2 | 71224800094 | -1.000000e+12 | -1.000000e+12 | no_data | 48.743363 | -92.283339 | no_data | -1.000000e+12 | -1.000000e+12 | -1.000000e+12 | ... | 2911.208 | 3 | 1769941.108 | 591.152774 | -1.000000e+12 | 312 | 2 | 1 | 0 | LINESTRING (-92.28594 48.74103, -92.28569 48.7... |
| 3 | 71224800103 | 7.603424e+08 | 7.603424e+08 | 2024-02-04T06:13:25Z | 48.751442 | -92.242669 | no_data | 3.573636e+02 | 3.541900e-01 | 3.425700e-01 | ... | 57688.777 | 31 | 1776077.746 | 6136.638581 | -1.000000e+12 | 0 | 2 | 1 | 0 | LINESTRING (-92.28196 48.74559, -92.28163 48.7... |
| 4 | 71224800113 | 7.603424e+08 | 7.603424e+08 | 2024-02-04T06:13:25Z | 48.762334 | -92.189341 | no_data | 3.568195e+02 | 2.215800e-01 | 2.024800e-01 | ... | 20821.463 | 13 | 1778617.305 | 2539.558538 | -1.000000e+12 | 0 | 3 | 1 | 0 | LINESTRING (-92.20553 48.75837, -92.20512 48.7... |
| ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... |
| 1055 | 77127000061 | 7.603419e+08 | 7.603419e+08 | 2024-02-04T06:04:26Z | 18.112694 | -98.776172 | no_data | 6.574899e+02 | 7.421400e-01 | 7.366700e-01 | ... | 281.012 | 77 | 648208.288 | 15423.229958 | -1.000000e+12 | 0 | 2 | 1 | 0 | LINESTRING (-98.81309 18.06512, -98.8128 18.06... |
| 1056 | 77127000071 | -1.000000e+12 | -1.000000e+12 | no_data | 18.094132 | -98.694466 | no_data | -1.000000e+12 | -1.000000e+12 | -1.000000e+12 | ... | 414.760 | 54 | 658944.243 | 10735.955307 | -1.000000e+12 | 0 | 2 | 1 | 0 | LINESTRING (-98.7177 18.11625, -98.71764 18.11... |
| 1057 | 77127000081 | -1.000000e+12 | -1.000000e+12 | no_data | 18.093657 | -98.658640 | no_data | -1.000000e+12 | -1.000000e+12 | -1.000000e+12 | ... | 436.883 | 54 | 669675.936 | 10731.692763 | -1.000000e+12 | 0 | 2 | 1 | 0 | LINESTRING (-98.66628 18.07224, -98.66611 18.0... |
| 1058 | 77127000121 | 7.603419e+08 | 7.603419e+08 | 2024-02-04T06:04:26Z | 18.047708 | -98.761798 | no_data | 6.574580e+02 | 8.277400e-01 | 8.228300e-01 | ... | 784.041 | 67 | 646253.540 | 13468.481775 | -1.000000e+12 | 0 | 2 | 1 | 0 | LINESTRING (-98.81252 18.06539, -98.81223 18.0... |
| 1059 | 77127000131 | -1.000000e+12 | -1.000000e+12 | no_data | 17.980635 | -98.689634 | no_data | -1.000000e+12 | -1.000000e+12 | -1.000000e+12 | ... | 824.145 | 97 | 665637.353 | 19383.813426 | -1.000000e+12 | 0 | 3 | 1 | 0 | LINESTRING (-98.71239 18.03246, -98.71239 18.0... |
1060 rows × 127 columns
Quickly plot the SWOT river data
# Simple plot
fig, ax = plt.subplots(figsize=(10,7))
SWOT_HR_shp1.plot(ax=ax, color='black')
cx.add_basemap(ax, crs=SWOT_HR_shp1.crs, source=cx.providers.OpenTopoMap)
# Another way to plot geopandas dataframes is with `explore`, which also plots a basemap
#SWOT_HR_shp1.explore()2. Lake Vector Shapefiles
The lake vector shapefiles can be accessed in the same way as the river shapefiles above.
For additional tips on spatial searching of SWOT HR L2 data, see also PO.DAAC Cookbook - SWOT Chapter tips section.
Search for data of interest
lake_results = earthaccess.search_data(short_name = 'SWOT_L2_HR_LakeSP_2.0',
temporal = ('2024-02-01 00:00:00', '2024-07-15 23:59:59'), # can also specify by time
granule_name = '*Prior*_287_NA*') # here we filter by files with 'Prior' in the name (This collection has three options: Obs, Unassigned, and Prior), pass 287 and continent code=NA
print(f"Found {len(lake_results)} granules")Found 8 granules
Let’s download the first data file! earthaccess.download has a list as the input format, so we need to put brackets around the single file we pass.
earthaccess.download([lake_results[0]], "./data_downloads")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 SWOT_downloads folder.
filename2 = earthaccess.results.DataGranule.data_links(lake_results[0], access='external')
filename2 = filename2[0].split("/")[-1]
filename2'SWOT_L2_HR_LakeSP_Prior_010_287_NA_20240204T060400_20240204T061541_PIC0_01.zip'
with zipfile.ZipFile(f'data_downloads/{filename2}', 'r') as zip_ref:
zip_ref.extractall('data_downloads')Open the shapefile using geopandas
filename_shp2 = filename2.replace('.zip','.shp')
filename_shp2'SWOT_L2_HR_LakeSP_Prior_010_287_NA_20240204T060400_20240204T061541_PIC0_01.shp'
SWOT_HR_shp2 = gpd.read_file(f'data_downloads/{filename_shp2}')
#view the attribute table
SWOT_HR_shp2| lake_id | reach_id | obs_id | overlap | n_overlap | time | time_tai | time_str | wse | wse_u | ... | lake_name | p_res_id | p_lon | p_lat | p_ref_wse | p_ref_area | p_date_t0 | p_ds_t0 | p_storage | geometry | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | 7120822822 | no_data | 712239R999998 | 99 | 1 | 7.603424e+08 | 7.603424e+08 | 2024-02-04T06:13:08Z | 5.281870e+02 | 1.500000e-02 | ... | no_data | -99999999 | -91.557528 | 47.616292 | -1.000000e+12 | 1.038600 | no_data | -1.000000e+12 | -1.000000e+12 | MULTIPOLYGON (((-91.56583 47.612, -91.56589 47... |
| 1 | 7120822902 | no_data | no_data | no_data | no_data | -1.000000e+12 | -1.000000e+12 | no_data | -1.000000e+12 | -1.000000e+12 | ... | no_data | -99999999 | -91.623241 | 47.756499 | -1.000000e+12 | 0.113400 | no_data | -1.000000e+12 | -1.000000e+12 | None |
| 2 | 7120822932 | no_data | no_data | no_data | no_data | -1.000000e+12 | -1.000000e+12 | no_data | -1.000000e+12 | -1.000000e+12 | ... | TONY LAKE | -99999999 | -91.635242 | 47.726123 | -1.000000e+12 | 0.017100 | no_data | -1.000000e+12 | -1.000000e+12 | None |
| 3 | 7120822982 | no_data | no_data | no_data | no_data | -1.000000e+12 | -1.000000e+12 | no_data | -1.000000e+12 | -1.000000e+12 | ... | no_data | -99999999 | -91.665522 | 47.705366 | -1.000000e+12 | 0.026100 | no_data | -1.000000e+12 | -1.000000e+12 | None |
| 4 | 7120823182 | no_data | no_data | no_data | no_data | -1.000000e+12 | -1.000000e+12 | no_data | -1.000000e+12 | -1.000000e+12 | ... | HEART LAKE | -99999999 | -91.651807 | 47.769148 | -1.000000e+12 | 0.124200 | no_data | -1.000000e+12 | -1.000000e+12 | None |
| ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... |
| 65495 | 7130133552 | no_data | 713244L999972;713244L000002 | 73;2 | 2 | 7.603424e+08 | 7.603424e+08 | 2024-02-04T06:14:01Z | 3.926960e+02 | 5.900000e-02 | ... | no_data | -99999999 | -90.889026 | 50.669027 | -1.000000e+12 | 0.695690 | no_data | -1.000000e+12 | -1.000000e+12 | POLYGON ((-90.8921 50.67709, -90.89185 50.6770... |
| 65496 | 7130141612 | no_data | 713245L999974;713245L000001 | 31;5 | 2 | 7.603424e+08 | 7.603425e+08 | 2024-02-04T06:14:08Z | 3.741850e+02 | 8.200000e-02 | ... | LAKE ST JOSEPH;ST JOSEPH | -99999999 | -90.750682 | 51.061383 | -1.000000e+12 | 0.256500 | no_data | -1.000000e+12 | -1.000000e+12 | MULTIPOLYGON (((-90.75251 51.06305, -90.75228 ... |
| 65497 | 7420206383 | 74226000013;74227100043;74227100013;7422710006... | 742214L000175;742214L999934;742214L000500;7422... | 64;23;0;0 | 4 | 7.603421e+08 | 7.603422e+08 | 2024-02-04T06:09:01Z | 1.875740e+02 | 1.000000e-03 | ... | LAKE TEXOMA | 1135 | -96.688976 | 33.901142 | -1.000000e+12 | 257.028517 | no_data | -1.000000e+12 | -1.000000e+12 | MULTIPOLYGON (((-96.70899 33.82534, -96.70885 ... |
| 65498 | 7420280413 | 74246000423;74246000413;74246000404 | 742218L999996;742218L001654;742219L999885 | 4;0;0 | 3 | 7.603422e+08 | 7.603422e+08 | 2024-02-04T06:09:48Z | 1.941800e+02 | 2.100000e-02 | ... | OOLAGAHL LAKE;OOLOGAH LAKE | 1032 | -95.593848 | 36.550604 | -1.000000e+12 | 123.796498 | no_data | -1.000000e+12 | -1.000000e+12 | MULTIPOLYGON (((-95.67217 36.43803, -95.67157 ... |
| 65499 | 7710056183 | 77125000273;77125000263;77125000283;7712500030... | 771186L999995;771186L000013;771186L999993 | 12;1;0 | 3 | 7.603419e+08 | 7.603419e+08 | 2024-02-04T06:04:22Z | 4.586500e+02 | 3.400000e-02 | ... | PRESA EL CARACOL | 1384 | -99.861530 | 17.975030 | -1.000000e+12 | 35.410155 | no_data | -1.000000e+12 | -1.000000e+12 | MULTIPOLYGON (((-99.75753 18.02115, -99.75739 ... |
65500 rows × 51 columns
Quickly plot the SWOT lakes data
fig, ax = plt.subplots(figsize=(10,7))
SWOT_HR_shp2.plot(ax=ax, color='black')
cx.add_basemap(ax, crs=SWOT_HR_shp2.crs, source=cx.providers.OpenTopoMap)
Accessing the remaining files is different than the shp files above. We do not need to extract the shapefiles from a zip file because the following SWOT HR collections are stored in netCDF files in the cloud. For the rest of the products, we will open via xarray, not geopandas.
3. Water Mask Pixel Cloud NetCDF
Search for data collection and time of interest
pixc_results = earthaccess.search_data(short_name = 'SWOT_L2_HR_PIXC_D',
temporal = ('2024-01-25 00:00:00', '2024-05-29 23:59:59'), # can also specify by time
bounding_box = (-106.62, 38.809, -106.54, 38.859)) # Lake Travis near Austin, TX
print(f"Found {len(pixc_results)} granules")Found 30 granules
Let’s download one data file! earthaccess.download has a list as the input format, so we need to put brackets around the single file we pass.
earthaccess.download([pixc_results[0]], "./data_downloads")Open data using xarray
The pixel cloud netCDF files are formatted with three groups titled, “pixel cloud”, “tvp”, or “noise” (more detail here). In order to access the coordinates and variables within the file, a group must be specified when calling xarray open_dataset.
ds_PIXC = xr.open_mfdataset("data_downloads/SWOT_L2_HR_PIXC_*.nc", group = 'pixel_cloud', engine='h5netcdf')
ds_PIXC<xarray.Dataset> Size: 1GB
Dimensions: (points: 3944651, complex_depth: 2,
num_pixc_lines: 3278)
Coordinates:
latitude (points) float64 32MB dask.array<chunksize=(493082,), meta=np.ndarray>
longitude (points) float64 32MB dask.array<chunksize=(493082,), meta=np.ndarray>
Dimensions without coordinates: points, complex_depth, num_pixc_lines
Data variables: (12/62)
azimuth_index (points) float64 32MB dask.array<chunksize=(986163,), meta=np.ndarray>
range_index (points) float64 32MB dask.array<chunksize=(986163,), meta=np.ndarray>
interferogram (points, complex_depth) float32 32MB dask.array<chunksize=(1314884, 1), meta=np.ndarray>
power_plus_y (points) float32 16MB dask.array<chunksize=(986163,), meta=np.ndarray>
power_minus_y (points) float32 16MB dask.array<chunksize=(986163,), meta=np.ndarray>
coherent_power (points) float32 16MB dask.array<chunksize=(986163,), meta=np.ndarray>
... ...
pixc_line_qual (num_pixc_lines) float64 26kB dask.array<chunksize=(3278,), meta=np.ndarray>
pixc_line_to_tvp (num_pixc_lines) float32 13kB dask.array<chunksize=(3278,), meta=np.ndarray>
data_window_first_valid (num_pixc_lines) float64 26kB dask.array<chunksize=(3278,), meta=np.ndarray>
data_window_last_valid (num_pixc_lines) float64 26kB dask.array<chunksize=(3278,), meta=np.ndarray>
data_window_first_cross_track (num_pixc_lines) float32 13kB dask.array<chunksize=(3278,), meta=np.ndarray>
data_window_last_cross_track (num_pixc_lines) float32 13kB dask.array<chunksize=(3278,), meta=np.ndarray>
Attributes:
description: cloud of geolocated interferogram pixels
interferogram_size_azimuth: 3278
interferogram_size_range: 5718
looks_to_efflooks: 1.5506655036039314
num_azimuth_looks: 7.0
azimuth_offset: 6For plotting PIXC using classification and quality flags
# mask to get good water pixels
mask = np.where(np.logical_and(ds_PIXC.classification > 2, ds_PIXC.geolocation_qual <28800))
#For more conservative filtering, use ds_PIXC.geolocation_qual <4 to also remove suspect values
plt.scatter(x=ds_PIXC.longitude[mask], y=ds_PIXC.latitude[mask], c=ds_PIXC.height[mask])
plt.colorbar().set_label('Height (m)')
4. Water Mask Pixel Cloud Vector Attribute NetCDF
Search for data of interest
#Let's plot the same pass and tile as the above
pixcvec_results = earthaccess.search_data(short_name = 'SWOT_L2_HR_PIXCVec_D',
granule_name = '*010_106_086R*') #The same cycle, pass and tile as previously downloaded
print(f"Found {len(pixcvec_results)} granules")Found 1 granules
Let’s download the first data file! earthaccess.download has a list as the input format, so we need to put brackets around the single file we pass.
earthaccess.download([pixcvec_results[0]], "./data_downloads")Open data using xarray
First, we’ll programmatically get the filename we just downloaded and then view the file via xarray.
ds_PIXCVEC = xr.open_mfdataset("data_downloads/SWOT_L2_HR_PIXCVec_*.nc", decode_cf=False, engine='h5netcdf')
ds_PIXCVEC<xarray.Dataset> Size: 308MB
Dimensions: (points: 3944651, nchar_reach_id: 11,
nchar_node_id: 14, nchar_lake_id: 10,
nchar_obs_id: 13)
Dimensions without coordinates: points, nchar_reach_id, nchar_node_id,
nchar_lake_id, nchar_obs_id
Data variables:
azimuth_index (points) int32 16MB dask.array<chunksize=(3944651,), meta=np.ndarray>
range_index (points) int32 16MB dask.array<chunksize=(3944651,), meta=np.ndarray>
latitude_vectorproc (points) float64 32MB dask.array<chunksize=(1972326,), meta=np.ndarray>
longitude_vectorproc (points) float64 32MB dask.array<chunksize=(1972326,), meta=np.ndarray>
height_vectorproc (points) float32 16MB dask.array<chunksize=(3944651,), meta=np.ndarray>
reach_id (points, nchar_reach_id) |S1 43MB dask.array<chunksize=(1972326, 6), meta=np.ndarray>
node_id (points, nchar_node_id) |S1 55MB dask.array<chunksize=(1972326, 7), meta=np.ndarray>
lake_id (points, nchar_lake_id) |S1 39MB dask.array<chunksize=(1972326, 5), meta=np.ndarray>
obs_id (points, nchar_obs_id) |S1 51MB dask.array<chunksize=(1972326, 5), meta=np.ndarray>
ice_clim_f (points) int8 4MB dask.array<chunksize=(3944651,), meta=np.ndarray>
ice_dyn_f (points) int8 4MB dask.array<chunksize=(3944651,), meta=np.ndarray>
Attributes: (12/46)
Conventions: CF-1.7
title: Level 2 KaRIn high rate pixel cloud vect...
short_name: L2_HR_PIXCVec
institution: JPL
source: Ka-band radar interferometer
history: 2025-11-28T10:37:52.143646Z: Creation
... ...
xref_prior_lake_db_file: SWOT_LakeDatabase_Nom_106_20000101T00000...
xref_reforbittrack_files: SWOT_RefOrbitTrackTileBoundary_Nom_20000...
xref_param_l2_hr_laketile_file: SWOT_Param_L2_HR_LakeTile_20000101T00000...
ellipsoid_semi_major_axis: 6378137.0
ellipsoid_flattening: 0.0033528106647474805
is_complete: TrueSimple plot
pixcvec_htvals = ds_PIXCVEC.height_vectorproc.compute()
pixcvec_latvals = ds_PIXCVEC.latitude_vectorproc.compute()
pixcvec_lonvals = ds_PIXCVEC.longitude_vectorproc.compute()
#Before plotting, we set all fill values to nan so that the graph shows up better spatially
pixcvec_htvals[pixcvec_htvals > 15000] = np.nan
pixcvec_latvals[pixcvec_latvals == 0] = np.nan
pixcvec_lonvals[pixcvec_lonvals == 0] = np.nanplt.scatter(x=pixcvec_lonvals, y=pixcvec_latvals, c=pixcvec_htvals)
plt.colorbar().set_label('Height (m)')
5. Raster NetCDF
Search for data of interest
raster_results = earthaccess.search_data(short_name = 'SWOT_L2_HR_Raster_D',
temporal = ('2024-01-25 00:00:00', '2024-07-15 23:59:59'), # can also specify by time
granule_name = '*100m*', # here we filter by files with '100m' in the name (This collection has two resolution options: 100m & 250m)
bounding_box = (-106.62, 38.809, -106.54, 38.859)) # Lake Travis near Austin, TX
print(f"Found {len(raster_results)} granules")Found 129 granules
Let’s download one data file.
earthaccess.download([raster_results[0]], "./data_downloads")Open data with xarray
First, we’ll programmatically get the filename we just downloaded and then view the file via xarray.
ds_raster = xr.open_mfdataset(f'data_downloads/SWOT_L2_HR_Raster*', engine='h5netcdf', combine='nested', concat_dim='x')
ds_raster<xarray.Dataset> Size: 622MB
Dimensions: (x: 1782, y: 1782)
Coordinates:
* x (x) float64 14kB 4.485e+05 4.486e+05 ... 6.266e+05
* y (y) float64 14kB 7.584e+06 7.584e+06 ... 7.762e+06
Data variables: (12/39)
crs (x) object 14kB b'1' b'1' b'1' ... b'1' b'1' b'1'
longitude (y, x) float64 25MB dask.array<chunksize=(594, 594), meta=np.ndarray>
latitude (y, x) float64 25MB dask.array<chunksize=(594, 594), meta=np.ndarray>
wse (y, x) float32 13MB dask.array<chunksize=(891, 891), meta=np.ndarray>
wse_qual (y, x) float32 13MB dask.array<chunksize=(1782, 1782), meta=np.ndarray>
wse_qual_bitwise (y, x) float64 25MB dask.array<chunksize=(891, 891), meta=np.ndarray>
... ...
load_tide_fes (y, x) float32 13MB dask.array<chunksize=(891, 891), meta=np.ndarray>
load_tide_got (y, x) float32 13MB dask.array<chunksize=(891, 891), meta=np.ndarray>
pole_tide (y, x) float32 13MB dask.array<chunksize=(891, 891), meta=np.ndarray>
model_dry_tropo_cor (y, x) float32 13MB dask.array<chunksize=(891, 891), meta=np.ndarray>
model_wet_tropo_cor (y, x) float32 13MB dask.array<chunksize=(891, 891), meta=np.ndarray>
iono_cor_gim_ka (y, x) float32 13MB dask.array<chunksize=(891, 891), meta=np.ndarray>
Attributes: (12/49)
Conventions: CF-1.7
title: Level 2 KaRIn High Rate Raster Data Product
institution: JPL
source: Ka-band radar interferometer
history: 2025-11-25T14:03:21Z : Creation
platform: SWOT
... ...
utm_zone_num: 60
mgrs_latitude_band: W
x_min: 448500.0
x_max: 626600.0
y_min: 7583700.0
y_max: 7761800.0Quick interactive plot with hvplot
Note: this is not filtered by quality
ds_raster.wse.hvplot.quadmesh(y='y', x='x')6. SLC
Search for data collection and time of interest
slc_results = earthaccess.search_data(short_name = 'SWOT_L1B_HR_SLC_D',
#temporal = ('2024-01-01 00:00:00', '2024-01-30 23:59:59'), # can also specify by time
bounding_box = (-106.62, 38.809, -106.54, 38.859)) # Lake Travis near Austin, TX
print(f"Found {len(slc_results)} granules")Found 275 granules
Let’s download one data file! earthaccess.download has a list as the input format, so we need to put brackets around the single file we pass.
earthaccess.download([slc_results[0]], "./data_downloads")Open data using xarray
The L1B_HR_SLC product file contains five NetCDF data group called the slc, xfactor, noise, tvp, and grdem groups. More info can be found in the product description document within the dataset table for each group.
# Find all SLC granules and open them without loading the data into memory.
# chunks={} makes xarray read the data in small pieces later, when a result is requested.
paths = sorted(glob.glob("data_downloads/SWOT_L1B_HR_SLC*.nc"))
dsets = [xr.open_dataset(p, group='slc', engine='h5netcdf', chunks={}) for p in paths]
# Why not open_mfdataset? The files have different sizes (e.g. 22956 vs 22804 lines),
# and SLC data has no shared coordinates to align on, so we stack them manually.
# The range dimension (num_pixels) differs by one pixel between files (5622 vs 5623).
# Trim every file to the smallest width so they can be stacked.
# This drops one range pixel from the wider files. That is fine for viewing, but not
# for phase-sensitive processing that relies on the original pixel grid.
n = min(d.sizes['num_pixels'] for d in dsets)
trimmed = [d.isel(num_pixels=slice(0, n)) for d in dsets]
# Stack the granules end to end along-track. The 'minimal'/'override' options keep
# variables without a num_lines dimension (scalars, metadata) from being duplicated,
# using the first file's copy.
ds_SLC = xr.concat(trimmed, dim='num_lines',
data_vars='minimal', coords='minimal', compat='override')
ds_SLC<xarray.Dataset> Size: 2GB
Dimensions: (num_lines: 22956, num_pixels: 5623, complex_depth: 2)
Dimensions without coordinates: num_lines, num_pixels, complex_depth
Data variables:
slc_plus_y (num_lines, num_pixels, complex_depth) float32 1GB dask.array<chunksize=(3280, 804, 1), meta=np.ndarray>
slc_minus_y (num_lines, num_pixels, complex_depth) float32 1GB dask.array<chunksize=(3280, 804, 1), meta=np.ndarray>
slc_qual (num_lines) float32 92kB dask.array<chunksize=(22956,), meta=np.ndarray>
Attributes:
description: Single look complex images for plus_y and minus_y channelsPlot SLC data
# function to get interferogram from slc images
# Multilook size: number of pixels averaged into one output pixel (azimuth x range).
# Larger values use less memory but give a coarser image.
looks_az, looks_rg = 30, 5
def interfere_multilook(slc, looks_az, looks_rg):
# Combine the real and imaginary layers into complex arrays.
# These lines only define the calculation; no data is read yet.
slc_plus_y = slc.slc_plus_y[:, :, 0] + 1j * slc.slc_plus_y[:, :, 1]
slc_minus_y = slc.slc_minus_y[:, :, 0] + 1j * slc.slc_minus_y[:, :, 1]
# Interferogram: plus_y times the complex conjugate of minus_y
ifgram = slc_plus_y * np.conj(slc_minus_y)
# Average over blocks BEFORE plotting so only a small array is held in memory.
# Average the complex values, not the phase angles, so the phase wrap at +/- pi
# doesn't corrupt the result. boundary='trim' drops leftover edge pixels.
ifgram_ml = ifgram.coarsen(num_lines=looks_az, num_pixels=looks_rg, boundary='trim').mean()
# .compute() runs the calculation now: the files are read in pieces and only
# the small averaged result is kept in memory.
ifgram_ml = ifgram_ml.compute()
mag = np.abs(ifgram_ml)
phase = np.arctan2(np.imag(ifgram_ml), np.real(ifgram_ml))
return ifgram_ml, mag, phase
ifgram, mag, phase = interfere_multilook(ds_SLC, looks_az, looks_rg)
print(ifgram.shape)(765, 1124)
# ignore the divide-by-zero warning in log10
warnings.filterwarnings("ignore")
# make a plot of slc interferogram
# Magnitude in dB (grayscale) with the phase overlaid semi-transparent
plt.figure(figsize=(8, 10))
plt.imshow(10*np.log10(mag), interpolation='none', cmap='gray', aspect='auto')
plt.clim((0, 90))
plt.colorbar().set_label('interferogram magnitude (dB)')
plt.imshow(phase, interpolation='none', cmap='hsv', alpha=0.5, aspect='auto')
plt.colorbar().set_label('interferometric phase')
# Amplitude image for the plus_y channel, using the same multilook size as the interferogram
# Combine the real and imaginary layers into a complex array.
# This line only defines the calculation; no data is read yet.
slc_plus_y = ds_SLC.slc_plus_y[:, :, 0] + 1j * ds_SLC.slc_plus_y[:, :, 1]
# Average over blocks (looks_az x looks_rg pixels) first, then run the calculation with
# .compute(). The files are read in pieces and only the small averaged result stays in memory.
power_plus_y = (np.abs(slc_plus_y)
.coarsen(num_lines=looks_az, num_pixels=looks_rg, boundary='trim')
.mean()
.compute())
plt.figure(figsize=(8, 10))
plt.imshow(10*np.log10(power_plus_y), interpolation='none', cmap='gray', aspect='auto')
plt.clim((0, 45))
plt.colorbar().set_label('interferometric power (plus_y channel)')