Skip to content

Commit

Permalink
Merge pull request #554 from psavery/edf-ge-format
Browse files Browse the repository at this point in the history
LGTM. Thanks for the contribution
  • Loading branch information
kif authored Mar 19, 2024
2 parents d08b8ea + 16f0bf1 commit 564346f
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/fabio/GEimage.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,12 @@
import io
import numpy
import struct
from .edfimage import EdfImage
from .fabioimage import FabioImage
from .fabioutils import next_filename, previous_filename
from .openimage import MAGIC_NUMBERS

EDF_MAGIC_NUMBERS = [(x, y) for x, y in MAGIC_NUMBERS if y == 'edf']

GE_HEADER_INFO = [
# Name, length in bytes, format for struct (None means string)
Expand Down Expand Up @@ -265,6 +269,20 @@ def _readheader(self, infile):
raise IOError("GE file size is incorrect")
nframes = file_size // bytes_per_frames
self.header['NumberOfFrames'] = nframes
elif any(self.header["ImageFormat"].startswith(x) for x, _ in EDF_MAGIC_NUMBERS):
# At APS, they started saving some GE headers with an EDF format in ~2022

# Use the same metadata as the blank header...
self.header.update(self.BLANKED_HEADER_METADATA)

# Read the EDF header
cur = infile.tell()
infile.seek(0)
header = EdfImage._read_header_block(infile, 1)
infile.seek(cur, io.SEEK_SET)

# Extract any needed info from the EDF header
self.header['NumberOfFrames'] = int(header[0]["Num_Images"])

def read(self, fname, frame=None):
"""Read header into self.header and the data into self.data."""
Expand Down
14 changes: 14 additions & 0 deletions src/fabio/openimage.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@

import os.path
import logging
import re
from . import fabioutils
from .compression import ExternalCompressors
from .fabioutils import FilenameObject, BytesIO
from .fabioimage import FabioImage

Expand Down Expand Up @@ -149,6 +151,18 @@ def do_magic(byts, filename):
return "marccd"
else:
return "tif"

if format_type == "edf" and isinstance(filename, str):
# Might be GE with an EDF header. Check the extension.
extension = filename.split(".")[-1]
# If it is a compression extension, check the next one up.
if f'.{extension}' in ExternalCompressors.COMMANDS:
extension = filename.split(".")[-2]

# If the extension is `ge` plus a number, assume it is GE
if re.search(r'^ge\d*$', extension):
return "GE"

return format_type
raise Exception("Could not interpret magic string")

Expand Down
1 change: 1 addition & 0 deletions src/fabio/test/codecs/test_geimage.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ class TestGE(unittest.TestCase):
("GE_aSI_detector_image_1529", (2048, 2048), (1515, 16353, 1833.0311, 56.9124)),
("GE_image_1frame_intact_header.ge", (2048, 2048), (1515, 16353, 2209.1113, 437.6377)),
("GE_image_1frame_blanked_header.ge", (2048, 2048), (1300, 16349, 1886.41111, 117.0603)),
("dark_before_000428.edf.ge5", (2048, 2048), (1300, 16349, 1833.87892, 84.5265)),
]

def setUp(self):
Expand Down

0 comments on commit 564346f

Please sign in to comment.