rnoaa 显示数据可用,但没有返回请求
rnoaa showing data is available, but not returning request
我正在使用 rnoaa()
包来获取一些历史天气数据,并且 运行 遇到了检索数据的问题,这些数据说可用,但不会 return。
为了使这个可重现的示例起作用,您首先需要一个来自 http://www.ncdc.noaa.gov/cdo-web/token
的令牌
设置:
options(noaakey = "KEY_EMAILED_TO_YOU")
library(rnoaa)
检查可用的数据类型:
ncdc_datatypes(stationid = "GHCND:US009052008", datasetid='GHCND')
输出:
$meta
offset count limit
1 1 4 25
$data
Source: local data frame [4 x 5]
mindate maxdate name datacoverage id
(chr) (chr) (chr) (int) (chr)
1 1781-01-01 2015-10-30 Precipitation (tenths of mm) 1 PRCP
2 1857-01-18 2015-10-29 Snow depth (mm) 1 SNWD
3 1763-01-01 2015-10-30 Maximum temperature (tenths of degrees C) 1 TMAX
4 1763-01-01 2015-10-30 Minimum temperature (tenths of degrees C) 1 TMIN
attr(,"class")
[1] "ncdc_datatypes"
ncdc(stationid = "GHCND:US009052008", datasetid='GHCND', datatypeid = 'PRCP', startdate = "1900-01-01", enddate = "1900-12-30")
请注意 PRCP
可用的最小数据是 1781。所以让我尝试从 1900 年提取数据,因为它应该可用。
尝试从 1900 年提取数据:
ncdc(stationid = "GHCND:US009052008", datasetid='GHCND', datatypeid = 'PRCP', startdate = "1900-01-01", enddate = "1900-12-30")
输出:
$meta
$meta$totalCount
NULL
$meta$pageCount
NULL
$meta$offset
NULL
$data
Source: local data frame [0 x 0]
attr(,"class")
[1] "ncdc_data"
Warning message:
In check_response(temp) : Sorry, no data found
一种方式:
sta <- "US009052008"
input <- paste0("ftp://ftp.ncdc.noaa.gov/pub/data/ghcn/daily/all/",sta,".dly")
output <- read.fwf(input, n = -1,
widths = c(11,4,2,4),
col.names = c("ID", "YEAR", "MONTH", "ELEMENT"))
out <- split(output, output$ELEMENT)
foo <- function(x){
y1 <- head(x[,c("YEAR", "MONTH")], 1)
y2 <- tail(x[,c("YEAR", "MONTH")], 1)
paste(month.abb[y1$MONTH], y1$YEAR, "-", month.abb[y2$MONTH], y2$YEAR)
}
do.call(rbind, lapply(out, foo))
# [,1]
# PRCP "Oct 2008 - Oct 2015"
# SNWD "Dec 2009 - Oct 2015"
# TMAX "Oct 2008 - Oct 2015"
# TMIN "Oct 2008 - Oct 2015"
我正在使用 rnoaa()
包来获取一些历史天气数据,并且 运行 遇到了检索数据的问题,这些数据说可用,但不会 return。
为了使这个可重现的示例起作用,您首先需要一个来自 http://www.ncdc.noaa.gov/cdo-web/token
的令牌设置:
options(noaakey = "KEY_EMAILED_TO_YOU")
library(rnoaa)
检查可用的数据类型:
ncdc_datatypes(stationid = "GHCND:US009052008", datasetid='GHCND')
输出:
$meta
offset count limit
1 1 4 25
$data
Source: local data frame [4 x 5]
mindate maxdate name datacoverage id
(chr) (chr) (chr) (int) (chr)
1 1781-01-01 2015-10-30 Precipitation (tenths of mm) 1 PRCP
2 1857-01-18 2015-10-29 Snow depth (mm) 1 SNWD
3 1763-01-01 2015-10-30 Maximum temperature (tenths of degrees C) 1 TMAX
4 1763-01-01 2015-10-30 Minimum temperature (tenths of degrees C) 1 TMIN
attr(,"class")
[1] "ncdc_datatypes"
ncdc(stationid = "GHCND:US009052008", datasetid='GHCND', datatypeid = 'PRCP', startdate = "1900-01-01", enddate = "1900-12-30")
请注意 PRCP
可用的最小数据是 1781。所以让我尝试从 1900 年提取数据,因为它应该可用。
尝试从 1900 年提取数据:
ncdc(stationid = "GHCND:US009052008", datasetid='GHCND', datatypeid = 'PRCP', startdate = "1900-01-01", enddate = "1900-12-30")
输出:
$meta
$meta$totalCount
NULL
$meta$pageCount
NULL
$meta$offset
NULL
$data
Source: local data frame [0 x 0]
attr(,"class")
[1] "ncdc_data"
Warning message:
In check_response(temp) : Sorry, no data found
一种方式:
sta <- "US009052008"
input <- paste0("ftp://ftp.ncdc.noaa.gov/pub/data/ghcn/daily/all/",sta,".dly")
output <- read.fwf(input, n = -1,
widths = c(11,4,2,4),
col.names = c("ID", "YEAR", "MONTH", "ELEMENT"))
out <- split(output, output$ELEMENT)
foo <- function(x){
y1 <- head(x[,c("YEAR", "MONTH")], 1)
y2 <- tail(x[,c("YEAR", "MONTH")], 1)
paste(month.abb[y1$MONTH], y1$YEAR, "-", month.abb[y2$MONTH], y2$YEAR)
}
do.call(rbind, lapply(out, foo))
# [,1]
# PRCP "Oct 2008 - Oct 2015"
# SNWD "Dec 2009 - Oct 2015"
# TMAX "Oct 2008 - Oct 2015"
# TMIN "Oct 2008 - Oct 2015"