| 1 |
# H5Z-SPERR |
| 2 |
This is an HDF5 Plugin for the [SPERR](https://github.com/ncar/sperr) compressor. |
| 3 |
It is registered with the HDF Group with a plugin ID of `32028`. |
| 4 |
|
| 5 |
## Build and Install |
| 6 |
Needless to say, `H5Z-SPERR` depends on both `HDF5` and `SPERR`. |
| 7 |
After HDF5 and SPERR are installed, `H5Z-SPERR` can be configured and built using `cmake`: |
| 8 |
```bash |
| 9 |
export HDF5_ROOT=/path/to/your/preferred/HDF5/installation |
| 10 |
mkdir build-H5-SPERR && cd build-H5-SPERR |
| 11 |
cmake -DSPERR_INSTALL_DIR=/path/to/SPERR/installation \ |
| 12 |
-DCMAKE_INSTALL_PREFIX=/path/to/install/this/plugin \ |
| 13 |
/path/to/H5Z-SPERR/source/code |
| 14 |
make install |
| 15 |
``` |
| 16 |
The plugin library file `libh5z-sperr.so` will be placed at directory `/path/to/install/this/plugin`. |
| 17 |
|
| 18 |
## Use As a Dynamically Loaded Plugin |
| 19 |
Using the [dynamically loaded plugin](https://docs.hdfgroup.org/hdf5/rfc/HDF5DynamicallyLoadedFilters.pdf) mechanism by HDF5, |
| 20 |
one may use `H5Z-SPERR` by simply setting the environment variable `HDF5_PLUGIN_PATH` to the directory containing the plugin |
| 21 |
library file: |
| 22 |
```bash |
| 23 |
export HDF5_PLUGIN_PATH=/path/to/install/this/plugin |
| 24 |
``` |
| 25 |
The user program does not need to link to this plugin or SPERR; it only needs to specify the plugin ID of `32028`. |
| 26 |
|
| 27 |
<!-- |
| 28 |
## Use in NetCDF-4 APIs |
| 29 |
`H5Z-SPERR` also facilitates the application of SPERR compression on |
| 30 |
[NetCDF-4 files](https://docs.unidata.ucar.edu/netcdf/NUG/md_filters.html#filters_enable); |
| 31 |
one simply needs to define the filter on a variable: |
| 32 |
```C |
| 33 |
nc_def_var_filter(ncid, varid, 32028, 1, &cd_values); |
| 34 |
``` |
| 35 |
See a complete example [here](https://github.com/NCAR/H5Z-SPERR/blob/main/example/simple_xy_nc4_wr.c). |
| 36 |
--> |
| 37 |
|
| 38 |
## Use in Python |
| 39 |
`H5Z-SPERR` version `0.1.3` is supported by the Python package [hdf5plugin](https://github.com/silx-kit/hdf5plugin) |
| 40 |
since version `5.0.0`. |
| 41 |
One can install the package by issuing |
| 42 |
```bash |
| 43 |
pip install hdf5plugin [--user] # using pip |
| 44 |
conda install -c conda-forge hdf5plugin # using conda |
| 45 |
``` |
| 46 |
and use it by importing these two packages: |
| 47 |
```python |
| 48 |
import h5py # provide general HDF5 support |
| 49 |
import hdf5plugin # provide HDF5 plugin support |
| 50 |
``` |
| 51 |
|
| 52 |
## Handling of Missing Values |
| 53 |
Simulation models sometimes use a special value (i.e., missing value) to indicate that there's no meaningful value at that specific location. |
| 54 |
For example, in an ocean simulation, all the land area is marked by missing values. |
| 55 |
The most often seen missing values are either `NaN`, or an extremely large value, such as `1e35` or `-9.9e35`. |
| 56 |
When these missing values participate in compression, they easily introduce numeric error and result in data corruption. |
| 57 |
|
| 58 |
`H5Z-SPERR` can handle common missing values with a little help from the user. |
| 59 |
Specifically, a user can indicate that there's potentially missing values, |
| 60 |
and `H5Z-SPERR` will use a compact bitmask to keep track of where exactly those missing values are. |
| 61 |
`H5Z-SPERR` then replaces them with a value that is friendly to SPERR compression before passing the field to the SPERR compressor. |
| 62 |
|
| 63 |
During decompression, `H5Z-SPERR` fills the original missing value at locations indicated by the compact bitmask. |
| 64 |
Specifically, if the original missing values are `NaN`, then `NaN` will be filled. If the orignal missing values |
| 65 |
are values with a magnitude larger than `1e35`, then the *first occurance* of such values will be used to fill |
| 66 |
in all missing value locations. |
| 67 |
|
| 68 |
Users use an integer to indicate the potential existance of missing values: |
| 69 |
- Mode `0`: no missing values; |
| 70 |
- Mode `1`: there are potential `NaN`s; |
| 71 |
- Mode `2`: there are potential values with a magnitude larger than `1e35`. |
| 72 |
|
| 73 |
`H5Z-SPERR` behaves accordingly: (`1e35` denotes the *first occurance* of such values) |
| 74 |
| Mode | Actual Input Data | Filter Behavior | |
| 75 |
|-----------|----------------------|------------------| |
| 76 |
| 0 | No `NaN`, no `1e35` | :heavy_check_mark: Normal SPERR compression | |
| 77 |
| 0 | Has `NaN` or `1e35` | :x: Likely numeric error | |
| 78 |
| 1 | No `NaN`, no `1e35` | :heavy_check_mark: Normal SPERR compression | |
| 79 |
| 1 | Has `NaN`, no `1e35` | :heavy_check_mark: Normal SPERR compression; `NaN` is restored at its exact locations | |
| 80 |
| 1 | Regardless of `NaN`, has `1e35` | :x: Likely numeric error | |
| 81 |
| 2 | No `NaN`, no `1e35` | :heavy_check_mark: Normal SPERR compression | |
| 82 |
| 2 | No `NaN`, has `1e35` | :heavy_check_mark: Normal SPERR compression; `1e35` is restored at its exact locations | |
| 83 |
| 2 | Has `NaN`, regardless of `1e35` | :x: Likely numeric error | |
| 84 |
|
| 85 |
**Final note:** if a variable is indicated to have missing values, but it actually does not, then there's no bitmasks involved thus no storage overhead! |
| 86 |
|
| 87 |
## Find `cd_values[]` |
| 88 |
To apply SPERR compression using the HDF5 plugin, one needs to specify 1) what compression mode and 2) |
| 89 |
what compression quality to use. Supported compression modes and qualities are summarized below: |
| 90 |
| Mode No. | Mode Meaning | Meaningful Quality Range | |
| 91 |
| ------------- | -------------------- | ------------------------- | |
| 92 |
| 1 | Fixed bit-per-pixel (BPP) | 0.0 < quality < 64.0 | |
| 93 |
| 2 | Fixed peak signal-to-noise ratio (PSNR) | 0.0 < quality | |
| 94 |
| 3 | Fixed point-wise error (PWE) | 0.0 < quality | |
| 95 |
|
| 96 |
In addition, the rank order needs to be swapped sometimes to achieve the best compression. |
| 97 |
For example, if a 2D slice of dimensions `(64, 128)` has the `dim=128` rank being the fastest |
| 98 |
varying rank, then this slice needs a "rank order swap" to achieve the best compression. |
| 99 |
In general, given dimensions of `(NX, NY, NZ)`, we want the `X` rank to be varying the fastest, |
| 100 |
and the `Z` rank to be varying the slowest, before the data is passed to the compressor. |
| 101 |
"Rank order swap" helps to achieve it. |
| 102 |
|
| 103 |
The HDF5 libraries takes in these compression parameters as one or more 32-bit `unsigned int` values, |
| 104 |
which are named `cd_values[]` in most HDF5 routines. |
| 105 |
In the case of `H5Z-SPERR`, there is exactly one `unsigned int` used to carry compression-related information, and |
| 106 |
possibly one more `unsigned int` to indicate the potential existance of missing values. |
| 107 |
|
| 108 |
### Find `cd_values[]` Using the Programming Interface |
| 109 |
Using the HDF5 programming interface, `cd_values[]` carrying the compression parameters are passed |
| 110 |
to HDF5 routines such as `H5Pset_filter()`. To find the correct `cd_values[]`, a user |
| 111 |
needs to include the header [`h5z-sperr.h`](https://github.com/NCAR/H5Z-SPERR/blob/main/include/h5z-sperr.h) |
| 112 |
from this repository |
| 113 |
and call the `unsigned int H5Z_SPERR_make_cd_values(int mode, double quality, int swap)` function |
| 114 |
to have these two pieces of information correctly encoded. For example: |
| 115 |
```C |
| 116 |
int mode = 3; /* Fixed PWE compression */ |
| 117 |
double quality = 1e-6; /* PWE tolerance = 1e-6 */ |
| 118 |
int swap = 1; /* Enable rank order swap */ |
| 119 |
unsigned int cd_values = H5Z_SPERR_make_cd_values(mode, quality, swap); /* Generate cd_values */ |
| 120 |
H5Pset_filter(prop, 32028, H5Z_FLAG_MANDATORY, 1, &cd_values); /* Specify SPERR compression in HDF5 */ |
| 121 |
``` |
| 122 |
See a complete example [here](https://github.com/NCAR/H5Z-SPERR/blob/main/utilities/example-3d.c). |
| 123 |
|
| 124 |
### Find `cd_values[]` Using the CLI Tool `generate_cd_values` |
| 125 |
After building `H5Z-SPERR`, a command line tool named `generate_cd_values` becomes available to encode 1) SPERR |
| 126 |
compression mode, 2) quality, and 3) if to perform rank order swap |
| 127 |
into a single `unsigned int`. The produced value can then be used in other command line tools such as `h5repack`. |
| 128 |
In the following example, `generate_cd_values` reports that `268651725u` encodes fixed-rate compression with |
| 129 |
a bitrate of 3.3 bit-per-value, without doing rank order swap. |
| 130 |
```Bash |
| 131 |
$ ./bin/generate_cd_values 1 3.3 |
| 132 |
For fixed-rate compression with a bitrate of 3.3, without swapping rank orders, |
| 133 |
H5Z-SPERR cd_values = 268651725u (Filter ID = 32028). |
| 134 |
Please use this value as a single 32-bit unsigned integer in your applications. |
| 135 |
``` |
| 136 |
Note: an integer produced by `generate_cd_values` can be decoded by another command line tool, `decode_cd_values`, |
| 137 |
to show the coded compression parameters. |
| 138 |
|
| 139 |
### Examples |
| 140 |
Assume using the `nccopy` tool: |
| 141 |
```Bash |
| 142 |
# Compress variable VAR0, using fixed-rate compression, bitrate = 3.3, no special handling of missing values. |
| 143 |
nccopy -F "VAR0, 268651725u" <input_file> <output_file> |
| 144 |
nccopy -F "VAR0, 268651725u, 0" <input_file> <output_file> |
| 145 |
|
| 146 |
# Compress variable VAR1, using fixed-rate compression, bitrate = 3.3. VAR1 might have NaNs! |
| 147 |
nccopy -F "VAR1, 268651725u, 1" <input_file> <output_file> |
| 148 |
|
| 149 |
# Compress variable VAR2, using fixed-rate compression, bitrate = 3.3. VAR2 might have values such as 1e35! |
| 150 |
nccopy -F "VAR2, 268651725u, 2" <input_file> <output_file> |
| 151 |
``` |
| 152 |
|