Ejemplo n.º 1
0
def test_codes_write(tmpdir):
    message_id = bindings.codes_new_from_samples(b'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_handle_new_from_file(file)
Ejemplo n.º 2
0
def test_codes_get(key, expected_type, expected_value):
    grib = bindings.codes_handle_new_from_file(open(TEST_DATA))

    result = bindings.codes_get(grib, key)

    assert isinstance(result, expected_type)
    assert result == expected_value
Ejemplo n.º 3
0
def test_codes_handle_clone():
    handle = bindings.codes_handle_new_from_file(open(TEST_DATA))

    res = bindings.codes_handle_clone(handle)

    assert isinstance(res, bindings.ffi.CData)
    assert "'grib_handle *'" in repr(res)
Ejemplo n.º 4
0
def test_codes_get_array_errors():
    grib = bindings.codes_handle_new_from_file(open(TEST_DATA))

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

    with pytest.raises(bindings.EcCodesError) as err:
        bindings.codes_get_array(grib, b'values', key_type=bindings.CODES_TYPE_LONG)  # wrong type
    assert err.value.code == bindings.lib.GRIB_NOT_IMPLEMENTED
Ejemplo n.º 5
0
def test_codes_keys_iterator():
    grib = bindings.codes_handle_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) == b'globalDomain'
    assert bindings.codes_keys_iterator_next(iterator) == 1
    assert bindings.codes_keys_iterator_get_name(iterator) == b'GRIBEditionNumber'

    bindings.codes_keys_iterator_delete(iterator)

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

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

    bindings.codes_keys_iterator_delete(iterator)
Ejemplo n.º 6
0
def test_codes_handle_new_from_file_errors(tmpdir):
    empty_grib = tmpdir.join('empty.grib')
    empty_grib.ensure()

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

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

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

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

    with pytest.raises(bindings.EcCodesError):
        bindings.codes_handle_new_from_file(open(str(bad_grib)))
Ejemplo n.º 7
0
def test_codes_get_array(key, expected_value):
    grib = bindings.codes_handle_new_from_file(open(TEST_DATA))

    result = bindings.codes_get_array(grib, key)

    assert result == expected_value
Ejemplo n.º 8
0
def test_codes_get_errors():
    grib = bindings.codes_handle_new_from_file(open(TEST_DATA))

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