From c897ead5f0ab03f59915d77aa054e0da303c8103 Mon Sep 17 00:00:00 2001 From: Schuch666 Date: Wed, 21 Nov 2018 17:03:16 -0200 Subject: [PATCH] added 'MACCITY' to read --- R/read.R | 134 +++++++++++++++++++++++++++------------ docs/reference/read.html | 39 ++++++++++-- man/read.Rd | 29 +++++++-- 3 files changed, 150 insertions(+), 52 deletions(-) diff --git a/R/read.R b/R/read.R index 1c62aab..ab68cfa 100644 --- a/R/read.R +++ b/R/read.R @@ -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 #' @@ -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="")) @@ -38,7 +54,7 @@ #'} 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 @@ -46,29 +62,74 @@ read <- function(file = file.choose(), coef = rep(1,length(file)), spec = NULL, 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) @@ -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) - # } } diff --git a/docs/reference/read.html b/docs/reference/read.html index 6439c02..2b84a81 100644 --- a/docs/reference/read.html +++ b/docs/reference/read.html @@ -32,8 +32,8 @@ - + @@ -103,13 +103,14 @@

Read NetCDF data from global inventaries

-

Read data from global inventories, can read several files and merge into one emission -and/or split into several species (speciation process)

+

Read data from global inventories, can read several files and merge into one +emission and/or split into several species (speciation process)

read(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)

Arguments

@@ -128,7 +129,15 @@

Arg

- + + + + + + + + + @@ -142,12 +151,26 @@

Arg

Source

-

Read abbout EDGAR at http://edgar.jrc.ec.europa.eu

+

Read abbout EDGAR at http://edgar.jrc.ec.europa.eu and MACCITY at +http://accent.aero.jussieu.fr/MACC_metadata.php

Value

Matrix or raster

+

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.

+

See also

rasterSource and gridInfo

@@ -166,6 +189,8 @@

Contents

  • Value
  • +
  • References
  • +
  • See also
  • Examples
  • diff --git a/man/read.Rd b/man/read.Rd index cf4e8c1..fd7f72a 100644 --- a/man/read.Rd +++ b/man/read.Rd @@ -4,11 +4,13 @@ \alias{read} \title{Read NetCDF data from global inventaries} \source{ -Read abbout EDGAR at http://edgar.jrc.ec.europa.eu +Read abbout EDGAR at http://edgar.jrc.ec.europa.eu and MACCITY at +http://accent.aero.jussieu.fr/MACC_metadata.php } \usage{ read(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) } \arguments{ \item{file}{file name or names (variables are summed)} @@ -17,7 +19,11 @@ read(file = file.choose(), coef = rep(1, length(file)), spec = NULL, \item{spec}{numeric speciation vector to split emission into different species} -\item{version}{inventory name and verion} +\item{version}{inventory name 'EDGAR' or 'MACCITY'} + +\item{month}{the desired month of the inventary (MACCITY)} + +\item{categories}{considered categories (MACCITY variable names), empty for all} \item{as_raster}{return a raster (defoult) or matrix (with units)} @@ -27,8 +33,8 @@ read(file = file.choose(), coef = rep(1, length(file)), spec = NULL, Matrix or raster } \description{ -Read data from global inventories, can read several files and merge into one emission -and/or split into several species (speciation process) +Read data from global inventories, can read several files and merge into one +emission and/or split into several species (speciation process) } \examples{ \donttest{ @@ -44,6 +50,19 @@ image(nox_d1, main = "NOx emissions from transport from EDGAR 3.4.1 for d1") image(nox_d2, main = "NOx emissions from transport from EDGAR 3.4.1 for d2") } } +\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. +} \seealso{ \code{\link{rasterSource}} and \code{\link{gridInfo}}
    version

    inventory name and verion

    inventory name 'EDGAR' or 'MACCITY'

    month

    the desired month of the inventary (MACCITY)

    categories

    considered categories (MACCITY variable names), empty for all

    as_raster