2017-08-28 03:35:20 +08:00
|
|
|
/* This is part of the netCDF package.
|
2018-12-07 06:27:32 +08:00
|
|
|
Copyright 2018 University Corporation for Atmospheric Research/Unidata
|
2017-08-28 03:35:20 +08:00
|
|
|
See COPYRIGHT file for conditions of use.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Example illustrates the use of SZIP compression in netCDF5
|
|
|
|
* Taken from HDF5 example.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include "nc_tests.h"
|
|
|
|
#include "err_macros.h"
|
|
|
|
#include "netcdf.h"
|
|
|
|
|
2017-08-28 10:38:37 +08:00
|
|
|
#undef PLAIN
|
|
|
|
#define USECLOSE
|
2017-08-28 03:35:20 +08:00
|
|
|
|
|
|
|
/* Szip Constants. */
|
|
|
|
#define HDF5_FILTER_SZIP 4
|
2018-09-16 05:21:51 +08:00
|
|
|
#define H5_SZIP_MAX_PIXELS_PER_BLOCK_IN 32
|
|
|
|
#define H5_SZIP_MAX_PIXELS_PER_BLOCK_OUT 64
|
2017-08-28 03:35:20 +08:00
|
|
|
|
2017-08-28 10:38:37 +08:00
|
|
|
/* Option Mask Flags (Refere to HDF5 szip documentation) */
|
|
|
|
#define H5_SZIP_ALLOW_K13_OPTION_MASK 1 /*Allows k split = 13 compression mode. (Default)*/
|
|
|
|
#define H5_SZIP_CHIP_OPTION_MASK 2 /*Compresses exactly as in hardware*/
|
|
|
|
#define H5_SZIP_EC_OPTION_MASK 4 /*Selects entropy coding method. (Default)*/
|
|
|
|
#define H5_SZIP_NN_OPTION_MASK 32 /*Selects nearest neighbor coding method*/
|
|
|
|
|
2017-08-28 03:35:20 +08:00
|
|
|
#define NX 500
|
|
|
|
#define NY 600
|
|
|
|
#define CH_NX 100
|
|
|
|
#define CH_NY 25
|
|
|
|
|
2017-08-29 08:11:24 +08:00
|
|
|
|
|
|
|
static void initialize(void);
|
|
|
|
static int compare(void);
|
|
|
|
|
|
|
|
static float buf[NX][NY];
|
|
|
|
static float buf_r[NX][NY];
|
|
|
|
|
2017-08-28 03:35:20 +08:00
|
|
|
int
|
|
|
|
main(void)
|
|
|
|
{
|
|
|
|
int ncid, varid, dimids[2];
|
|
|
|
size_t dims[2], chunk_size[2];
|
|
|
|
unsigned int szip_params[2]; /* [0]=options_mask [1]=pixels_per_block */
|
2017-08-29 08:11:24 +08:00
|
|
|
int errcnt = 0;
|
2017-08-28 03:35:20 +08:00
|
|
|
|
|
|
|
/* Create a new file using read/write access. */
|
|
|
|
if(nc_create("testszip.nc", NC_CLOBBER|NC_NETCDF4, &ncid)) ERR;
|
|
|
|
|
|
|
|
/* Create dims */
|
|
|
|
dims[0] = NX;
|
|
|
|
dims[1] = NY;
|
|
|
|
if(nc_def_dim(ncid, "x", dims[0], &dimids[0])) ERR;
|
|
|
|
if(nc_def_dim(ncid, "y", dims[1], &dimids[1])) ERR;
|
|
|
|
|
|
|
|
/* Create a dimensioned variable */
|
2017-08-28 10:38:37 +08:00
|
|
|
if(nc_def_var(ncid, "datasetF32", NC_FLOAT, 2, dimids, &varid)) ERR;
|
|
|
|
|
|
|
|
/* no fill */
|
|
|
|
if(nc_def_var_fill(ncid, varid, 1, NULL)) ERR;
|
2017-08-28 03:35:20 +08:00
|
|
|
|
|
|
|
/* Define chunking for the variable:
|
|
|
|
* the raw data is to be partitioned into 100x100 element chunks.
|
|
|
|
*/
|
|
|
|
chunk_size[0] = CH_NX;
|
|
|
|
chunk_size[1] = CH_NY;
|
|
|
|
if(nc_def_var_chunking(ncid, varid, NC_CHUNKED, chunk_size)) ERR;
|
|
|
|
|
|
|
|
#ifndef PLAIN
|
|
|
|
/*
|
|
|
|
* Set parameters for SZIP compression; check the description of
|
|
|
|
* the H5Pset_szip function in the HDF5 Reference Manual for more
|
|
|
|
* information.
|
|
|
|
*/
|
|
|
|
szip_params[0] = H5_SZIP_NN_OPTION_MASK;
|
2018-09-16 05:21:51 +08:00
|
|
|
szip_params[1] = H5_SZIP_MAX_PIXELS_PER_BLOCK_IN;
|
2017-08-28 10:38:37 +08:00
|
|
|
{ int stat = nc_def_var_filter(ncid, varid, HDF5_FILTER_SZIP, 2, szip_params);
|
|
|
|
if(stat) {
|
|
|
|
fprintf(stderr,"XXX: %d %s\b",stat,nc_strerror(stat));
|
|
|
|
ERR;
|
|
|
|
}
|
|
|
|
}
|
2017-08-28 03:35:20 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
if(nc_enddef(ncid)) ERR;
|
|
|
|
|
2017-08-29 08:11:24 +08:00
|
|
|
initialize();
|
2017-08-28 03:35:20 +08:00
|
|
|
|
|
|
|
/* Write the array to the file */
|
|
|
|
if(nc_put_var_float(ncid, varid, &buf[0][0])) ERR;
|
|
|
|
|
2017-08-28 10:38:37 +08:00
|
|
|
#ifdef USECLOSE
|
2017-08-28 03:35:20 +08:00
|
|
|
/* Close and re-open the file */
|
|
|
|
if(nc_close(ncid)) ERR;
|
2017-08-28 10:38:37 +08:00
|
|
|
if(nc_open("testszip.nc", NC_NETCDF4, &ncid)) ERR;
|
|
|
|
if(nc_inq_varid(ncid, "datasetF32", &varid)) ERR;
|
2017-08-28 03:35:20 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Read the array. This is similar to writing data,
|
|
|
|
* except the data flows in the opposite direction.
|
|
|
|
* Note: Decompression should be automatic.
|
|
|
|
*/
|
|
|
|
|
|
|
|
memset(buf_r,0,sizeof(buf_r));
|
|
|
|
if(nc_get_var_float(ncid, varid, &buf_r[0][0])) ERR;
|
|
|
|
|
|
|
|
/* Do comparison */
|
2017-08-29 08:11:24 +08:00
|
|
|
errcnt = compare();
|
2017-08-28 03:35:20 +08:00
|
|
|
|
|
|
|
if(nc_close(ncid)) ERR;
|
|
|
|
|
2017-08-29 08:11:24 +08:00
|
|
|
if(errcnt) ERR;
|
|
|
|
|
2017-08-28 03:35:20 +08:00
|
|
|
SUMMARIZE_ERR;
|
|
|
|
FINAL_RESULTS;
|
2017-08-29 08:11:24 +08:00
|
|
|
|
|
|
|
return (errcnt==0?0:1);
|
2017-08-28 03:35:20 +08:00
|
|
|
}
|
2017-08-29 08:11:24 +08:00
|
|
|
|
|
|
|
static int
|
|
|
|
compare()
|
|
|
|
{
|
|
|
|
int i,j;
|
|
|
|
int errs = 0;
|
|
|
|
|
|
|
|
/* Do comparison */
|
|
|
|
for (i=0; i < NX; i++) {
|
|
|
|
for (j=0; j < NY; j++) {
|
|
|
|
if(buf[i][j] != buf_r[i][j]) {
|
|
|
|
errs++;
|
|
|
|
printf("mismatch: [%d][%d]: write = %f read=%f\n",
|
|
|
|
i,j,buf[i][j],buf_r[i][j]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return errs;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
initialize(void)
|
|
|
|
{
|
|
|
|
int i, j;
|
|
|
|
/* Initialize data buffer with some bogus data. */
|
|
|
|
for(i=0; i < NX; i++) {
|
|
|
|
for(j=0; j < NY; j++) {
|
|
|
|
buf[i][j] = (float)(i + j);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|