# Changes between Version 4 and Version 5 of udg/ecoms/RPackage/examples/drift

Ignore:
Timestamp:
Sep 4, 2014 12:21:14 PM (7 years ago)
Comment:

--

### Legend:

Unmodified
 v4 {{{#!text/R > # The library sp needs to be installed to do this example: > require(sp) Loading required package: sp > # Matrix of anomalies between lead month and reference > aux.mat <- matrix(ncol = length(cfs.list), nrow = length(ref$xyCoords$x)*length(ref$xyCoords$y)) > for (i in 1:length(cfs.list)) { +       mm.field <- apply(cfs.list[[i]]$Data, MARGIN = c(3,2), FUN = mean, na.rm = TRUE) + aux.mat[ ,i] <- mm.field - ref.field + } > # 2D coordinates > xy <- expand.grid(ref$xyCoords$x, ref$xyCoords$y) > # This step ensures regularity of the CFS grid, which is not perfectly regular: > xy.coords <- coordinates(points2grid(points = SpatialPoints(xy), tolerance = .003)) > # Now we create a data.frame with the coordinates X-Y in the first two columns and the mean anomalies in the next 6 columns: > df <- cbind.data.frame(xy.coords, aux.mat) > names(df) <- c("x","y",paste("LeadMonth_",1:6, sep = "")) > str(df) # The library sp needs to be installed to do this example: require(sp) # Matrix of anomalies between lead month and reference aux.mat <- sapply(1:length(cfs.list), function(i) {apply(cfs.list[[i]]$Data, MARGIN = c(3,2), FUN = mean, na.rm = TRUE) - ref.field}) # 2D coordinates xy <- expand.grid(ref$xyCoords$x, ref$xyCoords$y) # This step ensures regularity of the CFS grid, which is not perfectly regular: xy.coords <- coordinates(points2grid(points = SpatialPoints(xy), tolerance = .003)) # Now we create a data.frame with the coordinates X-Y in the first two columns and the mean anomalies in the next 6 columns: df <- cbind.data.frame(xy.coords, aux.mat) names(df) <- c("x","y",paste("LeadMonth_",1:6, sep = "")) str(df) 'data.frame':   55 obs. of  8 variables: $x : num -10.31 -9.38 -8.44 -7.5 -6.56 ...$ LeadMonth_5: num  -1.212 -0.986 -0.682 -0.587 -0.584 ... \$ LeadMonth_6: num  -1.314 -0.732 -0.32 -0.5 -0.98 ... > coordinates(df) <- c(1,2) > gridded(df) <- TRUE > class(df) coordinates(df) <- c(1,2) gridded(df) <- TRUE class(df) }}} Which returns the new spatial object class: {{{ [1] "SpatialPixelsDataFrame" attr(,"package") {{{#!text/R > load(url("http://meteo.unican.es/work/downscaler/aux/wlines.rda"), verbose = TRUE) Loading objects: wlines > l1 <- list("sp.lines", wlines) > spplot(df, as.table = TRUE, col.regions = colorRampPalette(c("blue","white","red")), at = seq(-5.25,5.25,.25), scales = list(draw = TRUE), sp.layout = list(l1)) load(url("http://meteo.unican.es/work/downscaler/aux/wlines.rda"), verbose = TRUE) l1 <- list("sp.lines", wlines) spplot(df, as.table = TRUE, col.regions = colorRampPalette(c("blue","white","red")), at = seq(-5.25,5.25,.25), scales = list(draw = TRUE), sp.layout = list(l1)) }}} {{{#!text/R > barplot(colMeans(df@data), names.arg = abbreviate(names(df)), xlab = "lead month", ylab = "anomaly (ºC)") > title(main = "Mean forecast bias w.r.t. the lead-month 0 initialization") > mtext("Member 1") barplot(colMeans(df@data), names.arg = abbreviate(names(df)), xlab = "lead month", ylab = "anomaly (ºC)") title(main = "Mean forecast bias w.r.t. the lead-month 0 initialization") mtext("Member 1") }}}