-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathplotting.R
2363 lines (1963 loc) · 123 KB
/
plotting.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
# Plots key figures for the superwell manuscript
#
# Hassan Niazi, June 2023
# load libraries ----
{
library(tidyverse)
library(sf)
library(ggplot2)
# library(tmap)
library(RColorBrewer)
library(psych)
}
# load data ----
{
df_in <- read_csv("inputs/inputs.csv")
mappings_all <- read_csv("processing/basin_country_region_mapping.csv")
sf_in <- st_read("inputs/shapefiles/inputs.shp") %>% st_make_valid()
colnames(sf_in) <- c(colnames(df_in), "geometry") # correct col names in sf
df_in_R <- read_csv("inputs/inputs_recharge.csv")
sf_in_R <- st_read("inputs/shapefiles/inputs_recharge.shp") %>% st_make_valid()
colnames(sf_in_R) <- c(colnames(df_in_R), "geometry")
figs_dir <- "processing/figures/notimelimit/"
out_dir <- "outputs/superwell_py_deep_all_"
# main scenario, to load lesser rows n_max = 1000
df_0.3PD_0.25DL <- read_csv(paste0(out_dir, '0.3PD_0.25DL.csv')) %>% rename("GridCellID" = "grid_id")
print(paste0("Number of Grid Cells Processed: ", length(unique(df_0.3PD_0.25DL$GridCellID)),
" out of ", length(unique(df_in$GridCellID)),
" (", round(length(unique(df_0.3PD_0.25DL$GridCellID))*100/length(unique(df_in$GridCellID)), 1), "%)"))
# base plot format
my_theme <- function () {
theme_bw() +
theme(
strip.background = element_blank(),
strip.text = element_text(face = "bold"),
# axis.title = element_text(face="bold"),
legend.position = c(0.91, 0.12),
# legend.direction = "horizontal",
legend.background = element_blank(),
plot.title = element_text(hjust = 0.5),
panel.grid.major = element_blank(),
plot.tag = element_text(),
plot.tag.position = c(0.01 , 0.99),
plot.margin = margin(t = 1, # Top margin
r = 0, # Right margin
b = 0, # Bottom margin
l = 3) # Left margin
)
}
saveformat <- ".png"
}
# read in all scenarios ----
# only read in for cost curves and volume statistics
{
df_0.3PD_0.05DL <- read_csv(paste0(out_dir, '0.3PD_0.05DL.csv')) %>% rename("GridCellID" = "grid_id")
df_0.3PD_0.4DL <- read_csv(paste0(out_dir, '0.3PD_0.4DL.csv')) %>% rename("GridCellID" = "grid_id")
df_0.6PD_0.05DL <- read_csv(paste0(out_dir, '0.6PD_0.05DL.csv')) %>% rename("GridCellID" = "grid_id")
df_0.6PD_0.25DL <- read_csv(paste0(out_dir, '0.6PD_0.25DL.csv')) %>% rename("GridCellID" = "grid_id")
df_0.6PD_0.4DL <- read_csv(paste0(out_dir, '0.6PD_0.4DL.csv')) %>% rename("GridCellID" = "grid_id")
}
# make a list of all scenarios
# scen_list <- c("0.3PD_0.05DL", "0.3PD_0.25DL", "0.3PD_0.4DL", "0.6PD_0.05DL", "0.6PD_0.25DL", "0.6PD_0.4DL")
# scen_names_list <- c("0.3PD_0.05DL", "0.3PD_0.25DL", "0.3PD_0.4DL", "0.6PD_0.05DL", "0.6PD_0.25DL", "0.6PD_0.4DL")
scen_names_list_labels <- c("0.3PD_0.05DL", "0.3PD_0.25DL", "0.3PD_0.4DL", "0.6PD_0.05DL", "0.6PD_0.25DL", "0.6PD_0.4DL")
# scen_list <- list("0.3PD_0.05DL" = df_0.3PD_0.05DL,
# "0.3PD_0.25DL" = df_0.3PD_0.25DL,
# "0.3PD_0.4DL" = df_0.3PD_0.4DL,
# "0.6PD_0.05DL" = df_0.6PD_0.05DL,
# "0.6PD_0.25DL" = df_0.6PD_0.25DL,
# "0.6PD_0.4DL" = df_0.6PD_0.4DL)
# output data ----
df_0.3PD_0.25DL %>% left_join(select(sf_in, c("GridCellID", "geometry")), by = "GridCellID") -> df_out
# clean data
# # determine unique combinations of country and GridCellID in df and in df_in
# df %>% as.data.frame() %>% select(COUNTRY, OriginalOb) %>% unique() -> df_unique
# df_in %>% select(CNTRY_NAME, OBJECTID) %>% unique() -> df_in_unique
# # determine the difference in both unique dataframes df and df_in
# df_unique %>% anti_join(df_in_unique, by = c("COUNTRY" = "CNTRY_NAME", "OriginalOb" = "OBJECTID")) -> df_diff
# high-level analysis on volume ----
df_in %>% mutate(available_volume_allcells = Porosity * Grid_area * (Aquifer_thickness - Depth_to_water),
ponded_depth_avail = Porosity * (Aquifer_thickness - Depth_to_water)) -> df_in_vol
df_in %>% mutate(Aquifer_thickness = case_when(Aquifer_thickness > 1000 ~ 1000,
TRUE ~ Aquifer_thickness),
available_volume_allcells = Porosity * Grid_area * (Aquifer_thickness - Depth_to_water),
ponded_depth_avail = Porosity * (Aquifer_thickness - Depth_to_water)) -> df_in_vol_screened
# start analysis
#
# plot(df_in_vol$available_volume_allcells, df_in_vol$ponded_depth_avail)
{
# global available volume
print(paste0("Global Available Volume from inputs = ",
round(sum(unique(df_in_vol$available_volume_allcells)) * 1e-15, 3), " million km3"))
print(paste0("Global Available Volume from screened inputs = ",
round(sum(unique(df_in_vol_screened$available_volume_allcells)) * 1e-15, 3), " million km3"))
print(paste0("Global Accessible Volume in 0.3PD 0.05DL = ",
round(sum(unique(df_0.3PD_0.05DL$available_volume)) * 1e-15, 3), " million km3"))
print(paste0("Global Accessible Volume in 0.3PD 0.25DL = ",
round(sum(unique(df_0.3PD_0.25DL$available_volume)) * 1e-15, 3), " million km3"))
print(paste0("Global Accessible Volume in 0.3PD 0.40DL = ",
round(sum(unique(df_0.3PD_0.4DL$available_volume)) * 1e-15, 3), " million km3"))
print(paste0("Global Accessible Volume in 0.6PD 0.05DL = ",
round(sum(unique(df_0.6PD_0.05DL$available_volume)) * 1e-15, 3), " million km3"))
print(paste0("Global Accessible Volume in 0.6PD 0.25DL = ",
round(sum(unique(df_0.6PD_0.25DL$available_volume)) * 1e-15, 3), " million km3"))
print(paste0("Global Accessible Volume in 0.6PD 0.40DL = ",
round(sum(unique(df_0.6PD_0.4DL$available_volume)) * 1e-15, 3), " million km3"))
# global volume produced
print(paste0("Global Produced Volume in 0.3PD 0.05DL = ",
round(sum(df_0.3PD_0.05DL$volume_produced_allwells) * 1e-15, 4), " million km3"))
print(paste0("Global Produced Volume in 0.3PD 0.25DL = ",
round(sum(df_0.3PD_0.25DL$volume_produced_allwells) * 1e-15, 4), " million km3"))
print(paste0("Global Produced Volume in 0.3PD 0.40DL = ",
round(sum(df_0.3PD_0.4DL$volume_produced_allwells) * 1e-15, 4), " million km3"))
print(paste0("Global Produced Volume in 0.6PD 0.05DL = ",
round(sum(df_0.6PD_0.05DL$volume_produced_allwells) * 1e-15, 4), " million km3"))
print(paste0("Global Produced Volume in 0.6PD 0.25DL = ",
round(sum(df_0.6PD_0.25DL$volume_produced_allwells) * 1e-15, 4), " million km3"))
print(paste0("Global Produced Volume in 0.6PD 0.40DL = ",
round(sum(df_0.6PD_0.4DL$volume_produced_allwells) * 1e-15, 4), " million km3"))
}
## summary stats of available, accessible and pumped volumes ----
# since there are duplicate available vol values, we need to group by grid cell before unique
length(unique(df_in_vol$GridCellID))
length(unique(df_in_vol$available_volume_allcells))
global_avail <- df_in_vol %>% select(GridCellID, available_volume_allcells) %>% group_by(GridCellID) %>% unique() %>% ungroup() %>%
summarise(available = sum(available_volume_allcells) * 1e-15)
# define get function to get the data frame from the environment
get <- function(x) eval(as.name(x), envir = .GlobalEnv)
# function to calculate accessible and pumped volumes
calculate_stats <- function(scen_list) {
vols <- data.frame(scenario = character(),
accessible = numeric(),
pumped = numeric()
)
scen_names_list <- scen_list
for (scenario in scen_names_list) {
# get the data frame loaded in the environment if it is equal to scenario name
df <- get(paste0("df_", scenario))
# length(unique(df$available_volume)) - nrow(df %>% select(GridCellID, available_volume) %>% group_by(GridCellID) %>% unique())
# accessible <- sum(unique(df$available_volume)) * 1e-15 # assuming every grid cell has a different accessible volume
accessible <- as.numeric(df %>% select(GridCellID, available_volume) %>% group_by(GridCellID) %>% unique() %>% ungroup() %>%
summarise(available = sum(available_volume) * 1e-15))
# length(unique(df_0.6PD_0.4DL$GridCellID)) - length(unique(df_0.6PD_0.4DL$volume_produced_allwells))
pumped <- sum(df$volume_produced_allwells) * 1e-15
# pumped <- df %>% select(GridCellID, cumulative_vol_produced_allwells) %>% group_by(GridCellID) %>% filter(cumulative_vol_produced_allwells == max(cumulative_vol_produced_allwells)) %>% ungroup() %>% summarise(pumped = sum(cumulative_vol_produced_allwells) * 1e-12)
vols <- as.data.frame(rbind(vols, data.frame(scenario, accessible, pumped)))
}
return(vols)
}
# Getting fractions
fractions <- calculate_stats(scen_names_list_labels) %>%
mutate(available = global_avail$available,
accessible_perc = (accessible*100)/available,
pumped_perc = (pumped*100)/available,
pumped_accessible_perc = (pumped*100)/accessible)
print(fractions)
# write fraction as a csv
write.csv(fractions, "outputs/fractions.csv", row.names = FALSE)
# mean and std dev across scenarios
sapply(fractions[, -1], function(x) c(mean = mean(x), sd = sd(x)))
# timeseries by scenario ----
# some tests
df_0.3PD_0.05DL %>% select(year_number, volume_produced_allwells, total_cost_allwells, unit_cost) %>%
mutate(unit_cost_calc = total_cost_allwells/volume_produced_allwells,
diff_uc = unit_cost_calc - unit_cost) -> a
a %>% group_by(year_number) %>%
summarise(global_vol_prod = sum(volume_produced_allwells),
global_total_cost = sum(total_cost_allwells),
global_unit_cost = mean(unit_cost)) %>%
mutate(global_unit_cost_calc = global_total_cost/global_vol_prod,
diff_uc_global = global_unit_cost_calc - global_unit_cost) -> b
plot(b$year_number, b$global_vol_prod, type = "l", xlab = "Year", ylab = "Global Volume Produced (km3)")
plot(b$year_number, b$global_total_cost, type = "l", xlab = "Year", ylab = "Global Total Cost ($)")
plot(b$year_number, b$global_unit_cost, type = "l", xlab = "Year", ylab = "Global Unit Cost ($/km3)")
plot(b$year_number, b$global_unit_cost_calc, type = "l", xlab = "Year", ylab = "Global Unit Cost ($/km3)")
# plot drawdown vs depth to water to see the difference between water depth decrease at well head and over water table decrease
plot(df_0.3PD_0.05DL$depth_to_water, df_0.3PD_0.05DL$drawdown, xlab = "Depth to Water (m)", ylab = "Drawdown (m)", pch = 20, cex = 0.5)
# global timeseries df by scenario
# function to calculate global timeseries df
calculate_global_timeseries <- function(scen_list) {
global_timeseries <- data.frame()
for (scenario in scen_list) {
# get the data frame loaded in the environment if it is equal to scenario name
df <- get(paste0("df_", scenario))
# calculate global timeseries df
df %>% group_by(year_number) %>%
summarise(global_vol_prod = sum(volume_produced_allwells) * 1e-9,
global_dep_vol_frac_avg = mean(depleted_vol_fraction),
global_aqfr_sat_thickness_avg = mean(aqfr_sat_thickness),
global_transmissivity_avg = mean(transmissivity),
global_depth_to_water_avg = mean(depth_to_water),
global_drawdown_avg = mean(drawdown),
global_total_head_avg = mean(total_head),
global_total_well_length_avg = mean(total_well_length),
global_energy = sum(energy),
global_energy_avg = mean(energy),
global_power = sum(power),
global_power_avg = mean(power),
global_energy_cost = sum(energy_cost),
global_energy_cost_avg = mean(energy_cost),
global_nonenergy_cost = sum(nonenergy_cost),
global_nonenergy_cost_avg = mean(nonenergy_cost),
global_number_of_wells_avg = mean(number_of_wells),
global_well_installation_cost_avg = mean(well_installation_cost),
global_annual_capital_cost_avg = mean(annual_capital_cost),
global_maintenance_cost_avg = mean(maintenance_cost),
global_total_cost_perwell = sum(total_cost_perwell),
global_total_cost_perwell_avg = mean(total_cost_perwell),
global_total_cost = sum(total_cost_allwells),
global_total_cost_avg = mean(total_cost_allwells),
energy2nonenergy_ratio = sum(energy_cost)/sum(nonenergy_cost),
global_unit_cost_avg = mean(unit_cost)) %>%
mutate(scenario = scenario) -> df
global_timeseries <- as.data.frame(rbind(global_timeseries, df))
}
return(global_timeseries)
}
global_timeseries <- calculate_global_timeseries(scen_names_list_labels)
# make sure scenarios are not reordered
global_timeseries$scenario <- factor(global_timeseries$scenario,
levels = c("0.3PD_0.05DL", "0.6PD_0.05DL", "0.3PD_0.25DL",
"0.6PD_0.25DL", "0.3PD_0.4DL", "0.6PD_0.4DL"))
# plot timeseries of all variables in global_timeseries as figure. x = year_number, y = variable, color = scenario
global_timeseries_long <- tidyr::pivot_longer(global_timeseries, cols = -c(year_number, scenario), names_to = "variable", values_to = "value")
ggplot(global_timeseries_long, aes(x = year_number, y = value, color = scenario, linetype = scenario)) +
geom_line(size = 1) +
facet_wrap(~ variable, scales = "free_y") +
labs(title = "Time Series of Global Variables", x = "Year", y = "Value") +
scale_color_brewer(palette = "Paired") +
scale_linetype_manual(values = c("solid", "dashed", "solid", "dashed", "solid", "dashed")) +
my_theme() +
theme(legend.position = c(0.875, 0.06), legend.box = "horizontal")
ggsave(paste0(figs_dir, "timeseries/global_timeseries.png"), width = 16, height = 9, units = "in", dpi = 300)
# plot(global_timeseries$year_number, global_timeseries$energy2nonenergy_ratio, type = "l", xlab = "Year", ylab = "Global Energy to Nonenergy Cost Ratio")
# function to plot timeseries by scenario. Arguments y variable, y label, and legend position
plot_timeseries <- function(y_var, multiplier, y_lab, legend_pos, tag, logbool = F) {
gg <- ggplot(global_timeseries) +
geom_line(aes(x = year_number, y = get(y_var) * multiplier, color = scenario, linetype = scenario), linewidth = 1) +
labs(x = 'Pumping Year', y = y_lab, color = "Scenario", linetype = "Scenario", tag = tag) +
# scale_x_sqrt(breaks = c(1, 5, 10, 25, 50, 100, 300), expand = c(0.01,0.01)) + # Setting breaks at squares and labels at the square roots
# scale_x_log10(expand = c(0, 0.035), breaks = scales::log_breaks(n = 10)) + #-0.12
# annotation_logticks(base = 10, sides = "b") +
scale_color_brewer(palette = "Paired") +
scale_linetype_manual(values = c("solid", "dashed", "solid", "dashed", "solid", "dashed")) +
theme_bw() +
theme(
strip.background = element_blank(),
strip.text = element_text(face = "bold"),
# axis.title = element_text(face="bold"),
legend.position = if (legend_pos == "r") {c(0.875, 0.78)} else {c(0.125, 0.78)}, # c(0.875, 0.78) for right c(0.125, 0.78) for left
# legend.direction = "horizontal",
legend.background = element_blank(),
legend.title = element_text(face = "bold"),
plot.title = element_text(hjust = 0.5),
panel.grid.major = element_blank(),
plot.tag = element_text(),
plot.tag.position = c(0.01 , 0.99),
legend.key.size = unit(0.2, "in"),
legend.text = element_text(size = 8),
legend.key.width = unit(0.4, "in"),
plot.margin = margin(t = 1, # Top margin
r = 0, # Right margin
b = 0, # Bottom margin
l = 3) # Left margin
)
if (logbool == T) {
gg <- gg + scale_x_log10(expand = c(0, 0.035), breaks = scales::log_breaks(n = 10)) + #-0.12
annotation_logticks(base = 10, sides = "b")
}
ggsave(paste0(figs_dir, "timeseries/time_", y_var, "_all_scen.png"), width = 6, height = 4, units = "in", dpi = 300)
return(gg)
}
{ # for SI
# global_vol_prod = sum(volume_produced_allwells) * 1e-9,
# global_dep_vol_frac_avg = mean(depleted_vol_fraction),
#
# global_aqfr_sat_thickness_avg = mean(aqfr_sat_thickness),
# global_transmissivity_avg = mean(transmissivity),
#
# global_depth_to_water_avg = mean(depth_to_water),
# global_drawdown_avg = mean(drawdown),
# global_total_head_avg = mean(total_head),
# global_total_well_length_avg = mean(total_well_length),
#
# global_energy = sum(energy),
# global_energy_avg = mean(energy),
# global_power = sum(power),
# global_power_avg = mean(power),
#
# global_energy_cost = sum(energy_cost),
# global_energy_cost_avg = mean(energy_cost),
#
# global_number_of_wells_avg = mean(number_of_wells),
# global_well_installation_cost_avg = mean(well_installation_cost),
# global_annual_capital_cost_avg = mean(annual_capital_cost),
# global_maintenance_cost_avg = mean(maintenance_cost),
# global_nonenergy_cost = sum(nonenergy_cost),
# global_nonenergy_cost_avg = mean(nonenergy_cost),
#
# global_total_cost_perwell = sum(total_cost_perwell),
# global_total_cost_perwell_avg = mean(total_cost_perwell),
# global_total_cost = sum(total_cost_allwells),
# global_total_cost_avg = mean(total_cost_allwells),
#
# energy2nonenergy_ratio = sum(energy_cost)/sum(nonenergy_cost),
# global_unit_cost_avg = mean(unit_cost)
# volume and hydraulics
plot_timeseries("global_vol_prod", 1e-3, expression('Global Volume Produced (1,000 km'^3*')'), "r", "(a)", T)
plot_timeseries("global_dep_vol_frac_avg", 1, expression('Depleted Volume Fraction (-)'), "l", "(b)", T)
# T = Kb
plot_timeseries("global_aqfr_sat_thickness_avg", 1, expression('Aquifer Saturated Thickness (m)'), "l", "(a)", T)
plot_timeseries("global_transmissivity_avg", 86400, expression('Transmissivity (m'^2*'/day)'), "r", "(b)", T)
# depths
plot_timeseries("global_depth_to_water_avg", 1, expression('Global Mean Depth to Water (m)'), "l", "(a)")
plot_timeseries("global_drawdown_avg", 1, expression('Global Mean Drawdown (m)'), "r", "(b)")
plot_timeseries("global_total_head_avg", 1, expression('Global Mean Total Head (m)'), "l", "(c)")
plot_timeseries("global_total_well_length_avg", 1, expression('Global Mean Total Well Length (m)'), "l", "(d)")
# energy and power
plot_timeseries("global_energy", 1e-12, expression('Global Energy (million GWh)'), "r", "(a)", T) # K to giga, million
plot_timeseries("global_energy_avg", 1e-6, expression('Global Mean Energy (GWh)'), "l", "(b)", T)
plot_timeseries("global_power", 1e-6, expression('Global Power (GW)'), "r", "(c)", T)
plot_timeseries("global_power_avg", 1e-6, expression('Global Mean Power (GW)'), "l", "(d)", T)
# energy and non energy costs
plot_timeseries("global_energy_cost", 1e-12, expression('Global Energy Cost (trillion $)'), "r", "(a)", T)
plot_timeseries("global_energy_cost_avg", 1e-6, expression('Global Mean Energy Cost (million $)'), "l", "(b)", T)
plot_timeseries("global_nonenergy_cost", 1e-12, expression('Global Nonenergy Cost (trillion $)'), "r", "(c)", T)
plot_timeseries("global_nonenergy_cost_avg", 1e-6, expression('Global Mean Nonenergy Cost (million $)'), "l", "(d)", T)
# nonenergy costs components
plot_timeseries("global_number_of_wells_avg", 1e-3, expression('Global Mean Number of Wells (x1000)'), "r", "(a)", F)
plot_timeseries("global_well_installation_cost_avg", 1e-6, expression('Global Mean Well Installation Cost (million $)'), "l", "(b)", F)
plot_timeseries("global_annual_capital_cost_avg", 1e-6, expression('Global Mean Annual Capital Cost (million $)'), "r", "(c)", F)
plot_timeseries("global_maintenance_cost_avg", 1e-6, expression('Global Mean Maintenance Cost (million $)'), "r", "(d)", F)
# total costs
plot_timeseries("global_total_cost_perwell", 1e-6, expression('Global Total Cost per Well (million $)'), "r", "(a)", T)
plot_timeseries("global_total_cost_perwell_avg", 1e-6, expression('Global Mean Total Cost per Well (million $)'), "l", "(b)", T)
plot_timeseries("global_total_cost", 1e-12, expression('Global Total Cost (trillion $)'), "r", "(c)", T)
plot_timeseries("global_total_cost_avg", 1e-6, expression('Global Mean Total Cost (million $)'), "l", "(d)", T)
# cost ratios
plot_timeseries("energy2nonenergy_ratio", 1, expression('Global Energy to Nonenergy Cost Ratio (-)'), "l", "(a)")
plot_timeseries("global_unit_cost_avg", 1, expression('Global Mean Unit Cost ($/m'^3*')'), "l", "(b)")
}
# IGNORE THE FOLLOWING. THE FUNCTION ABOVE IS NOW USED TO GENERATE THE PLOTS
### timeseries volume produced by scenario ----
ggplot(global_timeseries) +
geom_line(aes(x = year_number, y = global_vol_prod * 1e-3, color = scen, linetype = scen), linewidth = 1) +
labs(x = 'Pumping Year', y = expression('Global Volume Produced (1,000 km'^3*')'), color = "Scenario", linetype = "Scenario", tag = "(c)") +
# scale_x_sqrt(breaks = c(1, 5, 10, 25, 50, 100, 300), expand = c(0.01,0.01)) + # Setting breaks at squares and labels at the square roots
scale_x_log10(expand = c(0, 0.035), breaks = scales::log_breaks(n = 10)) + #-0.12
annotation_logticks(base = 10, sides = "b") +
scale_color_brewer(palette = "Paired") +
scale_linetype_manual(values = c("solid", "dashed", "solid", "dashed", "solid", "dashed")) +
theme_bw() +
theme(
strip.background = element_blank(),
strip.text = element_text(face = "bold"),
# axis.title = element_text(face="bold"),
legend.position = c(0.875, 0.78),
# legend.direction = "horizontal",
legend.background = element_blank(),
legend.title = element_text(face = "bold"),
plot.title = element_text(hjust = 0.5),
panel.grid.major = element_blank(),
plot.tag = element_text(),
plot.tag.position = c(0.01 , 0.99),
legend.key.size = unit(0.2, "in"),
legend.text = element_text(size = 8),
legend.key.width = unit(0.4, "in"),
plot.margin = margin(t = 1, # Top margin
r = 0, # Right margin
b = 0, # Bottom margin
l = 3) # Left margin
)
ggsave(paste0(figs_dir, "global_vol_prod_all_scen.png"), width = 6, height = 4, units = "in", dpi = 300)
# cumulative volume produced
ggplot(global_timeseries %>%
group_by(scen) %>% mutate(global_vol_prod_cumsum = cumsum(global_vol_prod) * 1e-6)) +
geom_line(aes(x = year_number, y = global_vol_prod_cumsum, color = scen, linetype = scen), linewidth = 1) +
labs(x = 'Pumping Year', y = expression('Global Cumulative Volume Produced (mln km'^3*')'), color = "Scenario", linetype = "Scenario", tag = "(d)") +
# scale_x_sqrt(breaks = c(1, 5, 10, 25, 50, 100, 300), expand = c(0.01,0.1)) + # Setting breaks at squares and labels at the square roots
scale_x_log10(expand = c(0, 0.035), breaks = scales::log_breaks(n = 10)) + #-0.12
annotation_logticks(base = 10, sides = "b") +
scale_color_brewer(palette = "Paired") +
scale_linetype_manual(values = c("solid", "dashed", "solid", "dashed", "solid", "dashed")) +
theme_bw() +
theme(
strip.background = element_blank(),
strip.text = element_text(face = "bold"),
# axis.title = element_text(face="bold"),
legend.position = c(0.125, 0.78),
# legend.direction = "horizontal",
legend.background = element_blank(),
legend.title = element_text(face = "bold"),
plot.title = element_text(hjust = 0.5),
panel.grid.major = element_blank(),
plot.tag = element_text(),
plot.tag.position = c(0.01 , 0.99),
legend.key.size = unit(0.2, "in"),
legend.text = element_text(size = 8),
legend.key.width = unit(0.4, "in"),
plot.margin = margin(t = 1, # Top margin
r = 0, # Right margin
b = 0, # Bottom margin
l = 3) # Left margin
)
ggsave(paste0(figs_dir, "global_vol_prod_all_scen_cumulative.png"), width = 6, height = 4, units = "in", dpi = 300)
### timeseries energy to nonenergy cost ratio by scenario ----
ggplot(global_timeseries) +
geom_line(aes(x = year_number, y = energy2nonenergy_ratio, color = scen, linetype = scen), linewidth = 1) +
labs(x = 'Pumping Year', y = expression('Global Energy to Nonenergy Cost Ratio)'), color = "Scenario", linetype = "Scenario", tag = "(c)") +
# scale_x_sqrt(breaks = c(1, 5, 10, 25, 50, 100, 300), expand = c(0.01,0.01)) + # Setting breaks at squares and labels at the square roots
scale_x_log10(expand = c(0, 0.035), breaks = scales::log_breaks(n = 10)) + #-0.12
annotation_logticks(base = 10, sides = "b") +
scale_color_brewer(palette = "Paired") +
scale_linetype_manual(values = c("solid", "dashed", "solid", "dashed", "solid", "dashed")) +
theme_bw() +
theme(
strip.background = element_blank(),
strip.text = element_text(face = "bold"),
# axis.title = element_text(face="bold"),
legend.position = c(0.125, 0.78),
# legend.direction = "horizontal",
legend.background = element_blank(),
legend.title = element_text(face = "bold"),
plot.title = element_text(hjust = 0.5),
panel.grid.major = element_blank(),
plot.tag = element_text(),
plot.tag.position = c(0.01 , 0.99),
legend.key.size = unit(0.2, "in"),
legend.text = element_text(size = 8),
legend.key.width = unit(0.4, "in"),
plot.margin = margin(t = 1, # Top margin
r = 0, # Right margin
b = 0, # Bottom margin
l = 3) # Left margin
)
ggsave(paste0(figs_dir, "global_energy2nonenergy_ratio_all_scen.png"), width = 6, height = 4, units = "in", dpi = 300)
### timeseries total cost by scenario ----
ggplot(global_timeseries) +
geom_line(aes(x = year_number, y = global_total_cost * 1e-9, color = scen, linetype = scen), linewidth = 1) +
labs(x = 'Pumping Year', y = expression('Global Total Cost (billion $)'), color = "Scenario", linetype = "Scenario", tag = "(c)") +
# scale_x_sqrt(breaks = c(1, 5, 10, 25, 50, 100, 300), expand = c(0.01,0.01)) + # Setting breaks at squares and labels at the square roots
scale_x_log10(expand = c(0, 0.035), breaks = scales::log_breaks(n = 10)) + #-0.12
annotation_logticks(base = 10, sides = "b") +
scale_color_brewer(palette = "Paired") +
scale_linetype_manual(values = c("solid", "dashed", "solid", "dashed", "solid", "dashed")) +
theme_bw() +
theme(
strip.background = element_blank(),
strip.text = element_text(face = "bold"),
# axis.title = element_text(face="bold"),
legend.position = c(0.875, 0.78),
# legend.direction = "horizontal",
legend.background = element_blank(),
legend.title = element_text(face = "bold"),
plot.title = element_text(hjust = 0.5),
panel.grid.major = element_blank(),
plot.tag = element_text(),
plot.tag.position = c(0.01 , 0.99),
legend.key.size = unit(0.2, "in"),
legend.text = element_text(size = 8),
legend.key.width = unit(0.4, "in"),
plot.margin = margin(t = 1, # Top margin
r = 0, # Right margin
b = 0, # Bottom margin
l = 3) # Left margin
)
ggsave(paste0(figs_dir, "global_total_cost_all_scen.png"), width = 6, height = 4, units = "in", dpi = 300)
# cumulative volume produced
ggplot(global_timeseries %>%
group_by(scen) %>% mutate(global_total_cost_cumsum = cumsum(global_total_cost) * 1e-12)) +
geom_line(aes(x = year_number, y = global_total_cost_cumsum, color = scen, linetype = scen), linewidth = 1) +
labs(x = 'Pumping Year', y = expression('Global Cumulative Total Cost (trillion $)'), color = "Scenario", linetype = "Scenario", tag = "(d)") +
# scale_x_sqrt(breaks = c(1, 5, 10, 25, 50, 100, 300), expand = c(0.01,0.1)) + # Setting breaks at squares and labels at the square roots
scale_x_log10(expand = c(0, 0.035), breaks = scales::log_breaks(n = 10)) + #-0.12
annotation_logticks(base = 10, sides = "b") +
scale_color_brewer(palette = "Paired") +
scale_linetype_manual(values = c("solid", "dashed", "solid", "dashed", "solid", "dashed")) +
theme_bw() +
theme(
strip.background = element_blank(),
strip.text = element_text(face = "bold"),
# axis.title = element_text(face="bold"),
legend.position = c(0.125, 0.78),
# legend.direction = "horizontal",
legend.background = element_blank(),
legend.title = element_text(face = "bold"),
plot.title = element_text(hjust = 0.5),
panel.grid.major = element_blank(),
plot.tag = element_text(),
plot.tag.position = c(0.01 , 0.99),
legend.key.size = unit(0.2, "in"),
legend.text = element_text(size = 8),
legend.key.width = unit(0.4, "in"),
plot.margin = margin(t = 1, # Top margin
r = 0, # Right margin
b = 0, # Bottom margin
l = 3) # Left margin
)
ggsave(paste0(figs_dir, "global_total_cost_all_scen_cumulative.png"), width = 6, height = 4, units = "in", dpi = 300)
### timeseries unit cost ----
ggplot(global_timeseries) +
geom_line(aes(x = year_number, y = global_unit_cost, color = scen, linetype = scen), linewidth = 1) +
labs(x = 'Pumping Year', y = expression('Global Unit Cost ($/m'^3*')'), color = "Scenario", linetype = "Scenario", tag = "(c)") +
# scale_x_sqrt(breaks = c(1, 5, 10, 25, 50, 100, 300), expand = c(0.01,0.01)) + # Setting breaks at squares and labels at the square roots
scale_x_log10(expand = c(0, 0.035), breaks = scales::log_breaks(n = 10)) + #-0.12
annotation_logticks(base = 10, sides = "b") +
scale_color_brewer(palette = "Paired") +
scale_linetype_manual(values = c("solid", "dashed", "solid", "dashed", "solid", "dashed")) +
theme_bw() +
theme(
strip.background = element_blank(),
strip.text = element_text(face = "bold"),
# axis.title = element_text(face="bold"),
# legend.position = c(0.875, 0.78),
legend.position = c(0.125, 0.78),
# legend.direction = "horizontal",
legend.background = element_blank(),
legend.title = element_text(face = "bold"),
plot.title = element_text(hjust = 0.5),
panel.grid.major = element_blank(),
plot.tag = element_text(),
plot.tag.position = c(0.01 , 0.99),
legend.key.size = unit(0.2, "in"),
legend.text = element_text(size = 8),
legend.key.width = unit(0.4, "in"),
plot.margin = margin(t = 1, # Top margin
r = 0, # Right margin
b = 0, # Bottom margin
l = 3) # Left margin
)
ggsave(paste0(figs_dir, "global_unit_cost_all_scen.png"), width = 6, height = 4, units = "in", dpi = 300)
# cumulative unit cost
# this probably doesn't mean anything in reality
ggplot(global_timeseries %>%
group_by(scen) %>% mutate(global_unit_cost_cumsum = cumsum(global_unit_cost))) +
geom_line(aes(x = year_number, y = global_unit_cost_cumsum, color = scen, linetype = scen), linewidth = 1) +
labs(x = 'Pumping Year', y = expression('Global Cumulative Unit Cost ($/m'^3*')'), color = "Scenario", linetype = "Scenario", tag = "(d)") +
# scale_x_sqrt(breaks = c(1, 5, 10, 25, 50, 100, 300), expand = c(0.01,0.1)) + # Setting breaks at squares and labels at the square roots
scale_x_log10(expand = c(0, 0.035), breaks = scales::log_breaks(n = 10)) + #-0.12
annotation_logticks(base = 10, sides = "b") +
scale_color_brewer(palette = "Paired") +
scale_linetype_manual(values = c("solid", "dashed", "solid", "dashed", "solid", "dashed")) +
theme_bw() +
theme(
strip.background = element_blank(),
strip.text = element_text(face = "bold"),
# axis.title = element_text(face="bold"),
legend.position = c(0.125, 0.78),
# legend.direction = "horizontal",
legend.background = element_blank(),
legend.title = element_text(face = "bold"),
plot.title = element_text(hjust = 0.5),
panel.grid.major = element_blank(),
plot.tag = element_text(),
plot.tag.position = c(0.01 , 0.99),
legend.key.size = unit(0.2, "in"),
legend.text = element_text(size = 8),
legend.key.width = unit(0.4, "in"),
plot.margin = margin(t = 1, # Top margin
r = 0, # Right margin
b = 0, # Bottom margin
l = 3) # Left margin
)
ggsave(paste0(figs_dir, "global_unit_cost_all_scen_cumulative.png"), width = 6, height = 4, units = "in", dpi = 300)
# stacked bar charts of costs over time ----
df_0.3PD_0.25DL %>%
select(year_number, number_of_wells, annual_capital_cost, maintenance_cost, energy_cost) %>%
mutate(annual_capital_cost = annual_capital_cost/number_of_wells,
maintenance_cost = maintenance_cost/number_of_wells,
energy_cost = energy_cost/number_of_wells) %>%
group_by(year_number) %>% summarise_all(mean) %>% ungroup() %>% mutate(scen = "0.3PD_0.25DL") %>%
pivot_longer(cols = c(annual_capital_cost, maintenance_cost, energy_cost), names_to = "cost_type", values_to = "cost") %>%
mutate(cost_type = case_when(cost_type == "annual_capital_cost" ~ "Capital",
cost_type == "maintenance_cost" ~ "Maintenance",
cost_type == "energy_cost" ~ "Energy"),
cost_type = factor(cost_type, levels = c("Capital", "Maintenance", "Energy")) %>% fct_rev()) %>%
ggplot() +
geom_bar(aes(fill = cost_type, y = cost, x = year_number), position = "stack", stat = "identity") +
labs(x = 'Pumping Year', y = expression('Global Annual Average Cost per Well ($/well)'), fill = "Cost Type", tag = "(c)") +
scale_fill_manual(values = c("#00b0f0", "#f4aa00", "#c10435")) + my_theme() +
theme(legend.position = c(0.125, 0.85),
legend.title = element_text(face = "bold"))
ggsave(paste0(figs_dir, "global_annual_cost_perwell_0.3PD_0.25DL.png"), width = 6, height = 4, units = "in", dpi = 300)
# one grid cell
df_0.3PD_0.25DL %>% filter(GridCellID == "39689") %>% # 72548 for deepen and added wells, 12422 south africa for 199th year, US 96934, Amu Darya 39689 nice balanced graph, Nile 19565
select(year_number, number_of_wells, annual_capital_cost, maintenance_cost, energy_cost) %>%
mutate(annual_capital_cost = annual_capital_cost/number_of_wells,
maintenance_cost = maintenance_cost/number_of_wells,
energy_cost = energy_cost/number_of_wells) %>%
group_by(year_number) %>% summarise_all(mean) %>% ungroup() %>% mutate(scen = "0.3PD_0.25DL") %>%
pivot_longer(cols = c(annual_capital_cost, maintenance_cost, energy_cost), names_to = "cost_type", values_to = "cost") %>%
mutate(cost_type = case_when(cost_type == "annual_capital_cost" ~ "Capital",
cost_type == "maintenance_cost" ~ "Maintenance",
cost_type == "energy_cost" ~ "Energy"),
cost_type = factor(cost_type, levels = c("Capital", "Maintenance", "Energy")) %>% fct_rev()) %>%
ggplot() +
geom_bar(aes(fill = cost_type, y = cost, x = year_number), position = "stack", stat = "identity") +
labs(x = 'Pumping Year', y = expression('Grid Annual Cost per Well ($/well)'), fill = "Cost Type", tag = "(a)") +
scale_fill_manual(values = c("#00b0f0", "#f4aa00", "#c10435")) + my_theme() +
theme(legend.position = c(0.125, 0.85),
legend.title = element_text(face = "bold"))
ggsave(paste0(figs_dir, "grid_annual_cost_perwell_0.3PD_0.25DL.png"), width = 6, height = 4, units = "in", dpi = 300)
# one grid with deepened and added wells
# select grid cell that has both added wells and deepening
df_0.3PD_0.25DL %>%
# select(GridCellID, well_yield, number_of_wells) %>%
select(GridCellID, year_number, well_yield, number_of_wells) %>%
filter(year_number > 19) %>% select(-year_number) %>%
# group_by(GridCellID) %>%
unique() %>%
group_by(GridCellID) %>% mutate(n = n()) %>% ungroup() %>% filter(n > 1) -> df_addedwells
# why is that the cells which reduce the pumping rate don't go beyond 50 years?
df_0.3PD_0.25DL %>% filter(GridCellID == "82509") %>% # 82509 (seems to be better to use), 72548
select(GridCellID, year_number, number_of_wells, well_yield, total_well_length, depth_to_water, total_head, annual_capital_cost, maintenance_cost, energy_cost, total_cost_allwells) -> df_deepen_addedwells
# write_csv(df_0.3PD_0.25DL %>% filter(GridCellID == "72548"), "df_0.3PD_0.25DL_grid_id_72548.csv")
# one grid cell
# read output file of one grid cell
df_0.3PD_0.25DL_Grid_72548 <- read_csv(paste0(out_dir, '0.3PD_0.25DL_Grid_72548.csv')) %>% rename("GridCellID" = "grid_id")
df_0.3PD_0.25DL_Grid_72548 <- df_0.3PD_0.25DL %>% filter(GridCellID == "72548")
# df_0.3PD_0.25DL
df_0.3PD_0.25DL %>% filter(GridCellID == "72548") %>% # 82509 3 pumping rates, 2450 added wells only, 72548 for deepen and added wells, 12422 south africa for 199th year, US 96934, Amu Darya 39689 nice balanced graph, Nile 19565
#
select(year_number, number_of_wells, annual_capital_cost, maintenance_cost, energy_cost) %>%
mutate(annual_capital_cost = annual_capital_cost/number_of_wells,
maintenance_cost = maintenance_cost/number_of_wells,
energy_cost = energy_cost/number_of_wells) %>%
group_by(year_number) %>% summarise_all(mean) %>% ungroup() %>% mutate(scen = "0.3PD_0.25DL") %>%
pivot_longer(cols = c(annual_capital_cost, maintenance_cost, energy_cost), names_to = "cost_type", values_to = "cost") %>%
mutate(cost_type = case_when(cost_type == "annual_capital_cost" ~ "Capital",
cost_type == "maintenance_cost" ~ "Maintenance",
cost_type == "energy_cost" ~ "Energy"),
cost_type = factor(cost_type, levels = c("Capital", "Maintenance", "Energy")) %>% fct_rev()) %>%
ggplot() +
geom_bar(aes(fill = cost_type, y = cost, x = year_number), position = "stack", stat = "identity") +
labs(x = 'Pumping Year', y = expression('Grid Annual Cost per Well ($/well)'), fill = "Cost Type", tag = "(b)") +
scale_fill_manual(values = c("#00b0f0", "#f4aa00", "#c10435")) + my_theme() +
theme(legend.position = c(0.125, 0.85),
legend.title = element_text(face = "bold"))
# ggsave(paste0(figs_dir, "grid_annual_cost_perwell_0.3PD_0.25DL_addedwells.png"), width = 6, height = 4, units = "in", dpi = 300)
df_0.3PD_0.25DL_Grid_72548 %>% filter(GridCellID == "72548") %>% # 82509 3 pumping rates, 2450 added wells only, 72548 for deepen and added wells, 12422 south africa for 199th year, US 96934, Amu Darya 39689 nice balanced graph, Nile 19565
#
select(year_number, number_of_wells, annual_capital_cost, maintenance_cost, energy_cost) %>%
# mutate(annual_capital_cost = annual_capital_cost/number_of_wells,
# maintenance_cost = maintenance_cost/number_of_wells,
# energy_cost = energy_cost/number_of_wells) %>%
group_by(year_number) %>% summarise_all(mean) %>% ungroup() %>% mutate(scen = "0.3PD_0.25DL") %>%
pivot_longer(cols = c(annual_capital_cost, maintenance_cost, energy_cost), names_to = "cost_type", values_to = "cost") %>%
mutate(cost_type = case_when(cost_type == "annual_capital_cost" ~ "Capital",
cost_type == "maintenance_cost" ~ "Maintenance",
cost_type == "energy_cost" ~ "Energy"),
cost_type = factor(cost_type, levels = c("Capital", "Maintenance", "Energy")) %>% fct_rev()) %>%
ggplot() +
geom_bar(aes(fill = cost_type, y = cost * 1e-6, x = year_number), position = "stack", stat = "identity") +
labs(x = 'Pumping Year', y = expression('Grid Annual Costs (million $)'), fill = "Cost Type", tag = "(d)") +
scale_fill_manual(values = c("#00b0f0", "#f4aa00", "#c10435")) + my_theme() +
theme(legend.position = c(0.125, 0.85),
legend.title = element_text(face = "bold"))
# ggsave(paste0(figs_dir, "grid_annual_cost_0.3PD_0.25DL_addedwells.png"), width = 6, height = 4, units = "in", dpi = 300)
# single well dynamics ----
df_single <- df_0.3PD_0.25DL_Grid_72548
plot_single_well <- function(data, x_var = "year_number", y_var, y_scale = 1, y_label, color = "red", tag = "") {
gg <- ggplot(data, aes_string(x = x_var, y = paste0(y_var, " * ", y_scale))) +
geom_point(color = color) +
labs(x = "Pumping Year", y = y_label, tag = tag) + my_theme()
ggsave(paste0(figs_dir, "singlecell/cell_", y_var, ".png"), plot = gg, width = 6, height = 4, units = "in", dpi = 300)
return(gg)
}
{
# depths
plot_single_well(df_single, "year_number", "total_thickness", 1, expression('Total Aquifer Thickness (m)'), "blue2", "(a)")
plot_single_well(df_single, "year_number", "depth_to_water", 1, expression('Depth to Water (m)'), "red2", "(b)")
plot_single_well(df_single, "year_number", "orig_aqfr_sat_thickness", 1, expression('Original Aquifer Saturated Thickness (m)'), "green3", "(c)")
plot_single_well(df_single, "year_number", "total_well_length", 1, expression('Total Well Length (m)'), "orange", "(d)")
plot_single_well(df_single, "year_number", "aqfr_sat_thickness", 1, expression('Aquifer Saturated Thickness (m)'), "purple", "(e)")
plot_single_well(df_single, "year_number", "total_head", 1, expression('Total Head (m)'), "brown", "(f)")
# T=Kb
plot_single_well(df_single, "year_number", "hydraulic_conductivity", 86400, expression('Hydraulic Conductivity (m/day)'), "red2", "(a)")
plot_single_well(df_single, "year_number", "transmissivity", 86400, expression('Transmissivity (m'^2*'/day)'), "green4", "(b)")
# well hydraulics
plot_single_well(df_single, "year_number", "well_yield", 1, expression('Well Yield (m'^3*'/day)'), "orange2", "(a)")
plot_single_well(df_single, "year_number", "areal_extent", 1, expression('Areal Extent (m'^2*')'), "royalblue2", "(b)")
plot_single_well(df_single, "year_number", "number_of_wells", 1, 'Number of Wells', "red2", "(c)")
plot_single_well(df_single, "year_number", "drawdown", 1, expression('Drawdown (m)'), "green3", "(d)")
# volumes
plot_single_well(df_single, "year_number", "volume_produced_perwell", 1e-6, expression('Volume Produced Per Well (million m'^3*')'), "blue3", "(a)")
plot_single_well(df_single, "year_number", "cumulative_vol_produced_perwell", 1e-6, expression('Cumulative Volume Produced Per Well (million m'^3*')'), "red3", "(b)")
plot_single_well(df_single, "year_number", "volume_produced_allwells", 1e-9, expression('Volume Produced All Wells (billion m'^3*')'), "green3", "(c)")
plot_single_well(df_single, "year_number", "cumulative_vol_produced_allwells", 1e-9, expression('Cumulative Volume Produced All Wells (billion m'^3*')'), "orange", "(d)")
plot_single_well(df_single, "year_number", "available_volume", 1e-9, expression('Available Volume (billion m'^3*')'), "purple", "(e)")
plot_single_well(df_single, "year_number", "depleted_vol_fraction", 1, 'Depleted Volume Fraction', "brown", "(f)")
# power and energy
plot_single_well(df_single, "year_number", "power", 1e-6, expression('Power (GW)'), "olivedrab3", "(a)")
plot_single_well(df_single, "year_number", "energy", 1e-6, expression('Energy (GWh)'), "darkorchid4", "(b)")
# energy cost
plot_single_well(df_single, "year_number", "energy_cost_rate", 1, expression('Electricity Cost Rate (USD/KWh)'), "darkorange", "(a)")
plot_single_well(df_single, "year_number", "energy_cost", 1e-6, expression('Energy Cost (million $)'), "green", "(b)")
# nonenergy cost components
plot_single_well(df_single, "year_number", "well_installation_cost", 1e-6, expression('Well Installation Cost (million $)'), "darkslateblue", "(a)")
plot_single_well(df_single, "year_number", "annual_capital_cost", 1e-6, expression('Annual Capital Cost (million $)'), "cadetblue", "(b)")
plot_single_well(df_single, "year_number", "maintenance_cost", 1e-6, expression('Maintenance Cost (million $)'), "darkmagenta", "(c)")
plot_single_well(df_single, "year_number", "nonenergy_cost", 1e-6, expression('Nonenergy Cost (million $)'), "khaki3", "(d)")
# total costs
plot_single_well(df_single, "year_number", "total_cost_perwell", 1e-6, expression('Total Cost Per Well (million $)'), "mediumpurple", "(a)")
plot_single_well(df_single, "year_number", "total_cost_allwells", 1e-6, expression('Total Cost All Wells (million $)'), "hotpink4", "(b)")
# unit cost
plot_single_well(df_single, "year_number", "unit_cost", 1, expression('Unit Cost (USD/m'^3*')'), "chartreuse3", "(a)")
plot_single_well(df_single, "year_number", "unit_cost_per_acreft", 1, expression('Unit Cost (USD/acre-ft)'), "darkolivegreen4", "(b)")
}
# plot(x = df_0.3PD_0.25DL_Grid_72548$year_number, y = df_0.3PD_0.25DL_Grid_72548$energy_cost * 1e-6, type = "p", col = "red", xlab = "Pumping Year", ylab = "Energy Costs (million $)")
# plot(x = df_0.3PD_0.25DL_Grid_72548$year_number, y = df_0.3PD_0.25DL_Grid_72548$annual_capital_cost * 1e-6, type = "p", col = "blue2", xlab = "Pumping Year", ylab = "Capital Costs (million $)")
# # maintenance cost
# plot(x = df_0.3PD_0.25DL_Grid_72548$year_number, y = df_0.3PD_0.25DL_Grid_72548$maintenance_cost * 1e-6, type = "p", col = "green4", xlab = "Pumping Year", ylab = "Maintenance Costs (million $)")
# plot(x = df_0.3PD_0.25DL_Grid_72548$year_number, y = df_0.3PD_0.25DL_Grid_72548$nonenergy_cost * 1e-6, type = "p", col = "blue2", xlab = "Pumping Year", ylab = "Nonenergy Costs (million $)")
# plot(x = df_0.3PD_0.25DL_Grid_72548$year_number, y = df_0.3PD_0.25DL_Grid_72548$total_cost_allwells * 1e-6, type = "p", col = "green4", xlab = "Pumping Year", ylab = "Total Cost (million $)")
# # plot transmissivity
# plot(x = df_0.3PD_0.25DL_Grid_72548$year_number, y = df_0.3PD_0.25DL_Grid_72548$transmissivity * 86400, type = "p", col = "gold", xlab = "Pumping Year", ylab = "Transmissivity (m2/day)")
#
# plot(x = df_0.3PD_0.25DL_Grid_72548$year_number, y = df_0.3PD_0.25DL_Grid_72548$well_yield * 86400, type = "p", col = "dodgerblue", xlab = "Pumping Year", ylab = "Well Yield (m3/day)")
# plot(x = df_0.3PD_0.25DL_Grid_72548$year_number, y = df_0.3PD_0.25DL_Grid_72548$areal_extent * 1e-6, type = "p", col = "darkorange", xlab = "Pumping Year", ylab = "Well Area (km2)")
# plot all input variables -----------------------------------------------
# setting up the plot
plot_input <- sf_in_R %>% # filter(Country == "United States") %>%
mutate(Grid_area_km = Grid_area/10^6, # convert to km2
Aquifer_thickness = case_when(Aquifer_thickness > 1000 ~ 1000,
TRUE ~ Aquifer_thickness)) %>% # replace all MEAN_thk_m values greater than 1000 with 1000
select(c("Porosity", "Permeability", "Aquifer_thickness",
"Depth_to_water", "Grid_area_km", "Recharge", "WHYClass")) # filter only relevant ones
# rename(c("storativity" = "MEAN_Poros", "permeability" = "MEAN_Perme",
# "grid_area_km" = "CalcArea_m", "total_thickness" = "MEAN_thk_m",
# "depth_to_water" = "MEAN_Depth", "country_name" = "COUNTRY",
# "GridCellID" = "OriginalOb")) # rename to make the join with output smooth
# legendnames <- c("Porosity" = "Porosity (-)", "Permeability" = "Permeability (m2)",
legendnames <- c("Porosity" = "Porosity (-)", "Permeability" = expression('Permeability (m'^2*')'),
"Aquifer_thickness" = "Aquifer Thickness (m)", "Depth_to_water" = "Depth to Water (m)",
"Grid_area_km" = expression('Grid Area (km'^2*')'), "Recharge" = "Recharge (m/yr)", "WHYClass" = "WHY Class") # to label legend properly
# see all palettes available: tmaptools::palette_explorer()
pal <- c("Porosity" = "BuPu", "Permeability" = "YlGn", "Grid_area_km" = "BuGn",
"Aquifer_thickness" = "YlOrRd", "Depth_to_water" = "Blues", "Recharge" = "Blues", "WHYClass" = "PuBuGn")
# max(plot_input$Permeability)
# plot all input variables using ggplot
for (plot in colnames(plot_input)[6:(length(plot_input) - 2)]) { # plot all input variables except last two
ggplot(plot_input) +
geom_sf(aes(fill = .data[[plot]]), lwd = 0, colour = NA) +
scale_fill_gradientn(name = legendnames[plot],
colors = c(brewer.pal(9, pal[plot])),
values = scales::rescale(sort(c(min(plot_input[[plot]]),
kmeans(plot_input[[plot]], centers = 7)$center, # breaks by kmeans clustering
max(plot_input[[plot]]))))
) +
coord_sf(expand = FALSE) + # makes sure the plot doesn't extend beyond Earth (also plots y axis ticks and crops)
theme_minimal() + theme(plot.margin = grid::unit(c(-4, 0, -4, 0), "cm"),
legend.justification = c(0,0), legend.position = c(0,0),
# legend.title = element_text(face = "bold"),
panel.grid = element_blank())
# save each plot
plotname <- paste0(figs_dir, "map_in_", plot, ".png")
ggsave(plotname, width = 11, height = 3.5, units = "in", dpi = 300)
}
# for WHYClass
ggplot(plot_input) +
geom_sf(lwd = 0, aes(fill = as.factor(WHYClass))) +
scale_fill_manual(name = "WHY Class", values = c("#377eb8", "#e41a1c", "#4daf4a")) +
coord_sf(expand = FALSE) + # makes sure the plot doesn't extend beyond Earth (also plots y axis ticks and crops)
theme_minimal() + theme(panel.grid = element_blank(),
plot.margin = grid::unit(c(-4, 0, -4, 0), "cm"),
legend.justification = c(0,0), legend.position = c(0,0)
# legend.title = element_text(face = "bold")
)
ggsave(paste0(figs_dir, "map_in_WHYClass.png"), width = 11, height = 3.5, units = "in", dpi = 300)
# for recharge
ggplot(sf_in_R) +
geom_sf(lwd = 0, aes(fill = Recharge), lwd = 0, colour = NA) +
scale_fill_gradientn(name = "Recharge (m/yr)",
colors = c(brewer.pal(9, "Blues")),
values = scales::rescale(sort(c(min(sf_in_R$Recharge),
kmeans(sf_in_R$Recharge, centers = 2)$center, # breaks by kmeans clustering
max(sf_in_R$Recharge))))
) +
coord_sf(expand = FALSE) + # makes sure the plot doesn't extend beyond Earth (also plots y axis ticks and crops)
theme_minimal() + theme(plot.margin = grid::unit(c(-4, 0, -4, 0), "cm"),
legend.justification = c(0,0), legend.position = c(0,0),
panel.grid = element_blank())
# save each plot
ggsave(paste0(figs_dir, "map_in_Recharge.png"), width = 11, height = 3.5, units = "in", dpi = 300)
# plot outputs #################################################################
add_geometry <- function (df) {
df %>% left_join(select(sf_in, c("GridCellID", "geometry")), by = "GridCellID") %>% st_as_sf()
}
# for old inputs and outputs
# df_0.3PD_0.25DL %>% filter(country_name %in% c("United States", "Canada")) %>% #%in% c("United States", "Canada")
# left_join(select(input, c("country_name", "GridCellID", "grid_area_km", "permeability",
# "storativity", "depth_to_water", "total_thickness",
# "Shape_Leng", "Shape_Area", "geometry")),
# by = c("country_name", "GridCellID")) %>%
# select(-contains(".y")) %>%
# rename_at(vars(contains(".x")), ~ str_remove(., ".x")) %>%
# st_as_sf() -> df_out
# summary_df_0.3PD_0.25DL <- psych::describe(df_out)
# function to plot maps for outputs
plot_map <- function (plot_data, plot_var, legend_var, color_var, kcenters = 7) {
# plot <-
ggplot(plot_data) +
geom_sf(aes(fill = .data[[plot_var]]), color = NA, lwd = 0) +
scale_fill_gradientn(name = legend_var,
colors = c(brewer.pal(9, color_var)),
values = scales::rescale(sort(c(min(plot_data[[plot_var]]),
kmeans(plot_data[[plot_var]], centers = kcenters)$center, # breaks by kmeans clustering
max(plot_data[[plot_var]]))))) +
coord_sf(expand = FALSE) + # makes sure the plot doesn't extend beyond Earth (also plots y axis ticks and crops)
theme_minimal() + theme(panel.grid = element_blank(),
plot.margin = grid::unit(c(-4, 0, -4, 0), "cm"),
legend.justification=c(0,0), legend.position=c(0,0)
# legend.title = element_text(face = "bold")
)
# save each plot
plotname <- paste0(figs_dir, "map_out_", plot_var, ".png")
ggsave(plotname, width = 11, height = 3.5, units = "in", dpi = 300)
# return(plot)
}
## available water map ----
### using inputs (df_in_vol_screened) and covert to ponded depth
# cells above 200 ponded depth
# plot(df_in_vol$ponded_depth_avail > 200, breaks = 100, col = brewer.pal(9, "Blues"), main = "Available Groundwater as Ponded Depth (m)")
# df_in_vol %>% filter(ponded_depth_avail > 200) -> ponded_200
# plot(x = ponded_200$ponded_depth_avail,
# y = ponded_200$Aquifer_thickness,
# col = brewer.pal(9, "Blues"), main = "Available Groundwater as Ponded Depth (m)")
plot_map(df_in_vol_screened %>% add_geometry() %>% select(c("GridCellID", "ponded_depth_avail", "geometry")),
"ponded_depth_avail", "Available Groundwater as \nPonded Depth (m)", "Blues")
### using outputs
system.time(df_out %>% select(c("GridCellID", "available_volume", "geometry")) %>% #unique() %>%
mutate(available_volume = available_volume * 1e-9) %>%
st_as_sf() %>% unique() -> df_plot)
system.time(plot_map(df_plot, "available_volume", expression('Available Volume (km'^3*')'), "Blues"))
## total volume produced map ----
system.time(df_out %>% select(c("GridCellID", "volume_produced_allwells", "geometry")) %>%
group_by(GridCellID) %>%
summarize(volume_produced_allwells = sum(volume_produced_allwells) * 1e-9) %>%
ungroup() %>% add_geometry() -> df_out_vol)
print(paste0("Global Pumped Volume in 0.3PD 0.25DL = ",