Skip to content

Commit c03eeda

Browse files
committed
Update: encode the date of a time step with its filename
1 parent 0f59fb5 commit c03eeda

File tree

1 file changed

+33
-17
lines changed

1 file changed

+33
-17
lines changed

kaleidoscope/main/resolve.py

Lines changed: 33 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@
2020
from typing import TextIO
2121

2222
import yaml
23+
from xarray import DataArray
2324
from xarray import Dataset
25+
from xarray import decode_cf
2426

2527
from kaleidoscope import __name__
2628
from kaleidoscope import __version__
@@ -82,8 +84,9 @@ def _add_arguments(parser):
8284
)
8385
parser.add_argument(
8486
"target_file",
85-
help="the file path of the target datasets. The pattern TTT is "
86-
"replaced with the index value of the time slice extracted.",
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.",
8790
type=Path,
8891
)
8992

@@ -130,15 +133,28 @@ def _add_version(parser):
130133
)
131134

132135

133-
def index(i: int, w: int = 3) -> str:
136+
def date(t: DataArray) -> str:
134137
"""
135-
Converts an index number into an index string.
138+
Converts a time stamp into a date (YYYYMMDD).
136139
137-
:param i: The index number.
138-
:param w: The width of the index string.
139-
:return: The index string
140+
:param t: The time stamp.
141+
:return: The date.
140142
"""
141-
return str(i).rjust(w, "0")
143+
return t.dt.strftime("%Y%m%d").item()
144+
145+
146+
def time(d: Dataset) -> DataArray:
147+
"""
148+
Extracts the time data from a dataset.
149+
150+
:param d: The dataset.
151+
:return: The time data.
152+
"""
153+
t = decode_cf(
154+
d,
155+
drop_variables=[v for v in d.variables if v != VID_TIM],
156+
)
157+
return t[VID_TIM]
142158

143159

144160
class Processor(Processing):
@@ -180,20 +196,20 @@ def run(self, args: Namespace): # noqa: D102
180196
source: Dataset | None = None
181197
try:
182198
reader: Reading = self._create_reader(args)
183-
get_logger().debug(f"opening source dataset: {args.source_file}")
184-
source = reader.read(args.source_file)
185-
n = source[VID_TIM].size
186-
for i in range(n):
187-
writer: Writing = self._create_writer(args)
199+
source: Dataset = reader.read(args.source_file)
200+
t: DataArray = time(source)
201+
for i in range(t.size):
188202
target: Dataset = source.isel(time=slice(i, i + 1))
189-
get_logger().info(
190-
f"writing time step: {index(i)} ({index(n - 1)})"
191-
)
192203
try:
204+
get_logger().info(
205+
f"starting writing time step: {date(t[i])}"
206+
)
207+
writer: Writing = self._create_writer(args)
193208
writer.write(
194209
target,
195-
f"{args.target_file}".replace("TTT", f"{index(i)}"),
210+
f"{args.target_file}".replace("YYYYMMDD", date(t[i])),
196211
)
212+
get_logger().info(f"finished writing time step")
197213
finally:
198214
if target is not None:
199215
target.close()

0 commit comments

Comments
 (0)