Skip to content

Instantly share code, notes, and snippets.

@troyhill
Last active August 29, 2015 13:57
Show Gist options
  • Save troyhill/9494280 to your computer and use it in GitHub Desktop.
Save troyhill/9494280 to your computer and use it in GitHub Desktop.
R function to scrape data on harmonic constituent data for a NOAA CO-OPS tide station.
### Important note: I'm no longer maintaining this gist, since this code is incorporated into the
### VulnToolkit package.
### Because it's not being maintained, the code below might not work. The latest, updated version
### is available at https://github.com/troyhill/VulnToolkit/blob/master/R/harcon.R
### Input argument is a single NOAA station number. See http://co-ops.nos.noaa.gov/stations.html?type=Water+Levels
### Output is a dataframe with a row for each harmonic constituent, and columns for
### each station's amplitude, phase, and speed.
### Output amplitudes are in meters, phases are in degrees, referenced to GMT
###
### Code dependencies: RCurl and XML packages. To install: install.packages(c("RCurl", "XML"))
###
### Have a problem? Find a bug? Email [email protected]
harcon <- function(station) {
require(RCurl)
require(XML)
page <- htmlParse(getURL(paste("http://co-ops.nos.noaa.gov/harcon.html?unit=0&timezone=0&id=", station, sep = "")),
useInternalNodes = TRUE)
nodes <- getNodeSet(page, "//td")
nodes.text <- xmlSApply(nodes, xmlValue)
hc.name <- as.factor(nodes.text[c(seq(from = 2, to = length(nodes), by = 6))])
hc.desc <- as.factor(nodes.text[c(seq(from = 6, to = length(nodes), by = 6))])
hc.amp <- as.numeric(nodes.text[c(seq(from = 3, to = length(nodes), by = 6))])
hc.phase <- as.numeric(nodes.text[c(seq(from = 4, to = length(nodes), by = 6))])
hc.speed <- as.numeric(nodes.text[c(seq(from = 5, to = length(nodes), by = 6))])
output.tmp <- data.frame(hc.desc, hc.name, hc.amp, hc.phase, hc.speed)
names(output.tmp)[3] <- paste("amplitude.", station, sep = "")
names(output.tmp)[4] <- paste("phase.", station, sep = "")
names(output.tmp)[5] <- paste("speed.", station, sep = "")
invisible(output.tmp)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment