Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Robust writing of with missing header values #76

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 20 additions & 14 deletions src/pye57/e57.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@
class WindowsError(OSError):
pass

def get_attr_try(object, attribute, default):
"""wraps get_attr_try in try to avoid libe57 exception when attribute is missing"""
try:
return getattr(object, attribute, default)
except libe57.E57Exception:
return default

SUPPORTED_CARTESIAN_POINT_FIELDS = {
"cartesianX": "d",
Expand Down Expand Up @@ -223,17 +229,17 @@ def write_scan_raw(self, data: Dict, *, name=None, rotation=None, translation=No
raise ValueError("Unsupported point field: %s" % field)

if rotation is None:
rotation = getattr(scan_header, "rotation", np.array([1, 0, 0, 0]))
rotation = get_attr_try(scan_header, "rotation", np.array([1, 0, 0, 0]))

if translation is None:
translation = getattr(scan_header, "translation", np.array([0, 0, 0]))
translation = get_attr_try(scan_header, "translation", np.array([0, 0, 0]))

if name is None:
name = getattr(scan_header, "name", "Scan %s" % len(self.data3d))
name = get_attr_try(scan_header, "name", "Scan %s" % len(self.data3d))

temperature = getattr(scan_header, "temperature", 0)
relativeHumidity = getattr(scan_header, "relativeHumidity", 0)
atmosphericPressure = getattr(scan_header, "atmosphericPressure", 0)
temperature = get_attr_try(scan_header, "temperature", 0)
relativeHumidity = get_attr_try(scan_header, "relativeHumidity", 0)
atmosphericPressure = get_attr_try(scan_header, "atmosphericPressure", 0)

scan_node = libe57.StructureNode(self.image_file)
scan_node.set("guid", libe57.StringNode(self.image_file, "{%s}" % uuid.uuid4()))
Expand Down Expand Up @@ -265,8 +271,8 @@ def write_scan_raw(self, data: Dict, *, name=None, rotation=None, translation=No
scan_node.set("indexBounds", ibox)

if "intensity" in data:
int_min = getattr(scan_header, "intensityMinimum", np.min(data["intensity"]))
int_max = getattr(scan_header, "intensityMaximum", np.max(data["intensity"]))
int_min = get_attr_try(scan_header, "intensityMinimum", np.min(data["intensity"]))
int_max = get_attr_try(scan_header, "intensityMaximum", np.max(data["intensity"]))
intbox = libe57.StructureNode(self.image_file)
intbox.set("intensityMinimum", libe57.FloatNode(self.image_file, int_min))
intbox.set("intensityMaximum", libe57.FloatNode(self.image_file, int_max))
Expand Down Expand Up @@ -323,10 +329,10 @@ def write_scan_raw(self, data: Dict, *, name=None, rotation=None, translation=No
translation_node.set("z", libe57.FloatNode(self.image_file, translation[2]))
pose_node.set("translation", translation_node)

start_datetime = getattr(scan_header, "acquisitionStart_dateTimeValue", 0)
start_atomic = getattr(scan_header, "acquisitionStart_isAtomicClockReferenced", False)
end_datetime = getattr(scan_header, "acquisitionEnd_dateTimeValue", 0)
end_atomic = getattr(scan_header, "acquisitionEnd_isAtomicClockReferenced", False)
start_datetime = get_attr_try(scan_header, "acquisitionStart_dateTimeValue", 0)
start_atomic = get_attr_try(scan_header, "acquisitionStart_isAtomicClockReferenced", False)
end_datetime = get_attr_try(scan_header, "acquisitionEnd_dateTimeValue", 0)
end_atomic = get_attr_try(scan_header, "acquisitionEnd_isAtomicClockReferenced", False)
acquisition_start = libe57.StructureNode(self.image_file)
scan_node.set("acquisitionStart", acquisition_start)
acquisition_start.set("dateTimeValue", libe57.FloatNode(self.image_file, start_datetime))
Expand Down Expand Up @@ -376,9 +382,9 @@ def write_scan_raw(self, data: Dict, *, name=None, rotation=None, translation=No
max_row = np.max(data["rowIndex"])
min_col = np.min(data["columnIndex"])
max_col = np.max(data["columnIndex"])
points_prototype.set("rowIndex", libe57.IntegerNode(self.image_file, 0, min_row, max_row))
points_prototype.set("rowIndex", libe57.IntegerNode(self.image_file, min_row, min_row, max_row))
field_names.append("rowIndex")
points_prototype.set("columnIndex", libe57.IntegerNode(self.image_file, 0, min_col, max_col))
points_prototype.set("columnIndex", libe57.IntegerNode(self.image_file, min_col, min_col, max_col))
field_names.append("columnIndex")

if "cartesianInvalidState" in data:
Expand Down
Loading