Skip to content

Instantly share code, notes, and snippets.

@hrbrmstr
Last active September 12, 2018 16:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save hrbrmstr/e3253ddd353f1a489bb4 to your computer and use it in GitHub Desktop.
Save hrbrmstr/e3253ddd353f1a489bb4 to your computer and use it in GitHub Desktop.
Track Hurricane Danny (or any other Unisys-data-available-tropical storm)
library(leaflet)
library(stringi)
library(htmltools)
library(RColorBrewer)
# get tracks
danny <- readLines("http://weather.unisys.com/hurricane/atlantic/2015/DANNY/track.dat")
# extract data
danny_dat <- read.table(textConnection(gsub("TROPICAL ", "TROPICAL_", danny[3:length(danny)])),
header=TRUE, stringsAsFactors=FALSE)
# make storm type names prettier
danny_dat$STAT <- stri_trans_totitle(gsub("_", " ", danny_dat$STAT))
# make column names prettier
colnames(danny_dat) <- c("advisory", "lat", "lon", "time", "wind_speed", "pressure", "status")
# assign colors
danny_dat$color <- as.character(factor(danny_dat$status,
levels=c("Tropical Depression", "Tropical Storm",
"Hurricane-1", "Hurricane-2", "Hurricane-3",
"Hurricane-4", "Hurricane-5"),
labels=rev(brewer.pal(7, "YlOrBr"))))
last_advisory <- tail(which(grepl("^[[:digit:]]+$", danny_dat$advisory)), 1)
# draw the map
leaflet() %>%
addTiles() %>%
addPolylines(data=danny_dat[1:last_advisory,], ~lon, ~lat, color=~color) -> tmp_map
if (last_advisory < nrow(danny_dat)) {
tmp_map <- tmp_map %>%
addCircles(data=danny_dat[last_advisory:nrow(danny_dat),], ~lon, ~lat, color=~color, fill=~color, radius=25000,
popup=~sprintf("<b>Advisory forecast for +%sh (%s)</b><hr noshade size='1'/>
Position: %3.2f, %3.2f<br/>
Expected strength: <span style='color:%s'><strong>%s</strong></span><br/>
Forecast wind: %s (knots)<br/>Forecast pressure: %s",
htmlEscape(advisory), htmlEscape(time), htmlEscape(lon),
htmlEscape(lat), htmlEscape(color), htmlEscape(status),
htmlEscape(wind_speed), htmlEscape(pressure)))
}
html_print(tmp_map)
@ziina
Copy link

ziina commented Mar 3, 2016

@hrbrmstr i try to execute but i habe this error : Error in map$x : $ operator is invalid for atomic vector in line "addPolylines(data=danny_dat[1:last_advisory,], ~lon, ~lat, color=~color) -> tmp_map"

@hrbrmstr
Copy link
Author

hrbrmstr commented Mar 3, 2016

the exact same code ran for me perfectly just now in a clean R session

@kmatt
Copy link

kmatt commented Sep 1, 2016

Some updates for the current season: https://gist.github.com/kmatt/dbf1b67c17ad996d0acb

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment