示例#1
0
    def test_Query(self):
        f = cf.read(self.filename)[0]

        q = cf.Query('wi', [2, 5])
        r = cf.Query('le', 67)
        s = q | r
        t = cf.Query('gt', 12, attr='bounds')
        u = s & t

        _ = repr(q)
        _ = repr(s)
        _ = repr(t)
        _ = repr(u)
        _ = str(q)
        _ = str(s)
        _ = str(t)
        _ = str(u)
        _ = u.dump(display=False)

        _ = u.attr
        _ = u.operator
        _ = q.attr
        _ = q.operator
        _ = q.value
        with self.assertRaises(Exception):
            _ = u.value

        self.assertTrue(u.equals(u.copy(), verbose=2))
        self.assertFalse(u.equals(t, verbose=0))

        _ = copy.deepcopy(u)

        c = f.dimension_coordinate('X')
        self.assertTrue(((cf.contains(21.1) == c).array ==
                         numpy.array([0, 1, 0, 0, 0, 0, 0, 0, 0], bool)).all())
        self.assertTrue(((cf.contains(999) == c).array ==
                         numpy.array([0, 0, 0, 0, 0, 0, 0, 0, 0], bool)).all())

        _ = cf.cellsize(34)
        _ = cf.cellsize(q)

        _ = cf.celllt(3)
        _ = cf.cellle(3)
        _ = cf.cellge(3)
        _ = cf.cellgt(3)
        _ = cf.cellwi(1, 2)
        _ = cf.cellwo(1, 2)
示例#2
0
    def test_Query_object_units(self):
        """Check units are processed correctly in and from queries."""

        equivalent_units = {
            1000: ("m", "km"),
            60: ("s", "minute"),
        }  # keys are conversion factors; only use exact equivalents for test
        for conversion, equivalents in equivalent_units.items():
            s_unit, l_unit = equivalents  # s for smaller, l for larger

            # Case 1: only specify units to one component
            q1 = cf.Query("gt", cf.Data(1, s_unit))
            q2 = cf.Query("ge", 1, units=s_unit)
            # Case 2: specify *equal* units across the two components
            q3 = cf.Query("lt", cf.Data(1, s_unit), units=s_unit)
            # Case 3: specify *equivalent* units across the two components
            q4 = cf.Query("le", cf.Data(1, s_unit), units=l_unit)
            # See also final Case 4, below.

            # A) test processed correctly inside unit itself
            for q in [q1, q2, q3, q4]:
                self.assertIsInstance(q, cf.query.Query)
                # TODO: should q4 should return s_ or l_unit? ATM is s_unit.
                self.assertEqual(q._value.Units._units, s_unit)

            with self.assertRaises(ValueError):
                # Case 4: provide non-equivalent units i.e. non-sensical query
                cf.Query("le", cf.Data(1, "m"), units="s")

            # B) Check units are processed correctly when a Query is evaluated
            q5 = cf.Query("eq", conversion, units=s_unit)
            # Pre-comparison, values should be converted for consistent units
            self.assertTrue(
                q5.evaluate(cf.Data([1, 2],
                                    l_unit)).equals(cf.Data([True, False])))
示例#3
0
fl.select_by_identity(re.compile('.*potential.*'))
fl.select_by_identity('relative_humidity')
fl('air_temperature')
fl.select('air_temperature')
print(t)
t.match_by_identity('air_temperature')
t.match_by_rank(4)
t.match_by_units('degC', exact=False)
t.match_by_construct(longitude=cf.wi(-10, 10))
t.match('specific_humidity')
t.match('specific_humidity', 'air_temperature')

print("\n**Encapsulating conditions**\n")

c = cf.Query('lt', 3)
c
c.evaluate(2)
c == 2
c != 2
c.evaluate(3)
c == cf.Data([1, 2, 3])
c == numpy.array([1, 2, 3])
ge3 = cf.Query('ge', 3)
lt5 = cf.Query('lt', 5)
c = ge3 & lt5
c
c == 2
c != 2
c = ge3 | lt5
c