Last active
November 29, 2017 01:09
-
-
Save ozjimbob/3abc78528021d2095317a647941b2853 to your computer and use it in GitHub Desktop.
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
## Load required packages | |
# you will need to install these, first do | |
# eg. install.packages("tidyverse") | |
# etc. to install them one by one | |
library(tidyverse) | |
library(httr) | |
library(sf) | |
library(tmap) | |
########## SETUP ################ | |
# Blitzortung user name and password | |
username = "xxxxx" | |
password = "xxxxx" | |
# Blitzortung station code | |
# Note it does a pretty dumb grep for station code to isolate it | |
# If you have like a 2-digit station code this will probably | |
# work poorly. 4-digit should be fine | |
station_code="2050" | |
# How many hours to plot | |
n_hours = 12 | |
# Blitzortung region data to use, eg. Oceania = "Data_2", North America = "Data_3" | |
region = "Data_3" | |
################################## | |
# Default world map | |
data(World) | |
World = st_as_sf(World) | |
# Plot all points your station has helped with in the lat 12 hours | |
lst=list() | |
j=0 | |
for(i in seq(-60*n_hours,-10,10)){ | |
j=j+1 | |
time_now=Sys.time()+i*60 | |
year = format(time_now,"%Y",tz="GMT") | |
month = format(time_now,"%m",tz="GMT") | |
day = format(time_now,"%d",tz="GMT") | |
hour = format(time_now,"%H",tz="GMT") | |
mint = as.numeric(format(time_now,"%M",tz="GMT")) | |
mint=floor(mint/10)*10 | |
mint = as.character(mint) | |
if(mint=="0"){mint="00"} | |
url = sprintf("http://data.blitzortung.org/%s/Protected/Strokes/%s/%s/%s/%s/%s.log",region,year,month,day,hour,mint) | |
dat=GET(url,authenticate(username,password)) | |
dat=content(dat,as="text") | |
dat = unlist(strsplit(dat,"\n")) | |
stpos = str_extract(dat,"(?<=pos;)(.*)(?=str)") | |
lat =stpos %>% str_split(";") %>% map(1) %>% unlist() %>% as.numeric() | |
lon =stpos %>% str_split(";") %>% map(2) %>% unlist() %>% as.numeric() | |
datx = tibble(lat = lat,lon=lon,sel=0) | |
stion = str_extract(dat,"(?<=sta;)(.*)") %>% str_detect(station_code) | |
datx = datx[stion,] | |
lst[[j]]=datx | |
} | |
datx=bind_rows(lst) | |
dats = st_as_sf(datx,coords=c("lon","lat"),crs=4326) | |
mp = tm_shape(World) + tm_polygons() + tm_shape(dats,is.master = TRUE) + tm_dots(col="red",scale=2) + tm_compass() + tm_grid() + tm_layout(bg.color = "lightblue") | |
mp |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment