<- "output"
output_dir <- c("png", "eps")[1] # modify index number to change format plot_file_format
3 Example of simulation outputs of the Weather model for 5 years
Choose file format for generated figures:
Load source file containing the R implementation of the Weather model:
source("source/weatherModel.R")
Initialisation using the default parametrisation, based on data from Rakhigarhi (example location, see Fig. 1):
<- 0
SEED <- 365 # ignoring leap year adjustment
YEAR_LENGTH <- 5
NUM_YEARS <- NUM_YEARS * YEAR_LENGTH
NUM_DAYS
<- initialise_weather_model(seed = SEED, year_length = YEAR_LENGTH) weather_model
Show table with parameter values:
source("source/extract_params.R")
# Extract initial parameters
<- list(
initial_params names = c("seed", "year_length", "albedo", "southern_hemisphere"),
values = unlist(weather_model$PARAMS[1:4])
)
# Extract remaining parameters
<- lapply(names(weather_model$PARAMS)[5:length(weather_model$PARAMS)],
remaining_params function(name) extract_params(weather_model$PARAMS[[name]], name))
# Combine all parameters
<- list(
all_params names = c(initial_params$names, unlist(lapply(remaining_params, `[[`, "names"))),
values = c(initial_params$values, unlist(lapply(remaining_params, `[[`, "values")))
)
# Create the table
<- cbind(all_params$names, all_params$values)
params_values row.names(params_values) <- NULL
::kable(params_values,
knitrformat = "html",
col.names = c("parameter", "values"),
align = c("l", "r"))
parameter | values |
---|---|
seed | 0 |
year_length | 365 |
albedo | 0.4 |
southern_hemisphere | 0 |
temperature - annual_max | 40 |
temperature - annual_min | 15 |
temperature - daily_fluctuation | 5 |
temperature - daily_lower_dev | 5 |
temperature - daily_upper_dev | 5 |
solar - annual_max | 7 |
solar - annual_min | 3 |
solar - daily_fluctuation | 1 |
precipitation - annual_sum_mean | 400 |
precipitation - annual_sum_sd | 130 |
precipitation - plateau_value_mean | 0.1 |
precipitation - plateau_value_sd | 0.05 |
precipitation - inflection1_mean | 40 |
precipitation - inflection1_sd | 20 |
precipitation - rate1_mean | 0.15 |
precipitation - rate1_sd | 0.02 |
precipitation - inflection2_mean | 200 |
precipitation - inflection2_sd | 20 |
precipitation - rate2_mean | 0.05 |
precipitation - rate2_sd | 0.01 |
precipitation - n_samples_mean | 200 |
precipitation - n_samples_sd | 5 |
precipitation - max_sample_size_mean | 10 |
precipitation - max_sample_size_sd | 3 |
Run model:
<- run_weather_model(weather_model, num_years = NUM_YEARS) weather_model
Set colours for maximum and minimum temperature:
= hsv(7.3/360, 74.6/100, 70/100)
max_temperature_colour
= hsv(232/360, 64.6/100, 73/100) min_temperature_colour
Plot time-series:
# Helper functions
<- function(solar_radiation, num_days, year_length) {
plot_solar_radiation plot(1:num_days, solar_radiation,
type = "l", xlab = "", xaxt = 'n', ylab = "")
mark_end_years(num_days, year_length = year_length)
}
<- function(temperature, max_temperature_colour, min_temperature_colour, num_days, year_length) {
plot_temperature plot(1:num_days, temperature,
type = "l", xlab = "", xaxt = 'n', ylab = "",
ylim = c(floor(min(weather_model$daily$temperature_min)),
ceiling(max(weather_model$daily$temperature_max))))
lines(1:num_days, weather_model$daily$temperature_max,
col = adjustcolor(max_temperature_colour, alpha.f = 0.8))
lines(1:num_days, weather_model$daily$temperature_min,
col = adjustcolor(min_temperature_colour, alpha.f = 0.8))
mark_end_years(num_days, year_length = year_length)
}
<- function(ETr, num_days, year_length) {
plot_ETr plot(1:num_days, weather_model$daily$ETr, type = "l",
ylab = "", xlab = "", xaxt = 'n')
mark_end_years(num_days, year_length = year_length)
}
<- function(precipitation, num_days, year_length) {
plot_precipitation par(mar = c(2, 1, 0.1, 0.1))
barplot(weather_model$daily$precipitation,
ylab = "", xlab = "", xaxt = 'n')
mark_end_years(num_days, year_length = year_length, offset = 1.2)
abline(v = num_days * 1.2, lty = 3)
}
<- function(num_days, graphic_scale, font_rescale, margin_text_rescale) {
plot_time_axis par(mar = c(1, 1, 0, 0.1))
plot(c(1, num_days), c(0, 1), ann = FALSE, bty = 'n', type = 'n', xaxt = 'n', yaxt = 'n')
axis(3, at = 1:num_days, tck = 1, lwd = 0, line = axis_line)
mtext("day", side = 1, line = x_margin_line,
font = 4, cex = graphic_scale * (1 + font_rescale + margin_text_rescale))
}
# Main plotting function
<- function(weather_model, num_days, year_length, graphic_scale, font_rescale, axis_text_rescale, margin_text_rescale, max_temperature_colour, min_temperature_colour) {
plot_weather_simulation layout(matrix(c(1:10),
nrow = 5, ncol = 2, byrow = FALSE),
widths = c(1, 10),
heights = c(10, 10, 10, 12, 2))
<- c(expression(paste(
y_labs " Solar\nRadiation (", MJ/m^-2, ")")),
"Temperature (C)", "ETr (mm)", "Precipitation (mm)")
par(cex = graphic_scale)
# First column
par(mar = c(0, 0, 0, 0))
for (i in 1:4) {
plot(c(0, 1), c(0, 1), ann = FALSE, bty = 'n', type = 'n', xaxt = 'n', yaxt = 'n')
text(x = 0.5, y = 0.5 + (i > 2) * 0.1, font = 4,
cex = graphic_scale * (0.6 + 0.1 * (i > 1) + font_rescale + margin_text_rescale),
srt = 90,
labels = y_labs[i])
}plot(c(0, 1), c(0, 1), ann = FALSE, bty = 'n', type = 'n', xaxt = 'n', yaxt = 'n')
# Second column
par(mar = c(0.2, 1, 0.5, 0.1), cex.axis = graphic_scale * (0.6 + axis_text_rescale))
# 1: Solar radiation
plot_solar_radiation(weather_model$daily$solar_radiation, num_days = num_days, year_length = year_length)
# 2: Temperature
plot_temperature(weather_model$daily$temperature, num_days = num_days, year_length = year_length,
max_temperature_colour = max_temperature_colour, min_temperature_colour = min_temperature_colour)
# 3: Reference evapotranspiration
plot_ETr(weather_model$daily$ETr, num_days = num_days, year_length = year_length)
# 4: Precipitation
plot_precipitation(weather_model$daily$precipitation, num_days = NUM_DAYS, year_length = year_length)
# 5: x-axis title
plot_time_axis(num_days = num_days, graphic_scale = graphic_scale, font_rescale = font_rescale, margin_text_rescale = margin_text_rescale)
}
# Main execution
<- file.path(output_dir, paste0("Fig3-weather_modelExample.", plot_file_format))
plot_name
if (plot_file_format == "png") {
<- 1
graphic_scale <- 1
font_rescale <- 1
axis_text_rescale <- 0.3
margin_text_rescale <- -1
axis_line <- -0.3
x_margin_line png(plot_name, width = graphic_scale * 800, height = graphic_scale * 930)
else if (plot_file_format == "eps") {
} <- 1.2
graphic_scale <- 0.5
font_rescale <- -0.1
axis_text_rescale <- -0.3
margin_text_rescale <- 0
axis_line <- -1
x_margin_line ::loadfonts(device = "postscript")
extrafont::cairo_ps(filename = plot_name, pointsize = 12,
grDeviceswidth = graphic_scale * 6, height = graphic_scale * 7,
onefile = FALSE, family = "sans")
else {
} stop("Unsupported file format")
}
plot_weather_simulation(weather_model, num_days = NUM_DAYS, year_length = YEAR_LENGTH,
graphic_scale = graphic_scale, font_rescale = font_rescale,
axis_text_rescale = axis_text_rescale, margin_text_rescale = margin_text_rescale,
max_temperature_colour = max_temperature_colour, min_temperature_colour = min_temperature_colour)
dev.off()
svg
2
::include_graphics(plot_name) knitr