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
The response from
data <- content(response, "parsed")
is a list. The daily values are stored in the "results" element of the list. AndTo get the individual values you will need to access the list element in the the data$results list.
Or collect all of the values in as a matrix:
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.