Newer
Older
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
171
172
173
174
175
176
177
178
179
180
compute_nao <- function(data, recipe, obsproj, plot_ts, plot_sp,
alpha, logo = NULL) {
## TODO: if fcst object in data compute the nao too
if (!is.null(data$fcst)) {
warning("NAO computed only for hindcast data.")
}
# Check if subsetting the data for the region is required
lons <- data$hcst$coords$longitude
lats <- data$hcst$coords$latitude
subset_needed <- FALSE
nao_region <- c(lonmin = -80, lonmax = 40,
latmin = 20, latmax = 80)
if (any(lons < 0)) { #[-180, 180]
if (!(min(lons) > -90 & min(lons) < -70 &
max(lons) < 50 & max(lons) > 30)) {
subset_needed <- TRUE
}
} else { #[0, 360]
if (any(lons >= 50 & lons <= 270)) {
susbset_needed <- TRUE
} else {
lon_E <- lons[which(lons < 50)]
lon_W <- lons[-which(lons < 50)]
if (max(lon_E) < 30 | min(lon_W) > 290) {
subset_needed <- TRUE
}
}
}
if (any(max(lats) > 80 & min(lats) < 20)) {
subset_needed <- TRUE
}
if (subset_needed) {
warning("The data is being subsetted for 20N-80N and 80W-40E region.")
hcst1 <- ClimProjDiags::SelBox(data$hcst$data,
lon = as.vector(lons),
lat = as.vector(lats),
region = nao_region,
londim = "longitude",
latdim = "latitude")
obs1 <- ClimProjDiags::SelBox(data$obs$data,
lon = as.vector(lons),
lat = as.vector(lats),
region = nao_region,
londim = "longitude",
latdim = "latitude")
hcst <- s2dv_cube(data = hcst1$data, lat = hcst1$lat, lon = hcst1$lon,
Variable = c(data$hcst$Variable[1], level = 'surface'),
data$hcst$Va, Datasets = data$hcst$Datasets,
time_dims = c('syear', 'time'),
Dates = data$hcst$Dates)
obs <- s2dv_cube(data = obs1$data, lat = obs1$lat, lon = obs1$lon,
Variable = c(data$obs$Variable[1], level = 'surface'),
Datasets = data$obs$Datasets, time_dims = c('syear', 'time'))
# TODO check and create data object for the next steps
data <- list(hcst = hcst, obs = obs)
lons <- data$hcst$coords$longitude
lats <- data$hcst$coords$latitude
obs1 <- hcst1 <- NULL
gc()
}
nao <- NAO(exp = data$hcst$data, obs = data$obs$data,
lat = data$hcst$coords$latitude,
lon = data$hcst$coords$longitude,
time_dim = 'syear',
memb_dim = 'ensemble', space_dim = c('latitude', 'longitude'),
ftime_dim = 'time', ftime_avg = NULL,
obsproj = obsproj, ncores = recipe$Analysis$ncores)
## Standardisation:
nao$exp <- Apply(list(nao$exp), target_dims = c('syear', 'ensemble'),
fun = function(x) {
sd <- sqrt(var(as.vector(x), na.rm = TRUE))
means <- mean(as.vector(x), na.rm = TRUE)
res <- apply(x, c(1,2), function(x) {(x-means)/sd})},
ncores = recipe$Analysis$ncores)$output1
nao$obs <- Apply(list(nao$obs), target_dims = c('syear', 'ensemble'),
fun = function(x) {
sd <- sqrt(var(as.vector(x), na.rm = TRUE))
means <- mean(as.vector(x), na.rm = TRUE)
res <- apply(x, c(1,2), function(x) {(x-means)/sd})},
ncores = recipe$Analysis$ncores)$output1
nao$exp <- InsertDim(nao$exp, posdim = 1, lendim = 1, name = 'region')
nao$obs <- InsertDim(nao$obs, posdim = 1, lendim = 1, name = 'region')
hcst_dates <- data$hcst$attrs$Dates
hcst_dates <- drop(data$hcst$attrs$Dates)
if (!("time" %in% names(dim(hcst_dates)))) {
if (is.null(dim(hcst_dates))) {
hcst_dates <- array(hcst_dates, c(syear = length(hcst_dates)))
}
hcst_dates <- InsertDim(hcst_dates, pos = 1, len = 1, name = 'time')
hcst_dates <- as.POSIXct(hcst_dates, origin = '1970-01-01', tz = 'UTC')
}
nao <- list(hcst = s2dv_cube(
data = nao$exp,
varName = "nao",
metadata = list(
region = list(name = "NAO region",
lats_range = paste0(range(lats)),
lons_range = paste0(range(lons))),
time = data$hcst$attrs$Variable$metadata$time,
nao = list(units = 'adim',
longname = 'North Atlantic Oscillation')),
Dates = hcst_dates,
Dataset = recipe$Analysis$Datasets$System$name),
obs = s2dv_cube(
data = nao$obs,
varName = "nao",
metadata = list(
region = list(name = "NAO region",
lats_range = paste0(range(lats)),
lons_range = paste0(range(lons))),
time = data$obs$attrs$Variable$metadata$time,
nao = list(units = 'adim',
longname = 'North Atlantic Oscillation')),
Dates = data$obs$attrs$Dates,
Dataset = recipe$Analysis$Datasets$Reference$name))
if (recipe$Analysis$Workflow$Indices$NAO$save == 'all') {
file_dest <- paste0(recipe$Run$output_dir, "/outputs/Indices/")
if (tolower(recipe$Analysis$Horizon) == "seasonal") {
# Use startdates param from SaveExp to correctly name the files:
if (length(data$hcst$attrs$source_files) == dim(data$hcst$data)['syear']) {
file_dates <- Apply(data$hcst$attrs$source_files, target_dim = NULL,
fun = function(x) {
pos <- which(strsplit(x, "")[[1]] == ".")
res <- substr(x, pos-8, pos-1)
})$output1
}
} else if (tolower(recipe$Analysis$Horizon) == "decadal"){
file_dates <- paste0('s',
recipe$Analysis$Time$hcst_start : recipe$Analysis$Time$hcst_end)
}
# need to recover original dimensions after saving to make Skill module work
nao_original_dims_hcst <- nao$hcst$data
nao$hcst$data <- .drop_indices_dims(nao$hcst$data)
CST_SaveExp(data = nao$hcst,
destination = file_dest,
startdates = as.vector(file_dates),
dat_dim = NULL, sdate_dim = 'syear',
ftime_dim = 'time', var_dim = NULL,
memb_dim = 'ensemble')
nao_original_dims_obs <- nao$obs$data
nao$obs$data <- .drop_indices_dims(nao$obs$data)
CST_SaveExp(data = nao$obs, #res,
destination = file_dest,
startdates = as.vector(file_dates),
dat_dim = NULL, sdate_dim = 'syear',
ftime_dim = 'time', var_dim = NULL,
memb_dim = NULL)
nao$hcst$data <- nao_original_dims_hcst
nao$obs$data <- nao_original_dims_obs
nao_original_dims_hcst <- nao_original_dims_obs <- NULL
gc()
}
# Read variable long_name to plot it
conf <- yaml::read_yaml("conf/variable-dictionary.yml")
var_name <- conf$vars[[which(names(conf$vars) ==
recipe$Analysis$Variables$name)]]$long
if (plot_ts) {
dir.create(paste0(recipe$Run$output_dir, "/plots/Indices/"),
showWarnings = F, recursive = T)
source("modules/Indices/R/plot_deterministic_forecast.R")
for (tstep in 1:dim(nao$hcst$data)['time']) {
mes <- as.numeric(substr(recipe$Analysis$Time$sdate, 1,2)) +
(tstep - 1) + (recipe$Analysis$Time$ftime_min - 1)
mes <- ifelse(mes > 12, mes - 12, mes)
fmonth <- sprintf("%02d", tstep - 1 + recipe$Analysis$Time$ftime_min)
obs <- Subset(nao$obs$data, along = 'time', ind = tstep)
exp <- Subset(nao$hcst$data, along = 'time', ind = tstep)
if (gsub(".", "", recipe$Analysis$Datasets$System$name) == "") {
system <- recipe$Analysis$Datasets$System$name
} else {
system <-gsub(".", "", recipe$Analysis$Datasets$System$name)
}
if (tolower(recipe$Analysis$Horizon) == "seasonal") {
toptitle <- paste("NAO Index\n",
month.abb[mes],
"/", recipe$Analysis$Time$hcst_start, "-",
recipe$Analysis$Time$hcst_end)
plotfile <- paste0(recipe$Run$output_dir, "/plots/Indices/NAO_",
system, "_", recipe$Analysis$Datasets$Reference$name,
"_s", recipe$Analysis$Time$sdate, "_ftime",
vagudets
committed
sprintf("%02d", tstep - 1 + recipe$Analysis$Time$ftime_min), ".pdf")
caption <- paste0("NAO method: ",
ifelse(recipe$Analysis$Workflow$Indices$NAO$obsproj,
"Pobs", "Pmod"), " (Doblas-Reyes et al., 2003)\n",
"Nominal start date: 1st of ",
month.name[as.numeric(substr(recipe$Analysis$Time$sdate, 1,2))],
"\n",
"Forecast month: ", fmonth, "\n")
xlabs <- as.numeric(substr(file_dates, 1, 4))
} else if (tolower(recipe$Analysis$Horizon) == "decadal"){
toptitle <- paste("NAO Index\n",
"Lead time", fmonth,
" / Start dates", recipe$Analysis$Time$hcst_start, "-",
recipe$Analysis$Time$hcst_end)
plotfile <- paste0(recipe$Run$output_dir, "/plots/Indices/NAO_",
system, "_", recipe$Analysis$Datasets$Reference$name,
"_ftime",
vagudets
committed
sprintf("%02d", tstep - 1 + recipe$Analysis$Time$ftime_min), ".pdf")
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
caption <- paste0("NAO method: ",
ifelse(recipe$Analysis$Workflow$Indices$NAO$obsproj,
"Pobs", "Pmod"), " (Doblas-Reyes et al., 2003)\n",
"Start date month: ",
month.name[get_archive(recipe)$System[[recipe$Analysis$Datasets$System$name]]$initial_month],
"\n",
"Lead time: ", fmonth, "\n")
xlabs <- file_dates
} else {
toptitle <- NULL
warning("The plot title is not defined for this time horizon. ",
"The plots will not have a title.")
}
plot_deterministic_forecast(obs, exp,
time_dim = 'syear',
member_dim = 'ensemble', style = 'boxplot',
xlabs = xlabs,
title = toptitle, fileout = plotfile,
caption = caption, caption_line = 6.5,
legend_text = c(
recipe$Analysis$Datasets$Reference$name,
recipe$Analysis$Datasets$System$name))
}
}
if (plot_sp) {
## TODO: To be removed when s2dv is released:
source("modules/Visualization/R/tmp/PlotRobinson.R")
source("modules/Indices/R/correlation_eno.R")
source("modules/Visualization/R/get_proj_code.R")
dir.create(paste0(recipe$Run$output_dir, "/plots/Indices/"),
showWarnings = F, recursive = T)
# Get correct code for stereographic projection
projection_code <- get_proj_code(proj_name = "stereographic")
correl_obs <- Apply(list(data$obs$data, nao$obs$data),
target_dims = 'syear', fun = .correlation_eno,
time_dim = 'syear', method = 'pearson', alpha = alpha,
test.type = 'two-sided', pval = FALSE,
ncores = recipe$Analysis$ncores)
correl_hcst <- Apply(list(data$hcst$data, nao$hcst$data),
target_dims = c('syear', 'ensemble'),
fun = function(x, y) {
x <- apply(x, 1, mean, na.rm = TRUE)
y <- apply(y, 1, mean, na.rm = TRUE)
dim(y) <- c(syear = length(y))
dim(x) <- c(syear = length(x))
res <- .correlation_eno(x, y,
time_dim = 'syear', method = 'pearson', alpha = alpha,
test.type = 'two-sided', pval = FALSE)},
ncores = recipe$Analysis$ncores)
correl_hcst_full <- Apply(list(data$hcst$data, nao$hcst$data),
target_dims = c('syear', 'ensemble'),
fun = function(x,y) {
dim(y) <- c(syear = length(y))
dim(x) <- c(syear = length(x))
res <- .correlation_eno(x, y,
time_dim = 'syear', method = 'pearson', alpha = alpha,
test.type = 'two-sided', pval = FALSE)},
ncores = recipe$Analysis$ncores)
for (tstep in 1:dim(nao$obs$data)['time']) {
fmonth <- sprintf("%02d", tstep - 1 + recipe$Analysis$Time$ftime_min)
## Observations
map <- drop(Subset(correl_obs$r, along = 'time', ind = tstep))
sig <- drop(Subset(correl_obs$sign, along = 'time', ind = tstep))
if (tolower(recipe$Analysis$Horizon) == "seasonal") {
mes <- as.numeric(substr(recipe$Analysis$Time$sdate, 1,2)) +
(tstep - 1) + (recipe$Analysis$Time$ftime_min - 1)
mes <- ifelse(mes > 12, mes - 12, mes)
toptitle <- paste(recipe$Analysis$Datasets$Reference$name, "\n",
"NAO Index -",var_name, "\n",
" Correlation /", month.abb[mes],
"/", recipe$Analysis$Time$hcst_start, "-",
recipe$Analysis$Time$hcst_end)
plotfile <- paste0(recipe$Run$output_dir, "/plots/Indices/NAO_correlation_",
recipe$Analysis$Variable$name, "_",
recipe$Analysis$Datasets$Reference$name,
"_s", recipe$Analysis$Time$sdate,
vagudets
committed
"_ftime", fmonth, ".pdf")
caption <- paste0("NAO method: ",
ifelse(recipe$Analysis$Workflow$Indices$NAO$obsproj,
"Pobs", "Pmod"), " (Doblas-Reyes et al., 2003)\n",
"Nominal start date: 1st of ",
month.name[as.numeric(substr(recipe$Analysis$Time$sdate, 1,2))],
"\n",
"Forecast month: ", fmonth, "\n",
"Pearson correlation; alpha = ", alpha)
} else if (tolower(recipe$Analysis$Horizon) == "decadal") {
toptitle <- paste(recipe$Analysis$Datasets$Reference$name, "\n",
"NAO Index -",var_name, "\n",
" Correlation / Start dates ",
recipe$Analysis$Time$hcst_start, "-",
recipe$Analysis$Time$hcst_end)
plotfile <- paste0(recipe$Run$output_dir, "/plots/Indices/NAO_correlation_",
recipe$Analysis$Variable$name, "_",
recipe$Analysis$Datasets$Reference$name,
vagudets
committed
"_ftime", fmonth, ".pdf")
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
caption <- paste0("NAO method: ",
ifelse(recipe$Analysis$Workflow$Indices$NAO$obsproj,
"Pobs", "Pmod"), " (Doblas-Reyes et al., 2003)\n",
"Start date: month ",
month.name[get_archive(recipe)$System[[recipe$Analysis$Datasets$System$name]]$initial_month],
"\n",
"Forecast month: ", fmonth, "\n",
"Pearson correlation; alpha = ", alpha)
} else {
toptitle <- NULL
warning("The plot title is not defined for this time horizon. ",
"The plots will not have a title.")
}
if (gsub(".", "", recipe$Analysis$Datasets$System$name) == "") {
system <- recipe$Analysis$Datasets$System$name
} else {
system <- gsub(".", "", recipe$Analysis$Datasets$System$name)
}
PlotRobinson(map, lon = lons, lat = lats, target_proj = projection_code,
lat_dim = 'latitude', lon_dim = 'longitude',
legend = 's2dv', style = 'polygon',
toptitle = toptitle, crop_coastlines = nao_region,
caption = caption, mask = sig, bar_extra_margin = c(4,0,4,0),
fileout = plotfile, width = 8, height = 6,
brks = seq(-1, 1, 0.2), cols = brewer.pal(10, 'PuOr'))
## Ensemble-mean
map <- drop(Subset(correl_hcst$r, along = 'time', ind = tstep))
sig <- drop(Subset(correl_hcst$sign, along = 'time', ind = tstep))
if (tolower(recipe$Analysis$Horizon) == "seasonal") {
toptitle <- paste(recipe$Analysis$Datasets$System$name, "\n",
"NAO Index -", var_name, "\n",
" Correlation /", month.abb[mes],
"/", recipe$Analysis$Time$hcst_start, "-",
recipe$Analysis$Time$hcst_end)
plotfile <- paste0(recipe$Run$output_dir, "/plots/Indices/NAO_correlation_",
recipe$Analysis$Variable$name, "_ensmean_",
system,
"_s", recipe$Analysis$Time$sdate,
vagudets
committed
"_ftime", fmonth, ".pdf")
caption <- paste0("NAO method: ",
ifelse(recipe$Analysis$Workflow$Indices$NAO$obsproj,
"Pobs", "Pmod"), " (Doblas-Reyes et al., 2003)\n",
"Correlation ensemble mean\n",
"Nominal start date: 1st of ",
month.name[as.numeric(substr(recipe$Analysis$Time$sdate, 1,2))],
"\n",
"Forecast month: ", fmonth, "\n",
"Pearson correlation; alpha = ", alpha)
} else if (tolower(recipe$Analysis$Horizon) == "decadal"){
toptitle <- paste(recipe$Analysis$Datasets$System$name,"\n",
"NAO Index -",var_name, "\n",
" Correlation / Start dates ",
recipe$Analysis$Time$hcst_start, "-",
recipe$Analysis$Time$hcst_end)
plotfile <- paste0(recipe$Run$output_dir, "/plots/Indices/NAO_correlation_",
recipe$Analysis$Variable$name, "_ensmean_",
system,
recipe$Analysis$Datasets$Reference$name,
vagudets
committed
"_ftime", fmonth, ".pdf")
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
caption <- paste0("NAO method: ",
ifelse(recipe$Analysis$Workflow$Indices$NAO$obsproj,
"Pobs", "Pmod"), " (Doblas-Reyes et al., 2003)\n",
"Correlation ensemble mean\n",
"Start date month: ",
month.name[get_archive(recipe)$System[[recipe$Analysis$Datasets$System$name]]$initial_month],
"\n",
"Forecast month: ", fmonth, "\n",
"Pearson correlation; alpha = ", alpha)
} else {
toptitle <- NULL
warning("The plot title is not defined for this time horizon. ",
"The plots will not have a title.")
}
PlotRobinson(map, lon = lons, lat = lats, target_proj = projection_code,
lat_dim = 'latitude', lon_dim = 'longitude',
legend = 's2dv', bar_extra_margin = c(4,0,4,0),
toptitle = toptitle, style = 'polygon',
caption = caption, mask = sig, crop_coastline = nao_region,
fileout = plotfile, width = 8, height = 6,
brks = seq(-1, 1, 0.2), cols = brewer.pal(10, 'PuOr'))
# Full hcst corr
map <- drop(Subset(correl_hcst_full$r, along = 'time', ind = tstep))
sig <- drop(Subset(correl_hcst_full$sign, along = 'time', ind = tstep))
if (tolower(recipe$Analysis$Horizon) == "seasonal") {
toptitle <- paste(recipe$Analysis$Datasets$System$name,"\n",
"NAO Index -",var_name, "\n",
" Correlation /", month.abb[mes],
"/", recipe$Analysis$Time$hcst_start, "-",
recipe$Analysis$Time$hcst_end)
plotfile <- paste0(recipe$Run$output_dir, "/plots/Indices/NAO_correlation_",
recipe$Analysis$Variable$name, "_member_",
system,
"_s", recipe$Analysis$Time$sdate,
vagudets
committed
"_ftime", fmonth, ".pdf")
caption <- paste0("NAO method: ",
ifelse(recipe$Analysis$Workflow$Indices$NAO$obsproj,
"Pobs", "Pmod"), " (Doblas-Reyes et al., 2003)\n",
"Correlation all members\n",
"Nominal start date: 1st of ",
month.name[as.numeric(substr(recipe$Analysis$Time$sdate, 1,2))],
"\n",
"Forecast month: ", fmonth, "\n",
"Pearson correlation; alpha = ", alpha)
} else if (tolower(recipe$Analysis$Horizon) == "decadal"){
toptitle <- paste(recipe$Analysis$Datasets$System$name,"\n",
"NAO Index -",var_name, "\n",
" Correlation / Start dates ",
recipe$Analysis$Time$hcst_start, "-",
recipe$Analysis$Time$hcst_end)
plotfile <- paste0(recipe$Run$output_dir, "/plots/Indices/NAO_correlation_",
recipe$Analysis$Variable$name, "_member_",
system,
recipe$Analysis$Datasets$Reference$name,
vagudets
committed
"_ftime", fmonth, ".pdf")
caption <- paste0("NAO method: ",
ifelse(recipe$Analysis$Workflow$Indices$NAO$obsproj,
"Pobs", "Pmod"), " (Doblas-Reyes et al., 2003)\n",
"Correlation all members\n",
"Start date month: ",
month.name[get_archive(recipe)$System[[recipe$Analysis$Datasets$System$name]]$initial_month],
"\n",
"Forecast month: ", fmonth, "\n",
"Pearson correlation; alpha = ", alpha)
} else {
toptitle <- NULL
warning("The plot title is not defined for this time horizon. ",
"The plots will not have a title.")
}
PlotRobinson(map, lon = lons, lat = lats, target_proj = projection_code,
lat_dim = 'latitude', lon_dim = 'longitude',
legend = 's2dv', bar_extra_margin = c(4,0,4,0),
toptitle = toptitle, style = 'polygon',
caption = caption, mask = sig, crop_coastline = nao_region,
fileout = plotfile, width = 8, height = 6,
brks = seq(-1, 1, 0.2), cols = brewer.pal(10, 'PuOr'))
} # end tstep loop
}
return(nao)
}