library(fmi)
library(raster)
library(rasterVis)
apiKey <- readLines("apikey.txt")
# 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 = apiKey)
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/RtmpjgiVB9/file292f397c01b4", 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")
