diff --git a/src/climetlab/readers/grib/output.py b/src/climetlab/readers/grib/output.py index 3b45cff4..c7438104 100644 --- a/src/climetlab/readers/grib/output.py +++ b/src/climetlab/readers/grib/output.py @@ -10,6 +10,7 @@ import datetime import logging import re +from io import IOBase from climetlab.decorators import normalize from climetlab.decorators import normalize_grib_keys @@ -61,9 +62,16 @@ def __getitem__(self, key): class GribOutput: - def __init__(self, filename, split_output=False, template=None, **kwargs): + def __init__(self, file, split_output=False, template=None, **kwargs): self._files = {} - self.filename = filename + self.fileobj = None + self.filename = None + + if isinstance(file, IOBase): + self.fileobj = file + split_output = False + else: + self.filename = file if split_output: self.split_output = re.findall(r"\{(.*?)\}", self.filename) @@ -80,6 +88,9 @@ def _normalize_kwargs_names(self, **kwargs): return kwargs def f(self, handle): + if self.fileobj: + return self.fileobj, None + if self.split_output: path = self.filename.format(**{k: handle.get(k) for k in self.split_output}) else: