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/meteorology/feeds/atom#skew-t-ln-p-diagrams">Skew-T / Ln-P diagrams</a><ul>
<li><a href="https://wxster.com/blog/tag/meteorology/feeds/atom#example-plot">Example plot</a></li>
</ul>
</li>
<li><a href="https://wxster.com/blog/tag/meteorology/feeds/atom#plotting-from-data">Plotting from data</a><ul>
<li><a href="https://wxster.com/blog/tag/meteorology/feeds/atom#tabular">Tabular</a></li>
<li><a href="https://wxster.com/blog/tag/meteorology/feeds/atom#cm1">CM1</a></li>
<li><a href="https://wxster.com/blog/tag/meteorology/feeds/atom#cm1-hdf5">CM1 / HDF5</a></li>
<li><a href="https://wxster.com/blog/tag/meteorology/feeds/atom#wrf">WRF</a></li>
<li><a href="https://wxster.com/blog/tag/meteorology/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>