-
Notifications
You must be signed in to change notification settings - Fork 0
/
build-verif-script.qmd
540 lines (427 loc) · 13.1 KB
/
build-verif-script.qmd
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
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
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
---
title: Build a Point Verification Script
---
In this tutorial we are going to build a basic script that could be used to run verification tasks in a production environment.
## Basic Skeleton
We will start our script from a skeleton of comments that describe the basic point verification workflow, and then populate it with the variable assignments and functions. First this is the basic workflow.
```{r comments-only}
# Attach libraries
# Read Forecasts
# Scale
# Select Common cases
# Read observations
# Join
# Observation errors
# Verify
# Save
```
We can now add the function calls for each section
```{r function-calls, eval=FALSE}
# Attach libraries
library(harp)
library(here)
# Read Forecasts
fcst <- read_point_forecast(
dttm = seq_dttm(...),
fcst_model = c("..."),
fcst_type = "det",
parameter = "...",
lead_time = seq(...),
file_path = "..."
)
# Scale
fcst <- scale_param(fcst, ..., "...")
# Select Common cases
fcst <- common_cases(fcst)
# Read observations
obs <- read_point_obs(
dttm = unique_valid_dttm(fcst),
parameter = "...",
stations = unique_stations(fcst),
obs_path = "...",
min_allowed = ...,
max_allowed = ...
)
# Scale
obs <- scale_param(obs, ..., "...", col = ...)
# Join
fcst <- join_to_fcst(fcst, obs)
# Observation errors
fcst <- check_obs_against_fcst(fcst, ..., num_sd_allowed = ...)
# Verify
verif <- det_verify(fcst, ..., thresholds = ..., groupings = c("..."))
# Save
save_point_verif(verif, "...")
```
Everything we have marked with `...` is essentially a variable that we can set at the beginning of the script. So we can assign those variables and test the first iteration of the script.
```{r first-iteration, message=FALSE, warning=FALSE, error=TRUE}
# Attach libraries
library(harp)
library(here)
# Paths
fcst_dir <- here("data", "FCTABLE")
obs_dir <- here("data", "OBSTABLE")
verif_dir <- here("data", "verification")
# Parameters
prm <- "T2m"
# Forecast variables
date_times <- seq_dttm(2023080100, 2023083118, "6h")
fcst_models <- "arome_arctic"
lt <- seq(0, 24, 3)
fc_scaling <- -273.15
fc_units <- "degC"
# Obs variables
obs_scaling <- -273.15
obs_units <- "degC"
min_obs <- 223
max_obs <- 333
error_sd <- 4
# Verif variables
thresh <- seq(-5, 25, 5)
grps <- list(
"lead_time",
c("lead_time", "fcst_cycle")
)
# Read Forecasts
fcst <- read_point_forecast(
dttm = date_times,
fcst_model = fcst_models,
fcst_type = "det",
parameter = prm,
lead_time = lt,
file_path = fcst_dir
)
# Scale
fcst <- scale_param(fcst, fc_scaling, fc_units)
# Select Common cases
fcst <- common_cases(fcst)
# Read observations
obs <- read_point_obs(
dttm = unique_valid_dttm(fcst),
parameter = prm,
stations = unique_stations(fcst),
obs_path = obs_dir,
min_allowed = min_obs,
max_allowed = max_obs
)
# Scale
obs <- scale_param(obs, obs_scaling, obs_units, col = prm)
# Join
fcst <- join_to_fcst(fcst, obs)
# Observation errors
fcst <- check_obs_against_fcst(fcst, prm, num_sd_allowed = error_sd)
# Verify
verif <- det_verify(fcst, prm, thresholds = thresh, groupings = grps)
# Save
save_point_verif(verif, verif_dir)
```
We have now run into our first problem as an error has occurred with `scale_param()` for the observations data. The error message isn't that instructive (unfortunately). It is caused because many __harp__ functions use Non Standard Evaluation (NSE).
## Non Standard Evaluation
__harp__ takes a lot of inspiration from the [tidyverse](https://www.tidyverse.org/) - a collection of R packages designed to provide a consistent way of working with data in _R_. Some of the functions that we have already seen, such as `mutate()`, are part of the tidyverse. These functions are designed to make working interactively smooth, and one aspect of this is passing arguments that are unquoted. This applies to the names of columns in data frames and harp follows suit. While working interactively is easier and more intuitive with NSE, it comes at the expense of making programming a little bit trickier.
However, the solution is straightforward. When passing the name of a column to a function as a variable rather than the name itself it needs to be _embraced_. That is to say wrapped in double curly braces `{{ }}`. In the case of __harp__ functions, this is often where the parameter name is passed to a function to identify the column that contains observations. We can now modify our basic script to embrace arguments where appropriate.
```{r first-iteration-embraced, message=FALSE, warning=FALSE, error=TRUE}
# Attach libraries
library(harp)
library(here)
# Paths
fcst_dir <- here("data", "FCTABLE")
obs_dir <- here("data", "OBSTABLE")
verif_dir <- here("data", "verification")
# Parameters
prm <- "T2m"
# Forecast variables
date_times <- seq_dttm(2023080100, 2023083118, "6h")
fcst_models <- "arome_arctic"
lt <- seq(0, 24, 3)
fc_scaling <- -273.15
fc_units <- "degC"
# Obs variables
obs_scaling <- -273.15
obs_units <- "degC"
min_obs <- 223
max_obs <- 333
error_sd <- 4
# Verif variables
thresh <- seq(-5, 25, 2.5)
grps <- list(
"lead_time",
c("lead_time", "fcst_cycle")
)
# Read Forecasts
fcst <- read_point_forecast(
dttm = date_times,
fcst_model = fcst_models,
fcst_type = "det",
parameter = prm,
lead_time = lt,
file_path = fcst_dir
)
# Scale
fcst <- scale_param(fcst, fc_scaling, fc_units)
# Select Common cases
fcst <- common_cases(fcst)
# Read observations
obs <- read_point_obs(
dttm = unique_valid_dttm(fcst),
parameter = prm,
stations = unique_stations(fcst),
obs_path = obs_dir,
min_allowed = min_obs,
max_allowed = max_obs
)
# Scale
obs <- scale_param(obs, obs_scaling, obs_units, col = {{prm}})
# Join
fcst <- join_to_fcst(fcst, obs)
# Observation errors
fcst <- check_obs_against_fcst(fcst, {{prm}}, num_sd_allowed = error_sd)
# Verify
verif <- det_verify(fcst, {{prm}}, thresholds = thresh, groupings = grps)
# Save
save_point_verif(verif, verif_dir)
```
We now have the beginnings of a verification script. But what if we want to do verification for different parameters. One approach would be for the user to simply change the variable assignments at the top of the script for each use. However, this still requires a lot of manual work. A more efficient approach would be to set up the script to handle multiple parameters.
## Script for multiple parameters
When dealing with multiple parameters, each parameter may require different scalings, different thresholds, different error settings, different parameter names for forecasts and observations, or possibly different groupings. This will require a little more setting up. One approach may be to set some defaults and then some specific settings for different parameters. Lists in _R_ provide an excellent mechanism for doing this.
Let's now make the parameter part of our script more descriptive for multiple parameters.
```{r param-loop, message=FALSE, warning=FALSE}
# Attach libraries
library(harp)
library(here)
# Date times
start_dttm <- 2023080100
end_dttm <- 2023083118
dttm_step <- "6h"
# Forecast variables
date_times <- seq_dttm(start_dttm, end_dttm, dttm_step)
fcst_models <- "arome_arctic"
lt <- seq(0, 24, 3)
# Paths
fcst_dir <- here("data", "FCTABLE")
obs_dir <- here("data", "OBSTABLE")
verif_dir <- here("data", "verification")
defaults <- list(
grps = list(
"lead_time",
c("lead_time", "fcst_cycle")
),
error_sd = 6
)
# Parameters
params <- list(
T2m = list(
fc_scaling = list(
scaling = -273.15,
new_units = "degC"
),
obs_scaling = list(
scaling = -273.15,
new_units = "degC"
),
min_obs = 223,
max_obs = 333,
error_sd = 4,
thresh = seq(-5, 25, 5)
),
S10m = list(
fc_param = "ws10m",
min_obs = 0,
max_obs = 100,
thresh = c(1, 2, 3, 5, 7.5, 10, 15, 20, 25)
)
)
# Loop over parameters
for (prm in names(params)) {
fc_prm <- params[[prm]]$fc_param
if (is.null(fc_prm)) {
fc_prm <- prm
}
# Read Forecasts
fcst <- read_point_forecast(
dttm = date_times,
fcst_model = fcst_models,
fcst_type = "det",
parameter = fc_prm,
lead_time = lt,
file_path = fcst_dir
)
# Scale
if (!is.null(params[[prm]]$fc_scaling)) {
fcst <- do.call(
scale_param,
c(list(x = fcst), params[[prm]]$fc_scaling)
)
}
# Select Common cases
fcst <- common_cases(fcst)
# Read observations
obs_prm <- params[[prm]]$obs_param
if (is.null(obs_prm)) {
obs_prm <- prm
}
obs <- read_point_obs(
dttm = unique_valid_dttm(fcst),
parameter = obs_prm,
stations = unique_stations(fcst),
obs_path = obs_dir,
min_allowed = params[[prm]]$min_obs,
max_allowed = params[[prm]]$max_obs
)
# Scale
if (!is.null(params[[prm]]$obs_scaling)) {
obs <- do.call(
scale_param,
c(
list(x = obs),
params[[prm]]$obs_scaling,
list(col = {{obs_prm}})
)
)
}
# Join
fcst <- join_to_fcst(fcst, obs)
# Observation errors
error_sd <- params[[prm]]$error_sd
if (is.null(error_sd)) {
error_sd <- defaults$error_sd
}
fcst <- check_obs_against_fcst(
fcst,
{{obs_prm}},
num_sd_allowed = error_sd
)
# Verify
grps <- params[[prm]]$grps
if (is.null(grps)) {
grps <- defaults$grps
}
thresh <- params[[prm]]$thresh
verif <- det_verify(
fcst,
{{obs_prm}},
thresholds = thresh,
groupings = grps
)
# Save
save_point_verif(verif, verif_dir)
}
```
## Function instead of loop
_R_ is designed to be a _functional programming_ language. One of the advantages of this is that a function can be called repeatedly with different arguments making for (hopefully!) more structured and repeatable programs. If a function fails, it is also much easier to move onto the next iteration.
Let's rewrite the contents of our loop as a function that takes some arguments.
```{r verif-function}
run_verif <- function(
param_list,
param_name,
fc_models,
dttm,
ld_times,
fc_data_dir,
obs_data_dir,
dflts,
vrf_data_dir
) {
fc_prm <- param_list$fc_param
if (is.null(fc_prm)) {
fc_prm <- param_name
}
# Read Forecasts
fcst <- read_point_forecast(
dttm = dttm,
fcst_model = fc_models,
fcst_type = "det",
parameter = fc_prm,
lead_time = ld_times,
file_path = fc_data_dir
)
# Scale
if (!is.null(param_list$fc_scaling)) {
fcst <- do.call(
scale_param,
c(list(x = fcst), param_list$fc_scaling)
)
}
# Select Common cases
fcst <- common_cases(fcst)
# Read observations
obs_prm <- param_list$obs_param
if (is.null(obs_prm)) {
obs_prm <- param_name
}
obs <- read_point_obs(
dttm = unique_valid_dttm(fcst),
parameter = obs_prm,
stations = unique_stations(fcst),
obs_path = obs_dir,
min_allowed = param_list$min_obs,
max_allowed = param_list$max_obs
)
# Scale
if (!is.null(param_list$obs_scaling)) {
obs <- do.call(
scale_param,
c(
list(x = obs),
param_list$obs_scaling,
list(col = {{obs_prm}})
)
)
}
# Join
fcst <- join_to_fcst(fcst, obs)
# Observation errors
error_sd <- param_list$error_sd
if (is.null(error_sd)) {
error_sd <- dflts$error_sd
}
fcst <- check_obs_against_fcst(
fcst,
{{obs_prm}},
num_sd_allowed = error_sd
)
# Verify
grps <- param_list$grps
if (is.null(grps)) {
grps <- dflts$grps
}
thresh <- param_list$thresh
verif <- det_verify(
fcst,
{{obs_prm}},
thresholds = thresh,
groupings = grps
)
# Save
save_point_verif(verif, vrf_data_dir)
}
```
Now instead of using a for loop we can use `iwalk()` from the _purrr_ package. `iwalk()` calls a function for each element in the list that it is given. The first argument to that function is the list element and the second argument is the element name. `iwalk()` only calls the function for its side effects (e.g. writing a file) and returns the input untouched. Here we will call an anonymous function that calls `run_verif()`.
```{r walk-run-verif, message=FALSE, warning=FALSE}
library(purrr)
iwalk(
params,
\(x, y) run_verif(
param_list = x,
param_name = y,
fc_models = fcst_models,
dttm = date_times,
ld_times = lt,
fc_data_dir = fcst_dir,
obs_data_dir = obs_dir,
dflts = defaults,
vrf_data_dir = verif_dir
)
)
```
Using this approach, the `run_verif()` function can be modified to do any manipulations of the data that the user can think of, or using lists in combination with defaults can allow the user to provide specific information without having to edit the `run_verif()` function.
In the next tutorial we will go through the workflow do verify data on regular grids.
::: grid
::: g-col-1
<a href=point-verif-workflow.html><i class="bi bi-arrow-left-circle-fill"></i></a>
:::
::: g-col-10
:::
::: g-col-1
<a href=spatial-verif.html><i class="bi bi-arrow-right-circle-fill"></i></a>
:::
:::