示例#1
0
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)
示例#2
0
def test_tfdataset_grib_4():
    s = cml.load_source(
        "multi",
        cml.load_source("file", climetlab_file("docs/examples/test.grib")),
        cml.load_source("file", climetlab_file("docs/examples/test.grib")),
    )
    dataset = s.to_tfdataset(label="paramId")
    for r in dataset:
        print(len(r), [type(x) for x in r])
示例#3
0
def test_normalize_dates_from_source():

    dates_3 = normalize("d", "date")(f)
    dates_list_3 = normalize("d", "date", multiple=True)(f)

    source = load_source("file", climetlab_file("docs/examples/test.grib"))
    assert dates_3(source[0]) == datetime.datetime(2020, 5, 13, 12, 0)
    assert dates_list_3(source[0]) == [datetime.datetime(2020, 5, 13, 12, 0)]

    source = load_source("file", climetlab_file("docs/examples/test.nc"))

    #  For now
    with pytest.raises(NotImplementedError):
        assert dates_3(source[0]) == datetime.datetime(2020, 5, 13, 12, 0)
        assert dates_list_3(
            source[0]) == [datetime.datetime(2020, 5, 13, 12, 0)]
示例#4
0
def test_dates():
    npdate = np.datetime64("2016-01-01")
    assert dates_1(date=npdate) == datetime.datetime(2016, 1, 1)
    assert dates_list_1(date=npdate) == [datetime.datetime(2016, 1, 1)]

    source = load_source("file", climetlab_file("docs/examples/test.grib"))
    assert dates_1(source[0]) == datetime.datetime(2020, 5, 13, 12, 0)
    assert dates_list_1(source[0]) == [datetime.datetime(2020, 5, 13, 12, 0)]

    source = load_source("file", climetlab_file("docs/examples/test.nc"))

    #  For now
    with pytest.raises(NotImplementedError):
        assert dates_1(source[0]) == datetime.datetime(2020, 5, 13, 12, 0)
        assert dates_list_1(
            source[0]) == [datetime.datetime(2020, 5, 13, 12, 0)]
示例#5
0
def test_plot():
    for s in load_source("file", climetlab_file("docs/examples/test.grib")):
        plot_map(s)

        # test.grib fields endStep is 0, so datetime == valid_datetime
        assert s.datetime() == s.valid_datetime()

        # test shape
        assert s.shape == (11, 19)
示例#6
0
def test_glob():
    s = load_source("file", climetlab_file("docs/examples/test.grib"))
    with temp_directory() as tmpdir:
        s.save(os.path.join(tmpdir, "a.grib"))
        s.save(os.path.join(tmpdir, "b.grib"))

        s = load_source("file", os.path.join(tmpdir, "*.grib"))
        assert len(s) == 4, len(s)

        s = load_source("file", tmpdir)
        assert len(s) == 4, len(s)
示例#7
0
def test_user_2():
    s = load_source("file", climetlab_file("docs/examples/test.grib"))
    home_file = os.path.expanduser("~/.climetlab/test.grib")
    try:
        s.save(home_file)
        # Test expand vars
        s = load_source("file", "$HOME/.climetlab/test.grib", expand_vars=True)
        assert len(s) == 2
    finally:
        try:
            os.unlink(home_file)
        except OSError:
            LOG.exception("unlink(%s)", home_file)
示例#8
0
def test_plot_1():
    s = cml.load_source("file", climetlab_file("docs/examples/test.grib"))
    assert cml.plot_map(s) is None

    assert cml.plot_map(s[0]) is None

    p = cml.new_plot()
    p.plot_map(s[0])
    p.plot_map(s[1])

    assert p.show() is None

    with pytest.raises(TypeError):
        cml.plot_map(s, unknown=42)
示例#9
0
def test_tfdataset_grib_1():
    s = cml.load_source("file", climetlab_file("docs/examples/test.grib"))
    dataset = s.to_tfdataset()

    # First pass
    cnt = 0
    for _ in dataset:
        cnt += 1
    assert cnt == 2

    # Second pass
    cnt = 0
    for _ in dataset:
        cnt += 1
    assert cnt == 2
示例#10
0
def test_url_part_file_source():
    filename = os.path.abspath(climetlab_file("docs/examples/test.grib"))
    ds = load_source(
        "url",
        f"file://{filename}",
        parts=[
            (0, 4),
            (522, 4),
            (526, 4),
            (1048, 4),
        ],
    )

    assert os.path.getsize(ds.path) == 16

    with open(ds.path, "rb") as f:
        assert f.read() == b"GRIB7777GRIB7777"
示例#11
0
def test_datetime():

    s = load_source("file", climetlab_file("docs/examples/test.grib"))

    assert s.to_datetime() == datetime.datetime(2020, 5, 13,
                                                12), s.to_datetime()

    assert s.to_datetime_list() == [datetime.datetime(2020, 5, 13, 12)
                                    ], s.to_datetime_list()

    s = load_source(
        "dummy-source",
        kind="grib",
        paramId=[129, 130],
        date=[19900101, 19900102],
        level=[1000, 500],
    )
    assert s.to_datetime_list() == [
        datetime.datetime(1990, 1, 1, 12, 0),
        datetime.datetime(1990, 1, 2, 12, 0),
    ], s.to_datetime_list()
示例#12
0
import os

import pytest

from climetlab.testing import IN_GITHUB, climetlab_file

IGNORE = [
    "conf.py",
    "xml2rst.py",
    "actions.py",
    "generate-examples-maps.py",
    "settings-2-set.py",
]

EXAMPLES = climetlab_file("docs", "examples")


def example_list():

    # return []

    examples = []
    for root, _, files in os.walk(EXAMPLES):
        for file in files:
            path = os.path.join(root, file)
            if path.endswith(".py") and file not in IGNORE:
                n = len(EXAMPLES) + 1
                examples.append(path[n:])

    return sorted(examples)
示例#13
0
def test_bbox():
    s = load_source("file", climetlab_file("docs/examples/test.grib"))
    assert s.to_bounding_box().as_tuple() == (73, -27, 33,
                                              45), s.to_bounding_box()
示例#14
0
def test_numpy_xarray():
    s = cml.load_source("file", climetlab_file("docs/examples/test.grib"))
    x = s.to_xarray()
    cml.plot_map(x.msl.values, metadata=x.msl)
示例#15
0
def test_tfdataset_grib_3():
    s = cml.load_source("file", climetlab_file("docs/examples/test.grib"))
    dataset = s.to_tfdataset(label="paramId")
    for data, paramId in dataset:
        LOG.debug("Shape %s, param %s", data.shape, paramId)
示例#16
0
def test_sel():
    s = load_source("file", climetlab_file("docs/examples/test.grib"))

    s.sel(shortName="2t")
示例#17
0
def test_tfdataset_grib_2():
    s = cml.load_source("file", climetlab_file("docs/examples/test.grib"))
    dataset = s.to_tfdataset(dtype="float64")
    for _ in dataset:
        pass
示例#18
0
def test_to_datetimes_list_grib():
    source = load_source("file", climetlab_file("docs/examples/test.grib"))
    for s in source:
        assert to_datetime_list(s) == [datetime.datetime(2020, 5, 13, 12, 0)]
示例#19
0
import os

import pytest

from climetlab.testing import IN_GITHUB, climetlab_file

IGNORE = [
    "conf.py",
    "xml2rst.py",
    "actions.py",
    "generate-examples-maps.py",
    "settings-2-set.py",
]

EXAMPLES = climetlab_file("docs")


def example_list():

    # return []

    examples = []
    for root, _, files in os.walk(EXAMPLES):
        for file in files:
            path = os.path.join(root, file)
            if path.endswith(".py") and file not in IGNORE:
                n = len(EXAMPLES) + 1
                examples.append(path[n:])

    return sorted(examples)
示例#20
0
def test_metview_netcdf():
    s = cml.load_source("file", climetlab_file("docs/examples/test.nc"))
    fs = s.to_metview()

    assert fs.url() == s.path
示例#21
0
def test_netcdf():
    for s in load_source("file", climetlab_file("docs/examples/test.nc")):
        plot_map(s)
示例#22
0
def test_url_file_source():
    filename = os.path.abspath(climetlab_file("docs/examples/test.nc"))
    s = load_source("url", f"file://{filename}")
    assert len(s) == 2
示例#23
0
def test_file_source_netcdf():
    s = load_source("file", climetlab_file("docs/examples/test.nc"))
    assert len(s) == 2
示例#24
0
def test_file_source_grib():

    s = load_source("file", climetlab_file("docs/examples/test.grib"))
    assert len(s) == 2