Hi there I'm working with ncdf data. I have different dataset for 4 runs, 4 seasons and 3 timeslices (48 datasets in total). The datasets have the following dimensions: 96 longitudes, 48 latitudes and 30 time steps. To read all of them in, I wrote the following loop: runs <- c("03","04","05","06") years <- c(1851,1961,2061) seasons <- c("DJF","MAM","JJA","SON") for (i in runs) { for (j in years) { for (k in seasons) { data <- open.ncdf(paste("/data09/cstocker/data/atm/tr_1500_", i,"/PRECT/PREC_tr1500_", i,"_", j,"_", k,".nc", sep="")) prect <- get.var.ncdf(data, "PRECT") assign(paste("prec", k, j, i, sep="_"), prect) } } } I produced 48 variables called prec_DJF_1851_03 and so on.. Now, how can I select an element out of each of these variables? If I have only one variable, I use: example[1:5, 6:10, 10:15] ...just as you would choose out of a simple array. That I don't have to do it for every single variable by hand, I wanted to create something like: for (i in runs) { for (j in years) { for (k in seasons) { assign(paste("newname", k, j, i, sep="_"), choose.element(paste("prec", k, j, i, sep="_"), at.position(x,y,z))) } } } How can I solve this problem? Took me already quite a lot of time to surf through all former posts. Unfortunately I could't find what I was looking for. I'd be very thankful for any kind of help as I am slowly very confused and frustrated... cheers.. -- View this message in context: http://r.789695.n4.nabble.com/Select-element-out-of-several-ncdf-variables-tp3651945p3651945.html Sent from the R help mailing list archive at Nabble.com.
David William Pierce
2011-Jul-07 18:07 UTC
[R] Select element out of several ncdf variables
On Thu, Jul 7, 2011 at 9:10 AM, confused <cstocker@climate.unibe.ch> wrote:> Hi there > > I'm working with ncdf data. I have different dataset for 4 runs, 4 seasons > and 3 timeslices (48 datasets in total). The datasets have the following > dimensions: 96 longitudes, 48 latitudes and 30 time steps. To read all of > them in, I wrote the following loop: > > runs <- c("03","04","05","06") > years <- c(1851,1961,2061) > seasons <- c("DJF","MAM","JJA","SON") > > for (i in runs) { > for (j in years) { > for (k in seasons) { > > data <- open.ncdf(paste("/data09/cstocker/data/atm/tr_1500_", > i,"/PRECT/PREC_tr1500_", i,"_", j,"_", k,".nc", sep="")) > > prect <- get.var.ncdf(data, "PRECT") > > assign(paste("prec", k, j, i, sep="_"), prect) > > } > } > } > > I produced 48 variables called prec_DJF_1851_03 and so on.. > > Now, how can I select an element out of each of these variables? >Are all your data arrays (i.e., the data in each of your 48 files) the same shape? If so, easiest approach (assuming I'm understanding your problem, which is not a given!) would be simply to put all the data into one array in the first place. I.e, something like: nruns <- length(runs) nyears <- length(years) nseaons <- length(seasons) bigdata <- NA # flag to show it's not initialized yet for (i in runs) { for (j in years) { for (k in seasons) { data <- open.ncdf(paste("/data09/cstocker/data/atm/tr_1500_",i,"/PRECT/PREC_tr1500_", i,"_", j,"_", k,".nc", sep="")) prect <- get.var.ncdf(data, "PRECT") if( length(bigdata) == 1 ) { # is bigdata un-initialized? nx = dim(prect)[1] ny = dim(prect)[2] nz = dim(prect)[3] bigdata = array(0., dim=c(nx,ny,nz,nseasons,nyears,nruns)) } bigdata[,,,k,j,i] = prect close.ncdf( data ) # remember to close the file } } } Then you can conveniently select from bigdata however you want. If the shape of the data in each file is different, you have to use an analogous approach with lists instead. Regards, --Dave -- David W. Pierce Division of Climate, Atmospheric Science, and Physical Oceanography Scripps Institution of Oceanography (858) 534-8276 (voice) / (858) 534-8561 (fax) dpierce@ucsd.edu [[alternative HTML version deleted]]