From b5a171d748e5e0bc5914632275e4f3bab7ce87cb Mon Sep 17 00:00:00 2001 From: Gert Mertes Date: Wed, 19 Jun 2024 14:15:37 +0000 Subject: [PATCH] Let GribOutput take a filename or file object --- src/climetlab/readers/grib/output.py | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) 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: