def test_resample_nn(): """Test 1d nearest neighbor functionality.""" a = np.arange(5.) b = np.array([2, 3.8]) truth = np.array([2, 4]) assert_array_equal(truth, resample_nn_1d(a, b))
def plot_sounding(P, T, Td, U, V, r_time, ID): ##need the metpy package ##from metpy.plots import SkewT ##from metpy.units import units ##import metpy.calc as mcalc fig = plt.figure(figsize=(7, 6)) skew = SkewT(fig) skew.plot(P, T, 'r') skew.plot(P, Td, 'g') idx = mcalc.resample_nn_1d( P, np.array([1000, 975, 950, 925, 900, 850, 800, 750, 700, 650, 600, 500])) skew.plot_barbs(P[idx], U[idx] * units('m/s'), V[idx] * units('m/s'), plot_units=units('m/s')) ##special lines skew.plot_dry_adiabats() skew.plot_moist_adiabats(color='blue') skew.plot_mixing_lines(p=np.linspace(1100, 300) * units.hPa) ##title etc., plt.title(str(ID) + '_' + str(r_time) + 'LST') plt.xlim(-40, 40) plt.ylim(1100, 300) for i in range(-70, 40, 20): plt.fill_between(range(i, i + 11), 1100, 300, color='#C4FF8C') #plt.savefig('merged_data_plot/' + fname + '.png') plt.show()
def plot_metpy(data, title="", saveplot=None, showplot=True): # Convert data into a suitable format for metpy. _altitude = data[:,0] * units('m') p = mpcalc.height_to_pressure_std(_altitude) T = data[:,3] * units.degC Td = data[:,4] * units.degC wind_speed = data[:,1] * units('m/s') wind_direction = data[:,2] * units.degrees u, v = mpcalc.wind_components(wind_speed, wind_direction) fig = plt.figure(figsize=(6,8)) skew = SkewT(fig=fig) skew.plot(p, T, 'r') skew.plot(p, Td, 'g') my_interval = np.arange(300, 1000, 50) * units('mbar') ix = mpcalc.resample_nn_1d(p, my_interval) skew.plot_barbs(p[ix], u[ix], v[ix]) skew.ax.set_ylim(1000,300) skew.ax.set_xlim(-40, 30) skew.plot_dry_adiabats() heights = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9]) * units.km std_pressures = mpcalc.height_to_pressure_std(heights) for height_tick, p_tick in zip(heights, std_pressures): trans, _, _ = skew.ax.get_yaxis_text1_transform(0) skew.ax.text(0.02, p_tick, '---{:~d}'.format(height_tick), transform=trans) plt.title("Sounding: " + title) if saveplot != None: fig.savefig(saveplot, bbox_inches='tight')
skew.plot_mixing_lines() skew.ax.set_ylim(1000, 100) ########################################### # Example of defining your own vertical barb spacing 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') # Set spacing interval--Every 50 mb from 1000 to 100 mb my_interval = np.arange(100, 1000, 50) * units('mbar') # Get indexes of values closest to defined interval ix = resample_nn_1d(p, my_interval) # Plot only values nearest to defined interval values skew.plot_barbs(p[ix], u[ix], v[ix]) # Add the relevant special lines skew.plot_dry_adiabats() skew.plot_moist_adiabats() skew.plot_mixing_lines() skew.ax.set_ylim(1000, 100) # Show the plot plt.show()
skew.plot_mixing_lines() skew.ax.set_ylim(1000, 100) ########################################### # Example of defining your own vertical barb spacing 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') # Set spacing interval--Every 50 mb from 1000 to 100 mb my_interval = np.arange(100, 1000, 50) * units('mbar') # Get indexes of values closest to defined interval ix = mpcalc.resample_nn_1d(p, my_interval) # Plot only values nearest to defined interval values skew.plot_barbs(p[ix], u[ix], v[ix]) # Add the relevant special lines skew.plot_dry_adiabats() skew.plot_moist_adiabats() skew.plot_mixing_lines() skew.ax.set_ylim(1000, 100) # Show the plot plt.show()
'u_wind', 'v_wind'), how='all').reset_index(drop=True) p = df['pressure'].values * units.hPa T = df['temperature'].values * units.degC Td = df['dewpoint'].values * units.degC wind_speed = df['speed'].values * units.knots wind_dir = df['direction'].values * units.degrees u, v = mpcalc.wind_components(wind_speed, wind_dir) ###wind speed and dir are cols = 5,6; only plot winds every 50 mb. You can modify this if desired. interval = np.arange(100,1000,50) * units('mbar') ix = resample_nn_1d(p,interval) #%% #Can change the size of the figure by modifying 'figsize' to desired dimensions fig = plt.figure(figsize=(8, 8)) 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') skew.plot(p, Td, 'g')
def SkewT_plot(p, T, Td, heights, u=0, v=0, wind_barb=0, p_lims=[1000, 100], T_lims=[-50, 35], metpy_logo=0, plt_lfc=0, plt_lcl=0, plt_el=0, title=None): #plotting fig = plt.figure(figsize=(10, 10)) skew = plots.SkewT(fig) skew.plot(p, T, 'red') #Virtual Temperature skew.plot(p, Td, 'green') #Dewpoint skew.ax.set_ylim(p_lims) skew.ax.set_xlim(T_lims) if wind_barb == 1: # resampling wind barbs interval = np.logspace(2, 3) * units.hPa idx = mpcalc.resample_nn_1d(p, interval) skew.plot_barbs(p[idx], u[idx], v[idx]) #Showing Adiabasts and Mixing Ratio Lines skew.plot_dry_adiabats() skew.plot_moist_adiabats() skew.plot_mixing_lines() parcel_path = mpcalc.parcel_profile(p, T[0], Td[0]) skew.plot(p, parcel_path, color='k') # CAPE and CIN skew.shade_cape(p, T, parcel_path) skew.shade_cin(p, T, parcel_path) # Isotherms and Isobars # skew.ax.axhline(500 * units.hPa, color='k') # skew.ax.axvline(0 * units.degC, color='c') # LCL, LFC, EL lcl_p, lcl_T = mpcalc.lcl(p[0], T[0], Td[0]) lfc_p, lfc_T = mpcalc.lfc(p, T, Td) el_p, el_T = mpcalc.el(p, T, Td) if plt_lfc == 1: skew.ax.axhline(lfc_p, color='k') if plt_lcl == 1: skew.ax.axhline(lcl_p, color='k') # skew.ax.text(lcl_p, ) if plt_el == 1: skew.ax.axhline(el_p, color='k') if metpy_logo == 1: plots.add_metpy_logo(fig, x=55, y=50) decimate = 3 for p, T, heights in zip(df['pressure'][::decimate], df['temperature'][::decimate], df['height'][::decimate]): if p >= 700: skew.ax.text(T + 1, p, round(heights, 0)) plt.title(title) plt.show() return