R/emis3d.R

Defines functions emis3d

Documented in emis3d

#' Estimation of emissions resulting in a 3D EmissionsArray
#'
#' @description \code{emis3d} emissions
#'
#' @param veh data.frame or matrix similar to object class "Vehicles"
#' @param lkm Numeric, with length equal to number of rows of veh
#' @param ef EmissionFactor with mode double, with length equal to number of columns of veh,
#' @param profile data.frame with rows as 24 hours and columnas as each dayfor extrapolating traffic.
#' It can be also a numeric vector.
#' @useDynLib  forvein, .registration = TRUE
#' @export
#' @examples \dontrun{
#' #do not run
#' library(vein)
#' data(net)
#' data(pc_profile)
#' data(fkm)
#' PC_G <- c(33491,22340,24818,31808,46458,28574,24856,28972,37818,49050,87923,
#'          133833,138441,142682,171029,151048,115228,98664,126444,101027,
#'          84771,55864,36306,21079,20138,17439, 7854,2215,656,1262,476,512,
#'          1181, 4991, 3711, 5653, 7039, 5839, 4257,3824, 3068)
#' pc1 <- my_age(x = net$ldv, y = PC_G, name = "PC", agemax = 41)
#' lef <- ef_cetesb("CO", "PC_G", agemax = 41)
#' system.time(E_CO <- emis(veh = pc1,
#'                          lkm = net$lkm,
#'                          ef = lef,
#'                          profile = pc_profile) )
#' system.time(  E_COs <- emis(veh = pc1,
#'                             lkm = net$lkm,
#'                             ef = lef,
#'                             profile = pc_profile,
#'                             simplify = T) )
#' system.time( df <- emis3d(veh = pc1,
#'                           lkm = net$lkm,
#'                           ef = lef,
#'                           profile = pc_profile) )
#' veh <- rep(1, 4)
#' lkm = rep(1, 2)
#' ef = c(1, 2)
#' profile = rep(1, 2)
#' emis3d(veh = veh, lkm = lkm, ef = ef, profile = profile)
#' }
emis3d <- function(veh,
                   lkm,
                   ef,
                   profile) {
  veh <- as.matrix(veh)
  lkm <- as.numeric(lkm)
  ef <- as.numeric(ef)
  profile = as.numeric(unlist(profile))
  nrowv = as.integer(nrow(veh))
  ncolv = as.integer(ncol(veh))
  prok = as.integer(length(unlist(profile)))

  a <-   .Fortran("emis3df",
                  nrowv = nrowv,
                  ncolv = ncolv,
                  prok = prok,
                  veh = veh,
                  lkm = lkm,
                  ef = ef,
                  pro = profile,
                  emis = numeric(nrowv*ncolv*prok))$emis
  e <- array(a, dim = c(nrowv, ncolv,prok))
  return(e)
}
ibarraespinosa/forvein documentation built on Dec. 23, 2019, 7:31 p.m.