extractwindow <- function(x, y, data, imonth, iyear, month1, year1, month2, year2 ,xmin=min(x), xmax=max(x), ymin=min(y), ymax=max(y)) { # Extract field of monthly data for a given time period from a three-dimensional # array with first dimension p (longitude), second dimension q (latitude) and third # dimension n (time) # # Description: # # Returns field of data for a given time period # # Usage: # # extractwindow(x, y, data, imonth, iyear, month1, year1, # month2, year2 ,xmin=min(x), xmax=max(x), # ymin=min(y), ymax=max(y)) # # Arguments: # # x: Vector of longitudes # # y: Vector of latitudes # # data: Matrix or three-dimensional array of data. Space dimensions of # `data' must be `length(x)' by `length(y)' respectively. # Three-dimensional array must have the following dimension order: # first dimension for longitude, second dimension for latitude, # and third dimention for time # # imonth: Number of the first month of the time series of 'data' # # iyear: Number of the first year of the time series of 'data' # # month1: Number of the month from where to start extracting data # # year1: Number of the year from where to start extracting data # # month2: Number of the month up to where to extract data # # year2: Number of the year up to where to extract data # # xmin: Lower bound longitude from where to start extracting data # Default is the minimum value of x # # xmax: Upper bound longitude up to where to extract data # Default is the maximum value of x # # ymin: Lower bound latitude from where to start extracting data # Default is the minimum value of y # # ymax: Upper bound latitude up to where to extract data # Default is the maximum value of y # # # Output: # # A list with three components is returned invisibly: # # x: Vector of longitudes for the extracted data # # y: Vector of latitudes for the extracted data # # data: Extracted three-dimensional array (field) # # # Authors: # # Caio Coelho 3 November 2005 # Chris Ferro # # Examples: # # # Extract data from a three-dimensional array (field) # x <- seq(-20, 20, 5) # y <- seq(30, 60, 5) # dim <- c(length(x), length(y), 100) # data <- array(rnorm(prod(dim)), dim) # output <- extractwindow(x, y,data,1,1948,3,1948,5,1949) # output <- extractwindow(x, y,data,1,1948,3,1948,5,1949,xmin=-12,xmax=12,ymin=32,ymax=58) # output <- extractwindow(x, y,data,9,1948,11,1948,5,1949,xmin=-12,xmax=12,ymin=32,ymax=58) # if (imonth == 1 & month1 == 1) { t1<-((year1-iyear)*12)+1 if (month2 ==1){ t2<-((year2-iyear)*12)+1 } else{ t2<-((year2-iyear)*12)+month2 } } if (imonth == 1 & month1 != 1) { t1<-((year1-iyear)*12)+month1 if (month2 ==1){ t2<-((year2-iyear)*12)+1 } else{ t2<-((year2-iyear)*12)+month2 } } if (imonth != 1) { t1<-12-imonth+1+((year1-iyear)*12)+month1-12 t2<-12-imonth+1+((year2-iyear)*12)+month2-12 } xnew <- x ynew <- y tnew <- t xnew <- x[x >= xmin & x <= xmax] ynew <- y[y >= ymin & y <= ymax] datanew <- data[match(xnew, x), match(ynew, y), t1:t2, drop = FALSE] cat(paste("Extracted data from month",as.character(month1),"of",as.character(year1),"to month",as.character(month2),"of",as.character(year2),"\n",sep=" ")) invisible(list(x = xnew, y = ynew, data = datanew)) }