Skip to content

Commit

Permalink
tidy up stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
pascal-sauer committed Nov 21, 2024
1 parent 75622f7 commit 25e947e
Show file tree
Hide file tree
Showing 8 changed files with 216 additions and 293 deletions.
2 changes: 1 addition & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export(addMapping)
export(cacheCleanup)
export(cacheCopy)
export(calcOutput)
export(calcOutputWrapper)
export(compareData)
export(compareMadratOutputs)
export(downloadSource)
Expand All @@ -28,7 +29,6 @@ export(metadataGFZ)
export(pucAggregate)
export(putMadratMessage)
export(readSource)
export(readSourceAlternative)
export(readSourceWrapper)
export(redirectSource)
export(regionscode)
Expand Down
53 changes: 26 additions & 27 deletions R/convertTau.R
Original file line number Diff line number Diff line change
@@ -1,44 +1,43 @@
#' Convert Tau
#'
#'
#' Convert landuse intensity data (tau) to data on ISO country level.
#'
#'
#'
#'
#' @param x MAgPIE object containing tau values and corresponding weights xref
#' at 0.5deg cellular level.
#' @return Tau data and weights as MAgPIE object aggregated to country level
#' @author Jan Philipp Dietrich
#' @importFrom magclass ncells getCells<- collapseNames getCells

convertTau <- function(x) {

"!# @monitor madrat:::sysdata$iso_cell magclass:::ncells"
"!# @ignore madrat:::toolAggregate"
tau <- x[,,"tau"]
xref <- x[,,"xref"]

tau <- x[, , "tau"]
xref <- x[, , "xref"]

# clean data
# make sure that the weight for nonexisting tau values is 10^-10
xref[is.na(tau) | is.nan(tau)] <- 10^-10
#fill gaps within tau factors with 1 (the global mean)
tau[ is.na(tau) | is.nan(tau)] <- 1

#calculate numbers on country level if they are provided on cellular level
if (ncells(x) == 59199) {
xref[is.na(tau)] <- 10^-10
# fill gaps within tau factors with 1 (the global mean)
tau[is.na(tau)] <- 1

# calculate numbers on country level if they are provided on cellular level
if (ncells(x) == 59199) {
# read mapping cells -> iso countries
iso_cell <- sysdata$iso_cell
iso_cell[,2] <- getCells(x)
isoCell <- sysdata$iso_cell
isoCell[, 2] <- getCells(x)

# aggregate data
tau <- toolAggregate(tau, rel=iso_cell, weight = collapseNames(xref))
xref <- toolAggregate(xref, rel=iso_cell)
# aggregate data
tau <- toolAggregate(tau, rel = isoCell, weight = collapseNames(xref))
xref <- toolAggregate(xref, rel = isoCell)
}
#check whether the country list agrees with the list of countries in the madrat package
#remove unrequired data, add missing data
tau <- toolCountryFill(tau,fill=1,TLS="IDN",HKG="CHN",SGP="CHN",BHR="QAT")
xref <- toolCountryFill(xref, fill=0, verbosity=2)

return(mbind(tau,xref))
}

# check whether the country list agrees with the list of countries in the madrat package
# remove unrequired data, add missing data
tau <- toolCountryFill(tau, fill = 1, TLS = "IDN", HKG = "CHN", SGP = "CHN", BHR = "QAT")
xref <- toolCountryFill(xref, fill = 0, verbosity = 2)

return(mbind(tau, xref))
}
2 changes: 1 addition & 1 deletion R/readSource.R
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
#' if the convert argument is set to TRUE, should be on ISO code country level.
#' @author Jan Philipp Dietrich, Anastasis Giannousakis, Lavinia Baumstark, Pascal Sauer
#' @seealso \code{\link{setConfig}}, \code{\link{downloadSource}}, \code{\link{readTau}}
#' #' @note The underlying read-functions can return a magpie object or a list of information
#' @note The underlying read-functions can return a magpie object or a list of information
#' (preferred) back to \code{readSource}. In list format the object should have the following
#' structure:
#' \itemize{
Expand Down
254 changes: 0 additions & 254 deletions R/readSourceAlternative.R

This file was deleted.

2 changes: 1 addition & 1 deletion R/readSourceWrapper.R
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
#' @export
readSourceWrapper <- function(read, correct = NULL, convert = NULL, subtype = NULL, subset = NULL) {

#' get string/bool from function(s) for use in readSource
# get string/bool from function(s) for use in readSource
.getConvert <- function(convert, correct) {
if (!is.null(convert)) {do_convert <- TRUE}
else if (!is.null(correct)) {do_convert <- "onlycorrect"}
Expand Down
Loading

0 comments on commit 25e947e

Please sign in to comment.