forked from pik-piam/mrremind
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request pik-piam#157 from 0UmfHxcvx5J7JoaOhFSs5mncnisTJJ6q…
…/dev/Mueller add readMueller() to read per-capita steel stock data from Müller et al. 2013
- Loading branch information
Showing
6 changed files
with
89 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
#' Read Müller et al. 2013 data. | ||
#' | ||
#' Read data from Müller et al. 2013 (http://dx.doi.org/10.1021/es402618m). | ||
#' | ||
#' @md | ||
#' @param subtype One of: | ||
#' - `countries`: read table mapping country names use by Müller et al. 2013 | ||
#' to ISO 3166-1 alpha-3 codes. | ||
#' - `stocks`: read low/medium/high estimates of per-capita steel stocks from | ||
#' Müller et al. 2013 SI2 | ||
#' | ||
#' @return A [`magpie`][magclass::magclass] object. | ||
#' | ||
#' @author Michaja Pehl | ||
#' | ||
#' @importFrom assertr assert in_set | ||
#' | ||
#' @seealso [`readSource()`] | ||
|
||
|
||
#' @export | ||
readMueller <- function(subtype) | ||
{ | ||
# ---- list all available subtypes with functions doing all the work ---- | ||
switchboard <- list( | ||
countries = function() | ||
{ | ||
read_csv(file = './Mueller_countries.csv', | ||
col_types = 'cc') %>% | ||
# check for duplicated ISO codes | ||
distinct(.data$country, .data$iso3c) %>% | ||
group_by(.data$iso3c) %>% | ||
mutate(iso3c.count = n()) %>% | ||
ungroup() %>% | ||
assert(in_set(1), .data$iso3c.count) %>% | ||
select(-.data$iso3c.count) %>% | ||
madrat_mule() | ||
}, | ||
|
||
stocks = function() | ||
{ | ||
# read low, medium, and high estimates ---- | ||
lapply(c('low', 'med', 'high'), function(estimate) | ||
{ | ||
read_excel(path = paste0('./Mueller_2013_CarbonEmissions_', | ||
'InfrastructureDevelopment_SI2.xlsx'), | ||
sheet = paste('steel stock per cap', estimate), | ||
range = 'A3:BH292') %>% | ||
select(country = .data$Country, matches('^[0-9]{4}$')) %>% | ||
pivot_longer(cols = matches('^[0-9]{4}$'), names_to = 'year', | ||
names_transform = list(year = as.integer)) %>% | ||
filter(.data$value != 0) %>% | ||
mutate(estimate = estimate) | ||
}) %>% | ||
# combine all three sheets ---- | ||
bind_rows() %>% | ||
# combine USA and "USA (before 1981)" ---- | ||
mutate(country = ifelse('USA (before 1981)' == .data$country, 'USA', | ||
.data$country)) %>% | ||
# add iso3c codes ---- | ||
inner_join( | ||
readSource(type = 'Mueller', subtype = 'countries', | ||
convert = FALSE) %>% | ||
madrat_mule(), | ||
|
||
'country' | ||
) %>% | ||
select(.data$estimate, .data$iso3c, .data$year, | ||
steel.stock.per.capita = .data$value) %>% | ||
madrat_mule() | ||
} | ||
) | ||
|
||
# ---- check if the subtype called is available ---- | ||
if (is_empty(intersect(subtype, names(switchboard)))) { | ||
stop(paste('Invalid subtype -- supported subtypes are:', | ||
names(switchboard))) | ||
} else { | ||
# ---- load data and do whatever ---- | ||
return(switchboard[[subtype]]()) | ||
} | ||
} |