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

view --dem-blend: support data/DEM in diff resolutions #1102

Merged
merged 1 commit into from
Oct 6, 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
1 change: 1 addition & 0 deletions .github/ISSUE_TEMPLATE/bug_report.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,5 @@ PASTE ERROR MESSAGE HERE
* Operating system: <!-- macOS, Linux, Windows, etc. -->
* Python environment: <!-- conda, macports, pip, path setup, etc. -->
* MintPy version: <!-- output of `smallbaselineApp.py -v` -->
* InSAR processor/product: <!-- isce2, aria, fringe, miaplpy, hyp3, gamma, snap, roipac, etc. -->
* Your custom / default template file (if the bug is related to a specific dataset): <!-- It helps the diagnose a lot if you could post the configurations you used. You can drag-and-drop them here directly. -->
3 changes: 3 additions & 0 deletions src/mintpy/cli/view.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,9 @@ def cmd_line_parse(iargs=None):
# --dem-blend option requires --dem option
if inps.dem_file is None:
parser.error("--dem-blend requires -d/-dem.")
# --cbar-ext option is ignored
if '--cbar-ext' in inps.argv:
print('WARNING: --cbar-ext is NOT compatiable with --dem-blend, ignore --cbar-ext and continue.')

# check: conflicted options (geo-only options if inpput file is in radar-coordinates)
geo_opt_names = ['--coord', '--show-gps', '--coastline', '--lalo-label', '--lalo-step', '--scalebar', '--faultline']
Expand Down
2 changes: 1 addition & 1 deletion src/mintpy/utils/arg_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ def add_dem_argument(parser):
# DEM-blended image
dem.add_argument('--shade-frac', dest='shade_frac', type=float, default=0.5, metavar='NUM',
help='[Blend] Increases/decreases the contrast of the hillshade (default: %(default)s).')
dem.add_argument('--base-color', dest='base_color', type=float, default=0.9, metavar='NUM',
dem.add_argument('--base-color', dest='base_color', type=float, default=0.7, metavar='NUM',
help='[Blend] Topograhpy basemap greyish color ranges in [0,1] (default: %(default)s).')
dem.add_argument('--blend-mode', dest='blend_mode', type=str, default='overlay',
choices={'hsv','overlay','soft'}, metavar='STR',
Expand Down
44 changes: 32 additions & 12 deletions src/mintpy/utils/plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -2106,7 +2106,7 @@ def blend_colorbar(cax, inps, vlim, orientation='vertical', ticks=None,

# create an artificial topography for illumination
x = np.linspace(-np.pi/4, np.pi/4, nx) # x position along the illum. profile
elev = np.ones_like(arr) * np.cos(0.6*x) # altitude as a cosine along teh illum. profile
elev = np.ones_like(arr) * np.cos(0.6*x) # altitude as a cosine along the illum. profile

if orientation == 'vertical':
extent = [0, nx, vlim[0], vlim[1]] # (left, right, bottom, top)
Expand All @@ -2119,22 +2119,27 @@ def blend_colorbar(cax, inps, vlim, orientation='vertical', ticks=None,
ls = LightSource(azdeg=inps.shade_azdeg, altdeg=inps.shade_altdeg)
mappable = plt.cm.ScalarMappable(norm=mpl.colors.Normalize(0,1), cmap=inps.colormap)
img_rgb = mappable.to_rgba(arr)[:, :, :3]
illum_rgb = ls.shade_rgb(img_rgb, elev, fraction=fraction, blend_mode=blend_mode, vert_exag=vert_exag)
illum_rgb = ls.shade_rgb(
img_rgb, elev,
fraction=fraction,
blend_mode=blend_mode,
vert_exag=vert_exag,
)

# plot colorbar as an image
cax.imshow(illum_rgb, extent=extent, aspect='auto')

# axis format
tick_kwargs = dict(labelsize=inps.font_size, colors=inps.font_color)
tick_kwargs = dict(labelsize=inps.font_size, colors=inps.font_color, which='both')
label_kwargs = dict(fontsize=inps.font_size, color=inps.font_color)
if orientation == 'vertical':
cax.tick_params(which='both', bottom=False, top=False, labelbottom=False, **tick_kwargs)
cax.tick_params(bottom=False, top=False, labelbottom=False, **tick_kwargs)
cax.set_ylabel(inps.cbar_label, rotation=90, **label_kwargs)
cax.yaxis.set_label_position("right")
cax.yaxis.tick_right()
cax.set_xticks([])
else:
cax.tick_params(which='both', left=False, right=False, labeltop=False, **tick_kwargs)
cax.tick_params(left=False, right=False, labeltop=False, **tick_kwargs)
cax.set_xlabel(inps.cbar_label, **label_kwargs)
cax.xaxis.set_label_position("bottom")
cax.xaxis.tick_bottom()
Expand All @@ -2151,7 +2156,7 @@ def blend_colorbar(cax, inps, vlim, orientation='vertical', ticks=None,


def prep_blend_image(data, dem, vmin=None, vmax=None, cmap='viridis',
base_color=0.9, shade_frac=0.5, blend_mode='overlay',
base_color=0.7, shade_frac=0.5, blend_mode='overlay',
azdeg=315, altdeg=45, vert_exag=0.5, mask_nan_dem=True,
mask_nan_data=False, fill_value=0):
"""Prepare the illuminated RGB array for the data, using shaded relief DEM from a light source,
Expand All @@ -2176,6 +2181,20 @@ def prep_blend_image(data, dem, vmin=None, vmax=None, cmap='viridis',
"""
from matplotlib.colors import LightSource

# resample the lower resolution matrix into higher resolution
# link: https://scikit-image.org/docs/stable/api/skimage.transform.html#skimage.transform.resize
if data.shape != dem.shape:
from skimage.transform import resize
print(f'different dimension detected between data {data.shape} and DEM {dem.shape}!')
msg = 'via skimage.transform.resize(order=1)'
kwargs = dict(order=1, mode='edge', anti_aliasing=True, preserve_range=True)
if data.size < dem.size:
print(f'resampling data from {data.shape} to {dem.shape} {msg}...')
data = resize(data, dem.shape, **kwargs)
else:
print(f'resampling DEM from {dem.shape} to {data.shape} {msg}...')
dem = resize(dem, data.shape, **kwargs)

# use numpy.ma to mask missing or invalid entries
data = np.ma.masked_invalid(data)
dem = np.ma.masked_invalid(dem)
Expand All @@ -2195,14 +2214,15 @@ def prep_blend_image(data, dem, vmin=None, vmax=None, cmap='viridis',
# assign a greyish basemap color to the masked pixels
img_rgb[data.mask, :] = base_color

# check if the dimensions are coherent
# Note: in the future, resample one into the grid of the other one
if dem.shape != img_rgb.shape[:-1]:
raise ValueError(f'Dimension mismatch between DEM ({dem.shape}) and data ({data.shape})!')

## add shaded relief to illuminate the RGB array
# link: https://matplotlib.org/stable/api/_as_gen/matplotlib.colors.LightSource.html
ls = LightSource(azdeg=azdeg, altdeg=altdeg)
illum_rgb = ls.shade_rgb(img_rgb, dem, fraction=shade_frac, blend_mode=blend_mode, vert_exag=vert_exag)
illum_rgb = ls.shade_rgb(
img_rgb, dem,
fraction=shade_frac,
blend_mode=blend_mode,
vert_exag=vert_exag,
)

# add tranparency layer to the array (defualt: all ones = opaque)
illum_rgb = np.dstack([illum_rgb, np.ones_like(illum_rgb[:, :, 0])])
Expand Down