[R] MultiDimensional arrays
Steve_Friedman at nps.gov
Steve_Friedman at nps.gov
Mon Feb 28 21:27:37 CET 2011
hello,
I'm working with a 4 dimensional NetCDF file, trying to calculate some
summary statistics on a parameter.
The dimensions are number of rows and columns, time and days. The
attribute in question in this case happens
to be a probability of woodstork foraging success. But it could be anything
My question is whether the following code is efficient to grab the
probability values ? I am not skilled with multidimensional arrays, the
code below was written for by a highly skilled Java coder, who does not
know R.
Any suggestions would be greatly appreciated.
Thanks
Steve
Working with a Windows XP machine and R 2.12.1
----------------------------------------------------------------------------------------------------------------------------------------------------------------
library(tis) # includes function for leap year
library(ncdf)
library(fields) links to an animation routine not shown
woodstorks <- open.ncdf("WoodStork_Eden_2000_2009.nc")
v2 <- woodstorks$var[[2]] # longname
"probability_of_successful_foraging_cycle_daily"
DaysToMonth365 <- c(0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334,
365)
DaysToMonth366 <- c(0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335,
366)
Lyear <- isLeapYear(2000:2009) # returns boolean vector TRUE FALSE for
years in sequence
start_year<- 1999 # we process 11 years with 265 daily data records in
a 287 x 405 row x column map.
for(t in 1:11) {
currentYear = start_year + t
leapYear <- isLeapYear(currentYear:currentYear) # returns boolean
vector TRUE FALSE for years in sequence
#print(leapYear)
Days<-DaysToMonth365
if(leapYear){
Days<-DaysToMonth366
}
#print(Days)
data <- array(data, dim = c(Days[1], 405, 265))
for(d in 1: 226) {
for(y in 1: 405) {
for(x in 1:287) {
day = 1
counter <- 2 # first element in array
#print(paste("day = ", d))
if(d >= Days[counter]) {
counter <- counter + 1
#print(paste("here = ", t))
processMonth(data) # function defined outside
of this command sequence
data <- array(data, dim = c(Days[1], 405, 265))
day = 1
}
else
{
index<-c(x,y,d,t)
count<-c(1,1,1,1)
temp<- get.var.ncdf(woodstorks, v2, start=index, count
= count)
i <- (d*405*287) + (y*287) + x #map 3d to 1dimension
data[i] = temp[1]
#print(data[i])
day <- day + 1
}
}
}
}
}
Steve Friedman Ph. D.
Ecologist / Spatial Statistical Analyst
Everglades and Dry Tortugas National Park
950 N Krome Ave (3rd Floor)
Homestead, Florida 33034
Steve_Friedman at nps.gov
Office (305) 224 - 4282
Fax (305) 224 - 4147
More information about the R-help
mailing list