Learn R Programming

EGRET (version 3.0.12)

populateDaily: Populate Daily data frame

Description

Using a data frame that has at least Date, Q, Qualifier, populates the rest of the basic Daily data frame used in EGRET analysis.

Usage

populateDaily(rawData, qConvert, verbose = TRUE, adjust = TRUE,
  fill = FALSE, maxgap = 21, fill_type = "interpolation")

Value

A data frame 'Daily' with the following columns:

NameTypeDescription
QnumericDischarge in m^3/s
JulianintegerNumber of days since Jan. 1, 1850
MonthintegerMonth of the year [1-12]
DayintegerDay of the year [1-366]
DecYearnumericDecimal year
MonthSeqintegerNumber of months since January 1, 1850
QualifiercharacterQualifying code
iintegerIndex of days, starting with 1
LogQnumericNatural logarithm of Q
Q7numeric7 day running average of Q
Q30numeric30 day running average of Q

Arguments

rawData

dataframe contains at least Date, Q, Qualifier columns.

qConvert

numeric conversion to cubic meters per second.

verbose

logical specifying whether or not to display messages.

adjust

logical specifying whether or not to add a constant to zero values to allow log transformation. Defaults to TRUE.

fill

logical specifying whether to fill NA values by linear interpolation. Defaults to FALSE.

maxgap

Maximum number of NA days allowed for interpolating gaps. Default is 21. Only used if fill is set to TRUE.

fill_type

character to define what process to fill missing data. Options are "interpolation" - linear interpolation from the `zoo::na.approx`, or "log_interp" - linear interpolation in the log space. Only used if fill is set to TRUE.

Author

Robert M. Hirsch rhirsch@usgs.gov

See Also

readNWISDaily, readUserDaily

Examples

Run this code
Date <- as.character(seq(from = as.Date("2001/1/1"),
                         to = as.Date("2002/1/2"),
                         by = "day"))
Q <- c(-1:365)
Qualifier <- rep("",367)
dataInput_complete <- data.frame(Date, Q, Qualifier)
dataInput <- dataInput_complete[-4:-5,]

# No fill, but with 0 and negative:
Daily <- populateDaily(dataInput, qConvert = 1)

# No negatives/zeros:
Q <- 2+sin(seq(from = 0, to = 2*pi, length.out = 367))
Q <- jitter(Q, factor = 500)
plot(Q, ylim = c(0, 3.2))
dataInput_complete <- data.frame(Date, Q, Qualifier)
# Remove some rows to test missing:
dataInput <- dataInput_complete[-4:-5,]
dataInput <- dataInput[-10:-20,]

# No fill:
Daily <- populateDaily(dataInput, qConvert = 1)
plot(Daily$Date[1:30], Daily$Q[1:30], type = "b", ylim = c(0, 3.2))

# Linear interpolation:
Daily_fill <- populateDaily(dataInput,
                            qConvert = 1,
                            fill = TRUE,
                            fill_type = "interpolation")
plot(Daily_fill$Date[1:30],
     Daily_fill$Q[1:30],
     col = as.factor(Daily_fill$Qualifier[1:30]),
     type = "b", pch = 16, ylim = c(0, 3.2),
     main = "Linear Interpolation")


# Add a gap that is too big do deal with:
dataInput <- dataInput_complete[-4:-20,]
dataInput <- dataInput[-200:-255,]

Daily_interp <- populateDaily(dataInput,
                              qConvert = 1,
                              fill = TRUE,
                              fill_type = "interpolation")
plot(Daily_interp$Date, Daily_interp$Q,
     col = as.factor(Daily_interp$Qualifier),
     main = "Linear Interpolation",
     type = "b", pch = 16, ylim = c(0, 3.2))
plot(Daily_interp$Date[1:50], Daily_interp$Q[1:50],
     col = as.factor(Daily_interp$Qualifier[1:50]),
     main = "Linear Interpolation",
     type = "b", pch = 16, ylim = c(0, 3.2))

Daily_log_interp <- populateDaily(dataInput,
                              qConvert = 1,
                              fill = TRUE,
                              fill_type = "log_interp")
plot(Daily_log_interp$Date, Daily_log_interp$Q,
     col = as.factor(Daily_log_interp$Qualifier),
     main = "Linear Interpolation in Log Scale",
     type = "b", pch = 16, ylim = c(0, 3.2))
plot(Daily_log_interp$Date[1:50], Daily_log_interp$Q[1:50],
     col = as.factor(Daily_log_interp$Qualifier[1:50]),
     main = "Linear Interpolation in Log Scale",
     type = "b", pch = 16, ylim = c(0, 3.2))


# Real data:
eList <- Choptank_eList
Daily_chop <- eList$Daily
df <- Daily_chop[,c("Date", "Q")]
df <- df[-2:-5, ]
df <- df[-100:-200,]
D2 <- populateDaily(df, 1, fill = TRUE)
plot(D2$Date[1:20], D2$Q[1:20],
     col = as.factor(D2$Qualifier[1:20]),
     main = "Linear Interpolation",
     type = "b", pch = 16)
plot(D2$Date[1:110], D2$Q[1:110],
     col = as.factor(D2$Qualifier[1:110]),
     main = "Linear Interpolation",
     type = "b", pch = 16)

Run the code above in your browser using DataLab