
\subsection{Emission estimation}

After inputting the database of vehicles and their respective emission factors, VEIN is ready to use the \textit{emis} function. The VEIN package counts with several \textit{emis} functions according to the type of emission being estimated. The \textit{emis} function assembles data and outputs from other VEIN functions, and estimates the emissions for the number of hours and days in the week. This function reads the morning rush hour traffic data by age of vehicle use and extrapolates it with the profile data-frame, as previously explained. 
It reads the emission factors stored in a list with length matching the age distribution of the vehicle category and then reads the list of speeds. This function returns the emissions at each street in an array with 4 dimensions: 1) number of streets;
2) max age of age distribution; 3) hours (usually 24); and 4) days (usually 7). For convenience, there are defined default values for this function hour = 24, day = 7 and array = TRUE. The values can be changed accordingly.

For example, the estimation of the traffic simulations shown in Fig. \ref{fig:ldv} has 34,733 streets, fleet of 41 years age distribution, 24 hours of the day, and 7 days of the week. Therefore, it will produce an emissions array with dimensions 34,733, 41, 24 and 7. 
The vehicle fleet used to produce the age distribution is shown in green in Fig. \ref{fig:Figura1}(a) and it has 41 years of length. 

	
	\begin{figure}[H]
		\centering
		\includegraphics[width=17.5cm]{figures/Figura1.png}
		\caption{(a) Distribution of LDV composition by age of use, (b) temporal factor for expanding morning rush hour traffic data  and (c) CO emission factors used in the estimation presented in this manuscript.}
    \label{fig:Figura1}
    \end{figure}
	
    
	\begin{verbatim}
	data(pc_profile)
E_CO <- emis(veh = pc1, lkm = net$lkm, ef = lef, speed = speed, profile = pc_profile)

	\end{verbatim}
	This emissions array output for 34,733 streets and vehicle fleet with 41 years age distribution, 24 hours and 7 days of the week, has the size of 1.8 Gb. 
    Hence, it is recommended to use the function \textit{emis\_post} and then delete the original emissions array. The arguments include: the emissions array, type of vehicle, size or weight, fuel, pollutant, and the boolean argument \textit{by}. The \textit{emis\_post} function was created to preserve the most important information in the emissions array, to use less memory size and to be compatible with the packages \textbf{sp} \citep{PebesmaBivand2005} and \textbf{ggplot2} \citep{ggplot2}. VEIN outputs could also be used with the package \textbf{openair} \citep{carslaw2012openair}. \textit{emis\_post} returns a data-frame, but the argument \textit{by} determines the shape of the data-frame. 
    When \textit{by} has the value 'veh', it returns a data-frame with an aggregation of the emissions array by each vehicle's age of use with columns: vehicle name, emission (in grams), vehicle type, size, fuel, pollutant, age, hour and day. This output allows the user to visualize hourly emissions at each day of the week, as shown in Fig. \ref{fig:emis_hour}. Higher emissions are found at morning and evening rush hours from Monday to Friday. Saturday has peak higher emissions at noon and Sunday has the lowest emissions.
	
    VEIN enables the user to identify which type of vehicle emits more by age of use. This is particularly useful for environmental authorities who aim to reduce local traffic emissions and restrict the circulation of high-emitting vehicles. Fig. \ref{fig:emis_hour} shows the CO emissions of gasoline fueled LDV by the vehicle's age of use. The average age of these vehicles is 15.17 years, as shown by the green curve in Fig. \ref{fig:Figura1} (a). The total number of vehicles is 24708767 $\mathrm{veh \cdot h^{-1}}$ (8:00-9:00 LT on a Monday). 
    The total CO emissions is 233095 $\mathrm{t \cdot y^{-1}}$, considering a year of 52 weeks, but the emissions are concentrated for the LDV between 20 and 23 years of use. The vehicles in this age interval represent 14.76\% of the fleet, emitting 63.79\% (148712 $\mathrm{t \cdot y^{-1}}$) of the total emissions. In other words, 15\% of the fleet emit more than  60\% of the CO.
    Between 1992 and 1996, the emissions standard was Proconve L2, equivalence with Euro 1 (see Table \ref{tab:euro}), and also the introduction of the catalytic system in Brazil. Therefore, the high emissions are due to vehicles with a deteriorated catalytic converter. 
    This information is useful for reducing air pollution, thus supporting the aims of environmental planners and local authorities.
	\begin{figure}[H]
		\centering
		\includegraphics[width=17cm]{figures/Figura3.png}
		\caption{(a) CO Emissions ($\mathrm{g \cdot h^{-1}}$) per hour of the day and day of the week (colored and shaped lines) for LDV from MASP, (b) CO Emissions ($\mathrm{t \cdot y^{-1}}$) according to the age of use of the LDV from MASP.}
		\label{fig:emis_hour}
	\end{figure}

	
\subsection{Post estimation}
\label{pe}
    
	The spatial dimensions of the emissions estimation is an important feature of VEIN because it allows the representation of the streets into spatial vectors. This is accomplished by using the function \textit{emis\_post} with the argument \textit{by} equal to 'streets\_narrow' or 'streets\_wide'. Both options return a data-frame with different characteristics, which can be converted into spatial vectors. When \textit{by} is equal to 'streets\_narrow', it returns a data-frame with four columns: id, indicating the number of rows, emissions, hour and day of the week. The number of rows in the data-frame is the original number of selected streets multiplied by the hours and days of the week. For example, when there are 34733 streets, 24 hours and 7 days of the week, it returns a data-frame with 5835144 rows having a size of 133.6 Mb. This option is useful to visualize the temporal behavior of specific streets with \textbf{ggplot2} \citep{ggplot2} or \textbf{ggmap} \citep{ggmap}, for instance. 
    
	In most cases, users will be particularly interested when the argument \textit{by} is equal to 'streets\_wide'. This produces a data-frame with number of rows matching number of streets for the domain and number of columns as hours. Fig. \ref{fig:emis_street} shows the CO emissions for LDV at each street on a Friday at 19:00 LT. The following code shows how to produce hourly emissions by street and then add these emissions back into the {\tt SpatialLinesDataFrame} net. This is possible because the number of rows in {\tt E\_CO\_STREETS} is equal and it matches the number of rows in {\tt net}. 
	
	\begin{verbatim}
E_CO_STREETS <- emis_post(arra = E_CO, pollutant = "CO", by = "streets_wide")
net@data <- cbind(net@data, E_CO_STREETS)

	\end{verbatim}
	
	The emissions shown in Fig. \ref{fig:emis_street} are concentrated in two streets, a motorway and a trunk street at the northern part of the emissions map. This image was generated with the function \textbf{spplot} in the package \textbf{sp}.%, as explained in the supplementary material.  
	\begin{figure}[H]
		\centering
		\includegraphics[width=15cm]{figures/emis_street.png}
		\caption{CO Emissions (colored lines; $ \mathrm{g \cdot h^{-1}}$) for LDV at Friday 19:00 LT over MASP.}
		\label{fig:emis_street}
	\end{figure}
It also shows a rectangular grid, which can be used for allocating the emissions. 
    The allocation of emissions into the grid is very important for visualization and for inputs to air quality models. We included a simple function to create a rectangular grid in VEIN. The function was named, \textit{make\_grid}, which has the arguments, \textit{width}, \textit{height} and a boolean argument \textit{polygon} for determining the type of output. When the argument \textit{polygon} is TRUE, it returns a {\tt SpatialPolygonsDataFrame}, and when it is FALSE, it returns a {\tt SpatialGridDataFrame}. The units of 'width' and 'height' depend on the coordinate reference systems of the data. 
    
	The allocation of emissions in each grid cell is produced by a spatial interception between the emissions at each street and the polygon-grid. Firstly, the {\tt SpatialLinesDataFrame} object with emission must contain a column with the length of the street. The length is calculated with the function \textit{gLength} in the package \textbf{rgeos} \citep{BivandRundel2016}. Secondly, it is performed at the intersection between the {\tt SpatialLinesDataFrame} of emissions and the grid {\tt SpatialPolygonsDataFrame}. The intersection is performed by importing the function \textit{intersect} in the package \textbf{raster} \citep{raster}. The grid must have a column with the id for each cell. Thirdly, it calculates, in another column, the length of the street in the resulting {\tt SpatialPolygonsDataFrame}. Then it multiplies the emissions with the proportion of the new and old length of the street. This allows proportional emissions in each grid cell. Fourthly, it aggregates the emissions by id of grid and adds these emissions by grid id into the grid. The results are in emissions grid with format {\tt SpatialPolygonsDataFrame}. These calculations can be performed automatically by the function \textit{emis\_grid}. 
    
    The function \textit{make\_grid} is suitable in mid-size or small cities when the resolution is approximately 1 km. When dealing with larger cities and higher resolution, it is recommended to use other tools because \textit{make\_grid} would take up too much time. This difficulty will be overcome in a future version of VEIN with dependencies on package \textbf{sf} \citep{sf}. In the following code, we show the use of the function \textit{make\_grid} only for example purposes. It is recommended that function \textit{emis\_post} be used with the argument 'by = streets\_wide', in order to return a data-frame with hourly emissions for each street. This output can be used with the functions \textit{emis\_grid} to create an emissions grid map, as shown in Fig. \ref{fig:emis_grid}. This is helpful when the user plans to use the data to construct inputs for air quality models. The following code applies with the demo inside the VEIN model.

	\begin{verbatim}
g <- make_grid(spobj = net, width = 0.00976, height = 0.00976, polygon = T)
E_CO_g <- emis_grid(spobj = net, g = g,sr = "+init=epsg:31983", type = "lines")
\end{verbatim}

The Fig. \ref{fig:emis_grid} (a) shows the resulting emissions of CO %$kg \cdot m^{-2} \cdot s^{-1}$ 
in a grid with class {\tt SpatialPolygonsDataFrame} built for a grid with resolution of 1 km representing the base year 2015. This emissions grid was built using the package \textbf{sf} (process showed in the Appendix A). Fig. \ref{fig:emis_grid} (b) shows the CO emissions grid of road transport from EDGAR for the same area and base year 2010 \citep{edgar}, which is the latest available year.

	
\subsection{Speciation}

	Atmospheric simulations of ozone require knowledge about the VOC compounds and particulate matter speciation, which are necessary for solving the different chemical mechanisms. For example, a São Paulo study of ozone concentrations that used models BRAMS/SPM \citep{Freitasetal2005} and WRF/Chem \citep{Grelletal2005}, involved detailed VOC speciation \citep{Andradeetal2015}. It is important to mention that there is evidence to prove that reducing Black Carbon emissions would help lower the Global Radiative Forcing and improve population health \citep{bc}. Hence, the speciation of emissions is important and VEIN provides this information. The VEIN function \textit{speciate} splits VOC and PM into their constituents. The arguments of these functions are: emissions estimation, type of speciation, type of vehicle, fuel and Euro Standard. There are four types of PM speciation: 'bcom' \textcolor{red}{ (15) because it splits PM in black carbon and organic matter} \citep{NtziachristosSamaras2016}, 'tyre', 'break', and 'road' \citep{NtziachristosBoulter2009}. However, there is currently only one type of VOC speciation for MASP, the 'iag' \citep{Rafee2015}. \textcolor{red}{ (15) The name of the speciation 'iag' comes from the initials of the Institute of Astronomy, Geophysics and Atmospheric Sciences of the University of São Paulo. The speciation 'iag' it is based on measurements made by this institute.}

\subsection{Input of atmospheric models}


Meteorological factors influence the chemical process of pollutants in the atmosphere. Therefore, their transport and behavior in the atmosphere must be predicted by a model that includes the meteorological components (\textcolor{red}{ (16) "in-line"} coupling of meteorology and chemistry), such as the Weather Research and Forecasting Chemistry model (WRF-Chem; \citealt{Grelletal2005}). This model has been widely used around the world since its inception (2005 to 2006). 

WRF-Chem, as another regional atmospheric model, requires a superficial layer of emission fluxes as input data. There are tools to assimilate top-down emissions inventories, such as EDGAR \citep{Olivieretal1996} and REanalysis of the TROpospheric chemical composition (RETRO; \citealt{Schultz2007}), using the software PREP-Chem \citep{Freitasetal2011}. These tools are very important to the modeling community, however, their spatial resolutions are very limited. VEIN includes functions to generate WRF-Chem inputs from the emissions grid with any desired resolution in the following way. VEIN estimates emissions of different pollutants at each street and also produces emissions grids needed to do the regional modeling. This is performed through the spatial intersection between emissions at streets and a polygon grid with the required resolution. The resulting grid has total emissions in each grid cell proportional to the length of the streets inside each cell. 

Fig. \ref{fig:emis_grid} shows a comparison for VEIN (Fig. \ref{fig:emis_grid}a) and EDGAR (Fig. \ref{fig:emis_grid}b), using emissions inventory for the CO in MASP. One can notice that CO is spatially well represented for VEIN, by comparison with EDGAR. Furthermore, VEIN offers much more details about the emission of this pollutant, which occurs mainly in the urban motorways due to the high volume of traffic in these roads. The total emissions of CO emissions using VEIN are 1.73e-06 ($\mathrm{kg \cdot m^{-2} \cdot s^{-1}}$), considering the first second of a typical Monday at 00:00 LT and EDGAR, 8.46e-08 ($\mathrm{kg \cdot m^{-2} \cdot s^{-1}}$). Therefore, VEIN estimates are 20.50 times higher than EDGAR. This difference could be higher if compared with the morning rush hour of VEIN. However, it is important to mention that the estimate with VEIN for this manuscript is illustrative, and that more detailed emissions inventories should be made when comparing to others. For example, the inventory for this manuscript includes estimates only for LDV assuming that all are PC. It does not include other types of vehicles as the total amount of vehicles were not calibrated with fuel consumption. \cite{NtziachristosSamaras2016} recommends to compare bottom-up estimates with fuel consumption in order to calibrate inputs of emissions inventory (traffic data in this case). These differences highlight the needs for development, inter-comparison and uncertainty evaluation of emission estimates. These results are very useful for many scientific and standardization purposes such as health effects in air pollution studies, urban planning and strategies to cut greenhouse gas emissions. 

	\begin{figure}[h]
		\centering
\includegraphics[width=18cm]{figures/Figura2.png}
		\caption{CO emissions ($\mathrm{kg \cdot m^{-2} \cdot s^{-1}}$) in MASP for (a) Monday at 00:00:00 LT, estimated with VEIN, and (b) the emissions of road transport for the same area from EDGAR.}
		\label{fig:emis_grid}
	\end{figure}

The VEIN model provides functions to transform the emissions grids into  inputs for the model Assimilating Anthropogenic Emissions (AAS4WRF) \citep{VaraVelaetal2015}. AAS4WRF consists of an NCL \citep{ncl} script that reads the wrfinput file and an emissions text file produced by VEIN to create a WRF-Chem input file. The VEIN model provides the function \textit{emis\_wrf} to automatically create a data-frame in the correct format with columns longitude, latitude, id of grid cell, pollutants, local time and GMT time in format POSIXct. 
 The arguments of \textit{emis\_wrf} are \textit{sdf}, which is a list of {\tt SpatialPolygonsDataFrames}, each per pollutant. \textit{nr} indicates how many times the hours of estimations will be repeated. \textit{dmyhm} indicates the day, month, year, hour and minute of the first hour of emissions in local time. \textit{tz} is the time-zone and \textit{utc} indicates the difference in hours between local and GMT time. \textit{islist} indicates if the first argument, \textit{df} is a list (islist = TRUE) or a data-frame (islist = FALSE). The output of VEIN emissions are in $g \cdot h^{-1}$, and in the following code chunk, emissions were converted to mol. 
 
    \begin{verbatim}
E_CO_g@data <- E_CO_g@data[, -1] / (12 + 16)
ldf <- list("co" = E_CO_g)
df_wrf <- emis_wrf(ldf, nr = 1, dmyhm = "04-08-2014 00:00", 
tz = "America/Sao_Paulo", utc = -3, islist = TRUE)
	\end{verbatim}
	
\textcolor{red}{ (17) The authors are currently developing another model called \textit{eixport} \citep{eixport} for producing emission inputs to other models and it is currently implemented the generation of WRF-Chem input files inside R (see https://github.com/ibarraespinosa/eixport). Other models considered are BRAMS-SPM \citep{Freitasetal2005}, R-LINE \citep{snyder2013rline} and more. }

%\subsubsection{HEADING}
%TEXT