From 59a99a8f0e38e2241d6e22658dffd80fa252f968 Mon Sep 17 00:00:00 2001 From: Gabriele Girelli Date: Thu, 13 Aug 2020 19:51:26 +0200 Subject: [PATCH] Update reader.py --- nd2reader/reader.py | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/nd2reader/reader.py b/nd2reader/reader.py index 8246125..e711ca3 100644 --- a/nd2reader/reader.py +++ b/nd2reader/reader.py @@ -13,16 +13,12 @@ class ND2Reader(FramesSequenceND): class_priority = 12 - def __init__(self, filename): + def __init__(self, fh): super(ND2Reader, self).__init__() - if not filename.endswith(".nd2"): - raise InvalidFileType("The file %s you want to read with nd2reader does not have extension .nd2." % filename) + self._fh = fh + self.filename = "" - self.filename = filename - - # first use the parser to parse the file - self._fh = open(filename, "rb") self._parser = Parser(self._fh) # Setup metadata @@ -37,6 +33,16 @@ class ND2Reader(FramesSequenceND): # Other properties self._timesteps = None + @staticmethod + def from_file(filename): + if not filename.endswith(".nd2"): + raise InvalidFileType("The file %s you want to read with nd2reader does not have extension .nd2." % filename) + + nd2r = ND2Reader(open(filename, "rb")) + nd2r.filename = filename + + return nd2r + @classmethod def class_exts(cls): """Let PIMS open function use this reader for opening .nd2 files