Saturday, March 28, 2015

R -Get the First and Last elevation of each of the floodplain cross-sections


This is one of the output file from Flo-2d modeling. There are 1500 floodplain cross section in the project area.It is very difficult to get the first and Last elevation for each of those floodplain c/s manually. The original file can be downloaded from the below mentioned link.

Sample File
First and Last elevation of each the floodplain cross-sections

Original File
First and Last elevation of each of the Floodplain cross-sections

The file looks like this one:



Here is the code in R:

rm(list=ls())

workDir <- "C:/Users/PKuranjekar/Desktop/Thumb drive/R/reneedyourhelpmann/"
setwd(workDir)

inData <- read.csv("First and Last of each Floodplain.txt", header=FALSE, sep=",", as.is=TRUE)

begRows <- which(is.na(inData[,2]) & is.na(inData[,3]) & inData[,1]!="END")
endRows <- which(is.na(inData[,2]) & is.na(inData[,3]) & inData[,1]=="END")

outFile <- file("code1_output.txt","w")
for (eachCS in 1:length(begRows)) {
  writeLines(paste(inData[begRows[eachCS],1], inData[begRows[eachCS]+1,3], inData[endRows[eachCS]-1,3]), outFile)
}
close(outFile)
clear


The output of this code will be a text file with:

and so on.....

No comments:

Post a Comment