|
| 1 | +SWOT River Time-Series Tutorial in R |
| 2 | +================ |
| 3 | +Navid Khizri (NASA JPL PO.DAAC Summer Intern; Alaska Pacific University |
| 4 | +Student) |
| 5 | + |
| 6 | +**Summary** |
| 7 | + |
| 8 | +This introductory tutorial guides students through plotting a river |
| 9 | +water surface elevation (WSE) time series in R using observations from |
| 10 | +NASA’s Surface Water and Ocean Topography (SWOT) mission. Students will |
| 11 | +learn how to locate river reach identifiers (reach_id) in the SWOT River |
| 12 | +Database (SWORD), request observations via the Hydrocron API, clean |
| 13 | +missing-data placeholders, and visualize the results using base R. |
| 14 | + |
| 15 | +**Requirements** |
| 16 | + |
| 17 | +Any compute environment (local RStudio or cloud-based RStudio). |
| 18 | + |
| 19 | +**Learning Objectives** |
| 20 | + |
| 21 | +- Identify river reach IDs of interest using the SWOT River Database |
| 22 | + (SWORD) <https://www.swordexplorer.com/>. |
| 23 | +- Interact with Hydrocron <https://podaac.github.io/hydrocron/>, NASA’s |
| 24 | + API for accessing SWOT hydrology time series. |
| 25 | +- Clean raw river height and slope time series. |
| 26 | +- Plot a time series using base R. |
| 27 | + |
| 28 | +**Why SWOT Matters** |
| 29 | + |
| 30 | +SWOT (Surface Water and Ocean Topography) is the first satellite mission |
| 31 | +to survey nearly 90% of Earth’s rivers, lakes, and hydrologic systems. |
| 32 | +SWOT uses Ka-band radar interferometry to measure water surface |
| 33 | +elevation globally, providing data crucial for hydrology, flood |
| 34 | +forecasting, and water management. |
| 35 | + |
| 36 | +Alaska, with more than 12,000 rivers and limited stream gauges due to |
| 37 | +high installation and maintenance costs, stands to benefit |
| 38 | +significantly. SWOT’s wide-swath coverage provides consistent 21‑day |
| 39 | +revisit observations — helping fill data gaps for remote communities and |
| 40 | +improving hydrologic modeling, especially under climate change. |
| 41 | + |
| 42 | +This tutorial shows an example of exploring river observations from SWOT |
| 43 | +for a river in Alaska. |
| 44 | + |
| 45 | +As a student at Alaska Pacific University in Anchorage, I had the |
| 46 | +privilege of learning about Alaska Native communities. Many of these |
| 47 | +communities are accessible only by plane or boat, and face increasing |
| 48 | +flood risks driven by climate change. SWOT can help improve flood models |
| 49 | +and provide critical water-level data to support flood readiness in |
| 50 | +rural Alaska. For additional reading, see Water Mission to Gauge Alaskan |
| 51 | +Rivers on Front Lines of Climate Change. |
| 52 | + |
| 53 | +Link: |
| 54 | +<https://sealevel.jpl.nasa.gov/news/1599/water-mission-to-gauge-alaskan-rivers-on-front-lines-of-climate-change/> |
| 55 | + |
| 56 | +Explore the SWORD River Database to find your river reach_id of interest |
| 57 | + |
| 58 | +Visit the interactive dashboard: |
| 59 | + |
| 60 | +**<https://www.swordexplorer.com/>** |
| 61 | + |
| 62 | +``` r |
| 63 | +library(knitr) |
| 64 | +``` |
| 65 | + |
| 66 | +<!-- --> |
| 67 | + |
| 68 | +Click on the North American basin, then click on one of the numbers that |
| 69 | +has your river in it. I will click on Alaska which is \#81. |
| 70 | + |
| 71 | +<!-- --> |
| 72 | + |
| 73 | +After clicking on the basin you will see colorful lines which represent |
| 74 | +reach_ids. You can zoom in on the map into the river area of interest. |
| 75 | + |
| 76 | +<!-- --> |
| 77 | + |
| 78 | +The example in this tutorial uses reaches from the Kuskokwim River in |
| 79 | +Southwestern Alaska. In the right-hand corner, you can see fields that |
| 80 | +are available. For now, just keep reach_id selected. Hovering the mouse |
| 81 | +over a river reach will display information about that reach, including |
| 82 | +the reach ID. When you find your reach ID, note it because will use it |
| 83 | +later when creating a time series. |
| 84 | + |
| 85 | +**What is Hydrocron?** |
| 86 | + |
| 87 | +Hydrocron is an API developed by NASA’s PO.DAAC that provides |
| 88 | +time-series hydrology data from SWOT in formats such as GeoJSON and CSV. |
| 89 | +At the time of the making of this tutorial each request retrieves data |
| 90 | +for a **single reach_id**. |
| 91 | + |
| 92 | +**Required Packages** |
| 93 | + |
| 94 | +``` r |
| 95 | +library(httr) |
| 96 | +library(jsonlite) |
| 97 | +``` |
| 98 | + |
| 99 | +**Functions Used in This Tutorial** |
| 100 | + |
| 101 | +To simplify utilization of this workflow, several helper functions are |
| 102 | +defined. - This part of the tutorial typically would only need to be run |
| 103 | +once. - After that, if the user wishes to change the Hydrocron API query |
| 104 | +parameters (start_time,end_time,fields), they can do so in the next |
| 105 | +section below: Fetch, Clean, and Plot. - If a user wishes to modify the |
| 106 | +API parameters requested, some modification of the helper functions may |
| 107 | +be needed. |
| 108 | + |
| 109 | +What the functions do: get_reach_data() creates the url to connect with |
| 110 | +Hydrocron API to get your river reach data. |
| 111 | + |
| 112 | +clean_hydrocron() filters out missing data. |
| 113 | + |
| 114 | +plot_reach() plots the data. |
| 115 | + |
| 116 | +``` r |
| 117 | +# Fetch Hydrocron data |
| 118 | +# Send a request to NASA's Hydrocron API, download SWOT river data, and prepare it for R. |
| 119 | + |
| 120 | +get_reach_data <- function(reach_id, start_time, end_time, fields = "reach_id,time_str,wse,slope") { |
| 121 | + |
| 122 | + # Constructs a valid Hydrocron API URL by plugging in: your reach_id, your start date, your end date |
| 123 | + url <- paste0( |
| 124 | + "https://soto.podaac.earthdatacloud.nasa.gov/hydrocron/v1/timeseries?", |
| 125 | + "feature=Reach", |
| 126 | + "&feature_id=", reach_id, |
| 127 | + "&output=geojson", |
| 128 | + "&start_time=", start_time, |
| 129 | + "&end_time=", end_time, |
| 130 | + "&fields=", fields |
| 131 | + ) |
| 132 | + |
| 133 | + # R sends the request to NASA's servers |
| 134 | + res <- GET(url) |
| 135 | + |
| 136 | + # Convert returned JSON into an R list |
| 137 | + geo <- fromJSON(content(res, "text")) |
| 138 | + |
| 139 | + # Extract the actual river measurements |
| 140 | + data <- geo$results$geojson$features$properties |
| 141 | + |
| 142 | + # Convert time strings into real time stamps |
| 143 | + data$time <- as.POSIXct(data$time_str, format = "%Y-%m-%dT%H:%M:%SZ", tz = "UTC") |
| 144 | + |
| 145 | + # Return the clean data table |
| 146 | + return(data) |
| 147 | +} |
| 148 | + |
| 149 | + |
| 150 | +clean_hydrocron <- function(df) { |
| 151 | + |
| 152 | + # Convert wse and slope to numeric. Hydrocron stores data as characters strings instead of numeric which will crash the plot if not converted. |
| 153 | + df$wse <- as.numeric(df$wse) |
| 154 | + df$slope <- as.numeric(df$slope) |
| 155 | + |
| 156 | + # Remove rows with "no_data" |
| 157 | + df <- df[df$time_str != "no_data", ] |
| 158 | + |
| 159 | + # Remove fill value rows |
| 160 | + df <- df[df$wse != -999999999999.0, ] |
| 161 | + df <- df[df$slope != -999999999999.0, ] |
| 162 | + |
| 163 | + # Remove rows where timestamp conversion failed |
| 164 | + df <- df[!is.na(df$time), ] |
| 165 | + |
| 166 | + # Output the cleaned dataset |
| 167 | + return(df) |
| 168 | +} |
| 169 | + |
| 170 | + |
| 171 | +# Plot a SWOT river reach |
| 172 | +plot_reach <- function(data, title = "SWOT River Time-Series") { |
| 173 | + |
| 174 | + plot( |
| 175 | + data$time, data$wse, |
| 176 | + main = title, |
| 177 | + xlab = "Time", |
| 178 | + ylab = "Water Surface Elevation (m)", |
| 179 | + col = "red", |
| 180 | + pch = 16 |
| 181 | + ) |
| 182 | + |
| 183 | + lines(data$time, data$wse, col = "black") |
| 184 | + grid() # Improves readability for students |
| 185 | +} |
| 186 | +``` |
| 187 | + |
| 188 | +**Fetch, Clean, and Plot SWOT Data** |
| 189 | + |
| 190 | +In this example, we request all observations for reach `81181700021` |
| 191 | +from 2023–2026. A user can change these inputs to request different time |
| 192 | +periods and/or river IDs. User only needs to re-run the cell below when |
| 193 | +modifying the query parameters(reach_id,start_time,end_time). |
| 194 | + |
| 195 | +Note: At the time of writing this tutorial, there is a limit on how much |
| 196 | +data the API can query. If you’re reach is too large, consider breaking |
| 197 | +the query up into smaller requests. If interested in 2023 to 2027 data, |
| 198 | +you could do two queries: 2023-10-01 to 2025-05-31 and 2025-06-01 to |
| 199 | +2027-07-25. |
| 200 | + |
| 201 | +``` r |
| 202 | +data_raw <- get_reach_data( |
| 203 | + reach_id = "81181700021", # Insert your reach ID here |
| 204 | + start_time = "2025-06-01T00:00:00Z", # Insert Start Date |
| 205 | + end_time = "2027-07-25T00:00:00Z" # Insert End Date |
| 206 | +) |
| 207 | +``` |
| 208 | + |
| 209 | + ## No encoding supplied: defaulting to UTF-8. |
| 210 | + |
| 211 | +``` r |
| 212 | +data <- clean_hydrocron(data_raw) |
| 213 | + |
| 214 | +plot_reach(data, title = "Kuskokwim River") |
| 215 | +``` |
| 216 | + |
| 217 | +<!-- --> |
| 218 | + |
| 219 | +**Conclusion** |
| 220 | + |
| 221 | +You successfully retrieved SWOT river surface elevation data using |
| 222 | +Hydrocron, cleaned missing data, and plotted a time series. This |
| 223 | +workflow can be reused for any river reach available in the SWORD |
| 224 | +database. |
| 225 | + |
| 226 | +SWOT offers valuable high-resolution hydrologic data — especially for |
| 227 | +remote and ungauged regions like rural Alaska — unlocking new |
| 228 | +opportunities for hydrology education, research, and community impact. |
0 commit comments