def test_first_derivative_xarray_pint_conversion(test_da_lonlat): """Test first derivative with implicit xarray to pint quantity conversion.""" dx, _ = grid_deltas_from_dataarray(test_da_lonlat) deriv = first_derivative(test_da_lonlat, delta=dx, axis=-1) truth = np.array([[[-3.30782978e-06] * 4, [-3.42816074e-06] * 4, [-3.57012948e-06] * 4, [-3.73759364e-06] * 4]] * 3) * units('kelvin / meter') assert_array_almost_equal(deriv, truth, 12)
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 test_first_derivative(deriv_1d_data): """Test first_derivative with a simple 1D array.""" dv_dx = first_derivative(deriv_1d_data.values, x=deriv_1d_data.x) # Worked by hand and taken from Chapra and Canale 23.2 truth = np.array([-1.333333, -1.06666667, -0.5333333]) * units('delta_degC / cm') assert_array_almost_equal(dv_dx, truth, 5)
def test_find_intersections(direction, expected): """Test finding the intersection of two curves functionality.""" x = np.linspace(5, 30, 17) y1 = 3 * x**2 y2 = 100 * x - 650 # Note: Truth is what we will get with this sampling, not the mathematical intersection assert_array_almost_equal(expected, find_intersections(x, y1, y2, direction=direction), 2)
def test_laplacian(deriv_1d_data): """Test laplacian with simple 1D data.""" laplac = laplacian(deriv_1d_data.values, coordinates=(deriv_1d_data.x,)) # Worked by hand truth = np.ones_like(deriv_1d_data.values) * 0.2133333 * units('delta_degC/cm**2') assert_array_almost_equal(laplac, truth, 5)
def test_second_derivative(deriv_1d_data): """Test second_derivative with a simple 1D array.""" d2v_dx2 = second_derivative(deriv_1d_data.values, x=deriv_1d_data.x) # Worked by hand truth = np.ones_like(deriv_1d_data.values) * 0.2133333 * units('delta_degC/cm**2') assert_array_almost_equal(d2v_dx2, truth, 5)
def test_parcel_profile_saturated(): """Test parcel_profile works when LCL in levels (issue #232).""" levels = np.array([1000., 700., 500.]) * units.mbar true_prof = np.array([296.95, 284.381, 271.123]) * units.kelvin prof = parcel_profile(levels, 23.8 * units.degC, 23.8 * units.degC) assert_array_almost_equal(prof, true_prof, 2)
def test_basic_dewpoint_rh(): """Test dewpoint_rh function.""" temp = np.array([30., 25., 10., 20., 25.]) * units.degC rh = np.array([30., 45., 55., 80., 85.]) / 100. real_td = np.array([11, 12, 1, 16, 22]) * units.degC assert_array_almost_equal(real_td, dewpoint_rh(temp, rh), 0)
def test_grid_deltas_from_dataarray_xy(test_da_xy): """Test grid_deltas_from_dataarray with a xy grid.""" dx, dy = grid_deltas_from_dataarray(test_da_xy) true_dx = np.array([[[[500] * 3]]]) * units('km') true_dy = np.array([[[[500]] * 3]]) * units('km') assert_array_almost_equal(dx, true_dx, 5) assert_array_almost_equal(dy, true_dy, 5)
def test_temperature_from_potential_temperature(): """Test temperature_from_potential_temperature calculation.""" theta = np.array([286.12859679, 288.22362587, 290.31865495, 292.41368403]) * units.kelvin pres = np.array([850] * 4) * units.mbar real_t = np.array([273.15, 275.15, 277.15, 279.15]) * units.kelvin assert_array_almost_equal(temperature_from_potential_temperature(pres, theta), real_t, 2)
def test_basic(self): levels = np.array([1000., 900., 800., 700., 600., 500., 400.]) * units.mbar true_prof = np.array([303.15, 294.16, 288.026, 283.073, 277.058, 269.402, 258.966]) * units.kelvin prof = parcel_profile(levels, 30. * units.degC, 20. * units.degC) assert_array_almost_equal(prof, true_prof, 2)
def test_log_interpolate_3d(): """Test interpolating with log x-scale 3 dimensions along second axis.""" x_log = np.ones((3, 4, 3)) * np.array([1e3, 1e4, 1e5, 1e6]).reshape(-1, 1) y_log = np.log(x_log) * 2 + 3 x_interp = np.array([5e3, 5e4, 5e5]) y_interp_truth = np.array([20.0343863828, 24.6395565688, 29.2447267548]) y_interp = log_interpolate_1d(x_interp, x_log, y_log, axis=1) assert_array_almost_equal(y_interp[0, :, 0], y_interp_truth, 7)
def test_sigma_to_pressure(): """Test sigma_to_pressure.""" surface_pressure = 1000. * units.hPa model_top_pressure = 0. * units.hPa sigma = np.arange(0., 1.1, 0.1) expected = np.arange(0., 1100., 100.) * units.hPa pressure = sigma_to_pressure(sigma, surface_pressure, model_top_pressure) assert_array_almost_equal(pressure, expected, 5)
def test_log_interpolate_1d_units(): """Test interpolating with log x-scale with units.""" x_log = np.array([1e3, 1e4, 1e5, 1e6]) * units.hPa y_log = (np.log(x_log.m) * 2 + 3) * units.degC x_interp = np.array([5e5, 5e6, 5e7]) * units.Pa y_interp_truth = np.array([20.0343863828, 24.6395565688, 29.2447267548]) * units.degC y_interp = log_interpolate_1d(x_interp, x_log, y_log) assert_array_almost_equal(y_interp, y_interp_truth, 7)
def test_log_interpolate_2d(): """Test interpolating with log x-scale in 2 dimensions.""" x_log = np.array([[1e3, 1e4, 1e5, 1e6], [1e3, 1e4, 1e5, 1e6]]) y_log = np.log(x_log) * 2 + 3 x_interp = np.array([5e3, 5e4, 5e5]) y_interp_truth = np.array([20.0343863828, 24.6395565688, 29.2447267548]) y_interp = log_interpolate_1d(x_interp, x_log, y_log, axis=1) assert_array_almost_equal(y_interp[1], y_interp_truth, 7)
def test_interpolate_masked_units(): """Test interpolating with masked arrays with units.""" x = np.ma.array([1., 2., 3., 4.]) * units.m y = np.ma.array([50., 60., 70., 80.]) * units.degC x_interp = np.array([250., 350.]) * units.cm y_interp_truth = np.array([65., 75.]) * units.degC y_interp = interpolate_1d(x_interp, x, y) assert_array_almost_equal(y_interp, y_interp_truth, 7)
def test_interpolate_nans_1d_log(): """Test log interpolation of arrays with NaNs in the y-coordinate.""" x = np.logspace(1, 5, 15) y = 5 * np.log(x) + 3 nan_indexes = [1, 5, 11, 12] y_with_nan = y.copy() y_with_nan[nan_indexes] = np.nan assert_array_almost_equal(y, interpolate_nans_1d(x, y_with_nan, kind='log'), 2)
def test_interpolate_decrease_xp(): """Test interpolation with decreasing order.""" x = np.array([4., 3., 2., 1.]) y = x x_interp = np.array([3.5000000, 2.5000000]) y_interp_truth = np.array([3.5000000, 2.5000000]) y_interp = interpolate_1d(x_interp, x, y) assert_array_almost_equal(y_interp, y_interp_truth, 7)
def test_interpolate_end_point(): """Test interpolation with point at data endpoints.""" x = np.array([1., 2., 3., 4.]) y = x x_interp = np.array([1.0, 4.0]) y_interp_truth = np.array([1.0, 4.0]) y_interp = interpolate_1d(x_interp, x, y) assert_array_almost_equal(y_interp, y_interp_truth, 7)
def test_interpolate_nans_1d_linear(): """Test linear interpolation of arrays with NaNs in the y-coordinate.""" x = np.linspace(0, 20, 15) y = 5 * x + 3 nan_indexes = [1, 5, 11, 12] y_with_nan = y.copy() y_with_nan[nan_indexes] = np.nan assert_array_almost_equal(y, interpolate_nans_1d(x, y_with_nan), 2)
def test_heat_index_basic(): """Test the basic heat index calculation.""" temp = np.array([80, 88, 92, 110]) * units.degF rh = np.array([40, 100, 70, 40]) * units.percent hi = heat_index(temp, rh) values = np.array([80, 121, 112, 136]) * units.degF assert_array_almost_equal(hi, values, 0)
def test_basic(self): 'Test the basic wind chill calculation.' temp = np.array([40, -10, -45, 20]) * units.degF speed = np.array([5, 55, 25, 15]) * units.mph wc = windchill(temp, speed) values = np.array([36, -46, -84, 6]) * units.degF assert_array_almost_equal(wc, values, 0)
def test_face_level(self): 'Tests using the face_level flag' temp = np.array([20, 0, -20, -40]) * units.degF speed = np.array([15, 30, 45, 60]) * units.mph wc = windchill(temp, speed, face_level_winds=True) values = np.array([3, -30, -64, -98]) * units.degF assert_array_almost_equal(wc, values, 0)
def test_basic(self): 'Test the basic heat index calculation.' temp = np.array([80, 88, 92, 110]) * units.degF rh = np.array([40, 100, 70, 40]) hi = heat_index(temp, rh) values = np.array([80, 121, 112, 136]) * units.degF assert_array_almost_equal(hi, values, 0)
def test_log_interpolate_4d(): """Test interpolating with log x-scale 4 dimensions.""" x_log = np.ones((2, 2, 3, 4)) * np.array([1e3, 1e4, 1e5, 1e6]) y_log = np.log(x_log) * 2 + 3 x_interp = np.array([5e3, 5e4, 5e5]) y_interp_truth = np.array([20.0343863828, 24.6395565688, 29.2447267548]) y_interp = log_interpolate_1d(x_interp, x_log, y_log, axis=3) assert_array_almost_equal(y_interp[0, 0, 0, :], y_interp_truth, 7)
def test_interpolate_decrease(): """Test interpolation with decreasing interpolation points.""" x = np.array([1., 2., 3., 4.]) y = x x_interp = np.array([3.5000000, 2.5000000]) y_interp_truth = np.array([3.5000000, 2.5000000]) y_interp = interpolate_1d(x_interp, x, y) assert_array_almost_equal(y_interp, y_interp_truth, 7)
def test_interpolate_nan_linear(): """Test deprecated interpolate_nans function.""" x = np.linspace(0, 20, 15) y = 5 * x + 3 nan_indexes = [1, 5, 11, 12] y_with_nan = y.copy() y_with_nan[nan_indexes] = np.nan with pytest.warns(MetpyDeprecationWarning): assert_array_almost_equal(y, interpolate_nans(x, y_with_nan), 2)
def test_log_interpolate_set_nan_below(): """Test interpolating with log x-scale setting out of bounds below data to nan.""" x_log = np.array([1e3, 1e4, 1e5, 1e6]) y_log = np.log(x_log) * 2 + 3 x_interp = 1e2 y_interp_truth = np.nan with pytest.warns(Warning): y_interp = log_interpolate_1d(x_interp, x_log, y_log) assert_array_almost_equal(y_interp, y_interp_truth, 7)
def test_parse_angle_ext(): """Test extended (unabbrieviated) directional text in degrees.""" test_dir_strs = ['NORTH', 'NORTHnorthEast', 'North_East', 'East__North_East', 'easT', 'east south east', 'south east', ' south southeast', 'SOUTH', 'SOUTH SOUTH WEST', 'southWEST', 'WEST south_WEST', 'WeSt', 'WestNorth West', 'North West', 'NORTH north_WeSt'] expected_angles_degrees = np.arange(0, 360, 22.5) * units.degree output_angles_degrees = list(map(parse_angle, test_dir_strs)) assert_array_almost_equal(output_angles_degrees, expected_angles_degrees)
def test_parse_angle_mix_multiple(): """Test list of extended (unabbrieviated) directional text in degrees in one go.""" test_dir_strs = ['NORTH', 'nne', 'ne', 'east north east', 'easT', 'east se', 'south east', ' south southeast', 'SOUTH', 'SOUTH SOUTH WEST', 'sw', 'WEST south_WEST', 'w', 'wnw', 'North West', 'nnw'] expected_angles_degrees = np.arange(0, 360, 22.5) * units.degree output_angles_degrees = parse_angle(test_dir_strs) assert_array_almost_equal(output_angles_degrees, expected_angles_degrees)
def test_parse_angles_series(): """Test pandas.Series of angles to parse.""" angles = pd.Series(['N', 'S', 'E', 'W']) expected_angles = np.array([0, 180, 90, 270]) * units.degree calculated_angles = parse_angle(angles) assert_array_almost_equal(calculated_angles, expected_angles)
def test_time_deltas(): """Test the time_deltas attribute.""" ds = xr.open_dataset(get_test_data('irma_gfs_example.nc', as_file_obj=False)) time = ds['time1'] truth = 3 * np.ones(8) * units.hr assert_array_almost_equal(time.metpy.time_deltas, truth)
def test_array(self): levels = np.array([1000, 900, 864.89]) * units.mbar temps = dry_lapse(levels, 303.15 * units.kelvin) assert_array_almost_equal(temps, np.array([303.15, 294.16, 290.83]) * units.kelvin, 2)
def test_dry_lapse_2_levels(): """Test dry_lapse calculation when given only two levels.""" temps = dry_lapse( np.array([1000., 500.]) * units.mbar, 293. * units.kelvin) assert_array_almost_equal(temps, [293., 240.3723] * units.kelvin, 4)
def test_parse_angle_abbrieviated(): """Test abbrieviated directional text in degrees.""" expected_angles_degrees = np.arange(0, 360, 22.5) * units.degree output_angles_degrees = list(map(parse_angle, DIR_STRS)) assert_array_almost_equal(output_angles_degrees, expected_angles_degrees)
def test_precipitable_water(): """Test precipitable water with observed sounding.""" data = get_upper_air_data(datetime(2016, 5, 22, 0), 'DDC') pw = precipitable_water(data['pressure'], data['dewpoint'], top=400 * units.hPa) truth = 22.60430651 * units.millimeters assert_array_almost_equal(pw, truth, 4)
def test_exner_function(): """Test Exner function calculation.""" pres = np.array([900., 500., 300., 100.]) * units.mbar truth = np.array([0.9703542, 0.8203834, 0.7090065, 0.518048 ]) * units.dimensionless assert_array_almost_equal(exner_function(pres), truth, 6)
def test_second_derivative_scalar_delta(): """Test second_derivative with a scalar passed for a delta.""" df_dx = second_derivative(np.arange(3), delta=1) assert_array_almost_equal(df_dx, np.array([0., 0., 0.]), 6)
def test_first_derivative_scalar_delta(): """Test first_derivative with a scalar passed for a delta.""" df_dx = first_derivative(np.arange(3), delta=1) assert_array_almost_equal(df_dx, np.array([1., 1., 1.]), 6)
def test_basic(self): temp = np.array([5., 10., 18., 25.]) * units.degC real_es = np.array([8.72, 12.27, 20.63, 31.67]) * units.mbar assert_array_almost_equal(saturation_vapor_pressure(temp), real_es, 2)
def test_potential_temperature(): """Test potential_temperature calculation.""" temp = np.array([278., 283., 291., 298.]) * units.kelvin pres = np.array([900., 500., 300., 100.]) * units.mbar real_th = np.array([286.493, 344.961, 410.4335, 575.236]) * units.kelvin assert_array_almost_equal(potential_temperature(pres, temp), real_th, 3)
def test_parse_angle_invalid_number(): """Test list of extended (unabbrieviated) directional text in degrees in one go.""" test_dir_strs = 365. expected_angles_degrees = np.nan output_angles_degrees = parse_angle(test_dir_strs) assert_array_almost_equal(output_angles_degrees, expected_angles_degrees)
def test_parse_angle_abbrieviated(): """Test abbrieviated directional text in degrees.""" expected_angles_degrees = FULL_CIRCLE_DEGREES output_angles_degrees = parse_angle(DIR_STRS[:-1]) assert_array_almost_equal(output_angles_degrees, expected_angles_degrees)
def test_dry_lapse(): """Test dry_lapse calculation.""" levels = np.array([1000, 900, 864.89]) * units.mbar temps = dry_lapse(levels, 303.15 * units.kelvin) assert_array_almost_equal(temps, np.array([303.15, 294.16, 290.83]) * units.kelvin, 2)
def test_sat_vapor_pressure_fahrenheit(): """Test saturation_vapor_pressure handles temperature in Fahrenheit.""" temp = np.array([50., 68.]) * units.degF real_es = np.array([12.2717, 23.3695]) * units.mbar assert_array_almost_equal(saturation_vapor_pressure(temp), real_es, 4)
def test_sat_vapor_pressure(): """Test saturation_vapor_pressure calculation.""" temp = np.array([5., 10., 18., 25.]) * units.degC real_es = np.array([8.72, 12.27, 20.63, 31.67]) * units.mbar assert_array_almost_equal(saturation_vapor_pressure(temp), real_es, 2)
def test_moist_lapse(): """Test moist_lapse calculation.""" temp = moist_lapse(np.array([1000., 800., 600., 500., 400.]) * units.mbar, 293. * units.kelvin) true_temp = np.array([293, 284.64, 272.81, 264.42, 252.91]) * units.kelvin assert_array_almost_equal(temp, true_temp, 2)
def test_laplacian_2d(deriv_2d_data): """Test lapacian with full 2D arrays.""" laplac_true = 2 * (np.ones_like(deriv_2d_data.f) * (deriv_2d_data.a + deriv_2d_data.b)) laplac = laplacian(deriv_2d_data.f, x=(deriv_2d_data.y, deriv_2d_data.x)) assert_array_almost_equal(laplac, laplac_true, 5)
def test_3d_gradient_3d_data_no_axes(deriv_4d_data): """Test 3D gradient with 3D data and no axes parameter.""" test = deriv_4d_data[0] res = gradient(test, deltas=(1, 1, 1)) truth = tuple(factor * np.ones_like(test) for factor in (16., 4., 1.)) assert_array_almost_equal(res, truth, 8)
def test_moist_lapse_degc(): """Test moist_lapse with Celsius temperatures.""" temp = moist_lapse(np.array([1000., 800., 600., 500., 400.]) * units.mbar, 19.85 * units.degC) true_temp = np.array([293, 284.64, 272.81, 264.42, 252.91]) * units.kelvin assert_array_almost_equal(temp, true_temp, 2)
def test_geopotential_to_height(): """Test conversion from geopotential to height.""" geopotential = units.Quantity([0., 9805.11102602, 19607.14506998, 29406.10358006], units('m**2 / second**2')) height = geopotential_to_height(geopotential) assert_array_almost_equal(height, units.Quantity([0, 1000, 2000, 3000], units.m), 0)
def test_2d_gradient_4d_data_2_axes_2_deltas(deriv_4d_data): """Test 2D gradient of 4D data with 2 axes and 2 deltas.""" res = gradient(deriv_4d_data, deltas=(1, 1), axes=(0, 1)) truth = tuple(factor * np.ones_like(deriv_4d_data) for factor in (48., 16.)) assert_array_almost_equal(res, truth, 8)
def test_basic(self): temp = np.array([30., 25., 10., 20., 25.]) * units.degC rh = np.array([30., 45., 55., 80., 85.]) / 100. real_td = np.array([11, 12, 1, 16, 22]) * units.degC assert_array_almost_equal(real_td, dewpoint_rh(temp, rh), 0)
def test_gradient_4d(deriv_4d_data): """Test gradient with 4D arrays.""" res = gradient(deriv_4d_data, deltas=(1, 1, 1, 1)) truth = tuple(factor * np.ones_like(deriv_4d_data) for factor in (48., 16., 4., 1.)) assert_array_almost_equal(res, truth, 8)
def test_2_levels(self): temps = dry_lapse(np.array([1000., 500.]) * units.mbar, 293. * units.kelvin) assert_array_almost_equal(temps, [293., 240.3723] * units.kelvin, 4)
def test_height_to_geopotential(): """Test conversion from height to geopotential.""" height = units.Quantity([0, 1000, 2000, 3000], units.m) geopot = height_to_geopotential(height) assert_array_almost_equal(geopot, units.Quantity([0., 9805, 19607, 29406], units('m**2 / second**2')), 0)
def test_get_bound_pressure_height(pressure, bound, hgts, interp, expected): """Test getting bounds in layers with various parameter combinations.""" bounds = _get_bound_pressure_height(pressure, bound, heights=hgts, interpolate=interp) assert_array_almost_equal(bounds[0], expected[0], 4) assert_array_almost_equal(bounds[1], expected[1], 4)
def test_farenheit(self): temp = np.array([50., 68.]) * units.degF real_es = np.array([12.2717, 23.3695]) * units.mbar assert_array_almost_equal(saturation_vapor_pressure(temp), real_es, 4)
def test_get_layer(pressure, variable, heights, bottom, depth, interp, expected): """Test get_layer functionality.""" p_layer, y_layer = get_layer(pressure, variable, heights=heights, bottom=bottom, depth=depth, interpolate=interp) assert_array_almost_equal(p_layer, expected[0], 4) assert_array_almost_equal(y_layer, expected[1], 4)
def test_masked_and_no_mask(): """Test that we can compare a masked array with no masked values and a regular array.""" a = np.array([10, 20]) b = np.ma.array([10, 20], mask=[False, False]) assert_array_almost_equal(a, b)