Skip to contents

Download American river-gauge data from USGS

Usage

dod.river.usgs(
  id = "03242350",
  start = NULL,
  end = NULL,
  destdir = ".",
  debug = 0
)

Arguments

id

character value giving the numeric code for the gauge. This defaults to a river in Ohio.

start

an indication of the start time of the requested data window. If provided, this may be in a (non-ambiguous) character form or, better, in a POSIXt object. The "UTC" timezone is assumed. If not provided, start defaults to 1 week before the present time.

end

as for start, but instead for the end date.

destdir

character giving the destination directory in which the downloaded file will be stored. The default value is ".", meaning to store the file in the persent directory.

debug

an integer value indicating the level of debugging. If this exceeds 0, then some debugging messages will be printed. This value is passed down to related functions, but with 1 subtracted for each pass.

Author

Dan Kelley

Examples

if (interactive()) { # pkgdown::build_site() cannot handle downloads
    library(dod)
    destdir <- tempdir(check = TRUE)
    print(destdir)
    file <- dod.river.usgs(start = "2025-08-01", end = "2025-08-08", destdir = destdir, debug = 4)
    lines <- readLines(file)
    skip <- 1 + grep("agency_cd", lines)
    data <- read.delim(text = lines, skip = skip, sep = "\t", header = FALSE)
    # The data at this site, if downloaded during daylight-savings
    # time, are offset from UTC by 4 hours. I'm not sure how to make the server
    # return in UTC.
    time <- as.POSIXct(data$V3, tz = "UTC") + 4 * 3600
    # Convert from feet to metres
    height <- 0.3048 * data$V5
    plot(time, height,
        type = "l", xlab = "Day in year 2025",
        ylab = "River Height [m]"
    )
    mtext(gsub("#[ ]*", "", lines[grep("#    USGS ", lines)]))
    # Clean up space
    unlink(destdir, recursive = TRUE)
}