R/readRFCropPast.R

## possibly use spatial.tools:brickstack_to_raster_list(x)? package seems not to be developed anymore
## maybe use package spacetime to organize the data: Is at the moment mostly for polygons/points so sparse data
#geosphere permits computations of distance and area to be carried out on spatial data in geographical coordinates
#Modern country boundaries are provided at 2 resolutions by rworldmap along with functions to join and map tabular data referenced by country names or codes.
#Higher resolution country borders are available from the linked package rworldxtra. Historical country boundaries (1946-2012) can be obtained from the cshapes package along with functions for calculating distance matrices 

## for the current version of raster2magpie it would be better to have the different time slices within one raster brick/Stack
## for every list element lapply could be applied: This would m

# for time series with rasters possibly use zoo or rts package. But so far not seen any advantage

## output is accessed like this tmp[["crop"]][["y1995"]]



#' Read Global Cropland and Pasture Data from 1700-2007
#' 
#' Reads Global Cropland and Pasture Data from 1700-2007 from Ramankutty and
#' Foley.
#' 
#' 
#' @usage readRFCropPast(years=NULL, types=NULL)
#' @param years Specify the year the data is to be read for, e.g. 2000. If left
#' empty all years will be read.
#' @param types Available types are "crop" and "past"
#' @return If one or more types are specified a list of raster stacks.
#' Otherwise simply a raster stack
#' @author Ulrich Kreidenweis
#' @examples
#' 
#' \dontrun{readRFCropPast()}
#' 
#' @export readRFCropPast
readRFCropPast <- function (years=NULL, types=NULL) {
  
  setwd(geodata$config$mainfolder)
  
  if (is.null(years)) years <- 1700:2007
  if (!is.integer(years)) stop("Years have to be specified as integer values e.g. 2000")
  if (min(years) < 1700 | max(years) > 2007) stop("Years have to be in the range of 1700 - 2007")
  
  bands <- as.integer(years-1699)
        
    cropland <- stack("Global Cropland and Pasture Data from 1700-2007/glcrop_1700-2007_0.5.nc", bands=bands)
    names(cropland) <- gsub("X","y",names(cropland))
       
    pasture <- stack("Global Cropland and Pasture Data from 1700-2007/glpast_1700-2007_0.5.nc", bands=bands)
    names(pasture) <- gsub("X","y",names(pasture))

  if (is.null(types)) {
    out <- list()
    out[["crop"]] <- cropland
    out[["past"]] <- pasture
  } else if (identical(sort(types), c("crop","past")) ){
    out <- list()
    out[["crop"]] <- cropland
    out[["past"]] <- pasture
  } else if (types == "crop") {
    out <- cropland
  } else if (types == "past") {
    out <- pasture
  }
  
  attr(out, "unit") <- "share"
  
  return(out)
}
pik-piam/geodata documentation built on Nov. 5, 2019, 12:21 a.m.