我使用这段代码来获取R中的数据:
link <- "https://dataset.api.hub.geosphere.at/v1/grid/historical/inca-v1-1h-1km?parameters=RR¶meters=T2M¶meters=UU¶meters=VV¶meters=RH2M&start=2014-01-01&end=2014-02-01&bbox=47.25392576%2C11.34458542%2C47.278834%2C11.44352138&output_format=netcdf&filename=file_try"
data <- httr::GET(link) %>%
httr::content()
我的问题是获取 NetCDF 文件。在 API 文档中,它说:内容类型:application/x-netcdf。到目前为止,我收到了一个“raw”格式的对象。我猜解决方案在于 content() 函数,但我不知道这里要指定什么。我认为 NetCDF 文件不像 R 对象那样安全,而是从硬盘驱动器访问,这就是为什么这不起作用。
调用 download.file(link) 会是更好的选择吗?这里的问题是下载的文件不能被 ncdf4 识别(Error in R_nc4_open: NETCDF: Unknown file format)。对我来说,该文件似乎有一个“双”文件名,因为我在 API 请求(最后)以及 download.file(destfile=...) 中指定了文件名。 任何帮助表示赞赏!
您可以(如果您只想临时下载)执行以下操作:
the_tempfile <- tempfile() ## create a temporary file
download.file(link,
destfile = the_tempfile,
mode = "wb" ## save as BINARY
)
检查(使用,例如{ncdf4})
library(ncdf4)
nc_open(the_tempfile)