|
NEP 2.7.0
NetCDF Extension Pack
|
Demonstrates how BZIP2 compression levels and the shuffle filter affect NetCDF-4 compression ratio and I/O throughput. More...
#include <netcdf.h>#include <netcdf_filter.h>#include <stdio.h>#include <stdlib.h>#include <string.h>#include <sys/stat.h>#include <sys/time.h>Macros | |
| #define | NZ 500 |
| #define | NY 180 |
| #define | NX 360 |
| #define | TMP_FILE "bzip2_tmp.nc" |
| #define | CHUNK_Z 10 |
| #define | CHUNK_Y 45 |
| #define | CHUNK_X 90 |
| #define | NVALS ((size_t)NZ * NY * NX) |
| #define | UNCOMPRESSED_BYTES ((double)NVALS * sizeof(float)) |
| #define | ERRCODE 2 |
| #define | ERR(e) {printf("Error: %s\n", nc_strerror(e)); exit(ERRCODE);} |
| #define | MIN_LEVEL 1 |
| #define | MAX_LEVEL 9 |
Functions | |
| static double | get_time (void) |
| Return current wall-clock time in seconds. | |
| static int | run_one (float *data, int level, int shuffle) |
| Run one bzip2/shuffle combination and print a CSV row. | |
| int | main (void) |
| Main entry point. | |
Demonstrates how BZIP2 compression levels and the shuffle filter affect NetCDF-4 compression ratio and I/O throughput.
NetCDF-4/HDF5 supports BZIP2 compression via nc_def_var_bzip2(). BZIP2 is a block-sorting lossless compression algorithm that typically achieves higher compression ratios than LZ4 or Zstandard, but at the cost of significantly slower compression speed. BZIP2 offers a level range (1–9) where higher levels spend more CPU time searching for better compression patterns within each block.
The shuffle filter (shuffle = 1) reorders bytes before compression: for IEEE 754 floating-point data the most-significant bytes of adjacent values are grouped together, which typically increases the compression ratio by 3–5× at little additional cost. Because nc_def_var_bzip2() does not accept a shuffle parameter directly, shuffle is enabled by calling nc_def_var_deflate(ncid, varid, 1, 0, 0) (shuffle only, no deflate) before nc_def_var_bzip2().
This program iterates BZIP2 levels 1–9 × shuffle {off, on} (18 combinations total) over a 500×180×360 NC_FLOAT temperature dataset (~129 MB uncompressed) and reports:
Output: CSV printed to stdout with columns:
Typical workflow:
Learning Objectives:
Key Concepts:
Prerequisites:
Related Examples:
Key API functions:
| #define CHUNK_X 90 |
| #define CHUNK_Y 45 |
| #define CHUNK_Z 10 |
Chunk shape: matches all v1.10.0 performance examples for consistency.
| #define ERR | ( | e | ) | {printf("Error: %s\n", nc_strerror(e)); exit(ERRCODE);} |
| #define ERRCODE 2 |
| #define MAX_LEVEL 9 |
| #define MIN_LEVEL 1 |
Minimum and maximum BZIP2 levels.
| #define NX 360 |
Longitude dimension: 360 degrees.
| #define NY 180 |
Latitude dimension: 180 degrees.
| #define NZ 500 |
Time dimension: 500 time steps.
| #define TMP_FILE "bzip2_tmp.nc" |
Temporary NetCDF file created and removed for each measurement.
| #define UNCOMPRESSED_BYTES ((double)NVALS * sizeof(float)) |
Uncompressed size in bytes.
|
static |
Return current wall-clock time in seconds.
| int main | ( | void | ) |
Main entry point.
Allocates a 500×180×360 NC_FLOAT buffer with synthetic temperature data, then iterates BZIP2 levels 1–9 × shuffle {0, 1} (18 rows total), printing one CSV row per combination.
|
static |
Run one bzip2/shuffle combination and print a CSV row.
Creates bzip2_tmp.nc with the given bzip2 level and shuffle setting, writes a 500×180×360 NC_FLOAT temperature variable, stats the file to obtain the compressed size, reads the variable back, then removes the file.
When shuffle is 1, nc_def_var_deflate() is called with shuffle=1 and deflate=0 to activate the byte-shuffle filter without zlib compression; nc_def_var_bzip2() is then called to add bzip2 on top.
| data | Pre-allocated buffer of NVALS floats (synthetic data). |
| level | BZIP2 compression level (1 to 9). |
| shuffle | 1 to enable the shuffle filter, 0 to disable. |