NetCDF to Raster Brick “Unable to find inherited method for function 'brick' for 'ncdf4'”

两盒软妹~` 提交于 2019-12-04 17:12:48

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)
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!