library(rowr) library(shapefiles) library(sp) library(maptools) setwd("J:\\typhoons\\test45\\") ###READ FILES, PREPARE IN FORMAT THAT CAN KEEP ORGANIZED BELOW ### Make sure the files output from awk function in Linux have .txt extensions. May need to change these in command line, if many files. ## This loop is for years that have wind data, which is the 7th column; if no wind, change column number to 6 filenames<-list.files(getwd()) for(j in 1:length(filenames)){ m<-max(count.fields(filenames[j])) print(m) dat<-read.table(filenames[j], sep="", header=FALSE, skip = 1, fill = TRUE, colClasses = c(rep("integer", 7), rep("NULL", m))) # Use fill = TRUE to avoid error; also, skip = 1, skips first line of data, as needed print(dim(dat)) name<-filenames[j] name<-substring(name, 1, nchar(name)-4) write.csv(dat, paste(name,".csv",sep="")) } #####FORMAT###### ##After changing to .csv, format to get ready to change to shapefile, points to line filenames<-list.files(getwd()) for(j in 1:length(filenames)){ dat<-read.csv(filenames[j]) print(dim(dat)) dat<-dat[,2:8] ## removes first row number ID column d1<-dat[,1] ## divides yr month day and hr into individual columns Year<-substr(d1,1,2) Month<-substr(d1,3,4) Day<-substr(d1,5,6) Hour<-substr(d1,7,8) dateszzz<-cbind(Year,Month,Day,Hour) dateszzz<-as.data.frame(dateszzz) d2<-dat[,2:7] print(d2) colnames(d2)<-c("Grade","Grade2","CtrLat","CtrLon","CtrPressure","WindKnots") ## Assigns column names to remaining data (after removing ymd hr) ##CHANGE LAT LON AFTER (MEAN ROW 1,2, THEN 2,3, ETC.) y<-d2[,3] print(y) ##rowr chngLatAf<-rollApply(y, mean, window = 2, minimum = 1, align = "left") ## Find after mean values between data pt and next location for lat and lon values and divide by 10 for decimal degree CLatAft<-chngLatAf/10 CLatAft<-data.frame(CLatAft) x<-d2[,4] chngLonAf<-rollApply(x, mean, window = 2, minimum = 1, align = "left") CLonAft<-chngLonAf/10 CLonAft<-data.frame(CLonAft) ##CHANGE LAT LON BEFORE (MEAN ROW 1,1, THEN 2,1, 3,2 ETC.) y<-d2[,3] y<-rev(y) chngLatBF<-rollApply(y, mean, window = 2, minimum = 1, align = "left") ## Find before mean values between data pt and next location for lat and lon values and divide by 10 for decimal degree CLatBF<-chngLatBF/10 CLatBF<-as.matrix(CLatBF) CLatBF<-rev(CLatBF) CLatBF<-data.frame(CLatBF) x<-d2[,4] x<-rev(x) chngLonBF<-rollApply(x, mean, window = 2, minimum = 1, align = "left") ##length is 36 CLonBF<-chngLonBF/10 CLonBF<-as.matrix(CLonBF) CLonBF<-rev(CLonBF) CLonBF<-data.frame(CLonBF) ###cbind dates columns and new before and after averages out<-cbind(dateszzz,d2,CLatAft,CLonAft,CLatBF,CLonBF) ymd<-substr(d1,1,6) ymd<-data.frame(ymd) out2<-cbind(ymd,out) print(out2[,1]) out4<-out2 out4[,8]<-out4[,8]/10 out4[,9]<-out4[,9]/10 out5<-out4[,c(1,7,8,9,11,12,13,14,15)] ##Get latitude and longitude before, central, and after in individual rows to cbind to duplicate ## rows data set (r) later lat<-data.frame() for(g in 1:nrow(out5)) { ay<-out5[g,8] by<-out5[g,3] cy<-out5[g,6] lat<-rbind(lat,ay,by,cy) } lon<-data.frame() for(h in 1:nrow(out5)) { ax<-out5[h,9] bx<-out5[h,4] cx<-out5[h,7] lon<-rbind(lon,ax,bx,cx) } out5$freq<-3 r <- out5[rep(seq(nrow(out5)), out5$freq),] r$LAT<-lat r$LON<-lon colnames(r[,11])<-"LAT" colnames(r[,12])<-"LON" r2<-r[,c(1,12,11,2,5)] print(r2) x <- rep(1:3, length=nrow(r2)) r2$Order<-x yr<-substr(r2[1,1],1,2) name<-paste(yr,filenames[j], sep = "") name<-substring(name, 1, nchar(name)-4) print(name) z<-nrow(r2) z<-z/3 f<-seq(from = 1, to = z, by = 1) w <- rep(f, each=3) r2$ID<-w #name<-substring(name, 1, nchar(name2)-4) #print(name) write.csv(r2, paste("t",name,".csv",sep="")) } ##NOW LOOP library(shapefiles) library(maptools) ## make sure only .csv files from last loop in folder, so those starting with t and the year in the name filenames<-list.files(getwd()) for(i in 1:length(filenames)) { dat<-read.csv(filenames[i]) dat2<-dat[,c(3:4,2,5:8)] att<-dat2[seq(1, nrow(dat2), by=3), ] #selects every third row for ddTable attributes ddd<-unique(dat2$ID) ##The dat2 data.frame has 111,222,333, etc.; the data table can have only unique Ids, so it has 1/3 the number of Ids as dat2 and dd dd <- data.frame(Id=dat2$ID,X=dat2$LON,Y=dat2$LAT) ##includes the coordinates and the line IDs (repeated IDs for each line segment) ddTable <- data.frame(Id=ddd, ymd=att$ymd,Grade2=att$Grade2,Wind=att$WindKnots) ##containes unique IDs (so 1/3 fewer than dd), and attribute data ddShapefile <- convert.to.shapefile(dd, ddTable, "Id", 3) ## 3 indicates polyline name<-paste(filenames[i]) name<-substring(name, 1, nchar(name)-4) write.shapefile(ddShapefile, name,arcgis=T) } ## If need a unique ID column for each shapefile, you will need to change the names so that they start with the letter "t" (as it is now), but only has numbers in the name. Additionally, the "t" will need to be removed before placing the file name into the ID column and change the format to numeric, so that R does not record it as a factor.