Commit 9ccda136 authored by Thomas Vincent's avatar Thomas Vincent

Allow to only save a ROI of the images during the merge

parent 76006110
......@@ -30,6 +30,7 @@ __authors__ = ["D. Naudet"]
__date__ = "20/04/2016"
__license__ = "MIT"
import logging
import re
import copy
import os.path
......@@ -46,6 +47,8 @@ from silx.third_party import EdfFile
from ...io import XsocsH5
_logger = logging.getLogger(__name__)
class KmapMerger(object):
(READY, RUNNING, DONE,
ERROR, CANCELED, UNKNOWN) = __STATUSES = range(6)
......@@ -121,6 +124,8 @@ class KmapMerger(object):
self.__no_img_ids = sorted(self.__no_img_scans)
self.__on_error_ids = sorted(self.__on_error_scans)
self.__image_roi = None
self.__results = None
self.__master = None
self.__shared_progress = None
......@@ -274,7 +279,8 @@ class KmapMerger(object):
self.beam_energy,
self.chan_per_deg,
self.center_chan,
self.compression)
self.compression,
self.image_roi)
results[scan_id] = pool.apply_async(_add_edf_data,
args,
callback=callback)
......@@ -527,6 +533,23 @@ class KmapMerger(object):
else:
self.__n_proc = n_proc
image_roi = property(lambda self: self.__image_roi)
""" Image ROI (origin_row, origin_column, height, width) to save.
If None (the default) the whole image is saved
"""
@image_roi.setter
def image_roi(self, roi):
if roi is not None:
if len(roi) != 4:
raise ValueError('Image ROI expects 4 positive integers')
for i in roi:
if not isinstance(i, int) or i < 0:
raise ValueError('Image ROI expects 4 positive integers')
self.__image_roi = roi
def get_imagefile_info(self, scan_id, key=None):
try:
......@@ -685,7 +708,8 @@ def _add_edf_data(scan_id,
beam_energy,
chan_per_deg,
center_chan,
compression):
compression,
image_roi):
"""
Creates an entry_*.h5 file with scan data from the provided
......@@ -745,6 +769,21 @@ def _add_edf_data(scan_id,
image = edf_file.GetData(0)
dtype = image.dtype
img_shape = image.shape
row, column = 0, 0 # Offset in images
if image_roi: # Use ROI and clip it with image shape
row, column, height, width = image_roi
if row >= img_shape[0] or column >= img_shape[1]:
raise ValueError('Image ROI defined outside image')
img_shape = (min(img_shape[0] - row, height),
min(img_shape[1] - column, width))
if img_shape[0] != height or img_shape[1] != width:
_logger.warning(
'Image ROI clipped: It was larger than images')
entry_h5f.set_image_offset((row, column), entry)
dset_shape = (n_images, img_shape[0], img_shape[1])
chunks = (1, dset_shape[1]//4, dset_shape[2]//4)
......@@ -763,7 +802,8 @@ def _add_edf_data(scan_id,
''.format(scan_id))
data = edf_file.GetData(i)
image_dset[i, :, :] = data
image_dset[i, :, :] = data[row:row+img_shape[0],
column:column+img_shape[1]]
except Exception as ex:
print(ex)
......
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