Last active
January 12, 2019 14:01
-
-
Save SwampThingPaul/ba8f3859cb967ba257ce6ddd66fb811e to your computer and use it in GitHub Desktop.
Hurricane Data Analysis
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
## Code was compiled by Paul Julian | |
## contact infor: [email protected] | |
#Libraries | |
library(HURDAT) | |
library(plyr) | |
library(sp) | |
library(tmap) | |
#Projection | |
nad83.pro=CRS("+init=epsg:4269");# I assumed NAD83 projection | |
# Hurricane_TropcialStorm ------------------------------------------------- | |
hur.dat=get_hurdat(basin = c("AL")) | |
#Convert XY data to points | |
hur.dat.sp.pt=SpatialPointsDataFrame(coords=hur.dat[,c("Lon","Lat")],data=hur.dat,proj4string = CRS("+init=epsg:4269")) | |
#Convert SpatialPointsDataFrame to SpatialLinesDataFrame data | |
hur.dat2=hur.dat | |
hur.id=ddply(hur.dat2,c("Key","Name"),summarise,N.val=length(which(Key!="NA"))) | |
hur.dat2=subset(hur.dat2,Key%in%subset(hur.id,N.val>1)$Key)# Can't draw a line with only one point. | |
coordinates(hur.dat2)=c("Lon","Lat") | |
hur.dat2=hur.dat2[order(hur.dat2$Key,hur.dat2$DateTime),];#reorders the data accordingly | |
path=split(hur.dat2,hur.dat2$Key) | |
sp_lines=SpatialLinesDataFrame(SpatialLines(list(Lines(list(Line(path[[1]])),unique(path[[1]]@data$Key))),CRS("+init=epsg:4269")),data.frame(row.names=hur.id$Key,Key=hur.id$Key,Name=hur.id$Name)) | |
pb=txtProgressBar(1,max=length(path),style=3);#progress bar | |
for(i in 2:length(path)){ | |
tmp=SpatialLinesDataFrame(SpatialLines(list(Lines(list(Line(path[[i]])),unique(path[[i]]@data$Key))),CRS("+init=epsg:4269")),data.frame(row.names=hur.id$Key,Key=hur.id$Key,Name=hur.id$Name)) | |
sp_lines=rbind(sp_lines,tmp) | |
setTxtProgressBar(pb,i) | |
} | |
tm_shape(sp_lines)+tm_lines(col="Key") | |
## Explore the yearly occurance of hurricanes | |
hur.dat$Year=as.numeric(format(hur.dat$date,"%Y")) | |
hur.year=ddply(hur.dat,c("Key","Name"),summarise,Year.start=min(Year,na.rm=T),max.wind=max(Wind,na.rm=T),min.pres=min(Pressure,na.rm=T)) | |
sp_lines=merge(sp_lines,hur.year,by.x=c("Key","Name"),by.y=c("Key","Name")) | |
tmap_mode("view") | |
tm_shape(hur.tck)+tm_lines(col="Year.start",breaks=seq(1851,2017,20)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Fixed line 22. This gives a count of all points from any given hurricane track and only counts numbers (i.e. something not "NA").