Skip to content

Commit

Permalink
build: add netcdf4 and cfgrib as dependencies (#69)
Browse files Browse the repository at this point in the history
  • Loading branch information
amarandon authored Nov 13, 2024
1 parent 4a15c79 commit 7e08bcc
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 4 deletions.
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ repos:
- id: check-merge-conflict

- repo: 'https://github.com/PyCQA/flake8'
rev: 5.0.4 # needed for py < 3.8.1
rev: 7.1.1
hooks:
- id: flake8

Expand Down
8 changes: 7 additions & 1 deletion eodag_cube/api/product/drivers/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@
logger = logging.getLogger("eodag-cube.driver.generic")


# File extensions to accept on top of those known to rasterio/GDAL
EXTRA_ALLOWED_FILE_EXTENSIONS = [".grib", ".grib2"]


class GenericDriver(DatasetDriver):
"""Generic Driver for products that need to be downloaded"""

Expand All @@ -56,7 +60,9 @@ def get_data_address(self, eo_product: EOProduct, band: str) -> str:
matching_files = []
for f_path in Path(uri_to_path(eo_product.location)).glob("**/*"):
f_str = str(f_path.resolve())
if p.search(f_str):
if f_path.suffix in EXTRA_ALLOWED_FILE_EXTENSIONS:
matching_files.append(f_str)
elif p.search(f_str):
try:
# files readable by rasterio
rasterio.drivers.driver_from_extension(f_path)
Expand Down
2 changes: 2 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
"rasterio",
"xarray",
"rioxarray",
"netcdf4",
"cfgrib",
],
extras_require={
"dev": [
Expand Down
11 changes: 11 additions & 0 deletions tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,17 @@
RESOURCES_PATH = jp(dirn(__file__), "..", "eodag", "resources")
TESTS_DOWNLOAD_PATH = "/tmp/eodag_tests"

TEST_GRIB_PRODUCT = (
"CAMS_EAC4_20210101_20210102_4d792734017419d1719b53f4d5b5d4d6888641de"
)
TEST_GRIB_FILENAME = f"{TEST_GRIB_PRODUCT}.grib"
TEST_GRIB_PRODUCT_PATH = os.path.join(
TEST_RESOURCES_PATH,
"products",
TEST_GRIB_PRODUCT,
)
TEST_GRIB_FILE_PATH = os.path.join(TEST_GRIB_PRODUCT_PATH, TEST_GRIB_FILENAME)


class EODagTestCase(unittest.TestCase):
def setUp(self):
Expand Down
Binary file not shown.
Binary file not shown.
13 changes: 12 additions & 1 deletion tests/units/test_eoproduct.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import xarray as xr
from rasterio.session import AWSSession

from tests import EODagTestCase
from tests import TEST_GRIB_FILE_PATH, TEST_GRIB_FILENAME, EODagTestCase
from tests.context import (
DEFAULT_PROJ,
Authentication,
Expand Down Expand Up @@ -75,6 +75,17 @@ def test_get_data_local_product_ok(self):
self.assertIsInstance(data, xr.DataArray)
self.assertNotEqual(data.values.size, 0)

def test_get_data_local_grib_product_ok(self):
"""A call to get_data on a local product in GRIB format must succeed""" # noqa
product = EOProduct(self.provider, self.eoproduct_props)
product.driver = mock.MagicMock(spec_set=NoDriver())
product.driver.get_data_address.return_value = TEST_GRIB_FILE_PATH
data = product.get_data(band=TEST_GRIB_FILENAME)
self.assertEqual(product.driver.get_data_address.call_count, 1)

self.assertIsInstance(data, xr.DataArray)
self.assertEqual(data.attrs["GRIB_COMMENT"], "Temperature [C]")

def test_get_data_download_on_unsupported_dataset_address_scheme_error(self):
"""If a product is not on the local filesystem, it must download itself before returning the data""" # noqa
product = EOProduct(
Expand Down
25 changes: 24 additions & 1 deletion tests/units/test_eoproduct_driver_generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,13 @@
import os
from contextlib import contextmanager

from tests import TEST_RESOURCES_PATH, EODagTestCase
from tests import (
TEST_GRIB_FILE_PATH,
TEST_GRIB_FILENAME,
TEST_GRIB_PRODUCT_PATH,
TEST_RESOURCES_PATH,
EODagTestCase,
)
from tests.context import (
AddressNotFound,
EOProduct,
Expand Down Expand Up @@ -58,6 +64,14 @@ def test_driver_get_local_dataset_address_ok(self):
address = self.product.driver.get_data_address(product, band)
self.assertEqual(address, self.local_band_file)

def test_driver_get_local_grib_dataset_address_ok(self):
"""Driver returns a good address for a grib file"""
with self._grib_product() as product:

address = self.product.driver.get_data_address(product, TEST_GRIB_FILENAME)

self.assertEqual(address, TEST_GRIB_FILE_PATH)

def test_driver_get_http_remote_dataset_address_fail(self):
"""Driver must raise UnsupportedDatasetAddressScheme if location scheme is http or https"""
# Default value of self.product.location is 'https://...'
Expand All @@ -79,3 +93,12 @@ def _filesystem_product(self):
yield self.product
finally:
self.product.location = original

@contextmanager
def _grib_product(self):
original = self.product.location
try:
self.product.location = f"file://{TEST_GRIB_PRODUCT_PATH}"
yield self.product
finally:
self.product.location = original

0 comments on commit 7e08bcc

Please sign in to comment.