非常简单的问题与光栅包,也使用ncdf4加载在ECMWF时代-过渡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个月(Jan2016)。当我将其转换为数组(即提取维度/变量,并将时间转换为UTC,将其插入数组)时,我不会得到相同的错误,但是光栅包表示它直接支持.nc文件(只要它们兼容cf-1,哪个时代-临时的..nc是)
任何帮助非常感谢,已经尝试了与许多Netcdf文件(非时代过渡)。
发布于 2017-04-06 02:26:08
感谢勒诺·兰斯洛特,他们给出了清晰的源代码。我已经修改了他的代码以符合你的数据
# 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)https://stackoverflow.com/questions/42982599
复制相似问题