diff --git a/nc_test4/tst_quantize.c b/nc_test4/tst_quantize.c index a465edecf..d86fa2a7c 100644 --- a/nc_test4/tst_quantize.c +++ b/nc_test4/tst_quantize.c @@ -836,5 +836,83 @@ main(int argc, char **argv) } } SUMMARIZE_ERR; + printf("*** Nice, simple example of using BitGroom plus zlib..."); + { +#define DIM_LEN_SIMPLE 8 +#define EPSILON .1 + int ncid; + int dimid; + int varid1, varid2; + float *float_data; + double *double_data; + int i; + + /* Set up some data to write. */ + if (!(float_data = malloc(DIM_LEN_SIMPLE * sizeof(float)))) + ERR; + if (!(double_data = malloc(DIM_LEN_SIMPLE * sizeof(double)))) + ERR; + for (i = 0; i < DIM_LEN_SIMPLE; i++) + { + float_data[i] = 1.5 * i; + double_data[i] = 1.5 * i; + } + + /* Create file. */ + if (nc_create(FILE_NAME, NC_NETCDF4, &ncid)) ERR; + + /* Create dims. */ + if (nc_def_dim(ncid, "dim1", DIM_LEN_SIMPLE, &dimid)) ERR; + + /* Create the variables. */ + if (nc_def_var(ncid, "var1", NC_FLOAT, NDIM1, &dimid, &varid1)) ERR; + if (nc_def_var(ncid, "var2", NC_DOUBLE, NDIM1, &dimid, &varid2)) ERR; + + /* Set up quantization. This will not make the data any + * smaller, unless compression is also turned on. */ + if (nc_def_var_quantize(ncid, varid1, NC_QUANTIZE_BITGROOM, NSD_3)) ERR; + if (nc_def_var_quantize(ncid, varid2, NC_QUANTIZE_BITGROOM, NSD_3)) ERR; + + /* Set up zlib compression. This will work better because the + * data are quantized, yielding a smaller output file. */ + if (nc_def_var_deflate(ncid, varid1, 0, 1, 1)) ERR; + if (nc_def_var_deflate(ncid, varid2, 0, 1, 1)) ERR; + + /* Write data. */ + if (nc_put_var_float(ncid, varid1, float_data)) ERR; + if (nc_put_var_double(ncid, varid2, double_data)) ERR; + + /* Close the file. */ + if (nc_close(ncid)) ERR; + + /* Check the resulting file for correctness. */ + { + float float_data_in[DIM_LEN_SIMPLE]; + double double_data_in[DIM_LEN_SIMPLE]; + + /* Now reopen the file and check. */ + if (nc_open(FILE_NAME, NC_NETCDF4, &ncid)) ERR; + + /* Read the data. */ + if (nc_get_var_float(ncid, varid1, float_data_in)) ERR; + if (nc_get_var_double(ncid, varid2, double_data_in)) ERR; + + for (i = 0; i < DIM_LEN_SIMPLE; i++) + { + if (abs(float_data_in[i] - float_data[i]) > EPSILON) + ERR; + if (abs(double_data_in[i] - double_data[i]) > EPSILON) + ERR; + } + + /* Close the file. */ + if (nc_close(ncid)) ERR; + } + + /* Free resources. */ + free(float_data); + free(double_data); + } + SUMMARIZE_ERR; FINAL_RESULTS; }