示例#1
0
    def test_Query_datetime1(self):
        d = cf.Data([[1., 5.], [6, 2]],
                    'days since 2000-12-29 21:00:00',
                    calendar='standard')

        message = 'Diff =' + str(
            (d -
             cf.Data(cf.dt('2001-01-03 21:00:00', calendar='standard'))).array)

        self.assertTrue((d == cf.eq(cf.dt('2001-01-03 21:00:00'))).equals(
            cf.Data([[False, True], [False, False]]), verbose=2), message)
        self.assertTrue((d == cf.ne(cf.dt('2001-01-03 21:00:00'))).equals(
            cf.Data([[True, False], [True, True]])), message)
        self.assertTrue((d == cf.ge(cf.dt('2001-01-03 21:00:00'))).equals(
            cf.Data([[False, True], [True, False]])), message)
        self.assertTrue((d == cf.gt(cf.dt('2001-01-03 21:00:00'))).equals(
            cf.Data([[False, False], [True, False]])), message)
        self.assertTrue((d == cf.le(cf.dt('2001-01-03 21:00:00'))).equals(
            cf.Data([[True, True], [False, True]])), message)
        self.assertTrue((d == cf.lt(cf.dt('2001-01-03 21:00:00'))).equals(
            cf.Data([[True, False], [False, True]])), message)
        self.assertTrue((d == cf.wi(cf.dt('2000-12-31 21:00:00'),
                                    cf.dt('2001-01-03 21:00:00'))).equals(
                                        cf.Data([[False, True],
                                                 [False, True]])), message)
        self.assertTrue((d == cf.wo(cf.dt('2000-12-31 21:00:00'),
                                    cf.dt('2001-01-03 21:00:00'))).equals(
                                        cf.Data([[True, False],
                                                 [True, False]])), message)
        self.assertTrue((d == cf.set([
            cf.dt('2000-12-31 21:00:00'),
            cf.dt('2001-01-03 21:00:00')
        ])).equals(cf.Data([[False, True], [False, True]])), message)

        _ = cf.seasons()
        [
            cf.seasons(n, start) for n in [1, 2, 3, 4, 6, 12]
            for start in range(1, 13)
        ]
        with self.assertRaises(Exception):
            cf.seasons(13)
        with self.assertRaises(Exception):
            cf.seasons(start=8.456)

        _ = cf.mam()
        _ = cf.djf()
        _ = cf.jja()
        _ = cf.son()
示例#2
0
    def test_Query_evaluate(self):
        for x in (5, cf.Data(5, 'kg m-2'), cf.Data([5], 'kg m-2 s-1')):
            self.assertEqual(x, cf.eq(5))
            self.assertEqual(x, cf.lt(8))
            self.assertEqual(x, cf.le(8))
            self.assertEqual(x, cf.gt(3))
            self.assertEqual(x, cf.ge(3))
            self.assertEqual(x, cf.wi(3, 8))
            self.assertEqual(x, cf.wo(8, 11))
            self.assertEqual(x, cf.set([3, 5, 8]))

            self.assertEqual(cf.eq(5), x)
            self.assertEqual(cf.lt(8), x)
            self.assertEqual(cf.le(8), x)
            self.assertEqual(cf.gt(3), x)
            self.assertEqual(cf.ge(3), x)
            self.assertEqual(cf.wi(3, 8), x)
            self.assertEqual(cf.wo(8, 11), x)
            self.assertEqual(cf.set([3, 5, 8]), x)

            self.assertNotEqual(x, cf.eq(8))
            self.assertNotEqual(x, cf.lt(3))
            self.assertNotEqual(x, cf.le(3))
            self.assertNotEqual(x, cf.gt(8))
            self.assertNotEqual(x, cf.ge(8))
            self.assertNotEqual(x, cf.wi(8, 11))
            self.assertNotEqual(x, cf.wo(3, 8))
            self.assertNotEqual(x, cf.set([3, 8, 11]))

            # Test that the evaluation is commutative i.e. A == B
            # means B == A
            self.assertNotEqual(x, cf.eq(8))
            self.assertNotEqual(x, cf.lt(3))
            self.assertNotEqual(x, cf.le(3))
            self.assertNotEqual(x, cf.gt(8))
            self.assertNotEqual(x, cf.ge(8))
            self.assertNotEqual(x, cf.wi(8, 11))
            self.assertNotEqual(x, cf.wo(3, 8))
            self.assertNotEqual(x, cf.set([3, 8, 11]))

            self.assertNotEqual(cf.eq(8), x)
            self.assertNotEqual(cf.lt(3), x)
            self.assertNotEqual(cf.le(3), x)
            self.assertNotEqual(cf.gt(8), x)
            self.assertNotEqual(cf.ge(8), x)
            self.assertNotEqual(cf.wi(8, 11), x)
            self.assertNotEqual(cf.wo(3, 8), x)
            self.assertNotEqual(cf.set([3, 8, 11]), x)
        # --- End: for

        c = cf.wi(2, 4)
        d = cf.wi(6, 8)

        e = d | c

        self.assertTrue(c.evaluate(3))
        self.assertFalse(c.evaluate(5))

        self.assertTrue(e.evaluate(3))
        self.assertTrue(e.evaluate(7))
        self.assertFalse(e.evaluate(5))

        self.assertEqual(3, c)
        self.assertNotEqual(5, c)

        self.assertEqual(c, 3)
        self.assertNotEqual(c, 5)

        self.assertEqual(3, e)
        self.assertEqual(7, e)
        self.assertNotEqual(5, e)

        self.assertEqual(e, 3)
        self.assertEqual(e, 7)
        self.assertNotEqual(e, 5)

        x = 'qwerty'
        self.assertEqual(x, cf.eq('qwerty'))
        self.assertEqual(x, cf.eq(re.compile('^qwerty$')))
        self.assertEqual(x, cf.eq(re.compile('qwe')))
        self.assertEqual(x, cf.eq(re.compile('qwe.*')))
        self.assertEqual(x, cf.eq(re.compile('.*qwe')))
        self.assertEqual(x, cf.eq(re.compile('.*rty')))
        self.assertEqual(x, cf.eq(re.compile('.*rty$')))
        self.assertEqual(x, cf.eq(re.compile('^.*rty$')))
        self.assertEqual(x, cf.eq(re.compile('rty$')))

        self.assertNotEqual(x, cf.eq('QWERTY'))
        self.assertNotEqual(x, cf.eq(re.compile('QWERTY')))
        self.assertNotEqual(x, cf.eq(re.compile('^QWERTY$')))
        self.assertNotEqual(x, cf.eq(re.compile('QWE')))
        self.assertNotEqual(x, cf.eq(re.compile('QWE.*')))
        self.assertNotEqual(x, cf.eq(re.compile('.*QWE')))
        self.assertNotEqual(x, cf.eq(re.compile('.*RTY')))
        self.assertNotEqual(x, cf.eq(re.compile('.*RTY$')))
        self.assertNotEqual(x, cf.eq(re.compile('^.*RTY$')))
示例#3
0
    def test_Domain_subspace(self):
        f = self.d.copy()

        x = f.dimension_coordinate("X")
        a = x.varray
        a[...] = numpy.arange(0, 360, 40)
        x.set_bounds(x.create_bounds())
        f.cyclic("X", iscyclic=True, period=360)

        f0 = f.copy()

        # wi (increasing)
        g = f.subspace(grid_longitude=cf.wi(50, 130))
        self.assertEqual(g.size, 20)
        x = g.construct("grid_longitude").array
        self.assertTrue((x == [80, 120]).all())

        g = f.subspace(grid_longitude=cf.wi(-90, 50))
        self.assertEqual(g.size, 40)
        x = g.construct("grid_longitude").array
        self.assertTrue((x == [-80, -40, 0, 40]).all())

        g = f.subspace(grid_longitude=cf.wi(310, 450))
        self.assertEqual(g.size, 40)
        x = g.construct("grid_longitude").array
        self.assertTrue((x == [-40, 0, 40, 80]).all())

        g = f.subspace(grid_longitude=cf.wi(310 - 1080, 450 - 1080))
        self.assertEqual(g.size, 40)
        x = g.construct("grid_longitude").array
        self.assertTrue((x == [-40, 0, 40, 80]).all())

        g = f.subspace(grid_longitude=cf.wi(310 + 720, 450 + 720))
        self.assertEqual(g.size, 40)
        x = g.construct("grid_longitude").array
        self.assertTrue((x == [-40, 0, 40, 80]).all())

        g = f.subspace(grid_longitude=cf.wi(-90, 370))
        self.assertEqual(g.size, 90)
        x = g.construct("grid_longitude").array
        self.assertTrue((x == [-80, -40, 0, 40, 80, 120, 160, 200,
                               240.0]).all())

        with self.assertRaises(IndexError):
            f.indices(grid_longitude=cf.wi(90, 100))

        # wi (decreasing)
        f.flip("X", inplace=True)

        g = f.subspace(grid_longitude=cf.wi(50, 130))
        self.assertEqual(g.size, 20)
        x = g.construct("grid_longitude").array
        self.assertTrue((x == [80, 120][::-1]).all())

        g = f.subspace(grid_longitude=cf.wi(-90, 50))
        self.assertEqual(g.size, 40)
        x = g.construct("grid_longitude").array
        self.assertTrue((x == [-80, -40, 0, 40][::-1]).all())

        g = f.subspace(grid_longitude=cf.wi(310, 450))
        self.assertEqual(g.size, 40)
        x = g.construct("grid_longitude").array
        self.assertTrue((x == [-40, 0, 40, 80][::-1]).all())

        g = f.subspace(grid_longitude=cf.wi(310 - 1080, 450 - 1080))
        self.assertEqual(g.size, 40)
        x = g.construct("grid_longitude").array
        self.assertTrue((x == [-40, 0, 40, 80][::-1]).all())

        g = f.subspace(grid_longitude=cf.wi(310 + 720, 450 + 720))
        self.assertEqual(g.size, 40)
        x = g.construct("grid_longitude").array
        self.assertTrue((x == [-40, 0, 40, 80][::-1]).all())

        with self.assertRaises(IndexError):
            f.indices(grid_longitude=cf.wi(90, 100))

        # wo
        f = f0.copy()

        g = f.subspace(grid_longitude=cf.wo(50, 130))
        self.assertEqual(g.size, 70)
        x = g.construct("grid_longitude").array
        self.assertTrue((x == [-200, -160, -120, -80, -40, 0, 40]).all())

        with self.assertRaises(IndexError):
            f.indices(grid_longitude=cf.wo(-90, 370))

        # set
        g = f.subspace(grid_longitude=cf.set([320, 40, 80, 99999]))
        self.assertEqual(g.size, 30)
        x = g.construct("grid_longitude").array
        self.assertTrue((x == [40, 80, 320]).all())

        g = f.subspace(grid_longitude=cf.lt(90))
        self.assertEqual(g.size, 30)
        x = g.construct("grid_longitude").array
        self.assertTrue((x == [0, 40, 80]).all())

        g = f.subspace(grid_longitude=cf.gt(90))
        self.assertEqual(g.size, 60)
        x = g.construct("grid_longitude").array
        self.assertTrue((x == [120, 160, 200, 240, 280, 320]).all())

        g = f.subspace(grid_longitude=cf.le(80))
        self.assertEqual(g.size, 30)
        x = g.construct("grid_longitude").array
        self.assertTrue((x == [0, 40, 80]).all())

        g = f.subspace(grid_longitude=cf.ge(80))
        self.assertEqual(g.size, 70)
        x = g.construct("grid_longitude").array
        self.assertTrue((x == [80, 120, 160, 200, 240, 280, 320]).all())

        # 2-d
        lon = f.auxiliary_coordinate("X")
        lon.data[...] = numpy.arange(60, 150).reshape(9, 10)

        lat = f.auxiliary_coordinate("Y")
        lat.data[...] = numpy.arange(-45, 45).reshape(10, 9)

        for mode in ("compress", "envelope"):
            g = f.subspace(mode, longitude=cf.wi(92, 134))
            size = 50
            self.assertEqual(g.size, size)

        for mode in (("compress", ), ("envelope", )):
            g = f.subspace(*mode, longitude=cf.wi(72, 83) | cf.gt(118))
            if mode == ("envelope", ):
                size = 80
            else:
                size = 60

            self.assertEqual(g.size, size, mode)

        for mode in ("compress", "envelope"):
            g = f.subspace(mode, grid_longitude=cf.contains(23.2))
            size = 10
            self.assertEqual(g.size, size)
            self.assertEqual(g.construct("grid_longitude").array, 40)

        for mode in ("compress", "envelope"):
            g = f.subspace(mode, grid_latitude=cf.contains(1))
            size = 9
            self.assertEqual(g.size, size)
            self.assertEqual(g.construct("grid_latitude").array, 0.88)

        for mode in ("compress", "envelope"):
            g = f.subspace(mode, longitude=cf.contains(83))
            size = 1
            self.assertEqual(g.size, size)

            self.assertEqual(g.construct("longitude").array, 83)

        # Calls that should fail
        with self.assertRaises(Exception):
            f.subspace(grid_longitude=cf.gt(23), X=cf.wi(92, 134))
        with self.assertRaises(Exception):
            f.subspace(grid_longitude=cf.gt(23), longitude=cf.wi(92, 134))
        with self.assertRaises(Exception):
            f.subspace(grid_latitude=cf.contains(-23.2))
示例#4
0
    def test_Query_evaluate(self):
        for x in (5, cf.Data(5, "kg m-2"), cf.Data([5], "kg m-2 s-1")):
            self.assertEqual(x, cf.eq(5))
            self.assertEqual(x, cf.lt(8))
            self.assertEqual(x, cf.le(8))
            self.assertEqual(x, cf.gt(3))
            self.assertEqual(x, cf.ge(3))
            self.assertEqual(x, cf.wi(3, 8))
            self.assertEqual(x, cf.wo(8, 11))
            self.assertEqual(x, cf.set([3, 5, 8]))

            self.assertEqual(cf.eq(5), x)
            self.assertEqual(cf.lt(8), x)
            self.assertEqual(cf.le(8), x)
            self.assertEqual(cf.gt(3), x)
            self.assertEqual(cf.ge(3), x)
            self.assertEqual(cf.wi(3, 8), x)
            self.assertEqual(cf.wo(8, 11), x)
            self.assertEqual(cf.set([3, 5, 8]), x)

            self.assertNotEqual(x, cf.eq(8))
            self.assertNotEqual(x, cf.lt(3))
            self.assertNotEqual(x, cf.le(3))
            self.assertNotEqual(x, cf.gt(8))
            self.assertNotEqual(x, cf.ge(8))
            self.assertNotEqual(x, cf.wi(8, 11))
            self.assertNotEqual(x, cf.wo(3, 8))
            self.assertNotEqual(x, cf.set([3, 8, 11]))

            # Test that the evaluation is commutative i.e. A == B
            # means B == A
            self.assertNotEqual(x, cf.eq(8))
            self.assertNotEqual(x, cf.lt(3))
            self.assertNotEqual(x, cf.le(3))
            self.assertNotEqual(x, cf.gt(8))
            self.assertNotEqual(x, cf.ge(8))
            self.assertNotEqual(x, cf.wi(8, 11))
            self.assertNotEqual(x, cf.wo(3, 8))
            self.assertNotEqual(x, cf.set([3, 8, 11]))

            self.assertNotEqual(cf.eq(8), x)
            self.assertNotEqual(cf.lt(3), x)
            self.assertNotEqual(cf.le(3), x)
            self.assertNotEqual(cf.gt(8), x)
            self.assertNotEqual(cf.ge(8), x)
            self.assertNotEqual(cf.wi(8, 11), x)
            self.assertNotEqual(cf.wo(3, 8), x)
            self.assertNotEqual(cf.set([3, 8, 11]), x)
        # --- End: for

        c = cf.wi(2, 4)
        d = cf.wi(6, 8)

        e = d | c

        self.assertTrue(c.evaluate(3))
        self.assertFalse(c.evaluate(5))

        self.assertTrue(e.evaluate(3))
        self.assertTrue(e.evaluate(7))
        self.assertFalse(e.evaluate(5))

        self.assertEqual(3, c)
        self.assertNotEqual(5, c)

        self.assertEqual(c, 3)
        self.assertNotEqual(c, 5)

        self.assertEqual(3, e)
        self.assertEqual(7, e)
        self.assertNotEqual(5, e)

        self.assertEqual(e, 3)
        self.assertEqual(e, 7)
        self.assertNotEqual(e, 5)

        x = "qwerty"
        self.assertEqual(x, cf.eq("qwerty"))
        self.assertEqual(x, cf.eq(re.compile("^qwerty$")))
        self.assertEqual(x, cf.eq(re.compile("qwe")))
        self.assertEqual(x, cf.eq(re.compile("qwe.*")))
        self.assertEqual(x, cf.eq(re.compile(".*qwe")))
        self.assertEqual(x, cf.eq(re.compile(".*rty")))
        self.assertEqual(x, cf.eq(re.compile(".*rty$")))
        self.assertEqual(x, cf.eq(re.compile("^.*rty$")))
        self.assertEqual(x, cf.eq(re.compile("rty$")))

        self.assertNotEqual(x, cf.eq("QWERTY"))
        self.assertNotEqual(x, cf.eq(re.compile("QWERTY")))
        self.assertNotEqual(x, cf.eq(re.compile("^QWERTY$")))
        self.assertNotEqual(x, cf.eq(re.compile("QWE")))
        self.assertNotEqual(x, cf.eq(re.compile("QWE.*")))
        self.assertNotEqual(x, cf.eq(re.compile(".*QWE")))
        self.assertNotEqual(x, cf.eq(re.compile(".*RTY")))
        self.assertNotEqual(x, cf.eq(re.compile(".*RTY$")))
        self.assertNotEqual(x, cf.eq(re.compile("^.*RTY$")))
示例#5
0
    def test_GENERAL(self):
        # Save original chunksize
        original_chunksize = cf.chunksize()

        cf.chunksize(60)

        g = self.f.squeeze()
        f = self.f.copy()

        c = cf.set([0, 3, 4, 5])

        _ = f == c

        # +, -, *, /, **
        h = g.copy()
        h **= 2
        h **= 0.5
        h.standard_name = g.standard_name
        self.assertTrue(g.data.allclose(h.data), repr(g.array - h.array))
        h *= 10
        h /= 10.0
        self.assertTrue(g.data.allclose(h.data), repr(g.array - h.array))
        h += 1
        h -= 1
        self.assertTrue(g.data.allclose(h.data), repr(g.array - h.array))
        h = h ** 2.0
        h = h ** 0.5
        h.standard_name = g.standard_name
        self.assertTrue(g.data.allclose(h.data), repr(g.array - h.array))
        h = h * 10
        h = h / 10.0
        self.assertTrue(g.data.allclose(h.data), repr(g.array - h.array))
        h = h + 1
        h = h - 1
        self.assertTrue(g.data.allclose(h.data), repr(g.array - h.array))

        # flip, expand_dims, squeeze and remove_axes
        h = g.copy()
        h.flip((1, 0), inplace=True)
        h.flip((1, 0), inplace=True)
        h.flip(0, inplace=True)
        h.flip(1, inplace=True)
        h.flip([0, 1], inplace=True)
        self.assertTrue(g.equals(h, verbose=2))

        # Access the field's data as a numpy array
        g.array
        g.item("latitude").array
        g.item("longitude").array

        # Subspace the field
        g[..., 2:5].array
        g[9::-4, ...].array
        h = g[(slice(None, None, -1),) * g.ndim]
        h = h[(slice(None, None, -1),) * h.ndim]
        self.assertTrue(g.equals(h, verbose=2))

        # Indices for a subspace defined by coordinates
        f.indices()
        f.indices(grid_latitude=cf.lt(5), grid_longitude=27)
        f.indices(
            grid_latitude=cf.lt(5),
            grid_longitude=27,
            atmosphere_hybrid_height_coordinate=1.5,
        )

        # Subspace the field
        g.subspace(
            grid_latitude=cf.lt(5),
            grid_longitude=27,
            atmosphere_hybrid_height_coordinate=1.5,
        )

        # Create list of fields
        fl = cf.FieldList([g, g, g, g])

        # Write a list of fields to disk
        cf.write((f, fl), tmpfile)
        cf.write(fl, tmpfile)

        # Read a list of fields from disk
        fl = cf.read(tmpfile, squeeze=True)
        for f in fl:
            try:
                del f.history
            except AttributeError:
                pass

        # Access the last field in the list
        x = fl[-1]

        # Access the data of the last field in the list
        x = fl[-1].array

        # Modify the last field in the list
        fl[-1] *= -1
        x = fl[-1].array

        # Changing units
        fl[-1].units = "mm.s-1"
        x = fl[-1].array

        # Combine fields not in place
        g = fl[-1] - fl[-1]
        x = g.array

        # Combine field with a size 1 Data object
        g += cf.Data([[[[[1.5]]]]], "cm.s-1")
        x = g.array

        # Setting of (un)masked elements with where()
        g[::2, 1::2] = numpy.ma.masked
        g.data.to_memory(1)
        g.where(True, 99)
        g.data.to_memory(1)
        g.where(g.mask, 2)
        g.data.to_memory(1)

        g[slice(None, None, 2), slice(1, None, 2)] = cf.masked
        g.data.to_memory(1)
        g.where(g.mask, [[-1]])
        g.data.to_memory(1)
        g.where(True, cf.Data(0, None))
        g.data.to_memory(1)

        h = g[:3, :4]
        h.where(True, -1)
        h[0, 2] = 2
        h.transpose([1, 0], inplace=True)

        h.flip([1, 0], inplace=True)

        g[slice(None, 3), slice(None, 4)] = h

        h = g[:3, :4]
        h[...] = -1
        h[0, 2] = 2
        g[slice(None, 3), slice(None, 4)] = h

        # Make sure all partitions' data are in temporary files
        g.data.to_disk()

        # Push partitions' data from temporary files into memory
        g.data.to_memory(regardless=True)
        g.data.to_disk()

        # Iterate through array values
        for x in f.data.flat():
            pass

        # Reset chunk size
        cf.chunksize(original_chunksize)

        # Move Data partitions to disk
        f.data.to_disk()

        cf.chunksize(original_chunksize)

        f.transpose(inplace=True)
        f.flip(inplace=True)

        cf.write(f, "delme.nc")
        f = cf.read("delme.nc")[0]
        cf.write(f, "delme.nca", fmt="CFA4")
        g = cf.read("delme.nca")[0]

        b = f[:, 0:6, :]
        c = f[:, 6:, :]
        cf.aggregate([b, c], verbose=2)[0]

        # Remove temporary files
        cf.data.partition._remove_temporary_files()

        cf.chunksize(original_chunksize)