def test_build_data_var_components_no_encode():
    index = messages.Stream(path=TEST_DATA).index(dataset.ALL_KEYS).subindex(paramId=130)
    dims, data_var, coord_vars = dataset.build_data_var_components(index=index)
    assert dims == {'number': 10, 'dataDate': 2, 'dataTime': 2, 'topLevel': 2, 'i': 7320}
    assert data_var.data.shape == (10, 2, 2, 2, 7320)

    # equivalent to not np.isnan without importing numpy
    assert data_var.data[:, :, :, :, :].mean() > 0.
def test_Index_errors():
    class MyMessage(messages.ComputedKeysMessage):
        computed_keys = {
            'error_key': lambda m: 1 / 0,
        }

    stream = messages.Stream(TEST_DATA, message_class=MyMessage)
    res = messages.Index.fromstream(stream, ['paramId', 'error_key'])
    assert res['paramId'] == [129, 130]
    assert len(res) == 2
    assert list(res) == ['paramId', 'error_key']
    assert res['error_key'] == ['undef']
def test_build_data_var_components_encode_geography():
    stream = messages.Stream(path=TEST_DATA, message_class=cfmessage.CfMessage)
    index = stream.index(dataset.ALL_KEYS).subindex(paramId=130)
    dims, data_var, coord_vars = dataset.build_data_var_components(
        index=index, encode_geography=True,
    )
    assert dims == {
        'number': 10, 'dataDate': 2, 'dataTime': 2,
        'topLevel': 2, 'latitude': 61, 'longitude': 120,
    }
    assert data_var.data.shape == (10, 2, 2, 2, 61, 120)

    # equivalent to not np.isnan without importing numpy
    assert data_var.data[:, :, :, :, :, :].mean() > 0.
def test_Index():
    res = messages.Index.fromstream(messages.Stream(TEST_DATA), ['paramId'])
    assert res['paramId'] == [129, 130]
    assert len(res) == 1
    assert list(res) == ['paramId']
    assert res.first()

    with pytest.raises(ValueError):
        res.getone('paramId')

    with pytest.raises(KeyError):
        res['non-existent-key']

    subres = res.subindex(paramId=130)

    assert subres.get('paramId') == [130]
    assert subres.getone('paramId') == 130
    assert len(subres) == 1
def test_Stream():
    res = messages.Stream(TEST_DATA)
    leader = res.first()
    assert len(leader) == 192
    assert sum(1 for _ in res) == leader['count']
    assert len(res.index(['paramId'])) == 1