示例#1
0
def test_codes_write(tmpdir):
    message_id = bindings.codes_new_from_samples('regular_ll_sfc_grib2')
    grib_file = tmpdir.join('test.grib')

    with open(str(grib_file), 'wb') as file:
        bindings.codes_write(message_id, file)

    assert grib_file.read_binary()[:4] == b'GRIB'

    with open(str(grib_file)) as file:
        bindings.codes_grib_new_from_file(file)
示例#2
0
def test_codes_write(tmpdir):
    message_id = bindings.codes_new_from_samples("regular_ll_sfc_grib2")
    grib_file = tmpdir.join("test.grib")

    with open(str(grib_file), "wb") as file:
        bindings.codes_write(message_id, file)

    assert grib_file.read_binary()[:4] == b"GRIB"

    with open(str(grib_file)) as file:
        bindings.codes_grib_new_from_file(file)
示例#3
0
def test_codes_get(key, expected_type, expected_value):
    grib = bindings.codes_grib_new_from_file(open(TEST_DATA))

    result = bindings.codes_get(grib, key)

    assert isinstance(result, expected_type)
    assert result == expected_value
示例#4
0
def test_codes_clone():
    handle = bindings.codes_grib_new_from_file(open(TEST_DATA))

    res = bindings.codes_clone(handle)

    assert isinstance(res, bindings.ffi.CData)
    assert "'grib_handle *'" in repr(res)
示例#5
0
def test_codes_get_length():
    grib = bindings.codes_grib_new_from_file(open(TEST_DATA))

    res = bindings.codes_get_string_length(grib, 'numberOfForecastsInEnsemble')
    assert res == 1025

    res = bindings.codes_get_string_length(grib, 'marsParam')
    assert res == 8
示例#6
0
def test_codes_get_array_errors():
    grib = bindings.codes_grib_new_from_file(open(TEST_DATA))

    with pytest.raises(bindings.GribInternalError) as err:
        bindings.codes_get_array(grib, 'values', size=1)  # too short
    assert err.value.code == bindings.lib.GRIB_ARRAY_TOO_SMALL

    with pytest.raises(bindings.GribInternalError) as err:
        bindings.codes_get_array(grib, 'values', key_type=int)  # wrong type
    assert err.value.code == bindings.lib.GRIB_NOT_IMPLEMENTED
示例#7
0
def test_codes_keys_iterator():
    grib = bindings.codes_grib_new_from_file(open(TEST_DATA))

    iterator = bindings.codes_keys_iterator_new(grib)

    assert bindings.codes_keys_iterator_next(iterator) == 1
    assert bindings.codes_keys_iterator_get_name(iterator) == 'globalDomain'
    assert bindings.codes_keys_iterator_next(iterator) == 1
    assert bindings.codes_keys_iterator_get_name(iterator) == 'GRIBEditionNumber'

    bindings.codes_keys_iterator_delete(iterator)

    iterator = bindings.codes_keys_iterator_new(grib, namespace='time')

    assert bindings.codes_keys_iterator_next(iterator) == 1
    assert bindings.codes_keys_iterator_get_name(iterator) == 'dataDate'
    assert bindings.codes_keys_iterator_next(iterator) == 1
    assert bindings.codes_keys_iterator_get_name(iterator) == 'dataTime'

    bindings.codes_keys_iterator_delete(iterator)
示例#8
0
def test_codes_keys_iterator():
    grib = bindings.codes_grib_new_from_file(open(TEST_DATA))

    iterator = bindings.codes_keys_iterator_new(grib)

    assert bindings.codes_keys_iterator_next(iterator) == 1
    assert bindings.codes_keys_iterator_get_name(iterator) == "globalDomain"
    assert bindings.codes_keys_iterator_next(iterator) == 1
    assert bindings.codes_keys_iterator_get_name(
        iterator) == "GRIBEditionNumber"

    bindings.codes_keys_iterator_delete(iterator)

    iterator = bindings.codes_keys_iterator_new(grib, namespace="time")

    assert bindings.codes_keys_iterator_next(iterator) == 1
    assert bindings.codes_keys_iterator_get_name(iterator) == "dataDate"
    assert bindings.codes_keys_iterator_next(iterator) == 1
    assert bindings.codes_keys_iterator_get_name(iterator) == "dataTime"

    bindings.codes_keys_iterator_delete(iterator)
示例#9
0
def test_codes_grib_new_from_file_errors(tmpdir):
    empty_grib = tmpdir.join('empty.grib')
    empty_grib.ensure()

    with pytest.raises(EOFError):
        bindings.codes_grib_new_from_file(open(str(empty_grib)))

    garbage_grib = tmpdir.join('garbage.grib')
    garbage_grib.write('gargage')

    with pytest.raises(EOFError):
        bindings.codes_grib_new_from_file(open(str(garbage_grib)))

    bad_grib = tmpdir.join('bad.grib')
    bad_grib.write('GRIB')

    with pytest.raises(bindings.GribInternalError):
        bindings.codes_grib_new_from_file(open(str(bad_grib)))
示例#10
0
def test_codes_get_array(key, expected_value):
    grib = bindings.codes_grib_new_from_file(open(TEST_DATA))

    result = bindings.codes_get_array(grib, key)

    assert result == expected_value
示例#11
0
def test_codes_get_errors():
    grib = bindings.codes_grib_new_from_file(open(TEST_DATA))

    with pytest.raises(bindings.GribInternalError) as err:
        bindings.codes_get(grib, 'gridType', length=1)  # too short
    assert err.value.code == bindings.lib.GRIB_BUFFER_TOO_SMALL