Skip to content

Commit d6fd430

Browse files
authored
fix(netcdf): Fix context time attributes (#204)
1 parent 674e788 commit d6fd430

File tree

3 files changed

+14
-9
lines changed

3 files changed

+14
-9
lines changed

src/anemoi/inference/outputs/netcdf.py

+6-4
Original file line numberDiff line numberDiff line change
@@ -91,10 +91,12 @@ def open(self, state: State) -> None:
9191

9292
time = 0
9393
self.reference_date = state["date"]
94-
if hasattr(self.context, "time_step") and hasattr(self.context, "lead_time"):
95-
time = self.context.lead_time // self.context.time_step
96-
if hasattr(self.context, "reference_date"):
97-
self.reference_date = self.context.reference_date
94+
if (time_step := getattr(self.context, "time_step", None)) and (
95+
lead_time := getattr(self.context, "lead_time", None)
96+
):
97+
time = lead_time // time_step
98+
if reference_date := getattr(self.context, "reference_date", None):
99+
self.reference_date = reference_date
98100

99101
with LOCK:
100102
self.values_dim = self.ncfile.createDimension("values", values)

src/anemoi/inference/runner.py

-4
Original file line numberDiff line numberDiff line change
@@ -214,10 +214,6 @@ def run(
214214

215215
lead_time = to_timedelta(lead_time)
216216

217-
# This may be used but Output objects to compute the step
218-
self.lead_time = lead_time
219-
self.time_step = self.checkpoint.timestep
220-
221217
with ProfilingRunner(self.use_profiler):
222218
with ProfilingLabel("Prepare input tensor", self.use_profiler):
223219
input_tensor = self.prepare_input_tensor(input_state)

src/anemoi/inference/runners/default.py

+8-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
from typing import List
1616

1717
from anemoi.utils.config import DotDict
18+
from anemoi.utils.dates import frequency_to_timedelta as to_timedelta
1819
from pydantic import BaseModel
1920

2021
from anemoi.inference.config import Configuration
@@ -86,6 +87,12 @@ def execute(self) -> None:
8687
if self.config.description is not None:
8788
LOG.info("%s", self.config.description)
8889

90+
lead_time = to_timedelta(self.config.lead_time)
91+
92+
# This may be used by Output objects to compute the step
93+
self.lead_time = lead_time
94+
self.time_step = self.checkpoint.timestep
95+
8996
input = self.create_input()
9097
output = self.create_output()
9198

@@ -104,7 +111,7 @@ def execute(self) -> None:
104111
output.open(state)
105112
output.write_initial_state(state)
106113

107-
for state in self.run(input_state=input_state, lead_time=self.config.lead_time):
114+
for state in self.run(input_state=input_state, lead_time=lead_time):
108115
for processor in post_processors:
109116
LOG.info("Post processor: %s", processor)
110117
state = processor.process(state)

0 commit comments

Comments
 (0)