Skip to content

Commit bb7580b

Browse files
EliEli
authored andcommitted
clip_dems uses yaml file list.
1 parent d5f48b2 commit bb7580b

1 file changed

Lines changed: 85 additions & 36 deletions

File tree

schimpy/clip_dems.py

Lines changed: 85 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/usr/bin/env python
22
import math
33
import click
4-
from schimpy.prepare_schism import process_prepare_yaml
4+
import schimpy.schism_yaml as schism_yaml
55

66
try:
77
from osgeo import gdal
@@ -22,6 +22,19 @@
2222

2323

2424
def bounding_coords(image):
25+
"""
26+
Compute bounding coordinates (upper-left and lower-right) of a GDAL-readable raster.
27+
28+
Parameters
29+
----------
30+
image : str
31+
Path to an image/DEM readable by GDAL.
32+
33+
Returns
34+
-------
35+
(xlo, xhi) : tuple[tuple[float, float], tuple[float, float]]
36+
xlo is (ulx, uly); xhi is (lrx, lry) in the raster’s coordinate system.
37+
"""
2538
ds = gdal.Open(image, GA_ReadOnly)
2639
gt = ds.GetGeoTransform()
2740
cols = ds.RasterXSize
@@ -32,43 +45,79 @@ def bounding_coords(image):
3245
ds = None
3346
return xlo, xhi
3447

48+
def load_dem_list(dem_spec_yaml):
49+
"""
50+
Load a prioritized DEM list from a SCHISM-YAML spec (via `schism_yaml`).
51+
52+
The YAML must contain either:
53+
- `dem_list: [...]`
54+
- or `dem: { dem_list: [...] }`
55+
56+
Parameters
57+
----------
58+
dem_spec_yaml : str
59+
Path to YAML file.
60+
61+
Returns
62+
-------
63+
list[str]
64+
DEM filenames in priority order (high → low).
65+
66+
Raises
67+
------
68+
FileNotFoundError
69+
If `dem_spec_yaml` does not exist.
70+
KeyError
71+
If no `dem_list` can be found in the supported locations.
72+
TypeError
73+
If the `dem_list` is not a list of strings.
74+
ValueError
75+
If the list is empty.
76+
"""
77+
if not os.path.isfile(dem_spec_yaml):
78+
raise FileNotFoundError(f"DEM spec YAML not found: {dem_spec_yaml}")
79+
80+
with open(dem_spec_yaml, "r") as f:
81+
data = schism_yaml.load(f)
3582

36-
def open_demlist(demlist):
37-
if not os.path.isfile(demlist):
38-
raise ValueError("demlist file %s not found" % demlist)
39-
try:
40-
inputs, out_dir, logger = process_prepare_yaml(
41-
demlist, use_logging=False, write_echo=False
83+
if isinstance(data, dict) and "dem_list" in data:
84+
dem_list = data["dem_list"]
85+
elif isinstance(data, dict) and "dem" in data and isinstance(data["dem"], dict) and "dem_list" in data["dem"]:
86+
dem_list = data["dem"]["dem_list"]
87+
else:
88+
raise KeyError(
89+
"DEM spec YAML must contain either `dem_list: [...]` or `dem: { dem_list: [...] }`"
4290
)
43-
if "mesh" in inputs:
44-
filelist = inputs["mesh"]["dem_list"]
45-
else:
46-
filelist = inputs.get("dem_list")
47-
48-
except:
49-
print(f"Unable to open {demlist} as schimpy yaml")
50-
with open(demlist, "r") as f:
51-
filelist = f.readlines()
52-
filelist = [
53-
f.strip() for f in filelist if (not f.startswith("#")) and (f.strip())
54-
]
55-
56-
if len(filelist) == 0:
57-
raise ValueError("demlist file %s is empty" % demlist)
58-
59-
return filelist
60-
61-
62-
def clip_dem(
63-
xlo,
64-
xhi,
65-
demlist="dem.txt",
66-
outformat="AAIGrid",
67-
hshift=False,
68-
prefix="clipped",
69-
verbose=False,
70-
):
71-
filelist = open_demlist(demlist)
91+
92+
if not isinstance(dem_list, list) or not all(isinstance(x, str) for x in dem_list):
93+
raise TypeError("`dem_list` must be a list of strings")
94+
if len(dem_list) == 0:
95+
raise ValueError("`dem_list` is empty")
96+
97+
return dem_list
98+
99+
100+
def clip_dem(xlo, xhi, demlist="dem.txt", outformat="AAIGrid",
101+
hshift=False, prefix="dem_clip", verbose=False):
102+
"""
103+
Clip each DEM that intersects the requested bounding box, producing numbered outputs.
104+
105+
Parameters
106+
----------
107+
xlo, xhi : tuple[float, float]
108+
Upper-left (xlo) and lower-right (xhi) coordinates for the clipping box.
109+
demlist : str
110+
Path to DEM list YAML spec (see `load_dem_list`).
111+
outformat : str
112+
GDAL output format name (e.g. 'AAIGrid', 'GTiff', 'PNG', ...).
113+
hshift : bool
114+
Deprecated SMS < 11.1 half-cell shift option.
115+
prefix : str
116+
Output filename prefix; outputs are `{prefix}_{i}.{ext}`.
117+
verbose : bool
118+
If True, print details and show GDAL command.
119+
"""
120+
filelist = load_dem_list(demlist)
72121

73122
iout = 0
74123
if outformat == "AAIGrid":

0 commit comments

Comments
 (0)