Source code for pycaps.plot.colormap_setup

#!/usr/bin/python

#Code: colormap_setup.py (Python Module)
#Author: Nate Snook (CASA/CAPS/SoM)
#Written: Dec. 2009
#Last modified:  20 Oct. 2015

#---------------------------------------------------------------------------------#

#------------------------------------------------------------------#
#  Since the CAPS installation of Python2.7 does not include the
#  Matplotlib function matplotlib.colors.from_levels_and_colors,
#  I have reproduced its functionality here from the github code
#  (https://github.com/matplotlib/matplotlib/pull/2050)

[docs]def from_levels_and_colors(levels, colors, extend='neither'): """ A helper routine to generate a cmap and a norm instance which behave similar to contourf's levels and colors arguments. The contents of this code are identical to the from_levels_and_colors in more recent versions of matplotlib (which are not available in the python2.7 installation on the CAPS servers as of 21 Oct. 2015) Args: levels : sequence of numbers -- The quantization levels used to construct the :class:`BoundaryNorm`. Values ``v`` are quantizized to level ``i`` if ``lev[i] <= v < lev[i+1]``. colors : sequence of colors -- The fill color to use for each level. If `extend` is "neither" there must be ``n_level - 1`` colors. For an `extend` of "min" or "max" add one extra color, and for an `extend` of "both" add two colors. extend : {'neither', 'min', 'max', 'both'}, optional. The behaviour when a value falls out of range of the given levels. See :func:`~matplotlib.pyplot.contourf` for details. Returns: (cmap, norm) : tuple containing a :class:`Colormap` and a :class:`Normalize` instance """ import matplotlib.colors colors_i0 = 0 colors_i1 = None if extend == 'both': colors_i0 = 1 colors_i1 = -1 extra_colors = 2 elif extend == 'min': colors_i0 = 1 extra_colors = 1 elif extend == 'max': colors_i1 = -1 extra_colors = 1 elif extend == 'neither': extra_colors = 0 else: raise ValueError('Unexpected value for extend: {0!r}'.format(extend)) n_data_colors = len(levels) - 1 n_expected_colors = n_data_colors + extra_colors if len(colors) != n_expected_colors: raise ValueError('With extend == {0!r} and n_levels == {1!r} expected' ' n_colors == {2!r}. Got {3!r}.' ''.format(extend, len(levels), n_expected_colors, len(colors))) cmap = matplotlib.colors.ListedColormap(colors[colors_i0:colors_i1], N=n_data_colors) if extend in ['min', 'both']: cmap.set_under(colors[0]) else: cmap.set_under('none') if extend in ['max', 'both']: cmap.set_over(colors[-1]) else: cmap.set_over('none') cmap.colorbar_extend = extend norm = matplotlib.colors.BoundaryNorm(levels, ncolors=n_data_colors) return cmap, norm
#end def(from_levels_and_colors) #------------------------------------------------------------------#
[docs]def thresh_setup(var, max = 100.0, min = -100.0, tornado=False): """ A function to define default thresholds and colorbar for plotting various fields. For unknown fields, passing in a max and min value will generate a generic 20-gradation colorbar. If max and min data are missing, -100 to 100 will be assumed as a last resort. NOTE: "max" and "min" are only used when "var" is not found in the presets. Args: var: the name of the variable, passed as a string max: OPTIONAL -- the maximum value you'd want to plot (default: -100.0) min: OPTIONAL -- the minimum value you'd want to plot (default: 100.0) tornado: OPTIONAL -- A flag to set True if you're plotting fields for a tornado and thus need to use otherwise extreme values for some fields (default: False) Returns: thresholds: a list of threshold values for contouring using contourf colormap: the name of a colormap to use from ns_colormaps.py cb_ticks: which ticks to show in the colorbar for this field no_proportional: A flag to set to select equal spacing in the colorbar """ from ns_colormap import * from numpy import arange no_proportional = False #By default, use a proportional colorbar. if var in ['probability', 'prob', 'NEP']: print 'Variable is probability -- contouring from 0.0 to 1.0' thresholds = [0.0, 0.05, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 0.95, 1.0] cb_ticks = [0.0, 0.2, 0.4, 0.6, 0.8, 1.0] colormap = prob_colors_12grades elif var == 'temp2m': print 'Variable is 2-meter temperature (temp2m) -- contouring from 30 to 90F' thresholds = arange(30., 93., 3.) cb_ticks = [30, 40, 50, 60, 70, 80, 90] colormap = coldhot_20 elif var in ['dewpk2', 'dewp2m']: print 'Variable is 2-meter dewpoint (dewp2m) -- contouring from 10 to 70F' thresholds = arange(10., 73., 3.) cb_ticks = [10, 20, 30, 40, 50, 60, 70] colormap = drywet_20 elif var == 'sfpres': if tornado == False: print 'Variable is surface pressure (mb) -- contouring from 920 to 1000 mb' thresholds = arange(920., 1004., 4.) cb_ticks = [920, 940, 960, 980, 1000] colormap = rainbow_20 else: print 'Variable is surface pressure (mb) -- contouring from 840 to 1000 mb' thresholds = arange(840., 1008., 8.) cb_ticks = [840, 880, 920, 960, 1000] colormap = rainbow_20 elif var in ['pt', 'theta']: print 'Variable is potential temperature (pt) -- contouring from 280 to 320 K' thresholds = arange(280., 322., 2.) cb_ticks = [280, 285, 290, 295, 300, 305, 310, 315, 320] colormap = coldhot_20 elif var in ['Z', 'truref', 'Zdm', 'refl2d', 'refl3d', 'refl', 'refmos', 'outgridtilt_zhh', 'obsordr_zhh']: print 'Variable is radar reflectivity (Z) -- contouring from 0 to 75 dBZ' thresholds = arange(5., 85., 5.) cb_ticks = [10., 20., 30., 40., 50., 60., 70.] colormap = refl_colors_adv elif var in ['zdr', 'Zdr', 'zdr_3d', 'zdr_2d', 'outgridtilt_zdr', 'obsordr_zdr']: print 'Variable is differential reflectivity (Zdr) -- contouring from -3.0 to 6.0 dB' thresholds = [-3.0, -2.0, -1.5, -1.0, -0.5, -0.01, 0.01, 0.25, 0.50, 0.75, 1.00, 1.25, 1.50, 1.75, 2.00, 2.25, 2.50, 2.75, 3.00, 3.50, 4.00, 4.50, 5.00] cb_ticks = [-3.0, -2.0, -1.0, 0, 1.0, 2.0, 3.0, 4.0, 5.0] colormap = zdr elif var in ['rhohv', 'rho_hv', 'rhv', 'rhv_2d', 'rhv_3d', 'outgridtilt_rhv', 'obsordr_rhv']: print 'Variable is cross-pol correlation coefficient (rho-hv) -- contouring from 0.0 to 1.05' thresholds = [-0.01, 0.00, 0.20, 0.40, 0.55, 0.65, 0.75, 0.80, 0.85, 0.90, 0.925, 0.95, 0.96, 0.97, 0.98, 0.99, 1.00, 1.05] cb_ticks = [0.0, 0.2, 0.4, 0.6, 0.8, 0.9, 0.95, 1.00] colormap = rho_hv no_proportional = True elif var in ['vr', 'radv2d', 'radv3d', 'radv', 'radvel', 'outgridtilt_vr', 'obsordr_vr']: print 'Variable is radial velocity (Vr) -- plotting from -30 to 30 m/s by irregular intervals' thresholds = [-30., -25., -20., -15., -10., -8., -6., -4., -2., -1., 0., 1., 2., 4., 6., 8., 10., 15., 20., 25., 30.] cb_ticks = [-30, -20, -10, -5, 0, 5, 10, 20, 30] colormap = vr_20 elif var == 'zp': print 'Variable is physical height -- plotting from 0m to 4000m above MSL' thresholds = [-10, 0, 50, 100, 150, 200, 300, 500, 750, 1000, 1300, 1700, 2200, 2800, 3200, 3500, 4000] cb_ticks = [0, 500, 1000, 2000, 3000, 4000] colormap = terrain_colors elif var == 'qv': print 'Variable is water vapor mixing ratio (qv) -- plotting from 0.000 to 0.020' thresholds = arange(0.00, 0.022, 0.002) cb_ticks = [0.00, 0.004, 0.008, 0.0012, 0.016, 0.020] colormap = teal_gradient_20 elif var == 'qr': print 'Variable is rainwater mixing ratio (qr) -- plotting from 0.000 to 0.010' thresholds = arange(0.00, 0.0105, 0.0005) cb_ticks = [0.00, 0.002, 0.004, 0.006, 0.008, 0.010] colormap = precip_20 elif var in ['qh', 'qg', 'qs']: print 'Variable is ice precip. mixing ratio (qh, qg, or qs) -- plotting from 0.000 to 0.010' thresholds = [0.00, 0.00025, 0.00050, 0.00075, 0.0010, 0.0015, 0.002, 0.0025, 0.003, 0.0035, 0.004, 0.0045, 0.005, 0.0055, 0.006, 0.0065, 0.007, 0.0075, 0.008, 0.009, 0.010] cb_ticks = [0.00, 0.002, 0.004, 0.006, 0.008, 0.010] colormap = iceprecip_20 elif var in ['mh', 'mg', 'ms']: print 'Variable is ice precip mass (mh, mg, or ms; kg/m^3) -- plotting from 0.0 to 5.0' thresholds = [0.00, 0.01, 0.05, 0.10, 0.2, 0.4, 0.6, 0.8, 1.0, 1.25, 1.50, 1.75, 2.0, 2.25, 2.5, 2.75, 3.0, 3.5, 4.0, 4.5, 5.0] cb_ticks = [0.0, 0.5, 1.0, 2.0, 3.0, 4.0, 5.0] colormap = iceprecip_20 elif var == 'mr': print 'Variable is rainwater mass (kg/m^3) -- plotting from 0.0 to 5.0' thresholds = [0.00, 0.01, 0.05, 0.10, 0.2, 0.4, 0.6, 0.8, 1.0, 1.25, 1.50, 1.75, 2.0, 2.25, 2.5, 2.75, 3.0, 3.5, 4.0, 4.5, 5.0] cb_ticks = [0.0, 0.5, 1.0, 2.0, 3.0, 4.0, 5.0] colormap = precip_20 elif var in ['mmdi_h', 'diam_h', 'MESH']: print 'Variable is hail diameter (mm) -- plotting from 0.0 to 100.0' thresholds = [0.0, 5.0, 10.0, 15.0, 20.0, 25.0, 30.0, 35.0, 40.0, 45.0, 50.0, 55.0, 60.0, 65.0, 70.0, 75.0, 80.0, 85.0, 90.0, 95.0, 100.0] cb_ticks = [5.0, 25.0, 50.0, 75.0] colormap = hail_maxdiam elif var in ['accppt']: print 'Variable is accumulated precipitation (mm) -- plotting from 0.0 to 150.0' thresholds = [0.0, 0.01, 1.0, 2.0, 3.0, 4.0, 5.0, 7.5, 10.0, 15.0, 20.0, 25.0, 30.0, 35.0, 40.0, 45.0, 50.0, 60.0, 70.0, 80.0, 90.0, 100.0, 125.0, 150.0] cb_ticks = [0.0, 10.0, 20.0, 50.0, 100.0, 150.0] colormap = rainfall elif var in ['w', 'W']: print 'Variable is vertical wind (w; m/s) -- plotting from -50.0 to 50.0' thresholds = [-50.0, -40.0, -30.0, -25.0, -20.0, -15.0, -10.0, -7.5, -5.0, -2.5, 0.0, 2.5, 5.0, 7.5, 10.0, 15.0, 20.0, 25.0, 30.0, 40.0, 50.0] cb_ticks = [-50, -40, -30, -20, -10, 0, 10, 20, 30, 40, 50] colormap = centered_grad_20 elif var in ['u', 'v', 'U', 'V']: print 'Variable is horizontal wind component (u or v; m/s) -- plotting from -50 to 50' thresholds = [-50.0, -40.0, -30.0, -25.0, -20.0, -15.0, -10.0, -7.5, -5.0, -2.5, 0.0, 2.5, 5.0, 7.5, 10.0, 15.0, 20.0, 25.0, 30.0, 40.0, 50.0] cb_ticks = [-50, -40, -30, -20, -10, 0, 10, 20, 30, 40, 50] colormap = vr_20 elif var in ['tke', 'TKE', 'turbulence']: print 'Variable is turbulent kinetic energy (TKE) -- plotting from -5 to 5' thresholds = [-5.0, -4.0, -3.0, -2.5, -2.0, -1.5, -1.0, -.75, -.5, -.25, 0.0, .25, .50, .75, 1.0, 1.5, 2.0, 2.5, 3.0, 4.0, 5.0] cb_ticks = [-5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5] colormap = posneg_20 elif var in ['wspd', 'windspeed']: print 'Variable is horizontal windspeed (m/s)' if tornado == True: print 'Plotting for tornadic EF-scale' thresholds = [0.0, 20.0, 29.0, 38.0, 50.0, 61.0, 74.0, 90.0, 100.0] cb_ticks = [0.0, 29.0, 38.0, 50.0, 61.0, 74.0, 90.0] colormap = efscale else: print 'Plotting from 0 to 30 m/s at irregular intervals.' thresholds = [0.00, 0.50, 1.0, 2.0, 3.0, 4.0, 5.0, 7.5, 10.0, 12.5, 15.0, 17.5, 20.0, 22.5, 25.0, 27.5, 30.0] cb_ticks = [0.0, 2.5, 5.0, 7.5, 10.0, 20.0, 30.0] colormap = bluered_gradient elif var in ['vort', 'zvort', 'xvort', 'yvort']: print 'Variable is vorticity (s^-1)' if tornado == True: print 'Plotting for tornadic values (0.00 to 2.00 s^-1)...' thresholds = arange(0.00, 2.10, 0.10) cb_ticks = [0.0, 0.5, 1.0, 1.5, 2.0] colormap = rainbow_grad_20 else: thresholds = arange(0.00, 0.21, 0.01) cb_ticks = [0.0, 0.05, 0.1, 0.15, 0.2] colormap = rainbow_grad_20 else: print 'Variable ' + str(var) + ' does not have default settings. Using generic 20 gradient colormap' thresholds = arange(min, max + ((max - min) / 20.), ((max - min) / 20.)) cb_ticks = thresholds[::3] colormap = rainbow_grad_20 return(thresholds, cb_ticks, colormap, no_proportional)
#--------------------------------------------------------------------------------#