Skip to content

Commit

Permalink
Improve internal column detection
Browse files Browse the repository at this point in the history
For magpie objects, we can use the order of columns when determining which refer to the iso3c and year columns.
At the same time we need to check for an eventual second spatial dimension (fixes #22).
  • Loading branch information
johanneskoch94 committed Jun 7, 2024
1 parent b02d742 commit 6b710f4
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions R/transform_user_input.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,14 @@ transform_user_input <- function(gdp, unit_in, unit_out, source, use_USA_deflato

# Convert to tibble, if necessary
if (class(gdp)[1] == "magpie") {
gdp <- tibble::as_tibble(gdp) %>% dplyr::rename(tidyselect::any_of(c("iso3c" = "region")))
# Check if the magpie object has 2 spatial dimensions
spat2 <- all(grepl("\\.", magclass::getItems(gdp, dim = 1)))
gdp <- tibble::as_tibble(gdp)
if (!spat2) {
gdp <- gdp %>% dplyr::rename("iso3c" = 1, "year" = 2)
} else {
gdp <- gdp %>% dplyr::rename("iso3c" = 1, "spatial2" = 2, "year" = 3)
}
}

# Extract base years if they exist, and adjust string
Expand Down Expand Up @@ -118,7 +125,13 @@ transform_internal <- function(x, gdp, with_regions, require_year_column) {

# Transform into original gdp type
if (class(gdp)[1] == "magpie") {
x <- magclass::as.magpie(x, spatial = "iso3c", temporal = "year")
# Check if the original magpie object had 2 spatial dimensions
spat2 <- all(grepl("\\.", magclass::getItems(gdp, dim = 1)))
if (!spat2) {
x <- magclass::as.magpie(x, spatial = "iso3c", temporal = "year")
} else {
x <- magclass::as.magpie(x, spatial = c("iso3c", "spatial2"), temporal = "year")
}
magclass::getSets(x) <- magclass::getSets(gdp)
return(x)
}
Expand Down

0 comments on commit 6b710f4

Please sign in to comment.