Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add plot of release scenario [com1] #894

Merged
merged 1 commit into from
Aug 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions avaframe/com1DFA/com1DFA.py
Original file line number Diff line number Diff line change
Expand Up @@ -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']
Expand Down
64 changes: 62 additions & 2 deletions avaframe/out3Plot/outCom1DFA.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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']
Expand Down Expand Up @@ -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)
2 changes: 2 additions & 0 deletions avaframe/out3Plot/plotUtils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
4 changes: 3 additions & 1 deletion avaframe/tests/test_com1DFA.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down