Skip to content
Jorge Baño-Medina edited this page Dec 19, 2019 · 4 revisions

downscaleR.keras: An R package to integrate keras models in the climate4R framework

Introduction

We used as tutorial an example of the manuscript entitled Configuration and Intercomparison of Deep Learning Neural Models for Statistical Downscaling by J. Baño-Medina, R. Manzanas and J. M. Gutiérrez, which has been submitted for discussion to Geoscientific Model Development in September 2019. Througout this example we can learn to use the downscaleR.keras functions in a real case study. In particular, we downscale temperature and precipitation using reanalysis predictors at a resolution of 2º to an observational gridded dataset of 0.5º of resolution. This package is integrated into the climate4R framework and therefore we rely on other climate4R libraries, such as loadeR to load the data.

Loading Data

To load the data we use the loadeR package (link). In particular we download the ERA-Interim reanalysis's geopotential height, specific humidity, air temperature and meridional and zonal wind at 1000,850,700,500 hPa (predictors) and the E-OBS observed temperature and precipitation (predictands) datasets.

library(loadeR)
# ERA-Interim (2º)
variables <- c("z@500","z@700","z@850","z@1000",
               "hus@500","hus@700","hus@850","hus@1000",
               "ta@500","ta@700","ta@850","ta@1000",
               "ua@500","ua@700","ua@850","ua@1000",
               "va@500","va@700","va@850","va@1000")
x <- lapply(variables, function(x) {
  loadGridData(dataset = "ECMWF_ERA-Interim-ESD",
               var = x,
               lonLim = c(-10,32), # 22 puntos en total
               latLim = c(36,72),  # 19 puntos en total
               years = 1979:2008)
}) %>% makeMultiGrid()

# E-OBS Temperature (0.5º)
y_tas <- loadGridData(dataset = "E-OBS_v14_0.50regular",
                  var = "tas",lonLim = c(-10,32),
                  latLim = c(36,72), 
                  years = 1979:2008)

# E-OBS Precipitation (0.5º)
y_pr <- loadGridData(dataset = "E-OBS_v14_0.50regular",
                  var = "pr",lonLim = c(-10,32),
                  latLim = c(36,72), 
                  years = 1979:2008)

We split into train and test data using the function subsetGrid of climate4R's package transformeR (link).

library(transformeR)
# Train
xT <- subsetGrid(x,years = 1979:2002)
yT_tas <- subsetGrid(y_tas,years = 1979:2002)
yT_tas <- subsetGrid(y_tas,years = 1979:2002)
# Test
xt <- subsetGrid(x,years = 2003:2008)
yt_pr <- subsetGrid(y_pr,years = 2003:2008)
yt_pr <- subsetGrid(y_pr,years = 2003:2008)

We standardize the predictors by gridbox using the transformeR's function scaleGrid.

xt <- scaleGrid(xt,xT, type = "standardize", spatial.frame = "gridbox") %>% redim(drop = TRUE)
xT <- scaleGrid(xT, type = "standardize", spatial.frame = "gridbox") %>% redim(drop = TRUE)
Clone this wiki locally