This demo blog post uses a very developmental version of fmi. To run the demo on your own computer, you will have to do the following:
# fmi depends on rwfs
install_github("ropengov/rwfs")
install_github("ropengov/fmi")
library(fmi)
library(raster)
library(rasterVis)
NOTE! You will need to provide your own apiKey first:
API_KEY <- "ENTER YOUR API KEY HERE"
# Get spatial data describing Ã…land
aland <- raster::getData('GADM', country = 'ALA', level = 0)
# Get geographical extent for Ã…land
aland_extent <- raster::extent(bbox(aland))
# Set the start and end dates
start_date <- "1992-01-01"
end_date <- "1992-12-31"
# Create a request and client objects using fmi
request <- FMIWFSRequest$new(apiKey = API_KEY)
client <- FMIWFSClient$new(request = request)
# Set also a geographic extent. NOTE: for unknown reasons, fmi API respects
# the latitudinal extent in the query, but not longitudinal (i.e. the whole
# width of Finland is returned)
response <- client$getMonthlyWeatherRaster(startDateTime = start_date, endDateTime = end_date, bbox = aland_extent)
## OGR data source with driver: GML
## Source: "/tmp/RtmpMBmx3O/file3763790dd5a2", layer: "GridSeriesObservation"
## with 1 features
## It has 14 fields, of which 1 list fields
# Crop the reponse data to Ã…land extent only
response <- raster::crop(response, aland_extent)
# First 12 items in the response RasterBrick are the temperature averages
temps <- subset(response, 1:12)
# The following 12 the precipitation averages
precips <- subset(response, 13:24)
# Create date objects to use as Z values for rasterVis
idx <- seq(as.Date(start_date), as.Date(end_date), 'month')
# Set Z values and names for temperatures
temps <- setZ(temps, idx)
names(temps) <- month.abb
levelplot(temps, main = "Monthly mean temperature (C) in 1992",
par.settings = BuRdTheme)
# Set Z values and names for precipitations
precips <- setZ(precips, idx)
names(precips) <- month.abb
levelplot(precips, main = "Monthly mean precipitation (mm) in 1992")
sessionInfo()
## R version 3.3.1 (2016-06-21)
## Platform: x86_64-suse-linux-gnu (64-bit)
## Running under: openSUSE Tumbleweed
##
## locale:
## [1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C
## [3] LC_TIME=C LC_COLLATE=fi_FI.UTF-8
## [5] LC_MONETARY=fi_FI.UTF-8 LC_MESSAGES=en_US.UTF-8
## [7] LC_PAPER=en_US.UTF-8 LC_NAME=C
## [9] LC_ADDRESS=C LC_TELEPHONE=C
## [11] LC_MEASUREMENT=fi_FI.UTF-8 LC_IDENTIFICATION=C
##
## attached base packages:
## [1] stats graphics grDevices utils datasets methods base
##
## other attached packages:
## [1] rasterVis_0.40 latticeExtra_0.6-28 RColorBrewer_1.1-2
## [4] lattice_0.20-33 raster_2.5-8 sp_1.2-3
## [7] fmi_0.1.15 R6_2.2.0
##
## loaded via a namespace (and not attached):
## [1] Rcpp_0.12.7 knitr_1.14 magrittr_1.5
## [4] viridisLite_0.1.3 stringr_1.1.0 tools_3.3.1
## [7] parallel_3.3.1 rgdal_1.1-10 grid_3.3.1
## [10] htmltools_0.3.5 yaml_2.1.13 assertthat_0.1
## [13] digest_0.6.10 tibble_1.2 formatR_1.4
## [16] evaluate_0.10 rmarkdown_1.1 stringi_1.1.2
## [19] rwfs_0.1.15.9002 hexbin_1.27.1 zoo_1.7-13