maximusdooku
maximusdooku

Reputation: 5512

How can I subset data from a netcdf file?

So I have this netcdf file:

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"

All I want to do is give it one ID (say 100300). And read a particular time period (Say 4 to 20th timestep)

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)

I am not able to finish the logic of the end timestep.

Also, I am getting this error: C function R_nc_get_vara_double returned error

Diagnosis using comments

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

Upvotes: 0

Views: 398

Answers (1)

user3710546
user3710546

Reputation:

This works:

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)

Upvotes: 1

Related Questions