Beispiel #1
0
def test_data():
    ds = xray.Dataset()
    ds['time'] = ('time', np.arange(4),
                  {'units': 'hours since 2013-12-12 12:00:00'})
    ds['longitude'] = (('longitude'),
                       np.mod(np.arange(235., 240.) + 180, 360) - 180,
                       {'units': 'degrees east'})
    ds['latitude'] = ('latitude',
                      np.arange(35., 40.),
                      {'units': 'degrees north'})
    shape = tuple([ds.dims[x]
                   for x in ['time', 'longitude', 'latitude']])

    x, y = np.meshgrid(np.arange(-2, 3), np.arange(-2, 3))

    wind_mids = 0.5 * (variables.wind_bins[1:] +
                       variables.wind_bins[:-1])
    wind_speed = wind_mids[x * x + y * y]

    current_mids = 0.5 * (variables.current_bins[1:] +
                          variables.current_bins[:-1])
    current_speed = current_mids[x * x + y * y]

    dir = np.arctan2(y, x)

    current_speeds = np.empty(shape)
    wind_speeds = np.empty(shape)
    dirs = np.empty(shape)
    for i in range(ds.dims['time']):
        wind_speeds[i] = wind_speed
        current_speeds[i] = current_speed
        dirs[i] = dir + i * np.pi / 2

    uwnd, vwnd = angles.radial_to_vector(wind_speeds, dirs.copy(),
                                         orientation="from")
    ds['x_wind'] = (('time', 'longitude', 'latitude'),
                    uwnd, {'units': 'm/s'})
    ds['y_wind'] = (('time', 'longitude', 'latitude'),
                    vwnd, {'units': 'm/s'})

    ucurr, vcurr = angles.radial_to_vector(current_speeds, dirs.copy(),
                                           orientation="from")
    ds['sea_water_x_velocity'] = (('time', 'longitude', 'latitude'),
                    ucurr, {'units': 'm/s'})
    ds['sea_water_y_velocity'] = (('time', 'longitude', 'latitude'),
                    vcurr, {'units': 'm/s'})
    return xray.decode_cf(ds)
Beispiel #2
0
 def test_vector_to_radial_roundtrip(self):
     # round trip some random values.
     for i in range(10):
         magnitude = np.random.uniform(0., 5.)
         direction = np.random.uniform(-np.pi, np.pi)
         vector = angles.radial_to_vector(magnitude, direction)
         radial = angles.vector_to_radial(*vector)
         self.assertAlmostEqual(radial[0], magnitude, 5)
         self.assertAlmostEqual(radial[1], direction, 5)
Beispiel #3
0
    def test_vector_radial(self):

        pairs = [((0., 1.), (1., 0.)),
                 ((1., 0.), (1., np.pi / 2)),
                 ((-1., 0.), (1., - np.pi / 2)),
                 ((0., -1.), (1., np.pi)),
                 ((3., 4.), (5., np.arccos(4. / 5.))),
                 ((0., 0.), (0., 0.))]

        for vector, radial in pairs:
            actual = angles.vector_to_radial(*vector)
            # convert from radial to vector and compare
            np.testing.assert_almost_equal(actual,
                                           radial,
                                           err_msg=("vector_to_radial\n"
                                                    "actual: %s "
                                                    "expected: %s "
                                                    "args: %s"
                                                    % (actual, radial,
                                                       vector)))
            # now do the same in reverse
            actual = angles.radial_to_vector(*radial)
            np.testing.assert_almost_equal(actual,
                                           vector,
                                           err_msg=("radial_to_vector\n"
                                                    "actual: %s "
                                                    "expected: %s "
                                                    "args: %s"
                                                    % (actual, vector,
                                                       radial)))
            # now try with direction indicating where the field is flowing from.
            actual = angles.vector_to_radial(*vector, orientation="from")
            # add pi to the expected value.  Note this is done in a way that
            # keeps the result in [-pi, pi).
            radial = (radial[0], np.mod(radial[1] + 2 * np.pi, 2 * np.pi) - np.pi)
            # convert from radial to vector and compare
            np.testing.assert_almost_equal(actual,
                                           radial,
                                           err_msg=("vector_to_radial\n"
                                                    "actual: %s "
                                                    "expected: %s "
                                                    "args: %s"
                                                    % (actual, radial,
                                                       vector)))