NetCDF到Raster Brick“无法找到'ncdf4'的函数'brick'的继承方法”

时间:2017-03-23 17:07:15

标签: r raster netcdf

光栅包非常简单,也使用ncdf4加载ECMWF Era-Interim Netcdf文件。

只需这样做:

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

该文件只是全球的SST数据,为期1个月(2016年1月)。当我将它转换为数组(即提取维度/变量,并将时间转换为UTC,将其推入数组)时,我没有得到相同的错误,但是光栅包说它直接支持.nc文件(这么长时间)因为它们是cf-1兼容的,Era-Interim .nc是)

任何帮助非常感谢,已经尝试过许多Netcdf文件(非Era Interim)。

1 个答案:

答案 0 :(得分:0)

感谢Renaud Lancelot,他们给出了明确的源代码。我修改了他的代码以适应您的数据

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