Skip to content

Instantly share code, notes, and snippets.

@Pakillo
Created April 22, 2016 10:34
Show Gist options
  • Save Pakillo/df07cd13a577f2a7d03657eaaf81512f to your computer and use it in GitHub Desktop.
Save Pakillo/df07cd13a577f2a7d03657eaaf81512f to your computer and use it in GitHub Desktop.
Fetch climate data in R with package RFc

New R package to fetch climate data from FetchClimate webservice

Francisco Rodriguez-Sanchez
22 April 2016

Trying new RFc package to get climate data into R from FetchClimate service.

See all the available climate variables here.

library(knitr)
opts_knit$set(upload.fun = imgur_upload)

library("RFc")
library("raster")
library("rworldmap")
data("coastsCoarse")

Fetching gridded data

Fetching temperature for Europe for Januaries 2000-2010 with 0.5 degrees resolution

pet <- fcGrid(variable = "airt",
              latitudeFrom = 35, latitudeTo = 60,latitudeBy = 0.5,
              longitudeFrom = -10, longitudeTo = 30, longitudeBy = 0.5,
              firstDay = 1,lastDay = 31,
              firstYear = 2000, lastYear = 2010)
## No encoding supplied: defaulting to UTF-8.
## [1] "completed=msds:ab?AccountName=fetchclimate2r1&Container=requests&Blob=daddb9143d322ff27c8f6e8f2bfadd962882b02f"
## [1] "Receiving data..."
# Convert to raster
pet <- raster(pet)

# Plot
plot(pet)
plot(coastsCoarse, add = TRUE)

Fetching time series data for a set of locations

Fetching annual temperature data for Sevilla (Spain) from 1950 to 2000

year.ts <- fcTimeSeriesYearly(variable="airt", 
                              latitude = 37.4, longitude = -6,
                              firstYear = 1950, lastYear = 2000)
## No encoding supplied: defaulting to UTF-8.
## [1] "completed=msds:ab?AccountName=fetchclimate2r1&Container=requests&Blob=a0a43a3b37ca6a220b1939c4c7444e42b66f3f2d"
## [1] "Receiving data..."
plot(year.ts$years, year.ts$values, type = "l", 
     xlab = "Year", ylab = "Temperature", las = 1,
     main = "Mean Annual Temperature in Sevilla (1950 - 2000)")

Fetching temperature data for Sevilla at January 1st from 1950 to 2000

year.ts <- fcTimeSeriesYearly(variable="airt", 
                              latitude = 37.4, longitude = -6,
                              firstYear = 1950, lastYear = 2000,
                              firstDay = 1, lastDay = 1)
## No encoding supplied: defaulting to UTF-8.
## [1] "completed=msds:ab?AccountName=fetchclimate2r1&Container=requests&Blob=d0634512d92b1b3d2e5e4d3dd0acb856cd69b960"
## [1] "Receiving data..."
plot(year.ts$years, year.ts$values, type = "l", 
     xlab = "Year", ylab = "Temperature", las = 1,
     main = "Temperature in Sevilla January 1st (1950 - 2000)")

Fetching hourly data for Sevilla at 1 January 1978

hour.ts <- fcTimeSeriesHourly(variable="airt", 
                              latitude = 37.4, longitude = -6,
                              firstYear = 1978, lastYear = 1978,
                              firstDay = 1, lastDay = 1,
                              startHour = 0, stopHour = 24)
## No encoding supplied: defaulting to UTF-8.
## [1] "completed=msds:ab?AccountName=fetchclimate2r1&Container=requests&Blob=d5af40c2f697a7b45a6df62abc4aa68ff52fa362"
## [1] "Receiving data..."
plot(hour.ts$hours, c(hour.ts$values, NA), type = "l", 
     xlab = "Hour", ylab = "Temperature", las = 1,
     main = "Hourly temperature in Sevilla 1 January 1978")

@corneliussenf
Copy link

Hi there!

I get an error when executing the example code:

install.packages("RFc")

library(RFc)

hour.ts <- fcTimeSeriesHourly(variable="airt",
latitude = 37.4, longitude = -6,
firstYear = 1978, lastYear = 1978,
firstDay = 1, lastDay = 1,
startHour = 0, stopHour = 24)

Error in x[sapply(x, is.null)] <- NA : invalid subscript type 'list'

My R Session Info:

R version 3.2.4 (2016-03-10)
Platform: x86_64-apple-darwin13.4.0 (64-bit)
Running under: OS X 10.11.6 (El Capitan)

locale:
[1] de_DE.UTF-8/de_DE.UTF-8/de_DE.UTF-8/C/de_DE.UTF-8/de_DE.UTF-8

attached base packages:
[1] stats graphics grDevices utils datasets methods base

other attached packages:
[1] RFc_0.1-2 sp_1.2-3 httr_1.2.1 jsonlite_1.0

loaded via a namespace (and not attached):
[1] R6_2.1.2 tools_3.2.4 curl_1.1 grid_3.2.4 lattice_0.20-33

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment