謝謝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)
你能給樣本數據集,也可以嘗試使用'stack'或使用以前的'ncdf package' –
同樣的問題[讀的NetCDF子類別,並轉換爲網格] http://stackoverflow.com/questions/33784940/read-netcdf-sub-categories-and-convert-to-grid –
感謝您的答覆。使用堆棧返回此: data.frame中的錯誤(值= unlist(unname(x)),ind = factor(rep.int(names(x),: 參數意味着行數不同:1654,16 示例數據集是這樣的:https://drive.google.com/open?id=0Bz0W7Ut_SNfjcjg1ODVrc2FhN2s – Ndharwood