From bf1bf5bb6604f2f83b08ecb4cf99681ab1745d1e Mon Sep 17 00:00:00 2001 From: Anna Wirbel Date: Mon, 14 Aug 2023 13:09:20 +0200 Subject: [PATCH] add release scenario plot com1DFA --- avaframe/com1DFA/com1DFA.py | 3 ++ avaframe/out3Plot/outCom1DFA.py | 64 +++++++++++++++++++++++++++++++-- avaframe/out3Plot/plotUtils.py | 2 ++ avaframe/tests/test_com1DFA.py | 4 ++- 4 files changed, 70 insertions(+), 3 deletions(-) diff --git a/avaframe/com1DFA/com1DFA.py b/avaframe/com1DFA/com1DFA.py index 3f0f30fc9..83ea53be8 100644 --- a/avaframe/com1DFA/com1DFA.py +++ b/avaframe/com1DFA/com1DFA.py @@ -941,6 +941,9 @@ def initializeSimulation(cfg, outDir, demOri, inputSimLines, logName): # if relTh provided - set release thickness with field or function releaseLine = prepareArea(releaseLine, dem, np.sqrt(2), combine=True, checkOverlap=False) + # plot release area scenario + outCom1DFA.plotReleaseScenarioView(cfgGen['avalancheDir'], releaseLine, dem, + ('Release Scenario %s' % inputSimLines['releaseLine']['file'].stem), logName) # compute release area header = dem['header'] csz = header['cellsize'] diff --git a/avaframe/out3Plot/outCom1DFA.py b/avaframe/out3Plot/outCom1DFA.py index 283f94f81..f1f7431af 100644 --- a/avaframe/out3Plot/outCom1DFA.py +++ b/avaframe/out3Plot/outCom1DFA.py @@ -238,7 +238,7 @@ def addParticles2Plot(particles, ax, dem, whatS='m', whatC='h', colBarResType='' return ax, cb -def addDem2Plot(ax, dem, what='slope', extent=''): +def addDem2Plot(ax, dem, what='slope', extent='', origHeader=False): """ Add dem to the background of a plot Parameters @@ -250,8 +250,13 @@ def addDem2Plot(ax, dem, what='slope', extent=''): what information about the dem will be plotted? slope: use the dem slope (computed from the normals) to color the plot z : use the elevation to color the plot + origHeader: bool + if True use originalHeader and not header """ - header = dem['header'] + if origHeader: + header = dem['originalHeader'] + else: + header = dem['header'] ncols = header['ncols'] nrows = header['nrows'] xllc = header['xllcenter'] @@ -446,3 +451,58 @@ def fetchContCoors(demHeader, flowF, cfgVisu, simName): contDictXY[simName]['thresholdValue'] = cfgVisu.getfloat('thresholdValue') return contDictXY + + +def plotReleaseScenarioView(avaDir, releaseLine, dem, titleFig, cuSimName): + """ plot release polygon, area with thickness on dem hillshade + saved to avaDir/Outputs/com1DFA/reports + + Parameters + ------------ + avaDir: str + path to ava directory + dem: dict + dict with dem header and data + releaseLine: dict + dict with raster of release line and x,y coors + titleFig: str + title of figure + cuSimName: str + name of simulation + + """ + + ny = releaseLine['rasterData'].shape[0] + nx = releaseLine['rasterData'].shape[1] + Ly = ny * dem['originalHeader']['cellsize'] + Lx = nx * dem['originalHeader']['cellsize'] + xL = dem['originalHeader']['xllcenter'] + yL = dem['originalHeader']['yllcenter'] + originCells = dem['header']['cellsize'] * 0.5 + rField = np.ma.masked_where(releaseLine['rasterData'] == 0.0, releaseLine['rasterData']) + + # choose colormap + cmap1, col, ticks, norm = pU.makeColorMap( + pU.colorMaps['pft'], np.amin(releaseLine['rasterData']), np.amax(releaseLine['rasterData']), + continuous=pU.contCmap) + cmap1.set_bad(alpha=0.) + extentCells = [xL - originCells, xL + Lx - originCells, yL + Ly - originCells, yL - originCells] + extentDem = [xL, xL + Lx, yL + Ly, yL] + + # create figure + fig, ax = plt.subplots() + addDem2Plot(ax, dem, what='hillshade', extent=extentDem, origHeader=True) + im1 = ax.imshow(rField, extent=extentCells, cmap=cmap1) + ax.plot(releaseLine['x'], releaseLine['y'], 'b-', label='release polygon') + ax.set_aspect('equal') + cax = ax.inset_axes([1.04, 0.0, 0.05, 1.]) + pU.addColorBar(im1, ax, ticks, 'm', cax=cax) + plt.legend(fontsize=8) + plt.title(titleFig) + pU.putAvaNameOnPlot(ax, avaDir) + + # save and or plot + plotName = 'releaseScenario_%s' % cuSimName + outDir = pathlib.Path(avaDir, 'Outputs', 'com1DFA', 'reports') + fU.makeADir(outDir) + pU.saveAndOrPlot({"pathResult": outDir}, plotName, fig) diff --git a/avaframe/out3Plot/plotUtils.py b/avaframe/out3Plot/plotUtils.py index 638c87842..ed23c416a 100644 --- a/avaframe/out3Plot/plotUtils.py +++ b/avaframe/out3Plot/plotUtils.py @@ -517,6 +517,7 @@ def addColorBar( extend="neither", pad=0.05, tickLabelsList="", + cax=None, ): """ Adds, styles and labels a colorbar to the given image and axes @@ -529,6 +530,7 @@ def addColorBar( extend=extend, pad=pad, shrink=0.9, + cax=cax, ) cbar.outline.set_visible(False) # make sure the cbar title does not overlap with the cbar itself diff --git a/avaframe/tests/test_com1DFA.py b/avaframe/tests/test_com1DFA.py index 0d07b6c7a..d6743e2a7 100644 --- a/avaframe/tests/test_com1DFA.py +++ b/avaframe/tests/test_com1DFA.py @@ -1649,13 +1649,15 @@ def test_initializeSimulation(tmp_path): demOri = {'header': demHeader, 'rasterData': demData} # setup release line, entrainment line + relFileTest = pathlib.Path(tmp_path, 'testRel.shp') releaseLine = {'x': np.asarray([6.9, 8.5, 8.5, 6.9, 6.9]), 'y': np.asarray([7.9, 7.9, 9.5, 9.5, 7.9]), 'Start': np.asarray([0]), 'Length': np.asarray([5]), 'Name': [''], 'thickness': [1.0], - 'thicknessSource': ['ini File'], 'type': 'release'} + 'thicknessSource': ['ini File'], 'type': 'release', 'file': relFileTest} entLine = {'fileName': 'test/entTest.shp', 'Name': ['testEnt'], 'Start': np.asarray([0.]), 'thickness': [0.3, 0.3], 'thicknessSource': ['shp file', 'shp file'], 'Length': np.asarray([5]), 'x': np.asarray([4, 5., 5.0, 4., 4.]), 'type': 'entrainment', 'y': np.asarray([4., 4., 5.0, 5., 4.0])} + inputSimLines = {'releaseLine': releaseLine, 'entResInfo': {'flagSecondaryRelease': 'No'}, 'entLine': entLine, 'resLine': '', 'relThField': '', 'damLine': None} # set release thickness read from file or not