Skip to content

Instantly share code, notes, and snippets.

Created May 21, 2013 19:17
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save anonymous/df899701271a62ff4543 to your computer and use it in GitHub Desktop.
Save anonymous/df899701271a62ff4543 to your computer and use it in GitHub Desktop.
#Extract data from RAS Geometry files
#@skulk001
########################################################################
import os
import re
import time
import csv
from operator import floordiv
folderLocation = "x:\folderlocation\Models"
listSubFolders = os.listdir(folderLocation)
flowChangeLocationsTable = []
streamGeometry = {}
for subFolder in listSubFolders:
subFolderPath = folderLocation + "\\" + subFolder
if os.path.isdir(subFolderPath):
listSubFolderFiles = os.listdir(subFolderPath)
for file in listSubFolderFiles:
#Geometry files in hecras have a ".g0*" extension, the code below filters to look that geometry files only
if file.find(".g") != -1:
openFile = open(subFolderPath + "\\" + file, "r")
readFile= openFile.read()
#pattern matching to extract specific portion of the file
pattern2 = re.compile("(?<=\n#Mann=).+(?![A-z]+)")
pattern3 = re.compile(r"(?<=Type RM Length L Ch R =).+(\d+)")
manningsCoeff = pattern2.finditer(readFile)
stations = pattern3.finditer(readFile)
stationsList = []
for station in stations:
if int(station.group().split(",")[0].strip()) == 1:
stationsList.append(station.group().split())
manningsCoeffList = []
for coeffs in manningsCoeff:
numberOfIter = floordiv(int(coeffs.group().split(",")[0])-1, 3) + 1
newStartLocation = coeffs.start() + readFile[coeffs.start():].find("\n") + 1
TempList = []
while numberOfIter > 0:
line = readFile[newStartLocation:newStartLocation + readFile[newStartLocation:].find("\n")]
newStartLocation = newStartLocation + readFile[newStartLocation:].find("\n") + 1
numberOfIter = numberOfIter - 1
for item in line.split():
TempList.append(item)
manningsCoeffList.append(TempList)
streamGeometryData = map(list.__add__,stationsList,manningsCoeffList)
streamGeometry[file] = streamGeometryData
#CSV generation
outputCheckMannings = csv.writer(open(folderLocation + "\\" +'streamGeometry'+ str(time.gmtime().tm_sec) + '.csv', 'wb'))
list = []
for key,value in streamGeometry.items():
temp = []
for i in range(len(value)):
temp1 = []
temp1.append(key)
for item in value[i]:
temp1.append(item)
temp.append(temp1)
for items in temp:
finalRow = []
for item in items:
for i in item.split(","):
finalRow.append(i)
outputCheckMannings.writerow(finalRow)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment