##UPDATE 2007 YTD HURRICANE RECORD #update information taken from sites http://www.weather.unisys.com/hurricane/w_pacific, http://www.weather.unisys.com/hurricane/atlantic, etc. ##BASIN NOMENCLATURE AND IDs loc<-file.path("http://www.weather.unisys.com/hurricane", c("atlantic/tracks.atl","w_pacific/tracks.bwp","e_pacific/tracks.epa", "s_indian/tracks.bsh","indian_oc/tracks.bio") ) basin.id<-c("ATL","WPAC","EPAC","SIO","NIO") basin0<-c("atlantic","w_pacific","e_pacific","s_indian","indian_oc","s_pacific") year0=2007 ################# ##FUNCTIONS USED IN UPDATE ################# ### tidy #tidies " -" and change factor to numeric tidy<-function(x) { if(class(x) == "factor") {levels(x)[levels(x)==" -"] <-NA;tidy<- as.numeric(levels(x))[x]} else tidy<-x; tidy} ### homogenize #a function to convert best-tracks online date to 6-hour interpolations for consistency to best tracks #in the collated data, readings are every 6 hours; this is not the case with uncollated data homogenize<-function(dummy) { dummy$date<-as.Date(paste(dummy$year,dummy$month,dummy$day,sep="-")) dummy$julian<-julian(dummy$date) NN<-nrow(dummy) dummy$julian<-dummy$julian+dummy$qtr/24 start0<-dummy$date[1];hourstart<-6*ceiling(dummy$qtr[1]/6) if(dummy$qtr[1]>18) {start0<-start0+1;hourstart<-0} end0<-dummy$date[NN];hourend<-6*floor(dummy$qtr[NN]/6) index<-seq(julian(start0)+hourstart/24, julian(end0)+hourend/24,.25) dum<-data.frame(index);names(dum)<-"julian" NNN<-length(index) dum$date<-seq(start0+hourstart/24,start0+hourstart/24+(NNN-1)*.25,.25); dum$date<-as.character(dum$date) dum$year<-as.numeric(substr(dum$date,1,4)) dum$month<-as.numeric(substr(dum$date,6,7)) dum$day<-as.numeric(substr(dum$date,9,10)) dum$qtr<- 24*dum$julian%%1 if(nrow(dummy)==1) homogenize<-dummy[,1:9] else{ if(length(unique(dummy$julian))==1) homogenize<-dummy[1,1:9] else { f<-approxfun(dummy$julian,dummy$lat);dum$lat<-f(dum$julian) f<-approxfun(dummy$julian,dummy$long);dum$long<-f(dum$julian) if(sum(!is.na(dummy$wind))>=2) {f<-approxfun(dummy$julian,dummy$wind);dum$wind<-f(dum$julian)} else dum$wind<-dummy$wind if(sum(!is.na(dummy$press))==0) dum$press<-NA else {f<-approxfun(dummy$julian,dummy$press);dum$press<-f(dum$julian)} dum$id<-rep(dummy$id[1],nrow(dum)) homogenize<-dum[,c(11,3:10)]}} homogenize } ### read_hurricane_id #this will scrape YTD names from webpage #parameter basin should be a name from the list in basin0 above e.g. "atlantic" read_hurricane_id=function(basin,year=2007) { loc=file.path("http://www.weather.unisys.com/hurricane",basin,year,"index.html") fred=readLines(loc);x=nchar(fred) temp=(substr(fred,1,8)== "") read_hurricane_id=rev(substr(fred[temp],9,x[temp]-11)) read_hurricane_id } #id=read_hurricane_id("atlantic"); ### read_track #this function will read the interim files at unisys and return estimates at 6-hour intervals for consistency with historic data #input data has form [#17] "+96 21.00 -55.00 09/30/18Z 55 - TROPICAL STORM" [#18] "+120 23.00 -57.00 10/01/18Z 55 - TROPICAL STORM" #parameters are k- the storm number; jbasin - the basin number and year (defaulting for now to 2007) #ONLY DONE ON ATLANTIC SO FAR AND MAY NEED TWEAKING FOR OTHER BASINS #note that there is an error in occasional advance files e,g, #12 where months don't change right read_track=function(k,jbasin=1,year=2007) { loc<-file.path("http://www.weather.unisys.com/hurricane",basin0[jbasin],year,hurdummy$name[k],"track.dat") skip0<-3 #if((jbasin==2)&(i==3)&(k==31)) skip0<-26 else #test<-read.fwf(loc,widths=c(3,8,7,3,1,2,1,2,1,6,5),skip=skip0) test<-read.fwf(loc,widths=c(4,7,8,10,6,5),skip=skip0) test[,4]=gsub(" ","",test[,4]) test[,7]=as.numeric(substr(test[,4],1,2)) test[,8]=as.numeric(substr(test[,4],4,5)) test[,9]=as.numeric(substr(test[,4],7,8)) test<-test[,c(1,2,3,7,8,9,5,6)] dimnames(test)[[2]]<-c("record","lat","long","month","day","qtr","wind","press") temp<-!is.na(test$qtr) test<-test[temp,] test$year<-rep(year,nrow(test)); test$id<-rep(hurdummy$id[k],nrow(test)) dummy<-test[,c(10,9,4,5,6,2,3,7,8)] dummy[,c("lat","long")]<-10*dummy[,c("lat","long")] #Track is in tenths ##if(i==4) dummy[,"long"]<- - dummy[,"long"] #keep in same Westings if((jbasin==2)|(jbasin==5)) dummy$long<- 3600-dummy$long if(jbasin==3) dummy$long<- -dummy$long if(jbasin==1)dummy$long=- dummy$long #positive westing dummy[,"wind"]<-tidy(dummy[,"wind"]); dummy[,"press"]<-tidy(dummy[,"press"] ) #deals with - dummy<-dummy[!(dummy$month==0),] temp= (diff(dummy$month)== -1) if(sum(temp)>0) { MM=(1:nrow(dummy))[temp]; dummy$month[MM:nrow(dummy)]=dummy$month[MM]} read_track=homogenize(dummy) read_track } ##ATLANTIC UPDATE SEPT 2007 #PRIOR COLLATION #hurricane and Track data have been previously collated to end 2006 load("d:/climate/data/hurricane/hurdat/hurricane.ATL.tab") load("d:/climate/data/hurricane/hurdat/Track.ATL.tab") #REMOVE PREVIOUS YTD UPDATE AND DO FRESH UPDATE hurricane=hurricane[hurricane$year<2007,] Track=Track[Track$year<2007,] id=read_hurricane_id("atlantic"); M<-length(id);M #13 jbasin=1 #set basin number K<-nrow(hurricane);K #1363 hurdummy<-data.frame(array(NA,dim=c(M,11)));names(hurdummy)<-names(hurricane) hurdummy$id<- K+ (1:M) hurdummy$year<- year#update.list[[j]][i] hurdummy$name<-id for (i in c(3,4,5,7:8,11))class(hurdummy[,i])="numeric" for (i in 9:10)class(hurdummy[,i])="character" for (k in 1:M){ dummy=read_track(k,jbasin=1) Track<-rbind(Track,homogenize(dummy)) hurdummy[k,c( "month_start", "day_start")]=dummy[1,3:4] } hurricane<-rbind(hurricane,hurdummy) save(hurricane,file=file.path("d:/climate/data/hurricane/hurdat",paste("hurricane",basin.id[j],"tab",sep="."))) save(Track,file=file.path("d:/climate/data/hurricane/hurdat",paste("Track",basin.id[j],"tab",sep=".")))