# -*- coding: utf-8 -*- """ Created on Tue Feb 20 10:39:51 2018 @author: noordam """ import csv import helpFunctions import re import sys import os #============================================================================== # read surfaceLine #============================================================================== def ReadSurfaceLine(surfaceLineFileName,lineNumber): with open(surfaceLineFileName,'rb') as csvfile: reader = csv.reader(csvfile,delimiter=';', quotechar='|') # print spamreader.row definitions = next(reader) for i in range(lineNumber-1): next(reader) row = next(reader) row = filter(None,row) originalProfileName =row[0].replace(',','.'); # geologicalProfile =row.index('geologicalprofile') #============================================================================== # geologicalProfile =row[1].replace(',','.'); # xGridPoint = row[2].replace(',','.'); # yGridPoint = row[3].replace(',','.'); # scenarioClusterID = row[4].replace(',','.'); #============================================================================== additionalInfo = []; additionalInfo.append(originalProfileName); #============================================================================== # additionalInfo.append(geologicalProfile); # additionalInfo.append(xGridPoint); # additionalInfo.append(yGridPoint); # additionalInfo.append(scenarioClusterID); #============================================================================== x1Index = definitions.index('X1') nColsInputSurfaceLine = len(row) xCoordsOriginalSurfaceLine = row[x1Index:nColsInputSurfaceLine:3]; yCoordsOriginalSurfaceLine = row[x1Index+2:nColsInputSurfaceLine:3]; xCoordsOriginalSurfaceLine =[float(i.replace(',','.')) for i in xCoordsOriginalSurfaceLine] yCoordsOriginalSurfaceLine =[float(i.replace(',','.')) for i in yCoordsOriginalSurfaceLine] for i in range(1,x1Index): info = row[i].replace(',','.'); additionalInfo.append(info); return xCoordsOriginalSurfaceLine, yCoordsOriginalSurfaceLine, additionalInfo,definitions #============================================================================== # read characteristic points #============================================================================== def ReadCharacteristicPoints(charPointsFileName,lineNumber): with open(charPointsFileName,'rb') as csvfile: reader = csv.reader(csvfile,delimiter=';', quotechar='|') # print spamreader.row definitions = next(reader) for i in range(lineNumber-1): next(reader) row = next(reader) nColsInputCharacteristicPoints = len(row) xCoordsOriginalCharacteristicPoints = row[1:nColsInputCharacteristicPoints-1:3]; yCoordsOriginalCharacteristicPoints = row[3:nColsInputCharacteristicPoints:3]; xCoordsOriginalCharacteristicPoints =[float(i.replace(',','.')) for i in xCoordsOriginalCharacteristicPoints] yCoordsOriginalCharacteristicPoints =[float(i.replace(',','.')) for i in yCoordsOriginalCharacteristicPoints] return xCoordsOriginalCharacteristicPoints, yCoordsOriginalCharacteristicPoints,definitions #============================================================================== # Read locations, scenarios and segments #============================================================================== def ReadOtherCsv(fileName,lineNumber): with open(fileName,'rb') as csvfile: reader = csv.reader(csvfile,delimiter=';', quotechar='|') # print spamreader.row definitions = next(reader) for i in range(lineNumber-1): next(reader) data = next(reader) data = [i.replace(',','.') for i in data] return definitions, data #============================================================================== # write the new surface line to a file #============================================================================== def WriteSurfaceLine(nLine, definitions,xCoords,zCoords,additionalInfo,safeFileName): newRow =[] newRow.extend(additionalInfo) for i in range(len(xCoords)): newRow.extend([xCoords[i]]) newRow.extend([0]) newRow.extend([zCoords[i]]) with open(safeFileName,'ab') as csvFile: writer = csv.writer(csvFile, delimiter =';', quotechar='|') if (nLine==0): writer.writerow(definitions) writer.writerow(newRow) #============================================================================== # write characteristicPoints to a new file #============================================================================== def WriteCharacteristicPoints(nLine, definitions,xCoords,zCoords,profileName,safeFileName): #combine all the coordinates in 1 row newRow =[] newRow.extend([profileName]) for i in range(len(xCoords)): newRow.extend([xCoords[i]]) if (xCoords[i]==-1 and zCoords[i]==-1): newRow.extend([-1]) else: newRow.extend([0]) newRow.extend([zCoords[i]]) newRow.extend([0]) with open(safeFileName,'ab') as csvFile: writer = csv.writer(csvFile, delimiter =';', quotechar='|') if (nLine==0): writer.writerow(definitions) writer.writerow(newRow) return # ============================================================================= # add forbidden line # ============================================================================= def AddForbiddenLine(x1,x2,z1,z2,xCoordsSurfaceLine,zCoordsSurfaceLine,fileName): indexesWithinLine = helpFunctions.FindIndexesInRange(x1,x2,xCoordsSurfaceLine,"surfaceLine") xCoordsForbiddenLine=[] zCoordsForbiddenLine=[] for ii in range(len(indexesWithinLine)): xCoordsForbiddenLine.append(xCoordsSurfaceLine[indexesWithinLine[ii]]) zCoordsForbiddenLine.append(zCoordsSurfaceLine[indexesWithinLine[ii]]) xCoordsForbiddenLine.append(x2) zCoordsForbiddenLine.append(z2) stiFile = open(fileName, 'r') stiFileLines= stiFile.readlines() stiFile.close for ii in range(len(stiFileLines)): if '[UNIT WEIGHT WATER]' in stiFileLines[ii]: stiFileLines.insert(ii,'[FORBIDDEN LINES] \n') ii+=1 stiFileLines.insert(ii,str(len(xCoordsForbiddenLine)-1) + ' - Forbidden lines \n') ii+=1 for jj in range(len(xCoordsForbiddenLine)-1): stiFileLines.insert(ii,str(jj+1)+ "\n") ii+=1 stiFileLines.insert(ii,str(xCoordsForbiddenLine[jj])+' '+str(zCoordsForbiddenLine[jj]) + ' - X1 Y1 \n') ii+=1 stiFileLines.insert(ii,str(xCoordsForbiddenLine[jj+1])+' '+str(zCoordsForbiddenLine[jj+1]) + ' - X2 Y2 \n') ii+=1 stiFileLines.insert(ii,'[END OF FORBIDDEN LINES] \n') ii+=1 break #outputStiName = fileName.replace(".sti","_X"+str(x1)+".sti") outputStiName = fileName newStiFile=open(outputStiName,'w') newStiFile.writelines(stiFileLines) test=1+1; # ============================================================================= # # def ReadStiFile(fileName): # stiFile = open(fileName, 'r') # stiFileLines= stiFile.readlines() # stiFile.close # # soilLayer=[] # soilIsAquifer=[] # aquiferSoils=[] # topBoundary=[] # point =[] # #boundaryTopLayer # foundSoilInList=False # foundLayers=False # # for ii in range(len(stiFileLines)): # if (('[SOIL]' in stiFileLines[ii]) or (foundSoilInList == True)): # if ('[SOIL]' in stiFileLines[ii]): # foundSoilInList=True # soilLayer.append(stiFileLines[ii+1]) # if "SoilIsAquifer" in stiFileLines[ii]: # soilIsAquifer.append(int(re.search(r'\d+', stiFileLines[ii]).group())) # if soilIsAquifer[-1] == 1: # aquiferSoils.append(soilLayer[-1]) # foundSoilInList = False # if '[POINTS]' in stiFileLines[ii]: # nPoints = int(re.search(r'\d+', stiFileLines[ii+1]).group()) # for jj in range(nPoints): # point.append([float(x) for x in stiFileLines[ii+jj+2].split()]) # if '[CURVES]' in stiFileLines[ii]: # nCurves = int(re.search(r'\d+', stiFileLines[ii+1]).group()) # for jj in range(nCurves): # # @@!@!!@!! point.append([float(x) for x in stiFileLines[ii+jj+2].split()]) # # if '[LAYERS]' in stiFileLines[ii] or (foundLayers==True): # foundLayers=True # for jj in range(len(aquiferSoils)): # if (aquiferSoils[jj] in stiFileLines[ii+3]): # topBoundary.append(int(re.search(r'\d+', stiFileLines[ii+3]).group())) # if (len(aquiferSoils)==len(topBoundary)): # foundLayers = False # # test=1+1 # # ============================================================================= def WriteOtherCsv(nLine, definitions, data, safeFileName): with open(safeFileName,'ab') as csvFile: writer = csv.writer(csvFile, delimiter =';', quotechar='|') if (nLine==0): writer.writerow(definitions) writer.writerow(data) def WritePythonInformationForDamKernel(damExecutableFolder): with open(damExecutableFolder+'PythonInformation','w') as cSharpFile: cSharpFile.write(sys.executable+'\n') cSharpFile.write(os.getcwd()+'\n') cSharpFile.write('AddForbiddenLine = 1\n') cSharpFile.write('AddMaxXEntrance = 1\n')