1 year ago
#134966
Offset Buffalo
Reading 18 bit binary in Python
I am trying to read 18 bit unsigned integers from a GRIB file in Python 3.
I already found the following code for this on 12 bit values, from the pupygrib package:
def read_uint12(data: NDArray[np.uint8]) -> NDArray[np.uint16]:
fst_uint8, mid_uint8, lst_uint8 = (
np.resize(data, (math.ceil(len(data) / 3), 3)).astype(np.uint16).T
)
fst_uint12 = (fst_uint8 << 4) + (mid_uint8 >> 4)
snd_uint12 = ((mid_uint8 % 16) << 8) + lst_uint8
output: NDArray[np.uint16] = np.concatenate(
(fst_uint12[:, None], snd_uint12[:, None]), axis=1
)
output.resize(2 * len(data) // 3, refcheck=False)
return output
This is based on @cyrilgaudefroy's post on this tread: Python: reading 12-bit binary files. How would this be possible for 18 bit integers? I'm not that experienced in these field with bits bytes, and all.
python
integer
bit
unsigned
grib
0 Answers
Your Answer