Skip to content

Commit

Permalink
Feature/fix fdb stream (#175)
Browse files Browse the repository at this point in the history
* Fix fdb stream
  • Loading branch information
sandorkertesz authored Sep 6, 2023
1 parent 19a2157 commit dd382be
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 1 deletion.
3 changes: 2 additions & 1 deletion earthkit/data/readers/grib/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ def memory_reader(source, buf, magic=None, deeper_check=False):


def stream_reader(source, stream, magic=None, deeper_check=False):
if _match_magic(magic, deeper_check):
# by default we assume the stream is grib data
if magic is None or _match_magic(magic, deeper_check):
from .memory import GribFieldListInMemory, GribStreamReader

r = GribStreamReader(stream)
Expand Down
8 changes: 8 additions & 0 deletions earthkit/data/testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,14 @@ def modules_installed(*modules):
except Exception:
NO_EOD = True

try:
import pyfdb # noqa

fdb_home = os.environ.get("FDB_HOME", None)
NO_FDB = fdb_home is None
except Exception:
NO_FDB = True


def MISSING(*modules):
return not modules_installed(*modules)
Expand Down
68 changes: 68 additions & 0 deletions tests/sources/test_fdb.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
#!/usr/bin/env python3

# (C) Copyright 2020 ECMWF.
#
# This software is licensed under the terms of the Apache Licence Version 2.0
# which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
# In applying this licence, ECMWF does not waive the privileges and immunities
# granted to it by virtue of its status as an intergovernmental organisation
# nor does it submit to any jurisdiction.
#

import datetime

import pytest

from earthkit.data import from_source
from earthkit.data.testing import NO_FDB


@pytest.mark.long_test
@pytest.mark.download
@pytest.mark.skipif(NO_FDB, reason="No access to FDB")
def test_fdb_grib_stream():
d = datetime.datetime.now() - datetime.timedelta(days=2)
request = {
"class": "od",
"expver": "0001",
"stream": "oper",
"date": d.strftime("%Y%m%d"),
"time": [0, 12],
"domain": "g",
"type": "an",
"levtype": "sfc",
"step": 0,
"param": [151, 167],
}

ds = from_source("fdb", request)
cnt = sum([1 for f in ds])
assert cnt == 4


@pytest.mark.long_test
@pytest.mark.download
@pytest.mark.skipif(NO_FDB, reason="No access to FDB")
def test_fdb_grib_file():
d = datetime.datetime.now() - datetime.timedelta(days=2)
request = {
"class": "od",
"expver": "0001",
"stream": "oper",
"date": d.strftime("%Y%m%d"),
"time": [0, 12],
"domain": "g",
"type": "an",
"levtype": "sfc",
"step": 0,
"param": [151, 167],
}

ds = from_source("fdb", request, stream=False)
assert len(ds) == 4


if __name__ == "__main__":
from earthkit.data.testing import main

main(__file__)

0 comments on commit dd382be

Please sign in to comment.