如何从netcdf文件中对数据进行子集化?

时间:2015-07-13 08:31:28

标签: r

所以我有这个netcdf文件:

1] "file test.nc has 2 dimensions:"
[1] "Time   Size: 216"
[1] "ID   Size: 36"
[1] "------------------------"
[1] "file test.nc has 1 variables:"
[1] "float Q[ID,Time]  Longname:Q Missval:1e+30"

我想做的就是给它一个ID(比如100300)。并阅读特定的时间段(比如说4到20次)

library(ncdf)
ncfile<-nc<-open.ncdf("test.nc")
varcomid <- get.var.ncdf(ncfile,varid = "ID")
vartime <- get.var.ncdf(ncfile,varid = "Time")
ndims    = ncfile$var[['Q']]$ndims 
varsize    = ncfile$var[['Q']]$varsize
comid<-which(ncfile$dim$ID$vals == 100300)

start=c(comid,4)
count = c(varsize[1], varsize[2])

dat<-get.var.ncdf(nc=ncfile,varid="Q",start,count)

我无法完成结束时间步的逻辑。

另外,我收到此错误:C函数R_nc_get_vara_double返回错误

使用评论进行诊断

start=c(comid,4)
count = c(varsize[2], varsize[1])

dat<-get.var.ncdf(nc=ncfile,varid="Q",start,count)

Error in R_nc_get_vara_double: NetCDF: Start+count exceeds dimension bound
Var: Q  Ndims: 2   Start: 3,27Count: 36,216Error in get.var.ncdf(nc = ncfile, varid = "Q", start, count) : 
  C function R_nc_get_vara_double returned error

1 个答案:

答案 0 :(得分:1)

这有效:

library(ncdf)
ncfile <- nc<-open.ncdf("test.nc")
varcomid <- get.var.ncdf(ncfile,varid = "COMID")
vartime <- get.var.ncdf(ncfile,varid = "Time")
ndims <- ncfile$var[['Qout']]$ndims 
varsize <- ncfile$var[['Qout']]$varsize
comid <- which(varcomid == 1439445)

start <- c(comid, 4)
count <- c(1, varsize[2] - start[2])

dat <- get.var.ncdf(nc = ncfile,varid = "Qout", start = start, count = count)