README.md

title: " ukghg: spatio-temporal modelling of anthropogenic and biogenic fluxes of greenhouse gases in the UK" author: "Peter Levy" date: "2020-03-30"

output: rmarkdown::html_vignette

output: html_document: keep_md: yes vignette: > %\VignetteIndexEntry{use ukghg} %\VignetteEngine{knitr::rmarkdown} %\usepackage[utf8]{inputenc}

R-CMD-check

Introduction

This document describes the ukghg R package for producing spatio-temporal predictions of UK GHG emissions for the period 1990 to near-present. Most users will only use a single function calcFlux, which returns a time-series of maps, given a date-time stamp as input (i.e. a 3D data cube). The package uses the data structures provided by the raster package (https://CRAN.R-project.org/package=raster), but output can be written to generic netCDF files for use in other programs.

Installation

The package can be installed directly from a bundled .tar.gz file. Alternatively, and to keep versions up to date more easily, the package can be installed directly from GitHub, using install_github from devtools. However, this is slightly more complicated than usual because there are some large files to install, and install_github does not work with the standard GitHub approach to this (Git LFS). Instead we use the piggyback package to retrieve the large files. This is achieved in the code chunk below.

# If not already installed:
# install.packages(c("devtools", "piggyback"))
library(devtools)  # for install_github
library(piggyback) # for binary files from github

# install ukghg from github
install_github("NERC-CEH/ukghg")
# find where this has been installed:
pkgpath <- find.package("ukghg")
# and download the large binary files into here from github:
pb_download(repo = "NERC-CEH/ukghg",
            tag = "v0.7.2", dest = pkgpath) # update version no. as necessary

Using the package

Now we can load the package.

library(ukghg)

One way to use it is via a web interface (a Shiny app), which opens as a web page in your browser. You start this by typing:

runShinyApp()

at the R command line. Alternatively, you can call the ukghg functions directly from the R command line, as described below. The basic input is a sequence of time points, in POSIXct format. You can specify these any way you want, but a simple sequence with regular intervals between two points is created with the following lines.

# create a sequence of 2-hourly time points over one day
startDate <- as.POSIXct(strptime("01/07/2016", "%d/%m/%Y"), tz = "UTC")
endDate   <- as.POSIXct(strptime("02/07/2016", "%d/%m/%Y"), tz = "UTC")
nTimes <- 13  # number of time points
datect <- seq(startDate, endDate, length = nTimes)
datect
##  [1] "2016-07-01 00:00:00 UTC" "2016-07-01 02:00:00 UTC"
##  [3] "2016-07-01 04:00:00 UTC" "2016-07-01 06:00:00 UTC"
##  [5] "2016-07-01 08:00:00 UTC" "2016-07-01 10:00:00 UTC"
##  [7] "2016-07-01 12:00:00 UTC" "2016-07-01 14:00:00 UTC"
##  [9] "2016-07-01 16:00:00 UTC" "2016-07-01 18:00:00 UTC"
## [11] "2016-07-01 20:00:00 UTC" "2016-07-01 22:00:00 UTC"
## [13] "2016-07-02 00:00:00 UTC"

Now we can use the calcFlux function to output flux maps for these times. At a minumum, you need to specify:

For example, to predict diurnal carbon dioxide flux on 1st July 2016 on a 20-km grid in $\mu$mol CO$_2$ m^-2^ s^-1^, we would use:

flux_co2 <- calcFlux("co2", datect, res = "20", unitType = "mol", unitSIprefix = "micro")

To predict methane on a 100-km grid in nmol CH$_4$ m^-2^ s^-1^, we would use:

flux_ch4 <- calcFlux("ch4", datect, res = "100", unitType = "mol", unitSIprefix = "nano")

To predict monthly ethane emission over four years on a 100-km grid in pg C$_2$H$_6$ m^-2^ s^-1^, ignoring day-of-the-week and diurnal variation, we would use:

# create a sequence of monthly time points over four years
startDate <- as.POSIXct(strptime("01/01/2013", "%d/%m/%Y"), tz = "UTC")
endDate   <- as.POSIXct(strptime("01/12/2016", "%d/%m/%Y"), tz = "UTC")
nTimes <- 12*4  # number of time points
datect <- seq(startDate, endDate, length = nTimes)

system.time(
  flux_c2h6 <- calcFlux("c2h6", datect, res = "100", unitType = "g", unitSIprefix = "pico",
    timeScales = c(TRUE, TRUE, FALSE, FALSE))
)
##    user  system elapsed 
##   13.14    3.23   21.09

We can plot the resulting time series and raster maps in several ways, most simply with base R graphics e.g.

plot(datect, flux_c2h6$total, ylab = "Ethane flux (Tg/y)", type = "b")

names(flux_co2$ls_ghgByTimeBySector[[4]]) <- c(sectorLongName, "Natural")
plot(flux_co2$s_ghgTotal[[7]], 
main = "CO2 flux at 2016-07-01 12:00, umol/m2/s")

Other options

Further options allow you to specify the geographic projection of output rasters, the option to write to netCDF files, and which sectors to include. Variation on four different temporal scales is included by default, but this can be restricted with the timeScales parameter.

Output

The object returned by the calcFlux function provides several aggregations of the data:



NERC-CEH/ukghg documentation built on March 31, 2022, 3:16 a.m.