## ----setup, include = FALSE---------------------------------------------------
knitr::opts_chunk$set(collapse = TRUE, comment = "#>")
library(nestedscales)

## ----carriers-----------------------------------------------------------------
carriers <- read_scale("carriers")
carriers
scale_units(carriers, "carrier")
scale_children(carriers, "carrier", "FossilCoal")

## ----lhv----------------------------------------------------------------------
lhv <- scale_attributes(carriers, "lhv", frame = "fuel")
lhv[!is.na(lhv$value), c("unit", "value", "unit_of_measure", "source")]

## ----mix----------------------------------------------------------------------
coal <- filter_scale(carriers, "carrier", "FossilCoal")
lt <- scale_leaftable(coal)
lt$tj <- c(100, 300, 50, 400, 20, 0)
mix <- scale_from_leaftable(
  lt, frames = c("group", "carrier", "fuel"), key = "fuel",
  attributes = scale_attributes(coal), name = "mix"
)
lhv_coal <- scale_attributes(mix, "lhv", frame = "fuel")
recast_scale(
  data.frame(fuel = lhv_coal$unit, lhv = lhv_coal$value), mix,
  source = "fuel", target = "carrier", rule = "weighted_mean", weight = "tj"
)

## ----inherit------------------------------------------------------------------
gas <- attach_attributes_scale(
  carriers,
  data.frame(frame = "carrier", unit = "FossilGas", attribute = "lhv",
             kind = "property", value = 46, unit_of_measure = "GJ/t"),
  source = "example"
)
res <- scale_attributes(gas, "lhv", frame = "fuel")
res[res$unit %in% scale_children(gas, "carrier", "FossilGas"),
    c("unit", "value", "source", "origin_frame", "origin_unit")]

## ----sources, error = TRUE----------------------------------------------------
try({
two <- attach_attributes_scale(
  carriers,
  data.frame(frame = "fuel", unit = "Anthracite", attribute = "lhv",
             kind = "property", value = 27.5, unit_of_measure = "GJ/t"),
  source = "other"
)
scale_attributes(two, "lhv", frame = "fuel")
other <- scale_attributes(two, "lhv", frame = "fuel", source = "other")
other[!is.na(other$value), c("unit", "value", "source")]
})

## ----convert------------------------------------------------------------------
use <- data.frame(
  fuel = c("Anthracite", "Lignite", "NaturalGas", "Biodiesel"),
  kt   = c(120, 900, 0, 15)
)
use$t <- use$kt * 1e3                 # kt -> t: no prefix arithmetic is done
energy <- convert_scale(use[c("fuel", "t")], carriers, "lhv",
                        from_unit = "t", to_unit = "GJ")
energy

## ----invert-------------------------------------------------------------------
convert_scale(energy[c("fuel", "t")], carriers, "lhv",
              from_unit = "GJ", to_unit = "t")

## ----recast-------------------------------------------------------------------
tj <- data.frame(fuel = energy$fuel, tj = energy$t / 1e3)   # GJ -> TJ
register_scale_rule("tj", "sum")
by_carrier <- recast_scale(tj, carriers, source = "fuel", target = "carrier",
                           missing_sources = "ignore")
by_carrier[by_carrier$tj > 0, ]
sum(by_carrier$tj, na.rm = TRUE) == sum(tj$tj)

## ----emissions----------------------------------------------------------------
co2 <- convert_scale(tj, carriers, "ef_co2", from_unit = "TJ", to_unit = "kg")
names(co2)[names(co2) == "tj"] <- "co2_kg"
co2

## ----emissions-inverse, error = TRUE------------------------------------------
try({
convert_scale(tj, carriers, "ef_co2", from_unit = "kg", to_unit = "TJ")
})

## ----ipcc---------------------------------------------------------------------
ipcc <- read.csv(system.file("extdata", "ipcc2006_fuel_defaults.csv",
                             package = "nestedscales"))
table(ipcc$attribute, ipcc$mapping_status)
review <- ipcc[ipcc$mapping_status == "review" & ipcc$attribute == "lhv", ]
review[c("ipcc_fuel", "unit", "value")]

## ----plant--------------------------------------------------------------------
mine <- attach_attributes_scale(
  carriers,
  data.frame(frame = "fuel", unit = "Lignite", attribute = "lhv",
             kind = "property", value = 8.4, unit_of_measure = "GJ/t"),
  source = "plant"
)
plant <- scale_attributes(mine, "lhv", frame = "fuel", source = "plant")
plant[!is.na(plant$value), c("unit", "value", "source")]

## ----match--------------------------------------------------------------------
scale_match(carriers, c("GAS", "LIG", "ELC", "Hydrogen"))
scale_match(carriers, "Hydrogen", frame = "carrier")

## ----join---------------------------------------------------------------------
join_scale(use[c("fuel", "kt")], carriers, frame = "fuel", attach = TRUE)

## ----cleanup, include = FALSE-------------------------------------------------
clear_scale_rules()

