-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
204 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,204 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"# `get_skewt_vars`\n", | ||
"\n", | ||
"This notebook is a simple example of the GeoCAT-viz function <a href=\"../user_api/generated/geocat.viz.util.get_skewt_vars.html#geocat-viz.util.get_skewt_vars\">get_skewt_vars</a>." | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 1, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"# Import packages:\n", | ||
"\n", | ||
"import matplotlib.pyplot as plt\n", | ||
"import matplotlib.lines as mlines\n", | ||
"import numpy as np\n", | ||
"import pandas as pd\n", | ||
"from metpy.plots import SkewT\n", | ||
"from metpy.units import units\n", | ||
"import metpy.calc as mpcalc\n", | ||
"\n", | ||
"import geocat.viz as gv\n", | ||
"import geocat.datafiles as gdf" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 2, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"# Read in data\n", | ||
"\n", | ||
"# Open a ascii data file using pandas' read_csv function\n", | ||
"ds = pd.read_csv(gdf.get('ascii_files/sounding.testdata'),\n", | ||
" delimiter='\\\\s+',\n", | ||
" header=None)\n", | ||
"\n", | ||
"# Extract the data\n", | ||
"p = ds[1].values * units.hPa # Pressure [mb/hPa]\n", | ||
"tc = (ds[5].values + 2) * units.degC # Temperature [C]\n", | ||
"tdc = ds[9].values * units.degC # Dew pt temp [C]\n", | ||
"\n", | ||
"# Create dummy wind data\n", | ||
"wspd = np.linspace(0, 150, len(p)) * units.knots # Wind speed [knots or m/s]\n", | ||
"wdir = np.linspace(0, 360, len(p)) * units.degrees # Meteorological wind dir\n", | ||
"u, v = mpcalc.wind_components(wspd, wdir) # Calculate wind components\n", | ||
"\n", | ||
"# Generate subtitle with Pressure of LCL, Temperature of LCL, Showalter Index,\n", | ||
"# Precipitable Water, and CAPE\n", | ||
"tc0 = tc[0] # Temperature of surface parcel\n", | ||
"tdc0 = tdc[0] # Dew point temperature of surface parcel\n", | ||
"pro = mpcalc.parcel_profile(p, tc0, tdc0) # Temperature profile of parcel\n", | ||
"subtitle = gv.get_skewt_vars(p, tc, tdc, pro) # Create subtitle" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"# Create plot:\n", | ||
"\n", | ||
"# Note that MetPy forces the x axis scale to be in Celsius and the y axis\n", | ||
"# scale to be in hectoPascals. Once data is plotted, then the axes labels are\n", | ||
"# automatically added\n", | ||
"fig = plt.figure(figsize=(10, 12))\n", | ||
"\n", | ||
"# The rotation keyword changes how skewed the temperature lines are. MetPy has\n", | ||
"# a default skew of 30 degrees\n", | ||
"skew = SkewT(fig, rotation=45)\n", | ||
"ax = skew.ax\n", | ||
"\n", | ||
"# Plot temperature and dew point\n", | ||
"skew.plot(p, tc, color='black')\n", | ||
"skew.plot(p, tdc, color='blue')\n", | ||
"\n", | ||
"# Draw parcel path\n", | ||
"parcel_prof = mpcalc.parcel_profile(p, tc[0], tdc[0]).to('degC')\n", | ||
"skew.plot(p, parcel_prof, color='red', linestyle='--')\n", | ||
"u = np.where(p >= 100 * units.hPa, u, np.nan)\n", | ||
"v = np.where(p >= 100 * units.hPa, v, np.nan)\n", | ||
"p = np.where(ds[1].values >= 100, ds[1].values,\n", | ||
" np.nan) # unitless for plot_barbs\n", | ||
"\n", | ||
"# Add wind barbs\n", | ||
"skew.plot_barbs(pressure=p[::2],\n", | ||
" u=u[::2],\n", | ||
" v=v[::2],\n", | ||
" xloc=1.05,\n", | ||
" fill_empty=True,\n", | ||
" sizes=dict(emptybarb=0.075, width=0.1, height=0.2))\n", | ||
"\n", | ||
"# Draw line underneath wind barbs\n", | ||
"line = mlines.Line2D([1.05, 1.05], [0, 1],\n", | ||
" color='gray',\n", | ||
" linewidth=0.5,\n", | ||
" transform=ax.transAxes,\n", | ||
" clip_on=False,\n", | ||
" zorder=1)\n", | ||
"ax.add_line(line)\n", | ||
"\n", | ||
"# Shade every other section between isotherms\n", | ||
"x1 = np.linspace(-100, 40, 8) # The starting x values for the shaded regions\n", | ||
"x2 = np.linspace(-90, 50, 8) # The ending x values for the shaded regions\n", | ||
"y = [1050, 100] # The range of y values that the shades regions should cover\n", | ||
"for i in range(0, 8):\n", | ||
" skew.shade_area(y=y,\n", | ||
" x1=x1[i],\n", | ||
" x2=x2[i],\n", | ||
" color='limegreen',\n", | ||
" alpha=0.25,\n", | ||
" zorder=1)\n", | ||
"\n", | ||
"# Choose starting temperatures in Kelvin for the dry adiabats\n", | ||
"t0 = units.K * np.arange(243.15, 444.15, 10)\n", | ||
"skew.plot_dry_adiabats(t0=t0, linestyles='solid', colors='tan', linewidths=1.5)\n", | ||
"\n", | ||
"# Choose starting temperatures in Kelvin for the moist adiabats\n", | ||
"t0 = units.K * np.arange(281.15, 306.15, 4)\n", | ||
"skew.plot_moist_adiabats(t0=t0,\n", | ||
" linestyles='solid',\n", | ||
" colors='lime',\n", | ||
" linewidth=1.5)\n", | ||
"\n", | ||
"# Choose mixing ratios\n", | ||
"w = np.array([0.001, 0.002, 0.003, 0.005, 0.008, 0.012, 0.020]).reshape(-1, 1)\n", | ||
"\n", | ||
"# Choose the range of pressures that the mixing ratio lines are drawn over\n", | ||
"p_levs = units.hPa * np.linspace(1000, 400, 7)\n", | ||
"\n", | ||
"# Plot mixing ratio lines\n", | ||
"skew.plot_mixing_lines(mixing_ratio=w,\n", | ||
" pressure=p_levs,\n", | ||
" linestyle='dashed',\n", | ||
" colors='lime',\n", | ||
" linewidths=1)\n", | ||
"\n", | ||
"# Use geocat.viz utility functions to set axes limits and ticks\n", | ||
"gv.set_axes_limits_and_ticks(\n", | ||
" ax=ax,\n", | ||
" xlim=[-32, 38],\n", | ||
" yticks=[1000, 850, 700, 500, 400, 300, 250, 200, 150, 100])\n", | ||
"\n", | ||
"# Use geocat.viz utility function to change the look of ticks and ticklabels\n", | ||
"gv.add_major_minor_ticks(ax=ax,\n", | ||
" x_minor_per_major=1,\n", | ||
" y_minor_per_major=1,\n", | ||
" labelsize=14)\n", | ||
"# The utility function draws tickmarks all around the plot. We only need ticks\n", | ||
"# on the left and bottom edges\n", | ||
"ax.tick_params('both', which='both', top=False, right=False)\n", | ||
"\n", | ||
"# Use geocat.viz utility functions to add labels\n", | ||
"gv.set_titles_and_labels(ax=ax,\n", | ||
" xlabel='Temperature (C)',\n", | ||
" ylabel='P (hPa)',\n", | ||
" labelfontsize=14)\n", | ||
"\n", | ||
"# Manually add suptitle and subtitle for appropriate positioning\n", | ||
"fig.suptitle('Raob; [Wind Reports]', fontsize=24, y=0.92)\n", | ||
"ax.set_title(subtitle, color='darkgoldenrod')\n", | ||
"\n", | ||
"# Change the style of the gridlines\n", | ||
"plt.grid(True,\n", | ||
" which='major',\n", | ||
" axis='both',\n", | ||
" color='tan',\n", | ||
" linewidth=1.5,\n", | ||
" alpha=0.5)\n", | ||
"\n", | ||
"plt.show();" | ||
] | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": "geocat_sandbox", | ||
"language": "python", | ||
"name": "python3" | ||
}, | ||
"language_info": { | ||
"codemirror_mode": { | ||
"name": "ipython", | ||
"version": 3 | ||
}, | ||
"file_extension": ".py", | ||
"mimetype": "text/x-python", | ||
"name": "python", | ||
"nbconvert_exporter": "python", | ||
"pygments_lexer": "ipython3", | ||
"version": "3.10.10" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 2 | ||
} |