Skip to content

Commit 97aab46

Browse files
committed
Update example to use pandas
1 parent aa9de15 commit 97aab46

File tree

1 file changed

+23
-13
lines changed

1 file changed

+23
-13
lines changed

tutorials/upperair_soundings.py

+23-13
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,15 @@
1414
air weather service guide.
1515
"""
1616

17-
from datetime import datetime
1817

1918
import matplotlib.pyplot as plt
2019
from mpl_toolkits.axes_grid1.inset_locator import inset_axes
21-
20+
import numpy as np
21+
import pandas as pd
2222
import metpy.calc as mpcalc
23-
from metpy.io import get_upper_air_data
24-
from metpy.io.upperair import UseSampleData
23+
from metpy.cbook import get_test_data
2524
from metpy.plots import Hodograph, SkewT
25+
from metpy.units import units
2626

2727
#########################################################################
2828
# Getting Data
@@ -31,19 +31,29 @@
3131
# Upper air data can be obtained using the siphon package, but for this tutorial we will use
3232
# some of MetPy's sample data. This event is the Veterans Day tornado outbreak in 2002.
3333

34-
with UseSampleData(): # Only needed to use our local sample data
35-
dataset = get_upper_air_data(datetime(2002, 11, 11, 0), 'BNA')
34+
col_names = ['pressure', 'height', 'temperature', 'dewpoint', 'direction', 'speed']
35+
36+
df = pd.read_fwf(get_test_data('nov11_sounding.txt', as_file_obj=False),
37+
skiprows=5, usecols=[0, 1, 2, 3, 6, 7], names=col_names)
38+
39+
df['u_wind'], df['v_wind'] = mpcalc.get_wind_components(df['speed'],
40+
np.deg2rad(df['direction']))
41+
42+
# Drop any rows with all NaN values for T, Td, winds
43+
df = df.dropna(subset=('temperature', 'dewpoint', 'direction', 'speed',
44+
'u_wind', 'v_wind'), how='all').reset_index(drop=True)
3645

3746
##########################################################################
3847

39-
# We will pull the data out of the example dataset into individual variables.
48+
# We will pull the data out of the example dataset into individual variables and
49+
# assign units.
4050

41-
p = dataset.variables['pressure'][:]
42-
T = dataset.variables['temperature'][:]
43-
Td = dataset.variables['dewpoint'][:]
44-
u = dataset.variables['u_wind'][:]
45-
v = dataset.variables['v_wind'][:]
46-
wind_speed = dataset.variables['speed'][:]
51+
p = df['pressure'].values * units.hPa
52+
T = df['temperature'].values * units.degC
53+
Td = df['dewpoint'].values * units.degC
54+
wind_speed = df['speed'].values * units.knots
55+
wind_dir = df['direction'].values * units.degrees
56+
u, v = mpcalc.get_wind_components(wind_speed, wind_dir)
4757

4858
##########################################################################
4959
# Thermodynamic Calculations

0 commit comments

Comments
 (0)