Skip to content

Commit 53cafb9

Browse files
committed
New: resolve depth level
1 parent 6b0c73c commit 53cafb9

File tree

2 files changed

+60
-17
lines changed

2 files changed

+60
-17
lines changed

kaleidoscope/interface/constants.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,6 @@
2222

2323
VID_LON = "lon"
2424
"""The longitude variable identifier."""
25+
26+
VID_DEP = "depth"
27+
"""The depth variable identifier."""

kaleidoscope/main/resolve.py

Lines changed: 57 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626

2727
from kaleidoscope import __name__
2828
from kaleidoscope import __version__
29+
from kaleidoscope.interface.constants import VID_DEP
2930
from kaleidoscope.interface.constants import VID_TIM
3031
from kaleidoscope.interface.processing import Processing
3132
from kaleidoscope.interface.reading import Reading
@@ -63,7 +64,7 @@ def create() -> ArgumentParser:
6364
prog=f"{__name__}-resolve",
6465
description="This scientific processor transforms a time series "
6566
"dataset into separate datasets, each of which corresponds to a "
66-
"different time step.",
67+
"different time step, and depth level if present.",
6768
epilog="Copyright (c) Brockmann Consult GmbH, 2025. "
6869
"License: MIT",
6970
exit_on_error=False,
@@ -84,9 +85,10 @@ def _add_arguments(parser):
8485
)
8586
parser.add_argument(
8687
"target_file",
87-
help="the file path of the target datasets. The pattern "
88-
"YYYYMMDD is replaced with the date associated with the "
89-
"time step extracted.",
88+
help="the file path pattern of target datasets. Patterns "
89+
"YYYY/MM and YYYYMMDD are replaced with the date associated "
90+
"with the time step extracted. The pattern ZZZZ is replaced "
91+
"with the depth level, if applicable.",
9092
type=Path,
9193
)
9294

@@ -165,6 +167,27 @@ def time(d: Dataset) -> DataArray:
165167
return t[VID_TIM]
166168

167169

170+
def depth(d: Dataset) -> DataArray:
171+
"""
172+
Extracts the depth data from a dataset.
173+
174+
:param d: The dataset.
175+
:return: The depth data.
176+
"""
177+
return d[VID_DEP]
178+
179+
180+
def index(k: int, w: int = 4) -> str:
181+
"""
182+
Converts an index number into an index string.
183+
184+
:param k: The index number.
185+
:param w: The width of the index string.
186+
:return: The index string
187+
"""
188+
return str(k).rjust(w, "0")
189+
190+
168191
class Processor(Processing):
169192
"""! The Kaleidoscope resolve processor."""
170193

@@ -207,25 +230,42 @@ def run(self, args: Namespace): # noqa: D102
207230
source: Dataset = reader.read(args.source_file)
208231
t: DataArray = time(source)
209232
for i in range(t.size):
210-
target: Dataset = source.isel(time=slice(i, i + 1))
211-
try:
212-
get_logger().info(
213-
f"starting writing time step: {date(t[i])}"
214-
)
233+
get_logger().info(f"starting writing time step: {date(t[i])}")
234+
if VID_DEP in source:
235+
z: DataArray = depth(source)
236+
for k in range(z.size):
237+
target: Dataset = source.isel(
238+
{VID_TIM: i, VID_DEP: k}
239+
)
240+
writer: Writing = self._create_writer(args)
241+
target_path: Path = Path(
242+
f"{args.target_file}".replace(
243+
"YYYY/MM", date(t[i], "%Y/%m")
244+
)
245+
.replace("YYYYMMDD", date(t[i]))
246+
.replace("ZZZZ", index(k))
247+
)
248+
if not target_path.parent.exists():
249+
target_path.parent.mkdir(parents=True)
250+
try:
251+
writer.write(target, target_path)
252+
finally:
253+
target.close()
254+
else:
255+
target: Dataset = source.isel({VID_TIM: i})
215256
writer: Writing = self._create_writer(args)
216257
target_path: Path = Path(
217-
f"{args.target_file}".replace("YYYYMMDD", date(t[i]))
218-
.replace("YYYY", date(t[i], "%Y"))
219-
.replace("MM", date(t[i], "%m"))
220-
.replace("DD", date(t[i], "%d"))
258+
f"{args.target_file}".replace(
259+
"YYYY/MM", date(t[i], "%Y/%m")
260+
).replace("YYYYMMDD", date(t[i]))
221261
)
222262
if not target_path.parent.exists():
223263
target_path.parent.mkdir(parents=True)
224-
writer.write(target, target_path)
225-
get_logger().info(f"finished writing time step")
226-
finally:
227-
if target is not None:
264+
try:
265+
writer.write(target, target_path)
266+
finally:
228267
target.close()
268+
get_logger().info(f"finished writing time step")
229269
finally:
230270
if source is not None:
231271
source.close()

0 commit comments

Comments
 (0)