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()
qr = q.roll('X', shift=-3) print(qr.array[0]) print(q.dimension_coordinate('X').array) print(qr.dimension_coordinate('X').array) print(q.anchor('X', -150)) print(q.anchor('X', -750)) print("\n**Subspacing by metadata**\n") print(q) print(q.construct('X').array) q2 = q.subspace(X=112.5) print(q2) print(q.construct('latitude').array) print(q.subspace(X=112.5, latitude=cf.gt(-60))) c = cf.eq(-45) | cf.ge(20) c print(q.subspace(latitude=c)) print(q.subspace(X=[1, 2, 4], Y=slice(None, None, -1))) print(q.subspace(X=cf.wi(-100, 200))) print(q.subspace(X=slice(-2, 4))) a = cf.read('timeseries.nc')[0] print(a) print(a.coordinate('T').array[0:9]) print(a.coordinate('T').datetime_array[0:9]) print(a.subspace(T=410.5)) print(a.subspace(T=cf.dt('1960-04-16'))) print(a.subspace(T=cf.wi(cf.dt('1962-11-01'), cf.dt('1967-03-17 07:30')))) print(q.array) q2 = q.subspace('compress', X=[1, 2, 4, 6]) print(q2)
qr = q.roll('X', shift=-3) print(qr.array[0]) print(q.dimension_coordinate('X').array) print(qr.dimension_coordinate('X').array) print(q.anchor('X', -150)) print(q.anchor('X', -750)) print("\n**Subspacing by metadata**\n") print(q) print(q.construct('X').array) q2 = q.subspace(X=112.5) print(q2) print(q.construct('latitude').array) print(q.subspace(X=112.5, latitude=cf.gt(-60))) c = cf.eq(-45) | cf.ge(20) c print(q.subspace(latitude=c)) print(q.subspace(X=[1, 2, 4], Y=slice(None, None, -1))) print(q.subspace(X=cf.wi(-100, 200))) print(q.subspace(X=slice(-2, 4))) a = cf.read('timeseries.nc')[0] print(a) print(a.coordinate('T').array[0:9]) print(a.coordinate('T').datetime_array[0:9]) print(a.subspace(T=410.5)) print(a.subspace(T=cf.dt('1960-04-16'))) print(a.subspace(T=cf.wi(cf.dt('1962-11-01'), cf.dt('1967-03-17 07:30')))) print(q.array) q2 = q.subspace('compress', X=[1, 2, 4, 6]) print(q2)
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$')))
def test_Query_dteq_dtne_dtge_dtgt_dtle_dtlt(self): d = cf.Data([[1., 5.], [6, 2]], 'days since 2000-12-29 21:00:00') 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]])), 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.eq(cf.dt(2001, 1, 3, 21, 0, 0))).equals( cf.Data([[False, True], [False, False]])), message ) self.assertTrue( (d == cf.ne(cf.dt(2001, 1, 3, 21, 0, 0))).equals( cf.Data([[True, False], [True, True]])), message ) self.assertTrue( (d == cf.ge(cf.dt(2001, 1, 3, 21, 0, 0))).equals( cf.Data([[False, True], [True, False]])), message ) self.assertTrue( (d == cf.gt(cf.dt(2001, 1, 3, 21, 0, 0))).equals( cf.Data([[False, False], [True, False]])), message ) self.assertTrue( (d == cf.le(cf.dt(2001, 1, 3, 21, 0, 0))).equals( cf.Data([[True, True], [False, True]])), message ) self.assertTrue( (d == cf.lt(cf.dt(2001, 1, 3, 21, 0, 0))).equals( cf.Data([[True, False], [False, True]])), message ) d = cf.dt(2002, 6, 16) self.assertEqual(cf.eq(cf.dt(2002, 6, 16)), d) self.assertNotEqual(cf.eq(cf.dt(1990, 1, 1)), d) self.assertNotEqual(cf.eq(cf.dt('2100-1-1')), d) self.assertNotEqual( cf.eq(cf.dt('2001-1-1')) & cf.eq(cf.dt(2010, 12, 31)), d) d = cf.dt(2002, 6, 16) self.assertEqual(cf.ge(cf.dt(1990, 1, 1)), d) self.assertEqual(cf.ge(cf.dt(2002, 6, 16)), d) self.assertNotEqual(cf.ge(cf.dt('2100-1-1')), d) self.assertNotEqual( cf.ge(cf.dt('2001-1-1')) & cf.ge(cf.dt(2010, 12, 31)), d) d = cf.dt(2002, 6, 16) self.assertEqual(cf.gt(cf.dt(1990, 1, 1)), d) self.assertNotEqual(cf.gt(cf.dt(2002, 6, 16)), d) self.assertNotEqual(cf.gt(cf.dt('2100-1-1')), d) self.assertEqual( cf.gt(cf.dt('2001-1-1')) & cf.le(cf.dt(2010, 12, 31)), d) d = cf.dt(2002, 6, 16) self.assertEqual(cf.ne(cf.dt(1990, 1, 1)), d) self.assertNotEqual(cf.ne(cf.dt(2002, 6, 16)), d) self.assertEqual(cf.ne(cf.dt('2100-1-1')), d) self.assertEqual( cf.ne(cf.dt('2001-1-1')) & cf.ne(cf.dt(2010, 12, 31)), d) d = cf.dt(2002, 6, 16) self.assertNotEqual(cf.le(cf.dt(1990, 1, 1)), d) self.assertEqual(cf.le(cf.dt(2002, 6, 16)), d) self.assertEqual(cf.le(cf.dt('2100-1-1')), d) self.assertNotEqual( cf.le(cf.dt('2001-1-1')) & cf.le(cf.dt(2010, 12, 31)), d) d = cf.dt(2002, 6, 16) self.assertNotEqual(cf.lt(cf.dt(1990, 1, 1)), d) self.assertNotEqual(cf.lt(cf.dt(2002, 6, 16)), d) self.assertEqual(cf.lt(cf.dt('2100-1-1')), d) self.assertNotEqual( cf.lt(cf.dt('2001-1-1')) & cf.lt(cf.dt(2010, 12, 31)), d)
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$")))
def test_Query_dteq_dtne_dtge_dtgt_dtle_dtlt(self): d = cf.Data([[1.0, 5.0], [6, 2]], "days since 2000-12-29 21:00:00") 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]])), 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.eq(cf.dt(2001, 1, 3, 21, 0, 0))).equals( cf.Data([[False, True], [False, False]])), message, ) self.assertTrue( (d == cf.ne(cf.dt(2001, 1, 3, 21, 0, 0))).equals( cf.Data([[True, False], [True, True]])), message, ) self.assertTrue( (d == cf.ge(cf.dt(2001, 1, 3, 21, 0, 0))).equals( cf.Data([[False, True], [True, False]])), message, ) self.assertTrue( (d == cf.gt(cf.dt(2001, 1, 3, 21, 0, 0))).equals( cf.Data([[False, False], [True, False]])), message, ) self.assertTrue( (d == cf.le(cf.dt(2001, 1, 3, 21, 0, 0))).equals( cf.Data([[True, True], [False, True]])), message, ) self.assertTrue( (d == cf.lt(cf.dt(2001, 1, 3, 21, 0, 0))).equals( cf.Data([[True, False], [False, True]])), message, ) d = cf.dt(2002, 6, 16) self.assertEqual(cf.eq(cf.dt(2002, 6, 16)), d) self.assertNotEqual(cf.eq(cf.dt(1990, 1, 1)), d) self.assertNotEqual(cf.eq(cf.dt("2100-1-1")), d) self.assertNotEqual( cf.eq(cf.dt("2001-1-1")) & cf.eq(cf.dt(2010, 12, 31)), d) d = cf.dt(2002, 6, 16) self.assertEqual(cf.ge(cf.dt(1990, 1, 1)), d) self.assertEqual(cf.ge(cf.dt(2002, 6, 16)), d) self.assertNotEqual(cf.ge(cf.dt("2100-1-1")), d) self.assertNotEqual( cf.ge(cf.dt("2001-1-1")) & cf.ge(cf.dt(2010, 12, 31)), d) d = cf.dt(2002, 6, 16) self.assertEqual(cf.gt(cf.dt(1990, 1, 1)), d) self.assertNotEqual(cf.gt(cf.dt(2002, 6, 16)), d) self.assertNotEqual(cf.gt(cf.dt("2100-1-1")), d) self.assertEqual( cf.gt(cf.dt("2001-1-1")) & cf.le(cf.dt(2010, 12, 31)), d) d = cf.dt(2002, 6, 16) self.assertEqual(cf.ne(cf.dt(1990, 1, 1)), d) self.assertNotEqual(cf.ne(cf.dt(2002, 6, 16)), d) self.assertEqual(cf.ne(cf.dt("2100-1-1")), d) self.assertEqual( cf.ne(cf.dt("2001-1-1")) & cf.ne(cf.dt(2010, 12, 31)), d) d = cf.dt(2002, 6, 16) self.assertNotEqual(cf.le(cf.dt(1990, 1, 1)), d) self.assertEqual(cf.le(cf.dt(2002, 6, 16)), d) self.assertEqual(cf.le(cf.dt("2100-1-1")), d) self.assertNotEqual( cf.le(cf.dt("2001-1-1")) & cf.le(cf.dt(2010, 12, 31)), d) d = cf.dt(2002, 6, 16) self.assertNotEqual(cf.lt(cf.dt(1990, 1, 1)), d) self.assertNotEqual(cf.lt(cf.dt(2002, 6, 16)), d) self.assertEqual(cf.lt(cf.dt("2100-1-1")), d) self.assertNotEqual( cf.lt(cf.dt("2001-1-1")) & cf.lt(cf.dt(2010, 12, 31)), d)