Skip to content

Commit fb1fd11

Browse files
committed
Refactor FileOutput to inherit from FileOutputBase
1 parent 58fc061 commit fb1fd11

File tree

1 file changed

+22
-14
lines changed

1 file changed

+22
-14
lines changed

src/ai_models/outputs/__init__.py

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -25,29 +25,31 @@ def finalise(self, *args, **kwargs):
2525
pass
2626

2727

28-
class FileOutput(Output):
28+
class FileOutputBase(Output):
2929
def __init__(self, owner, path, metadata, **kwargs):
3030
self._first = True
3131
metadata.setdefault("stream", "oper")
3232
metadata.setdefault("expver", owner.expver)
3333
metadata.setdefault("class", "ml")
3434

35-
LOG.info("Writing results to %s.", path)
3635
self.path = path
3736
self.owner = owner
3837
self.metadata = metadata
3938

4039
@cached_property
41-
def output(self):
42-
40+
def grib_keys(self):
4341
edition = self.metadata.pop("edition", 2)
4442

45-
self.grib_keys = dict(
43+
_grib_keys = dict(
4644
edition=edition,
4745
generatingProcessIdentifier=self.owner.version,
4846
)
49-
self.grib_keys.update(self.metadata)
47+
_grib_keys.update(self.metadata)
48+
49+
return _grib_keys
5050

51+
@cached_property
52+
def output(self):
5153
return cml.new_grib_output(
5254
self.path,
5355
split_output=True,
@@ -91,6 +93,20 @@ def write(self, data, *args, check=False, **kwargs):
9193
return handle, path
9294

9395

96+
class FileOutput(FileOutputBase):
97+
def __init__(self, *args, **kwargs):
98+
super().__init__(*args, **kwargs)
99+
LOG.info("Writing results to %s", self.path)
100+
101+
102+
class NoneOutput(Output):
103+
def __init__(self, *args, **kwargs):
104+
LOG.info("Results will not be written.")
105+
106+
def write(self, *args, **kwargs):
107+
pass
108+
109+
94110
class HindcastReLabel:
95111
def __init__(self, owner, output, hindcast_reference_year=None, hindcast_reference_date=None, **kwargs):
96112
self.owner = owner
@@ -151,14 +167,6 @@ def write(self, *args, **kwargs):
151167
return self.output.write(*args, **kwargs)
152168

153169

154-
class NoneOutput(Output):
155-
def __init__(self, *args, **kwargs):
156-
LOG.info("Results will not be written.")
157-
158-
def write(self, *args, **kwargs):
159-
pass
160-
161-
162170
def get_output(name, owner, *args, **kwargs):
163171
result = available_outputs()[name].load()(owner, *args, **kwargs)
164172
if kwargs.get("hindcast_reference_year") is not None or kwargs.get("hindcast_reference_date") is not None:

0 commit comments

Comments
 (0)