/
write_full_analysis.R
1808 lines (1632 loc) 路 84.7 KB
/
write_full_analysis.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
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
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# Copyright 2019 Province of British Columbia
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and limitations under the License.
#' @title Write a suite of tables and plots from various fasstr functions into a directory
#'
#' @description Calculates and writes tables and plots from a suite of statistics from \code{fasstr} functions into
#' an Excel workbook, and accompanying plot files for certain analyses. Due to the number of tables and plots to be made, this
#' function may take several minutes to complete. If \code{ignore_missing = FALSE} (default) and there is missing data,
#' some tables and plots may be empty and produce warnings. Use \code{ignore_missing = TRUE} to ignore the missing values or
#' filter your data to complete years. Calculates statistics from all values, unless specified. Returns a list of tibbles and
#' plots, along with saving the Excel and image files in a directory.
#'
#' @inheritParams compute_full_analysis
#' @param file_name Character string of the name of the Excel Workbook (and folder for plots if necessary) to create on drive to
#' write all results.
#' @param plot_filetype Image type to write. One of \code{'png'}, \code{'eps'}, \code{'ps'}, \code{'tex'}, \code{'pdf'},
#' \code{'jpeg'}, \code{'tiff'}, \code{'bmp'}, or \code{'svg'}. If not \code{'pdf'} then individual plots will be created instead
#' of a combined PDF. Default \code{'pdf'}.
#'
#' @seealso \code{\link{compute_full_analysis}},
#' \code{\link{screen_flow_data}},
#' \code{\link{plot_data_screening}},
#' \code{\link{plot_missing_dates}},
#' \code{\link{calc_longterm_monthly_stats}},
#' \code{\link{plot_longterm_monthly_stats}},
#' \code{\link{calc_longterm_daily_stats}},
#' \code{\link{plot_longterm_daily_stats}},
#' \code{\link{plot_monthly_means}},
#' \code{\link{plot_flow_duration}},
#' \code{\link{calc_annual_stats}},
#' \code{\link{plot_annual_stats}},
#' \code{\link{calc_annual_cumulative_stats}},
#' \code{\link{plot_annual_cumulative_stats}},
#' \code{\link{calc_annual_flow_timing}},
#' \code{\link{plot_annual_flow_timing}},
#' \code{\link{calc_annual_normal_days}},
#' \code{\link{plot_annual_normal_days}},
#' \code{\link{calc_annual_lowflows}},
#' \code{\link{plot_annual_lowflows}},
#' \code{\link{plot_annual_means}},
#' \code{\link{calc_monthly_stats}},
#' \code{\link{plot_monthly_stats}},
#' \code{\link{calc_monthly_cumulative_stats}},
#' \code{\link{plot_monthly_cumulative_stats}},
#' \code{\link{calc_daily_stats}},
#' \code{\link{plot_daily_stats}},
#' \code{\link{calc_daily_cumulative_stats}},
#' \code{\link{plot_daily_cumulative_stats}},
#' \code{\link{compute_annual_trends}},
#' \code{\link{compute_annual_frequencies}},
#' \code{\link{write_flow_data}},
#' \code{\link{write_plots}}
#'
#' @examples
#' \dontrun{
#'
#' # Working examples:
#'
#' # Save a full analysis will all the analyses
#' write_full_analysis(station_number = "08NM116",
#' file_name = "Mission Creek",
#' start_year = 1980,
#' end_year = 2010)
#'
#' # Save a full analysis with only Annual and Daily analyses
#' write_full_analysis(station_number = "08NM116",
#' file_name = "Mission Creek",
#' start_year = 1980,
#' end_year = 2010,
#' analyses = c(3,5))
#'
#' }
#' @export
write_full_analysis <- function(data,
dates = Date,
values = Value,
groups = STATION_NUMBER,
station_number,
analyses = 1:7,
basin_area,
water_year_start = 1,
start_year,
end_year,
exclude_years,
months = 1:12,
ignore_missing = FALSE,
complete_years = FALSE,
allowed_missing_annual = ifelse(ignore_missing,100,0),
allowed_missing_monthly = ifelse(ignore_missing,100,0),
zyp_method = 'zhang',
zyp_alpha,
file_name,
plot_filetype = 'pdf'){
## ARGUMENT CHECKS
## ---------------
if (missing(data)) {
data <- NULL
}
if (missing(station_number)) {
station_number <- NULL
}
if (missing(start_year)) {
start_year <- 0
}
if (missing(end_year)) {
end_year <- 9999
}
if (missing(exclude_years)) {
exclude_years <- NULL
}
if (missing(basin_area)) {
basin_area <- NA
}
if (missing(zyp_alpha)) {
zyp_alpha <- NA
}
water_year_checks(water_year_start)
years_checks(start_year, end_year, exclude_years)
logical_arg_check(ignore_missing)
allowed_missing_checks(allowed_missing_annual, ignore_missing)
allowed_missing_checks(allowed_missing_monthly, ignore_missing)
months_checks(months)
logical_arg_check(complete_years)
if (complete_years) {
if (ignore_missing | allowed_missing_annual > 0 | allowed_missing_monthly > 0) {
ignore_missing <- FALSE
allowed_missing_annual <- 0
allowed_missing_monthly <- 0
message("complete_years argument overrides ignore_missing and allowed_missing_* arguments.")
}
}
if (missing(file_name)) stop("A file name is required with the file_name argument to write all results.", call. = FALSE)
if (!is.numeric(analyses))
stop("analyses argument must be numbers between 1 and 7. See ?write_full_analysis for analysis group numbers.", call. = FALSE)
if (!all(analyses %in% 1:7))
stop("analyses argument must be numbers between 1 and 7. See ?write_full_analysis for analysis group numbers.", call. = FALSE)
if (6 %in% analyses) {
zyp_alpha_checks(zyp_alpha)
zyp_method_checks(zyp_method)
}
# Do this for now, until looping of include_year plots is sorted out
if (length(station_number) > 1) stop("Only one station_number can be listed.", call. = FALSE)
message("* this may take a few moments...")
## FLOW DATA CHECKS AND FORMATTING
## -------------------------------
# Check if data is provided and import it
flow_data_source <- flowdata_import(data = data,
station_number = station_number)
flow_data_raw <- flow_data_source
# Save the original columns (to check for STATION_NUMBER col at end) and ungroup if necessary
orig_cols <- names(flow_data_raw)
flow_data_raw <- dplyr::ungroup(flow_data_raw)
# Check and rename columns
flow_data_raw <- format_all_cols(data = flow_data_raw,
dates = as.character(substitute(dates)),
values = as.character(substitute(values)),
groups = as.character(substitute(groups)),
rm_other_cols = TRUE)
# # Data setup
flow_data_unfiltered <- fill_missing_dates(data = flow_data_raw, water_year_start = water_year_start)
flow_data_unfiltered <- add_date_variables(data = flow_data_unfiltered, water_year_start = water_year_start)
flow_data_unfiltered <- add_rolling_means(data = flow_data_unfiltered)
#
# # Set up basin_area
flow_data_unfiltered <- add_basin_area(flow_data_unfiltered, basin_area = basin_area)
basin_area_stn <- unique(flow_data_unfiltered$Basin_Area_sqkm)[1]
# Get the start and end years of the data to make a list of all included years
flow_data_filtered <- flow_data_unfiltered
flow_data_filtered <- analysis_prep(data = flow_data_filtered,
water_year_start = water_year_start)
if (start_year < min(flow_data_filtered$WaterYear)) {
start_year <- min(flow_data_filtered$WaterYear)
}
if (end_year > max(flow_data_filtered$WaterYear)) {
end_year <- max(flow_data_filtered$WaterYear)
}
# flow_data_plus <- dplyr::filter(flow_data, WaterYear >= start_year - 1 & WaterYear <= end_year + 1)
flow_data_filtered <- dplyr::filter(flow_data_filtered,
WaterYear >= start_year & WaterYear <= end_year,
!(WaterYear %in% exclude_years))
if (any(is.na(flow_data_filtered$Value)) & !ignore_missing) {
message("** warning: selected data contains dates with missing values; some NAs in tables and gaps in plots may be produced")
}
# Create function for condensing consecutive numbers
conseq_fn <- function(s){
dif <- s[seq(length(s))][-1] - s[seq(length(s)-1)]
new <- !c(0, dif == 1)
cs <- cumsum(new)
res <- vector(mode="list", max(cs))
for(i in seq(res)){
s.i <- s[which(cs == i)]
if(length(s.i) > 2){
res[[i]] <- paste(min(s.i), max(s.i), sep=":")
} else {
res[[i]] <- as.character(s.i)
}
}
paste0(ifelse(length(s)==1,paste(s),paste0("c(", paste(unlist(res), collapse=","), ")")))
}
# Create list of all objects
message("** creating data frames and plots")
### Compute results
##########################
results <- suppressMessages(
compute_full_analysis(data = flow_data_raw,
dates = "Date",
values = "Value",
groups = "STATION_NUMBER",
analyses = analyses,
basin_area = basin_area,
water_year_start = water_year_start,
start_year = start_year,
end_year = end_year,
exclude_years = exclude_years,
months = months,
ignore_missing = ignore_missing,
complete_years = complete_years,
allowed_missing_annual = allowed_missing_annual,
allowed_missing_monthly = allowed_missing_monthly,
zyp_method = zyp_method,
zyp_alpha = zyp_alpha)
)
### Writing Functions
##########################
# Create add table function
add_table <- function(wb, sheet, data, title, col, row, comment = NA) {
openxlsx::writeData(wb = wb, sheet = sheet, x = title, startCol = col, startRow = row)
openxlsx::writeData(wb = wb, sheet = sheet, x = data, startCol = col, startRow = row + 1,
headerStyle = openxlsx::createStyle(fontSize = 11,
textDecoration = "bold",
border = "TopBottom",
fgFill = "#add8e6",
halign = "left"))
openxlsx::addStyle(wb = wb, sheet = sheet, cols = col, rows = row,
style = openxlsx::createStyle(fontSize = 11,
textDecoration = "bold"))
if (!is.na(comment)) {
openxlsx::writeComment(wb = wb, sheet = sheet, col = col, row = row,
comment = openxlsx::createComment(comment = comment,
visible = FALSE,
width = 5,
height = 4))
}
}
# Create add plot function
add_plot <- function(wb, sheet, plot, title, col, row, height, width, comment = NA) {
openxlsx::writeData(wb = wb, sheet = sheet, x = title, startCol = col, startRow = row)
print(plot)
openxlsx::insertPlot(wb = wb, sheet = sheet, startCol = col, startRow = row + 1, height = height, width = width)
openxlsx::addStyle(wb = wb, sheet = sheet, cols = col, rows = row,
style = openxlsx::createStyle(fontSize = 11,
textDecoration = "bold"))
if (!is.na(comment)) {
openxlsx::writeComment(wb = wb, sheet = sheet, col = col, row = row,
comment = openxlsx::createComment(comment = comment,
visible = FALSE,
width = 5,
height = 4))
}
}
### Setup dataframe and values for writing the fasstr functions
##########################
fasstr_functions <- data.frame("Worksheet" = character(), "Output" = character(), "Function" = character())
# Create some values for creating character versions of fasstr functions
fn_data <- paste0(ifelse(!is.null(data),
paste0("data = ", as.character(substitute(data)),
ifelse(as.character(substitute(dates)) != "Date",
paste0(", dates = '", as.character(substitute(dates)), "'"), ""),
ifelse(as.character(substitute(values)) != "Value",
paste0(", values = '", as.character(substitute(values)), "'"), ""),
ifelse(as.character(substitute(groups)) != "STATION_NUMBER",
paste0(", groups = '", as.character(substitute(groups)), "'"), "")),
paste0("station_number = '", station_number, "'")))
fn_area <- paste0(ifelse(!is.na(basin_area),
paste0(", basin_area = ", basin_area),
""))
fn_wys <- ifelse(water_year_start != 1, paste0(", water_year_start = ", water_year_start), "")
fn_startend <- paste0(ifelse(start_year != min(flow_data_unfiltered$WaterYear),
paste0(", start_year = ", start_year), ""),
ifelse(end_year != max(flow_data_unfiltered$WaterYear),
paste0(", end_year = ", end_year), ""))
fn_exclude <- paste0(ifelse(!is.null(exclude_years),
paste0(", exclude_years = ", ifelse(length(exclude_years) == 1,
paste(exclude_years),
paste0("c(",paste(exclude_years, collapse = ","),")"))),
""))
fn_months <- ifelse(all(1:12 %in% months),
paste(""),
paste0(", months = ", conseq_fn(months)))
fn_missing <- ifelse(ignore_missing, paste0(", ignore_missing = TRUE"), "")
fn_allowmiss_ann <- ifelse(allowed_missing_annual > 0 , paste0(", allowed_missing = ", allowed_missing_annual), "")
fn_allowmiss_mon <- ifelse(allowed_missing_monthly > 0 , paste0(", allowed_missing = ", allowed_missing_monthly), "")
fn_allowmiss_ann_trend <- ifelse(allowed_missing_annual > 0 , paste0(", allowed_missing_annual = ", allowed_missing_annual), "")
fn_allowmiss_mon_trend <- ifelse(allowed_missing_monthly > 0 , paste0(", allowed_missing_monthly = ", allowed_missing_monthly), "")
fn_complete <- ifelse(complete_years, paste0(", complete_years = TRUE"), "")
fn_zyp <- paste0(", zyp_method = '", zyp_method, "'",
", zyp_alpha = ", ifelse(!is.na(zyp_alpha), zyp_alpha, "NA"))
# Create fasstr function strings for output
analysis_function <- paste0("compute_full_analysis(",
fn_data,
fn_area,
fn_wys,
fn_startend,
fn_exclude,
fn_months,
fn_missing,
fn_complete,
ifelse(6 %in% analyses, fn_zyp, ""),
ifelse(all(1:7 %in% analyses),
paste(", analyses = c(1:7)"),
paste0(", analyses = ", conseq_fn(analyses))),
")")
# Add fasstr functions to table
fasstr_functions <- dplyr::add_row(fasstr_functions,
"Worksheet" = paste0(file_name,".xlsx"),
"Output" = "Full Analysis Function",
"Function" = analysis_function)
### Excel workbook Setup
##########################
# Create the excel document and first worksheet
output_excel <- openxlsx::createWorkbook()
overview_sheet <- "Analysis Overview"
openxlsx::addWorksheet(wb = output_excel,
sheetName = overview_sheet,
tabColour = "#003e1f") # 73fbd3 44e5e7 59d2fe 4a8fe7 5c7aff
# Add raw data to first worksheet
rawdata_sheet <- "Data Input"
openxlsx::addWorksheet(wb = output_excel,
sheetName = rawdata_sheet,
tabColour = "#003e1f")
add_table(wb = output_excel,
sheet = rawdata_sheet,
data = flow_data_source,
title = "Source Daily Data",
col = 1,
row = 1,
comment = NA)
openxlsx::setColWidths(wb = output_excel, sheet = rawdata_sheet,
cols = seq_len(ncol(flow_data_source)), widths = 11)
## Screening
##########################
if (1 %in% analyses) {
# Add worksheet
timeseries_sheet <- "Data Timeseries"
openxlsx::addWorksheet(wb = output_excel,
sheetName = timeseries_sheet,
tabColour = "#73ba9b")
# Create fasstr function strings for output
data_function <- paste0("add_date_variables(",
fn_data,
fn_wys,
") %>% add_rolling_means() %>% add_basin_area(",
paste0(ifelse(!is.na(basin_area),
paste0("basin_area = ", basin_area),
"")),
")")
data_plot_function <- paste0("plot_flow_data(",
fn_data,
fn_wys,
fn_exclude,
")")
# Add data tables
flow_data_out <- flow_data_filtered[,!colnames(flow_data_filtered) %in% "STATION_NUMBER"]
add_table(wb = output_excel,
sheet = timeseries_sheet,
data = flow_data_out,
title = paste0("Daily Flows, Dates, Rolling Means (cubic metres per second), and Basin Area (",
start_year, "-", end_year, ")"),
col = 1,
row = 1,
comment = data_function)
openxlsx::setColWidths(wb = output_excel, sheet = timeseries_sheet,
cols = seq_len(ncol(flow_data_source)), widths = 12)
# Add plots
add_plot(wb = output_excel,
sheet = timeseries_sheet,
plot = results$Screening$Daily_Flows_Plot[[1]],
title = paste0("Daily Flows (", start_year, "-", end_year, ")"),
col = ncol(flow_data_out) + 2,
row = 2,
height = 5,
width = 20,
comment = data_plot_function)
# Add worksheet
screening_sheet <- "Data Screening"
openxlsx::addWorksheet(wb = output_excel,
sheetName = screening_sheet,
tabColour = "#73ba9b")
# Create fasstr function strings for output
screening_function <- paste0("screen_flow_data(",
fn_data,
fn_wys,
fn_startend,
fn_months,
")")
screeningplot_function <- paste0("plot_data_screening(",
fn_data,
fn_wys,
fn_startend,
fn_months,
")")
missingingplot_function <- paste0("plot_missing_dates(",
fn_data,
fn_wys,
fn_startend,
fn_months,
")")
# Add data tables
flow_screening_out <- results$Screening$Flow_Screening
flow_screening_out <- flow_screening_out[,!colnames(flow_screening_out) %in% "STATION_NUMBER"]
add_table(wb = output_excel,
sheet = screening_sheet,
data = flow_screening_out,
title = paste0("Data Screening: Annual Summary Statistics (cubic metres per second) and Data Availability (",
start_year, "-", end_year, ")"),
col = 1,
row = 1,
comment = screening_function)
# Add plots
add_plot(wb = output_excel,
sheet = screening_sheet,
plot = results$Screening$Flow_Screening_Plot[[1]],
title = paste0("Annual Summary Statistics (", start_year, "-", end_year, ")"),
col = ncol(flow_screening_out) + 2,
row = 2,
height = 5,
width = 8.5,
comment = screeningplot_function)
add_plot(wb = output_excel,
sheet = screening_sheet,
plot = results$Screening$Missing_Dates_Plot[[1]],
title = paste0("Number of Missing Dates Per Month (", start_year, "-", end_year, ")"),
col = ncol(flow_screening_out) + 2,
row = 29,
height = 4,
width = 8.5,
comment = missingingplot_function)
# Add fasstr functions to table
fasstr_functions <- dplyr::add_row(fasstr_functions,
"Worksheet" = timeseries_sheet,
"Output" = "Daily Data Timeseries Table",
"Function" = data_function)
fasstr_functions <- dplyr::add_row(fasstr_functions,
"Worksheet" = timeseries_sheet,
"Output" = "Daily Data Timeseries Plot",
"Function" = data_plot_function)
fasstr_functions <- dplyr::add_row(fasstr_functions,
"Worksheet" = screening_sheet,
"Output" = "Data Screening Table",
"Function" = screening_function)
fasstr_functions <- dplyr::add_row(fasstr_functions,
"Worksheet" = screening_sheet,
"Output" = "Annual Statistics for Screening Plot",
"Function" = screeningplot_function)
fasstr_functions <- dplyr::add_row(fasstr_functions,
"Worksheet" = screening_sheet,
"Output" = "Missing Data Plot",
"Function" = missingingplot_function)
}
### Long-term Stats
##########################
if (2 %in% analyses) {
# Add worksheet
lt_sheet <- "Long-term Daily Stats"
openxlsx::addWorksheet(wb = output_excel,
sheetName = lt_sheet,
tabColour = "#73fbd3")
lt_mon_sheet <- "Long-term Monthly Stats"
openxlsx::addWorksheet(wb = output_excel,
sheetName = lt_mon_sheet,
tabColour = "#73fbd3")
# Create fasstr function strings for output
longterm_mon_function <- paste0("calc_longterm_monthly_stats(",
fn_data,
fn_wys,
fn_startend,
fn_exclude,
fn_months,
fn_missing,
fn_complete,
")")
longtermplot_mon_function <- paste0("plot_longterm_monthly_stats(",
fn_data,
fn_wys,
fn_startend,
fn_exclude,
fn_months,
fn_missing,
fn_complete,
")")
longterm_function <- paste0("calc_longterm_daily_stats(",
fn_data,
fn_wys,
fn_startend,
fn_exclude,
fn_months,
fn_missing,
fn_complete,
")")
longtermplot_function <- paste0("plot_longterm_daily_stats(",
fn_data,
fn_wys,
fn_startend,
fn_exclude,
fn_months,
fn_missing,
fn_complete,
")")
durationplot_function <- paste0("plot_flow_duration(",
fn_data,
fn_wys,
fn_startend,
fn_exclude,
fn_months,
fn_missing,
fn_complete,
")")
monthmeanplot_function <- paste0("plot_monthly_means(",
fn_data,
fn_wys,
fn_startend,
fn_exclude,
fn_months,
fn_missing,
fn_complete,
")")
# Add data tables
lt_stats_out <- results$Longterm$Longterm_Daily_Summary_Stats_Percentiles
lt_stats_out <- lt_stats_out[,!colnames(lt_stats_out) %in% "STATION_NUMBER"]
add_table(wb = output_excel,
sheet = lt_sheet,
data = lt_stats_out,
title = paste0("Long-term Summary Statistics of Daily Mean Flows, in cubic metres per second (",
start_year, "-", end_year, ")"),
col = 1,
row = 1,
comment = longterm_function)
# Add plots
add_plot(wb = output_excel,
sheet = lt_sheet,
plot = results$Longterm$Longterm_Daily_Summary_Stats_Plot[[1]],
title = paste0("Long-term Summary Statistics of Daily Mean Flows, in cubic metres per second (",
start_year, "-", end_year, ")"),
col = ncol(lt_stats_out) + 2,
row = 2,
height = 4,
width = 10,
comment = longtermplot_function)
add_plot(wb = output_excel,
sheet = lt_sheet,
plot = results$Longterm$Flow_Duration_Curves[[1]],
title = paste0("Flow Duration Curves (", start_year, "-", end_year, ")"),
col = ncol(lt_stats_out) + 2,
row = 24,
height = 5,
width = 10,
comment = durationplot_function)
add_plot(wb = output_excel,
sheet = lt_sheet,
plot = results$Longterm$Longterm_Monthly_Means_Plot[[1]],
title = paste0("Long-term Monthly Means (", start_year, "-", end_year, ")"),
col = ncol(lt_stats_out) + 2,
row = 50,
height = 4,
width = 10,
comment = durationplot_function)
# Add data tables
lt_mon_stats_out <- results$Longterm$Longterm_Monthly_Summary_Stats_Percentiles
lt_mon_stats_out <- lt_mon_stats_out[,!colnames(lt_mon_stats_out) %in% "STATION_NUMBER"]
add_table(wb = output_excel,
sheet = lt_mon_sheet,
data = lt_mon_stats_out,
title = paste0("Long-term Summary Statistics of Monthly Mean Flows (", start_year, "-", end_year, ")"),
col = 1,
row = 1,
comment = longterm_function)
# Add plots
add_plot(wb = output_excel,
sheet = lt_mon_sheet,
plot = results$Longterm$Longterm_Monthly_Summary_Stats_Plot[[1]],
title = paste0("Long-term Summary Statistics of Monthly Mean Flows (", start_year, "-", end_year, ")"),
col = ncol(lt_mon_stats_out) + 2,
row = 2,
height = 4,
width = 10,
comment = longtermplot_function)
# Add fasstr functions to table
fasstr_functions <- dplyr::add_row(fasstr_functions,
"Worksheet" = lt_mon_sheet,
"Output" = "Long-term Monthly Summary Statistics Table",
"Function" = longterm_mon_function)
fasstr_functions <- dplyr::add_row(fasstr_functions,
"Worksheet" = lt_mon_sheet,
"Output" = "Long-term Monthly Summary Statistics Plot",
"Function" = longtermplot_mon_function)
fasstr_functions <- dplyr::add_row(fasstr_functions,
"Worksheet" = lt_sheet,
"Output" = "Long-term Daily Summary Statistics Table",
"Function" = longterm_function)
fasstr_functions <- dplyr::add_row(fasstr_functions,
"Worksheet" = lt_sheet,
"Output" = "Long-term Daily Summary Statistics Plot",
"Function" = longtermplot_function)
fasstr_functions <- dplyr::add_row(fasstr_functions,
"Worksheet" = lt_sheet,
"Output" = "Flow Duration Curves",
"Function" = durationplot_function)
}
### Annual Stats
##########################
if (3 %in% analyses) {
# Add worksheet
ann_stat_sheet <- "Annual Stats"
openxlsx::addWorksheet(wb = output_excel,
sheetName = ann_stat_sheet,
tabColour = "#44e5e7")
# Create fasstr function strings for output
annual_stats_function <- paste0("calc_annual_stats(",
fn_data,
fn_wys,
fn_startend,
fn_exclude,
fn_months,
fn_missing,
fn_allowmiss_ann,
fn_complete,
")")
annual_plot_function <- paste0("plot_annual_stats(",
fn_data,
fn_wys,
fn_startend,
fn_exclude,
fn_months,
fn_missing,
fn_allowmiss_ann,
fn_complete,
")")
annual_mean_plot_function <- paste0("plot_annual_means(",
fn_data,
fn_wys,
fn_startend,
fn_exclude,
fn_months,
fn_missing,
fn_allowmiss_ann,
fn_complete,
")")
# Add data tables
ann_stats_out <- results$Annual$Annual_Summary_Stats[,!colnames(results$Annual$Annual_Summary_Stats) %in% "STATION_NUMBER"]
add_table(wb = output_excel,
sheet = ann_stat_sheet,
data = ann_stats_out,
title = paste0("Annual Summary Statistics, in cubic metres per second (", start_year, "-", end_year, ")"),
col = 1,
row = 1,
comment = annual_stats_function)
# Add plots
add_plot(wb = output_excel,
sheet = ann_stat_sheet,
plot = results$Annual$Annual_Summary_Stats_Plot[[1]],
title = paste0("Annual Summary Statistics (", start_year, "-", end_year, ")"),
col = ncol(ann_stats_out) + 2,
row = 2,
height = 3,
width = 8.5,
comment = annual_plot_function)
add_plot(wb = output_excel,
sheet = ann_stat_sheet,
plot = results$Annual$Annual_Means_Plot[[1]],
title = paste0("Annual Mean Flows In Relation to the Long-term Mean (", start_year, "-", end_year, ")"),
col = ncol(ann_stats_out) + 2,
row = 19,
height = 3,
width = 8.5,
comment = annual_mean_plot_function)
fasstr_functions <- dplyr::add_row(fasstr_functions,
"Worksheet" = ann_stat_sheet,
"Output" = "Annual Summary Statistics Table",
"Function" = annual_stats_function)
fasstr_functions <- dplyr::add_row(fasstr_functions,
"Worksheet" = ann_stat_sheet,
"Output" = "Annual Summary Statistics Plot",
"Function" = annual_plot_function)
fasstr_functions <- dplyr::add_row(fasstr_functions,
"Worksheet" = ann_stat_sheet,
"Output" = "Annual Mean Flows Plot",
"Function" = annual_mean_plot_function)
# Add worksheet
ann_cumul_sheet <- "Annual Cumulative Stats"
openxlsx::addWorksheet(wb = output_excel,
sheetName = ann_cumul_sheet,
tabColour = "#44e5e7")
# Create fasstr function strings for output
annual_vol_function <- paste0("calc_annual_cumulative_stats(",
fn_data,
fn_wys,
fn_startend,
fn_exclude,
fn_complete,
fn_months,
ifelse(all(1:12 %in% months),", include_seasons = TRUE)",")"))
annual_yield_function <- paste0("calc_annual_cumulative_stats(",
fn_data,
fn_wys,
fn_startend,
fn_exclude,
fn_complete,
fn_months,
", use_yield = TRUE",
fn_area,
ifelse(all(1:12 %in% months),", include_seasons = TRUE)",")"))
annual_vol_plot_function <- paste0("plot_annual_cumulative_stats(",
fn_data,
fn_wys,
fn_startend,
fn_exclude,
fn_complete,
fn_months,
ifelse(all(1:12 %in% months),", include_seasons = TRUE)",")"))
annual_yield_plot_function <- paste0("plot_annual_cumulative_stats(",
fn_data,
fn_wys,
fn_startend,
fn_exclude,
fn_complete,
fn_months,
", use_yield = TRUE",
fn_area,
ifelse(all(1:12 %in% months),", include_seasons = TRUE)",")"))
# Add data tables
ann_cumul_out <- dplyr::left_join(results$Annual$Annual_Cumul_Volume_Stats_m3,
results$Annual$Annual_Cumul_Yield_Stats_mm,
by = c("STATION_NUMBER", "Year"))
ann_cumul_out <- ann_cumul_out[,!colnames(ann_cumul_out) %in% "STATION_NUMBER"]
add_table(wb = output_excel,
sheet = ann_cumul_sheet,
data = ann_cumul_out,
title = paste0("Annual Cumulative Volumetric (cubic metres) and Yield (millimetres) Summary Statistics (",
start_year, "-", end_year, ")"),
col = 1,
row = 1,
comment = paste0(annual_vol_function, " ", annual_yield_function))
# Add plots
add_plot(wb = output_excel,
sheet = ann_cumul_sheet,
plot = results$Annual$Annual_Cumul_Volume_Stats_m3_Plot[[1]],
title = paste0("Annual Total Volumetric Flows (", start_year, "-", end_year, ")"),
col = ncol(ann_cumul_out) + 2,
row = 2,
height = 2.2,
width = 6,
comment = annual_vol_plot_function)
add_plot(wb = output_excel,
sheet = ann_cumul_sheet,
plot = results$Annual$Annual_Cumul_Yield_Stats_mm_Plot[[1]],
title = paste0("Annual Total Yield Runoff (", start_year, "-", end_year, ")"),
col = ncol(ann_cumul_out) + 2 + ifelse(all(1:12 %in% months),8,0),
row = ifelse(all(1:12 %in% months),2,14),
height = 2.2,
width = 6,
comment = annual_yield_plot_function)
if (all(1:12 %in% months)) {
add_plot(wb = output_excel,
sheet = ann_cumul_sheet,
plot = results$Annual$Annual_Cumul_Volume_Stats_m3_Plot[[2]],
title = paste0("Seasonal (Two Seasons) Total Volumetric Flows (", start_year, "-", end_year, ")"),
col = ncol(ann_cumul_out) + 2,
row = 14,
height = 3,
width = 6,
comment = annual_vol_plot_function)
add_plot(wb = output_excel,
sheet = ann_cumul_sheet,
plot = results$Annual$Annual_Cumul_Volume_Stats_m3_Plot[[3]],
title = paste0("Seasonal (Four Seasons) Total Volumetric Flows (", start_year, "-", end_year, ")"),
col = ncol(ann_cumul_out) + 2,
row = 33,
height = 3,
width = 6,
comment = annual_vol_plot_function)
add_plot(wb = output_excel,
sheet = ann_cumul_sheet,
plot = results$Annual$Annual_Cumul_Yield_Stats_mm_Plot[[2]],
title = paste0("Seasonal (Two Seasons) Total Yield Runoff (", start_year, "-", end_year, ")"),
col = ncol(ann_cumul_out) + 2 + 8,
row = 14,
height = 3,
width = 6,
comment = annual_yield_plot_function)
add_plot(wb = output_excel,
sheet = ann_cumul_sheet,
plot = results$Annual$Annual_Cumul_Yield_Stats_mm_Plot[[3]],
title = paste0("Seasonal (Four Seasons) Total Yield Runoff (", start_year, "-", end_year, ")"),
col = ncol(ann_cumul_out) + 2 + 8,
row = 33,
height = 3,
width = 6,
comment = annual_yield_plot_function)
}
# Add worksheet
ann_oth_sheet <- "Annual Stats Other"
openxlsx::addWorksheet(wb = output_excel,
sheetName = ann_oth_sheet,
tabColour = "#44e5e7")
# Create fasstr function strings for output
annual_lows_function <- paste0("calc_annual_lowflows(",
fn_data,
fn_wys,
fn_startend,
fn_exclude,
fn_months,
fn_missing,
fn_allowmiss_ann,
fn_complete,
")")
annual_lows_plot_function <- paste0("plot_annual_lowflows(",
fn_data,
fn_wys,
fn_startend,
fn_exclude,
fn_months,
fn_missing,
fn_allowmiss_ann,
fn_complete,
")")
annual_timing_function <- paste0("calc_annual_flow_timing(",
fn_data,
fn_wys,
fn_startend,
fn_exclude,
fn_complete,
fn_months,
")")
annual_timing_plot_function <- paste0("plot_annual_flow_timing(",
fn_data,
fn_wys,
fn_startend,
fn_exclude,
fn_complete,
fn_months,
")")
annual_norm_function <- paste0("calc_annual_normal_days(",
fn_data,
fn_wys,
fn_startend,
fn_exclude,
fn_complete,
fn_months,
")")
annual_norm_plot_function <- paste0("plot_annual_normal_days(",
fn_data,
fn_wys,
fn_startend,
fn_exclude,
fn_complete,
fn_months,
")")
# Add data tables
ann_other_out <- dplyr::left_join(results$Annual$Annual_Low_Flows,
results$Annual$Annual_Flow_Timing,
by = c("STATION_NUMBER", "Year"))
ann_other_out <- dplyr::left_join(ann_other_out,
results$Annual$Annual_Normal_Days,
by = c("STATION_NUMBER", "Year"))
ann_other_out <- ann_other_out[,!colnames(ann_other_out) %in% "STATION_NUMBER"]
add_table(wb = output_excel,
sheet = ann_oth_sheet,
data = ann_other_out,
title = paste0("Annual Low-flow Values (cubic metres per second) and Dates, Timing of Flows",
" (day of year), and Number Days Normal and Above/Below Normal (", start_year, "-", end_year, ")"),
col = 1,
row = 1,
comment = paste0(annual_lows_function, " ", annual_timing_function,
" ", annual_norm_function))
# Add plots
add_plot(wb = output_excel,
sheet = ann_oth_sheet,
plot = results$Annual$Annual_Low_Flows_Plot[[1]],
title = paste0("Annual Low-flows (", start_year, "-", end_year, ")"),
col = ncol(ann_other_out) + 2,
row = 2,
height = 5.5,
width = 6,
comment = annual_lows_plot_function)
add_plot(wb = output_excel,
sheet = ann_oth_sheet,
plot = results$Annual$Annual_Low_Flows_Plot[[2]],
title = paste0("Day of Annual Low-flows (", start_year, "-", end_year, ")"),
col = ncol(ann_other_out) + 2,
row = 31,
height = 5.5,
width = 6,
comment = annual_lows_plot_function)
add_plot(wb = output_excel,
sheet = ann_oth_sheet,
plot = results$Annual$Annual_Flow_Timing_Plot[[1]],
title = paste0("Annual Timing of Flows (", start_year, "-", end_year, ")"),
col = ncol(ann_other_out) + 2 + 8,
row = 2,
height = 5.5,
width = 6,