Skip to content

Commit 01b5ae6

Browse files
GohNgeeJuaypre-commit-ci[bot]euronion
authored
Add argument logic for the arguments into cutout init and prepare (#445)
* Refactor cutout.py and data.py. Improve documentation and argument handling. * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * doc: Add release notes. * code: Fix UnboundLocalError when no bounds are provided --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: euronion <[email protected]> Co-authored-by: Johannes HAMPP <[email protected]>
1 parent 719b9b3 commit 01b5ae6

File tree

3 files changed

+30
-5
lines changed

3 files changed

+30
-5
lines changed

RELEASE_NOTES.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ Release Notes
1818
.. ``pip install git+https://github.com/pypsa/atlite``.
1919
2020
* Feature: Add new onshore turbine models: eno 126 3.5 MW, eno 126 4 MW, and eno 126 4.8 MW . (turbines that match more closely the PyPSA/technologydata cost assumptions)
21+
* Fix `atlite.Cutout()` to be able to handle the `bounds` argument to be a `DataFrame` in accordance to the docstring (https://github.com/PyPSA/atlite/pull/445).
22+
* Raise a `FileNotFoundError` if the `temp_dir` explicitly specified for cutout preparation does not exist instead of failing with an obfuscated error message (https://github.com/PyPSA/atlite/pull/445).
2123

2224
`v0.4.1 <https://github.com/PyPSA/atlite/releases/tag/v0.4.1>`__ (12th May 2025)
2325
=======================================================================================

atlite/cutout.py

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -98,9 +98,10 @@ def __init__(self, path, **cutoutparams):
9898
Time range to include in the cutout, e.g. "2011" or
9999
("2011-01-05", "2011-01-25")
100100
This is necessary when building a new cutout.
101-
bounds : GeoSeries.bounds | DataFrame, optional
102-
The outer bounds of the cutout or as a DataFrame
103-
containing (min.long, min.lat, max.long, max.lat).
101+
bounds : DataFrame | Tuple, optional
102+
The outer bounds of the cutout containing (min.long, min.lat, max.long, max.lat)
103+
or a single-row DataFrame with [["minx", "miny", "maxx", "maxy"]] column values.
104+
From GeoPandas DataFrames and Series, this is easily accessible through `.geometry.bounds`.
104105
x : slice, optional
105106
Outer longitudinal bounds for the cutout (west, east).
106107
y : slice, optional
@@ -163,7 +164,18 @@ def __init__(self, path, **cutoutparams):
163164
logger.info(f"Building new cutout {path}")
164165

165166
if "bounds" in cutoutparams:
166-
x1, y1, x2, y2 = cutoutparams.pop("bounds")
167+
bounds = cutoutparams.pop("bounds")
168+
# If its a dataframe, we will extract the values
169+
if isinstance(bounds, pd.DataFrame) and bounds.shape[0] == 1:
170+
x1, y1, x2, y2 = bounds.iloc[0][
171+
["minx", "miny", "maxx", "maxy"]
172+
].to_list()
173+
elif isinstance(bounds, tuple): # If its a tuple
174+
x1, y1, x2, y2 = bounds
175+
else:
176+
raise ValueError(
177+
"`bounds` must be a tuple or a DataFrame with a single row entry."
178+
)
167179
cutoutparams.update(x=slice(x1, x2), y=slice(y1, y2))
168180

169181
try:

atlite/data.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import logging
99
import os
1010
from functools import wraps
11+
from pathlib import Path
1112
from shutil import rmtree
1213
from tempfile import mkdtemp, mkstemp
1314

@@ -161,7 +162,7 @@ def cutout_prepare(
161162
tmpdir : str/Path, optional
162163
Directory in which temporary files (for example retrieved ERA5 netcdf
163164
files) are stored. If set, the directory will not be deleted and the
164-
intermediate files can be examined.
165+
intermediate files can be examined. The path must be a valid path.
165166
data_format : str, optional
166167
The data format used to retrieve the data. Only relevant for ERA5 data. The default is 'grib'.
167168
overwrite : bool, optional
@@ -191,6 +192,12 @@ def cutout_prepare(
191192
cutout : atlite.Cutout
192193
Cutout with prepared data. The variables are stored in `cutout.data`.
193194
195+
196+
Raises
197+
------
198+
NotADirectoryError
199+
The argument `tmpdir` is not a valid path.
200+
194201
"""
195202
if dask_kwargs is None:
196203
dask_kwargs = {}
@@ -199,6 +206,10 @@ def cutout_prepare(
199206
logger.info("Cutout already prepared.")
200207
return cutout
201208

209+
# ensure that the tmpdir actually exists
210+
temp_dir_path = Path(tmpdir)
211+
if not temp_dir_path.is_dir():
212+
raise FileNotFoundError(f"The tmpdir: {temp_dir_path} does not exist.")
202213
logger.info(f"Storing temporary files in {tmpdir}")
203214

204215
modules = atleast_1d(cutout.module)

0 commit comments

Comments
 (0)