-
Notifications
You must be signed in to change notification settings - Fork 104
/
gwl_time_series_plots.R
172 lines (123 loc) · 4.6 KB
/
gwl_time_series_plots.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# gwl_time_series_plots.R
#
# Copyright (C) 2021 Santander Meteorology Group (http://meteo.unican.es)
#
# This work is licensed under a Creative Commons Attribution 4.0 International
# License (CC BY 4.0 - http://creativecommons.org/licenses/by/4.0)
#' @title Global Warming Level time timings displayed as multi-model time series plots
#' @description For a given CMIP project, experiment and filtering window width, produces
#' a plot of smoothed delta time series (1850-2100, w.r.t. the PI period 1850-1900)
#' for each ensemble member and draws its corresponding (central-year) warming level
#'
#' @author J. Bedia
library(magrittr)
library(RColorBrewer)
#
# Assume Atlas repo home as base directory
#
source("warming-levels/scripts/getGWL.R")
#
# Parameter settings (current values reproduce Fig. in the README file)
#
cmip <- "CMIP6"
gwl <- 2 # Possible values c(1.5, 2 ,3, 4)
exp <- "ssp370" # This is a CMIP-dependent parameter. See a few lines below.
window <- 20 # window width for centered moving average.
# Default to 20 (as used in the IPCC AR6 Atlas products)
cmip <- match.arg(cmip, choices = c("CMIP5", "CMIP6"))
#
# CMIP-dependent parameters
#
exp <- if (cmip == "CMIP6") {
match.arg(exp, choices = c("ssp126", "ssp245", "ssp370", "ssp585"))
} else {
match.arg(exp, choices = c("rcp26", "rcp45", "rcp85"))
}
#
# Load filenames to process
#
datadir <- sprintf("./datasets-aggregated-regionally/data/%s/%s_tas_landsea", cmip, cmip)
filelist <- list.files(datadir)
allfiles <- sprintf("%s/%s", datadir, filelist)
aux <- grep("historical", filelist, value = TRUE)
modelruns <- gsub(sprintf("%s_|_historical|\\.csv", cmip), "", aux)
# Remove run IDs to simplify the plot legend later:
gcm.names <- gsub("_r.*", "", modelruns)
#
# Main loop
#
# First, create an empty list populated with the ensemble members.
# Models without the target rcp/ssp are discarded
l <- list()
counter <- 0L
for (i in 1:length(modelruns)) {
modelfiles <- gsub("_", "_.*", modelruns[i], fixed = TRUE) %>%
grep(allfiles, value = TRUE)
hist <- grep("historical", modelfiles, value = TRUE) %>% world.annual.mean()
rcp <- grep(exp, modelfiles, value = TRUE)
if (length(rcp > 0L)) {
counter <- counter + 1L
message("[", Sys.time(), "] Processing ", modelruns[i])
rcp %<>% world.annual.mean()
tas <- append(hist, rcp)
# Fill with NAs if needed to get a continuous annual series 1850-2100
aux <- rep(NA, length(1850:2100))
names(aux) <- 1850:2100
aux[which(names(aux) %in% names(tas))] <- tas
# Delta change w.r.t. the PI period (1850-1900)
baseline <- hist[1:51] %>% mean(na.rm = TRUE)
aux <- aux - baseline
# Compute GWL central year and store as attribute
attr(aux, "GWL") <- getGWL(aux, window = window, GWL = gwl)[1]
l[[gcm.names[i]]] <- aux
}
}
#
# The final dataset is stored in tabular form as a data.frame
#
mat <- do.call("cbind.data.frame", l)
#
# Apply moving average to each ensemble member
#
filtered.mat <- apply(mat, MARGIN = 2L, FUN = "filter",
filter = rep(1/window, window), sides = 2)
#
# Plotting
#
# Y-axis ranges
ylims <- range(filtered.mat, na.rm = TRUE)
ylim <- c(ylims[1] - .5, ylims[2] + .5)
# Choosing random distinct colors for each ensemble member
# NOTE: color palette may not be fully reproducible due to the random color selection in this step
# https://stackoverflow.com/questions/15282580/how-to-generate-a-number-of-most-distinctive-colors-in-r
n <- ncol(filtered.mat)
qual_col_pals <- brewer.pal.info[brewer.pal.info$category == 'qual',]
col_vector <- unlist(mapply(brewer.pal, qual_col_pals$maxcolors, rownames(qual_col_pals)))
set.seed(2)
model.colors <- sample(col_vector, n)
# Empty plot
plot(1850:2100, filtered.mat[,1], ty = 'n',
ylim = ylim, las = 1, ylab = "Delta change w.r.t. PI period 1850-1900 (degC)", xlab = "year")
grid()
abline(h = gwl, col = "grey60")
# Add ensemble members
for (i in 1:ncol(filtered.mat)) {
lines(1850:2100, filtered.mat[ ,i], col = model.colors[i])
abline(v = attr(l[[i]], "GWL"), col = model.colors[i], lty = 2)
}
# Add legend
legend(x = "topleft",
legend = names(mat),
lty = 1, col = model.colors,
cex = .7, ncol = 2, bg = "grey90")
# Add title
title(paste(cmip, exp, "- GWL +", gwl, "degC"))
mtext(paste(window, "- year window width"), line = .25)
# Add range as a text label
rng <- sapply(l, "attr", "GWL") %>% range(na.rm = TRUE)
txt <- if (any(is.infinite(rng))) {
paste0("+", gwl , " degC GWL not reached\n by any ensemble member")
} else {
paste("GWL range:", paste(rng, collapse = "-"))
}
text(1975, -0.5, txt, col = "blue")