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)
Author:
juaco
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • udg/ecoms/RPackage/examples/drift

    v4 v5  
    3636
    3737{{{#!text/R
    38 > # The library sp needs to be installed to do this example:
    39 > require(sp)
    40 Loading required package: sp
    41 > # Matrix of anomalies between lead month and reference
    42 > aux.mat <- matrix(ncol = length(cfs.list), nrow = length(ref$xyCoords$x)*length(ref$xyCoords$y))
    43 > for (i in 1:length(cfs.list)) {
    44 +       mm.field <- apply(cfs.list[[i]]$Data, MARGIN = c(3,2), FUN = mean, na.rm = TRUE)
    45 +       aux.mat[ ,i] <- mm.field - ref.field
    46 + }
    47 > # 2D coordinates
    48 > xy <- expand.grid(ref$xyCoords$x, ref$xyCoords$y)
    49 > # This step ensures regularity of the CFS grid, which is not perfectly regular:
    50 > xy.coords <- coordinates(points2grid(points = SpatialPoints(xy), tolerance = .003))
    51 > # 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:
    52 > df <- cbind.data.frame(xy.coords, aux.mat)
    53 > names(df) <- c("x","y",paste("LeadMonth_",1:6, sep = ""))
    54 > str(df)
     38# The library sp needs to be installed to do this example:
     39require(sp)
     40# Matrix of anomalies between lead month and reference
     41aux.mat <- sapply(1:length(cfs.list), function(i) {apply(cfs.list[[i]]$Data, MARGIN = c(3,2), FUN = mean, na.rm = TRUE) - ref.field})
     42# 2D coordinates
     43xy <- expand.grid(ref$xyCoords$x, ref$xyCoords$y)
     44# This step ensures regularity of the CFS grid, which is not perfectly regular:
     45xy.coords <- coordinates(points2grid(points = SpatialPoints(xy), tolerance = .003))
     46# 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:
     47df <- cbind.data.frame(xy.coords, aux.mat)
     48names(df) <- c("x","y",paste("LeadMonth_",1:6, sep = ""))
     49str(df)
    5550'data.frame':   55 obs. of  8 variables:
    5651 $ x          : num  -10.31 -9.38 -8.44 -7.5 -6.56 ...
     
    6257 $ LeadMonth_5: num  -1.212 -0.986 -0.682 -0.587 -0.584 ...
    6358 $ LeadMonth_6: num  -1.314 -0.732 -0.32 -0.5 -0.98 ...
    64 > coordinates(df) <- c(1,2)
    65 > gridded(df) <- TRUE
    66 > class(df)
     59coordinates(df) <- c(1,2)
     60gridded(df) <- TRUE
     61class(df)
     62}}}
     63
     64Which returns the new spatial object class:
     65
     66{{{
    6767[1] "SpatialPixelsDataFrame"
    6868attr(,"package")
     
    7373
    7474{{{#!text/R
    75 > load(url("http://meteo.unican.es/work/downscaler/aux/wlines.rda"), verbose = TRUE)
    76 Loading objects:
    77   wlines
    78 > l1 <- list("sp.lines", wlines)
    79 > 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))
     75load(url("http://meteo.unican.es/work/downscaler/aux/wlines.rda"), verbose = TRUE)
     76l1 <- list("sp.lines", wlines)
     77spplot(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))
    8078}}}
    8179
     
    8886
    8987{{{#!text/R
    90 > barplot(colMeans(df@data), names.arg = abbreviate(names(df)), xlab = "lead month", ylab = "anomaly (ºC)")
    91 > title(main = "Mean forecast bias w.r.t. the lead-month 0 initialization")
    92 > mtext("Member 1")
     88barplot(colMeans(df@data), names.arg = abbreviate(names(df)), xlab = "lead month", ylab = "anomaly (ºC)")
     89title(main = "Mean forecast bias w.r.t. the lead-month 0 initialization")
     90mtext("Member 1")
    9391}}}
    9492