def test_parse_angle_invalid_arr(): """Test list of extended (unabbrieviated) directional text in degrees in one go.""" test_dir_strs = ['nan', None, np.nan, 35, 35.5, 'north', 'andrewiscool'] expected_angles_degrees = [ np.nan, np.nan, np.nan, np.nan, np.nan, 0, np.nan ] output_angles_degrees = 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_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 = parse_angle(test_dir_strs) assert_array_almost_equal(output_angles_degrees, expected_angles_degrees)
# - First, assign units to the data, as applicable # - Convert cardinal wind direction to degrees # - Get wind components from speed and direction # Read in the data and assign units as defined by the Mesonet temperature = data['TAIR'].values * units.degF dewpoint = data['TDEW'].values * units.degF pressure = data['PRES'].values * units.hPa wind_speed = data['WSPD'].values * units.mph wind_direction = data['WDIR'] latitude = data['LAT'] longitude = data['LON'] station_id = data['STID'] # Take cardinal direction and convert to degrees, then convert to components wind_direction = mpcalc.parse_angle(list(wind_direction)) u, v = mpcalc.wind_components(wind_speed.to('knots'), wind_direction) ########################################### # Create the figure # ----------------- # Create the figure and an axes set to the projection. fig = plt.figure(figsize=(20, 8)) add_metpy_logo(fig, 70, 30, size='large') ax = fig.add_subplot(1, 1, 1, projection=proj) # Add some various map elements to the plot to make it recognizable. ax.add_feature(cfeature.LAND) ax.add_feature(cfeature.STATES.with_scale('50m'))
def test_parse_angles_single(): """Test single input into `parse_angles`.""" calculated_angle = parse_angle('SOUTH SOUTH EAST') expected_angle = 157.5 * units.degree assert_almost_equal(calculated_angle, expected_angle)
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_parse_angle_abbrieviated(): """Test abbrieviated directional text in degrees.""" expected_angles_degrees = np.arange(0, 360, 22.5) * units.degree output_angles_degrees = parse_angle(DIR_STRS) assert_array_almost_equal(output_angles_degrees, expected_angles_degrees)
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)
""" ============ Parse angles ============ Demonstrate how to convert direction strings to angles. The code below shows how to parse directional text into angles. It also demonstrates the function's flexibility in handling various string formatting. """ import metpy.calc as mpcalc ########################################### # Create a test value of a directional text dir_str = 'SOUTH SOUTH EAST' print(dir_str) ########################################### # Now throw that string into the function to calculate # the corresponding angle angle_deg = mpcalc.parse_angle(dir_str) print(angle_deg) ########################################### # The function can also handle arrays of strings # with different abbreviations and capitalizations dir_str_list = ['ne', 'NE', 'NORTHEAST', 'NORTH_EAST', 'NORTH east'] angle_deg_list = mpcalc.parse_angle(dir_str_list) print(angle_deg_list)
# SPDX-License-Identifier: BSD-3-Clause """ Parse angles ============ Demonstrate how to convert direction strings to angles. The code below shows how to parse directional text into angles. It also demonstrates the function's flexibility in handling various string formatting. """ import metpy.calc as mpcalc ########################################### # Create a test value of a directional text dir_str = 'SOUTH SOUTH EAST' print(dir_str) ########################################### # Now throw that string into the function to calculate # the corresponding angle angle_deg = mpcalc.parse_angle(dir_str) print(angle_deg) ########################################### # The function can also handle arrays of string # in many different abbrieviations and capitalizations dir_str_list = ['ne', 'NE', 'NORTHEAST', 'NORTH_EAST', 'NORTH east'] angle_deg_list = mpcalc.parse_angle(dir_str_list) print(angle_deg_list)