-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqa_all.qmd
3146 lines (2307 loc) · 101 KB
/
qa_all.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
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
---
title: "QA All Files"
format:
html:
toc: true
toc-expand: true
toc-location: left
html-table-processing: none
code-fold: true
execute:
echo: false
warning: false
editor: visual
project:
execute_dir: project
params:
year: 2023
year_abbrev: 23
---
This markdown file performs QA on the following files:
- Master database
- CHP database
- Generator file
- Unit file
- Plant file
- Region aggregation files
- Subregion file
- All file comparison
- Output file
Each QA step will return a list of of issues raised. If there are no issues in the step, nothing will be returned.
```{r}
#| label: load-libraries
#| message: false
library(dplyr)
library(tidyr)
library(readr)
library(readxl)
library(ggplot2)
library(stringr)
```
# Master Database QA
## Compare plants between EIA and EPA
Check if any plants are only in the EPA data and not EIA data. These plants will be checked with EPA to see if there are any ORIS (plant ID) matches we should be making or if the plants should be deleted.
```{r}
#| label: compare-eia-epa
# load in cleaned epa and eia data
epa <-
read_rds(glue::glue("data/clean_data/epa/{params$year}/epa_clean.RDS"))
eia_860 <-
read_rds(glue::glue("data/clean_data/eia/{params$year}/eia_860_clean.RDS"))
eia_923 <-
read_rds(glue::glue("data/clean_data/eia/{params$year}/eia_923_clean.RDS"))
# group data by distinct plant IDs
compare_plants <-
epa %>%
select(plant_id, plant_name) %>% distinct() %>%
full_join(eia_860$plant %>% select(plant_id) %>% distinct() %>%
mutate(eia_860_plant_id = plant_id), by = "plant_id") %>%
full_join(eia_923$generation_and_fuel_combined %>%
select(plant_id) %>% distinct() %>%
mutate(eia_923_plant_id = plant_id), by = "plant_id")
# identify plants only present in EPA data
check_compare_plants <-
compare_plants %>%
filter(!is.na(plant_id) & is.na(eia_860_plant_id) & is.na(eia_923_plant_id))
# print plants not in EIA
ifelse(nrow(check_compare_plants) == 0, "Status: PASS - No plants in EPA not in EIA.",
glue::glue("Status: NEEDS CHECKED - {nrow(check_compare_plants)} plants in EPA not in EIA."))
knitr::kable(check_compare_plants %>%
select(plant_id, plant_name), caption = "EPA plants not in EIA")
# compare to EPA plants to delete in unit file
epa_plants_to_delete <-
read_csv("data/static_tables/epa_plants_to_delete.csv")
only_in_epa <-
check_compare_plants %>%
filter(!plant_id %in% epa_plants_to_delete$`ORIS Code`)
# print plants not in epa_plants_to_delete
ifelse(nrow(only_in_epa) == 0, "Status: PASS - No plants in EPA not in list of EPA plants to delete.",
glue::glue("Status: NEEDS CHECKED - {nrow(only_in_epa)} plants in EPA not in list of EPA plants to delete."))
knitr::kable(only_in_epa %>%
select(plant_id, plant_name), caption = "EPA plants not in list of EPA plants to delete")
```
## Biomass units added table
Not automated. Need to check manually. Not done here.
## OG/OTH fuel crosswalks
Not automated. Need to check manually. Not done here.
## EPA fuel crosswalks
Not automated. Need to check manually. Not done here.
# Generator File QA
Load generator file. Currently using preliminary data for eGRID `r params$year`.
```{r}
#| label: load-gen-file
generator_file <-
read_rds(glue::glue("data/outputs/{params$year}/generator_file.RDS"))
```
## Duplicate generators
Check if there are any duplicate generators. There should be zero.
```{r}
#| label: generator-duplicates
check_gen_duplicates <-
generator_file %>%
group_by(plant_id, generator_id) %>%
filter(n() > 1)
ifelse(nrow(check_gen_duplicates) == 0, "Status: PASS - No generator duplicates.",
glue::glue("Status: NEEDS CHECKED - {nrow(check_gen_duplicates)} generators are duplicated."))
knitr::kable(check_gen_duplicates %>%
select(plant_id, generator_id, plant_name))
```
# Unit File QA
Load unit file.
Currently using preliminary data for eGRID `r params$year`.
```{r}
#| label: load-unit-file
unit_file <-
read_rds(glue::glue("data/outputs/{params$year}/unit_file.RDS"))
```
## Prime mover, botfirty, and fuel type missing from EF table
Identify if any combinations are missing from NOx and SO2 EF table.
```{r}
#| label: check-ef-table
emission_factors_pu <-
read_csv("data/static_tables/emission_factors_physicalunits.csv")
emission_factors_hi <-
read_csv("data/static_tables/emission_factors_heatinput.csv")
emission_factors <-
rbind(emission_factors_pu, emission_factors_hi) %>% distinct()
fuel_type_category <-
read_csv("data/static_tables/fuel_type_categories.csv",
col_types = cols_only(combustion_fuels = "c"))
combustion_fuels <- fuel_type_category[["combustion_fuels"]]
unit_pm_botfirty_fuel <-
unit_file %>%
select(prime_mover, botfirty, primary_fuel_type, contains("mass")) %>%
filter(primary_fuel_type %in% c(combustion_fuels, "OTH")) %>%
distinct()
ef_pm_botfirty_fuel <-
emission_factors %>%
select(prime_mover, botfirty, primary_fuel_type) %>%
mutate(botfirty = if_else(botfirty %in% c("N/A", "null"), NA_character_, botfirty)) %>%
distinct()
missing_from_ef <-
unit_pm_botfirty_fuel %>% anti_join(ef_pm_botfirty_fuel)
knitr::kable(missing_from_ef %>%
select(prime_mover, botfirty, primary_fuel_type) %>%
distinct() %>%
arrange(primary_fuel_type))
```
## FC prime mover
FC prime mover units must not have any CO2 emissions or sources.
```{r}
#| label: unit-fc-pm
check_unit_fc_pm <-
unit_file %>%
filter(prime_mover == "FC" & !is.na(co2_mass))
ifelse(nrow(check_unit_fc_pm) == 0, "Status: PASS - All CO2 values for FC prime mover are NA.",
glue::glue("Status: NEEDS CHECKED - {nrow(check_unit_fc_pm)} FC units have non-zero CO2 values."))
knitr::kable(check_unit_fc_pm %>%
select(plant_name, plant_id, unit_id))
```
## Heat input / emissions values for retired units
Check if any EPA units that retired in current data year have heat input / emission values.
```{r}
#| label: unit-values-retired
check_unit_retired_values <-
unit_file %>%
filter(capd_flag == "Yes" & operating_status == "RE" & (heat_input > 0 |
heat_input_oz > 0 |
nox_mass > 0 |
nox_oz_mass > 0 |
so2_mass > 0 |
co2_mass > 0))
ifelse(nrow(check_unit_retired_values) == 0,
"Status: PASS - No units that are retired and have positive heat input or emissions.",
glue::glue("Status: NEEDS CHECKED - {nrow(check_unit_retirement)} units that are retired and
have positive heat input or emissions."))
knitr::kable(check_unit_retired_values %>%
select(plant_name, plant_id, unit_id))
```
## EPA ozone season reporter outliers
Some EPA units only report their ozone season emissions, so we try to gap fill the remaining months with EIA data. In the source columns, we have an option that states this (i.e. "EIA non-ozone season distributed and EPA ozone season" for heat input and "Estimated using emissions factor and EIA data for non-ozone season and EPA ozone season emissions" or "Estimated based on unit-level NOx emission rates and EPA ozone season emissions" for annual NOx). We want to make sure that the NOx emissions we've assigned for these aren't extremely large outliers - especially compared to the ozone season values for NOx at the plant. So you'd filter the NOx source for these sources and then filter the NOx annual emissions from largest to smallest and see how those large ones compare to the ozone season NOx value. If super high we want to note that we need to look into that plant/unit.
```{r}
#| label: unit-nox-outliers
check_unit_nox_outliers <-
unit_file %>%
filter(nox_source %in%
c("Estimated using emissions factor and EIA data for non-ozone season
and EPA ozone season emissions",
"Estimated based on unit-level NOx emission rates and EPA ozone season emissions")) %>%
arrange(desc(nox_mass)) %>%
select(plant_name, plant_id, unit_id, nox_mass, nox_oz_mass)
knitr::kable(check_unit_nox_outliers)
```
## Ozone emissions and heat input
Ozone emissions and heat input values must not be greater than annual values.
```{r}
#| label: unit-ozone
check_unit_ozone <-
unit_file %>%
filter((nox_oz_mass > nox_mass) | (heat_input_oz > heat_input))
ifelse(nrow(check_unit_ozone) == 0,
"Status: PASS - All ozone emissions and heat input values are less than annual values.",
glue::glue("Status: NEEDS CHECKED - {nrow(check_unit_ozone)} units have heat input values greater than annual values."))
knitr::kable(check_unit_ozone %>%
select(plant_name, plant_id, unit_id, nox_oz_mass, nox_mass, heat_input_oz, heat_input))
```
## Positive annual heat input, missing ozone season for EPA plants
Check if units with a positive annual heat input are missing ozone season data.
```{r}
#| label: unit-ozone-missing
check_unit_ozone_input <-
unit_file %>%
filter(!is.na(heat_input) & heat_input != 0 & is.na(heat_input_oz))
ifelse(nrow(check_unit_ozone_input) == 0,
"Status: PASS - All annual heat inputs have ozone heat inputs.",
glue::glue("Status: NEEDS CHECKED - {nrow(check_unit_ozone_input)} units have positive annual heat input with missing ozone season heat input."))
knitr::kable(check_unit_ozone_input %>%
select(plant_name, plant_id, unit_id, heat_input, heat_input_oz))
```
## Duplicate units
Check if there are any duplicate units. There should be zero.
```{r}
#| label: unit-duplicates
check_unit_duplicates <-
unit_file %>%
group_by(plant_id, unit_id, prime_mover) %>%
filter(n() > 1)
unit_duplicates_number <-
check_unit_duplicates %>%
distinct(unit_id, .keep_all = TRUE)
ifelse(nrow(check_unit_duplicates) == 0, "Status: PASS - No duplicate units.",
glue::glue("Status: NEEDS CHECKED - {nrow(unit_duplicates_number)} units are duplicated."))
knitr::kable(unit_duplicates_number %>%
select(plant_name, plant_id, unit_id, prime_mover))
```
## Negative emissions values
Check if any emission values are negative. The emissions to check are:
- unadjusted annual NOx
- unadjusted ozone NOx
- unadjusted annual SO2
- unadjusted annual CO2
- unadjusted annual Hg
```{r}
#| label: unit-negative-emissions
check_unit_negative_emissions <-
unit_file %>%
filter(nox_mass < 0 |
nox_oz_mass < 0 |
so2_mass < 0 |
co2_mass < 0 |
hg_mass < 0)
ifelse(nrow(check_unit_negative_emissions) == 0, "Status: PASS - No negative emissions.",
glue::glue("Status: NEEDS CHECKED - {nrow(check_unit_negative_emissions)} units have negative emissions."))
knitr::kable(check_unit_negative_emissions %>%
select(plant_name, plant_id, unit_id, nox_mass, nox_oz_mass, so2_mass, co2_mass))
```
## Emissions from zero emission fuels
Check if there are positive emission values from zero-emission fuels. There should be zero. The emission values to check are:
- unadjusted annual NOx
- unadjusted ozone NOx
- unadjusted annual SO2
- unadjusted annual CO2
- unadjusted annual Hg
```{r}
#| label: unit-zero-emissions
check_unit_zero_emissions <-
unit_file %>%
filter(primary_fuel_type %in% c("WAT", "WND", "SUN", "NUC") &
(!is.na(nox_mass) | nox_mass > 0 |
!is.na(nox_oz_mass) | nox_oz_mass > 0 |
!is.na(so2_mass) | so2_mass > 0 |
!is.na(co2_mass) | co2_mass > 0 |
!is.na(hg_mass) | hg_mass > 0))
ifelse(nrow(check_unit_zero_emissions) == 0, "Status: PASS - No positive emissions for zero emissions technologies.",
glue::glue("Status: NEEDS CHECKED {nrow(check_zero_emissions)} zero emission units have positive emissions."))
knitr::kable(check_unit_zero_emissions %>%
select(plant_name, plant_id, unit_id, primary_fuel_type, nox_mass, nox_oz_mass, so2_mass, co2_mass, hg_mass))
```
## Primary fuel blanks
Check if any primary fuel rows are blank.
```{r}
#| label: unit-primary-fuel
check_unit_primary_fuel <-
unit_file %>%
filter(is.na(primary_fuel_type))
ifelse(nrow(check_unit_primary_fuel) == 0, "Status: PASS - No units have blank primary fuels.",
glue::glue("Status: NEEDS CHECKED - {nrow(check_unit_primary_fuel)} units have blank primary fuels."))
knitr::kable(check_unit_primary_fuel %>%
select(plant_name, plant_id, unit_id, primary_fuel_type, heat_input, heat_input_oz))
```
## Check CAPD flag and program code
Check if units with a CAPD flag have no program code. Columns: `program_code` and `capd_flag`.
```{r}
#| label: unit-capd-flag
check_unit_program_code <-
unit_file %>%
filter(capd_flag == "Yes" & is.na(program_code))
ifelse(nrow(check_unit_program_code) == 0, "Status: PASS - All CAPD flags have a program code.",
glue::glue("Status: NEEDS CHECKED - {nrow(check_unit_program_code)} units have CAPD flag but no program code."))
knitr::kable(check_unit_program_code %>%
select(plant_name, plant_id, unit_id, capd_flag, program_code))
```
## Emissions without heat input
Check if any units that have emission values are missing heat input values.
```{r}
#| label: unit-heat-input-missing
check_unit_heat_input <-
unit_file %>%
filter((nox_mass > 0 | nox_oz_mass > 0 | so2_mass > 0 | co2_mass > 0 | hg_mass > 0) &
(is.na(heat_input) & is.na(heat_input_oz)))
ifelse(nrow(check_unit_heat_input) == 0, "Status: PASS - All units with emissions have heat input values.",
glue::glue("Status: NEEDS CHECKED - {nrow(check_unit_heat_input)} units with emissions are missing heat input values."))
knitr::kable(check_unit_heat_input %>%
select(plant_name, plant_id, unit_id, heat_input, heat_input_oz, nox_mass, nox_oz_mass, so2_mass, co2_mass, hg_mass))
```
## Heat input without emissions
Check if any units have heat input values are missing emission values.
<!--# Can some units have only some emission types? Should "WH" and "PUR" be included? -->
```{r}
#| label: unit-emissions-missing
check_unit_emissions <-
unit_file %>%
filter((!(primary_fuel_type %in% c("WAT", "WND", "SUN", "NUC", "MWH"))) &
(!(prime_mover == "FC")) &
(is.na(nox_mass) & is.na(nox_oz_mass) & is.na(so2_mass) & is.na(co2_mass)) &
(heat_input > 0 | heat_input_oz > 0))
ifelse(nrow(check_unit_emissions) == 0, "Status: PASS - All units with heat inputs
have emission values.",
glue::glue("Status: NEEDS CHECKED - {nrow(check_unit_emissions)} units with heat inputs are missing emission values."))
knitr::kable(check_unit_emissions %>%
select(plant_name, plant_id, unit_id, primary_fuel_type, prime_mover, heat_input, heat_input_oz, nox_mass, nox_oz_mass, so2_mass, co2_mass, hg_mass))
```
## Blanks in heat input and emissions
Check if units have blanks in heat input and emissions.
<!--# which emissions are included here? -->
```{r}
#| label: unit-blanks
check_unit_blanks <-
unit_file %>%
filter((is.na(heat_input) | is.na(heat_input_oz)) & (is.na(nox_mass) | is.na(nox_oz_mass)
| is.na(so2_mass) | is.na(co2_mass)))
ifelse(nrow(check_unit_blanks) == 0, "Status: PASS - No units are missing both heat inputs and emission values.",
glue::glue("Status: NEEDS CHECKED - {nrow(check_unit_blanks)} units are missing both heat and emission values."))
knitr::kable(check_unit_blanks %>%
select(plant_name, plant_id, unit_id, primary_fuel_type, prime_mover, heat_input, heat_input_oz, nox_mass, nox_oz_mass, so2_mass, co2_mass))
```
## Positive NOx, missing SO2
Check if units have positive NOx values and missing SO2 values.
```{r}
#| label: unit-nox-so2
check_unit_nox_so2 <-
unit_file %>%
filter((nox_mass > 0) & (is.na(so2_mass) | so2_mass == 0))
ifelse(nrow(check_unit_nox_so2) == 0, "Status: PASS - All units with positive NOx values
have positive SO2 values.",
glue::glue("Status: NEEDS CHECKED - {nrow(check_unit_emissions)} units with positive NOx values have NA or 0 SO2 values."))
knitr::kable(check_unit_nox_so2 %>%
select(plant_name, plant_id, unit_id, primary_fuel_type, prime_mover, nox_mass, so2_mass))
```
# Plant File QA
Load current year plant file and previous year plant file.
```{r}
#| label: load-plant-data
plant_file <-
read_rds(glue::glue("data/outputs/{params$year}/plant_file.RDS"))
# pulls 2021 data, should pull 2022 data
plant_file_prev <-
read_excel(glue::glue("data/outputs/{params$year - 2}/egrid{params$year - 2}_data.xlsx"),
sheet = glue::glue("PLNT{params$year_abbrev - 2}"), skip = 1) %>%
janitor::clean_names() %>%
rename("plant_id" = "orispl",
"plant_name" = "pname",
"lat_prev" = "lat",
"lon_prev" = "lon",
"chp_flag_prev" = "chpflag",
"subrgn_prev" = "subrgn") %>%
mutate(plant_id = as.character(plant_id))
```
## Difference in latitude and longitude
Check if plants have a difference in longitude and latitude in comparison to previous year data. This is checked to three decimal points.
```{r}
#| label: plant-lat-long
plant_file_lat_lon <-
plant_file %>%
select(plant_id, lat, lon) %>%
left_join(plant_file_prev %>%
select(plant_id, lat_prev, lon_prev), by = "plant_id") %>%
mutate(across(where(is.numeric), round, 3))
check_plant_lat_lon <-
plant_file_lat_lon %>%
mutate(lat_diff = abs(lat - lat_prev), lon_diff = abs(lon - lon_prev)) %>%
filter(!is.na(lat_diff) & !is.na(lon_diff) & !(lat_diff == 0) | !(lon_diff == 0))
ifelse(nrow(check_plant_lat_lon) == 0, "Status: PASS - All plants have the same lat/lon as previous year.",
glue::glue("Status: NEEDS CHECKED - {nrow(check_plant_lat_lon)} plants have different lat/lon than last year."))
knitr::kable(check_plant_lat_lon %>%
select(plant_id, lat, lat_prev, lat_diff, lon, lon_prev, lon_diff))
```
## CHP status change
Check if CHP status has changed from previous year.
```{r}
#| label: plant-chp-flag
plant_chp_flag <-
plant_file %>%
left_join(plant_file_prev, by = c("plant_id")) %>%
mutate(chp_comparison = ifelse(chp_flag == chp_flag_prev, "Same",
"Changed"))
check_plant_chp_flag <-
plant_chp_flag %>%
filter(chp_comparison == "Changed")
ifelse(nrow(check_plant_chp_flag) == 0, "Status: PASS - No CHP flags are different",
glue::glue("Status: NEEDS CHECKED - {nrow(check_plant_chp_flag)} plants have different CHP flags than last year."))
knitr::kable(check_plant_chp_flag %>%
select(plant_id, chp_flag, chp_flag_prev))
```
## Different subregions
Check if any plants have changed subregion from previous year.
```{r}
#| label: plant-subregion
plant_subregion <- plant_file %>%
left_join(plant_file_prev, by = c("plant_id")) %>%
mutate(subrgn_comparison = ifelse(egrid_subregion == subrgn_prev, "Same",
"Changed"))
check_plant_subrgn <- plant_subregion %>%
filter(subrgn_comparison == "Changed")
ifelse(nrow(check_plant_subrgn) == 0, "Status: PASS - No subregions are different",
glue::glue("Status: NEEDS CHECKED - {nrow(check_plant_subrgn)} plants have different subregions than last year."))
knitr::kable(check_plant_subrgn %>%
select(plant_id, egrid_subregion, subrgn_prev))
```
## Missing Lat/Lon Values
```{r}
#| label: missing-lat-lon
missing_lat_lon <-
plant_file %>%
filter(is.na(lat) | is.na(lon))
plant_with_coordinates <-
plant_file %>%
filter(!is.na(lat) & !is.na(lon))
ifelse(nrow(missing_lat_lon) == 0, "Status: PASS - No plants missing coordinates",
glue::glue("Status: NEEDS CHECKED - {nrow(missing_lat_lon)} plants are missing either lat or lon coordinates."))
knitr::kable(missing_lat_lon %>%
select(plant_id, egrid_subregion, lat, lon))
```
## Mislabeled subregions
Map plants for each region and fix accordingly if needed.
```{r}
#| label: load-subregion-shapefile
#| results: false
options(timeout = 180)
# download subregion shapefiles from EPA website
shp_download <- function(shp_type) {
temp_file <- tempfile()
download.file(glue::glue("https://www.epa.gov/system/files/other-files/2024-05/egrid2022_{tolower(shp_type)}_shapefile.zip"), temp_file)
shp_file <- unzip(temp_file, exdir="data/shapefiles")
unlink(temp_file)
shp <-
sf::st_read(glue::glue("data/shapefiles/eGRID2022_{shp_type}.shp")) %>%
drop_na() %>%
rename("subregion" = "ZipSubregi") %>%
sf::st_transform(crs = "EPSG:4326")
return (shp)
}
if(length(list.files("data/shapefiles") == 1)) {
sbrgn_shp <- shp_download("Subregions")
multi_sbrgn_shp <- shp_download("Multiple_Subregions")
} else {
subrgn_shp <-
sf::st_read(glue::glue("data/shapefiles/eGRID2022_Subregions.shp")) %>%
drop_na() %>%
rename("subregion" = "ZipSubregi") %>%
sf::st_transform(crs = "EPSG:4326")
muti_subrgn_shp <-
sf::st_read(glue::glue("data/shapefiles/eGRID2022_Mutiple_Subregions.shp")) %>%
drop_na() %>%
rename("subregion" = "ZipSubregi") %>%
sf::st_transform(crs = "EPSG:4326")
}
sf::sf_use_s2(FALSE)
# define bounding coordinates for plotting
bounding_box_plot <- function(plant_points) {
if (nrow(plant_points)) {
bounds <- list(c(sf::st_bbox(plant_points)[1] - 5,
sf::st_bbox(plant_points)[3] + 5),
c(sf::st_bbox(plant_points)[2] - 3,
sf::st_bbox(plant_points)[4] + 3))
} else {
bounds <- list(c(-167, -70), c(25, 70))
}
return (bounds)
}
```
Check if plant locations are in regions. If there are any plants that are not in the region or in the overlapping regions, they will appear in the table. Some of the plants in the table may be due to being on the edge of region boundaries.
::: panel-tabset
### AKGD
```{r}
#| label: AKGD-map
AKGD_plants <-
plant_with_coordinates %>%
filter(egrid_subregion == "AKGD") %>%
select(egrid_subregion, plant_id, primary_fuel_category, lat, lon) %>%
sf::st_as_sf(coords = c("lon", "lat"), crs = "EPSG:4326")
# create table of plants not in region or multi-region area
# rounding lat long may help capture edge case plants (that are just outside of boundary)
AKGD_shp <-
sbrgn_shp %>%
filter(subregion == "AKGD") %>%
sf::st_transform(crs = "EPSG:4326")
AKGD_plants <-
AKGD_plants %>%
mutate(in_region_check = ifelse(plant_id %in%
c(sf::st_filter(AKGD_plants, AKGD_shp)$plant_id),
TRUE, FALSE),
multi_region_check = ifelse(plant_id %in%
c(sf::st_filter(AKGD_plants, multi_sbrgn_shp)$plant_id),
TRUE, FALSE))
ifelse(nrow(AKGD_plants), glue::glue("{nrow(AKGD_plants)} plants in subregion"),
"No plants fall within subregion")
AKGD_outside <-
AKGD_plants %>%
filter(in_region_check == FALSE & multi_region_check == FALSE)
ggplot() +
geom_sf(data = sbrgn_shp, aes(fill = subregion)) +
geom_sf(data = multi_sbrgn_shp, aes(color = "Multi-region")) +
geom_sf(data = AKGD_plants) +
geom_sf(data = AKGD_outside, color = "red") +
coord_sf(xlim = as.vector(bounding_box_plot(AKGD_plants)[[1]]),
ylim = as.vector(bounding_box_plot(AKGD_plants)[[2]]),
expand = TRUE)
ifelse(nrow(AKGD_outside) == 0, "No plants fall outside subregion and multi-region.",
glue::glue("{nrow(AKGD_outside)} plants fall outside region and subregion."))
knitr::kable(AKGD_outside)
```
### AKMS
```{r}
#| label: AKMS-summary
AKMS_plants <-
plant_with_coordinates %>%
filter(egrid_subregion == "AKMS") %>%
select(egrid_subregion, plant_id, primary_fuel_category, lat, lon) %>%
sf::st_as_sf(coords = c("lon", "lat"), crs = "EPSG:4326")
# create table of plants not in region or multi-region area
AKMS_shp <-
sbrgn_shp %>%
filter(subregion == "AKMS") %>%
sf::st_transform(crs = "EPSG:4326")
AKMS_plants <-
AKMS_plants %>%
mutate(in_region_check = ifelse(plant_id %in%
c(sf::st_filter(AKMS_plants, AKMS_shp)$plant_id),
TRUE, FALSE),
multi_region_check = ifelse(plant_id %in%
c(sf::st_filter(AKMS_plants, multi_sbrgn_shp)$plant_id),
TRUE, FALSE))
ifelse(nrow(AKMS_plants), glue::glue("{nrow(AKMS_plants)} plants in subregion"),
"No plants fall within subregion")
AKMS_outside <-
AKMS_plants %>%
filter(in_region_check == FALSE & multi_region_check == FALSE)
# plot all plants with ones outside bounds in red
ggplot() +
geom_sf(data = sbrgn_shp, aes(fill = subregion)) +
geom_sf(data = multi_sbrgn_shp, aes(color = "Multi-region")) +
geom_sf(data = AKMS_plants) +
geom_sf(data = AKMS_outside, color = "red") +
coord_sf(xlim = as.vector(bounding_box_plot(AKMS_plants)[[1]]),
ylim = as.vector(bounding_box_plot(AKMS_plants)[[2]]),
expand = TRUE)
ifelse(nrow(AKMS_outside) == 0, "No plants fall outside subregion and multi-region.",
glue::glue("{nrow(AKMS_outside)} plants fall outside region and subregion."))
knitr::kable(AKMS_outside)
```
### AZNM
```{r}
#| label: AZNM-summary
AZNM_plants <-
plant_with_coordinates %>%
filter(egrid_subregion == "AZNM") %>%
select(egrid_subregion, plant_id, primary_fuel_category, lat, lon) %>%
sf::st_as_sf(coords = c("lon", "lat"), crs = "EPSG:4326") %>%
sf::st_transform(sf::st_crs(sbrgn_shp))
# create table of plants not in region or multi-region area
AZNM_shp <- sbrgn_shp %>% filter(subregion == "AZNM") %>%
sf::st_transform(crs = "EPSG:4326")
AZNM_plants <-
AZNM_plants %>%
mutate(in_region_check = ifelse(plant_id %in%
c(sf::st_filter(AZNM_plants, AZNM_shp)$plant_id),
TRUE, FALSE),
multi_region_check = ifelse(plant_id %in%
c(sf::st_filter(AZNM_plants, multi_sbrgn_shp)$plant_id),
TRUE, FALSE))
ifelse(nrow(AZNM_plants), glue::glue("{nrow(AZNM_plants)} plants in subregion"),
"No plants fall within subregion")
AZNM_outside <-
AZNM_plants %>%
filter(in_region_check == FALSE & multi_region_check == FALSE)
ggplot() +
geom_sf(data = sbrgn_shp, aes(fill = subregion)) +
geom_sf(data = multi_sbrgn_shp, aes(color = "Multi-region")) +
geom_sf(data = AZNM_plants) +
geom_sf(data = AZNM_outside, color = "red") +
coord_sf(xlim = as.vector(bounding_box_plot(AZNM_plants)[[1]]),
ylim = as.vector(bounding_box_plot(AZNM_plants)[[2]]),
expand = TRUE)
ifelse(nrow(AZNM_outside) == 0, "No plants fall outside subregion and multi-region.",
glue::glue("{nrow(AZNM_outside)} plants fall outside region and subregion."))
knitr::kable(AZNM_outside)
```
### CAMX
```{r}
#| label: CAMX-summary
# one plant with no lat lon data
CAMX_plants <-
plant_with_coordinates %>%
filter(egrid_subregion == "CAMX") %>%
filter(!is.na(lat) | !is.na(lon)) %>%
select(egrid_subregion, plant_id, primary_fuel_category, lat, lon) %>% sf::st_as_sf(coords = c("lon", "lat"), crs = "EPSG:4326") %>%
sf::st_transform(sf::st_crs(sbrgn_shp))
# create table of plants not in region or multi-region area
CAMX_shp <- sbrgn_shp %>% filter(subregion == "CAMX") %>%
sf::st_transform(crs = "EPSG:4326")
CAMX_plants <-
CAMX_plants %>%
mutate(in_region_check = ifelse(plant_id %in%
c(sf::st_filter(CAMX_plants, CAMX_shp)$plant_id),
TRUE, FALSE),
multi_region_check = ifelse(plant_id %in%
c(sf::st_filter(CAMX_plants, multi_sbrgn_shp)$plant_id),
TRUE, FALSE))
ifelse(nrow(CAMX_plants), glue::glue("{nrow(CAMX_plants)} plants in subregion"),
"No plants fall within subregion")
CAMX_outside <-
CAMX_plants %>%
filter(in_region_check == FALSE & multi_region_check == FALSE)
ggplot() +
geom_sf(data = sbrgn_shp, aes(fill = subregion)) +
geom_sf(data = multi_sbrgn_shp, aes(color = "Multi-region")) +
geom_sf(data = CAMX_plants) +
geom_sf(data = CAMX_outside, color = "red") +
coord_sf(xlim = as.vector(bounding_box_plot(CAMX_plants)[[1]]),
ylim = as.vector(bounding_box_plot(CAMX_plants)[[2]]),
expand = TRUE)
ifelse(nrow(CAMX_outside) == 0, "No plants fall outside subregion and multi-region.",
glue::glue("{nrow(CAMX_outside)} plants fall outside region and subregion."))
knitr::kable(CAMX_outside)
```
### ERCT
```{r}
#| label: ERCT-summary
ERCT_plants <-
plant_with_coordinates %>%
filter(egrid_subregion == "ERCT") %>%
select(egrid_subregion, plant_id, primary_fuel_category, lat, lon) %>%
sf::st_as_sf(coords = c("lon", "lat"), crs = "EPSG:4326") %>%
sf::st_transform(sf::st_crs(sbrgn_shp))
ERCT_shp <-
sbrgn_shp %>%
filter(subregion == "ERCT") %>%
sf::st_transform(crs = "EPSG:4326")
ERCT_plants <-
ERCT_plants %>%
mutate(in_region_check = ifelse(plant_id %in%
c(sf::st_filter(ERCT_plants, ERCT_shp)$plant_id),
TRUE, FALSE),
multi_region_check = ifelse(plant_id %in%
c(sf::st_filter(ERCT_plants, multi_sbrgn_shp)$plant_id),
TRUE, FALSE))
ifelse(nrow(ERCT_plants), glue::glue("{nrow(ERCT_plants)} plants in subregion"),
"No plants fall within subregion")
ERCT_outside <-
ERCT_plants %>%
filter(in_region_check == FALSE & multi_region_check == FALSE)
ggplot() +
geom_sf(data = sbrgn_shp, aes(fill = subregion)) +
geom_sf(data = multi_sbrgn_shp, aes(color = "Multi-region")) +
geom_sf(data = ERCT_plants) +
geom_sf(data = ERCT_outside, color = "red") +
coord_sf(xlim = as.vector(bounding_box_plot(ERCT_plants)[[1]]),
ylim = as.vector(bounding_box_plot(ERCT_plants)[[2]]),
expand = TRUE)
ifelse(nrow(ERCT_outside) == 0, "No plants fall outside subregion and multi-region.",
glue::glue("{nrow(ERCT_outside)} plants fall outside region and subregion."))
knitr::kable(ERCT_outside)
```
### FRCC
```{r}
#| label: FRCC-summary
FRCC_plants <-
plant_with_coordinates %>%
filter(egrid_subregion == "FRCC") %>%
select(egrid_subregion, plant_id, primary_fuel_category, lat, lon) %>%
sf::st_as_sf(coords = c("lon", "lat"), crs = "EPSG:4326") %>%
sf::st_transform(sf::st_crs(sbrgn_shp))
FRCC_shp <-
sbrgn_shp %>%
filter(subregion == "FRCC") %>%
sf::st_transform(crs = "EPSG:4326")
FRCC_plants <-
FRCC_plants %>%
mutate(in_region_check = ifelse(plant_id %in%
c(sf::st_filter(FRCC_plants, FRCC_shp)$plant_id),
TRUE, FALSE),
multi_region_check = ifelse(plant_id %in%
c(sf::st_filter(FRCC_plants, multi_sbrgn_shp)$plant_id),
TRUE, FALSE))
ifelse(nrow(FRCC_plants), glue::glue("{nrow(FRCC_plants)} plants in subregion"),
"No plants fall within subregion")
FRCC_outside <-
FRCC_plants %>%
filter(in_region_check == FALSE & multi_region_check == FALSE)
ggplot() +
geom_sf(data = sbrgn_shp, aes(fill = subregion)) +
geom_sf(data = multi_sbrgn_shp, aes(color = "Multi-region")) +
geom_sf(data = FRCC_plants) +
geom_sf(data = FRCC_outside, color = "red") +
coord_sf(xlim = as.vector(bounding_box_plot(FRCC_plants)[[1]]),
ylim = as.vector(bounding_box_plot(FRCC_plants)[[2]]),
expand = TRUE)