Repository navigation
Add HDF5 Blosc2 filter (id 32026) to libblosc2-jna #1621
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
90 changes: 90 additions & 0 deletions
90
.../libblosc2-jna/src/main/java/edu/ucar/unidata/compression/jna/libblosc2/Blosc2Filter.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,90 @@ | ||
| /* | ||
| * Copyright (c) 2026 University Corporation for Atmospheric Research/Unidata | ||
| * See LICENSE for license information. | ||
| */ | ||
|
|
||
| package edu.ucar.unidata.compression.jna.libblosc2; | ||
|
|
||
| import java.io.IOException; | ||
| import java.util.Arrays; | ||
| import java.util.HashMap; | ||
| import java.util.Map; | ||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
| import ucar.nc2.filter.Filter; | ||
| import ucar.nc2.filter.FilterProvider; | ||
| import ucar.nc2.filter.Filters; | ||
|
|
||
| /** | ||
| * Decoder for the HDF5 Blosc2 filter (id 32026). | ||
| * <p> | ||
| * The filter parameters ({@code cd_values}) are set by the HDF5 plugin ({@code H5Zblosc2.c}): | ||
| * {@code [0]} filter revision, {@code [1]} Blosc2 version, {@code [2]} type size, {@code [3]} chunk size in | ||
| * bytes, {@code [4]} compression level, {@code [5]} shuffle, {@code [6]} codec, and for chunks of rank 2 or | ||
| * more {@code [7]} rank followed by the chunk shape. Only {@code [3]} is needed for decoding; shuffle and | ||
| * codec are recorded in each frame and handled by C-Blosc2. | ||
| */ | ||
| public class Blosc2Filter extends Filter { | ||
| private static final Logger logger = LoggerFactory.getLogger(Blosc2Filter.class); | ||
| private static final String name = "blosc2"; | ||
| private static final int id = 32026; | ||
| private static final int CD_CHUNK_BYTES = 3; | ||
|
|
||
| private final Map<String, Object> properties; | ||
| private final int chunkBytes; | ||
|
|
||
| public Blosc2Filter(Map<String, Object> properties) { | ||
| this.properties = new HashMap<>(properties); | ||
| Object data = this.properties.get(Filters.Keys.DATA); | ||
| this.chunkBytes = | ||
| (data instanceof int[] && ((int[]) data).length > CD_CHUNK_BYTES) ? ((int[]) data)[CD_CHUNK_BYTES] : -1; | ||
| logger.debug("Blosc2 properties = {}", this.properties); | ||
| } | ||
|
|
||
| @Override | ||
| public String getName() { | ||
| return name; | ||
| } | ||
|
|
||
| @Override | ||
| public int getId() { | ||
| return id; | ||
| } | ||
|
|
||
| @Override | ||
| public byte[] encode(byte[] dataIn) { | ||
| throw new UnsupportedOperationException("Blosc2 encoding is not supported"); | ||
| } | ||
|
|
||
| @Override | ||
| public byte[] decode(byte[] dataIn) throws IOException { | ||
| if (chunkBytes <= 0) { | ||
| Object data = properties.get(Filters.Keys.DATA); | ||
| throw new IOException("Blosc2 filter parameters do not contain the chunk size (cd_values[3]): " | ||
| + (data instanceof int[] ? Arrays.toString((int[]) data) : data)); | ||
| } | ||
| return LibBlosc2.decodeFrame(dataIn, chunkBytes); | ||
| } | ||
|
|
||
| public Map<String, Object> properties() { | ||
| return new HashMap<>(properties); | ||
| } | ||
|
|
||
| public static class Provider implements FilterProvider { | ||
|
|
||
| @Override | ||
| public String getName() { | ||
| return name; | ||
| } | ||
|
|
||
| @Override | ||
| public int getId() { | ||
| return id; | ||
| } | ||
|
|
||
| @Override | ||
| public Filter create(Map<String, Object> properties) { | ||
| return new Blosc2Filter(properties); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 2 additions & 1 deletion
3
...ression/libblosc2-jna/src/main/resources/META-INF/services/ucar.nc2.filter.FilterProvider
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,2 @@ | ||
| edu.ucar.unidata.compression.jna.libblosc2.BloscFilter$Provider | ||
| edu.ucar.unidata.compression.jna.libblosc2.BloscFilter$Provider | ||
| edu.ucar.unidata.compression.jna.libblosc2.Blosc2Filter$Provider |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| # Blosc2 (HDF5 filter 32026) test data | ||
|
|
||
| | File | Content | | ||
| |---|---| | ||
| | `blosc2_raw.nc` | netCDF-4, no filters (reference values) | | ||
| | `blosc2_32026.nc` | same data, every variable compressed with the HDF5 Blosc2 filter (id 32026) | | ||
|
|
||
| Variables (dimensions `time = 12`, `y = 20`, `x = 30`): | ||
|
|
||
| | Variable | Chunks | `_Filter` (user parameters) | Decoding path | | ||
| |---|---|---|---| | ||
| | `double time(time)` | 5 | `32026,0,0,0,0,3,0,5` (level 3, no shuffle, zstd) | rank 1: `blosc2_schunk_decompress_chunk` | | ||
| | `float temp(time, y, x)` | 5, 8, 16 | `32026,0,0,0,0,5,2,5` (level 5, bitshuffle, zstd) | rank 3: `b2nd_to_cbuffer` | | ||
| | `int count(y, x)` | 8, 16 | `32026,0,0,0,0,5,1,1` (level 5, byte shuffle, lz4) | rank 2: `b2nd_to_cbuffer` | | ||
|
|
||
| The chunk sizes leave partial edge chunks in every dimension. | ||
|
|
||
| Values: | ||
|
|
||
| - `time[t] = 6 * t` | ||
| - `temp[t][j][i] = 250 + 0.5 t + 20 sin(0.3 j) cos(0.2 i) + 0.01 i` (written with 4 decimals) | ||
| - `count[j][i] = (31 j + 7 i) % 1000 - 500` | ||
|
|
||
| Generated with netCDF-C 4.9.3, HDF5 1.14.6 and the HDF5 Blosc2 plugin (`H5Zblosc2.c` with C-Blosc2 2.23.1): | ||
| a CDL file with the values above was converted with `ncgen -k nc4`, once as is and once with | ||
| `_ChunkSizes` and `_Filter` attributes added. `ncdump` of both files gives identical data. | ||
|
|
||
| Single chunks (for decoder unit tests), extracted from `blosc2_32026.nc` with `H5Dread_chunk` | ||
| (`.b2frame`, bytes as stored in the file) and `H5Dread` with the HDF5 Blosc2 plugin (`.expected`): | ||
|
|
||
| | File | Chunk | Frame bytes | Decoded bytes | `cd_values` | | ||
| |---|---|---:|---:|---| | ||
| | `blosc2_temp_chunk0.*` | `temp` chunk 0 (rank 3, 5 x 8 x 16 floats) | 1203 | 2560 | `1,0,4,2560,5,2,5,3,5,8,16` | | ||
| | `blosc2_time_chunk0.*` | `time` chunk 0 (rank 1, 5 doubles) | 244 | 40 | `1,0,8,40,3,0,5` | |
Binary file not shown.
Binary file not shown.
Binary file added
BIN
+1.17 KB
native-compression/libblosc2-jna/src/test/data/blosc2_temp_chunk0.b2frame
Binary file not shown.
Binary file added
BIN
+2.5 KB
native-compression/libblosc2-jna/src/test/data/blosc2_temp_chunk0.expected
Binary file not shown.
Binary file added
BIN
+244 Bytes
native-compression/libblosc2-jna/src/test/data/blosc2_time_chunk0.b2frame
Binary file not shown.
Binary file added
BIN
+40 Bytes
native-compression/libblosc2-jna/src/test/data/blosc2_time_chunk0.expected
Binary file not shown.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.