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()
b = a.collapse('area: mean', weights='area') print(b) b = a.collapse('area: mean', weights='area').collapse('T: maximum') print(b) print(b.array) b = a.collapse('area: mean T: maximum', weights='area') print(b.array) y = cf.Y(month=12) y b = a.collapse('T: maximum', group=y) print(b) b = a.collapse('T: maximum', group=6) print(b) b = a.collapse('T: maximum', group=cf.djf()) print(b) c = cf.seasons() c b = a.collapse('T: maximum', group=c) print(b) b = a.collapse('X: mean', group=cf.Data(180, 'degrees')) print(b) b = a.collapse('T: mean within years T: mean over years', within_years=cf.seasons(), weights='T') print(b) print(b.coordinate('T').bounds.datetime_array) b = a.collapse('T: minimum within years T: variance over years', within_years=cf.seasons(), weights='T') print(b) print(b.coordinate('T').bounds.datetime_array)
b = a.collapse('area: mean', weights='area') print(b) b = a.collapse('area: mean', weights='area').collapse('T: maximum') print(b) print(b.array) b = a.collapse('area: mean T: maximum', weights='area') print(b.array) y = cf.Y(month=12) y b = a.collapse('T: maximum', group=y) print(b) b = a.collapse('T: maximum', group=6) print(b) b = a.collapse('T: maximum', group=cf.djf()) print(b) c = cf.seasons() c b = a.collapse('T: maximum', group=c) print(b) b = a.collapse('X: mean', group=cf.Data(180, 'degrees')) print(b) b = a.collapse('T: mean within years T: mean over years', within_years=cf.seasons(), weights='T') print(b) print(b.coordinate('T').bounds.datetime_array) b = a.collapse('T: minimum within years T: variance over years', within_years=cf.seasons(), weights='T') print(b) print(b.coordinate('T').bounds.datetime_array) b = a.collapse('T: mean within years T: mean over years', weights='T', within_years=cf.seasons(), over_years=cf.Y(5))
def test_Field_collapse_GROUPS(self): if self.test_only and inspect.stack()[0][3] not in self.test_only: return verbose = False f = cf.example_field(2) g = f.collapse("T: mean", group=cf.M(12), group_span=cf.Y()) expected_shape = list(f.shape) expected_shape[0] = 2 if verbose: print(f) print(g) print(g.constructs) self.assertEqual(list(g.shape), expected_shape, g.shape) g = f.collapse("T: mean", group=cf.M(12, month=12), group_span=cf.Y()) expected_shape = list(f.shape) expected_shape[0] = 3 if verbose: print(f) print(g) print(g.constructs) self.assertEqual(list(g.shape), expected_shape, g.shape) g = f.collapse("T: mean", group=cf.M(12, day=16), group_span=cf.Y()) expected_shape = list(f.shape) expected_shape[0] = 2 if verbose: print(f) print(g) print(g.constructs) self.assertEqual(list(g.shape), expected_shape, g.shape) g = f.collapse( "T: mean", group=cf.M(12, month=11, day=27), group_span=cf.Y() ) expected_shape = list(f.shape) expected_shape[0] = 3 if verbose: print(f) print(g) print(g.constructs) self.assertEqual(list(g.shape), expected_shape, g.shape) g = f.collapse( "T: mean", group=cf.M(12, month=6, day=27), group_span=cf.Y() ) expected_shape = list(f.shape) expected_shape[0] = 2 if verbose: print(f) print(g) print( g.dimension_coordinates("T").value().bounds.data.datetime_array ) print(g.constructs) self.assertEqual(list(g.shape), expected_shape, g.shape) g = f.collapse( "T: mean", group=cf.M(5, month=12), group_span=cf.M(5), group_contiguous=1, ) expected_shape = list(f.shape) expected_shape[0] = 7 if verbose: print(f) print(g) print( g.dimension_coordinates("T").value().bounds.data.datetime_array ) print(g.constructs) self.assertEqual(list(g.shape), expected_shape, g.shape) g = f.collapse( "T: mean", group=cf.M(5, month=12), group_span=cf.M(5), group_contiguous=1, ) expected_shape = list(f.shape) expected_shape[0] = 7 if verbose: print(f) print(g) print( g.dimension_coordinates("T").value().bounds.data.datetime_array ) print(g.constructs) self.assertEqual(list(g.shape), expected_shape, g.shape) g = f.collapse( "T: mean", group=cf.M(5, month=3), group_span=cf.M(5), group_contiguous=1, ) expected_shape = list(f.shape) expected_shape[0] = 7 if verbose: print(f) print(g) print( g.dimension_coordinates("T").value().bounds.data.datetime_array ) print(g.constructs) self.assertEqual(list(g.shape), expected_shape, g.shape) g = f.collapse( "T: mean", group=cf.M(5, month=2), group_span=cf.M(5), group_contiguous=1, ) expected_shape = list(f.shape) expected_shape[0] = 7 if verbose: print(f) print(g) print( g.dimension_coordinates("T").value().bounds.data.datetime_array ) print(g.constructs) self.assertEqual(list(g.shape), expected_shape, g.shape) g = f.collapse( "T: mean", group=cf.M(5, month=12), group_span=cf.M(5), group_contiguous=2, ) expected_shape = list(f.shape) expected_shape[0] = 7 if verbose: print(f) print(g) print( g.dimension_coordinates("T").value().bounds.data.datetime_array ) print(g.constructs) self.assertEqual(list(g.shape), expected_shape, g.shape) g = f.collapse("T: mean", group=cf.M(5, month=3)) expected_shape = list(f.shape) expected_shape[0] = 7 if verbose: print(f) print(g) print( g.dimension_coordinates("T").value().bounds.data.datetime_array ) print(g.constructs) self.assertEqual(list(g.shape), expected_shape, g.shape) # TODO - look into month offset when M< 12 g = f.collapse( "T: mean", group=cf.M(5, month=3), group_span=cf.M(5), group_contiguous=2, ) expected_shape = list(f.shape) expected_shape[0] = 7 if verbose: print(f) print(g) print( g.dimension_coordinates("T").value().bounds.data.datetime_array ) print(g.constructs) self.assertEqual(list(g.shape), expected_shape, g.shape) g = f.collapse("T: mean", group=cf.M(5, month=12), group_contiguous=1) expected_shape = list(f.shape) expected_shape[0] = 7 if verbose: print(f) print(g) print( g.dimension_coordinates("T").value().bounds.data.datetime_array ) print(g.constructs) self.assertEqual(list(g.shape), expected_shape, g.shape) g = f.collapse("T: mean", group=cf.M(5, month=3), group_contiguous=1) expected_shape = list(f.shape) expected_shape[0] = 7 if verbose: print(f) print(g) print( g.dimension_coordinates("T").value().bounds.data.datetime_array ) print(g.constructs) self.assertEqual(list(g.shape), expected_shape, g.shape) g = f.collapse("T: mean", group=cf.M(5, month=12), group_contiguous=2) expected_shape = list(f.shape) expected_shape[0] = 7 if verbose: print(f) print(g) print( g.dimension_coordinates("T").value().bounds.data.datetime_array ) print(g.constructs) self.assertEqual(list(g.shape), expected_shape, g.shape) # Test method=integral with groups g = f.collapse( "T: integral", group=cf.M(5, month=12), weights=True, measure=True ) expected_shape = list(f.shape) expected_shape[0] = 7 self.assertEqual(list(g.shape), expected_shape, g.shape) g = f.collapse("T: mean", group=cf.M(5, month=3), group_contiguous=2) expected_shape = list(f.shape) expected_shape[0] = 7 if verbose: print(f) print(g) print( g.dimension_coordinates("T").value().bounds.data.datetime_array ) print(g.constructs) self.assertEqual(list(g.shape), expected_shape, g.shape) g = f.collapse( "T: mean within years time: minimum over years", within_years=cf.M(3), group_span=True, ) expected_shape = list(f.shape) expected_shape[0] = 4 if verbose: print(f) print(g) print( g.dimension_coordinates("T").value().bounds.data.datetime_array ) print(g.constructs) self.assertEqual(list(g.shape), expected_shape, g.shape) g = f.collapse( "T: mean within years time: minimum over years", within_years=cf.seasons(), group_span=cf.M(3), ) expected_shape = list(f.shape) expected_shape[0] = 4 if verbose: print(f) print(g) print( g.dimension_coordinates("T").value().bounds.data.datetime_array ) print(g.constructs) self.assertEqual(list(g.shape), expected_shape, g.shape)
def test_Field_collapse_CLIMATOLOGICAL_TIME(self): if self.test_only and inspect.stack()[0][3] not in self.test_only: return verbose = False f = cf.example_field(2) g = f.collapse( "T: mean within years time: minimum over years", within_years=cf.seasons(), ) expected_shape = list(f.shape) expected_shape[0] = 4 if verbose: print("\n", f) print(g) print(g.constructs) self.assertEqual(list(g.shape), expected_shape, g.shape) g = f.collapse( "T: max within years time: minimum over years", within_years=cf.seasons(), ) expected_shape = list(f.shape) expected_shape[0] = 4 if verbose: print("\n", f) print(g) print(g.constructs) self.assertEqual(list(g.shape), expected_shape) g = f.collapse( "T: mean within years time: minimum over years", within_years=cf.M(), ) expected_shape = list(f.shape) expected_shape[0] = 12 if verbose: print("\n", f) print(g) print(g.constructs) self.assertEqual(list(g.shape), expected_shape) g = f.collapse( "T: max within years time: minimum over years", within_years=cf.M() ) expected_shape = list(f.shape) expected_shape[0] = 12 if verbose: print("\n", f) print(g) print(g.constructs) self.assertEqual(list(g.shape), expected_shape) g = f[:12].collapse( "T: mean within years time: minimum over years", within_years=cf.seasons(), ) expected_shape = list(f.shape) expected_shape[0] = 4 if verbose: print("\n", f[:12]) print(g) print(g.constructs) self.assertEqual(list(g.shape), expected_shape) g = f[:12].collapse( "T: max within years time: minimum over years", within_years=cf.seasons(), ) expected_shape = list(f.shape) expected_shape[0] = 4 if verbose: print("\n", f[:12]) print(g) print(g.constructs) self.assertEqual(list(g.shape), expected_shape) g = f[:12].collapse( "T: mean within years time: minimum over years", within_years=cf.M(), ) expected_shape = list(f.shape) expected_shape[0] = 12 if verbose: print("\n", f[:12]) print(g) print(g.constructs) self.assertEqual(list(g.shape), expected_shape) g = f[:12].collapse( "T: max within years time: minimum over years", within_years=cf.M() ) expected_shape = list(f.shape) expected_shape[0] = 12 if verbose: print("\n", f[:12]) print(g) print(g.constructs) self.assertEqual(list(g.shape), expected_shape) for key in f.cell_methods(todict=True): f.del_construct(key) g = f.collapse( "T: max within years time: minimum over years", within_years=cf.seasons(), ) expected_shape = list(f.shape) expected_shape[0] = 4 if verbose: print("\n", f) print(g) print(g.constructs) self.assertEqual(list(g.shape), expected_shape) g = f.collapse( "T: max within years time: min over years", within_years=cf.M() ) expected_shape = list(f.shape) expected_shape[0] = 12 if verbose: print("\n", f) print(g) print(g.constructs) self.assertEqual(list(g.shape), expected_shape) g = f[:12].collapse( "T: max within years time: minimum over years", within_years=cf.seasons(), ) expected_shape = list(f.shape) expected_shape[0] = 4 if verbose: print("\n", f[:12]) print(g) print(g.constructs) self.assertEqual(list(g.shape), expected_shape) g = f[:12].collapse( "T: max within years time: minimum over years", within_years=cf.M() ) expected_shape = list(f.shape) expected_shape[0] = 12 if verbose: print("\n", f[:12]) print(g) print(g.constructs) self.assertEqual(list(g.shape), expected_shape) g = f.collapse( "T: max within years time: minimum over years", within_years=cf.seasons(), over_years=cf.Y(2), ) expected_shape = list(f.shape) expected_shape[0] = 8 if verbose: print("\n", f) print(g) print(g.constructs) self.assertEqual(list(g.shape), expected_shape) g = f[::-1, ...].collapse( "T: max within years time: minimum over years", within_years=cf.seasons(), over_years=cf.Y(2), ) expected_shape = list(f.shape) expected_shape[0] = 8 if verbose: print("\n", f[::-1, ...]) print(g) print(g.constructs) self.assertEqual(list(g.shape), expected_shape)