expand_by_hour: Expand a dataframe with non-integer hours into one with...

View source: R/extract_helpers.R

expand_by_hourR Documentation

Expand a dataframe with non-integer hours into one with surrounding integer hours

Description

This function is designed to facilitate the extraction of FVCOM predictions at times that are not integer hours (the temporal resolution at which predictions are made). To this end, this function expands a dataframe with non-integer hours to include both surrounding integer hours. The resultant dataframe can be passed extract (outside of this function) to extract predictions for these integer hours. Predictions can then be interpolated to the non-integer hours of interest via shrink_by_hour.

Usage

expand_by_hour(dat)

Arguments

dat

A dataframe that defines the FVCOM array date names, hours, layers (if applicable) and mesh cell IDs for which FVCOM predictions are desired (see extract). However, unlike the direct implementation of extract, the 'hours' column should be named 'hour_dbl' to reflect the fact that predictions are desired at non-integer hours. Strictly speaking, only the 'hour_dbl' column is required for this function, but it is anticipated that this function will be implemented prior to extract and thus the dataframe will contain all other columns required for that function.

Value

The function returns a dataframe, as inputted, but in which any rows with a non-integer 'hour_dbl' value have been expanded into two rows, with the hour before and the hour after the non-integer hour respectively defined in a new column 'hour' (as required for extract). A column 'order' is also added; this provides a unique identifier for each original row.

Author(s)

Edward Lavender

Examples

#### Define example data for which FVCOM predictions are desired
# Times, including non integer hours
timestamp <- as.POSIXct(c("2016-03-01 00:00:00",
                          "2016-03-02 00:02:00",
                          "2016-03-01 00:00:00"))
dat <- data.frame(timestamp = timestamp)
# Define columns required to extract FVCOM predictions (see fvcom.tbx::extract())
dat$hour_dbl <- lubridate::hour(dat$timestamp) +
                lubridate::minute(dat$timestamp)/60 +
                lubridate::second(dat$timestamp)/3600
dat$date_name <- date_name(dat$timestamp)
dat$mesh_ID <- as.numeric(as.character(dat_mesh_around_nodes$ID))[1]

#### Implement expand_by_hour()
dat <- expand_by_hour(dat)
# Now fvcom.tbx::extract() can be implemented to extract predictions
# And then predictions at integer hours can be interpolated to the original timestamps.

edwardlavender/fvcom.tbx documentation built on Nov. 26, 2022, 10:28 p.m.