def quickPlotSimple(avaDir, inputDir, cfg): """ Plot two raster datasets of identical dimension and difference between two datasets figure 1: plot raster data for dataset1, dataset2 and their difference figure 2: plot cross and longprofiles for both datasets (ny_loc and nx_loc define location of profiles) -plots are saved to Outputs/out3Plot Parameters ---------- avaDir : str path to avalanche directory inputDir : str path to directory of input data (only 2 raster files allowed) """ outDir = os.path.join(avaDir, 'Outputs', 'out3Plot') fU.makeADir(outDir) # Get name of Avalanche avaName = os.path.basename(avaDir) # Load input datasets from input directory datafiles = glob.glob(inputDir + os.sep + '*.asc') datafiles.extend(glob.glob(inputDir + os.sep + '*.txt')) name1 = os.path.basename(datafiles[0]) name2 = os.path.basename(datafiles[1]) log.info('input dataset #1 is %s' % name1) log.info('input dataset #2 is %s' % name2) # Load data raster = IOf.readRaster(datafiles[0]) rasterRef = IOf.readRaster(datafiles[1]) data1, data2 = geoTrans.resizeData(raster, rasterRef) header = IOf.readASCheader(datafiles[0]) cellSize = header.cellsize # Create dataDict to be passed to generatePlot dataDict = { 'data1': data1, 'data2': data2, 'name1': name1, 'name2': name2, 'compareType': '', 'cellSize': cellSize } # Initialise plotList plotDict = {'plots': [], 'difference': [], 'stats': []} # Create Plots plotList = generatePlot(dataDict, avaName, outDir, cfg, plotDict)
def getReleaseThickness(avaDir, cfg, demFile): """ define release thickness for Flat Plane solution test """ # Read dem demOri = IOf.readRaster(demFile) nrows = demOri['header'].nrows ncols = demOri['header'].ncols xllc = demOri['header'].xllcenter yllc = demOri['header'].yllcenter csz = demOri['header'].cellsize # define release thickness distribution cfgFP = cfg['FPSOL'] H0 = float(cfgFP['H0']) deltaX = float(cfgFP['deltaX']) slope = float(cfgFP['slope']) x = np.linspace(0, ncols - 1, ncols) * csz + xllc y = np.linspace(0, nrows - 1, nrows) * csz + yllc X, Y = np.meshgrid(x, y) r = np.sqrt((X * X) + (Y * Y)) relTh = H0 - (r - deltaX) * slope relTh = np.where(relTh < 0, 0, relTh) relTh = np.where(relTh > H0, H0, relTh) relDict = {'relTh': relTh, 'demOri': demOri, 'X': X, 'Y': Y} return relDict
def saveInitialParticleDistribution(avaDir, simName, dem): x = np.empty(0) y = np.empty(0) z = np.empty(0) m = np.empty(0) DEM = IOf.readRaster(dem) header = DEM['header'] # Read log file fileName = os.path.join(os.getcwd(), avaDir, 'Outputs', 'com1DFAOrig', 'start%s.log' % (simName)) with open(fileName, 'r') as file: for line in file: if "IPD" in line: ltime = line.split(', ') x = np.append(x, float(ltime[1])) y = np.append(y, float(ltime[2])) z = np.append(z, float(ltime[3])) m = np.append(m, float(ltime[4])) x = x - header.xllcenter y = y - header.yllcenter particles = {'t': 0.0, 'x': x, 'y': y, 'z': z, 'm': m} partDit = os.path.join(os.getcwd(), avaDir, 'Outputs', 'com1DFAOrig', 'particles', simName) fU.makeADir(partDit) savePartToPickle(particles, partDit)
def getReleaseThickness(avaDir, cfg, demFile): """ define release thickness for similarity solution test Parameters ----------- avaDir: str path to avalanche directory cfg: dict confguration settings demFile: str path to DEM file Returns -------- relDict: dict dictionary with info on release thickness distribution """ # Read dem demOri = IOf.readRaster(demFile) nrows = demOri['header'].nrows ncols = demOri['header'].ncols xllc = demOri['header'].xllcenter yllc = demOri['header'].yllcenter csz = demOri['header'].cellsize # define release thickness distribution cfgSimi = cfg['SIMISOL'] L_x = cfgSimi.getfloat('L_x') L_y = cfgSimi.getfloat('L_y') Hini = cfg['GENERAL'].getfloat('relTh') planeinclinationAngleDeg = cfgSimi.getfloat('planeinclinationAngle') x = np.linspace(0, ncols - 1, ncols) * csz + xllc y = np.linspace(0, nrows - 1, nrows) * csz + yllc X, Y = np.meshgrid(x, y) cos = math.cos(math.pi * planeinclinationAngleDeg / 180) sin = math.sin(math.pi * planeinclinationAngleDeg / 180) X1 = X / cos Y1 = Y r = np.sqrt((X * X) / (cos * cos) + (Y * Y)) relTh = Hini * (1 - (r / L_x) * (r / L_y)) relTh = np.where(relTh < 0, 0, relTh) relDict = { 'relTh': relTh, 'X1': X1, 'Y1': Y1, 'demOri': demOri, 'X': X, 'Y': Y, 'cos': cos, 'sin': sin } return relDict
def test_readLine(capfd): '''Simple test for function readLine''' dirname = os.path.dirname(__file__) demFileName = os.path.join(dirname, 'data', 'testShpConv', 'testShpConv.asc') dem = ascUtils.readRaster(demFileName) shpFileName = os.path.join(dirname, 'data', 'testShpConv', 'testLine.shp') # do we react properly when the input line exceeds the dem? with pytest.raises(Exception) as e: assert shpConv.readLine(shpFileName, '', dem) assert str(e.value) == "Nan Value encountered. Try with another path" # do we react properly when the input line exceeds the dem? shpFileName = os.path.join(dirname, 'data', 'testShpConv', 'testLineOut.shp') with pytest.raises(Exception) as e: assert shpConv.readLine(shpFileName, '', dem) assert str(e.value) == "The avalanche path exceeds dem extent. Try with another path" shpFileName = os.path.join(dirname, 'data', 'testShpConv', 'testLineGood.shp') Line = shpConv.readLine(shpFileName, '', dem) # check lines name atol = 1e-10 assert Line['Name'] == ['goodLine'] # check start index lines Sol = np.array([0]) testRes = np.allclose(Line['Start'], Sol, atol=atol) assert testRes # check length lines Sol = np.array([3]) testRes = np.allclose(Line['Length'], Sol, atol=atol) assert testRes # check line x coord Sol = np.array([19.34206385, 35.20773381, 83.14231115]) testRes = np.allclose(Line['x'], Sol, atol=atol) assert testRes # check line y coord Sol = np.array([83.06609712, 72.43272257, 71.42002023]) testRes = np.allclose(Line['y'], Sol, atol=atol) assert testRes # check line z coord Sol = np.array([0., 0., 0.]) testRes = np.allclose(Line['z'], Sol, atol=atol) assert testRes
def test_readPoints(capfd): '''Simple test for function readPoints''' dirname = os.path.dirname(__file__) demFileName = os.path.join(dirname, 'data', 'testShpConv', 'testShpConv.asc') dem = ascUtils.readRaster(demFileName) # do we react properly when the input point exceeds the dem? shpFileName = os.path.join(dirname, 'data', 'testShpConv', 'testLine.shp') with pytest.raises(Exception) as e: assert shpConv.readPoints(shpFileName, dem) assert str(e.value) == 'Nan Value encountered. Try with another split point' # do we react properly when the input point exceeds the dem? shpFileName = os.path.join(dirname, 'data', 'testShpConv', 'testLineOut.shp') with pytest.raises(Exception) as e: assert shpConv.readPoints(shpFileName, dem) assert str(e.value) == 'The split point is not on the dem. Try with another split point'
def transform(fname, rasterTransfo, interpMethod): """ Transfer data from old raster to new raster Affect value to the points of the new raster (after domain transormation) Parameters ---------- fname: str path to rasterfile to transform rasterTransfo: dict transformation information interpMethod: str interpolation method to chose between 'nearest' and 'bilinear' Returns ------- newData: 2D numpy array new_data = z, pressure or depth... corresponding to fname on the new raster """ name = os.path.basename(fname) data = IOf.readRaster(fname) # read tranformation info newGridRasterX = rasterTransfo['gridx'] newGridRasterY = rasterTransfo['gridy'] n, m = np.shape(newGridRasterX) xx = newGridRasterX yy = newGridRasterY Points = {} Points['x'] = xx.flatten() Points['y'] = yy.flatten() iib = len(Points['x']) Points, ioob = geoTrans.projectOnRaster(data, Points, interp=interpMethod) newData = Points['z'].reshape(n, m) log.info( 'Data-file: %s - %d raster values transferred - %d out of original raster bounds!' % (name, iib - ioob, ioob)) return newData
def readDEM(avaDir): """ read the ascii DEM file from a provided avalanche directory Parameters ---------- avaDir : str path to avalanche directory Returns ------- dem : dict dict with header and raster data """ # get dem file name demSource = getDEMPath(avaDir) log.debug('Read DEM: %s' % demSource) dem = IOf.readRaster(demSource) return (dem)
def test_analyzeArea(capfd): '''Simple test for module analyzeArea''' # get input data dirname = os.path.dirname(__file__) dataRef = os.path.join(dirname, 'data', 'refTestAimecTopo.asc') dataSim = os.path.join(dirname, 'data', 'simTestAimecTopo.asc') dataMass = os.path.join(dirname, 'data', '000000.txt') dataMass1 = os.path.join(dirname, 'data', '000001.txt') cfgPath = {} cfgPath['projectName'] = 'NameOfAvalanche' cfgPath['ppr'] = [dataRef, dataSim] cfgPath['massBal'] = [dataMass, dataMass1] pathResult = os.path.join(dirname, 'data') cfgPath['pathResult'] = pathResult cfgPath['dirName'] = 'testAIMEC' cfgPath['referenceFile'] = 0 cfgPath['compType'] = ['singleModule', 'com1DFA'] cfgPath['numSim'] = 2 cfgPath['contCmap'] = True cfg = cfgUtils.getModuleConfig(ana3AIMEC) cfgSetup = cfg['AIMECSETUP'] cfgFlags = cfg['FLAGS'] cfgFlags['savePlot'] = 'True' cfgSetup['resType'] = 'ppr' cfgSetup['thresholdValue'] = '0.9' cfgSetup['contourLevels'] = '0.1|0.5|1' cfgSetup['domainWidth'] = '600' cfgSetup['startOfRunoutAreaAngle'] = '10' avalData = np.array(([None] * 2)) data = IOf.readRaster(dataRef) avalData[0] = np.transpose(data['rasterData']) data = IOf.readRaster(dataSim) avalData[1] = np.transpose(data['rasterData']) newRasters = {} newRasters['newRasterPPR'] = avalData newRasters['newRasterPFD'] = avalData newRasters['newRasterPFV'] = avalData newRasters['newRasterDEM'] = np.transpose(data['rasterData']) rasterTransfo = {} rasterTransfo['s'] = np.linspace(0, 499, 500) rasterTransfo['l'] = np.linspace(0, 99, 100) rasterTransfo['x'] = rasterTransfo['s'] rasterTransfo['y'] = 50 * np.ones(np.shape(rasterTransfo['s'])) rasterTransfo['rasterArea'] = np.ones((500, 100)) rasterTransfo['indStartOfRunout'] = 400 rasterTransfo['startOfRunoutAreaAngle'] = 10 # testing analyzeFields function resAnalysis = ana3AIMEC.postProcessAIMEC(rasterTransfo, newRasters, cfgSetup, cfgPath, cfgFlags) assert (resAnalysis['runout'][0][0] == 449) and (resAnalysis['runout'][1][1] == 419) and (resAnalysis['runout'][2][0] == 50) and (resAnalysis['MMPPR'][1] == 1) assert (resAnalysis['TP'][1] == 800) and (resAnalysis['FN'][1] == 1700) and ( resAnalysis['FP'][1] == 200) and (resAnalysis['TN'][1] == 7300)
def plotAllFields(avaDir, inputDir, outDir, cfg): """ Plot all fields within given directory and save to outDir Parameters ---------- avaDir : str path to avalanche directoy inputDir : str path to input directoy outDir : str path to directoy where plots shall be saved to cfg : dict configuration settings """ # Load all infos on simulations peakFiles = glob.glob(inputDir + os.sep + '*.asc') # create out dir if not allready existing fU.makeADir(outDir) # Loop through peakFiles and generate plot for filename in peakFiles: # Load data raster = IOf.readRaster(filename) data = raster['rasterData'] data = np.ma.masked_where(data == 0.0, data) name = os.path.splitext(os.path.basename(filename))[0] # get header info for file writing header = raster['header'] cellSize = header.cellsize # Set extent of peak file ny = data.shape[0] nx = data.shape[1] Ly = ny * cellSize Lx = nx * cellSize unit = pU.cfgPlotUtils['unit%s' % cfg['GENERAL']['peakVar']] # Figure shows the result parameter data fig = plt.figure(figsize=(pU.figW, pU.figH)) fig, ax = plt.subplots() # choose colormap cmap, _, _, norm, ticks = makePalette.makeColorMap( pU.cmapPres, np.amin(data), np.amax(data), continuous=pU.contCmap) cmap.set_bad('w') im1 = ax.imshow(data, cmap=cmap, extent=[0, Lx, 0, Ly], origin='lower', aspect=nx / ny) pU.addColorBar(im1, ax, ticks, unit) title = str('%s' % name) ax.set_title(title) ax.set_xlabel('x [m]') ax.set_ylabel('y [m]') plotName = os.path.join(outDir, '%s.%s' % (name, pU.outputFormat)) pU.putAvaNameOnPlot(ax, avaDir) fig.savefig(plotName) plt.close('all')
def AIMECIndi(cfgPath, cfg): """ perform AIMEC analysis and generate plots for reports Reads the required files location for ana3AIMEC postpocessing given a path dictionary to the input files Parameters ---------- cfgPath : dict dictionary with paths to data to analyze cfg : configparser configparser with ana3AIMEC settings defined in ana3AIMECCfg.ini Returns ------- rasterTransfo: dict domain transformation information newRasters: dict raster data expressed in the new coordinates resAnalysis: dict results of ana3AIMEC analysis """ # Extract input config parameters cfgSetup = cfg['AIMECSETUP'] cfgFlags = cfg['FLAGS'] interpMethod = cfgSetup['interpMethod'] resType = cfgSetup['resType'] log.info('Prepare data for post-ptocessing') # Make domain transformation log.info( "Creating new deskewed raster and preparing new raster assignment function" ) rasterTransfo = aT.makeDomainTransfo(cfgPath, cfgSetup) ########################################################################### # visualisation # TODO: needs to be moved somewhere else # read reference file nRef = cfgPath['referenceFile'] rasterSource = cfgPath[resType][nRef] anaRaster = IOf.readRaster(rasterSource) slRaster = aT.transform(rasterSource, rasterTransfo, interpMethod) inputData = {} inputData['slRaster'] = slRaster inputData['xyRaster'] = anaRaster['rasterData'] outAimec.visuTransfo(rasterTransfo, inputData, cfgSetup, cfgPath, cfgFlags) ################################################################# # transform resType_data in new raster newRasters = {} # assign pressure data log.debug("Assigning data to deskewed raster") newRasters['newRaster' + resType.upper()] = aT.assignData( cfgPath[resType], rasterTransfo, interpMethod) # assign dem data log.debug("Assigning dem data to deskewed raster") newRasterDEM = aT.assignData([cfgPath['demSource']], rasterTransfo, interpMethod) newRasters['newRasterDEM'] = newRasterDEM[0] # Analyze data log.debug('Analyzing data in path coordinate system') resAnalysis = postProcessAIMECIndi(rasterTransfo, newRasters, cfgSetup, cfgPath, cfgFlags) # ----------------------------------------------------------- # result visualisation + report # ----------------------------------------------------------- log.info('Visualisation of AIMEC results') outAimec.visuRunoutStat(rasterTransfo, resAnalysis, newRasters, cfgSetup, cfgPath, cfgFlags) outAimec.resultVisu(cfgSetup, cfgPath, cfgFlags, rasterTransfo, resAnalysis) return rasterTransfo, newRasters, resAnalysis
def AIMEC2Report(cfgPath, cfg): """ perform AIMEC analysis and generate plots for reports Reads the required files location for ana3AIMEC postpocessing given a path dictionary to the input files Parameters ---------- cfgPath : dict dictionary with paths to data to analyze cfg : configparser configparser with ana3AIMEC settings defined in ana3AIMECCfg.ini Returns ------- rasterTransfo: dict domain transformation information newRasters: dict raster data expressed in the new coordinates resAnalysis: dict results of ana3AIMEC analysis """ # Extract input config parameters cfgSetup = cfg['AIMECSETUP'] cfgFlags = cfg['FLAGS'] interpMethod = cfgSetup['interpMethod'] log.info('Prepare data for post-ptocessing') # Make domain transformation log.info( "Creating new deskewed raster and preparing new raster assignment function" ) rasterTransfo = aT.makeDomainTransfo(cfgPath, cfgSetup) ########################################################################### # visualisation # TODO: needs to be moved somewhere else # read reference file nRef = cfgPath['referenceFile'] rasterSource = cfgPath['ppr'][nRef] pressureRaster = IOf.readRaster(rasterSource) slRaster = aT.transform(rasterSource, rasterTransfo, interpMethod) inputData = {} inputData['slRaster'] = slRaster inputData['xyRaster'] = pressureRaster['rasterData'] outAimec.visuTransfo(rasterTransfo, inputData, cfgSetup, cfgPath, cfgFlags) ################################################################# # transform pressure_data, depth_data and speed_data in new raster newRasters = {} # assign pressure data log.debug("Assigning pressure data to deskewed raster") newRasters['newRasterPPR'] = aT.assignData(cfgPath['ppr'], rasterTransfo, interpMethod) # assign depth data log.debug("Assigning depth data to deskewed raster") newRasters['newRasterPFD'] = aT.assignData(cfgPath['pfd'], rasterTransfo, interpMethod) # assign speed data log.debug("Assigning speed data to deskewed raster") newRasters['newRasterPFV'] = aT.assignData(cfgPath['pfv'], rasterTransfo, interpMethod) # assign dem data log.debug("Assigning dem data to deskewed raster") newRasterDEM = aT.assignData([cfgPath['demSource']], rasterTransfo, interpMethod) newRasters['newRasterDEM'] = newRasterDEM[0] # Analyze data log.debug('Analyzing data in path coordinate system') resAnalysis = postProcessAIMEC(rasterTransfo, newRasters, cfgSetup, cfgPath, cfgFlags) # ----------------------------------------------------------- # result visualisation + report # ----------------------------------------------------------- log.info('Visualisation of AIMEC results') plotName = outAimec.visuRunoutComp(rasterTransfo, resAnalysis, newRasters, cfgSetup, cfgPath, cfgFlags) resAnalysis['slCompPlot'] = { 'Aimec comparison of mean and max values along path': plotName } if cfgFlags.getboolean('flagMass'): plotName = outAimec.visuMass(resAnalysis, cfgPath, cfgFlags) resAnalysis['massAnalysisPlot'] = {'Aimec mass analysis': plotName} return rasterTransfo, newRasters, resAnalysis
def mainAIMEC(cfgPath, cfg): """ Main logic for AIMEC postprocessing Reads the required files location for ana3AIMEC postpocessing given an avalanche directory Parameters ---------- cfgPath : dict dictionary with paths to data to analyze cfg : configparser configparser with ana3AIMEC settings defined in ana3AIMECCfg.ini Returns ------- rasterTransfo: dict domain transformation information newRasters: dict raster data expressed in the new coordinates resAnalysis: dict results of ana3AIMEC analysis """ # Extract input config parameters cfgSetup = cfg['AIMECSETUP'] cfgFlags = cfg['FLAGS'] interpMethod = cfgSetup['interpMethod'] log.info('Prepare data for post-ptocessing') # Make domain transformation log.info( "Creating new deskewed raster and preparing new raster assignment function" ) rasterTransfo = aT.makeDomainTransfo(cfgPath, cfgSetup) ########################################################################### # visualisation # TODO: needs to be moved somewhere else # read reference file nRef = cfgPath['referenceFile'] rasterSource = cfgPath['ppr'][nRef] pressureRaster = IOf.readRaster(rasterSource) slRaster = aT.transform(rasterSource, rasterTransfo, interpMethod) inputData = {} inputData['slRaster'] = slRaster inputData['xyRaster'] = pressureRaster['rasterData'] outAimec.visuTransfo(rasterTransfo, inputData, cfgSetup, cfgPath, cfgFlags) ################################################################# # transform pressure_data, depth_data and speed_data in new raster newRasters = {} # assign pressure data log.debug("Assigning pressure data to deskewed raster") newRasters['newRasterPPR'] = aT.assignData(cfgPath['ppr'], rasterTransfo, interpMethod) # assign depth data log.debug("Assigning depth data to deskewed raster") newRasters['newRasterPFD'] = aT.assignData(cfgPath['pfd'], rasterTransfo, interpMethod) # assign speed data if cfgPath['pfv']: log.debug("Assigning speed data to deskewed raster") newRasters['newRasterPFV'] = aT.assignData(cfgPath['pfv'], rasterTransfo, interpMethod) # assign dem data log.info("Assigning dem data to deskewed raster") newRasterDEM = aT.assignData([cfgPath['demSource']], rasterTransfo, interpMethod) newRasters['newRasterDEM'] = newRasterDEM[0] # Analyze data log.info('Analyzing data in path coordinate system') resAnalysis = postProcessAIMEC(rasterTransfo, newRasters, cfgSetup, cfgPath, cfgFlags) # ----------------------------------------------------------- # result visualisation + report # ----------------------------------------------------------- log.info('Visualisation of AIMEC results') outAimec.visuSimple(rasterTransfo, resAnalysis, newRasters, cfgPath, cfgFlags) if cfgPath['numSim'] == 2: outAimec.visuRunoutComp(rasterTransfo, resAnalysis, newRasters, cfgSetup, cfgPath, cfgFlags) if cfgFlags.getboolean('flagMass'): outAimec.visuMass(resAnalysis, cfgPath, cfgFlags) else: outAimec.visuRunoutStat(rasterTransfo, resAnalysis, newRasters, cfgSetup, cfgPath, cfgFlags) outAimec.resultVisu(cfgSetup, cfgPath, cfgFlags, rasterTransfo, resAnalysis) # ----------------------------------------------------------- # write results to file # ----------------------------------------------------------- log.info('Writing results to file') flagMass = cfgFlags.getboolean('flagMass') outAimec.resultWrite(cfgPath, cfgSetup, flagMass, rasterTransfo, resAnalysis) return rasterTransfo, newRasters, resAnalysis
def plotAllPeakFields(avaDir, cfg, cfgFLAGS, modName): """ Plot all peak fields and return dictionary with paths to plots Parameters ---------- avaDir : str path to avalanche directoy cfg : dict configuration used to perform simulations cfgFLAGS : str general configuration, required to define if plots saved to reports directoy modName : str name of module that has been used to produce data to be plotted Returns ------- plotDict : dict dictionary with info on plots, like path to plot """ # Load all infos on simulations inputDir = os.path.join(avaDir, 'Outputs', modName, 'peakFiles') peakFiles = fU.makeSimDict(inputDir, '', avaDir) demFile = gI.getDEMPath(avaDir) demData = IOf.readRaster(demFile) demField = demData['rasterData'] # Output directory if cfgFLAGS.getboolean('ReportDir'): outDir = os.path.join(avaDir, 'Outputs', modName, 'reports') fU.makeADir(outDir) else: outDir = os.path.join(avaDir, 'Outputs', 'out1Peak') fU.makeADir(outDir) # Initialise plot dictionary with simulation names plotDict = {} for sName in peakFiles['simName']: plotDict[sName] = {} # Loop through peakFiles and generate plot for m in range(len(peakFiles['names'])): # Load names and paths of peakFiles name = peakFiles['names'][m] fileName = peakFiles['files'][m] avaName = peakFiles['avaName'][m] log.debug('now plot %s:' % (fileName)) # Load data raster = IOf.readRaster(fileName) data = raster['rasterData'] # constrain data to where there is data cellSize = peakFiles['cellSize'][m] rowsMin, rowsMax, colsMin, colsMax = pU.constrainPlotsToData( data, cellSize) dataConstrained = data[rowsMin:rowsMax + 1, colsMin:colsMax + 1] demConstrained = demField[rowsMin:rowsMax + 1, colsMin:colsMax + 1] data = np.ma.masked_where(dataConstrained == 0.0, dataConstrained) unit = pU.cfgPlotUtils['unit%s' % peakFiles['resType'][m]] # Set extent of peak file ny = data.shape[0] nx = data.shape[1] Ly = ny * cellSize Lx = nx * cellSize # Figure shows the result parameter data fig = plt.figure(figsize=(pU.figW, pU.figH)) fig, ax = plt.subplots() # choose colormap cmap, _, _, norm, ticks = makePalette.makeColorMap( pU.cmapPres, np.amin(data), np.amax(data), continuous=pU.contCmap) cmap.set_bad(alpha=0) rowsMinPlot = rowsMin * cellSize rowsMaxPlot = (rowsMax + 1) * cellSize colsMinPlot = colsMin * cellSize colsMaxPlot = (colsMax + 1) * cellSize im0 = ax.imshow( demConstrained, cmap='Greys', extent=[colsMinPlot, colsMaxPlot, rowsMinPlot, rowsMaxPlot], origin='lower', aspect='equal') im1 = ax.imshow( data, cmap=cmap, extent=[colsMinPlot, colsMaxPlot, rowsMinPlot, rowsMaxPlot], origin='lower', aspect='equal') pU.addColorBar(im1, ax, ticks, unit) title = str('%s' % name) ax.set_title(title) ax.set_xlabel('x [m]') ax.set_ylabel('y [m]') plotName = os.path.join(outDir, '%s.%s' % (name, pU.outputFormat)) pU.putAvaNameOnPlot(ax, avaDir) fig.savefig(plotName) if cfgFLAGS.getboolean('showPlot'): plt.show() plotPath = os.path.join(os.getcwd(), plotName) plotDict[peakFiles['simName'][m]].update( {peakFiles['resType'][m]: plotPath}) plt.close('all') return plotDict
def quickPlot(avaDir, testDir, suffix, val, parameter, cfg, cfgPlot, rel='', simType='null', comModule='com1DFA', comModule2=''): """ Plot simulation result and compare to reference solution (two raster datasets of identical dimension) and save to Outputs/out3Plot within avalanche directoy figure 1: plot raster data for dataset1, dataset2 and their difference, including a histogram and the cumulative density function of the differences figure 2: plot cross and longprofiles for both datasets (ny_loc and nx_loc define location of profiles) -plots are saved to Outputs/out3Plot Parameters ---------- avaDir : str path to avalanche directory suffix : str result parameter abbreviation (e.g. 'ppr') val : str value of parameter parameter : str parameter that is used to filter simulation results within folder, for example, symType, parameter variation, etc. cfg : dict global configuration settings cfgPlot : dict configuration settings for plots, required for flag if plots shall be shown or only saved rel : str optional - name of release area scenarios simType : str optional - simulation type null or entres Returns ------- plotList : list list of plot dictionaries (path to plots, min, mean and max difference between plotted datasets, max and mean value of reference dataset ) """ # Create required directories workDir = os.path.join(avaDir, 'Work', 'out3Plot') fU.makeADir(workDir) outDir = os.path.join(avaDir, 'Outputs', 'out3Plot') fU.makeADir(outDir) # Initialise plotDictList plotList = [] # Setup input from com1DFA fU.getDFAData(avaDir, workDir, suffix, comModule=comModule) if comModule2 == '': # Get data from reference run fU.getRefData(testDir, workDir, suffix) else: fU.getDFAData(avaDir, workDir, suffix, comModule=comModule2) # prepare data if parameter == 'Mu' or parameter == 'RelTh': data = fU.makeSimDict(workDir, parameter, avaDir) else: data = fU.makeSimDict(workDir, '', avaDir) cellSize = data['cellSize'][0] unit = pU.cfgPlotUtils['unit%s' % suffix] # check if release Area and simType area provided if rel != '': relAreas = [rel] else: # Count the number of release areas relAreas = set(data['releaseArea']) if parameter == 'simType': simType = val for rel in relAreas: # Initialise plotList plotDict = {'relArea': rel, 'plots': [], 'difference': [], 'stats': []} # get list of indices of files that are of correct simulation type and result paramete indSuffix = [-9999, -9999] findComp = True for m in range(len(data['files'])): if data['resType'][m] == suffix and data['releaseArea'][ m] == rel and data[parameter][m] == val and data[ 'simType'][m] == simType: if (data['modelType'][m] == 'dfa') and findComp: indSuffix[0] = m findComp = False elif data['modelType'][m] == cfgPlot['PLOT']['refModel']: indSuffix[1] = m if findComp: log.error('No matching files found') # Load data raster = IOf.readRaster(data['files'][indSuffix[0]]) rasterRef = IOf.readRaster(data['files'][indSuffix[1]]) data1, data2 = geoTrans.resizeData(raster, rasterRef) log.debug('dataset1: %s' % data['files'][indSuffix[0]]) log.debug('dataset2: %s' % data['files'][indSuffix[1]]) # Get name of Avalanche avaName = data['avaName'][indSuffix[0]] # Create dataDict to be passed to generatePlot dataDict = { 'data1': data1, 'data2': data2, 'name1': data['names'][indSuffix[0]], 'name2': data['names'][indSuffix[1]], 'compareType': 'compToRef', 'simName': data['simName'][indSuffix[0]], 'suffix': suffix, 'cellSize': cellSize, 'unit': unit } # Create Plots plotDictNew = generatePlot(dataDict, avaName, outDir, cfg, plotDict) plotList.append(plotDictNew) return plotList
def test_makeDomainTransfo(capfd): '''Simple test for module makeDomainTransfo''' # Extract input file locations cfgPath = {} dir = os.path.dirname(__file__) dirname = os.path.join(dir, 'data', 'testAna3Aimec') pathData = os.path.join(dirname, 'data') profileLayer = glob.glob(os.path.join(dirname, 'LINES', '*aimec*.shp')) cfgPath['profileLayer'] = ''.join(profileLayer) splitPointLayer = glob.glob(os.path.join(dirname, 'POINTS', '*.shp')) cfgPath['splitPointSource'] = ''.join(splitPointLayer) demSource = glob.glob(os.path.join(dirname, '*.asc')) cfgPath['demSource'] = ''.join(demSource) cfgPath['ppr'] = [ os.path.join(pathData, 'testAimec_0.asc'), os.path.join(pathData, 'testAimec_1.asc'), os.path.join(pathData, 'testAimec_2.asc'), os.path.join(pathData, 'testAimec_3.asc'), os.path.join(pathData, 'testAimec_4.asc') ] cfgPath['pfd'] = [ os.path.join(pathData, 'testAimec_0.asc'), os.path.join(pathData, 'testAimec_1.asc'), os.path.join(pathData, 'testAimec_2.asc'), os.path.join(pathData, 'testAimec_3.asc'), os.path.join(pathData, 'testAimec_4.asc') ] cfgPath['pfv'] = [ os.path.join(pathData, 'testAimec_0.asc'), os.path.join(pathData, 'testAimec_1.asc'), os.path.join(pathData, 'testAimec_2.asc'), os.path.join(pathData, 'testAimec_3.asc'), os.path.join(pathData, 'testAimec_4.asc') ] cfgPath['massBal'] = [os.path.join(dirname, '000001.txt')] * 5 cfgPath['contCmap'] = True pathResult = os.path.join(dirname, 'results') cfgPath['pathResult'] = pathResult cfgPath['projectName'] = 'testAna3Aimec' pathName = os.path.basename(profileLayer[0]) cfgPath['pathName'] = pathName cfgPath['dirName'] = 'com1DFA' cfgPath['referenceFile'] = 0 cfgPath['compType'] = ['singleModule', 'com1DFA'] cfg = cfgUtils.getModuleConfig(ana3AIMEC) cfgSetup = cfg['AIMECSETUP'] cfgFlags = cfg['FLAGS'] cfgFlags['savePlot'] = 'False' cfgSetup['startOfRunoutAreaAngle'] = '0' cfgSetup['domainWidth'] = '160' cfgSetup['resType'] = 'ppr' cfgSetup['thresholdValue'] = '0.9' cfgSetup['contourLevels'] = '0.1|0.5|1' cfgPath['numSim'] = 5 rasterTransfo = aT.makeDomainTransfo(cfgPath, cfgSetup) assert rasterTransfo['gridx'][-1, 0] == 60 assert rasterTransfo['gridx'][-1, -1] == 220 assert rasterTransfo['gridy'][0, 0] == 180 assert rasterTransfo['gridy'][0, -1] == 20 assert rasterTransfo['gridy'][-1, -1] == 258 # transform pressure_data, depth_data and speed_data in new raster newRasters = {} # assign pressure data interpMethod = cfgSetup['interpMethod'] newRasters['newRasterPPR'] = aT.assignData(cfgPath['ppr'], rasterTransfo, interpMethod) newRasters['newRasterPFD'] = newRasters['newRasterPPR'] newRasters['newRasterPFV'] = newRasters['newRasterPPR'] newRasterDEM = aT.assignData([cfgPath['demSource']], rasterTransfo, interpMethod) newRasters['newRasterDEM'] = newRasterDEM[0] # Analyze data resAnalysis = ana3AIMEC.postProcessAIMEC(rasterTransfo, newRasters, cfgSetup, cfgPath, cfgFlags) for i in range(5): rasterSource = cfgPath['ppr'][i] sourceData = IOf.readRaster(rasterSource) rasterdata = sourceData['rasterData'] error = (resAnalysis['TP'][i] + resAnalysis['FP'][i] - np.nansum(rasterdata)) / (np.nansum(rasterdata) * 100) assert error < 0.4 assert np.abs(resAnalysis['runout'][0, i] - (240 + 10 * (i + 1))) < 5
def quickPlotBench(avaDir, simNameRef, simNameComp, refDir, compDir, cfg, cfgPlot, suffix): """ Plot simulation result and compare to reference solution (two raster datasets of identical dimension) and save to Outputs/out3Plot within avalanche directoy figure 1: plot raster data for dataset1, dataset2 and their difference, including a histogram and the cumulative density function of the differences figure 2: plot cross and longprofiles for both datasets (ny_loc and nx_loc define location of profiles) -plots are saved to Outputs/out3Plot Parameters ---------- avaDir : str path to avalanche directory suffix : str result parameter abbreviation (e.g. 'ppr') val : str value of parameter parameter : str parameter that is used to filter simulation results within folder, for example, symType, parameter variation, etc. cfg : dict global configuration settings cfgPlot : dict configuration settings for plots, required for flag if plots shall be shown or only saved rel : str optional - name of release area scenarios simType : str optional - simulation type null or entres Returns ------- plotList : list list of plot dictionaries (path to plots, min, mean and max difference between plotted datasets, max and mean value of reference dataset ) """ # Create required directories outDir = os.path.join(avaDir, 'Outputs', 'out3Plot') fU.makeADir(outDir) # Initialise plotDictList plotList = [] # Initialise plotList plotDict = {'plots': [], 'difference': [], 'stats': []} simRefFile = os.path.join(refDir, simNameRef + '_' + suffix + '.asc') simCompFile = os.path.join(compDir, simNameComp + '_' + suffix + '.asc') if os.path.isfile(simRefFile) == False or os.path.isfile( simCompFile) == False: log.error('File for result type: %s not found' % suffix) # Load data raster = IOf.readRaster(simCompFile) rasterRef = IOf.readRaster(simRefFile) data1, data2 = geoTrans.resizeData(raster, rasterRef) log.debug('dataset1: %s' % simCompFile) log.debug('dataset2: %s' % simRefFile) cellSize = rasterRef['header'].cellsize unit = pU.cfgPlotUtils['unit%s' % suffix] # Get name of Avalanche avaName = os.path.basename(avaDir) # Create dataDict to be passed to generatePlot dataDict = { 'data1': data1, 'data2': data2, 'name1': simNameComp + '_' + suffix, 'name2': simNameRef + '_' + suffix, 'compareType': 'compToRef', 'simName': simNameComp, 'suffix': suffix, 'cellSize': cellSize, 'unit': unit } # Create Plots plotDictNew = generatePlot(dataDict, avaName, outDir, cfg, plotDict) return plotDictNew
def com2ABMain(cfg, avalancheDir): """ Main AlphaBeta model function Loops on the given AvaPaths and runs com2AB to compute AlpahBeta model Parameters ---------- cfg : configparser configparser with all requiered fields in com2ABCfg.ini avalancheDir : str path to directory of avalanche to analyze Returns ------- resAB : dict dictionary with AlphaBeta model results """ abVersion = '4.1' cfgsetup = cfg['ABSETUP'] smallAva = cfgsetup.getboolean('smallAva') resAB = {} # Extract input file locations cfgPath = readABinputs(avalancheDir) log.info( "Running com2ABMain model on DEM \n \t %s \n \t with profile \n \t %s ", cfgPath['demSource'], cfgPath['profileLayer']) resAB['saveOutPath'] = cfgPath['saveOutPath'] # Read input data for ALPHABETA dem = IOf.readRaster(cfgPath['demSource']) resAB['dem'] = dem AvaPath = shpConv.readLine(cfgPath['profileLayer'], cfgPath['defaultName'], dem) resAB['AvaPath'] = AvaPath resAB['splitPoint'] = shpConv.readPoints(cfgPath['splitPointSource'], dem) # Read input setup eqParams = setEqParameters(cfg, smallAva) resAB['eqParams'] = eqParams NameAva = AvaPath['Name'] StartAva = AvaPath['Start'] LengthAva = AvaPath['Length'] for i in range(len(NameAva)): name = NameAva[i] start = StartAva[i] end = start + LengthAva[i] avapath = {} avapath['x'] = AvaPath['x'][int(start):int(end)] avapath['y'] = AvaPath['y'][int(start):int(end)] avapath['Name'] = name log.info('Running Alpha Beta %s on: %s ', abVersion, name) resAB = com2ABKern(avapath, resAB, cfgsetup.getfloat('distance'), cfgsetup.getfloat('dsMin')) if cfg.getboolean('FLAGS', 'fullOut'): # saving results to pickle saveABResults(resAB, name) savename = name + '_com2AB_eqparam.pickle' save_file = os.path.join(cfgPath['saveOutPath'], savename) pickle.dump(resAB['eqParams'], open(save_file, "wb")) log.info('Saving intermediate results to: %s' % (save_file)) savename = name + '_com2AB_eqout.pickle' save_file = os.path.join(cfgPath['saveOutPath'], savename) pickle.dump(resAB[name], open(save_file, "wb")) log.info('Saving intermediate results to: %s' % (save_file)) return resAB
def makeDomainTransfo(cfgPath, cfgSetup): """ Make domain transformation This function returns the information about the domain transformation Data given on a regular grid is projected on a nonuniform grid following a polyline to end up with "straightend raster" Parameters ---------- cfgPath : dict dictionary with path to data to analyze cfgSetup : configparser configparser with ana3AIMEC settings defined in ana3AIMECCfg.ini regarding domain transformation (domain width w, startOfRunoutAreaAngle or interpolation method) Returns ------- rasterTransfo: dict domain transformation information: gridx: 2D numpy array x coord of the new raster points in old coord system gridy: 2D numpy array y coord of the new raster points in old coord system s: 1D numpy array new coord system in the polyline direction l: 1D numpy array new coord system in the cross direction x: 1D numpy array coord of the resampled polyline in old coord system y: 1D numpy array coord of the resampled polyline in old coord system rasterArea: 2D numpy array real area of the cells of the new raster indStartOfRunout: int index for start of the runout area (in s) """ # Read input parameters demSource = cfgPath['demSource'] ProfileLayer = cfgPath['profileLayer'] splitPointSource = cfgPath['splitPointSource'] DefaultName = cfgPath['projectName'] w = float(cfgSetup['domainWidth']) startOfRunoutAreaAngle = float(cfgSetup['startOfRunoutAreaAngle']) log.info('Data-file %s analysed' % demSource) # read data # read dem data dem = IOf.readRaster(demSource) header = dem['header'] xllc = header.xllcenter yllc = header.yllcenter cellSize = header.cellsize rasterdata = dem['rasterData'] # Initialize transformation dictionary rasterTransfo = {} rasterTransfo['domainWidth'] = w rasterTransfo['xllc'] = xllc rasterTransfo['yllc'] = yllc rasterTransfo['cellSize'] = cellSize # read avaPath avaPath = shpConv.readLine(ProfileLayer, DefaultName, dem) # read split point splitPoint = shpConv.readPoints(splitPointSource, dem) # add 'z' coordinate to the avaPath avaPath, _ = geoTrans.projectOnRaster(dem, avaPath) # reverse avaPath if necessary _, avaPath = geoTrans.checkProfile(avaPath, projSplitPoint=None) log.info('Creating new raster along polyline: %s' % ProfileLayer) # Get new Domain Boundaries DB # input: ava path # output: Left and right side points for the domain rasterTransfo = geoTrans.path2domain(avaPath, rasterTransfo) # Make transformation matrix rasterTransfo = makeTransfoMat(rasterTransfo) # calculate the real area of the new cells as well as the scoord rasterTransfo = getSArea(rasterTransfo) log.debug('Size of rasterdata- old: %d x %d - new: %d x %d' % (np.size(rasterdata, 0), np.size( rasterdata, 1), np.size(rasterTransfo['gridx'], 0), np.size(rasterTransfo['gridx'], 1))) ########################################################################## rasterTransfo['header'] = header # put back scale and origin rasterTransfo['s'] = rasterTransfo['s'] * cellSize rasterTransfo['l'] = rasterTransfo['l'] * cellSize rasterTransfo['gridx'] = rasterTransfo['gridx'] * cellSize + xllc rasterTransfo['gridy'] = rasterTransfo['gridy'] * cellSize + yllc rasterTransfo[ 'rasterArea'] = rasterTransfo['rasterArea'] * cellSize * cellSize # (x,y) coordinates of the resamples avapth (centerline where l = 0) n = np.shape(rasterTransfo['l'])[0] indCenter = int(np.floor(n / 2)) rasterTransfo['x'] = rasterTransfo['gridx'][:, indCenter] rasterTransfo['y'] = rasterTransfo['gridy'][:, indCenter] ################################################################# # add 'z' coordinate to the centerline rasterTransfo, _ = geoTrans.projectOnRaster(dem, rasterTransfo) # find projection of split point on the centerline centerline projPoint = geoTrans.findSplitPoint(rasterTransfo, splitPoint) rasterTransfo['indSplit'] = projPoint['indSplit'] # prepare find start of runout area points angle, tmp, ds = geoTrans.prepareAngleProfile(startOfRunoutAreaAngle, rasterTransfo) # find the runout point: first point under startOfRunoutAreaAngle indStartOfRunout = geoTrans.findAngleProfile(tmp, ds, cfgSetup.getfloat('dsMin')) rasterTransfo['indStartOfRunout'] = indStartOfRunout rasterTransfo['xBetaPoint'] = rasterTransfo['x'][indStartOfRunout] rasterTransfo['yBetaPoint'] = rasterTransfo['y'][indStartOfRunout] rasterTransfo['startOfRunoutAreaAngle'] = angle[indStartOfRunout] log.info( 'Measuring run-out length from the %.2f ° point of coordinates (%.2f, %.2f)' % (rasterTransfo['startOfRunoutAreaAngle'], rasterTransfo['xBetaPoint'], rasterTransfo['yBetaPoint'])) return rasterTransfo