Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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_parse_angle_none():
"""Test list of extended (unabbrieviated) directional text in degrees in one go."""
test_dir_strs = None
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_parcel_profile():
"""Test parcel profile calculation."""
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_gradient_xarray_pint_conversion(test_da_xy):
"""Test the 2D gradient calculation with a 2D DataArray and implicit pint conversion."""
data = test_da_xy.isel(time=0, isobaric=2)
deriv_y, deriv_x = gradient(data, coordinates=(data.metpy.y, data.metpy.x))
truth_x = np.ones_like(data) * -6.993007e-07 * units('kelvin / meter')
truth_y = np.ones_like(data) * -2.797203e-06 * units('kelvin / meter')
assert_array_almost_equal(deriv_x, truth_x, 12)
assert_array_almost_equal(deriv_y, truth_y, 12)
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_wind_comps_basic():
"""Test the basic wind component calculation."""
speed = np.array([4, 4, 4, 4, 25, 25, 25, 25, 10.]) * units.mph
dirs = np.array([0, 45, 90, 135, 180, 225, 270, 315, 360]) * units.deg
s2 = np.sqrt(2.)
u, v = wind_components(speed, dirs)
true_u = np.array([0, -4 / s2, -4, -4 / s2, 0, 25 / s2, 25, 25 / s2, 0]) * units.mph
true_v = np.array([-4, -4 / s2, 0, 4 / s2, 25, 25 / s2, 0, -25 / s2, -10]) * units.mph
assert_array_almost_equal(true_u, u, 4)
assert_array_almost_equal(true_v, v, 4)
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_log_interp():
"""Test deprecated log_interp function."""
x_log = 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_interp(x_interp, x_log, y_log)
assert_array_almost_equal(y_interp, y_interp_truth, 7)
"""Test parcel profile with lcl calculation."""
p = np.array([1004., 1000., 943., 928., 925., 850., 839., 749., 700., 699.]) * units.hPa
t = np.array([24.2, 24., 20.2, 21.6, 21.4, 20.4, 20.2, 14.4, 13.2, 13.]) * units.degC
td = np.array([21.9, 22.1, 19.2, 20.5, 20.4, 18.4, 17.4, 8.4, -2.8, -3.0]) * units.degC
true_prof = np.array([297.35, 297.01, 294.5, 293.48, 292.92, 292.81, 289.79, 289.32,
285.15, 282.59, 282.53]) * units.kelvin
true_p = np.insert(p.m, 2, 970.699) * units.mbar
true_t = np.insert(t.m, 2, 22.047) * units.degC
true_td = np.insert(td.m, 2, 20.609) * units.degC
pressure, temp, dewp, prof = parcel_profile_with_lcl(p, t, td)
assert_almost_equal(pressure, true_p, 3)
assert_almost_equal(temp, true_t, 3)
assert_almost_equal(dewp, true_td, 3)
assert_array_almost_equal(prof, true_prof, 2)
def test_windchill_face_level():
"""Test windchill 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)