Say I have 10 model configurations of n timesteps for 3 different sites, producing a total 30 netcdf files I want to open and manipulate. I can open the 30 files such as
require(ncdf4)
allfiles= list()
nmod=10
nsites=3
for (i in 1:nmod) {
allfiles[[i]] = list(nc_open(paste0('Model',i,'siteA.nc')),
nc_open(paste0('Model',i,'siteB.nc')),
nc_open(paste0('Model',i,'siteC.nc')))
}
When querying the class of what was opened, I have
class(allfiles)
[1] "list"
class(allfiles[[1]][[1]])
[1] "ncdf4"
as expected. Now what I would like to do is extract the values from a variable in the files such that
var=list()
for (i in 1:nmod) {
for (j in 1:nsites) {
var[[i]][[j]] <- ncvar_get(allfiles[[i]][[j]],"var1")
nc_close(allfiles[[i]][[j]])
}}
but I get this error message:
`Error in *tmp*[[i]] : subscript out of bounds`
If I try
var[[i]] <- ncvar_get(allfiles[[i]][[j]],"var1")
it (understandbly) only produces a list of 10 model configurations at one site, ie var[[1]][[1]][1]
prints out the value of the variable at model configuration 1, site A, timestep 1 but var[[1]][[2]]
doesn't exist. How can I declare var
in the above loop so that it contains all the values for all models, all sites and all timesteps (eg for var[[1]][[2]][1]
to exist)?
In your original version where the error occurs, in the first inner loop, you try to do something: var[[1]][[1]] <- something
, but var[[1]]
doesn't exist, and R
doesn't know what to do, so I guess the following thing should work, you set var[i] <- list()
before you do var[[i]][[j]] <- something
:
var=list()
for (i in 1:nmod) {
var[i] <- list()
for (j in 1:nsites) {
var[[i]][[j]] <- ncvar_get(allfiles[[i]][[j]],"var1")
nc_close(allfiles[[i]][[j]])
}
}
For example, if you do:
var <- list()
for (i in 1:10) {
for (j in 1:10) {
var[[i]][[j]] <- 1
}
}
Then the same error happens. But if you set var[[i]] <- list()
before carrying out the inner loop like this:
var <- list()
for (i in 1:10) {
var[[i]] <- list()
for (j in 1:10) {
var[[i]][[j]] <- 1
}
}
Then the problem will be solved.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.