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

401 convert bruker100 #433

Merged
merged 8 commits into from
Dec 31, 2020
Merged
Show file tree
Hide file tree
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
47 changes: 34 additions & 13 deletions fabio/bruker100image.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@
logger = logging.getLogger(__name__)

from .brukerimage import BrukerImage
from .readbytestream import readbytestream
from .fabioutils import pad, StringTypes


Expand Down Expand Up @@ -167,7 +166,6 @@ class Bruker100Image(BrukerImage):

def __init__(self, data=None, header=None):
BrukerImage.__init__(self, data, header)
self.nover_one = self.nover_two = 0

def _readheader(self, infile):
"""
Expand Down Expand Up @@ -216,11 +214,12 @@ def read(self, fname, frame=None):

Data is stored in three blocks:

- data (uint8)
- overflow (uint32)
- underflow (int32).
- data (uint8 mainly when bpp=1), possibly int32 if bpp=4
- overflow1 (2 bytes/value in uint16)
- overflow2 (4 bytes/value in int32)
- underflow (int8, int16 or int32).

The blocks are zero padded to a multiple of 16 bits.
The blocks are zero padded to a multiple of 16 bytes.
"""
with self._open(fname, "rb") as infile:
self._readheader(infile)
Expand All @@ -233,7 +232,8 @@ def read(self, fname, frame=None):
# The total size is nbytes * nrows * ncolumns.

data_size = rows* cols* npixelb
data_size_padded = mround(data_size, 512)
# data_size_padded = mround(data_size, 512)
data_size_padded = data_size
raw_data = infile.read(data_size_padded)
data = numpy.frombuffer(raw_data[:data_size], dtype=self.bpp_to_numpy[npixelb]).reshape((rows, cols))
# self.data = readbytestream(infile, infile.tell(), rows, cols, npixelb,
Expand All @@ -252,11 +252,11 @@ def read(self, fname, frame=None):
for k, nov in enumerate(noverfl_values):
if nov <= 0:
continue
if k > 2:
break
if k == 0:
bpp = int(self.header['NPIXELB'].split()[1])
datatype = numpy.dtype(f"int{bpp*8}")
elif k > 2:
break
else:
bpp = 2* k
datatype = self.bpp_to_numpy[bpp]
Expand All @@ -267,7 +267,7 @@ def read(self, fname, frame=None):
# Multiple of 16 just above
data_str = infile.read(nbytes)
# ar without zeros
ar = numpy.frombuffer(data_str[:to_read], datatype)
ar = numpy.frombuffer(data_str[:to_read], dtype=datatype)
if k == 0:
# read the set of "underflow pixels" - these will be completely disregarded for now
to_merge["underflow"] = ar
Expand Down Expand Up @@ -308,6 +308,8 @@ def gen_header(self):
line += "".join(str(v).ljust(24, ' ') for k, v in enumerate(value.split()) if k < 3)
elif key == "NPIXELB":
line += "".join(str(v).ljust(36, ' ') for k, v in enumerate(value.split()) if k < 2)
elif key in ("NROWS", "NCOLS"):
line += "".join(str(v).ljust(36, ' ') for k, v in enumerate(value.split()) if k < 2)
elif key == "NEXP":
line += "".join(str(v).ljust(72 // 5, ' ') for k, v in enumerate(value.split()) if k < 5)
elif key == "DETTYPE":
Expand Down Expand Up @@ -340,12 +342,13 @@ def gen_header(self):
for i in range(len(headers)):
if headers[i].startswith("HDRBLKS"):
headers[i] = ("HDRBLKS:%s" % self.header["HDRBLKS"]).ljust(80, " ")

res = pad("".join(headers), self.SPACER + "." * 78, 512 * int(self.header["HDRBLKS"]))
return res

def write(self, fname):
"""
Write a bruker image
Write a bruker100 format image

"""
if numpy.issubdtype(self.data.dtype, float):
Expand All @@ -371,7 +374,7 @@ def write(self, fname):
else:
tmp_data = self.data

if (int(self.header.get("NOVERFL", "").split()[0]) ==-1):
if (int(self.header.get("NOVERFL", "-1").split()[0]) == -1):
baseline=False
elif (len(self.header.get("NEXP", "").split()) > 2):
baseline = int(self.header.get("NEXP", "").split()[2])
Expand Down Expand Up @@ -400,13 +403,31 @@ def write(self, fname):
lst = ["1", "1", "0", "0", "0"]
self.header["NEXP"] = " ".join(lst)
self.header["HDRBLKS"] = "5"
if "NROWS" in self.header:
self.header['NROWS'] = self.header['NROWS'].split()
else:
self.header['NROWS'] = [None]
if "NCOLS" in self.header:
self.header['NCOLS'] = self.header['NCOLS'].split()
else:
self.header['NCOLS'] = [None]
self.header['NROWS'][0] = str(tmp_data.shape[0])
self.header['NCOLS'][0] = str(tmp_data.shape[1])
self.header['NROWS'] = " ".join(self.header['NROWS'])
self.header['NCOLS'] = " ".join(self.header['NCOLS'])
bytes_header = self.gen_header().encode("ASCII")
# print(bytes_header.decode())
with self._open(fname, "wb") as bruker:
fast = isinstance(bruker, io.BufferedWriter)
bruker.write(self.gen_header().encode("ASCII"))
bruker.write(bytes_header)
if fast:
data.tofile(bruker)
else:
bruker.write(data.tobytes())
# # 512-Padding
# padded = mround(data.nbytes, 512)
# bruker.write(b"\x00"*(padded - data.nbytes))

for extra in (underflow, overflow1, overflow2):
if extra.nbytes:
if fast:
Expand Down
10 changes: 10 additions & 0 deletions fabio/test/codecs/test_bruker100image.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,16 @@ def test_split_merge(self):
obt = _merge_data(**split)
self.assertTrue(numpy.allclose(obt, ref), f"data are the same, baseline={baseline}")

def test_conversion(self):
fname = UtilsTest.getimage("testcbf.cbf.bz2")[:-4]
c = openimage(fname)
assert "Cbf" in c.__class__.__name__, "This is a CbfImage"
b = c.convert("bruker100")
fname_out = os.path.join(UtilsTest.tempdir, "testcbf2bruker100.sfrm")
b.write(fname_out)
a = openimage(fname_out)
self.assertTrue(numpy.allclose(a.data, c.data), msg="data are the same")


def suite():
loadTests = unittest.defaultTestLoader.loadTestsFromTestCase
Expand Down