Example #1
0
    def decode(self, compressed, decompressed=None):
        """perform decompression

        params:
            compressed: bytes - the data to be decompressed
            decompressed: numpy.ndarray - memory to be used to decompress data into
        """
        compressed_lp = None
        decompressed_lp = None
        try:
            compressed_lp = pressio.io_data_from_bytes(compressed)
            decompressed_lp = pressio.io_data_from_numpy(decompressed)

            rc = pressio.compressor_decompress(self._compressor, compressed_lp,
                                               decompressed_lp)
            if rc:
                raise PressioException.from_compressor(self._compressor)

            dec = pressio.io_data_to_numpy(decompressed_lp)

            if decompressed is not None:
                return dec
            else:
                return ndarray_copy(dec, decompressed)
        finally:
            pressio.data_free(compressed_lp)
            pressio.data_free(decompressed_lp)
Example #2
0
    def decode(self, buf, out=None):
        # normalise input
        enc = ensure_ndarray(buf).view('u1')

        # flatten to simplify implementation
        enc = enc.reshape(-1, order='A')

        # find out how many bits were padded
        n_bits_padded = int(enc[0])

        # apply decoding
        dec = np.unpackbits(enc[1:])

        # remove padded bits
        if n_bits_padded:
            dec = dec[:-n_bits_padded]

        # view as boolean array
        dec = dec.view(bool)

        # given a flattened version of what was originally an nxmx2 array,
        # reshape to group adjacent bits in second dimension and
        # convert back to int based on each little-endian bit pair
        dec = np.packbits(dec.reshape((-1, 2)), bitorder='little', axis=1)
        dec = dec.squeeze(axis=1)

        # handle destination
        return ndarray_copy(dec, out)
Example #3
0
    def decode(self, buf, out=None):
        buf = ensure_contiguous_ndarray(buf)

        if out is not None:
            out = ensure_contiguous_ndarray(out)

        tiled = jpeg_decode(buf)
        return ndarray_copy(tiled, out)
Example #4
0
    def decode(self, chunk, out=None):

        len_header, chunk_shape = self._read_header(chunk)
        chunk = chunk[len_header:]

        if out is not None:

            # out should only be used if we read a complete chunk
            assert chunk_shape == self.chunk_shape, (
                "Expected chunk of shape {}, found {}".format(
                    self.chunk_shape,
                    chunk_shape))

            if self._compressor:
                self._compressor.decode(chunk, out)
            else:
                ndarray_copy(chunk, out)

            # we can byteswap in-place
            if self._little_endian:
                out.byteswap(True)

            return out

        else:

            if self._compressor:
                chunk = self._compressor.decode(chunk)

            # more expensive byteswap
            chunk = self._from_big_endian(chunk)

            # read partial chunk
            if chunk_shape != self.chunk_shape:  # pragma: no cover
                chunk = np.frombuffer(chunk, dtype=self.dtype)
                chunk = chunk.reshape(chunk_shape)
                complete_chunk = np.zeros(self.chunk_shape, dtype=self.dtype)
                target_slices = tuple(slice(0, s) for s in chunk_shape)
                complete_chunk[target_slices] = chunk
                chunk = complete_chunk

            return chunk
Example #5
0
    def decode(self, buf, out=None):
        buf = ensure_contiguous_ndarray(buf)
        fn = tempfile.mktemp(suffix="grib2")
        buf.tofile(fn)

        # do decode
        ds = cfgrib.open_file(fn)
        data = ds.variables[self.var].data
        if hasattr(data, "build_array"):
            data = data.build_array()

        if out is not None:
            return ndarray_copy(data, out)
        else:
            return data
Example #6
0
    def decode(self,
               buf: np.ndarray,
               out: Optional[np.ndarray] = None) -> np.ndarray:
        dec = super().decode(buf, out=None)

        # given a flattened version of what was originally an nxmx2 array,
        # reshape to group adjacent bits in second dimension and
        # convert back to int based on each little-endian bit pair
        dec = np.packbits(dec.reshape((-1, 2)), bitorder="little", axis=1)
        dec = dec.squeeze(axis=1)

        # -1 which codes for missing data got encoded as 11000000 which codes for 3 in uint8,
        # we revert that here
        dec[dec == 3] = -1

        # handle destination
        return ndarray_copy(dec, out)
Example #7
0
    def decode(self, buf, out=None):

        # normalise inputs
        buf = ensure_contiguous_ndarray(buf)
        if out is not None:
            out = ensure_contiguous_ndarray(out)

        # N.B., bz2 cannot handle ndarray directly because of truth testing issues
        buf = memoryview(buf)

        # do decompression
        dec = _bz2.decompress(buf)

        # handle destination - Python standard library bz2 module does not
        # support direct decompression into buffer, so we have to copy into
        # out if given
        return ndarray_copy(dec, out)
Example #8
0
    def decode(self, buf, out=None):
        """The method to decode a jpeg image into a raw format.

        Parameters
        ----------
        buf : contiguous_ndarray
            The jpeg image to be decoded into raw format.
        out : contiguous_ndarray, optional
            Another location to write the raw image to.

        Returns
        -------
        ndarray
            The image in raw format

        """

        buf = ensure_contiguous_ndarray(buf)
        if out is not None:
            out = ensure_contiguous_ndarray(out)
        tiled = jpeg_decode(buf)
        return ndarray_copy(tiled, out)