#!/usr/bin/env python

import click
from copy import copy
import rasterio
from rasterio.plot import show
import gdal
import matplotlib.pyplot as plt

import projections.tiff_utils as tu

def get_min_max(fname):
  ds = gdal.Open(fname)
  min, max = tu.get_min_max(ds)
  del ds
  print('[%6.3f : %6.3f]' % (min, max))
  return min, max

@click.command()
@click.argument('fname', type=click.Path(dir_okay=False))
@click.option('-b', '--band', type=int, default=1)
@click.option('-s', '--save', type=click.Path(dir_okay=False))
@click.option('-t', '--title')
def main(fname, band, title, save):
  min, max = get_min_max(fname)
  if title is None:
    title = fname
  palette = copy(plt.cm.viridis)
  #palette.set_over('g', 1.0)
  #palette.set_under('r', 1.0)
  palette.set_bad('#0e0e2c', 1.0)

  src = rasterio.open(fname)
  #data = src.read(band, masked=True)
  if save:
    fig = plt.figure()
    #plt.gca().set_title(title)
    ax = plt.gca()
    show((src, band), cmap=palette, ax=ax)
    fig.tight_layout()
    ax.axis('off')
    fig.savefig(save, transparent=True)
    plt.show()
  else:
    ax = show((src, band), cmap=palette, title=title, vmin=min, vmax=max)

if __name__ == '__main__':
  main()

@@ -141,16 +141,84 @@
 t as plt
+%0Afrom mpl_toolkits.axes_grid1 import make_axes_locatable%0A%0Aimport pdb
 %0A%0Aimport
@@ -524,101 +524,983 @@
 lt=1
-)%0A@click.option('-s', '--save', type=click.Path(dir_okay=False))%0A@click.option('-t', '--title
+,%0A              help='Which raster band to display (default: 1)')%0A@click.option('-s', '--save', type=click.Path(dir_okay=False),%0A              help='Save the resulting image to disk.')%0A@click.option('-t', '--title',%0A              help='Title of the image (default: file name)')%0A@click.option('--max', type=float,%0A              help='Upper bound for color display.  Any cells less than ' +%0A              'this value will be shown in red.  By default it will ' +%0A              'compute the maximum such that 98%25 of the cells are below ' +%0A              'this value.')%0A@click.option('--min', type=float,%0A              help='Lower bound for color display.  Any cells less than ' +%0A              'this value will be shown in black.  By default it will' +%0A              'compute the minimum such that 2%25 of the cells are above ' +%0A              'this value.')%0A@click.option('--colorbar/--no-colorbar', default=True,%0A              help='Display/hide a colorbar with the value range.
 ')%0Ad
@@ -1535,18 +1535,40 @@
 save
+, max, min, colorbar
 ):%0A  
+r
 min, 
+r
 max 
@@ -1588,16 +1588,82 @@
 (fname)%0A
+  if max is None:%0A    max = rmax%0A  if min is None:%0A    min = rmin%0A
   if tit
@@ -1723,25 +1723,24 @@
 .viridis)%0A  
-#
 palette.set_
@@ -1745,17 +1745,17 @@
 t_over('
-g
+r
 ', 1.0)%0A
@@ -1756,17 +1756,16 @@
  1.0)%0A  
-#
 palette.
@@ -1771,25 +1771,25 @@
 .set_under('
-r
+k
 ', 1.0)%0A  pa
@@ -1778,32 +1778,33 @@
 der('k', 1.0)%0A  
+#
 palette.set_bad(
@@ -1818,16 +1818,44 @@
 c', 1.0)
+%0A  palette.set_bad('w', 1.0)
 %0A%0A  src 
@@ -2013,32 +2013,39 @@
 how((src, band),
+ ax=ax,
  cmap=palette, a
@@ -2043,19 +2043,45 @@
 alette, 
+title=title, vmin=min, vm
 ax=
+m
 ax)%0A    
@@ -2185,20 +2185,57 @@
 se:%0A    
-ax =
+fig = plt.figure()%0A    ax = plt.gca()%0A   
  show((s
@@ -2244,16 +2244,23 @@
 , band),
+ ax=ax,
  cmap=pa
@@ -2299,16 +2299,223 @@
 ax=max)%0A
+    if colorbar:%0A      divider = make_axes_locatable(ax)%0A      cax = divider.append_axes(%22bottom%22, size=%225%25%22, pad=0.25)%0A      plt.colorbar(ax.images%5B0%5D, cax=cax, orientation='horizontal')%0A    plt.show()%0A    
 %0Aif __na

Add a colorbar to the on-screen display