Skip to content

Commit

Permalink
added 'MACCITY' to read
Browse files Browse the repository at this point in the history
  • Loading branch information
Schuch666 committed Nov 21, 2018
1 parent d1753ac commit c897ead
Show file tree
Hide file tree
Showing 3 changed files with 150 additions and 52 deletions.
134 changes: 94 additions & 40 deletions R/read.R
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
#' Read NetCDF data from global inventaries
#'
#' @description Read data from global inventories, can read several files and merge into one emission
#' and/or split into several species (speciation process)
#' @description Read data from global inventories, can read several files and merge into one
#' emission and/or split into several species (speciation process)
#'
#' @return Matrix or raster
#'
#' @param file file name or names (variables are summed)
#' @param coef coef to merge different sources into one emission
#' @param spec numeric speciation vector to split emission into different species
#' @param version inventory name and verion
#' @param version inventory name 'EDGAR' or 'MACCITY'
#' @param month the desired month of the inventary (MACCITY)
#' @param categories considered categories (MACCITY variable names), empty for all
#' @param as_raster return a raster (defoult) or matrix (with units)
#' @param verbose display additional information
#'
Expand All @@ -22,7 +24,21 @@
#'
#' @seealso \code{\link{species}}
#'
#' @source Read abbout EDGAR at http://edgar.jrc.ec.europa.eu
#' @source Read abbout EDGAR at http://edgar.jrc.ec.europa.eu and MACCITY at
#' http://accent.aero.jussieu.fr/MACC_metadata.php
#'
#' @references
#' Janssens-Maenhout, G., Dentener, F., Van Aardenne, J., Monni, S., Pagliari, V., Orlandini,
#' L., ... & Wankmüller, R. (2012). EDGAR-HTAP: a harmonized gridded air pollution emission dataset
#' based on national inventories. European Commission Joint Research Centre Institute for
#' Environment and Sustainability. JRC 68434 UR 25229 EUR 25229, ISBN 978-92-79-23123-0.
#'
#' Lamarque, J.-F., Bond, T. C., Eyring, V., Granier, C., Heil, A., Klimont, Z., Lee, D., Liousse,
#' C., Mieville, A., Owen, B., Schultz, M. G., Shindell, D., Smith, S. J., Stehfest, E.,
#' Van Aardenne, J., Cooper, O. R., Kainuma, M., Mahowald, N., McConnell, J. R., Naik, V.,
#' Riahi, K., and van Vuuren, D. P.: Historical (1850-2000) gridded anthropogenic and biomass
#' burning emissions of reactive gases and aerosols: methodology and application,
#' Atmos. Chem. Phys., 10, 7017-7039, doi:10.5194/acp-10-7017-2010, 2010.
#'
#' @examples \donttest{
#' d1 <- gridInfo(paste(system.file("extdata", package = "EmissV"),"/wrfinput_d01",sep=""))
Expand All @@ -38,37 +54,82 @@
#'}

read <- function(file = file.choose(), coef = rep(1,length(file)), spec = NULL,
version = "EDGAR 4.3.1 v2", as_raster = T, verbose = T){
version = "EDGAR", month = 1, categories, as_raster = T, verbose = T){

if(is.list(coef))
coef <- as.numeric(as.character(unlist(coef))) #nocov
if(is.list(spec))
spec <- as.numeric(as.character(unlist(spec))) #nocov


if(version == "EDGAR 4.3.1 v2"){
if(version == "MACCITY"){ # nocov start
ed <- ncdf4::nc_open(file[1])
name <- names(ed$var)
if(verbose)
cat(paste0("reading",
" (",version,")",
" emissions for ",
format(ISOdate(1996,month,1),"%B"),
", output unit is g m-2 s-1 ...\n"))
var <- ncdf4::ncvar_get(ed,name[1])
var <- var[,,month]
varall <- units::as_units(0.0 * var,"g m-2 s-1")
var <- apply(var,1,rev)
if(as_raster){
# r <- raster::raster(x = 1000 * var,xmn=-180,xmx=180,ymn=-90,ymx=90)
# rz <- raster::raster(0.0 * var,xmn=-180,xmx=180,ymn=-90,ymx=90)
r <- raster::raster(x = 1000 * var,xmn=0,xmx=360,ymn=-90,ymx=90)
rz <- raster::raster(0.0 * var,xmn=0,xmx=360,ymn=-90,ymx=90)
values(rz) <- rep(0,ncell(rz))
raster::crs(rz) <- "+proj=longlat +ellps=GRS80 +no_defs"
}

for(i in 1:length(file)){
cat(paste0("from ",file[i]),"x",sprintf("%02.2f",coef[i]),"\n")
ed <- ncdf4::nc_open(file[i])
if(missing(categories)){
name <- names(ed$var)
}else{
name <- categories
}
for(j in 1:length(name)){
cat(paste0("using ",name[j]),"\n")
var_a <- ncdf4::ncvar_get(ed,name[j])[,,month]
var_a <- units::as_units(var_a,"g m-2 s-1")
var_a <- apply(var_a,1,rev)
var <- var + var_a
}
if(as_raster){
r <- raster::raster(x = 1000 * var,xmn=0,xmx=360,ymn=-90,ymx=90)
raster::crs(r) <- "+proj=longlat +ellps=GRS80 +no_defs"
names(r) <- name[1]
rz <- rz + r * coef[i]
}else{
var <- units::set_units(1000 * var,"g m-2 s-1")
return(varall)
varall <- varall + var * coef[i]
}
}
} # nocov end

if(version == "EDGAR"){
ed <- ncdf4::nc_open(file[1])
name <- names(ed$var)
if(verbose)
cat(paste0("reading ",name," (",version,") units are g m-2 s-1 ...\n"))
cat(paste0("reading ",name," (",version,") output unit is g m-2 s-1 ...\n"))
var <- ncdf4::ncvar_get(ed,name)
varall <- units::as_units(0.0 * var,"g m-2 s-1")
var <- apply(var,1,rev)
if(as_raster){
r <- raster::raster(x = 1000 * var,xmn=0,xmx=360,ymn=-90,ymx=90)
rz <- raster::raster(0.0 * var,xmn=0,xmx=360,ymn=-90,ymx=90)
values(rz) <- rep(0,ncell(rz))
raster::crs(rz) <- "+proj=longlat +ellps=GRS80 +no_defs"
}

for(i in 1:length(file)){
cat(paste0("from ",file[i]),"x",sprintf("%02.2f",coef[i]),"\n")
ed <- ncdf4::nc_open(file[i])
name <- names(ed$var)
var <- ncdf4::ncvar_get(ed,name[1])
var <- ncdf4::ncvar_get(ed,name)
if(as_raster){
var <- apply(var,1,rev)
r <- raster::raster(x = 1000 * var,xmn=0,xmx=360,ymn=-90,ymx=90)
Expand All @@ -80,40 +141,33 @@ read <- function(file = file.choose(), coef = rep(1,length(file)), spec = NULL,
varall <- varall + var * coef[i]
}
}
if(as_raster){
}

if(is.null(spec)){
return(raster::rotate(rz))
}else{
if(verbose) cat("using the following speciation:\n") # nocov start
rz_spec <- list()
for(i in 1:length(spec)){
if(verbose) cat(paste0(names(spec)[i]," = ",spec[i],"\n"))
rz_spec[[i]] <- raster::rotate(rz * spec[i])
}
names(rz_spec) <- names(spec)
return(rz_spec) # nocov end
if(as_raster){
if(is.null(spec)){
return(raster::rotate(rz))
}else{
if(verbose) cat("using the following speciation:\n") # nocov start
rz_spec <- list()
for(i in 1:length(spec)){
if(verbose) cat(paste0(names(spec)[i]," = ",spec[i],"\n"))
rz_spec[[i]] <- raster::rotate(rz * spec[i])
}
names(rz_spec) <- names(spec)
return(rz_spec) # nocov end
}
}else{
if(is.null(spec)){
return(varall)
}else{
if(is.null(spec)){
return(varall)
}else{
if(verbose) cat("using the following speciation:\n") # nocov start
var_spec <- list()
for(i in 1:length(spec)){
if(verbose) cat(paste0(names(spec)[i]," = ",spec[i],"\n"))
var_spec[[i]] <- varall * spec[i]
}
names(var_spec) <- names(spec)
return(var_spec) # nocov end
if(verbose) cat("using the following speciation:\n") # nocov start
var_spec <- list()
for(i in 1:length(spec)){
if(verbose) cat(paste0(names(spec)[i]," = ",spec[i],"\n"))
var_spec[[i]] <- varall * spec[i]
}
names(var_spec) <- names(spec)
return(var_spec) # nocov end
}
}
# if(version == "osm"){
# cat(paste("reading osm data from",file,"\n"))
# roads <- osmar::get_osm(osmar::complete_file(),source = osmar::osmsource_file(file))
# road_lines <- osmar::as_sp(roads,what = "lines")
# roads <- sf::st_as_sf(road_lines)
# return(roads)
# }
}
39 changes: 32 additions & 7 deletions docs/reference/read.html

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

29 changes: 24 additions & 5 deletions man/read.Rd

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

0 comments on commit c897ead

Please sign in to comment.