Skip to content

Commit

Permalink
fix #1068
Browse files Browse the repository at this point in the history
  • Loading branch information
DirkEilander committed Sep 27, 2024
1 parent e5c93ae commit 0e39435
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 15 deletions.
8 changes: 8 additions & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@ All notable changes to this project will be documented in this page.
The format is based on `Keep a Changelog`_, and this project adheres to
`Semantic Versioning`_.

unreleased
==========

Fixed
-----
- Bug in paths with "{}" in DataCatalog


v0.10.0 (2024-06-14)
====================

Expand Down
10 changes: 8 additions & 2 deletions hydromt/data_adapter/data_adapter.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""DataAdapter class."""

from __future__ import annotations

import logging
Expand Down Expand Up @@ -107,7 +108,6 @@ def harmonise_dims(ds):


class DataAdapter(object, metaclass=ABCMeta):

"""General Interface to data source for HydroMT."""

_DEFAULT_DRIVER = None # placeholder
Expand Down Expand Up @@ -325,6 +325,7 @@ def _resolve_paths(
known_keys = ["year", "month", "zoom_level", "variable"]
fns = []
keys = []
unknown_keys = []
# rebuild path based on arguments and escape unknown keys
if "{" in str(self.path):
path = ""
Expand All @@ -340,10 +341,15 @@ def _resolve_paths(
path += "*"
# escape unknown fields
elif key is not None and key not in known_keys:
path = path + "{" + key_str + "}"
unknown_keys.append(key)
path += key_str
else:
path = path + key_str
keys.append(key)
if keys and unknown_keys:
# escape unknown keys
for key in unknown_keys:
path = path.replace("{" + key + "}", "{{" + key + "}}")
else:
path = self.path

Expand Down
26 changes: 13 additions & 13 deletions tests/test_data_adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
GeoDatasetAdapter,
RasterDatasetAdapter,
)
from hydromt.data_adapter.dataframe import DataFrameAdapter
from hydromt.data_catalog import DataCatalog
from hydromt.exceptions import NoDataException
from hydromt.gis_utils import to_geographic_bbox
Expand All @@ -39,25 +40,15 @@

def test_resolve_path(tmpdir):
# create dummy files
path = join(tmpdir, "{unknown_key}_{zoom_level}_{variable}_{year}_{month:02d}.nc")
for variable in ["precip", "temp"]:
for year in [2020, 2021]:
for month in range(1, 13):
fn = join(tmpdir, f"{{unknown_key}}_0_{variable}_{year}_{month:02d}.nc")
with open(fn, "w") as f:
f.write("")
# create data catalog for these files
dd = {
"test": {
"data_type": "RasterDataset",
"driver": "netcdf",
"path": join(
tmpdir, "{unknown_key}_{zoom_level}_{variable}_{year}_{month:02d}.nc"
),
}
}
cat = DataCatalog()
cat.from_dict(dd)
source = cat.get_source("test")
# create adapter
source = RasterDatasetAdapter(path=path)
# test
fns = source._resolve_paths()
assert len(fns) == 48
Expand All @@ -69,6 +60,15 @@ def test_resolve_path(tmpdir):
source._resolve_paths(variables=["waves"])


def test_resolve_path_unknown_key(tmp_path):
# create dummy csv file
path = tmp_path / "{unknown_key}.csv"
path.write_text("test")
# create adapter
source = DataFrameAdapter(path=path)
source._resolve_paths()


def test_rasterdataset(rioda, tmpdir):
fn_tif = str(tmpdir.join("test.tif"))
rioda_utm = rioda.raster.reproject(dst_crs="utm")
Expand Down

0 comments on commit 0e39435

Please sign in to comment.