def load_data(cls):
        with netCDF4.Dataset(cls.GRID) as ds:
            z = np.flip(ds.variables['tcw'][:].T, axis=1)
            z[z.mask] = float("nan")

            return core.Grid3DFloat64(
                core.Axis(ds.variables['longitude'][:], is_circle=True),
                core.Axis(np.flip(ds.variables['latitude'][:])),
                core.Axis(ds.variables['time'][:]), z.data)
    def load_data(self):
        x = np.arange(-1, 1, 0.2)
        y = np.arange(-1, 1, 0.2)
        z = np.arange(-1, 1, 0.2)
        u = np.arange(-1, 10, 0.2)

        mx, my, mz, mu = np.meshgrid(x, y, z, u)
        return core.Grid4DFloat64(core.Axis(x), core.Axis(y), core.Axis(z),
                                  core.Axis(u), self.f4d(mx, my, mz, mu))
Esempio n. 3
0
def load_data(temporal_axis=False):
    with netCDF4.Dataset(GRID) as ds:
        z = np.flip(ds.variables['tcw'][:].T, axis=1)
        z[z.mask] = float("nan")
        z_axis = core.TemporalAxis(
            netCDF4.num2date(
                ds.variables['time'][:],
                ds.variables['time'].units,
                only_use_cftime_datetimes=False,
                only_use_python_datetimes=True).astype("datetime64[h]").astype(
                    "int64")) if temporal_axis else core.Axis(
                        ds.variables['time'][:])
        class_ = core.TemporalGrid3DFloat64 if temporal_axis else core.Grid3DFloat64

        return class_(core.Axis(ds.variables['longitude'][:], is_circle=True),
                      core.Axis(np.flip(ds.variables['latitude'][:])), z_axis,
                      z.data)
    def test_binning2d_acessors(self):
        x_axis = core.Axis(np.linspace(-180, 180, 10), is_circle=True)
        y_axis = core.Axis(np.linspace(-90, 90, 10))

        binning = core.Binning2DFloat64(x_axis, y_axis)
        self.assertIsInstance(binning.x, core.Axis)
        self.assertIsInstance(binning.y, core.Axis)
        # The class must return a reference on the axes provided during
        # construction
        self.assertEqual(id(x_axis), id(binning.x))
        self.assertEqual(id(y_axis), id(binning.y))

        binning.clear()
        count = binning.count()
        self.assertIsInstance(count, np.ndarray)
        self.assertEqual(count.size, len(x_axis) * len(y_axis))
        self.assertEqual(count.mean(), 0)
Esempio n. 5
0
def test_binning2d_acessors():
    x_axis = core.Axis(np.linspace(-180, 180, 10), is_circle=True)
    y_axis = core.Axis(np.linspace(-90, 90, 10))

    binning = core.Binning2DFloat64(x_axis, y_axis)
    assert isinstance(binning.x, core.Axis)
    assert isinstance(binning.y, core.Axis)

    # The class must return a reference on the axes provided during
    # construction
    assert id(x_axis) == id(binning.x)
    assert id(y_axis) == id(binning.y)

    binning.clear()
    count = binning.count()
    assert isinstance(count, np.ndarray)
    assert count.size == len(x_axis) * len(y_axis)
    assert count.mean() == 0
    def test_binning2d_methods(self):
        x_axis = core.Axis(np.linspace(-180, 180, 361 // 4), is_circle=True)
        y_axis = core.Axis(np.linspace(-90, 90, 180 // 4))

        binning = core.Binning2DFloat64(x_axis, y_axis, None)
        x, y, z = self.load_data()
        mx, my = np.meshgrid(x, y, indexing='ij')
        binning.push(mx.flatten(), my.flatten(), z.flatten())

        count = binning.count()
        self.assertNotEqual(count.max(), 0)
        simple_mean = np.ma.fix_invalid(binning.mean())
        if HAVE_PLT:
            mx, my = np.meshgrid(x_axis[:], y_axis[:], indexing='ij')
            plot(mx, my, simple_mean, "binning2d_simple.png")

        mx, my = np.meshgrid(x, y, indexing='ij')
        binning.clear()
        binning.push(mx.flatten(), my.flatten(), z.flatten(), simple=False)

        count = binning.count()
        self.assertNotEqual(count.max(), 0)
        linear_mean = np.ma.fix_invalid(binning.mean())
        if HAVE_PLT:
            mx, my = np.meshgrid(x_axis[:], y_axis[:], indexing='ij')
            plot(mx, my, linear_mean, "binning2d_linear.png")

        self.assertFalse(np.all(linear_mean == simple_mean))

        # Test of access to statistical variables
        self.assertIsInstance(binning.kurtosis(), np.ndarray)
        self.assertIsInstance(binning.max(), np.ndarray)
        self.assertIsInstance(binning.median(), np.ndarray)
        self.assertIsInstance(binning.min(), np.ndarray)
        self.assertIsInstance(binning.skewness(), np.ndarray)
        self.assertIsInstance(binning.sum(), np.ndarray)
        self.assertIsInstance(binning.sum_of_weights(), np.ndarray)
        self.assertIsInstance(binning.variance(), np.ndarray)
Esempio n. 7
0
    def test_axis_accessor(self):
        lon = np.linspace(0, 359, 360)
        a = core.Axis(lon, is_circle=False)
        b = core.Axis(lon, is_circle=False)
        self.assertEqual(a, b)
        self.assertFalse(a != b)
        self.assertEqual(str(a), str(b))
        self.assertEqual(str(a),
                         "Axis([0, 1, 2, ..., 358, 359], is_circle=false)")
        self.assertEqual(a.increment(), 1)
        self.assertTrue(a.is_ascending())
        self.assertEqual(a.front(), 0)
        self.assertEqual(a.back(), 359)
        self.assertTrue(
            np.all(
                a.find_index(np.arange(0, 359, 1) +
                             0.01) == np.arange(0, 359, 1)))
        indexes = a.find_indexes(np.arange(0, 359, 1))
        self.assertTrue(np.all(indexes[:, 0] == np.arange(0, 359, 1)))
        self.assertTrue(np.all(indexes[:, 1] == np.arange(1, 360, 1)))
        self.assertTrue(a.is_regular())
        self.assertEqual(a.min_value(), 0)
        self.assertEqual(a.max_value(), 359)
        self.assertEqual(a[0], 0)
        self.assertTrue(np.all(a[:] == np.arange(0, 360)))
        self.assertEqual(len(a), 360)
        b = a.flip(inplace=True)
        self.assertEqual(id(a), id(b))
        self.assertEqual(a.increment(), -1)
        self.assertFalse(a.is_ascending())
        self.assertEqual(a.front(), 359)
        self.assertEqual(a.back(), 0)
        self.assertTrue(
            np.all(
                a.find_index(np.arange(359, -1, -1) +
                             0.01) == np.arange(0, 360, 1)))
        self.assertTrue(a.is_regular())
        self.assertEqual(a.min_value(), 0)
        self.assertEqual(a.max_value(), 359)
        self.assertEqual(a[0], 359)
        self.assertTrue(np.all(a[:] == np.arange(359, -1, -1)))
        self.assertEqual(len(a), 360)

        frozen = pickle.loads(pickle.dumps(a))
        b = a.flip(inplace=False)
        self.assertEqual(a, frozen)
        self.assertNotEqual(id(a), id(b))
        self.assertEqual(b, a.flip(inplace=False))
        b = a.flip(inplace=True)
        self.assertEqual(id(a), id(b))
        self.assertEqual(b, a)
        self.assertNotEqual(a, frozen)

        with self.assertRaises(ValueError):
            core.Axis([], is_circle=False)

        with self.assertRaises(ValueError):
            core.Axis([5, 2, 7], is_circle=False)

        a = core.Axis(MERCATOR_LATITUDES, is_circle=True)
        self.assertFalse(a.is_circle)

        with self.assertRaises(RuntimeError):
            a.increment()
Esempio n. 8
0
def test_axis_accessor():
    lon = np.linspace(0, 359, 360)
    a = core.Axis(lon, is_circle=False)
    b = core.Axis(lon, is_circle=False, epsilon=1e-5)
    assert a == b
    assert not (a != b)
    assert str(a) == str(b)
    assert str(a) == "Axis([0, 1, 2, ..., 358, 359], is_circle=false)"
    assert a.increment() == 1
    assert a.is_ascending()
    assert a.front() == 0
    assert a.back() == 359
    assert np.all(
        a.find_index(np.arange(0, 359, 1) + 0.01) == np.arange(0, 359, 1))
    indexes = a.find_indexes(np.arange(0, 359, 1))
    assert np.all(indexes[:, 0] == np.arange(0, 359, 1))
    assert np.all(indexes[:, 1] == np.arange(1, 360, 1))
    assert a.is_regular()
    assert a.min_value() == 0
    assert a.max_value() == 359
    assert a[0] == 0
    assert np.all(a[:] == np.arange(0, 360))
    assert len(a) == 360
    b = a.flip(inplace=True)
    assert id(a) == id(b)
    assert a.increment() == -1
    assert not a.is_ascending()
    assert a.front() == 359
    assert a.back() == 0
    assert np.all(
        a.find_index(np.arange(359, -1, -1) + 0.01) == np.arange(0, 360, 1))
    assert a.is_regular()
    assert a.min_value() == 0
    assert a.max_value() == 359
    assert a[0] == 359
    assert np.all(a[:] == np.arange(359, -1, -1))
    assert len(a) == 360

    frozen = pickle.loads(pickle.dumps(a))
    b = a.flip(inplace=False)
    assert a == frozen
    assert id(a) != id(b)
    assert b == a.flip(inplace=False)
    b = a.flip(inplace=True)
    assert id(a) == id(b)
    assert b == a
    assert a != frozen

    with pytest.raises(ValueError):
        core.Axis([], is_circle=False)

    with pytest.raises(ValueError):
        core.Axis([5, 2, 7], is_circle=False)

    a = core.Axis(MERCATOR_LATITUDES, is_circle=True)
    assert not a.is_circle

    with pytest.raises(RuntimeError):
        a.increment()

    with pytest.raises(ValueError):
        a = core.Axis(np.full((10, ), 1))