Last active
May 31, 2017 04:21
-
-
Save jimmylovestea/7a09e09171f6de4cf021771f152120f1 to your computer and use it in GitHub Desktop.
A script that downloads wind forecast data from NOAA NHC
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Copyright 2017, James Molyneux | |
# MIT License, http://www.opensource.org/licenses/mit-license.php | |
# Load the rvest package | |
library(rvest) | |
# Naviagate to the site and obtain the form | |
site_url <- "http://www.nhc.noaa.gov/gis/archive_wsp.php" | |
my_session <- html_session(site_url) | |
site_form <- html_form(my_session)[[3]] | |
# Set form value to the year you'd like | |
my_form <- set_values(form = site_form, year = "2017") | |
# The form's url is NULL by default, which causes errors. So I | |
# set the url to be the same as the session's. | |
my_form$url <- my_session$url | |
# Submit the form | |
data_page <- submit_form(my_session, my_form, submit = "year") | |
# Find all of the links on the new page | |
page_links <- read_html(data_page) %>% | |
html_nodes("a") %>% | |
html_attr("href") | |
# Find all the links to just the forecast data | |
data_links <- page_links[grepl(pattern = "forecast/", page_links)] | |
# Create a set of links to download from | |
download_urls <- paste0("http://www.nhc.noaa.gov/gis/", data_links) | |
# Create a set of destination file names | |
dest_filenames <- paste0("~/Downloads/", basename(download_urls)) | |
# Download the files! (This seems to download | |
# all the files at once, fyi) | |
download.file(download_urls, dest_filenames) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment