如何使用 R 中的 API 访问国家气象局过去 7 天的降水数据?

问题描述 投票:0回答:1

我正在尝试开发一个 Shiny 应用程序,显示从您查看应用程序之日起未来 7 天和过去 7 天的预测和累积降水数据。我能够成功地使用 NWS API 来提取预测的降水量,但我在过去遇到了麻烦。当从本网站记录的 API 中提取数据时:https://www.ncei.noaa.gov/cdo-web/webservices/v2,我无法获取最近两天的数据。有人成功提取了过去两天的数据吗?

注意事项:

  • 我仅限于使用 NOAA 数据来完成此任务,但发现 Open-Meteo 等其他 API 更加用户友好。
  • 看起来过去 7 天的天气预报可以通过 NWS 获得,如本页所示:https://www.weather.gov/wrh/timeseries?site=MOIC1 但只是不能通过我能找到的他们的 API。他们的网站会将您转到 NCEI,那里存档了天气数据。
  • 我愿意接受建议,并尝试过对上面的页面进行网络抓取,但我对此非常新手,希望有一个更简单的解决方案。

这是一个示例代码片段:

`# 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 = "2024-03-07",   # Start date for the data retrieval
  enddate = "2024-03-13",     # 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)
}`
r shiny noaa nws
1个回答
0
投票

来自

data <- content(response, "parsed")
的回复是一个列表。每日值存储在列表的“结果”元素中。还有

要获取各个值,您需要访问 data$results 列表中的列表元素。

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

或者收集所有值作为矩阵:

#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" 

矩阵中的所有值都将是字符串,因此将其转换为数据框后,需要将感兴趣的列更改为所需的日期、数字类型。

© www.soinside.com 2019 - 2024. All rights reserved.