Skip to content

Commit

Permalink
Merge pull request pik-piam#157 from 0UmfHxcvx5J7JoaOhFSs5mncnisTJJ6q…
Browse files Browse the repository at this point in the history
…/dev/Mueller

add readMueller() to read per-capita steel stock data from Müller et al. 2013
  • Loading branch information
0UmfHxcvx5J7JoaOhFSs5mncnisTJJ6q authored Dec 1, 2021
2 parents e02b9ef + 771ec7c commit cdacb9f
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 5 deletions.
2 changes: 1 addition & 1 deletion .buildlibrary
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
ValidationKey: '17120880'
ValidationKey: '17255420'
AcceptedWarnings:
- 'Warning: package ''.*'' was built under R version'
- 'Warning: namespace ''.*'' is not available and has been replaced'
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/lucode2-check.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ jobs:

- name: Install dependencies
run: |
./run.sh install_aptget libhdf5-dev libgdal-dev
./run.sh install_aptget libgdal-dev
./run.sh install_all
./run.sh install_r lucode2 covr
Expand Down
2 changes: 1 addition & 1 deletion .zenodo.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"title": "mrremind: MadRat REMIND Input Data Package",
"version": "0.90.3",
"version": "0.91.0",
"description": "<p>The mrremind packages contains data preprocessing for the REMIND model.<\/p>",
"creators": [
{
Expand Down
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
Package: mrremind
Type: Package
Title: MadRat REMIND Input Data Package
Version: 0.90.3
Date: 2021-11-29
Version: 0.91.0
Date: 2021-12-01
Authors@R: c(person("Lavinia", "Baumstark", email = "lavinia@pik-potsdam.de", role = c("aut","cre")),
person("Renato", "Rodrigues", role = "aut"),
person("Antoine", "Levesque", role = "aut"),
Expand Down
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export(convertUNFCCC)
export(readADVANCE_WP2)
export(readAGEB)
export(readJRC_IDEES)
export(readMueller)
export(readUBA)
export(readUNFCCC)
export(readvanRuijven2016)
Expand All @@ -30,6 +31,7 @@ export(toolSolarFunctionAggregate)
import(mrcommons)
importFrom(R.utils,isZero)
importFrom(assertr,assert)
importFrom(assertr,in_set)
importFrom(assertr,not_na)
importFrom(assertr,verify)
importFrom(assertthat,assert_that)
Expand Down
82 changes: 82 additions & 0 deletions R/readMueller.R
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]]())
}
}

0 comments on commit cdacb9f

Please sign in to comment.