Exemple #1
0
def test_fieldset_merge():
    g = mv.Fieldset(path=os.path.join(PATH, "tuv_pl.grib"))
    h = mv.Fieldset(path=os.path.join(PATH, "all_missing_vals.grib"))
    i = g[0:3]
    j = i.merge(h)  # does not alter the original fieldset
    assert i.grib_get_string("shortName") == ["t", "u", "v"]
    assert j.grib_get_string("shortName") == ["t", "u", "v", "z"]
Exemple #2
0
def test_average():
    fs = mv.Fieldset(path=os.path.join(PATH, "test.grib"))

    # const fields
    v = mv.average(fs * 0 + 1)
    assert isinstance(v, float)
    assert np.isclose(v, 1)

    # # single field
    v = mv.average(fs)
    assert isinstance(v, float)
    assert np.isclose(v, 279.06647863)

    # multiple fields
    f = mv.Fieldset(path=os.path.join(PATH, "monthly_avg.grib"))
    v = mv.average(f)
    assert isinstance(v, list)
    v_ref = [
        290.639783636,
        294.654600877,
        306.688947846,
        305.515656561,
        300.804574428,
        297.732210991,
        291.848360371,
        286.85312407,
    ]

    assert len(v) == len(f)
    np.testing.assert_allclose(v, v_ref)
Exemple #3
0
def test_fieldset_fieldset_func():
    f = mv.Fieldset(path=os.path.join(PATH, "tuv_pl.grib"))[0:3]
    g = mv.Fieldset(path=os.path.join(PATH, "tuv_pl.grib"))[5:8]
    h = f + g
    np.testing.assert_allclose(h.values(), f.values() + g.values())
    i = g + f
    np.testing.assert_allclose(i.values(), g.values() + f.values())
    q = f - g
    np.testing.assert_allclose(q.values(), f.values() - g.values())
    r = g - f
    np.testing.assert_allclose(r.values(), g.values() - f.values())
    t = g * f
    np.testing.assert_allclose(t.values(), g.values() * f.values(), 0.0001)
    d = g / f
    np.testing.assert_allclose(d.values(), g.values() / f.values(), 0.0001)
    gt = f > g
    assert gt[0].values()[0] == 1
    assert gt[1].values()[0] == 0
    assert gt[2].values()[0] == 1
    assert gt[2].values()[22] == 0
    gt = f >= g
    assert gt[0].values()[0] == 1
    assert gt[1].values()[0] == 0
    assert gt[2].values()[0] == 1
    assert gt[2].values()[22] == 0
    lt = f < g
    assert lt[0].values()[0] == 0
    assert lt[1].values()[0] == 1
    assert lt[2].values()[0] == 0
    assert lt[2].values()[22] == 1
    lt = f <= g
    assert lt[0].values()[0] == 0
    assert lt[1].values()[0] == 1
    assert lt[2].values()[0] == 0
    assert lt[2].values()[22] == 1
Exemple #4
0
def test_write_fieldset():
    f = mv.Fieldset(path=os.path.join(PATH, "tuv_pl.grib"))
    temp_path = "written_tuv_pl.grib"
    f.write(temp_path)
    assert os.path.isfile(temp_path)
    g = mv.Fieldset(path=temp_path)
    assert type(g) == mv.Fieldset
    assert len(g) == 18
    sn = g.grib_get_string("shortName")
    assert sn == ["t", "u", "v"] * 6
    f = 0
    os.remove(temp_path)
Exemple #5
0
def test_write_modified_fieldset_unop():
    f = mv.Fieldset(path=os.path.join(PATH, "tuv_pl.grib"))
    negf = -f
    temp_path = "written_tuv_pl_unop.grib"
    negf.write(temp_path)
    assert os.path.isfile(temp_path)
    g = mv.Fieldset(path=temp_path)
    assert type(g) == mv.Fieldset
    assert len(g) == 18
    sn = g.grib_get_string("shortName")
    assert sn == ["t", "u", "v"] * 6
    np.testing.assert_allclose(g.values(), -f.values(), 0.0001)
    f = 0
    os.remove(temp_path)
Exemple #6
0
def test_set_values_single_field():
    f = mv.Fieldset(path=os.path.join(PATH, "tuv_pl.grib"))
    f0 = f[0]
    f0_vals = f0.values()
    vals_plus_10 = f0_vals + 10
    f0_modified = f0.set_values(vals_plus_10)
    f0_mod_vals = f0_modified.values()
    np.testing.assert_allclose(f0_mod_vals, vals_plus_10)
    # write to disk, read and check again
    testpath = "f0_modified.grib"
    f0_modified.write(testpath)
    f0_read = mv.Fieldset(path=testpath)
    np.testing.assert_allclose(f0_read.values(), vals_plus_10)
    os.remove(testpath)
Exemple #7
0
def test_grib_index_3():
    # merged fields from different files
    gp1 = os.path.join(PATH, "tuv_pl.grib")
    gp2 = os.path.join(PATH, "t_time_series.grib")
    fs1 = mv.Fieldset(path=gp1)
    fs2 = mv.Fieldset(path=gp2)
    fs3 = fs1[4:7]
    fs3.append(fs2[1])
    fs3.append(fs1[2])
    gi = fs3.grib_index()
    assert isinstance(gi, list)
    assert len(gi) == 5
    # assert gi == [(gp1, 5760), (gp1, 7200), (gp1, 8640), (gp2, 5520), (gp1, 2880)]
    assert gi == [(gp1, 5760), (gp1, 7200), (gp1, 8640), (gp2, 5436),
                  (gp1, 2880)]
Exemple #8
0
def test_fieldset_create_from_glob_paths():
    f = mv.Fieldset(
        path=[os.path.join(PATH, "test.g*ib"),
              os.path.join(PATH, "t_*.grib")])
    assert type(f) == mv.Fieldset
    assert len(f) == 18
    par_ref = [
        ["2t", "0"],
        ["t", "1000"],
        ["t", "850"],
        ["t", "700"],
        ["t", "500"],
        ["t", "400"],
        ["t", "300"],
        ["t", "1000"],
        ["z", "1000"],
        ["t", "1000"],
        ["z", "1000"],
        ["t", "1000"],
        ["z", "1000"],
        ["t", "1000"],
        ["z", "1000"],
        ["t", "1000"],
        ["z", "1000"],
        ["t", "1000"],
    ]
    assert par_ref == f.grib_get(["shortName", "level"])
Exemple #9
0
def test_date():

    fs = mv.Fieldset(path=os.path.join(PATH, "monthly_avg.grib"))

    # analysis, so valid=base
    bdate_ref = [
        "2016-01-01 00:00:00",
        "2016-02-01 00:00:00",
        "2016-03-01 00:00:00",
        "2016-04-01 00:00:00",
        "2016-05-01 00:00:00",
        "2016-06-01 00:00:00",
        "2016-07-01 00:00:00",
        "2016-08-01 00:00:00",
    ]
    vdate_ref = bdate_ref

    v = mv.base_date(fs)
    assert len(v) == len(fs)
    for i, d in enumerate(v):
        assert d == utils.date_from_str(bdate_ref[i])

    v = mv.valid_date(fs)
    assert len(v) == len(fs)
    for i, d in enumerate(v):
        assert d == utils.date_from_str(vdate_ref[i])
Exemple #10
0
def test_tanlat():
    fs = mv.Fieldset(path=os.path.join(PATH, "t_time_series.grib"))

    # WARN: it is important that the data should be at least 16 bit
    #  to keep accuracy in resulting fields

    # TODO: use pole_limit value from fieldset

    pole_limit = 90.0 - 1e-06

    f = fs[0]
    r = mv.tanlat(f)
    lat = f.latitudes()
    lat[np.fabs(lat) > pole_limit] = np.nan
    np.testing.assert_allclose(r.values(),
                               np.tan(np.deg2rad(lat)),
                               rtol=1e-06,
                               atol=1e-06)

    f = fs[:2]
    r = mv.tanlat(f)
    assert len(r) == 2
    for i in range(len(r)):
        lat = f[i].latitudes()
        lat[np.fabs(lat) > pole_limit] = np.nan
        np.testing.assert_allclose(r[i].values(),
                                   np.tan(np.deg2rad(lat)),
                                   rtol=1e-06,
                                   atol=1e-06)
Exemple #11
0
def test_grib_set_generic():
    f = mv.Fieldset(path=os.path.join(PATH, "tuv_pl.grib"))[0:2]
    g = f.grib_set(["shortName", "r"])
    assert g.grib_get_string("shortName") == ["r"] * 2
    assert f.grib_get_string("shortName") == ["t", "u"]
    g = f.grib_set(["shortName:s", "q"])
    assert g.grib_get_string("shortName") == ["q"] * 2
    assert f.grib_get_string("shortName") == ["t", "u"]

    g = f.grib_set(["level:l", 500, "shortName", "z"])
    assert g.grib_get_long("level") == [500] * 2
    assert g.grib_get_string("shortName") == ["z"] * 2
    assert f.grib_get_long("level") == [1000] * 2
    assert f.grib_get_string("shortName") == ["t", "u"]

    g = f.grib_set(["level:d", 500])
    np.testing.assert_allclose(np.array(g.grib_get_double("level")),
                               np.array([500] * 2))
    np.testing.assert_allclose(np.array(f.grib_get_double("level")),
                               np.array([1000] * 2))

    g = f.grib_set_double(["longitudeOfFirstGridPointInDegrees", 95.6])
    np.testing.assert_allclose(
        np.array(g.grib_get_double("longitudeOfFirstGridPointInDegrees")),
        np.array([95.6] * 2),
    )
    np.testing.assert_allclose(
        np.array(f.grib_get_double("longitudeOfFirstGridPointInDegrees")),
        [0, 0])
Exemple #12
0
def test_field_maths_funcs():
    f = mv.Fieldset(path=os.path.join(PATH, "tuv_pl.grib"))
    f = f[0]
    v = f.values()

    # no arg
    r = f.abs()
    np.testing.assert_allclose(r.values(), np.fabs(v), rtol=1e-05)

    r = f.cos()
    np.testing.assert_allclose(r.values(), np.cos(v), rtol=1e-05)

    f1 = f / 100
    r = f1.exp()
    np.testing.assert_allclose(r.values(), np.exp(f1.values()), rtol=1e-05)

    r = f.log()
    np.testing.assert_allclose(r.values(), np.log(v), rtol=1e-05)

    r = f.log10()
    np.testing.assert_allclose(r.values(), np.log10(v), rtol=1e-05)

    r = f.sgn()
    np.testing.assert_allclose(r.values(), np.sign(v), rtol=1e-05)

    r = f.sin()
    np.testing.assert_allclose(r.values(), np.sin(v), rtol=1e-05)

    r = f.sqrt()
    np.testing.assert_allclose(r.values(), np.sqrt(v), rtol=1e-05)

    r = f.tan()
    np.testing.assert_allclose(r.values(), np.tan(v), rtol=1e-04)

    # inverse functions
    # scale input between [-1, 1]
    f1 = (f - 282) / 80
    v1 = f1.values()
    r = f1.acos()
    np.testing.assert_allclose(r.values(), np.arccos(v1), rtol=1e-05)

    r = f1.asin()
    np.testing.assert_allclose(r.values(), np.arcsin(v1), rtol=1e-05)

    r = f1.atan()
    np.testing.assert_allclose(r.values(), np.arctan(v1), rtol=1e-05)

    # 1 arg
    f1 = f - 274
    v1 = f1.values()

    r = f.atan2(f1)
    np.testing.assert_allclose(r.values(), np.arctan2(v, v1), rtol=1e-05)

    r = f.div(f1)
    np.testing.assert_allclose(r.values(), np.floor_divide(v, v1), rtol=1e-05)

    r = f.mod(f1)
    np.testing.assert_allclose(r.values(), np.mod(v, v1), rtol=1e-04)
Exemple #13
0
def test_field_func_pos():
    f = mv.Fieldset(path=os.path.join(PATH, "tuv_pl.grib"))
    g = +f  # should return values unaltered
    assert type(g) == mv.Fieldset
    assert len(g) == 18
    vf = f.values()
    vg = g.values()
    np.testing.assert_allclose(vg, vf)
Exemple #14
0
def test_temporary_file():
    # create a temp file, then delete the fieldset - temp should be removed
    f = mv.Fieldset(path=os.path.join(PATH, "tuv_pl.grib"))
    g = -f
    temp_path = g.temporary.path
    assert os.path.isfile(temp_path)
    g = None
    assert not os.path.isfile(temp_path)
Exemple #15
0
def test_field_func_abs():
    f = mv.Fieldset(path=os.path.join(PATH, "tuv_pl.grib"))
    g = f.abs()
    assert type(g) == mv.Fieldset
    assert len(g) == 18
    vf = f.values()
    vg = g.values()
    np.testing.assert_allclose(vg, np.abs(vf))
Exemple #16
0
def test_values_1():
    f = mv.Fieldset(path=os.path.join(PATH, "test.grib"))
    v = f.values()
    assert isinstance(v, np.ndarray)
    assert len(v) == 115680
    assert np.isclose(v[0], 260.4356)
    assert np.isclose(v[24226], 276.1856)
    assert np.isclose(v[36169], 287.9356)
    assert np.isclose(v[115679], 227.1856)
Exemple #17
0
def test_grib_index_4():
    # test with a derived fieldset
    fs = mv.Fieldset(os.path.join(PATH, "t_time_series.grib"))[0:4]
    fs1 = fs + 1
    gi = fs1.grib_index()
    for i in gi:
        assert is_temp_file(i[0])
    offsets = [i[1] for i in gi]
    assert offsets == [0, 8440, 16880, 25320]
Exemple #18
0
def test_minvalue():
    fs = mv.Fieldset(path=os.path.join(PATH, "test.grib"))

    f = 3 * fs
    f = f.merge(fs)
    f = f.merge(2 * fs)
    v = mv.minvalue(f)
    assert isinstance(v, float)
    assert np.isclose(v, 206.93560791015625)
Exemple #19
0
def test_maxvalue():
    fs = mv.Fieldset(path=os.path.join(PATH, "test.grib"))

    f = fs
    f = f.merge(3 * fs)
    f = f.merge(2 * fs)
    v = mv.maxvalue(f)
    assert isinstance(v, float)
    assert np.isclose(v, 948.1818237304688)
Exemple #20
0
def test_fieldset_create_from_list_of_paths():
    paths = [
        os.path.join(PATH, "t_for_xs.grib"),
        os.path.join(PATH, "ml_data.grib")
    ]
    f = mv.Fieldset(path=paths)
    assert len(f) == 42
    assert f[0:2].grib_get_long("level") == [1000, 850]
    assert f[5:9].grib_get_long("level") == [300, 1, 1, 5]
    assert f[40:42].grib_get_long("level") == [133, 137]
Exemple #21
0
def test_grib_set_string():
    f = mv.Fieldset(path=os.path.join(PATH, "tuv_pl.grib"))[0:2]
    g = f.grib_set_string(["pressureUnits", "silly"])
    assert g.grib_get_string("pressureUnits") == ["silly"] * 2
    assert f.grib_get_string("pressureUnits") == ["hPa"] * 2
    g = f.grib_set_string(["pressureUnits", "silly", "shortName", "q"])
    assert g.grib_get_string("pressureUnits") == ["silly"] * 2
    assert g.grib_get_string("shortName") == ["q", "q"]
    assert f.grib_get_string("pressureUnits") == ["hPa"] * 2
    assert f.grib_get_string("shortName") == ["t", "u"]
Exemple #22
0
def test_grib_set_long():
    f = mv.Fieldset(path=os.path.join(PATH, "tuv_pl.grib"))[0:2]
    g = f.grib_set_long(["level", 95])
    assert g.grib_get_long("level") == [95] * 2
    assert f.grib_get_long("level") == [1000] * 2
    g = f.grib_set_long(["level", 95, "time", 1800])
    assert g.grib_get_long("level") == [95] * 2
    assert g.grib_get_long("time") == [1800] * 2
    assert f.grib_get_long("level") == [1000] * 2
    assert f.grib_get_long("time") == [1200] * 2
Exemple #23
0
def test_grib_index_2():
    # multiple fields
    grib_path = os.path.join(PATH, "tuv_pl.grib")
    fs = mv.Fieldset(path=grib_path)
    gi = fs.grib_index()
    assert isinstance(gi, list)
    assert len(gi) == 18
    for f, g in zip(fs, gi):
        assert g == (grib_path, f.grib_get_long("offset"))
    assert gi[5] == (grib_path, 7200)
Exemple #24
0
def test_set_values_with_missing_values_2():
    f = mv.Fieldset(path=os.path.join(PATH, "t_with_missing.grib"))
    g = f[0]
    v = g.values()
    v[1] = np.nan
    h = g.set_values(v)
    hv = h.values()[:10]
    assert np.isclose(hv[0], 272.56417847)
    assert np.isnan(hv[1])
    assert np.isclose(hv[2], 272.56417847)
Exemple #25
0
def test_grib_index_5():
    # test with grib written with write() function
    f_orig = mv.Fieldset(path=os.path.join(PATH, "tuv_pl.grib"))
    f = (f_orig[0:4]).merge(f_orig[7])
    p = "written_tuv_pl.grib"
    f.write(p)
    gi = f.grib_index()
    assert gi == [(p, 0), (p, 1440), (p, 2880), (p, 4320), (p, 5760)]
    f = 0
    os.remove(p)
Exemple #26
0
def test_grib_get_long_array_1():
    f = mv.Fieldset(
        path=os.path.join(PATH, "rgg_small_subarea_cellarea_ref.grib"))
    pl = f.grib_get_long_array("pl")
    assert isinstance(pl, np.ndarray)
    assert len(pl) == 73
    assert pl[0] == 24
    assert pl[1] == 28
    assert pl[20] == 104
    assert pl[72] == 312
Exemple #27
0
def test_fieldset_iterator_multiple():
    grib = mv.Fieldset(path=os.path.join(PATH, "tuv_pl.grib"))
    sn = grib.grib_get_string("shortName")
    assert len(sn) == 18
    for i in [1, 2, 3]:
        iter_sn = []
        for f in grib:
            iter_sn.append(f.grib_get_string("shortName"))
        assert len(iter_sn) == len(sn)
        for i in range(0, 18):
            assert sn[i] == iter_sn[i]
Exemple #28
0
def test_single_index_minus_1():
    path = os.path.join(PATH, "tuv_pl.grib")
    f = mv.Fieldset(path=path)
    fm1 = f[-1]
    assert type(fm1) is mv.Fieldset
    assert len(fm1) == 1
    assert fm1.grib_get_string("shortName") == "v"
    v = fm1.values()
    eps = 0.001
    assert len(v) == 2664
    assert np.isclose(v[2663], -11.0797, eps)
Exemple #29
0
def test_fieldset_iterator():
    grib = mv.Fieldset(path=os.path.join(PATH, "tuv_pl.grib"))
    sn = grib.grib_get_string("shortName")
    assert len(sn) == 18
    iter_sn = []
    for f in grib:
        iter_sn.append(f.grib_get_string("shortName"))
    assert len(iter_sn) == len(sn)
    assert iter_sn == sn
    iter_sn = [f.grib_get_string("shortName") for f in grib]
    assert iter_sn == sn
Exemple #30
0
def test_fieldset_reverse_iterator():
    grib = mv.Fieldset(path=os.path.join(PATH, "tuv_pl.grib"))
    sn = grib.grib_get_string("shortName")
    sn_reversed = list(reversed(sn))
    assert sn_reversed[0] == "v"
    assert sn_reversed[17] == "t"
    gribr = reversed(grib)
    iter_sn = [f.grib_get_string("shortName") for f in gribr]
    assert len(iter_sn) == len(sn_reversed)
    assert iter_sn == sn_reversed
    assert iter_sn == ["v", "u", "t"] * 6