R/mts_setTimeAxis.R
mts_setTimeAxis.Rd
Extends or contracts the time range of an mts object by adding/removing time steps at the start and end and filling any new time steps with missing values. The resulting time axis is guaranteed to be a regular, hourly axis with no gaps using the same timezone as the incoming mts object. This is useful when you want to place separate mts objects on the same time axis for plotting.
Dates can be anything that is understood by MazamaCoreUtils::parseDatetime()
including either of the following recommended formats:
"YYYYmmdd"
"YYYY-mm-dd"
Timezone determination precedence assumes that if you are passing in
POSIXct
values then you know what you are doing:
get timezone from startdate
if it is POSIXct
use passed in timezone
get timezone from mts
If either startdate
or enddate
is missing, the start or end of
the timeseries in mts
will be used.
If neither startdate
nor enddate
is a POSIXct
value
AND no timezone
is supplied, the timezone will be inferred from
the most common timezone found in mts
.
mts_setTimeAxis(mts = NULL, startdate = NULL, enddate = NULL, timezone = NULL)
mts object.
Desired start date (ISO 8601).
Desired end date (ISO 8601).
Olson timezone used to interpret startdate
and enddate
.
The incoming mts time series object defined on a new time axis.
(A list with meta
and data
dataframes.)
library(MazamaTimeSeries)
# Default range
range(example_mts$data$datetime)
#> [1] "2019-07-01 07:00:00 UTC" "2019-07-08 06:00:00 UTC"
# One-sided extend with user specified timezone
example_mts %>%
mts_setTimeAxis(enddate = 20190815, timezone = "UTC") %>%
mts_extractData() %>%
dplyr::pull(datetime) %>%
range()
#> [1] "2019-07-01 07:00:00 UTC" "2019-08-15 00:00:00 UTC"
# Two-sided extend with user specified timezone
example_mts %>%
mts_setTimeAxis(20190615, 20190815, timezone = "UTC") %>%
mts_extractData() %>%
dplyr::pull(datetime) %>%
range()
#> [1] "2019-06-15 UTC" "2019-08-15 UTC"
# Two-sided extend without timezone (uses timezone from mts$meta$timezone)
example_mts %>%
mts_setTimeAxis(20190615, 20190815) %>%
mts_extractData() %>%
dplyr::pull(datetime) %>%
range()
#> [1] "2019-06-15 07:00:00 UTC" "2019-08-15 07:00:00 UTC"