diff --git a/.buildlibrary b/.buildlibrary index daceb0d5a..088ea62dc 100644 --- a/.buildlibrary +++ b/.buildlibrary @@ -1,4 +1,4 @@ -ValidationKey: '5209960' +ValidationKey: '5396320' AcceptedWarnings: - 'Warning: package ''.*'' was built under R version' - 'Warning: namespace ''.*'' is not available and has been replaced' diff --git a/.zenodo.json b/.zenodo.json index 11a2c5c74..ae6a614ff 100644 --- a/.zenodo.json +++ b/.zenodo.json @@ -1,6 +1,6 @@ { "title": "mrmagpie: madrat based MAgPIE Input Data Library", - "version": "0.28.0", + "version": "0.29.0", "description": "
Provides functions for MAgPIE country and cellular input data generation.<\/p>",
"creators": [
{
diff --git a/DESCRIPTION b/DESCRIPTION
index 33b695e59..037959dd1 100644
--- a/DESCRIPTION
+++ b/DESCRIPTION
@@ -1,8 +1,8 @@
Package: mrmagpie
Type: Package
Title: madrat based MAgPIE Input Data Library
-Version: 0.28.0
-Date: 2020-12-11
+Version: 0.29.0
+Date: 2020-12-12
Authors@R: c(person("Kristine", "Karstens", email = "karstens@pik-potsdam.de", role = c("aut","cre")),
person("Jan Philipp", "Dietrich", email = "dietrich@pik-potsdam.de", role = "aut"),
person("David", "Chen", role = "aut"),
diff --git a/NAMESPACE b/NAMESPACE
index 5eb2c1d43..10687d663 100644
--- a/NAMESPACE
+++ b/NAMESPACE
@@ -78,6 +78,7 @@ importFrom(raster,area)
importFrom(raster,as.matrix)
importFrom(raster,brick)
importFrom(raster,extent)
+importFrom(raster,extract)
importFrom(raster,projectRaster)
importFrom(raster,raster)
importFrom(raster,rasterToPoints)
@@ -91,5 +92,7 @@ importFrom(stats,kmeans)
importFrom(stats,quantile)
importFrom(tidyr,pivot_wider)
importFrom(tools,file_ext)
+importFrom(utils,download.file)
importFrom(utils,read.table)
+importFrom(utils,unzip)
importFrom(utils,write.table)
diff --git a/R/calcClimateClass.R b/R/calcClimateClass.R
index ab21c7167..0b8a6f871 100644
--- a/R/calcClimateClass.R
+++ b/R/calcClimateClass.R
@@ -1,5 +1,6 @@
#' @title calcClimateClass
#' @description fraction of a cell belonging to a given climate classification based on Koeppen Geiger Classification. http://koeppen-geiger.vu-wien.ac.at/.
+#' @param source select source from: Koeppen, IPCC, IPCC_reduced
#'
#' @return Clustered MAgPIE object on requested resolution
#' @author Abhijeet Mishra
@@ -9,9 +10,24 @@
#'
#' @export
-calcClimateClass <-function(){
+calcClimateClass <-function(source="Koeppen"){
+
+ if(source=="Koeppen"){
+
+ x <- readSource("Koeppen", subtype="cellular",convert = "onlycorrect")
+
+ } else if(grepl("IPCC",source)){
+
+ x <- readSource("IPCCClimate", convert="onlycorrect")
+ getNames(x) <- gsub(" ","_", tolower(getNames(x)))
+
+ if(source=="IPCC_reduced"){
+ reduceIPCC <- toolGetMapping("IPCC2IPCCreduced.csv", type = "sectoral")
+ x <- toolAggregate(x,reduceIPCC,from="ipcc",to="ipcc_reduced", dim=3, partrel=TRUE)
+ }
+
+ } else { stop("Source unkown.")}
- x <- readSource("Koeppen", subtype="cellular",convert = FALSE)
weight <- dimSums(calcOutput("LanduseInitialisation", aggregate=FALSE, cellular=TRUE, land="fao", input_magpie=TRUE, years="y1995", round=6), dim=3)
return(list(
diff --git a/R/correctIPCCClimate.R b/R/correctIPCCClimate.R
new file mode 100644
index 000000000..92aa96e89
--- /dev/null
+++ b/R/correctIPCCClimate.R
@@ -0,0 +1,30 @@
+#' @title correctIPCCClimate
+#' @description Correct IPCC climate classification
+#'
+#' @return Magpie object with results on cellular level for 12 IPCC climate zone types
+#' @param x magpie object provided by the read function
+#' @author Kristine Karstens
+#' @examples
+#'
+#' \dontrun{
+#' readSource("IPCCClimate", convert="onlycorrect")
+#' }
+
+correctIPCCClimate <- function(x){
+
+ ### determine gaps on iso level
+ iso <- unique(substring(where(setYears(x[,,"NA"], "y2010")==1)$true$reg,1,3))
+
+ ## move unknown cells to most common country values
+ nmax <- rep(0,length(iso))
+ names(nmax) <- iso
+ for(i in iso){
+ nmax[i] <- which.max(dimSums((x[i,,]), dim=1))
+ x[i,,nmax[i]] <- x[i,,nmax[i]] + x[i,,"NA"]
+ }
+
+ if(any(x[,,"NA"]==1)) vcat(2,"Still grid cells without climate zone")
+ out <- x[,,"NA",invert=TRUE]
+
+ return(out)
+}
diff --git a/R/downloadIPCCClimate.R b/R/downloadIPCCClimate.R
new file mode 100644
index 000000000..a06704842
--- /dev/null
+++ b/R/downloadIPCCClimate.R
@@ -0,0 +1,34 @@
+#' @title downloadIPCCClimate
+#' @description Download IPCC climate classification
+#'
+#' @return Meta information on downloaded data
+#' @author Kristine Karstens
+#' @examples
+#'
+#' \dontrun{
+#' readSource("IPCCClimate", convert="onlycorrect")
+#' }
+#'
+#' @importFrom utils download.file unzip
+
+downloadIPCCClimate <- function() {
+
+ # Define meta data
+ meta <- list(title = "Thematic Data Layers for Commission Decision of [10 June 2010] on guidelines for the calculation of land carbon stocks for the purpose of Annex V to Directive 2009/28/EC",
+ url = "https://esdac.jrc.ec.europa.eu/projects/RenewableEnergy/Data/Climate_Zone.zip")
+
+ download.file(meta$url, destfile = "ipccclimate.zip")
+ unzip("ipccclimate.zip")
+ unlink("ipccclimate.zip")
+
+ # Compose meta data by adding elements that are the same for all subtypes.
+ return(list(url = meta$url,
+ doi = NULL,
+ title = meta$title,
+ author = NULL,
+ version = NULL,
+ release_date = NULL,
+ license = NULL,
+ reference = NULL)
+ )
+}
diff --git a/R/readIPCCClimate.R b/R/readIPCCClimate.R
new file mode 100644
index 000000000..575737634
--- /dev/null
+++ b/R/readIPCCClimate.R
@@ -0,0 +1,36 @@
+#' @title readIPCCClimate
+#' @description Read IPCC climate classification
+#'
+#' @return Magpie object with results on cellular level for 12 IPCC climate zone types
+#' @author Kristine Karstens
+#' @examples
+#'
+#' \dontrun{
+#' readSource("IPCCClimate", convert="onlycorrect")
+#' }
+#'
+#' @importFrom raster raster aggregate extract
+
+readIPCCClimate <- function(){
+
+ raster_1d12 <- raster("CLIMATE_ZONE.rst")
+ zone_names <- as.character(levels(raster_1d12)[[1]]$Class_name)
+ raster_1d2 <- aggregate(raster_1d12, fact=6, fun=max) # to avoid gaps (since 0 is NA)
+
+ map <- as.data.frame(magpie_coord)
+ mag <- clean_magpie(as.magpie(extract(raster_1d2,map), spatial=1))
+ #map[mag==0] <- 6
+ cellNames <- toolMappingFile(type="cell",name="CountryToCellMapping.csv",readcsv=TRUE)$celliso
+ getNames(mag) <- "NA"
+ getYears(mag) <- NULL
+ getCells(mag) <- cellNames
+ getSets(mag) <- c("country.cell","t","climatezone")
+
+ out <- add_columns(mag, dim=3.1, addnm=zone_names)
+ out[] <- 0
+ for(zone in c(zone_names)){out[,,zone][which(mag==which(zone_names==zone))] <- 1}
+ out[,,"NA"][which(dimSums(out, dim=3)==0)] <- 1
+ out <- mbind(out[,,zone_names],out[,,"NA"])
+
+ return(out)
+}
diff --git a/README.md b/README.md
index 63b40746e..177107d3b 100644
--- a/README.md
+++ b/README.md
@@ -1,6 +1,6 @@
# madrat based MAgPIE Input Data Library
-R package **mrmagpie**, version **0.28.0**
+R package **mrmagpie**, version **0.29.0**
@@ -38,9 +38,8 @@ In case of questions / problems please contact Kristine Karstens