Commit 10ce947c authored by Pierre Paleo's avatar Pierre Paleo
Browse files

Merge branch 'edf' into 'master'

Support for output EDF format

Closes #211

See merge request !101
parents c7277a15 ab93eeca
Pipeline #40513 failed with stages
in 4 minutes and 24 seconds
...@@ -11,6 +11,10 @@ from ..misc.utils import rescale_data ...@@ -11,6 +11,10 @@ from ..misc.utils import rescale_data
from .config import export_dict_to_h5 from .config import export_dict_to_h5
from .utils import check_h5py_version # won't be necessary once h5py >= 3.0 required from .utils import check_h5py_version # won't be necessary once h5py >= 3.0 required
try:
from silx.third_party.EdfFile import EdfFile
except ImportError:
EdfFile = None
try: try:
from glymur import Jp2k from glymur import Jp2k
__have_jp2k__ = True __have_jp2k__ = True
...@@ -282,6 +286,57 @@ class TIFFWriter(Writer): ...@@ -282,6 +286,57 @@ class TIFFWriter(Writer):
return path.dirname(self.fname) return path.dirname(self.fname)
class EDFWriter(Writer):
def __init__(self, fname, start_index=0, filemode="w"):
"""
EDF (ESRF Data Format) writer.
Parameters
-----------
fname: str
Path to the output file name
start_index: int, optional
When writing a stack of images, each image is written in a dedicated file
In this case, the output is a series of files `filename_0000.tif`,
`filename_0001.edf`, etc. This parameter is the starting index for
file names.
"""
super().__init__(fname)
self.filemode = filemode
self.start_index = start_index
def _write_edf(self, data, config=None, filename=None, filemode=None):
if filename is None:
filename = self.fname
edf = EdfFile(filename)
config = config or {}
config["software"] = str("nabu %s" % version)
config["date"] = get_datetime()
edf.WriteImage(
config,
data
)
edf = None
def write(self, data, *args, config=None, **kwargs):
if data.ndim < 3:
self._write_edf(data, config=config)
return
if (data.ndim == 3) and (data.shape[0] == 1):
self._write_edf(data[0], config=config)
return
dirname, rel_filename = path.split(self.fname)
prefix, ext = path.splitext(rel_filename)
for i in range(data.shape[0]):
curr_rel_filename = prefix + str("_%04d" % (self.start_index + i)) + ext
fname = path.join(dirname, curr_rel_filename)
self._write_edf(data[i], filename=fname, config=config)
def get_filename(self):
return path.dirname(self.fname)
class JP2Writer(Writer): class JP2Writer(Writer):
def __init__(self, fname, start_index=0, filemode="wb", psnr=None, auto_convert=True): def __init__(self, fname, start_index=0, filemode="wb", psnr=None, auto_convert=True):
""" """
...@@ -379,5 +434,6 @@ Writers = { ...@@ -379,5 +434,6 @@ Writers = {
"j2k": JP2Writer, "j2k": JP2Writer,
"jp2": JP2Writer, "jp2": JP2Writer,
"jp2k": JP2Writer, "jp2k": JP2Writer,
"edf": EDFWriter,
} }
...@@ -309,7 +309,7 @@ nabu_config = { ...@@ -309,7 +309,7 @@ nabu_config = {
}, },
"file_format": { "file_format": {
"default": "hdf5", "default": "hdf5",
"help": "Output file format. Available are: hdf5, tiff, jp2", "help": "Output file format. Available are: hdf5, tiff, jp2, edf",
"validator": output_file_format_validator, "validator": output_file_format_validator,
"type": "optional", "type": "optional",
}, },
......
...@@ -92,6 +92,7 @@ files_formats = { ...@@ -92,6 +92,7 @@ files_formats = {
"jp2k": "jp2", "jp2k": "jp2",
"j2k": "jp2", "j2k": "jp2",
"jpeg2000": "jp2", "jpeg2000": "jp2",
"edf": "edf",
} }
class FileFormat(Enum): class FileFormat(Enum):
EDF = "edf" EDF = "edf"
......
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