Skip to content

Commit

Permalink
IPCC climate class source + enable calcClimateClass to process it
Browse files Browse the repository at this point in the history
  • Loading branch information
k4rst3ns committed Dec 12, 2020
1 parent 64b2418 commit 137b322
Show file tree
Hide file tree
Showing 13 changed files with 207 additions and 12 deletions.
2 changes: 1 addition & 1 deletion .buildlibrary
Original file line number Diff line number Diff line change
@@ -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'
Expand Down
2 changes: 1 addition & 1 deletion .zenodo.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"title": "mrmagpie: madrat based MAgPIE Input Data Library",
"version": "0.28.0",
"version": "0.29.0",
"description": "<p>Provides functions for MAgPIE country and cellular input data generation.<\/p>",
"creators": [
{
Expand Down
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -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 = "[email protected]", role = c("aut","cre")),
person("Jan Philipp", "Dietrich", email = "[email protected]", role = "aut"),
person("David", "Chen", role = "aut"),
Expand Down
3 changes: 3 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
20 changes: 18 additions & 2 deletions R/calcClimateClass.R
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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(
Expand Down
30 changes: 30 additions & 0 deletions R/correctIPCCClimate.R
Original file line number Diff line number Diff line change
@@ -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)
}
34 changes: 34 additions & 0 deletions R/downloadIPCCClimate.R
Original file line number Diff line number Diff line change
@@ -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)
)
}
36 changes: 36 additions & 0 deletions R/readIPCCClimate.R
Original file line number Diff line number Diff line change
@@ -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)
}
9 changes: 4 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# madrat based MAgPIE Input Data Library

R package **mrmagpie**, version **0.28.0**
R package **mrmagpie**, version **0.29.0**



Expand Down Expand Up @@ -38,9 +38,8 @@ In case of questions / problems please contact Kristine Karstens <karstens@pik-p

To cite package **mrmagpie** in publications use:

Karstens K, Dietrich J, Chen D, Windisch M, Alves M, Beier F, v.
Jeetze P, Mishra A, Humpenoeder F (2020). _mrmagpie: madrat
based MAgPIE Input Data Library_. R package version 0.28.0.
Karstens K, Dietrich J, Chen D, Windisch M, Alves M, Beier F, v. Jeetze P, Mishra A, Humpenoeder F (2020). _mrmagpie: madrat
based MAgPIE Input Data Library_. R package version 0.29.0.

A BibTeX entry for LaTeX users is

Expand All @@ -49,7 +48,7 @@ A BibTeX entry for LaTeX users is
title = {mrmagpie: madrat based MAgPIE Input Data Library},
author = {Kristine Karstens and Jan Philipp Dietrich and David Chen and Michael Windisch and Marcos Alves and Felicitas Beier and Patrick {v. Jeetze} and Abhijeet Mishra and Florian Humpenoeder},
year = {2020},
note = {R package version 0.28.0},
note = {R package version 0.29.0},
}
```

5 changes: 4 additions & 1 deletion man/calcClimateClass.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 26 additions & 0 deletions man/correctIPCCClimate.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 24 additions & 0 deletions man/downloadIPCCClimate.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 24 additions & 0 deletions man/readIPCCClimate.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 137b322

Please sign in to comment.