def test_speed_dir_roundtrip(): """Test round-tripping between speed/direction and components.""" # Test each quadrant of the whole circle wspd = np.array([15., 5., 2., 10.]) * units.meters / units.seconds wdir = np.array([160., 30., 225., 350.]) * units.degrees u, v = get_wind_components(wspd, wdir) wdir_out = get_wind_dir(u, v) wspd_out = get_wind_speed(u, v) assert_array_almost_equal(wspd, wspd_out, 4) assert_array_almost_equal(wdir, wdir_out, 4)
def test_dir(): """Test calculating wind direction.""" u = np.array([4., 2., 0., 0.]) * units('m/s') v = np.array([0., 2., 4., 0.]) * units('m/s') direc = get_wind_dir(u, v) true_dir = np.array([270., 225., 180., 270.]) * units.deg assert_array_almost_equal(true_dir, direc, 4)
def test_get_wind_dir(): """Test that get_wind_dir wrapper works (deprecated in 0.9).""" with pytest.warns(MetpyDeprecationWarning): d = get_wind_dir(3. * units('m/s'), 4. * units('m/s')) assert_almost_equal(d, 216.870 * units.deg, 3)
def test_scalar_dir(): """Test wind direction with scalars.""" d = get_wind_dir(3. * units('m/s'), 4. * units('m/s')) assert_almost_equal(d, 216.870 * units.deg, 3)
def test_get_wind_dir(): """Test that get_wind_dir wrapper works (deprecated in 0.9).""" d = get_wind_dir(3. * units('m/s'), 4. * units('m/s')) assert_almost_equal(d, 216.870 * units.deg, 3)
def do(ts): """Process this date timestamp""" asos = get_dbconn('asos', user='******') iemaccess = get_dbconn('iem') icursor = iemaccess.cursor() df = read_sql(""" select station, network, iemid, drct, sknt, valid at time zone tzname as localvalid, tmpf, dwpf from alldata d JOIN stations t on (t.id = d.station) where (network ~* 'ASOS' or network = 'AWOS') and valid between %s and %s and t.tzname is not null and date(valid at time zone tzname) = %s ORDER by valid ASC """, asos, params=(ts - datetime.timedelta(days=2), ts + datetime.timedelta(days=2), ts.strftime("%Y-%m-%d")), index_col=None) # derive some parameters df['relh'] = mcalc.relative_humidity_from_dewpoint( df['tmpf'].values * munits.degF, df['dwpf'].values * munits.degF).to(munits.percent) df['feel'] = mcalc.apparent_temperature( df['tmpf'].values * munits.degF, df['relh'].values * munits.percent, df['sknt'].values * munits.knots ) df['u'], df['v'] = mcalc.get_wind_components( df['sknt'].values * munits.knots, df['drct'].values * munits.deg ) df['localvalid_lag'] = df.groupby('iemid')['localvalid'].shift(1) df['timedelta'] = df['localvalid'] - df['localvalid_lag'] ndf = df[pd.isna(df['timedelta'])] df.loc[ndf.index.values, 'timedelta'] = pd.to_timedelta( ndf['localvalid'].dt.hour * 3600. + ndf['localvalid'].dt.minute * 60., unit='s' ) df['timedelta'] = df['timedelta'] / np.timedelta64(1, 's') table = "summary_%s" % (ts.year,) for iemid, gdf in df.groupby('iemid'): if len(gdf.index) < 6: # print(" Quorum not meet for %s" % (gdf.iloc[0]['station'], )) continue ldf = gdf.copy() ldf.interpolate(inplace=True) totsecs = ldf['timedelta'].sum() avg_rh = clean((ldf['relh'] * ldf['timedelta']).sum() / totsecs, 1, 100) min_rh = clean(ldf['relh'].min(), 1, 100) max_rh = clean(ldf['relh'].max(), 1, 100) uavg = (ldf['u'] * ldf['timedelta']).sum() / totsecs vavg = (ldf['u'] * ldf['timedelta']).sum() / totsecs drct = clean( mcalc.get_wind_dir(uavg * munits.knots, vavg * munits.knots), 0, 360) avg_sknt = clean( (ldf['sknt'] * ldf['timedelta']).sum() / totsecs, 0, 150 # arb ) max_feel = clean(ldf['feel'].max(), -150, 200) avg_feel = clean( (ldf['feel'] * ldf['timedelta']).sum() / totsecs, -150, 200 ) min_feel = clean(ldf['feel'].min(), -150, 200) def do_update(): """Inline updating""" icursor.execute(""" UPDATE """ + table + """ SET avg_rh = %s, min_rh = %s, max_rh = %s, avg_sknt = %s, vector_avg_drct = %s, min_feel = %s, avg_feel = %s, max_feel = %s WHERE iemid = %s and day = %s """, (avg_rh, min_rh, max_rh, avg_sknt, drct, min_feel, avg_feel, max_feel, iemid, ts)) do_update() if icursor.rowcount == 0: print(('compute_daily Adding %s for %s %s %s' ) % (table, gdf.iloc[0]['station'], gdf.iloc[0]['network'], ts)) icursor.execute(""" INSERT into """ + table + """ (iemid, day) values (%s, %s) """, (iemid, ts)) do_update() icursor.close() iemaccess.commit() iemaccess.close()
fontsize=100, weight='bold') outline_effect = [patheffects.withStroke(linewidth=15, foreground='black')] text_time.set_path_effects(outline_effect) ax.set_extent([-124.5, -105, 38.5, 50]) # Transform plane heading to a map direction and plot a rotated marker u, v = mpcalc.get_wind_components(1 * units('m/s'), data['heading'] * units('degrees')) u, v = proj.transform_vectors(ccrs.PlateCarree(), np.array([data['longitude']]), np.array([data['latitude']]), np.array([u.m]), np.array([v.m])) map_direction = -mpcalc.get_wind_dir(u, v).to('degrees') map_direction = map_direction[0].m ax.scatter(data['longitude'], data['latitude'], transform=ccrs.PlateCarree(), marker=(3, 0, map_direction), color='red', s=4000) ax.text(data['longitude'], data['latitude'] - 0.3, 'Altitude: {}\nHeading: {}\nTime:{}'.format(data['altitude'], data['heading'], data['time']), transform=ccrs.PlateCarree(),