# Changes between Version 15 and Version 16 of udg/ecoms/RPackage/examples

Ignore:
Timestamp:
May 19, 2013 11:49:30 AM (9 years ago)
Comment:

--

### Legend:

Unmodified
 v15 The following examples are partly based on locally stored data. Only the loadSystem4 function does not require locally stored datasets because it works by remotely accessing the System4 databases stored in the SPECS-EUPORIAS Data Server. Therefore, it is recommended that the '''meteoR.zip''' file is downloaded and unzipped in a convenient directory. Once the zip file has been downloaded and unzipped, we will define the 'r' folder as the working directory. Therefore, in the following examples all the path names given will be relative to the 'r' directory. The following expression lists all the R functions available, read them and loads them into the R working session: {{{ > rfuncs <- list.files(pattern = "\\.R$") > print(rfuncs) [1] "dataInventory.R" "loadData.R" "loadObservations.R" "loadSystem4.R" "makeNcmlDataset.R" "makeVocabulary.R" > for (i in 1:length(rfuncs)) { + source(rfuncs[i]) + } }}} = loadSystem4 = In the next lines we describe an illustrative example of the loadSystem4 function. We will retrieve System4 simulation data for the Iberian Peninsula, considering mean surface temperature for January and the first simulation member, for the 10-year period 1990-1999. This simple example has been chosen because of the fast data access (note that this also depends on the connection speed). Using a standard broadband connection, loading the following dataset took approximately 19 seconds: [[Image(timeSeries.png)]] = loadObservations = {{{#!comment setwd("/home/juaco/Desktop/r") }}} # Loading Observations The function loadObservations is intended to deal with observational datasets from weather stations stored as csv files in a standard format. In the directory "./datasets/observations/Iberia_ECA" there is an example dataset. {{{ > list.files("./datasets/observations/Iberia_ECA") [1] "ecaIberia.nc" "Master.csv" "pr.csv" "tas.csv" "tasmax.csv" "tasmin.csv" }}} As we can see, there is a number of files with the name of the variables they store, and a Master.csv file, which contains the required metadata in order to identify each station. This is how the Master.csv file looks like: {{{ > master <- read.csv("./datasets/observations/Iberia_ECA/Master.csv") > str(master) 'data.frame': 28 obs. of 5 variables:$ Id       : int  33 229 230 231 232 233 234 236 309 336 ... $Longitude: num 1.38 -6.83 -3.68 -4.49 -4.01 ...$ Latitude : num  43.6 38.9 40.4 36.7 40.8 ... $Altitude : int 151 185 667 7 1894 790 251 44 43 704 ...$ Metadata : Factor w/ 1 level " Data provided by the ECA&D project. Available at http://www.ecad.eu": 1 1 1 1 1 1 1 1 1 1 ... }}} The Master contains the following fields: * Id: Identification code of the station. This code is used as argument by the function loadObservations. Note thas this field should actually be read as a character string, as internally done by the loadObservations function. However, in this case read.csv by default interpretes it as a numeric value. * Longitude: longitude * Latitude: latitude * Altitude: altitude * Metadata: other metadata associated to the dataset First of all we will plot the stations so that we can get an idea of their geographical situation and extent: {{{ > library(fields) > plot(master[ ,2:3], asp=1, pch=15, col="red") > world(add=TRUE) }}} In order to get a vector with the correct IDs as character strings instead of numeric values, we can load again the corresponding column using the argument colClasses = "character" {{{ > stationIDs <- read.csv("./datasets/observations/Iberia_ECA/Master.csv", colClasses = "character")[ ,1] > stationIDs [1] "000033" "000229" "000230" "000231" "000232" "000233" "000234" "000236" "000309" "000336" "000414" "000416" "000420" "000421" [15] "000788" "000800" "001392" "001398" "003904" "003905" "003907" "003908" "003922" "003928" "003936" "003947" "003948" "003949" }}}