knitr::opts_chunk$set(collapse = TRUE, comment = "#>", message=FALSE, cache = TRUE)

Package summary

The package weathergenr consists of a series of R scripts and function wrappers for synthetic weather generation based on the work of Steinscheineder et al (2013).

This implementation provides:

This vignette introduces you to the basic use of the multivariable, multigrid stochastic daily weather series generation process using weathergenr. You will learn\

Installation and setup

The latest version of the weathergenr package can be installed from github and then loaded to the environment:

#devtools::install_github("Deltares/weathergenr", upgrade = "never")
library(weathergenr)

Sample data

For this exercise, we will use the extracted ERA5 metereological dataset from the Ntoum basin, Gabon.

knitr::include_graphics('ntoum_basin.png')

Reading in gridded multivariate weather data

First, we will upload the gridded multivariate data for the basin from a netcdf file. The readNetcdf() is a wrapper for several ncdf4 functions to extract metereological data from netcdf files with associated spatial coordinates, dates, dimensions (time, x, y) and variable attributes.

ncfile <- system.file("extdata", "ntoum_era5_data.nc", package = "weathergenr")
ncdata <- readNetcdf(ncfile)

We can check the structure of this object:

names(ncdata)

Metereological data is stored as a list object via the data element. Each list represents a different grid cell in "tidy" format, i.e., with observations on rows and metereological variables on columns:

# Display climate data for the first gridcell
ncdata$data[[1]]

Information on the grids can be accessed via the grid element. It is also provided as a data frame with columns grid index followed by x and y dimension indices and x and y coordinate values:

# Display grid information
ncdata$grid

Finally, the date series associated with the data can be accessed with the date element:

# Display start and ending values date vector
head(ncdata$date)

Generate stochastic weather realizations

In this section, we introduce the procedure to obtain new weather sequences from the historical weather record via the generateWeatherSeries() function. This function serves as a wrapper arround a number of statistical procedures including a wavelet autoregressive model (WARM) coupled with a Markov chain knn resampling scheme based on Steinschneider and Brown (2013).

First, lets specify an output path for the results and variables to include from the dataset:

# Set path to store weather generator results
output_path <- tempdir()
variables <- c("precip", "temp", "temp_min", "temp_max")
realization_num <- 1

GenerateWeatherSeries() function includes a large set of essential and non-essential parameters to control the weather generation process. Essential parameters include: weather.data, weather.grid, and weather.date and variable.names to specify the attributes of the input metereological data, realization.num to set the desired number of new weather realizations, and the output.path:

stochastic_weather <- generateWeatherSeries(
  weather.data = ncdata$data,
  weather.grid = ncdata$grid,
  weather.date = ncdata$date,
  variable.names = variables,
  variable.labels = variables,
  variable.units = NULL,
  sim.year.num = 20,
  sim.year.start = 2020,
  month.start = 1,
  realization.num = realization_num,
  warm.variable = "precip",
  warm.signif.level = 0.90,
  warm.sample.num = 10000,
  warm.subset.criteria = NULL,
  knn.sample.num = 120,
  mc.wet.quantile= 0.2,
  mc.extreme.quantile = 0.8,
  evaluate.model = FALSE,
  evaluate.grid.num = 20,
  output.path = output_path,
  seed = 123)

The output is a list, where the first element is a data frame of resampled dates for each new stochastic realization and a date vector for the new (generated) weather data.

# Resampled dates
stochastic_weather$resampled

# Date vector
head(stochastic_weather$dates)
tail(stochastic_weather$dates)

Apply climate change on the realization

Delta factors can be imposed on the historical or stochastically generated weather data to reflect plausible changes on climate statistics. Currently, it is possible to shift mean and variance of precipitation and mean of temperature. Preciptation changes are specified as ratios, where a value of 1.0 indicates no change for the given calendar month. Temperature changes are specified as increases (or decreases) in degree celsius in a given month.

# Temperature changes Jan  Feb  Mar  Apr  May  Jun  Jul  Aug  Sep  Oct  Nov  Dec
delta_temp_mean <- c(3.0, 3.2, 3.4, 4.0, 4.1, 4.4, 5.0, 3.5, 3.3, 2.9, 2.8, 2.7)

# Precipitation changes   Jan  Feb  Mar  Apr  May  Jun  Jul  Aug  Sep  Oct  Nov  Dec
delta_precip_mean     <- c(0.7, 0.7, 0.8, 0.8, 0.7, 0.7, 0.7, 0.7, 0.7, 0.7, 0.7, 0.7)
delta_precip_variance <- c(1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0)

# Obtain stochastic series by re-ordering historical data
day_order <- match(stochastic_weather$resampled[[1]], ncdata$date)
stochastic_rlz <- lapply(ncdata$data, function(x) x[day_order,])

# Apply climate changes to climate data
stochastic2 <- imposeClimateChanges(
  climate.data = stochastic_rlz,
  climate.grid = ncdata$grid,
  sim.dates = stochastic_weather$dates,
  change.factor.precip.mean = delta_precip_mean,
  change.factor.precip.variance = delta_precip_variance,
  change.factor.temp.mean = delta_temp_mean,
  transient.temp.change = TRUE,
  transient.precip.change = TRUE,
  calculate.pet = TRUE,
  compute.parallel = TRUE,
  num.cores = NULL,
  fit.method = "mme")

Finally we can save the generated weather series back to a netcdf file:

# Save to netcdf file
writeNetcdf(
  data = stochastic2,
  coord.grid = ncdata$grid,
  output.path = output_path,
  origin.date =  stochastic_weather$dates[1],
  calendar.type = "noleap",
  nc.template.file = ncfile,
  nc.compression = 4,
  nc.spatial.ref = "spatial_ref",
  nc.file.prefix = "clim",
  nc.file.suffix = NULL)


Deltares/weathergenr documentation built on July 25, 2024, 6:15 p.m.