def test_precipitable_water(): """Test precipitable water with observed sounding.""" with UseSampleData(): data = get_upper_air_data(datetime(2016, 5, 22, 0), 'DDC', source='wyoming') pw = precipitable_water(data.variables['dewpoint'][:], data.variables['pressure'][:]) truth = (0.8899441949243486 * units('inches')).to('millimeters') assert_array_equal(pw, truth)
def test_bunkers_motion(): """Test Bunkers storm motion with observed sounding.""" with UseSampleData(): data = get_upper_air_data(datetime(2016, 5, 22, 0), 'DDC', source='wyoming') motion = concatenate(bunkers_storm_motion(data.variables['pressure'][:], data.variables['u_wind'][:], data.variables['v_wind'][:], data.variables['height'][:])) truth = [1.4537892577864744, 2.0169333025630616, 10.587950761120482, 13.915130377372801, 6.0208700094534775, 7.9660318399679308] * units('m/s') assert_almost_equal(motion.flatten(), truth, 8)
def test_bulk_shear(): """Test bulk shear with observed sounding.""" with UseSampleData(): data = get_upper_air_data(datetime(2016, 5, 22, 0), 'DDC', source='wyoming') u, v = bulk_shear(data.variables['pressure'][:], data.variables['u_wind'][:], data.variables['v_wind'][:], heights=data.variables['height'][:], depth=6000 * units('meter')) truth = [29.899581266946115, -14.389225800205509] * units('knots') assert_almost_equal(u.to('knots'), truth[0], 8) assert_almost_equal(v.to('knots'), truth[1], 8)
def test_high_alt_wyoming(): """Test Wyoming data that starts at pressure less than 925 hPa.""" with UseSampleData(): data = get_upper_air_data(datetime(2010, 12, 9, 12), 'BOI', source='wyoming') assert_almost_equal(data.variables['pressure'][2], 890.0 * units('hPa'), 2) assert_almost_equal(data.variables['temperature'][2], 5.4 * units.degC, 2) assert_almost_equal(data.variables['dewpoint'][2], 3.9 * units.degC, 2) assert_almost_equal(data.variables['u_wind'][2], -0.42 * units.knot, 2) assert_almost_equal(data.variables['v_wind'][2], 5.99 * units.knot, 2)
def test_iastate(): """Test that we properly parse data from Iowa State archive.""" with UseSampleData(): data = get_upper_air_data(datetime(2016, 7, 30, 12), 'KDEN', source='iastate') assert_almost_equal(data.variables['pressure'][3], 838.0 * units('hPa'), 2) assert_almost_equal(data.variables['temperature'][3], 17.0 * units.degC, 2) assert_almost_equal(data.variables['dewpoint'][3], 15.2 * units.degC, 2) assert_almost_equal(data.variables['u_wind'][3], 1.72 * units.knot, 2) assert_almost_equal(data.variables['v_wind'][3], 2.46 * units.knot, 2)
def test_wyoming(): """Test that we are properly parsing data from the wyoming archive.""" with UseSampleData(): data = get_upper_air_data(datetime(1999, 5, 4, 0), 'OUN', source='wyoming') assert_almost_equal(data.variables['pressure'][5], 867.9 * units('hPa'), 2) assert_almost_equal(data.variables['temperature'][5], 17.4 * units.degC, 2) assert_almost_equal(data.variables['dewpoint'][5], 14.3 * units.degC, 2) assert_almost_equal(data.variables['u_wind'][5], 6.60 * units.knot, 2) assert_almost_equal(data.variables['v_wind'][5], 37.42 * units.knot, 2)
def test_bulk_shear_elevated(): """Test bulk shear with observed sounding and a base above the surface.""" with UseSampleData(): data = get_upper_air_data(datetime(2016, 5, 22, 0), 'DDC', source='wyoming') u, v = bulk_shear(data.variables['pressure'][:], data.variables['u_wind'][:], data.variables['v_wind'][:], heights=data.variables['height'][:], bottom=data.variables['height'][0] + 3000 * units('meter'), depth=3000 * units('meter')) truth = [0.9655943923302139, -3.8405428777944466] * units('m/s') assert_almost_equal(u, truth[0], 8) assert_almost_equal(v, truth[1], 8)
def test_mean_pressure_weighted(): """Test pressure-weighted mean wind function with vertical interpolation.""" with UseSampleData(): data = get_upper_air_data(datetime(2016, 5, 22, 0), 'DDC', source='wyoming') u, v = mean_pressure_weighted(data.variables['pressure'][:], data.variables['u_wind'][:], data.variables['v_wind'][:], heights=data.variables['height'][:], depth=6000 * units('meter')) assert_almost_equal(u, 6.0208700094534775 * units('m/s'), 7) assert_almost_equal(v, 7.966031839967931 * units('m/s'), 7)
def test_precipitable_water_no_bounds(): """Test precipitable water with observed sounding and no bounds given.""" with UseSampleData(): data = get_upper_air_data(datetime(2016, 5, 22, 0), 'DDC', source='wyoming') dewpoint = data.variables['dewpoint'][:] pressure = data.variables['pressure'][:] inds = pressure >= 400 * units.hPa pw = precipitable_water(dewpoint[inds], pressure[inds]) truth = (0.8899441949243486 * units('inches')).to('millimeters') assert_array_equal(pw, truth)
def test_mean_pressure_weighted_elevated(): """Test pressure-weighted mean wind function with a base above the surface.""" with UseSampleData(): data = get_upper_air_data(datetime(2016, 5, 22, 0), 'DDC', source='wyoming') u, v = mean_pressure_weighted(data.variables['pressure'][:], data.variables['u_wind'][:], data.variables['v_wind'][:], heights=data.variables['height'][:], depth=3000 * units('meter'), bottom=data.variables['height'][0] + 3000 * units('meter')) assert_almost_equal(u, 8.270829843626476 * units('m/s'), 7) assert_almost_equal(v, 1.7392601775853547 * units('m/s'), 7)
def test_bulk_shear_no_depth(): """Test bulk shear with observed sounding and no depth given. Issue #568.""" with UseSampleData(): data = get_upper_air_data(datetime(2016, 5, 22, 0), 'DDC', source='wyoming') u, v = bulk_shear(data.variables['pressure'][:], data.variables['u_wind'][:], data.variables['v_wind'][:], heights=data.variables['height'][:]) truth = [20.225018939, 22.602359692] * units('knots') assert_almost_equal(u.to('knots'), truth[0], 8) assert_almost_equal(v.to('knots'), truth[1], 8)
'PIT': ['Pittsburgh', 'PA'], 'LBF' : ['North Platte', 'NE'], 'DDC' : ['Dodge City', 'KS'] } year = int(input("What year? ")) month = int(input("What month? ")) day = int(input("What day? ")) zulu = int(input("Time (zulu)? ")) station = input('Input the station (ex. DDC, GYX, IAD): ').upper() plt.close('all') try: dataset = get_upper_air_data(datetime(year, month, day, zulu), station) except ValueError: print('Trying iowa state data.') try: dataset = get_upper_air_data(datetime(year, month, day, zulu), station, source = 'iastate') except ValueError: print('No data available for {}z'.format(zulu)) print('Moving onto next station.') p = dataset.variables['pressure'][:] T = dataset.variables['temperature'][:] Td = dataset.variables['dewpoint'][:] u = dataset.variables['u_wind'][:] v = dataset.variables['v_wind'][:] hgt = dataset.variables['height'][:]
from metpy.io import get_upper_air_data from metpy.plots import Hodograph, SkewT ######################################################################### # Getting Data # ------------ # # We will download data from the # `University of Wyoming sounding data page <http://weather.uwyo.edu/upperair/sounding.html>`_ # , which has an extensive archive of data available, as well as current data. # # In this case, we will download the sounding data from the Veterans Day # tornado outbreak in 2002 by passing a ``datetime`` object and station name to the # ``get_upper_air_data`` function. dataset = get_upper_air_data(datetime(2002, 11, 11, 0), 'BNA') ########################################################################## # We can view the fields available in the dataset. We will create some simple # variables to make the rest of the code more concise. print(dataset.variables.keys()) p = dataset.variables['pressure'][:] T = dataset.variables['temperature'][:] Td = dataset.variables['dewpoint'][:] u = dataset.variables['u_wind'][:] v = dataset.variables['v_wind'][:] ##########################################################################
import numpy as np from metpy.calc import resample_nn_1d from metpy.io import get_upper_air_data from metpy.io.upperair import UseSampleData from metpy.plots import SkewT from metpy.units import units ########################################### # Change default to be better for skew-T plt.rcParams['figure.figsize'] = (9, 9) with UseSampleData(): # Only needed to use our local sample data # Download and parse the data dataset = get_upper_air_data(datetime(2013, 1, 20, 12), 'OUN') p = dataset.variables['pressure'][:] T = dataset.variables['temperature'][:] Td = dataset.variables['dewpoint'][:] u = dataset.variables['u_wind'][:] v = dataset.variables['v_wind'][:] ########################################### skew = SkewT() # Plot the data using normal plotting functions, in this case using # log scaling in Y, as dictated by the typical meteorological plot skew.plot(p, T, 'r') skew.plot(p, Td, 'g') skew.plot_barbs(p, u, v)
""" from datetime import datetime import matplotlib.pyplot as plt import metpy.calc as mpcalc from metpy.io import get_upper_air_data from metpy.io.upperair import UseSampleData from metpy.plots import SkewT from metpy.units import concatenate ########################################### with UseSampleData(): # Only needed to use our local sample data # Download and parse the data dataset = get_upper_air_data(datetime(1999, 5, 4, 0), 'OUN') p = dataset.variables['pressure'][:] T = dataset.variables['temperature'][:] Td = dataset.variables['dewpoint'][:] u = dataset.variables['u_wind'][:] v = dataset.variables['v_wind'][:] ########################################### # Create a new figure. The dimensions here give a good aspect ratio fig = plt.figure(figsize=(9, 9)) skew = SkewT(fig, rotation=45) # Plot the data using normal plotting functions, in this case using # log scaling in Y, as dictated by the typical meteorological plot skew.plot(p, T, 'r')
ambient profile and the parcel profile is colored as well. """ from datetime import datetime import matplotlib.pyplot as plt import metpy.calc as mpcalc from metpy.io import get_upper_air_data from metpy.io.upperair import UseSampleData from metpy.plots import SkewT ########################################### with UseSampleData(): # Only needed to use our local sample data # Download and parse the data dataset = get_upper_air_data(datetime(1999, 5, 4, 0), 'OUN') p = dataset.variables['pressure'][:] T = dataset.variables['temperature'][:] Td = dataset.variables['dewpoint'][:] u = dataset.variables['u_wind'][:] v = dataset.variables['v_wind'][:] ########################################### # Create a new figure. The dimensions here give a good aspect ratio fig = plt.figure(figsize=(9, 9)) skew = SkewT(fig, rotation=45) # Plot the data using normal plotting functions, in this case using # log scaling in Y, as dictated by the typical meteorological plot skew.plot(p, T, 'r')
def upper_air(config, date, use_nan_sounding=False, use_existing=True, save=True): """ Retrieves upper-air data and interpolates to pressure levels. If use_nan_sounding is True, then if a retrieval error occurs, a blank sounding will be returned instead of an error. :param config: :param date: datetime :param use_nan_sounding: bool: if True, use sounding of NaNs instead of raising an error :param use_existing: bool: preferentially use existing soundings in sounding_data_dir :param save: bool: if True, save processed soundings to sounding_data_dir :return: """ variables = ['height', 'temperature', 'dewpoint', 'u_wind', 'v_wind'] # Define levels for interpolation: same as model data, except omitting lowest_p_level plevs = [600, 750, 850, 925] pres_interp = [p for p in plevs if p <= config['lowest_p_level']] # Try retrieving the sounding, first checking for existing if config['verbose']: print('upper_air: retrieving sounding for %s' % datetime.strftime(date, '%Y%m%d%H')) nan_sounding = False retrieve_sounding = False sndg_data_dir = config['Obs']['sounding_data_dir'] if not (os.path.isdir(sndg_data_dir)): os.makedirs(sndg_data_dir) sndg_file = '%s/%s_SNDG_%s.pkl' % (sndg_data_dir, config['station_id'], datetime.strftime(date, '%Y%m%d%H')) if use_existing: try: with open(sndg_file, 'rb') as handle: data = pickle.load(handle) if config['verbose']: print(' Read from file.') except: retrieve_sounding = True else: retrieve_sounding = True if retrieve_sounding: try: dset = get_upper_air_data(date, config['Obs']['sounding_station_id']) except: # Try again try: dset = get_upper_air_data(date, config['Obs']['sounding_station_id']) except: if use_nan_sounding: if config['verbose']: print( 'upper_air: warning: unable to retrieve sounding; using nan.' ) nan_sounding = True else: raise ValueError('error retrieving sounding for %s' % date) # Retrieve pressure for interpolation to fixed levels if not nan_sounding: pressure = dset.variables['pressure'] pres = np.array([p.magnitude for p in list(pressure)]) # units are hPa # Get variables and interpolate; add to dictionary data = OrderedDict() for var in variables: if not nan_sounding: var_data = dset.variables[var] var_array = np.array([v.magnitude for v in list(var_data)]) var_interp = interp(pres_interp, pres, var_array) data[var] = var_interp.tolist() else: data[var] = [np.nan] * len(pres_interp) # Save if save and not nan_sounding: with open(sndg_file, 'wb') as handle: pickle.dump(data, handle, protocol=pickle.HIGHEST_PROTOCOL) return data
# # We will download data from the # `University of Wyoming sounding data page <http://weather.uwyo.edu/upperair/sounding.html>`_ # , which has an extensive archive of data available, as well as current data. # # In this case, we will download the sounding data from the 1999 May 3 # tornado by passing a ``datetime`` object and station name to the # ``get_upper_air_data`` function. #site = input("Enter Upper-Air Site") #year = input("Enter Year (YYYY)") #month = input("Enter Month (M/MM)") #day = input("Enter Day (D/DD)") #hour = input("Enter Hour (H/HH)") #dataset = get_upper_air_data(datetime(year, month, day, hour), DNR) dataset = get_upper_air_data(datetime(2017, 8, 27, 12), 'CRP') ########################################################################## # We can view the fields available in the dataset. We will create some simple # variables to make the rest of the code more concise. print(dataset.variables.keys()) p = dataset.variables['pressure'][:] T = dataset.variables['temperature'][:] Td = dataset.variables['dewpoint'][:] u = dataset.variables['u_wind'][:] v = dataset.variables['v_wind'][:] ##########################################################################