def test_bbox(): area = [30.0, 2.0, 3.0, 4.0] bbox = BoundingBox(north=30, west=2, south=3, east=4) assert bbox_list(None, area) == bbox assert bbox_list(area=area, ignore=None) == bbox assert bbox_tuple(area) == tuple(area) assert bbox_tuple(area=area) == tuple(area) assert bbox_bbox(area) == area assert bbox_dict(area) == dict(north=30, west=2, south=3, east=4) assert bbox_defaults(area) == bbox source = load_source("file", climetlab_file("docs/examples/test.grib")) assert bbox_tuple(source[0]) == (73.0, -27.0, 33.0, 45.0) source = load_source("file", climetlab_file("docs/examples/test.nc")) assert bbox_tuple(source[0]) == (73.0, -27.0, 33.0, 45.0) assert bbox_tuple("france") == (54.5, -6.0, 39.0, 9.5) assert bbox_tuple("verification.france") == (51.5, -5.0, 42.0, 8.5)
def test_types(): assert TypeTransformer(None, type=EnumType(enum)).transform("a") == "a" assert TypeTransformer(None, type=EnumListType(enum)).transform("a") == ["a"] assert TypeTransformer(None, type=StrType).transform(42) == "42" assert TypeTransformer(None, type=StrListType).transform(42) == ["42"] assert TypeTransformer(None, type=IntType).transform("42") == 42 assert TypeTransformer(None, type=IntListType).transform("42") == [42] assert TypeTransformer(None, type=FloatType).transform("3.14") == 3.14 assert TypeTransformer(None, type=FloatListType).transform(3.14) == [3.14] assert TypeTransformer( None, type=DateType).transform(20000101) == datetime.datetime(2000, 1, 1) assert TypeTransformer( None, type=DateListType).transform("20000101/to/20000103") == [ datetime.datetime(2000, 1, 1), datetime.datetime(2000, 1, 2), datetime.datetime(2000, 1, 3), ] assert TypeTransformer(None, type=DateListType).transform( (20000101, 20000102, 20000103)) == [ datetime.datetime(2000, 1, 1), datetime.datetime(2000, 1, 2), datetime.datetime(2000, 1, 3), ] with pytest.raises(AssertionError): # FIXME: Not sure what this should be assert TypeTransformer(None, type=VariableType("cf")).transform(42) == 0 with pytest.raises(AssertionError): # FIXME: Not sure what this should be assert TypeTransformer(None, type=VariableListType("cf")).transform(42) == 0 assert TypeTransformer(None, type=BoundingBoxType).transform( (1, -1, -1, 1)) == BoundingBox(north=1, west=-1, south=-1, east=1)
def xxxtest_overlaps(): b1 = BoundingBox(north=90, west=-200, south=-90, east=-130) b2 = BoundingBox(north=90, west=-180, south=-90, east=-90) b0 = b1.overlaps(b2) assert b0
def test_globe(): globe1 = BoundingBox(north=90, west=0, east=360, south=-90) assert globe1.width == 360 assert globe1.west == 0 globe2 = BoundingBox(north=90, west=-180, east=180, south=-90) assert globe2.width == 360 assert globe2.west == -180 assert globe1.merge(globe2).width == 360, globe1.merge(globe2).width assert globe2.merge(globe1).width == 360, globe2.merge(globe1).width # assert globe1.merge(globe2) == globe1,globe1.merge(globe2) # assert globe2.merge(globe1) == globe2, globe2.merge(globe1) b1 = BoundingBox(north=90, west=-180, east=0, south=-90) b2 = BoundingBox(north=90, west=0, east=180, south=-90) b0 = b1.merge(b2) assert b0.width == 360
def test_overlapping_bbox_1(): for offset in range(-500, 500, 10): one = BoundingBox(north=90, west=offset + 10, east=offset + 20, south=-90) two = BoundingBox(north=90, west=offset + 40, east=offset + 60, south=-90) three = BoundingBox(north=90, west=offset + 15, east=offset + 35, south=-90) sets = [] sets.append([one, two, three]) sets.append([two, one, three]) sets.append([one, three, two]) sets.append([one, three, two, one, three]) sets.append([one, one, one, two, one, two, one, three]) for i, s in enumerate(sets): merged = BoundingBox.multi_merge(s) expected = BoundingBox(east=offset + 60, west=offset + 10, north=90, south=-90) assert merged.east == expected.east, ( i, merged.east, expected.east, merged, s, ) assert merged.west == expected.west, ( i, merged.west, expected.west, merged, s, ) four = BoundingBox(north=90, west=offset - 200, east=offset + 10, south=-90) sets = [] sets.append([one, two, three, four]) sets.append([two, one, four, three]) sets.append([one, three, four, two]) sets.append([one, three, two, four, one, three]) sets.append([one, one, one, two, four, one, two, one, three]) for i, s in enumerate(sets): merged = BoundingBox.multi_merge(s) expected = BoundingBox(east=offset + 60, west=offset - 200, north=90, south=-90) assert merged.east % 360 == expected.east % 360, ( i, offset, merged.east, expected.east, merged, s, ) assert merged.west % 360 == expected.west % 360, ( i, merged.west, expected.west, merged, s, ) assert merged.west < merged.east
def to_bounding_box(self): return BoundingBox.multi_merge([s.to_bounding_box() for s in self])
def plot(): bbox = BoundingBox(north=90, west=0, east=360, south=-90) cml.plot_map(bounding_box=bbox, projection="polar-north")
def test_bbox(): for i in range(-365, 365): bbox = BoundingBox(north=90, west=i, south=30, east=10 + i) assert bbox.width == 10, bbox for i in range(-365, 365): bbox = BoundingBox(north=90, west=i, south=30, east=350 + i) assert bbox.width == 350, bbox for i in range(-365, 365): bbox = BoundingBox(north=90, west=i, south=30, east=(10 + i) % 360) assert bbox.width == 10, bbox for i in range(-365, 365): bbox = BoundingBox(north=90, west=i, south=30, east=(350 + i) % 360) assert bbox.width == 350, bbox for i in range(-365, 365): bbox = BoundingBox(north=90, west=i % 360, south=30, east=10 + i) assert bbox.width == 10, bbox for i in range(-365, 365): bbox = BoundingBox(north=90, west=i % 360, south=30, east=350 + i) assert bbox.width == 350, bbox for i in range(-365, 365): bbox = BoundingBox(north=90, west=i % 360, south=30, east=(10 + i) % 360) assert bbox.width == 10, bbox for i in range(-365, 365): bbox = BoundingBox(north=90, west=i % 360, south=30, east=(350 + i) % 360) assert bbox.width == 350, bbox for i in range(-365, 365): bbox1 = BoundingBox(north=90, west=150 + i, south=30, east=170 + i) bbox2 = BoundingBox(north=90, west=-170 + i, south=30, east=-150 + i) bbox = bbox1.merge(bbox2) assert bbox.width == 60, (bbox1, bbox2, bbox) bbox = bbox2.merge(bbox1) assert bbox.width == 60, (bbox1, bbox2, bbox) with pytest.raises(ValueError): BoundingBox(north=-10, west=0, south=30, east=1) with pytest.raises(ValueError): BoundingBox(north=90, west=1, south=30, east=1)
def test_formats(): assert (FormatTransformer(None, type=EnumType(enum), format="%4s").transform("a") == " a") assert FormatTransformer(None, type=EnumListType(enum), format="%4s").transform( ("a", "b")) == [" a", " b"] assert FormatTransformer(None, type=StrType, format="%4s").transform("a") == " a" assert FormatTransformer(None, type=StrListType, format="%4s").transform( ("a", "b")) == [" a", " b"] assert FormatTransformer(None, type=IntType, format="%04d").transform(42) == "0042" assert FormatTransformer(None, type=IntListType, format="%04d").transform( (42, 43)) == ["0042", "0043"] assert (FormatTransformer(None, type=FloatType, format="%4s").transform("3.14") == "3.14") assert (FormatTransformer(None, type=FloatType, format="%.1f").transform(3.14) == "3.1") assert FormatTransformer(None, type=FloatListType, format="%.1f").transform( (3.14, 2.72)) == ["3.1", "2.7"] assert (FormatTransformer(None, type=DateType, format="%Y").transform( datetime.datetime(2000, 1, 1)) == "2000") assert FormatTransformer(None, type=DateListType, format="%d").transform( (datetime.datetime(2000, 1, 1), datetime.datetime(2000, 1, 2))) == ["01", "02"] with pytest.raises(Exception): # FIXME: Not sure what this should be assert (FormatTransformer(None, type=VariableType, format="%4s").transform(42) == 0) with pytest.raises(Exception): # FIXME: Not sure what this should be assert (FormatTransformer(None, type=VariableListType, format="%4s").transform(42) == 0) with pytest.raises(Exception): # FIXME: Not sure what this should be assert FormatTransformer(None, type=BoundingBoxType, format="%4s").transform( (1, -1, -1, 1)) == BoundingBox(north=1, west=-1, south=-1, east=1) b1 = BoundingBox(north=90, west=-45, south=-90, east=45) assert FormatTransformer(None, type=BoundingBoxType, format=tuple).transform(b1) == ( 90.0, -45.0, -90.0, 45.0, ) assert FormatTransformer(None, type=BoundingBoxType, format="dict").transform(b1) == { "east": 45.0, "north": 90.0, "south": -90.0, "west": -45.0, } assert FormatTransformer(None, type=BoundingBoxType, format=list).transform(b1) == [ 90.0, -45.0, -90.0, 45.0, ]