diff --git a/NAMESPACE b/NAMESPACE index 06e36e18..478e9c23 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -1,6 +1,9 @@ # Generated by roxygen2: do not edit by hand export(annual_stats) +export(fasstr_add_date_vars) +export(fasstr_add_rolling_means) +export(fasstr_fill_missing_dates) export(longterm.stats) import(ggplot2) import(scales) diff --git a/R/fasstr_add_date_vars.R b/R/fasstr_add_date_vars.R new file mode 100644 index 00000000..87fd084d --- /dev/null +++ b/R/fasstr_add_date_vars.R @@ -0,0 +1,118 @@ +# Copyright 2017 Province of British Columbia +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and limitations under the License. + +#' @title Add calendar and water year date variables. +#' +#' @description Adds mulitple date variables to a dataframe from a column of dates, including +#' year, month (numeric and text), day of years, and water years and day of water years. +#' +#' @param flowdata Dataframe. A dataframe of daily mean streamflow data used to calculate the annual statistics. +#' Two columns are required: a 'Date' column with dates formatted YYYY-MM-DD and a 'Q' column with the daily +#' mean streamflow values in units of cubic metres per second. \code{flowdata} not required if \code{HYDAT} is used. +#' @param HYDAT Character. A HYDAT station number (e.g. "08NM116") of which to extract daily streamflow data from the HYDAT database. +#' tidyhydat package and a downloaded SQLite HYDAT required. +#' @param water_year_start Numeric. Month to start water year (1 to 12 for Jan to Dec). + +#' +#' @return A list with the following elements: +#' \item{flowdata} +#' +#' @examples +#' \dontrun{ +#' +#' set example :) +#' } +#' @export + +#' +#-------------------------------------------------------------- +# Compute the statistics on an (calendar and water) year basis + +fasstr_add_date_vars <- function( + flowdata=NULL, + HYDAT=NULL, + water_year_start=10){ + + # Compute statistics on an annual (calendar and water) year basis + # + # See the man-roxygen director for definition of parameters + # + # Output: List with elements given above. + # + ############################################################# + # Some basic error checking on the input parameters + # + + if( is.null(flowdata) & is.null(HYDAT)) { + stop("flowdata or HYDAT parameters must be set")} + if( !is.null(HYDAT) & !is.null(flowdata)) { + stop("Must select either flowdata or HYDAT parameters, not both.")} + if( is.null(HYDAT) & !is.data.frame(flowdata)) { + stop("flowdata parameter is not a data frame.")} + if( is.null(HYDAT) & !"Date" %in% names(flowdata)){ + stop("flowdata dataframe doesn't contain a Date variable.")} + if( is.null(HYDAT) & !inherits(flowdata$Date[1], "Date")){ + stop("Date column in flowdata dataframe is not a date.")} + if( !is.numeric(water_year_start)) { + stop("water_year_start parameter must be numeric between 1 and 12 (Jan-Dec)")} + if( water_year_start<1 & water_year_start>12 ) { + stop("water_year_start parameter must be numeric between 1 and 12 (Jan-Dec)")} + + # If HYDAT station is listed, check if it exists and make it the flowdata + if (!is.null(HYDAT)) { + if (!HYDAT %in% tidyhydat::allstations$STATION_NUMBER) {stop("Station in 'HYDAT' parameter does not exist.")} + flowdata <- tidyhydat::DLY_FLOWS(STATION_NUMBER = HYDAT) + } + + # Create values used to calculate the water year day of year + if (water_year_start==2) {doy.temp <- c(31,31)} + if (water_year_start==3) {doy.temp <- c(61,62)} + if (water_year_start==4) {doy.temp <- c(90,91)} + if (water_year_start==5) {doy.temp <- c(120,121)} + if (water_year_start==6) {doy.temp <- c(151,152)} + if (water_year_start==7) {doy.temp <- c(181,182)} + if (water_year_start==8) {doy.temp <- c(212,213)} + if (water_year_start==9) {doy.temp <- c(243,244)} + if (water_year_start==10) {doy.temp <- c(273,274)} + if (water_year_start==11) {doy.temp <- c(304,305)} + if (water_year_start==12) {doy.temp <- c(334,335)} + + # Calculate each date variable + flowdata$Year <- lubridate::year(flowdata$Date) + flowdata$Month <- lubridate::month(flowdata$Date) + flowdata$MonthName <- month.abb[flowdata$Month] + flowdata$DayofYear <- lubridate::yday(flowdata$Date) + + if (water_year_start==1) { + flowdata$WaterYear <- flowdata$Year + flowdata$WaterDayofYear <- flowdata$DayofYear + } else { + flowdata$WaterYear <- as.numeric(ifelse(flowdata$Month>=water_year_start, + flowdata$Year+1, + flowdata$Year)) + flowdata$WaterDayofYear <- ifelse(flowdata$Month12 ) { + stop("water_year_start parameter must be numeric between 1 and 12 (Jan-Dec)")} + + + # If HYDAT station is listed, check if it exists and make it the flowdata + if (!is.null(HYDAT)) { + if (!HYDAT %in% tidyhydat::allstations$STATION_NUMBER) {stop("Station in 'HYDAT' parameter does not exist.")} + flowdata <- tidyhydat::DLY_FLOWS(STATION_NUMBER = HYDAT) + } + + #Get the station_number and Parameter from flowdata if used HYDAT in a previous fasstr function + if ("STATION_NUMBER" %in% names(flowdata)){STATION_NUMBER <- flowdata$STATION_NUMBER[1]} + if ("Parameter" %in% names(flowdata)){Parameter <- flowdata$Parameter[1]} + + # If water year is TRUE and month is not January + if (water_year & water_year_start>1) { + + #Create a temp file to determine the min/max water years (cant affect flowdata yet) + flowdata.temp <- flowdata + flowdata.temp$Year <- lubridate::year(flowdata.temp$Date) + flowdata.temp$Month <- lubridate::month(flowdata.temp$Date) + flowdata.temp$WaterYear <- as.numeric(ifelse(flowdata.temp$Month>=water_year_start, + flowdata.temp$Year+1, + flowdata.temp$Year)) + min_wateryear <- min(flowdata.temp$WaterYear) + max_wateryear <- max(flowdata.temp$WaterYear) + + + # Extend the flowdata to well before the start and end dates (will filter to water years) + min_year <- lubridate::year(min(flowdata$Date))-1 + max_year <- lubridate::year(max(flowdata$Date))+1 + + flowdata <- merge(flowdata, + data.frame(Date=seq(as.Date(paste(min_year,'01-01',sep='-'), + "%Y-%m-%d"), + as.Date(paste(max_year ,'12-31',sep='-'), + '%Y-%m-%d'), 1)), + all.y=TRUE) + + # Add Water year to be able to filter it + flowdata$Year <- lubridate::year(flowdata$Date) + flowdata$Month <- lubridate::month(flowdata$Date) + flowdata$WaterYear <- as.numeric(ifelse(flowdata$Month>=water_year_start, + flowdata$Year+1, + flowdata$Year)) + + + # Filter flowdata for the min and max water years and remove date columns + flowdata <- dplyr::filter(flowdata,WaterYear>=min_wateryear & WaterYear<=max_wateryear) + flowdata <- dplyr::select(flowdata,-Year,-Month,-WaterYear) + + + + # If not water year, or January is chosen as water year start + } else { + min_year <- lubridate::year(min(flowdata$Date)) + max_year <- lubridate::year(max(flowdata$Date)) + + flowdata <- merge(flowdata, + data.frame(Date=seq(as.Date(paste(min_year,'01-01',sep='-'), + "%Y-%m-%d"), + as.Date(paste(max_year ,'12-31',sep='-'), + '%Y-%m-%d'), 1)), + all.y=TRUE) + } + + + # Fill in STATION_NUMBER and Parameter if HYDAT selected + if (!is.null(HYDAT)) { + flowdata$STATION_NUMBER <- HYDAT + flowdata$Parameter <- "FLOW" + } + + # If flowdata was from HYDAT in a previous function + if ("STATION_NUMBER" %in% names(flowdata)){flowdata$STATION_NUMBER <- STATION_NUMBER} + if ("Parameter" %in% names(flowdata)){flowdata$Parameter <- Parameter} + if (Value.Q) {flowdata <- dplyr::rename(flowdata,Value=Q)} + + # If fasstr_add_date_vars() used previously, add the date variables to the new dates + if (all(c("Year","Month","MonthName","WaterYear","DayofYear","WaterDayofYear") %in% names(flowdata))) { + flowdata <- fasstr_add_date_vars(flowdata=flowdata) + } + + + + return(flowdata) +} # end of function diff --git a/data prep testing.R b/data prep testing.R new file mode 100644 index 00000000..a9fc175b --- /dev/null +++ b/data prep testing.R @@ -0,0 +1,32 @@ +test <- fasstr_add_date_vars(HYDAT = "08HB048",water_year_start = 2) + +water_year <- T +HYDAT <- "08HB048" +water_year_start <- 2 +flowdata <- tidyhydat::DLY_FLOWS(STATION_NUMBER = HYDAT) +flowdata <- dplyr::select(flowdata,Date,Q=Value) +test <- fasstr_fill_missing_dates(flowdata = flowdata) +test <- fasstr_fill_missing_dates(HYDAT = "08HB048") +test <- fasstr_fill_missing_dates(HYDAT = "08HB048",water_year = T, water_year_start = 1) +test2 <- fasstr_add_rolling_means(HYDAT = "08NM116") +test2 <- fasstr_fill_missing_dates(flowdata = test2) +test2 <- fasstr_add_date_vars(flowdata = data) + + +test2 <- fasstr_fill_missing_dates(HYDAT = "08NM116") +test2 <- fasstr_add_rolling_means(flowdata = test2) +test2 <- fasstr_add_date_vars(test2) + +test2 <- fasstr_add_rolling_means(HYDAT = "08NM116") +test2 <- fasstr_fill_missing_dates(flowdata = test2) +test2 <- fasstr_add_date_vars(test2) + +test2 <- fasstr_add_date_vars(HYDAT = "08NM116") +test2 <- fasstr_add_rolling_means(test2) +test2 <- fasstr_fill_missing_dates(test2) + + +test2 <- fasstr_fill_missing_dates(HYDAT = "08NM116") + + + diff --git a/fasstr.Rproj b/fasstr.Rproj index 8e3c2ebc..21a4da08 100644 --- a/fasstr.Rproj +++ b/fasstr.Rproj @@ -11,3 +11,7 @@ Encoding: UTF-8 RnwWeave: Sweave LaTeX: pdfLaTeX + +BuildType: Package +PackageUseDevtools: Yes +PackageInstallArgs: --no-multiarch --with-keep.source diff --git a/man/fasstr_add_date_vars.Rd b/man/fasstr_add_date_vars.Rd new file mode 100644 index 00000000..7e01b23f --- /dev/null +++ b/man/fasstr_add_date_vars.Rd @@ -0,0 +1,32 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/fasstr_add_date_vars.R +\name{fasstr_add_date_vars} +\alias{fasstr_add_date_vars} +\title{Add calendar and water year date variables.} +\usage{ +fasstr_add_date_vars(flowdata = NULL, HYDAT = NULL, water_year_start = 10) +} +\arguments{ +\item{flowdata}{Dataframe. A dataframe of daily mean streamflow data used to calculate the annual statistics. +Two columns are required: a 'Date' column with dates formatted YYYY-MM-DD and a 'Q' column with the daily +mean streamflow values in units of cubic metres per second. \code{flowdata} not required if \code{HYDAT} is used.} + +\item{HYDAT}{Character. A HYDAT station number (e.g. "08NM116") of which to extract daily streamflow data from the HYDAT database. +tidyhydat package and a downloaded SQLite HYDAT required.} + +\item{water_year_start}{Numeric. Month to start water year (1 to 12 for Jan to Dec).} +} +\value{ +A list with the following elements: + \item{flowdata} +} +\description{ +Adds mulitple date variables to a dataframe from a column of dates, including + year, month (numeric and text), day of years, and water years and day of water years. +} +\examples{ +\dontrun{ + +set example :) +} +} diff --git a/man/fasstr_add_rolling_means.Rd b/man/fasstr_add_rolling_means.Rd new file mode 100644 index 00000000..723ae0ef --- /dev/null +++ b/man/fasstr_add_rolling_means.Rd @@ -0,0 +1,35 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/fasstr_add_rolling_means.R +\name{fasstr_add_rolling_means} +\alias{fasstr_add_rolling_means} +\title{Add rolling means.} +\usage{ +fasstr_add_rolling_means(flowdata = NULL, HYDAT = NULL, + rolling_nday = c(3, 7, 30), align = "right") +} +\arguments{ +\item{flowdata}{Dataframe. A dataframe of daily mean streamflow data used to calculate the annual statistics. +Two columns are required: a 'Date' column with dates formatted YYYY-MM-DD and a 'Q' column with the daily +mean streamflow values in units of cubic metres per second. \code{flowdata} not required if \code{HYDAT} is used.} + +\item{HYDAT}{Character. A HYDAT station number (e.g. "08NM116") of which to extract daily streamflow data from the HYDAT database. +tidyhydat package and a downloaded SQLite HYDAT required.} + +\item{rolling_nday}{Numeric. Default 3,7,30.} + +\item{align}{Character. specifyies whether the index of the result should be left- or right-aligned or centered +(default) compared to the rolling window of observations} +} +\value{ +A list with the following elements: + \item{flowdata} +} +\description{ +Adds rollings means. +} +\examples{ +\dontrun{ + +set example :) +} +} diff --git a/man/fasstr_fill_missing_dates.Rd b/man/fasstr_fill_missing_dates.Rd new file mode 100644 index 00000000..dd8d79f8 --- /dev/null +++ b/man/fasstr_fill_missing_dates.Rd @@ -0,0 +1,34 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/fasstr_fill_missing_dates.R +\name{fasstr_fill_missing_dates} +\alias{fasstr_fill_missing_dates} +\title{Fill missing dates with NA.} +\usage{ +fasstr_fill_missing_dates(flowdata = NULL, HYDAT = NULL, + water_year = FALSE, water_year_start = 10) +} +\arguments{ +\item{flowdata}{Dataframe. A dataframe of daily mean streamflow data used to calculate the annual statistics. +Two columns are required: a 'Date' column with dates formatted YYYY-MM-DD and a 'Q' column with the daily +mean streamflow values in units of cubic metres per second. \code{flowdata} not required if \code{HYDAT} is used.} + +\item{HYDAT}{Character. A HYDAT station number (e.g. "08NM116") of which to extract daily streamflow data from the HYDAT database. +tidyhydat package and a downloaded SQLite HYDAT required.} + +\item{water_year}{Logical (TRUE/FALSE). Choose to fill to the start of the first/last water years.} + +\item{water_year_start}{Numeric. Month to start water year (1 to 12 for Jan to Dec). Default 10 (Oct).} +} +\value{ +A list with the following elements: + \item{flowdata} +} +\description{ +Fill missing dates with NA. +} +\examples{ +\dontrun{ + +set example :) +} +}