
Build a Calendar from a flat table of leaf timeslices
Source:R/calendar-construct.R
calendar_from_leaftable.RdThis is the most general way to construct a Calendar: provide the leaf
timeslices directly as a data.frame, name the timeframe columns, and
optionally pin down the per-timeframe vocabulary and model-level metadata.
Arguments
- leaftable
A
data.framewith one row per leaf timeslice. Must contain:one column per timeframe named in
timeframes, in either form described belowshare— numeric > 0, one unit of the timeslice's timeframe as a fraction of the year: the unit declared inunits, or the timeslice's own length(optional)
width— numeric > 0, the length of the timeslice on the time axis in those units; 1 when absent(optional)
weight— numeric >= 0, the number of times the timeslice repeats; 1 when absent; 0 marks a timeslice that is kept in the calendar and left out of the samplea
totalcolumn, when present, is recomputed asshare x width x weight(optional, bare labels only)
timeslice— unique character ID of each leaf
- timeframes
Ordered character vector of timeframe names (coarsest first). Each must appear as a column in
leaftable.- members
Optional named list giving the full ordered set of values per timeframe column (members, or bare labels in that form); it fixes the member order. If
NULL, derived fromunique(leaftable[[tf]])in first-appearance order.- name, desc
Calendar name and free-text description.
- year_start
list(month = , day = ); defaults to January 1.- utc_offset_minutes
Integer minutes; model-local time = UTC + offset.
- year_fraction
Fraction of a year the timeslices stand for together (the sum of
share x width x weight). Defaults to1.- row_order
"nested"(default) sorts the rows by timeframe, coarsest first, each in its member order (for a calendar, chronological order);"given"keeps the rows as supplied.- layout
How the timeframe columns are read:
"auto"(default),"nested"or"bare"; see the section below.- units
Optional named numeric vector (or list): the length of one unit of a timeframe, in hours, by timeframe name (
c(HOUR = 1)). Kept inmeta$units.- ...
Additional named entries appended to
meta(free-form).
Value
A Calendar object.
Two forms of the table
The timeframe columns are read in one of two forms.
Members (
layout = "nested"). Each timeframe column holds the member of its row at that timeframe:MONTH=m01,HOUR=m01_h00. The finest column identifies the rows. This is the layout aCalendarstores, and whatcalendar_leaftable()returns. A column whose codes cross-cut the others (a grouping added on top) is kept as given.Bare labels (
layout = "bare"). Each timeframe column holds the label of its row inside the parent:MONTH=m01,HOUR=h00, repeated under every month. The members are composed from them as the_-joined path of the row (m01_h00). Atimeslicecolumn, when present, names the leaf timeslices instead; the labels of the finest timeframe are then kept inmeta$member_labels, since they cannot be read off those names.
layout = "auto" (default) reads the table as bare labels when it has a
timeslice column or when the finest timeframe column does not identify
the rows, and as members otherwise. A table of bare labels whose finest
column happens to be unique (quarters and months, say) is therefore taken
as members unless layout = "bare" is given.
Share, width, weight and units
The time a leaf timeslice stands for is share x width x weight, and
over the timeslices of positive weight it sums to year_fraction (see
the section on the three numbers in Calendar). A table that gives
share alone describes timeslices that are their own unit, once each:
the share is the timeslice's own length. units declares the unit of a
timeframe in hours; the share of every timeslice of the finest timeframe
is then that unit, width counts units and weight repetitions. A
timeframe may be given any name, with one rule: a timeslice of HOUR is
one hour long (share 1/8760 in a 365-day year), declared or not. An hour
of an average day is such an hour with the days it stands for as its
weight; a table that puts those days into the share is an error.
Examples
# A monthly calendar: each month is its own unit (non-leap year)
df <- data.frame(
MONTH = sprintf("m%02d", 1:12),
share = c(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31) / 365
)
cal <- calendar_from_leaftable(df, timeframes = "MONTH", name = "m12")
cal
#> Calendar: m12
#> Timeframes (1):
#> - MONTH (12)
#> Leaf timeslices: 12
#> year_fraction: 1
#> year_start: month=1, day=1
#> utc_offset_minutes: 0
# Bare labels: two seasons by day and night
bare <- data.frame(
SEASON = rep(c("WIN", "SUM"), each = 2),
PART = rep(c("DAY", "NIGHT"), 2),
share = 0.25
)
sn <- calendar_from_leaftable(bare, c("SEASON", "PART"), name = "s2_p2")
calendar_timeslices(sn)
#> [1] "WIN_DAY" "WIN_NIGHT" "SUM_DAY" "SUM_NIGHT"
# The same months counted in hours: the unit is declared, width counts it
hrs <- data.frame(
MONTH = sprintf("m%02d", 1:12),
share = 1 / 8760,
width = 24 * c(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31)
)
cal_h <- calendar_from_leaftable(hrs, "MONTH", name = "m12",
units = c(MONTH = 1))
all.equal(calendar_leaftable(cal_h)$total, calendar_leaftable(cal)$total)
#> [1] TRUE
# An average day of two seasons: one hour, repeated on every day
avg <- data.frame(
SEASON = rep(c("COLD", "WARM"), each = 24),
HOUR = rep(sprintf("h%02d", 0:23), 2),
share = 1 / 8760,
weight = rep(c(182, 183), each = 24)
)
calendar_width(calendar_from_leaftable(avg, c("SEASON", "HOUR"),
name = "s2_h24"))[c(1, 25), ]
#> HOUR unit_hours width weight hours
#> 1 COLD_h00 1 1 182 182
#> 25 WARM_h00 1 1 183 183
# The same calendar from its members
identical(
calendar_timeslices(calendar_from_leaftable(
calendar_leaftable(sn), c("SEASON", "PART"), name = "s2_p2")),
calendar_timeslices(sn)
)
#> [1] TRUE