Dynamic map displaying

Map is displayed at every iteration if matplotlib library is installed
This commit is contained in:
Gael-de-Sailly 2020-11-14 12:05:52 +01:00
parent 3fda369fb5
commit 8a15bc924d
2 changed files with 53 additions and 31 deletions

View File

@ -9,6 +9,8 @@ import os
import sys
import settings
import view_map
# Always place in this script's parent directory
os.chdir(os.path.dirname(sys.argv[0]))
argc = len(sys.argv)
@ -65,6 +67,7 @@ nn = n*vscale + offset
# Initialize landscape evolution model
print('Initializing model')
model = EvolutionModel(nn, K=1, m=0.35, d=1, sea_level=0, flex_radius=flex_radius)
view_map.update(model.dem, model.lakes, t=5, title='Initializing...')
dt = time/niter
@ -73,7 +76,9 @@ print('Initial flow calculation')
model.calculate_flow()
for i in range(niter):
print('Iteration {:d} of {:d}'.format(i+1, niter))
disp_niter = 'Iteration {:d} of {:d}...'.format(i+1, niter)
view_map.update(model.dem, model.lakes, title=disp_niter)
print(disp_niter)
print('Diffusion')
model.diffusion(dt)
print('Advection')
@ -83,7 +88,7 @@ for i in range(niter):
print('Flow calculation')
model.calculate_flow()
print('Done')
print('Done!')
# Twist the grid
bx, by = bounds.make_bounds(model.dirs, model.rivers)
@ -108,9 +113,5 @@ save(model.rivers, 'rivers', dtype='>u4')
with open('size', 'w') as sfile:
sfile.write('{:d}\n{:d}'.format(mapsize+1, mapsize+1))
# Display the map if matplotlib is found
try:
from view_map import view_map
view_map(model.dem, model.lakes, model.rivers)
except:
pass
view_map.stats(model.dem, model.lakes)
view_map.plot(model.dem, model.lakes, title='Final map')

View File

@ -20,8 +20,8 @@ try:
except ImportError: # No module matplotlib
has_matplotlib = False
def view_map(dem, lakes, scale):
if has_matplotlib:
def view_map(dem, lakes, scale=1, title=None):
if not has_matplotlib:
return
lakes_sea = np.maximum(lakes, 0)
@ -39,14 +39,34 @@ def view_map(dem, lakes, scale):
plt.imshow(np.flipud(water), alpha=np.flipud(alpha), cmap=cmap2, extent=extent, vmin=0, vmax=max_depth, interpolation='antialiased')
sm1 = plt.cm.ScalarMappable(cmap=cmap1, norm=plt.Normalize(vmin=0, vmax=max_elev))
plt.colorbar(sm1).set_label('Altitude')
plt.colorbar(sm1).set_label('Elevation')
sm2 = plt.cm.ScalarMappable(cmap=cmap2, norm=plt.Normalize(vmin=0, vmax=max_depth))
plt.colorbar(sm2).set_label('Profondeur d\'eau')
plt.colorbar(sm2).set_label('Water depth')
plt.xlabel('X')
plt.ylabel('Z')
if title is not None:
plt.title(title, fontweight='bold')
def update(*args, t=0.01, **kwargs):
plt.clf()
view_map(*args, **kwargs)
plt.pause(t)
def plot(*args, **kwargs):
plt.clf()
view_map(*args, **kwargs)
plt.show()
def map_stats(dem, lake_dem, scale):
else:
def update(*args, **kwargs):
pass
def plot(*args, **kwargs):
pass
def stats(dem, lake_dem, scale=1):
surface = dem.size
continent = lake_dem >= 0
@ -57,6 +77,7 @@ def map_stats(dem, lake_dem, scale):
print('--- General ---')
print('Grid size: {:5d}x{:5d}'.format(dem.shape[0], dem.shape[1]))
if scale > 1:
print('Map size: {:5d}x{:5d}'.format(int(dem.shape[0]*scale), int(dem.shape[1]*scale)))
print()
print('--- Surfaces ---')
@ -94,5 +115,5 @@ if __name__ == "__main__":
dem = load_map('dem', '>i2', shape)
lakes = load_map('lakes', '>i2', shape)
map_stats(dem, lakes, scale)
view_map(dem, lakes, scale)
stats(dem, lakes, scale=scale)
plot(dem, lakes, scale)