问题描述
光栅包确实很简单,也使用ncdf4加载到ECMWF Era-Interim Netcdf文件中.
Really simple problem with the raster package, also using ncdf4 to load in an ECMWF Era-Interim Netcdf file.
只需这样做:
a <- nc_open("SSTs.nc")
B <- brick(a, varname="sst")
返回此:
Error in (function (classes, fdef, mtable) :
unable to find an inherited method for function ‘brick’ for signature ‘"ncdf4"’
该文件只是1个月(2016年1月)在全球范围内的SST数据.当我将其转换为数组时(即提取尺寸/变量,并将时间转换为UTC,将其推入数组中),我没有得到相同的错误,但是栅格数据包说它直接支持.nc文件(太长了因为它们是cf-1兼容的,所以是Era-Interim .nc的)
The file is just SST data over the whole globe, for 1 month (Jan2016). When I convert it into an array (i.e. extract dimensions/variable, and convert time to UTC, shove it into an array) I don't get the same error, but the raster package says it supports .nc files straight in (so long as they're cf-1 compatible, which Era-Interim .nc's are)
非常感谢任何帮助,已经尝试了许多Netcdf文件(也包括非Era Interim).
Any help much appreciated, have tried this with many Netcdf files (non-Era Interim too).
推荐答案
感谢 Renaud Lancelot ,谁会清楚地给出源代码.我已经修改了他的代码以适合您的数据
thank Renaud Lancelot, who give clearly source code. I have modified his code to fit with your data
# load package
library(sp)
library(raster)
library(ncdf4)
# read ncdf file
nc<-nc_open('D:/SSTs.nc')
# extract variable name, size and dimension
v <- nc$var[[1]]
size <- v$varsize
dims <- v$ndims
nt <- size[dims] # length of time dimension
lat <- nc$dim$latitude$vals # latitude position
lon <- nc$dim$longitude$vals # longitude position
# read sst variable
r<-list()
for (i in 1:nt) {
start <- rep(1,dims) # begin with start=(1,1,...,1)
start[dims] <- i # change to start=(1,1,...,i) to read timestep i
count <- size # begin with count=(nx,ny,...,nt), reads entire var
count[dims] <- 1 # change to count=(nx,ny,...,1) to read 1 tstep
dt<-ncvar_get(nc, varid = 'sst', start = start, count = count)
# convert to raster
r[i]<-raster(dt)
}
# create layer stack with time dimension
r<-stack(r)
# transpose the raster to have correct orientation
rt<-t(r)
extent(rt)<-extent(c(range(lon), range(lat)))
# plot the result
spplot(rt)
这篇关于NetCDF到Raster Brick“无法为'ncdf4'找到功能'brick'的继承方法"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!