Blog | wxster.comhttps://wxster.com/blog2015-12-27T23:19:01+00:00Recent postsPlotting Skew-T diagrams in Python 2015-12-27T23:19:01+00:00Casey Websterhttps://wxster.com/blog/author/caseyhttps://wxster.com/blog/2015/12/plotting-skew-t-diagrams-in-python<p>One feature of my python meteorological package <a href="https://wxster.com/projects/pymeteo">pyMeteo</a> is SkewT/LnP plotting from various data formats. These plots can read directly from certain kinds of model input and output data files from command line scripts and from numpy arrays in your python scripts.</p>
<div class="toc">
<ul>
<li><a href="https://wxster.com/blog/tag/visualization/feeds/atom#skew-t-ln-p-diagrams">Skew-T / Ln-P diagrams</a><ul>
<li><a href="https://wxster.com/blog/tag/visualization/feeds/atom#example-plot">Example plot</a></li>
</ul>
</li>
<li><a href="https://wxster.com/blog/tag/visualization/feeds/atom#plotting-from-data">Plotting from data</a><ul>
<li><a href="https://wxster.com/blog/tag/visualization/feeds/atom#tabular">Tabular</a></li>
<li><a href="https://wxster.com/blog/tag/visualization/feeds/atom#cm1">CM1</a></li>
<li><a href="https://wxster.com/blog/tag/visualization/feeds/atom#cm1-hdf5">CM1 / HDF5</a></li>
<li><a href="https://wxster.com/blog/tag/visualization/feeds/atom#wrf">WRF</a></li>
<li><a href="https://wxster.com/blog/tag/visualization/feeds/atom#from-numpy-arrays">From Numpy arrays</a></li>
</ul>
</li>
</ul>
</div>
<h1 id="skew-t-ln-p-diagrams">Skew-T / Ln-P diagrams</h1>
<p>Skew-T / Ln-P plots are simply 2-D plots with a skewed temperature axis and a logarithmic pressure axes (y). Data plotted on this style of plot include temperature, dew point, and various other derived values such as the temperature of a parcel lifted from the surface. The plots produced by these scripts also have a hodograph plotted and a data block with convective storm parameters.</p>
<h2 id="example-plot">Example plot</h2>
<p><a href="https://wxster.com/static/media/skewt/skewt.png"><img alt="skewt" src="https://wxster.com/static/media/skewt/skewt.png"/></a></p>
<h1 id="plotting-from-data">Plotting from data</h1>
<p>In all of the plotting methods, the plot output type is determined by the extension of the output file provided. You can write any type of file that the matplotlib backend can write.</p>
<h2 id="tabular">Tabular</h2>
<p>If you have tabular data suitable for WRF or CM1 model initialization, you can plot a skewt of this data with:</p>
<div class="codehilite"><pre><span class="nv">$ </span>skewt -f sounding.dat -o skewt.pdf
</pre></div>
<p>The format of the sounding data file is</p>
<blockquote>
<p>1 line header that contains: surface pressure (mb) surface theta (K) surface qv (g/kg)</p>
<p>following lines are: height (m) theta (K) qv (g/kg) u (m/s) v (m/s)</p>
</blockquote>
<h2 id="cm1">CM1</h2>
<p>For CM1 output in native GrADS format, you can plot a skewt from model output with</p>
<div class="codehilite"><pre><span class="nv">$ </span>skewt -p . -d cm1out -x <span class="m">0</span> -y <span class="m">0</span> -o skewt-cm1.pdf
</pre></div>
<p>In this case, <code>-p</code> is the path to the dataset, <code>-d</code> is the CM1 <code>output_basename</code>, <code>-x</code> and <code>-y</code> are the location of the plot in km and <code>-o</code> is the file to output. The current version (v0.4) of this script only works for CM1 datasets that are output with one file per timestep and will plot whatever timestep the file contains. </p>
<h2 id="cm1-hdf5">CM1 / HDF5</h2>
<p>For CM1 output in HDF5 format, you can plot a skewt from model output with:</p>
<div class="codehilite"><pre><span class="nv">$ </span>skewt-hdf -d model-data.h5 -x <span class="m">0</span> -y <span class="m">0</span> -o output.pdf
</pre></div>
<p>This currently requires that HDF5 output be un-tiled (though it might work for tiled files, I have not checked). The options are as in the CM1 GrADS version except that there is no <code>-p</code> and <code>-d</code> references a specific HDF5 file. This also assumes one file per timestep and will plot whatever timestep the file contains.</p>
<h2 id="wrf">WRF</h2>
<p>For WRF output in NetCDF format, you can plot a skewt from model output with:</p>
<div class="codehilite"><pre><span class="nv">$ </span>skewt-wrf -d wrfout.nc --lat <span class="m">30</span> --lon -80 -t <span class="m">0</span> -o skewt.pdf
</pre></div>
<p>In this case, <code>-d</code> references a WRF output file, <code>--lat</code> and <code>--lon</code> reference a location within the WRF domain, <code>-t</code> reference a timestep within the WRF output and <code>-o</code> specifies an output files.</p>
<h2 id="from-numpy-arrays">From Numpy arrays</h2>
<div class="codehilite"><pre><span class="kn">import</span> <span class="nn">numpy</span> <span class="kn">as</span> <span class="nn">np</span>
<span class="kn">import</span> <span class="nn">pymeteo.skewt</span> <span class="kn">as</span> <span class="nn">skewt</span>
<span class="c"># prepare 1D arrays height (z), pressure (p), potential temperature (th), </span>
<span class="c"># water vapor mixing ratio (qv), winds (u and v) all of the same length.</span>
<span class="n">skewt</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span><span class="bp">None</span><span class="p">,</span> <span class="n">z</span><span class="p">,</span> <span class="n">th</span><span class="p">,</span> <span class="n">p</span><span class="p">,</span> <span class="n">qv</span><span class="p">,</span> <span class="n">u</span><span class="p">,</span> <span class="n">v</span><span class="p">,</span> <span class="s">'output.pdf'</span><span class="p">)</span>
</pre></div>
<p>You can also choose to plot just the sounding, just the hodograph or plot each on on axes that you define. For details see the implementation of <code>pymeteo.skewt.plot()</code> and the <a href="http://pythonhosted.org/pymeteo/">pyMeteo documentation</a>.</p>Using two filled contour plots simultaneously in matplotlib2015-12-02T01:56:22+00:00Casey Websterhttps://wxster.com/blog/author/caseyhttps://wxster.com/blog/2015/12/using-two-filled-contour-plots-simultaneously-in-matplotlib<p>In my research endeavors I came across the need to overlay two filled contour plots to combine information with context needed to interpret it. I found some older solutions that were quite complicated but managed to find a much simpler solution. </p>
<div class="toc">
<ul>
<li><a href="https://wxster.com/blog/tag/visualization/feeds/atom#motivation">Motivation</a></li>
<li><a href="https://wxster.com/blog/tag/visualization/feeds/atom#alpha-blending">Alpha blending</a></li>
<li><a href="https://wxster.com/blog/tag/visualization/feeds/atom#notes">Notes</a></li>
<li><a href="https://wxster.com/blog/tag/visualization/feeds/atom#references">References</a></li>
</ul>
</div>
<h3 id="motivation">Motivation</h3>
<p>First, to motivate the need for two filled contours, here are examples of the data that I was looking at. The data I want to display is circulation, a measure of macroscopic rotation given by: $$\Gamma = \oint \mathbf{u}\cdot\mathrm{d}l$$ This is straightforward to plot:</p>
<p><img alt="Circulation" src="https://wxster.com/static/media/uploads/contourf-single-circ-resized.png"/></p>
<p>This presentation lacks context for the circulation, which the circulation is located in reference to storm structure. I also have reflectivity (simulated) data, which is similarly straightforward to plot:</p>
<p><img alt="Reflectivity" src="https://wxster.com/static/media/uploads/contourf-single-dbz-resized.png"/></p>
<p>One solution to plotting both sets of data together it to switch one to unfilled contour. This, however, makes interpreting the data more work. So how do we combine both filled contour plots into one image?</p>
<h3 id="alpha-blending">Alpha blending</h3>
<p>The key to plotting two filled plots is use alpha blending. This can be accomplished by setting the <code>alpha</code> kwarg in the call to <code>contourf</code>. Doing this for both contours though presents presentation problems. Ideally I want the reflectivity data to be a subtle background and the circulation plot to be the focus. To accomplish this I do plot the relfectivity with an <code>alpha</code> kwarg, e.g.</p>
<div class="codehilite"><pre>CSdbz = plt.contourf(X,Y,dbz[t,z25,:,:], levels=radar.cmap_radar_levels(),
cmap=plt.get_cmap('pymeteo_radar'), alpha=0.2)
</pre></div>
<p>For the circulation data I instead modify the colormap to include transparency so that the reds and blues can remain distinct without too much blending.</p>
<div class="codehilite"><pre>c_red = matplotlib.colors.colorConverter.to_rgba('red')
c_blue= matplotlib.colors.colorConverter.to_rgba('blue')
c_white_trans = matplotlib.colors.colorConverter.to_rgba('white',alpha = 0.0)
cmap_rb = matplotlib.colors.LinearSegmentedColormap.from_list('rb_cmap',[c_red,c_white_trans,c_blue],512)
</pre></div>
<p>This sets the colormap ends to red and blue and uses white to blend them together. This is essentially how the normal RedBlue colorbar works except my white color is completely transparent. This lets the background show through the blank parts of the circulation plot but lets the colors stay bright. I plot this with:</p>
<div class="codehilite"><pre> CSc = plt.contourf(Xc,Yc,circ[ct_idx,z25c,:,:], levels=levels, cmap=cmap_rb, extend='both')
</pre></div>
<p>The resulting plot is:</p>
<p><img alt="Reflectivity and Circ" src="https://wxster.com/static/media/uploads/contourf-dual-resized.png"/></p>
<h3 id="notes">Notes</h3>
<p>You will have to save the image in a format that supports alpha levels for this to work, otherwise only one of the plots will show. The PNG and PDF output work for producing raster and vector output but EPS will not work.</p>
<h3 id="references">References</h3>
<p>The referenced python modules:</p>
<ul>
<li><a href="http://matplotlib.org/">matplotlib</a> </li>
<li><a href="https://github.com/cwebster2/pyMeteo">pymeteo</a></li>
</ul>