-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' of github.com:pik-piam/mredgebuildings into uvalues
- Loading branch information
Showing
105 changed files
with
6,156 additions
and
699 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
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,69 @@ | ||
#' Get Population Density | ||
#' | ||
#' @returns magpie object | ||
#' | ||
#' @author Hagen Tockhorn | ||
#' | ||
#' @importFrom madrat calcOutput | ||
#' @importFrom dplyr select mutate rename | ||
#' @importFrom quitte as.quitte | ||
#' @importFrom magclass as.magpie | ||
|
||
|
||
calcDensity <- function() { | ||
|
||
# READ-IN DATA---------------------------------------------------------------- | ||
|
||
pop <- calcOutput("PopulationPast", aggregate = FALSE) %>% | ||
as.quitte() | ||
|
||
surf <- calcOutput("Surface", aggregate = FALSE) %>% | ||
as.quitte() | ||
|
||
|
||
# PARAMETERS------------------------------------------------------------------ | ||
|
||
# Conversion from 1-e6 --> 1e0 | ||
million2unit <- 1e6 | ||
|
||
|
||
# PROCESS DATA---------------------------------------------------------------- | ||
|
||
# Clean DFs | ||
pop <- pop %>% | ||
select(-"model", -"scenario", -"unit", -"variable") %>% | ||
mutate(value = .data[["value"]] * million2unit) %>% | ||
rename(population = "value") | ||
|
||
surf <- surf %>% | ||
select(-"model", -"scenario", -"unit", -"variable", -"period") %>% | ||
rename(surface = "value") | ||
|
||
# Calculate Density | ||
dens <- left_join(pop, surf, by = "region") %>% | ||
mutate(value = .data[["population"]] / (.data[["surface"]] * 1e3)) %>% | ||
select(-"population", -"surface") | ||
|
||
|
||
# Fill inf values with global density average | ||
avgDens <- mean(dens[!is.infinite(dens$value), ]$value) | ||
|
||
dens <- dens %>% | ||
mutate(value = ifelse(is.infinite(.data[["value"]]), | ||
avgDens, | ||
.data[["value"]])) | ||
|
||
|
||
|
||
# OUTPUT---------------------------------------------------------------------- | ||
|
||
data <- dens %>% | ||
as.data.frame() %>% | ||
as.quitte() %>% | ||
as.magpie() | ||
|
||
return(list(x = data, | ||
weight = NULL, | ||
unit = "m^-2", | ||
description = "Population Density in m^-2")) | ||
} |
Oops, something went wrong.