forked from pik-piam/mrremind
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalcCement.R
54 lines (47 loc) · 1.66 KB
/
calcCement.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
#' Calculate Historic Cement Production
#'
#' Combines cement production data from [`readvanRuijven2016()`] and
#' [`readUSGS(cement)`][readUSGS] into a single data set, using USGS data from
#' 2005 on.
#'
#' @md
#' @return A list with a [`magpie`][magclass::magclass] object `x` with
#' country-level cement production in tonnes, `weight`, `unit`, `description`,
#' and `min` fields.
#'
#' @author Michaja Pehl
#'
#' @seealso [calcOutput]
#'
#' @importFrom dplyr anti_join arrange bind_rows filter group_by select ungroup
#' @importFrom rlang .data syms !!!
#' @export
calcCement <- function() {
transition_year <- 2005
. <- NULL
d_vanRuijvan2016 <- readSource('vanRuijven2016', convert = FALSE) %>% quitte::madrat_mule()
d_USGS_cement <- readSource('USGS', 'cement', convert = FALSE) %>%
quitte::madrat_mule() %>%
group_by(!!!syms(c('iso3c', 'year'))) %>%
filter(max(.data$reporting.year) == .data$reporting.year) %>%
ungroup() %>%
select(-'reporting.year')
d <- d_USGS_cement %>% filter(transition_year <= .data$year)
d <- bind_rows(
d,
d_vanRuijvan2016 %>%
anti_join(d, c('iso3c', 'year'))
) %>%
group_by(!!!syms(c('iso3c', 'year'))) %>%
mutate(count = n()) %>%
assertr::verify(1 == .data$count, description = 'only one data point per country/year') %>%
select(-'count') %>%
arrange(!!!syms(c('iso3c', 'year')))
return(list(x = d %>%
as.magpie(spatial = 1, temporal = 2, data = ncol(.)) %>%
toolCountryFill(verbosity = 2),
weight = NULL,
description = 'historical cement production',
unit = 'tonnes of cement',
min = 0))
}