Commit 31dac622 authored by payno's avatar payno
Browse files

Merge branch 'improve_io_edfconverter' into 'master'

edfconverter: improve file closing management

Closes #80

See merge request !79
parents a91aa312 529bb888
Pipeline #60198 passed with stages
in 6 minutes and 25 seconds
...@@ -36,7 +36,6 @@ __date__ = "27/11/2020" ...@@ -36,7 +36,6 @@ __date__ = "27/11/2020"
from collections import namedtuple from collections import namedtuple
from typing import Optional from typing import Optional
from nxtomomill.converter.hdf5.acquisition.baseacquisition import SourceType from nxtomomill.converter.hdf5.acquisition.baseacquisition import SourceType
from nxtomomill import utils from nxtomomill import utils
from nxtomomill.utils import ImageKey from nxtomomill.utils import ImageKey
...@@ -167,41 +166,51 @@ def edf_to_nx( ...@@ -167,41 +166,51 @@ def edf_to_nx(
indexes = sorted(projections_urls.keys()) indexes = sorted(projections_urls.keys())
first_proj_file = projections_urls[indexes[0]] first_proj_file = projections_urls[indexes[0]]
fid = fabio.open(first_proj_file.file_path()) fid = fabio.open(first_proj_file.file_path())
if hasattr(fid, "header"):
hd = fid.header
else:
hd = fid.getHeader()
try: try:
rotangle_index = ( if hasattr(fid, "header"):
hd[file_keys.motor_mne_key] hd = fid.header
.split(" ") else:
.index(file_keys.rot_angle_key) hd = fid.getHeader()
) try:
except: rotangle_index = (
rotangle_index = -1 hd[file_keys.motor_mne_key]
try: .split(" ")
xtrans_index = ( .index(file_keys.rot_angle_key)
hd[file_keys.motor_mne_key].split(" ").index(file_keys.x_trans_key) )
) except:
except: rotangle_index = -1
xtrans_index = -1 try:
try: xtrans_index = (
ytrans_index = ( hd[file_keys.motor_mne_key]
hd[file_keys.motor_mne_key].split(" ").index(file_keys.y_trans_key) .split(" ")
) .index(file_keys.x_trans_key)
except: )
ytrans_index = -1 except:
try: xtrans_index = -1
ztrans_index = ( try:
hd[file_keys.motor_mne_key].split(" ").index(file_keys.z_trans_key) ytrans_index = (
) hd[file_keys.motor_mne_key]
except: .split(" ")
ztrans_index = -1 .index(file_keys.y_trans_key)
)
except:
ytrans_index = -1
try:
ztrans_index = (
hd[file_keys.motor_mne_key]
.split(" ")
.index(file_keys.z_trans_key)
)
except:
ztrans_index = -1
if hasattr(fid, "bytecode"): if hasattr(fid, "bytecode"):
frame_type = fid.bytecode frame_type = fid.bytecode
else: else:
frame_type = fid.getByteCode() frame_type = fid.getByteCode()
finally:
fid.close()
fid = None
return frame_type, rotangle_index, xtrans_index, ytrans_index, ztrans_index return frame_type, rotangle_index, xtrans_index, ytrans_index, ztrans_index
( (
...@@ -338,19 +347,24 @@ def edf_to_nx( ...@@ -338,19 +347,24 @@ def edf_to_nx(
" for more information)" % url.path() " for more information)" % url.path()
) )
if fabio_file.nframes == 1: try:
if index != 0: if fabio_file.nframes == 1:
raise ValueError( if index != 0:
"Only a single frame available. Slice %s out of range" % index raise ValueError(
) "Only a single frame available. Slice %s out of range"
data = fabio_file.data % index
header = fabio_file.header )
else: data = fabio_file.data
data = fabio_file.getframe(index).data header = fabio_file.header
header = fabio_file.getframe(index).header else:
data = fabio_file.getframe(index).data
fabio_file.close() header = fabio_file.getframe(index).header
fabio_file = None except Exception as e:
data = None
header = None
finally:
fabio_file.close()
fabio_file = None
return data, header return data, header
if progress is not None: if progress is not None:
......
Supports Markdown
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