Commit 0f02b368 authored by Carsten Richter's avatar Carsten Richter

Merge branch 'change-qspace-file-open' into 'master'

Change qspace processing input file open pattern

See merge request !112
parents 8ef69a7a 8cef67e9
Pipeline #7087 passed with stages
in 6 minutes and 53 seconds
......@@ -118,23 +118,35 @@ class XsocsH5Base(object):
if prev_mode is not None:
self.mode = prev_mode
def _open(self):
def open(self):
"""Open the file if it is not opened.
This increase the *open counter* by 1.
You MUST call :meth:`close` for each call to this method
"""
if self.__file is None:
self.__file = _h5py.File(self.__h5_f, self.mode)
self.__file_count += 1
def _close(self):
def close(self):
"""Decreases the *open counter* by 1.
If the *open counter* reaches 0, the HDF5 file is closed.
"""
if self.__file_count <= 0:
raise RuntimeError("Cannot close, open counter already 0")
self.__file_count -= 1
if self.__file_count == 0 and self.__file:
self.__file.close()
self.__file = None
def __enter__(self):
self._open()
self.open()
return self
def __exit__(self, *args):
self._close()
self.close()
def _get_scalar_data(self, path):
with self._get_file() as h5_file:
......
......@@ -798,7 +798,7 @@ class QSpaceConverter(object):
sample_x = sample_x[shifted_idx]
sample_y = sample_y[shifted_idx]
for entry_idx, entry in enumerate(entries):
for entry in entries:
entry_file = master_h5.entry_filename(entry)
if not os.path.isabs(entry_file):
base_dir = os.path.dirname(xsocsH5_f)
......@@ -1381,6 +1381,13 @@ def _to_qspace(th_idx,
rc = None
errMsg = None
try:
# Open all input files
inputsH5 = []
for entry_file in entry_files:
h5f = XsocsH5.XsocsH5(entry_file, mode='r')
h5f.open()
inputsH5.append(h5f)
while True:
if term_evt.is_set(): # noqa
rc = QSpaceConverter.CANCELED
......@@ -1404,7 +1411,7 @@ def _to_qspace(th_idx,
image_indices = None
for entry_idx, entry in enumerate(entries):
xsocsH5 = XsocsH5.XsocsH5(entry_files[entry_idx], mode='r')
xsocsH5 = inputsH5[entry_idx]
t0 = time.time()
......@@ -1513,6 +1520,11 @@ def _to_qspace(th_idx,
progress_np[th_idx] = round(100. * (result_idx + 1.) / n_xy)
t_write += time.time() - t0
# Close input files
for h5f in inputsH5:
h5f.close()
except Exception as ex:
if rc is None:
rc = QSpaceConverter.ERROR
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment