0

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.

  • This question might help: https://stackoverflow.com/questions/1163459/reading-integers-from-binary-file-in-python – James Feb 03 '22 at 22:13
  • 2
    You need to be more specific about the format of the data in the files since the number of bits isn't a multiple of 4 or 8. A sample would be very useful… – martineau Feb 03 '22 at 22:17

0 Answers0