How to access National Weather Service precipitation data for the past 7 days using an API in R?

62 Views Asked by At

I'm trying to develop a Shiny app that shows forecasted and accumulated precipitation data for the upcoming and past 7 days from the day you view the application. I was able to successfully use the NWS API to pull the forecasted precipitation, but am having trouble with the past. When pulling from the API documented on this website: https://www.ncei.noaa.gov/cdo-web/webservices/v2, I am not able to get the most recent two-days' worth of data. Has anyone had success pulling the last two-days' worth of data?

Considerations:

  • I am confined to using NOAA data for this task, but have found other API's like Open-Meteo to be more user-friendly.
  • It looks like past 7 day forecast is available through the NWS as seen in this page: https://www.weather.gov/wrh/timeseries?site=MOIC1 but just not through their API that I can find. Their website sends you to NCEI where weather data is archived.
  • I am open to suggestions and have tried web-scraping the page above but am very novice at this and would like an easier solution.

Here's an example code snippet:

`# Load necessary libraries
library(httr)
library(jsonlite)

# Define your API key (get your own key from NOAA)
api_key <- "Your API Key" # An API key is needed for this to work

# Define latitude and longitude of the location you're interested in
latitude <- "40.7128"  # New York City latitude
longitude <- "-74.0060"  # New York City longitude

# Define the API endpoint
endpoint <- "https://www.ncdc.noaa.gov/cdo-web/api/v2/data"

# Define parameters for the API request
parameters <- list(
  datasetid = "GHCND",        # Dataset ID for daily summaries
  datatypeid = "PRCP",        # Data type ID for precipitation
  stationid = "GHCND:USW00094728",  # Station ID for Central Park, New York
  startdate = Sys.Date()-7,   # Start date for the data retrieval
  enddate = Sys.Date(),       # End date for the data retrieval
  units = "metric",           # Units for the data (metric or standard)
  limit = 1000,               # Limit number of records per request
  offset = 1                  # Offset for pagination
)

# Make the API request
response <- GET(
  url = endpoint,
  query = parameters,
  add_headers(.headers = c("token" = api_key))
)

# Check if request was successful
if (http_error(response)) {
  stop("HTTP error: ", http_status(response)$reason)
} else {
  # Parse JSON response
  data <- content(response, "parsed")
  
  # Extract relevant information (e.g., precipitation values)
  # Example: if the response contains daily precipitation values, you can access them like this
  daily_precipitation <- data$value
  
  # Print or further process the extracted data
  print(daily_precipitation)
}`

*Edit to update startdate and enddate parameters

1

There are 1 best solutions below

2
On

The response from data <- content(response, "parsed") is a list. The daily values are stored in the "results" element of the list. And

To get the individual values you will need to access the list element in the the data$results list.

#collection date
date <- sapply(data$results, function(x) {x$date} )
precip_value <- sapply(data$results, function(x) {x$value} )

Or collect all of the values in as a matrix:

#Or as a matrix
matrix(unlist(data$results), byrow=TRUE, ncol=5)


matrix(unlist(data$results), byrow=TRUE, ncol=5)
     [,1]                  [,2]   [,3]                [,4]        [,5]  
[1,] "2024-03-07T00:00:00" "PRCP" "GHCND:USW00094728" ",,W,2400"  "2.8" 
[2,] "2024-03-08T00:00:00" "PRCP" "GHCND:USW00094728" ",,W,2400"  "0"   
[3,] "2024-03-09T00:00:00" "PRCP" "GHCND:USW00094728" ",,W,2400"  "38.9"
[4,] "2024-03-10T00:00:00" "PRCP" "GHCND:USW00094728" ",,W,2400"  "0.8" 
[5,] "2024-03-11T00:00:00" "PRCP" "GHCND:USW00094728" "T,,W,2400" "0"   
[6,] "2024-03-12T00:00:00" "PRCP" "GHCND:USW00094728" ",,W,2400"  "0" 

All of the values in the matrix will be character strings so after converting this to a data frame, one will need to change the columns of interest to the desired date, numeric types.