= Regional-Continental domain selections In this example we will load data for Europe for the variable surface (2m) minimum temperature (`var = tasmin`), for the first two members (`members = 1:2`) of the CFSv2 hindcast (dataset = `CFSv2_seasonal_16`), considering the wintertime (DJF, `season = c(12,1,2)`) for the 10-year period 2001-2010 (`years = 2001:2010`), according to the forecast of previous September (`leadMonth = 3`). The original variable is stored as 6-hourly data for this particular dataset. Instead of loading the whole 6-hourly time series, or filtering by a particular time as in the [http://meteo.unican.es/ecoms-udg/RPackage/Examples/pointSelection previous example] we will retrieve the daily mean values, by setting the argument `time = "DD"`, that internally computes the daily mean from the 6-hourly instantaneous values. {{{ > ex2 <- loadECOMS(dataset = "CFSv2_seasonal_16", var = "tasmin", members = 1:2, lonLim = c(-15,35), latLim = c(32, 75), season = c(12,1,2), years = 2001:2010, leadMonth = 3, time = "DD") [2014-06-17 12:47:49] Defining homogeneization parameters for variable "tasmin" NOTE: daily mean will be calculated from the 6-h instantaneous model output [2014-06-17 12:47:49] Defining geo-location parameters [2014-06-17 12:47:49] Defining initialization time parameters [2014-06-17 12:47:54] Retrieving data subset ... [2014-06-17 12:54:33] Done > print(object.size(ex2), units = "Mb") 35 Mb }}} In this case, the data are stored in a 4D-array, with the dimensions indicated by the `dimensions`attribute: {{{ > str(ex2$Data) num [1:902, 1:54, 1:47, 1:2] 17.4 16.4 17.4 18.7 18.4 ... - attr(*, "dimensions")= chr [1:4] "time" "lon" "lat" "member" }}} This is an example on how to plot the members selected as spatial means for the 10-year period. Note that this example uses the library `fields`, not attached on load of the `ecomUDG.Raccess` package: {{{ > library(fields) # Install if not available to reproduce the example > member1 <- apply(ex2$Data[,,,1], FUN = mean, MARGIN = c(2,3)) > member2 <- apply(ex2$Data[,,,2], FUN = mean, MARGIN = c(2,3)) > x <- ex2$xyCoords$x > y <- ex2$xyCoords$y > par(mfrow = c(1,2)) > image.plot(x,y,member1, asp = 1, main = "Member 1") > world(add = TRUE) > image.plot(x,y,member2, asp = 1, main = "Member 2") > world(add = TRUE) }}} [[Image(image-20140617-130616.png)]] {{{#!comment = Alternative visualization tools: Monsoon in the Indian subcontinent So far we have shown plotting examples using the trellis plots generated by the `spplot` method. In this examples we show alternative plotting options using more standard R plotting functions for gridded data. To this aim, we load the precipitation data of 1997 for the lead month 1 forecast over the Indian subcontinent, considering the monsoon season from June to September: {{{ monsoon <- loadSeasonalForecast("CFS", var="tp", members=16, lonLim=c(65,92), latLim=c(5,37), season=6:9, years=1997) }}} The georeferencing of the data is stored as a `SpatialGrid`, which has several convenient attributes for an effective description of a gridded field, including the possibility of defining a coordinate reference system, highly useful -sometimes indispensable- for many geospatial operations. Plotting objects inheriting from this class or related classes (e.g. `SpatialGridDataFrame`) is straightforward using the `spplot` methods, as described in [wiki:../Trellis the previous example], but it is not directly usable as input for other plotting methods in R. Next, we present some typical plotting functions and how to extract the spatial coordinates in a suitable format for plotting. The total precipitation field is calculated as the total accumulated precipitation during the selected period for each model grid cell: {{{ tp <- colSums(monsoon$MemberData$Member_16) }}} And this is the classical way of displaying the data using the `spplot` method: {{{ sgdf <- SpatialGridDataFrame(monsoon$LonLatCoords, as.data.frame(tp)) data(world_map) wl <- as(world_map, "SpatialLines") wlines <- list("sp.lines", wl) spplot(sgdf, scales = list(draw = TRUE), sp.layout = list(wlines), col.regions = rev(topo.colors(41))) }}} [[Image(spplot.png)]] It is also straightforward to represent the precipitation using contour lines: {{{ spplot(sgdf, scales = list(draw = TRUE), contour = TRUE, col = "red", col.regions = rev(topo.colors(41))) }}} [[Image(spplot_contour.png)]] The help files of functions `image` and `contour` for instance, indicate the type of data structure required for displaying three-dimensional or spatial data (''images'') by many R standard functions. Essentially, this is a list of elements specifying the x and y positions of the elements to be displayed as a grid, being the field z a matrix of values whose positions coincide with those of the x and y elements. As an illustration, the following function produces such a list from an input `SpatialGridDataFrame`, by indicating also the corresponding data column to be represented, as in the argument `zcol` of `spplot`. This function takes care of the appropriate ordering of the data for spatial consistency. Note that by default, if `zcol` is omitted, the function will represent the first column of the data slot. {{{ sgdf2xyz <- function(sgdf, zcol = 1) { coords <- coordinates(sgdf) z <- slot(sgdf, "data")[ ,zcol] aux <- cbind(coords, z) aux.ordered <- aux[order(aux[ ,2], aux[ ,1]), ] x <- unique(aux.ordered[ ,1]) y <- unique(aux.ordered[ ,2]) z <- t(matrix(aux.ordered[ ,3], nrow = length(y), ncol = length(x), byrow = TRUE)) xyz.list <- list("x" = x, "y" = y, "z" = z) return(xyz.list) } }}} We create the xyz object for data visualization using several basic R functions: {{{ xyz <- sgdf2xyz(sgdf) }}} == `image` function {{{ image(xyz, asp = 1, col = rev(topo.colors(21))) lines(wl) }}} [[Image(image.png)]] == `contour` function `contour` can be used alone or in combination with other plots by setting the argument `add = TRUE` {{{ par(mfrow = c(1,2)) contour(xyz, levels=seq(0,4000,200), labcex = 1.2) image(xyz, col = terrain.colors(21)) contour(xyz, col = "blue", add = TRUE) # Use dev.off() To restore the original par settings }}} [[Image(contour.png)]] == `filled.contour` function Filled contour produces a nice output with a graduated colorbar, but placing lines or other elements on the plot is not straightforward... [[Image(filled_contour.png)]]