extractmap <- function(x, y, data, imonth, iyear, tmonth, tyear ,xmin=min(x), xmax=max(x), ymin=min(y), ymax=max(y)) { # Extract a matrix (map) on a specific date from a three-dimensional array # containing monthly data with first dimension p (longitude), second dimension # q (latitude) and third dimension n (time) # # Description: # # Returns the data of the specific date # # Usage: # # extractmap(x, y, data, imonth, iyear, tmonth, tyear , # 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' # # tmonth: Number of the target month to be extracted from the # time series of 'data' # # tyear: Number of the target year to be extracted from the # time series of 'data' # # xmin: Lower bound longitude from where to start extracting data # # xmax: Upper bound longitude up to where to extract data # # ymin: Lower bound latitude from where to start extracting data # # ymax: Upper bound latitude up to where to extract data # # Output: # # A list with three components is returned invisibly: # # lon: Vector of longitudes within the selected range # # lat: Vector of latitudes within the selected range # # data: Matrix or three-dimentional array containing the selected data # # Authors: # # Caio Coelho 3 November 2005 # Chris Ferro # # Examples: # # # Extract data from a three-dimentional 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 <- extractmap(x, y,data,1,1948,9,1951) # output <- extractmap(x, y,data,1,1948,9,1951,xmin=-12,xmax=12,ymin=32,ymax=58) # if (imonth == 1 & tmonth == 1) {t<-((tyear-iyear)*12)+1} if (imonth == 1 & tmonth != 1) {t<-((tyear-iyear)*12)+tmonth} if (imonth != 1) {t<-12-imonth+1+((tyear-iyear)*12)+tmonth-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), t, drop = TRUE] cat(paste("Extracted data for month",as.character(tmonth),"of",as.character(tyear),"\n",sep=" ")) invisible(list(lon = xnew, lat = ynew, data = datanew)) }