def test_smooth_gaussian(): """Test the smooth_gaussian function with a larger n.""" m = 10 s = np.zeros((m, m)) for i in np.ndindex(s.shape): s[i] = i[0] + i[1]**2 s = smooth_gaussian(s, 4) s_true = np.array([[0.40077472, 1.59215426, 4.59665817, 9.59665817, 16.59665817, 25.59665817, 36.59665817, 49.59665817, 64.51108392, 77.87487258], [1.20939518, 2.40077472, 5.40527863, 10.40527863, 17.40527863, 26.40527863, 37.40527863, 50.40527863, 65.31970438, 78.68349304], [2.20489127, 3.39627081, 6.40077472, 11.40077472, 18.40077472, 27.40077472, 38.40077472, 51.40077472, 66.31520047, 79.67898913], [3.20489127, 4.39627081, 7.40077472, 12.40077472, 19.40077472, 28.40077472, 39.40077472, 52.40077472, 67.31520047, 80.67898913], [4.20489127, 5.39627081, 8.40077472, 13.40077472, 20.40077472, 29.40077472, 40.40077472, 53.40077472, 68.31520047, 81.67898913], [5.20489127, 6.39627081, 9.40077472, 14.40077472, 21.40077472, 30.40077472, 41.40077472, 54.40077472, 69.31520047, 82.67898913], [6.20489127, 7.39627081, 10.40077472, 15.40077472, 22.40077472, 31.40077472, 42.40077472, 55.40077472, 70.31520047, 83.67898913], [7.20489127, 8.39627081, 11.40077472, 16.40077472, 23.40077472, 32.40077472, 43.40077472, 56.40077472, 71.31520047, 84.67898913], [8.20038736, 9.3917669, 12.39627081, 17.39627081, 24.39627081, 33.39627081, 44.39627081, 57.39627081, 72.31069656, 85.67448522], [9.00900782, 10.20038736, 13.20489127, 18.20489127, 25.20489127, 34.20489127, 45.20489127, 58.20489127, 73.11931702, 86.48310568]]) assert_array_almost_equal(s, s_true)
def tt(self): # Calculate TT-index tt_index = (self.tair(850) + self.dewp(850)) - (2 * self.tair(500)) # Smooth the data tt_index = mpcalc.smooth_gaussian(tt_index, 2) return tt_index
def li(self): # Extract LI-index li_index = self.data["Best_4_layer_Lifted_Index_surface"][ self.time_step] # Smooth the data li_index = mpcalc.smooth_gaussian(li_index, 2) return np.array(li_index)
def k(self): # Calculate K-index k_index = ((self.tair(850) - self.tair(500)) + self.dewp(850) - (self.tair(700) - self.dewp(700))) # Smooth the data k_index = mpcalc.smooth_gaussian(k_index, 2) return k_index
def test_smooth_gaussian_small_n(): """Test the smooth_gaussian function with a smaller n.""" m = 5 s = np.zeros((m, m)) for i in np.ndindex(s.shape): s[i] = i[0] + i[1]**2 s = smooth_gaussian(s, 1) s_true = [[0.0141798077, 1.02126971, 4.02126971, 9.02126971, 15.9574606], [1.00708990, 2.01417981, 5.01417981, 10.0141798, 16.9503707], [2.00708990, 3.01417981, 6.01417981, 11.0141798, 17.9503707], [3.00708990, 4.01417981, 7.01417981, 12.0141798, 18.9503707], [4.00000000, 5.00708990, 8.00708990, 13.0070899, 19.9432808]] assert_array_almost_equal(s, s_true)
def test_smooth_gaussian_temperature(): """Test the smooth_gaussian function with temperature units.""" t = np.array([[2.73, 3.43, 6.53, 7.13, 4.83], [ 3.73, 4.93, 6.13, 6.63, 8.23 ], [3.03, 4.83, 6.03, 7.23, 7.63], [3.33, 4.63, 7.23, 6.73, 6.23], [3.93, 3.03, 7.43, 9.23, 9.23]]) * units.degC smooth_t = smooth_gaussian(t, 3) smooth_t_true = np.array([[2.8892, 3.7657, 6.2805, 6.8532, 5.3174], [3.6852, 4.799, 6.0844, 6.7816, 7.7617], [3.2762, 4.787, 6.117, 7.0792, 7.5181], [3.4618, 4.6384, 6.886, 6.982, 6.6653], [3.8115, 3.626, 7.1705, 8.8528, 8.9605] ]) * units.degC assert_array_almost_equal(smooth_t, smooth_t_true, 4)
def test_smooth_gaussian_3d_units(): """Test the smooth_gaussian function with units and a 3D array.""" m = 5 s = np.zeros((3, m, m)) for i in np.ndindex(s.shape): s[i] = i[1] + i[2]**2 s[0::2, :, :] = 10 * s[0::2, :, :] s = s * units('m') s = smooth_gaussian(s, 1) s_true = ([[0.0141798077, 1.02126971, 4.02126971, 9.02126971, 15.9574606], [1.00708990, 2.01417981, 5.01417981, 10.0141798, 16.9503707], [2.00708990, 3.01417981, 6.01417981, 11.0141798, 17.9503707], [3.00708990, 4.01417981, 7.01417981, 12.0141798, 18.9503707], [4.00000000, 5.00708990, 8.00708990, 13.0070899, 19.9432808]]) * units('m') assert_array_almost_equal(s[1, :, :], s_true)
def sweat(self): """ SWEAT = 12 Td850hPa + 20 (TTS -49) + 2V850hPa + V500hPa + 125 (cis + 0,2) onde V850hPa e V500hPa são a velocidade do vento em nós em 850 hPa e 500 hPa, respectivamente; cis = sen [direção (graus) V500hPa -V850hPa] """ # Calculate SWEAT-index cis = np.sin((self.wind_dir(500) - self.wind_dir(850))) sweat_index = ((12 * self.tair(850)) + (20 * (self.tt() - 49)) + (2 * self.wind_spd(850)) + self.wind_spd(500) + (125 * (cis * 0.2))) # Smooth the data sweat_index = mpcalc.smooth_gaussian(sweat_index, 2) return sweat_index
raw_data = np.random.random((size, size)) * 0.3 + distance / distance.max() * 0.7 fig, ax = plt.subplots(1, 1, figsize=(4, 4)) ax.set_title('Raw Data') ax.imshow(raw_data, vmin=0, vmax=1) ax.axis('off') plt.show() ########################################### # Now, create a grid showing different smoothing options fig, ax = plt.subplots(3, 3, figsize=(12, 12)) for i, j in product(range(3), range(3)): ax[i, j].axis('off') # Gaussian Smoother ax[0, 0].imshow(mpcalc.smooth_gaussian(raw_data, 3), vmin=0, vmax=1) ax[0, 0].set_title('Gaussian - Low Degree') ax[0, 1].imshow(mpcalc.smooth_gaussian(raw_data, 8), vmin=0, vmax=1) ax[0, 1].set_title('Gaussian - High Degree') # Rectangular Smoother ax[0, 2].imshow(mpcalc.smooth_rectangular(raw_data, (3, 7), 2), vmin=0, vmax=1) ax[0, 2].set_title('Rectangular - 3x7 Window\n2 Passes') # 5-point smoother ax[1, 0].imshow(mpcalc.smooth_n_point(raw_data, 5, 1), vmin=0, vmax=1) ax[1, 0].set_title('5-Point - 1 Pass') ax[1, 1].imshow(mpcalc.smooth_n_point(raw_data, 5, 4), vmin=0, vmax=1) ax[1, 1].set_title('5-Point - 4 Passes')
# dataset. These kinds of datasets are easiest to work with it MetPy, since most of the # "xarray magic" uses CF metadata. For this kind of dataset, a typical workflow looks like the # following # Load data, parse it for a CF grid mapping, and promote lat/lon data variables to coordinates data = xr.open_dataset(get_test_data( 'narr_example.nc', False)).metpy.parse_cf().set_coords(['lat', 'lon']) # Subset to only the data you need to save on memory usage subset = data.metpy.sel(isobaric=500 * units.hPa) # Quantify if you plan on performing xarray operations that need to maintain unit correctness subset = subset.metpy.quantify() # Perform calculations heights = mpcalc.smooth_gaussian(subset['Geopotential_height'], 5) subset['u_geo'], subset['v_geo'] = mpcalc.geostrophic_wind(heights) # Plot heights.plot() ######################################################################### # Save output subset.metpy.dequantify().drop_vars('metpy_crs').to_netcdf( '500hPa_analysis.nc') ######################################################################### # Non-Compliant Dataset Example # ----------------------------- #