Added fromFile() method to overcome problem of multiple times opening same file

This commit is contained in:
Anne de Jong 2023-12-19 14:03:46 +01:00
parent 936f2d5708
commit 311a1274bf

View File

@ -970,6 +970,22 @@ class Measurement:
wavfile.write(fn, int(self.samplerate), data.astype(newtype)) wavfile.write(fn, int(self.samplerate), data.astype(newtype))
@staticmethod
def fromFile(fn):
"""
Try to open measurement from a given file name. First checks
whether the measurement is already open. Otherwise it might
happen that a Measurement object is created twice for the same backing file, which we do not allow.
"""
# See if the base part of the filename is referring to a file that is already open
with h5.File(fn, 'r') as f:
uuid = f.attrs['UUID']
if uuid in Measurement.uuid_s.keys():
return Measurement.uuid_s[uuid]
return Measurement(fn)
@staticmethod @staticmethod
def fromtxt( def fromtxt(
fn, fn,