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


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

--

Legend:

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

    v15 v16  
     1
     2The 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.
     3
     4The following expression lists all the R functions available, read them and loads them into the R working session:
     5
     6{{{
     7> rfuncs <- list.files(pattern = "\\.R$")
     8> print(rfuncs)
     9[1] "dataInventory.R"    "loadData.R"         "loadObservations.R" "loadSystem4.R"      "makeNcmlDataset.R"  "makeVocabulary.R" 
     10> for (i in 1:length(rfuncs)) {
     11+      source(rfuncs[i])
     12+ }
     13}}}
     14
     15
     16= loadSystem4 =
     17
    118In 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:
    219
     
    120137
    121138[[Image(timeSeries.png)]]
     139
     140
     141= loadObservations =
     142
     143
     144{{{#!comment
     145setwd("/home/juaco/Desktop/r")
     146}}}
     147
     148# Loading Observations
     149
     150The function `loadObservations` is intended to deal with observational datasets from weather stations stored as csv files in a standard format.
     151In the directory "./datasets/observations/Iberia_ECA" there is an example dataset.
     152
     153{{{
     154> list.files("./datasets/observations/Iberia_ECA")
     155[1] "ecaIberia.nc" "Master.csv"   "pr.csv"       "tas.csv"      "tasmax.csv"   "tasmin.csv"
     156}}}
     157
     158As 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.
     159This is how the Master.csv file looks like:
     160{{{     
     161> master <- read.csv("./datasets/observations/Iberia_ECA/Master.csv")
     162> str(master)
     163'data.frame':      28 obs. of  5 variables:
     164      $ Id       : int  33 229 230 231 232 233 234 236 309 336 ...
     165$ Longitude: num  1.38 -6.83 -3.68 -4.49 -4.01 ...
     166$ Latitude : num  43.6 38.9 40.4 36.7 40.8 ...
     167$ Altitude : int  151 185 667 7 1894 790 251 44 43 704 ...
     168$ 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 ...
     169}}}
     170
     171The Master contains the following fields:
     172* `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.
     173* `Longitude`: longitude
     174* `Latitude`: latitude
     175* `Altitude`: altitude
     176* `Metadata`: other metadata associated to the dataset
     177
     178First of all we will plot the stations so that we can get an idea of their geographical situation and extent:
     179
     180{{{
     181> library(fields)     
     182> plot(master[ ,2:3], asp=1, pch=15, col="red")
     183> world(add=TRUE)
     184}}}
     185
     186In 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"`
     187
     188{{{
     189> stationIDs <- read.csv("./datasets/observations/Iberia_ECA/Master.csv", colClasses = "character")[ ,1]
     190> stationIDs
     191[1] "000033" "000229" "000230" "000231" "000232" "000233" "000234" "000236" "000309" "000336" "000414" "000416" "000420" "000421"
     192[15] "000788" "000800" "001392" "001398" "003904" "003905" "003907" "003908" "003922" "003928" "003936" "003947" "003948" "003949"
     193}}}