From 86bc9a915e9d2853e1c32624c5ce563387842055 Mon Sep 17 00:00:00 2001 From: rolfhut Date: Tue, 20 May 2025 11:39:30 +0200 Subject: [PATCH 1/5] fixed download problem, still problem with file handling --- src/ewatercycle/_forcings/caravan.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/ewatercycle/_forcings/caravan.py b/src/ewatercycle/_forcings/caravan.py index 8a535f1c..c1e24b1b 100644 --- a/src/ewatercycle/_forcings/caravan.py +++ b/src/ewatercycle/_forcings/caravan.py @@ -188,8 +188,18 @@ def generate( # type: ignore[override] ds_basin = ds.sel(basin_id=basin_id.encode()) ds_basin_time = crop_ds(ds_basin, start_time, end_time) + if shape is None: shape = get_shapefiles(Path(directory), basin_id) + elif Path(shape).name == "combined.shp": + shape_path_out = Path(directory) / f"{basin_id}.shp" + extract_basin_shapefile(basin_id, Path(shape), shape_path_out) + elif not (Path(shape).name == f"{basin_id}.shp"): + msg = ( + "shape must either point to a shapefile of the basin ID" + "Or to the combined.shp file that contains all basins." + ) + raise ValueError(msg) if len(variables) == 0: variables = ds_basin_time.data_vars.keys() # type: ignore[assignment] From 2e8fdc00c368990b9f069c4dbecfa6ae90b43607 Mon Sep 17 00:00:00 2001 From: rolfhut Date: Tue, 20 May 2025 11:55:14 +0200 Subject: [PATCH 2/5] fixed it :-). removed check on polygon, which gave errors that were not correct, possible because check was done before file was written? --- src/ewatercycle/_forcings/caravan.py | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/src/ewatercycle/_forcings/caravan.py b/src/ewatercycle/_forcings/caravan.py index c1e24b1b..34e65016 100644 --- a/src/ewatercycle/_forcings/caravan.py +++ b/src/ewatercycle/_forcings/caravan.py @@ -151,7 +151,7 @@ def generate( # type: ignore[override] end_time: str, directory: str, variables: tuple[str, ...] = (), - shape: str | Path | None = None, + shape_in: str | Path | None = None, **kwargs, ) -> "CaravanForcing": """Retrieve caravan for a model. @@ -189,17 +189,19 @@ def generate( # type: ignore[override] ds_basin_time = crop_ds(ds_basin, start_time, end_time) - if shape is None: + if shape_in is None: shape = get_shapefiles(Path(directory), basin_id) - elif Path(shape).name == "combined.shp": - shape_path_out = Path(directory) / f"{basin_id}.shp" - extract_basin_shapefile(basin_id, Path(shape), shape_path_out) + elif Path(shape_in).name == "combined.shp": + shape = Path(directory) / f"{basin_id}.shp" + extract_basin_shapefile(basin_id, Path(shape_in), shape) elif not (Path(shape).name == f"{basin_id}.shp"): msg = ( "shape must either point to a shapefile of the basin ID" "Or to the combined.shp file that contains all basins." ) raise ValueError(msg) + else: + shape = shape_in if len(variables) == 0: variables = ds_basin_time.data_vars.keys() # type: ignore[assignment] @@ -309,9 +311,9 @@ def extract_basin_shapefile( # kind of clunky but it works: select filtered polygon if i == basin_index: geom = feat.geometry - if geom.type != "Polygon": - msg = "Only polygons are supported" - raise ValueError(msg) + # if geom.type != "Polygon": + # msg = "Only polygons are supported" + # raise ValueError(msg) # Add the signed area of the polygon and a timestamp # to the feature properties map. From a22b6d8110e64df956043108d5668ec0ed083f70 Mon Sep 17 00:00:00 2001 From: rolfhut Date: Tue, 20 May 2025 12:03:37 +0200 Subject: [PATCH 3/5] removed white space --- src/ewatercycle/_forcings/caravan.py | 1 - 1 file changed, 1 deletion(-) diff --git a/src/ewatercycle/_forcings/caravan.py b/src/ewatercycle/_forcings/caravan.py index 34e65016..aed720e7 100644 --- a/src/ewatercycle/_forcings/caravan.py +++ b/src/ewatercycle/_forcings/caravan.py @@ -188,7 +188,6 @@ def generate( # type: ignore[override] ds_basin = ds.sel(basin_id=basin_id.encode()) ds_basin_time = crop_ds(ds_basin, start_time, end_time) - if shape_in is None: shape = get_shapefiles(Path(directory), basin_id) elif Path(shape_in).name == "combined.shp": From f93f00f887a5c45ff75d8ea7822bb80a67d1b4a2 Mon Sep 17 00:00:00 2001 From: rolfhut Date: Tue, 20 May 2025 12:09:38 +0200 Subject: [PATCH 4/5] fixed typos based on lint feedback --- src/ewatercycle/_forcings/caravan.py | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/src/ewatercycle/_forcings/caravan.py b/src/ewatercycle/_forcings/caravan.py index aed720e7..c99a331d 100644 --- a/src/ewatercycle/_forcings/caravan.py +++ b/src/ewatercycle/_forcings/caravan.py @@ -164,7 +164,7 @@ def generate( # type: ignore[override] directory: Directory in which forcing should be written. variables: Variables which are needed for model, if not specified will default to all. - shape: (Optional) Path to a shape file. + shape_in: (Optional) Path to a shape file of the basin, or the combined.shp file of all basins. If none is specified, will be downloaded automatically. kwargs: Additional keyword arguments. basin_id: The ID of the desired basin. Data sets can be explored using @@ -193,7 +193,7 @@ def generate( # type: ignore[override] elif Path(shape_in).name == "combined.shp": shape = Path(directory) / f"{basin_id}.shp" extract_basin_shapefile(basin_id, Path(shape_in), shape) - elif not (Path(shape).name == f"{basin_id}.shp"): + elif Path(shape).name != f"{basin_id}.shp": msg = ( "shape must either point to a shapefile of the basin ID" "Or to the combined.shp file that contains all basins." @@ -310,9 +310,6 @@ def extract_basin_shapefile( # kind of clunky but it works: select filtered polygon if i == basin_index: geom = feat.geometry - # if geom.type != "Polygon": - # msg = "Only polygons are supported" - # raise ValueError(msg) # Add the signed area of the polygon and a timestamp # to the feature properties map. From ca884ee7113356df6e7601ce6e859879b2589906 Mon Sep 17 00:00:00 2001 From: rolfhut Date: Tue, 20 May 2025 12:16:51 +0200 Subject: [PATCH 5/5] blech minor details --- src/ewatercycle/_forcings/caravan.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/ewatercycle/_forcings/caravan.py b/src/ewatercycle/_forcings/caravan.py index c99a331d..96150497 100644 --- a/src/ewatercycle/_forcings/caravan.py +++ b/src/ewatercycle/_forcings/caravan.py @@ -164,7 +164,8 @@ def generate( # type: ignore[override] directory: Directory in which forcing should be written. variables: Variables which are needed for model, if not specified will default to all. - shape_in: (Optional) Path to a shape file of the basin, or the combined.shp file of all basins. + shape_in: (Optional) Path to a shape file of the basin, or the combined.shp + file of all basins. If none is specified, will be downloaded automatically. kwargs: Additional keyword arguments. basin_id: The ID of the desired basin. Data sets can be explored using