-
Notifications
You must be signed in to change notification settings - Fork 12
/
EIAData.R
80 lines (79 loc) · 4.24 KB
/
EIAData.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
#' Get EIA State Energy Data Systems (SEDS) code and description of state electricity data.
#' @return A data frame of EIA SEDS code and description of state electricity data.
getEIASEDSCodeDescription <- function() {
# Download SEDS Codes and Descriptions table
CodeDescFile <- "inst/extdata/EIA_SEDS_CodesDescriptions.xlsx"
url <- "https://www.eia.gov/state/seds/CDF/Codes_and_Descriptions.xlsx"
# Download EIA State Energy Data Systems (SEDS) consumption data
if (!file.exists(CodeDescFile)) {
utils::download.file(url, CodeDescFile, mode = "wb")
}
notes <- readLines("https://www.eia.gov/state/seds/seds-technical-notes-complete.php?sid=US")
date_last_modified <- stringr::str_match(toString(notes),
"Released: (.*?)<br/>")[2]
date_accessed <- as.character(as.Date(file.mtime(CodeDescFile)))
CodeDesc <- as.data.frame(readxl::read_excel(CodeDescFile,
sheet = "Codes_and_Descriptions",
skip = 9))
# Write data to .rds
data_name <- paste("EIA_SEDS_CodeDescription",
utils::packageDescription("stateior", fields = "Version"),
sep = "_")
saveRDS(object = CodeDesc,
file = paste0(file.path("data", data_name), ".rds"))
# Write metadata to JSON
useeior:::writeMetadatatoJSON(package = "stateior",
name = data_name,
year = NULL,
source = "US Energy Information Administration",
url = url,
date_last_modified = date_last_modified,
date_accessed = date_accessed)
}
# Download, save and document state electricity consumption code and description (from EIA)
getEIASEDSCodeDescription()
#' Get state electricity consumption data (total consumption and interstate trade,
#' in million kilowatt hours) from EIA State Energy Data Systems (SEDS), for a
#' specified year.
#' @param year A numeric value specifying year of interest.
#' @return A data frame of state electricity consumption data (total consumption
#' and interstate trade, in million kilowatt hours) from EIA SEDS for the specified
#' year.
getEIASEDSStateElectricityConsumption <- function(year) {
# Download state electricity consumption data from EIA State Energy Data Systems (SEDS)
ConsumptionFile <- "inst/extdata/EIA_SEDS_consumption.csv"
url <- "https://www.eia.gov/state/seds/sep_use/total/csv/use_all_phy.csv"
if (!file.exists(ConsumptionFile)) {
utils::download.file(url, ConsumptionFile, mode = "wb")
}
notes <- readLines("https://www.eia.gov/state/seds/seds-data-complete.php?sid=US")
date_last_modified <- stringr::str_match(toString(notes),
"Released:</strong> (.*?) ")[2]
date_accessed <- as.character(as.Date(file.mtime(ConsumptionFile)))
Consumption <- utils::read.table(ConsumptionFile, sep = ",", header = TRUE,
stringsAsFactors = FALSE, check.names = FALSE,
fill = TRUE)
if (year %in% colnames(Consumption)) {
# Save data
df <- Consumption[, c("State", "MSN", year)]
# Write data to .rds
data_name <- paste("EIA_SEDS_StateElectricityConsumption", year,
utils::packageDescription("stateior", fields = "Version"),
sep = "_")
saveRDS(object = df,
file = paste0(file.path("data", data_name), ".rds"))
# Write metadata to JSON
useeior:::writeMetadatatoJSON(package = "stateior",
name = data_name,
year = year,
source = "US Energy Information Administration",
url = url,
date_last_modified = date_last_modified,
date_accessed = date_accessed)
} else {
logging::logwarn(paste(year, "state electricity consumption data is not",
"avaliable from EIA SEDS. Nothing is returned."))
}
}
# Download, save and document EIA SEDS state electricity consumption
getEIASEDSStateElectricityConsumption(year)