|
NEP 2.7.0
NetCDF Extension Pack
|
Demonstrates how zlib deflate levels and the shuffle filter affect NetCDF-4 compression ratio and I/O throughput. More...
#include <netcdf.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 "deflate_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);} |
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 deflate/shuffle combination and print a CSV row. | |
| int | main (void) |
| Main entry point. | |
Demonstrates how zlib deflate levels and the shuffle filter affect NetCDF-4 compression ratio and I/O throughput.
NetCDF-4/HDF5 supports per-variable deflate compression via nc_def_var_deflate(). The level parameter (0 = no compression, 9 = maximum compression effort) trades CPU time for smaller files. 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 deflate ratio by 2–5× at little additional cost.
This program iterates all 20 combinations of deflate level (0–9) and shuffle (off/on) 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: one time slab per chunk (good general-purpose shape for deflate).
| #define ERR | ( | e | ) | {printf("Error: %s\n", nc_strerror(e)); exit(ERRCODE);} |
| #define ERRCODE 2 |
| #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 "deflate_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 all 20 combinations of shuffle (0 and 1) and deflate level (0–9), printing one CSV row per combination.
|
static |
Run one deflate/shuffle combination and print a CSV row.
Creates deflate_tmp.nc with the given deflate 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.
| data | Pre-allocated buffer of NVALS floats (synthetic data). |
| level | Deflate level (0–9). |
| shuffle | 1 to enable the shuffle filter, 0 to disable. |