示例#1
0
def test_angle_to_direction_level_1():
    """Test array of angles in degree."""
    expected_dirs = [
        'N', 'N', 'N', 'E', 'E', 'E', 'E', 'S', 'S', 'S', 'S',
        'W', 'W', 'W', 'W', 'N']
    output_dirs = angle_to_direction(FULL_CIRCLE_DEGREES, level=1)
    assert_array_equal(output_dirs, expected_dirs)
示例#2
0
def test_angle_to_direction_full():
    """Test the `full` keyword argument, expecting unabbrieviated output."""
    expected_dirs = [
        'North', 'North North East', 'North East', 'East North East',
        'East', 'East South East', 'South East', 'South South East',
        'South', 'South South West', 'South West', 'West South West',
        'West', 'West North West', 'North West', 'North North West'
    ]
    output_dirs = angle_to_direction(FULL_CIRCLE_DEGREES, full=True)
    assert_array_equal(output_dirs, expected_dirs)
示例#3
0
The code below shows how to convert angles into directional text.
It also  demonstrates the function's flexibility.
"""
import metpy.calc as mpcalc
from metpy.units import units

###########################################
# Create a test value of an angle
angle_deg = 70 * units('degree')
print(angle_deg)

###########################################
# Now throw that angle into the function to
# get the corresponding direction
dir_str = mpcalc.angle_to_direction(angle_deg)
print(dir_str)

###########################################
# The function can also handle array of angles,
# rounding to the nearest direction, handling angles > 360,
# and defaulting to degrees if no units are specified.
angle_deg_list = [0, 361, 719]
dir_str_list = mpcalc.angle_to_direction(angle_deg_list)
print(dir_str_list)

###########################################
# If you want the unabbrieviated version, input full=True
full_dir_str_list = mpcalc.angle_to_direction(angle_deg_list, full=True)
print(full_dir_str_list)
示例#4
0
def test_angle_to_direction_level_3():
    """Test array of angles in degree."""
    expected_dirs = DIR_STRS[:-1]  # UND at -1
    output_dirs = angle_to_direction(FULL_CIRCLE_DEGREES, level=3)
    assert_array_equal(output_dirs, expected_dirs)
示例#5
0
def test_angle_to_direction_level_4():
    """Test non-existent level of complexity."""
    with pytest.raises(ValueError) as exc:
        angle_to_direction(FULL_CIRCLE_DEGREES, level=4)
    assert 'cannot be less than 1 or greater than 3' in str(exc.value)
示例#6
0
def test_angle_to_direction_invalid_arr():
    """Test array of invalid angles."""
    expected_dirs = ['NE', UND, UND, UND, 'N']
    output_dirs = angle_to_direction(['46', None, np.nan, None, '362.'])
    assert_array_equal(output_dirs, expected_dirs)
示例#7
0
def test_angle_to_direction_invalid_scalar():
    """Test invalid angle."""
    expected_dirs = UND
    output_dirs = angle_to_direction(None)
    assert_array_equal(output_dirs, expected_dirs)
示例#8
0
def test_angle_to_direction_arr():
    """Test array of angles in degree."""
    expected_dirs = DIR_STRS[:-1]
    output_dirs = angle_to_direction(FULL_CIRCLE_DEGREES)
    assert_array_equal(output_dirs, expected_dirs)
示例#9
0
def test_angle_to_direction_edge():
    """Test single angle edge case (360 and no units) in degree."""
    expected_dirs = 'N'
    output_dirs = angle_to_direction(360)
    assert_array_equal(output_dirs, expected_dirs)
示例#10
0
def test_angle_to_direction():
    """Test single angle in degree."""
    expected_dirs = DIR_STRS[:-1]  # UND at -1
    output_dirs = [angle_to_direction(angle) for angle in FULL_CIRCLE_DEGREES]
    assert_array_equal(output_dirs, expected_dirs)
示例#11
0
    data = data.squeeze('depth')
    data.rio.write_crs(4326, inplace=True)
    clip = data.rio.clip(gdf.geometry.apply(mapping), gdf.crs)
    x, y = clip['uo'].metpy.coordinates('x', 'y')
    time = clip['uo'].metpy.time

    fig, ax = plt.subplots()
    Q = ax.quiver(x, y, clip['uo'], clip['vo'])
    gdf.plot(ax=ax, zorder=0)
    ax.set_xlim(103, 107)
    ax.set_ylim(-0.35, 2.5)

    ax.set_title('Current direction at '
                 + time.dt.strftime('%Y-%m').item())
    plt.show()


    df = clip.to_dataframe().reset_index()
    df_list.append(df)
    
dfs = pd.concat(df_list, ignore_index=True)
dfg = dfs.groupby('time').agg('mean')
dfg['angle'] = [mpcalc.wind_direction(u*units('m/s'), v*units('m/s'), convention='to').m for u, v in zip(dfg['uo'], dfg['vo'])]
# dfg['direction'] = [mpcalc.angle_to_direction(ang, full=True, level=1) for ang in dfg['angle']]
dfg.to_csv('kepri_current_mean.csv')

dfg = pd.read_csv('kepri_current_mean.csv')
dfg['direction'] = [mpcalc.angle_to_direction(ang, full=True, level=1) for ang in dfg['angle']]
dfg.to_csv('kepri_current_mean.csv')