Changes between Version 3 and Version 4 of udg/ecoms/RPackage/examples/drift
- Timestamp:
- Sep 3, 2014 6:16:16 PM (8 years ago)
Legend:
- Unmodified
- Added
- Removed
- Modified
-
udg/ecoms/RPackage/examples/drift
v3 v4 1 1 = Analysing model drift in South-western Iberia 2 2 3 We will take as reference for measuring the drift the lead-month 0 forecast (i.e. the July initializations) 3 In this practice, we will analyse the model drift by using the forecast of daily mean surface temperature for July 2001 considering 6 different forecast (lead) months, rfom January to June. The lead month 0 (i.e., the initialization of July itself) will be the reference from which we will compute the anomalies. In this example, we will consider the first member of the CFSv2 hindcast. 4 5 4 6 5 7 {{{#!text/R 6 > ref <- loadECOMS(dataset = "CFSv2_seasonal_16", var = "tas", lonLim = c(-10,-1), latLim = c(36,40), season = 7, years = 2006, leadMonth = 0) 8 > # The selection of leadMonth = 0 will give a NOTE on screen: 9 > ref <- loadECOMS(dataset = "CFSv2_seasonal_16", var = "tas", members = 1, lonLim = c(-10,-1), latLim = c(36,40), season = 7, years = 2006, leadMonth = 0, time = "DD") 10 [2014-09-03 17:50:20] Defining homogeneization parameters for variable "tas" 11 NOTE: daily mean will be calculated from the 6-h model output 12 NOTE: 'leadMonth = 0' selected 13 [2014-09-03 17:50:20] Defining geo-location parameters 14 [2014-09-03 17:50:20] Defining initialization time parameters 15 [2014-09-03 17:50:26] Retrieving data subset ... 16 [2014-09-03 17:50:31] Done 7 17 > plotMeanField(ref) 8 > ref.field <- apply(ref$Data, MARGIN = c(4,3), FUN = mean, na.rm = TRUE) 18 > title(main = "Lead month 0 forecast of July 2001") 19 > # This is the spatial mean of the reference field 20 > ref.field <- apply(ref$Data, MARGIN = c(3,2), FUN = mean, na.rm = TRUE) 9 21 }}} 10 22 11 12 [[Image(image-20140902-201518.png)]] 23 [[Image(image-20140903-175339.png)]] 13 24 14 25 … … 18 29 {{{#!text/R 19 30 > cfs.list <- lapply(1:6, function(lead.month) { 20 loadECOMS(dataset = "CFSv2_seasonal_16", var = "tas", lonLim = c(-10,-1), latLim = c(36,40), season = 7, years = 2006, leadMonth = lead.month)31 loadECOMS(dataset = "CFSv2_seasonal_16", var = "tas", members = 1, lonLim = c(-10,-1), latLim = c(36,40), season = 7, years = 2006, leadMonth = lead.month, time = "DD") 21 32 }) 22 33 }}} 23 34 24 In order to visualize the departures of each lead month from the reference in the same range of values, we will use the `spplot` method for plotting spatial objects of the library `sp`. To this aim, we will first compute the multi-member spatial mean for each lead month forecast, and then we will arrange the data in a matrix of 6 columns (one for each month), and x *y coordinates rows:35 In order to visualize the departures of each lead month from the reference in the same range of values, we will use the `spplot` method for plotting spatial objects of the library `sp`. To this aim, we will first compute the multi-member spatial mean for each lead month forecast, and then we will arrange the data in a matrix of 6 columns (one for each month), and x * y rows, as follows: 25 36 26 37 {{{#!text/R 38 > # The library sp needs to be installed to do this example: 27 39 > require(sp) 40 Loading required package: sp 28 41 > # Matrix of anomalies between lead month and reference 29 42 > aux.mat <- matrix(ncol = length(cfs.list), nrow = length(ref$xyCoords$x)*length(ref$xyCoords$y)) 30 +for (i in 1:length(cfs.list)) {31 + mm.field <- apply(cfs.list[[i]]$Data, MARGIN = c( 4,3), FUN = mean, na.rm = TRUE)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) 32 45 + aux.mat[ ,i] <- mm.field - ref.field 33 46 + } 34 47 > # 2D coordinates 35 48 > xy <- expand.grid(ref$xyCoords$x, ref$xyCoords$y) 49 > # This step ensures regularity of the CFS grid, which is not perfectly regular: 36 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: 37 52 > df <- cbind.data.frame(xy.coords, aux.mat) 38 53 > names(df) <- c("x","y",paste("LeadMonth_",1:6, sep = "")) … … 41 56 $ x : num -10.31 -9.38 -8.44 -7.5 -6.56 ... 42 57 $ y : num 40.2 40.2 40.2 40.2 40.2 ... 43 $ LeadMonth_1: num 0.0398 0.0691 0.1427 0.116 0.1935 ... 44 $ LeadMonth_2: num 0.386 0.451 0.472 0.311 0.161 ... 45 $ LeadMonth_3: num 0.0428 0.0787 0.0707 -0.125 -0.2751 ... 46 $ LeadMonth_4: num 0.261 0.258 0.131 -0.177 -0.464 ... 47 $ LeadMonth_5: num 0.0576 0.154 0.1327 -0.1282 -0.3963 ... 48 $ LeadMonth_6: num 0.0789 0.192 0.0989 -0.2667 -0.6457 ... 49 > Conversion of the data.frame to a Spatial gridded object: 58 $ LeadMonth_1: num 0.0596 0.1601 0.5955 0.9068 1.2601 ... 59 $ LeadMonth_2: num -0.1215 0.0509 0.3622 0.5444 0.6977 ... 60 $ LeadMonth_3: num -0.48 -0.359 -0.402 -0.693 -0.967 ... 61 $ LeadMonth_4: num 0.22303 0.27295 0.23869 0.04764 -0.00855 ... 62 $ LeadMonth_5: num -1.212 -0.986 -0.682 -0.587 -0.584 ... 63 $ LeadMonth_6: num -1.314 -0.732 -0.32 -0.5 -0.98 ... 50 64 > coordinates(df) <- c(1,2) 65 > gridded(df) <- TRUE 51 66 > class(df) 52 67 [1] "SpatialPixelsDataFrame" … … 55 70 }}} 56 71 57 In the next lines we use the `spplot` method of package `sp`, generating a lattice-type map. In first place, we will also load a `SpatialLines` dataset remotely stored at Santander Met Group server, in order to represent the grographical lines in the lattice map generated:72 In the next lines we use apply the `spplot` method of package `sp`, generating a lattice-type map. In first place, we will also load a `SpatialLines` dataset remotely stored at Santander Met Group server, in order to represent the coastline in the lattice map generated as a geographical reference: 58 73 59 74 {{{#!text/R … … 62 77 wlines 63 78 > l1 <- list("sp.lines", wlines) 64 > spplot(df, as.table = TRUE, col.regions = colorRampPalette(c("blue","white","red")), at = seq(- 2.5,2.5,.25), scales = list(draw = TRUE), sp.layout = list(l1))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)) 65 80 }}} 66 81 67 [[Image(image-20140903-1 71550.png)]]82 [[Image(image-20140903-180503.png)]] 68 83 69 84 70 The results show how a increasing lead month leads to a negative bias of the forecast, demonstrating that the mean state of a variable of a forecast is not stationary through time. 85 The results show how a increasing lead month leads to a negative bias of the forecast, demonstrating that the mean state of a variable of a forecast is not stationary along the runtime dimension. 86 87 Finally, we display the spatial mean of the anomalies w.r.t. the reference for each lead month considered using a barplot: 88 89 {{{#!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") 93 }}} 94 95 [[Image(image-20140903-181520.png)]] 96