Skip to content

Commit 2ccdf14

Browse files
committed
Merge branch 'csz_bitround' of https://github.com/nco/netcdf-c into gh2232.wif
2 parents cd421ee + d509396 commit 2ccdf14

File tree

17 files changed

+1423
-1203
lines changed

17 files changed

+1423
-1203
lines changed

include/netcdf.h

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -331,19 +331,23 @@ there. */
331331
#define NC_NOQUANTIZE 0 /**< No quantization in use. */
332332
#define NC_QUANTIZE_BITGROOM 1 /**< Use BitGroom quantization. */
333333
#define NC_QUANTIZE_GRANULARBR 2 /**< Use Granular BitRound quantization. */
334+
#define NC_QUANTIZE_BITROUND 3 /**< Use BitRound quantization. */
334335

335336
/** When quantization is used for a variable, an attribute of the
336337
* appropriate name is added. */
337-
#define NC_QUANTIZE_BITGROOM_ATT_NAME "_QuantizeBitgroomNumberOfSignificantDigits"
338+
#define NC_QUANTIZE_BITGROOM_ATT_NAME "_QuantizeBitGroomNumberOfSignificantDigits"
338339
#define NC_QUANTIZE_GRANULARBR_ATT_NAME "_QuantizeGranularBitRoundNumberOfSignificantDigits"
340+
#define NC_QUANTIZE_BITROUND_ATT_NAME "_QuantizeBitRoundNumberOfSignificantBits"
339341

340342
/** For quantization, the allowed value of number of significant
341-
* digits for float. */
343+
* decimal and binary digits, respectively, for float. */
342344
#define NC_QUANTIZE_MAX_FLOAT_NSD (7)
345+
#define NC_QUANTIZE_MAX_FLOAT_NSB (23)
343346

344347
/** For quantization, the allowed value of number of significant
345-
* digits for double. */
348+
* decimal and binary digits, respectively, for double. */
346349
#define NC_QUANTIZE_MAX_DOUBLE_NSD (15)
350+
#define NC_QUANTIZE_MAX_DOUBLE_NSB (52)
347351

348352
/** The netcdf version 3 functions all return integer error status.
349353
* These are the possible values, in addition to certain values from

libdispatch/dvar.c

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -469,7 +469,7 @@ nc_def_var_deflate(int ncid, int varid, int shuffle, int deflate, int deflate_le
469469
470470
The data are quantized by setting unneeded bits to zeros or ones
471471
so that they may compress well. BitGroom sets bits alternately to 1/0,
472-
while Granular BitRound (GBR) sets (more) bits to zeros.
472+
while BitRound and Granular BitRound (GBR) round (more) bits to zeros
473473
Quantization is lossy (data are irretrievably altered), and it
474474
improves the compression ratio provided by a subsequent lossless
475475
compression filter. Quantization alone will not reduce the data size.
@@ -479,18 +479,17 @@ nc_def_var_deflate(int ncid, int varid, int shuffle, int deflate, int deflate_le
479479
compression will result in significant improvent in the final data
480480
size.
481481
482-
This data quantization used the BitGroom algorithm. A notable
483-
feature of BitGroom is that the data it processes remain in IEEE754
484-
format after quantization. Therefore the BitGroom algorithm does
482+
A notable feature of all the quantization algorithms is data remain
483+
in IEEE754 format afterwards. Therefore quantization algorithms do
485484
nothing when data are read.
486485
487486
Quantization is only available for variables of type NC_FLOAT or
488487
NC_DOUBLE. Attempts to set quantization for other variable
489488
types return an error (NC_EINVAL).
490489
491490
Variables that use quantize will have added an attribute with name
492-
::NC_QUANTIZE_ATT_NAME, which will contain the number of
493-
significant digits. Users should not delete or change this
491+
::NC_QUANTIZE_[ALGORITHM_NAME]_ATT_NAME, which will contain the
492+
number of significant digits. Users should not delete or change this
494493
attribute. This is the only record that quantize has been applied
495494
to the data.
496495
@@ -529,11 +528,16 @@ nc_def_var_deflate(int ncid, int varid, int shuffle, int deflate, int deflate_le
529528
@param ncid File ID.
530529
@param varid Variable ID. ::NC_GLOBAL may not be used.
531530
@param quantize_mode Quantization mode. May be ::NC_NOQUANTIZE or
532-
::NC_QUANTIZE_BITGROOM or ::NC_QUANTIZE_GRANULARBR.
533-
@param nsd Number of significant digits. May be any integer from 1
534-
to ::NC_QUANTIZE_MAX_FLOAT_NSD (for variables of type ::NC_FLOAT)
535-
or ::NC_QUANTIZE_MAX_DOUBLE_NSD (for variables of type
536-
::NC_DOUBLE). Ignored if quantize_mode = NC_NOQUANTIZE.
531+
::NC_QUANTIZE_BITGROOM or ::NC_QUANTIZE_GRANULARBR or
532+
::NC_QUANTIZE_BITROUND.
533+
@param nsd Number of significant digits (either decimal or binary).
534+
May be any integer from 1 to ::NC_QUANTIZE_MAX_FLOAT_NSD (for variables
535+
of type ::NC_FLOAT) or ::NC_QUANTIZE_MAX_DOUBLE_NSD (for variables
536+
of type ::NC_DOUBLE) for mode ::NC_QUANTIZE_BITGROOM and mode
537+
::NC_QUANTIZE_GRANULARBR. May be any integer from 1 to
538+
::NC_QUANTIZE_MAX_FLOAT_NSB (for variables of type ::NC_FLOAT) or
539+
::NC_QUANTIZE_MAX_DOUBLE_NSB (for variables of type ::NC_DOUBLE)
540+
for mode ::NC_QUANTIZE_BITROUND. Ignored if quantize_mode = NC_NOQUANTIZE.
537541
538542
@return ::NC_NOERR No error.
539543
@return ::NC_EGLOBAL Can't use ::NC_GLOBAL with this function.

libhdf5/hdf5open.c

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1218,7 +1218,16 @@ static int get_quantize_info(NC_VAR_INFO_T *var)
12181218
attid = H5Aopen_by_name(datasetid, ".", NC_QUANTIZE_GRANULARBR_ATT_NAME,
12191219
H5P_DEFAULT, H5P_DEFAULT);
12201220
if (attid > 0)
1221+
{
12211222
var->quantize_mode = NC_QUANTIZE_GRANULARBR;
1223+
}
1224+
else
1225+
{
1226+
attid = H5Aopen_by_name(datasetid, ".", NC_QUANTIZE_BITROUND_ATT_NAME,
1227+
H5P_DEFAULT, H5P_DEFAULT);
1228+
if (attid > 0)
1229+
var->quantize_mode = NC_QUANTIZE_BITROUND;
1230+
}
12221231
}
12231232

12241233
/* If there is an attribute, read it for the nsd. */
@@ -2307,7 +2316,7 @@ read_type(NC_GRP_INFO_T *grp, hid_t hdf_typeid, char *type_name)
23072316
* for both global and variable attributes.
23082317
*
23092318
* @param loc_id HDF5 attribute ID.
2310-
* @param att_name Name of the attrigute.
2319+
* @param att_name Name of the attribute.
23112320
* @param ainfo HDF5 info struct for attribute.
23122321
* @param att_data Pointer to an att_iter_info struct, which contains
23132322
* pointers to the NC_GRP_INFO_T and (for variable attributes) the

libhdf5/hdf5var.c

Lines changed: 61 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -715,36 +715,54 @@ nc_def_var_extra(int ncid, int varid, int *shuffle, int *unused1,
715715
}
716716

717717
/* Remember quantization settings. They will be used when data are
718-
* written. */
718+
* written.
719+
* Code block is identical to one in zvar.c---consider functionalizing */
719720
if (quantize_mode)
720721
{
721-
/* Only two valid mode settings. */
722+
/* Only four valid mode settings. */
722723
if (*quantize_mode != NC_NOQUANTIZE &&
723724
*quantize_mode != NC_QUANTIZE_BITGROOM &&
724-
*quantize_mode != NC_QUANTIZE_GRANULARBR)
725+
*quantize_mode != NC_QUANTIZE_GRANULARBR &&
726+
*quantize_mode != NC_QUANTIZE_BITROUND)
725727
return NC_EINVAL;
726728

727-
if (*quantize_mode == NC_QUANTIZE_BITGROOM || *quantize_mode == NC_QUANTIZE_GRANULARBR)
729+
if (*quantize_mode == NC_QUANTIZE_BITGROOM ||
730+
*quantize_mode == NC_QUANTIZE_GRANULARBR ||
731+
*quantize_mode == NC_QUANTIZE_BITROUND)
728732
{
729733
/* Only float and double types can have quantization. */
730734
if (var->type_info->hdr.id != NC_FLOAT &&
731735
var->type_info->hdr.id != NC_DOUBLE)
732736
return NC_EINVAL;
733737

734-
/* For bitgroom, number of significant digits is required. */
738+
739+
/* All quantization codecs require number of significant digits */
735740
if (!nsd)
736741
return NC_EINVAL;
737742

738743
/* NSD must be in range. */
739744
if (*nsd <= 0)
740745
return NC_EINVAL;
741-
if (var->type_info->hdr.id == NC_FLOAT &&
742-
*nsd > NC_QUANTIZE_MAX_FLOAT_NSD)
743-
return NC_EINVAL;
744-
if (var->type_info->hdr.id == NC_DOUBLE &&
745-
*nsd > NC_QUANTIZE_MAX_DOUBLE_NSD)
746-
return NC_EINVAL;
747746

747+
if (*quantize_mode == NC_QUANTIZE_BITGROOM ||
748+
*quantize_mode == NC_QUANTIZE_GRANULARBR)
749+
{
750+
if (var->type_info->hdr.id == NC_FLOAT &&
751+
*nsd > NC_QUANTIZE_MAX_FLOAT_NSD)
752+
return NC_EINVAL;
753+
if (var->type_info->hdr.id == NC_DOUBLE &&
754+
*nsd > NC_QUANTIZE_MAX_DOUBLE_NSD)
755+
return NC_EINVAL;
756+
}
757+
else if (*quantize_mode == NC_QUANTIZE_BITROUND)
758+
{
759+
if (var->type_info->hdr.id == NC_FLOAT &&
760+
*nsd > NC_QUANTIZE_MAX_FLOAT_NSB)
761+
return NC_EINVAL;
762+
if (var->type_info->hdr.id == NC_DOUBLE &&
763+
*nsd > NC_QUANTIZE_MAX_DOUBLE_NSB)
764+
return NC_EINVAL;
765+
}
748766
var->nsd = *nsd;
749767
}
750768

@@ -812,12 +830,25 @@ NC4_def_var_deflate(int ncid, int varid, int shuffle, int deflate,
812830
* error.)
813831
*
814832
* When quantize is turned on, and the number of significant digits
815-
* has been specified, then the netCDF library will quantize according
816-
* to the selected algorithm. BitGroom will apply all zeros or
817-
* all ones (alternating) to bits which are not needed to specify the
818-
* value to the number of significant digits. GranularBR will zero
819-
* more bits than BG, and thus be more compressible and less accurate.
820-
* Both will change the value of the data, and will make it more compressible.
833+
* (NSD) has been specified, then the netCDF library will quantize according
834+
* to the selected algorithm. BitGroom interprets NSD as decimal digits
835+
* will apply all zeros or all ones (alternating) to bits which are not
836+
* needed to specify the value to the number of significant decimal digits.
837+
* BitGroom retain the same number of bits for all values of a variable.
838+
* BitRound (BR) interprets NSD as binary digits (i.e., bits) and keeps the
839+
* the user-specified number of significant bits then rounds the result
840+
* to the nearest representable number according to IEEE rounding rules.
841+
* BG and BR both retain a uniform number of significant bits for all
842+
* values of a variable. Granular BitRound interprest NSD as decimal
843+
* digits. GranularBR determines the number of bits to necessary to
844+
* retain the user-specified number of significant digits individually
845+
* for every value of the variable. GranularBR then applies the BR
846+
* quantization algorithm on a granular, value-by-value, rather than
847+
* uniformly for the entire variable. GranularBR quantizes more bits
848+
* than BG, and is thus more compressive and less accurate than BG.
849+
* BR knows bits and makes no guarantees about decimal precision.
850+
* All quantization algorithms change the values of the data, and make
851+
* it more compressible.
821852
*
822853
* Quantizing the data does not reduce the size of the data on disk,
823854
* but combining quantize with compression will allow for better
@@ -829,10 +860,10 @@ NC4_def_var_deflate(int ncid, int varid, int shuffle, int deflate,
829860
* size.
830861
*
831862
* Variables which use quantize will have added an attribute with name
832-
* ::NC_QUANTIZE_[ALG_NAME]_ATT_NAME, which will contain the number of
833-
* significant digits. Users should not delete or change this
834-
* attribute. This is the only record that quantize has been applied
835-
* to the data.
863+
* ::NC_QUANTIZE_BITGROOM_ATT_NAME, ::NC_QUANTIZE_GRANULARBR_ATT_NAME,
864+
* or ::NC_QUANTIZE_BITROUND_ATT_NAME that contains the number of
865+
* significant digits. Users should not delete or change this attribute.
866+
* This is the only record that quantize has been applied to the data.
836867
*
837868
* As with the deflate settings, quantize settings may only be
838869
* modified before the first call to nc_enddef(). Once nc_enddef() is
@@ -847,10 +878,15 @@ NC4_def_var_deflate(int ncid, int varid, int shuffle, int deflate,
847878
* @param ncid File ID.
848879
* @param varid Variable ID. NC_GLOBAL may not be used.
849880
* @param quantize_mode Quantization mode. May be ::NC_NOQUANTIZE or
850-
* ::NC_QUANTIZE_BITGROOM or ::NC_QUANTIZE_GRANULARBR.
851-
* @param nsd Number of significant digits. May be any integer from 1
852-
* to ::NC_QUANTIZE_MAX_FLOAT_NSD (for variables of type ::NC_FLOAT) or
853-
* ::NC_QUANTIZE_MAX_DOUBLE_NSD (for variables of type ::NC_DOUBLE).
881+
* ::NC_QUANTIZE_BITGROOM, ::NC_QUANTIZE_BITROUND or ::NC_QUANTIZE_GRANULARBR.
882+
* @param nsd Number of significant digits (either decimal or binary).
883+
* May be any integer from 1 to ::NC_QUANTIZE_MAX_FLOAT_NSD (for variables
884+
* of type ::NC_FLOAT) or ::NC_QUANTIZE_MAX_DOUBLE_NSD (for variables
885+
* of type ::NC_DOUBLE) for mode ::NC_QUANTIZE_BITGROOM and mode
886+
* ::NC_QUANTIZE_GRANULARBR. May be any integer from 1 to
887+
* ::NC_QUANTIZE_MAX_FLOAT_NSB (for variables of type ::NC_FLOAT) or
888+
* ::NC_QUANTIZE_MAX_DOUBLE_NSB (for variables of type ::NC_DOUBLE)
889+
* for mode ::NC_QUANTIZE_BITROUND.
854890
*
855891
* @returns ::NC_NOERR No error.
856892
* @returns ::NC_EBADID Bad ncid.

libhdf5/nc4hdf.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1012,7 +1012,9 @@ var_create_dataset(NC_GRP_INFO_T *grp, NC_VAR_INFO_T *var, nc_bool_t write_dimid
10121012
}
10131013

10141014
/* If quantization is in use, write an attribute indicating it, a
1015-
* single integer which is the number of significant digits. */
1015+
* single integer which is the number of significant digits
1016+
* (NSD, for BitGroom and Granular BitRound) or number of significant bits
1017+
* (NSB, for BitRound). */
10161018
if (var->quantize_mode == NC_QUANTIZE_BITGROOM)
10171019
if ((retval = nc4_put_att(var->container, var->hdr.id, NC_QUANTIZE_BITGROOM_ATT_NAME, NC_INT, 1,
10181020
&var->nsd, NC_INT, 0)))
@@ -1023,6 +1025,11 @@ var_create_dataset(NC_GRP_INFO_T *grp, NC_VAR_INFO_T *var, nc_bool_t write_dimid
10231025
&var->nsd, NC_INT, 0)))
10241026
BAIL(retval);
10251027

1028+
if (var->quantize_mode == NC_QUANTIZE_BITROUND)
1029+
if ((retval = nc4_put_att(var->container, var->hdr.id, NC_QUANTIZE_BITROUND_ATT_NAME, NC_INT, 1,
1030+
&var->nsd, NC_INT, 0)))
1031+
BAIL(retval);
1032+
10261033
/* Write attributes for this var. */
10271034
if ((retval = write_attlist(var->att, var->hdr.id, grp)))
10281035
BAIL(retval);

libnczarr/zsync.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -727,6 +727,10 @@ ncz_sync_atts(NC_FILE_INFO_T* file, NC_OBJ* container, NCindex* attlist, int isc
727727
if((stat = NCJinsert(jatts,NC_QUANTIZE_GRANULARBR_ATT_NAME,jint))) goto done;
728728
jint = NULL;
729729
break;
730+
case NC_QUANTIZE_BITROUND:
731+
if((stat = NCJinsert(jatts,NC_QUANTIZE_BITROUND_ATT_NAME,jint))) goto done;
732+
jint = NULL;
733+
break;
730734
default: break;
731735
}
732736
}

0 commit comments

Comments
 (0)