adding quantize functions to all the dispatch tables

This commit is contained in:
Edward Hartnett 2021-08-24 01:26:44 -06:00
parent d475e1f8ad
commit 3202b8b37c
10 changed files with 231 additions and 8 deletions

View File

@ -148,8 +148,8 @@ struct NC_Dispatch
int (*inq_var_filter_ids)(int ncid, int varid, size_t* nfilters, unsigned int* filterids);
int (*inq_var_filter_info)(int ncid, int varid, unsigned int id, size_t* nparams, unsigned int* params);
/* Version 4 Add quantization. */
int (*inq_var_quantize)(int ncid, int varid, int *quantize_modep, int *nsdp);
int (*def_var_quantize)(int ncid, int varid, int quantize_mode, int nsd);
int (*inq_var_quantize)(int ncid, int varid, int *quantize_modep, int *nsdp);
};
#if defined(__cplusplus)

View File

@ -178,6 +178,9 @@ NCD2_get_var_chunk_cache,
NC_NOOP_inq_var_filter_ids,
NC_NOOP_inq_var_filter_info,
NC_NOTNC4_def_var_quantize,
NC_NOTNC4_inq_var_quantize,
};
const NC_Dispatch* NCD2_dispatch_table = NULL; /* moved here from ddispatch.c */

View File

@ -974,4 +974,7 @@ NCD4_get_var_chunk_cache,
NC_NOTNC4_inq_var_filter_ids,
NC_NOTNC4_inq_var_filter_info,
NC_NOTNC4_def_var_quantize,
NC_NOTNC4_inq_var_quantize,
};

View File

@ -36,6 +36,24 @@ NC_NOTNC4_def_var_quantize(int ncid, int varid, int quantize_mode, int nsd)
return NC_ENOTNC4;
}
/**
* @internal Not implemented in some dispatch tables
*
* @param ncid Ignored.
* @param varid Ignored.
* @param quantize_modep Ignored.
* @param nsdp Ignored.
*
* @return ::NC_ENOTNC4 Not implemented for a dispatch table
* @author Ed Hartnett
*/
int
NC_NOTNC4_inq_var_quantize(int ncid, int varid, int *quantize_modep,
int *nsdp)
{
return NC_ENOTNC4;
}
/**
* @internal Not implemented in some dispatch tables
*

View File

@ -527,6 +527,56 @@ nc_inq_var_fill(int ncid, int varid, int *no_fill, void *fill_valuep)
);
}
/** @ingroup variables
* Learn whether BitGroom quantization is on for a variable, and, if so,
* the NSD setting.
*
* @param ncid File ID.
* @param varid Variable ID. Must not be NC_GLOBAL.
* @param quantize_modep Pointer that gets a 0 if BitGroom is not in
* use for this var, and a 1 if it is. Ignored if NULL.
* @param nsdp Pointer that gets the NSD setting (from 1 to 15), if
* BitGroom is in use. Ignored if NULL.
*
* @return 0 for success, error code otherwise.
* @author Charlie Zender, Ed Hartnett
*/
int
nc_inq_var_quantize(int ncid, int varid, int *quantize_modep, int *nsdp)
{
NC* ncp;
int stat = NC_check_id(ncid,&ncp);
if(stat != NC_NOERR) return stat;
TRACE(nc_inq_var_quantize);
/* Using NC_GLOBAL is illegal. */
if (varid == NC_GLOBAL) return NC_EGLOBAL;
/* return ncp->dispatch->inq_var_all( */
/* ncid, varid, */
/* NULL, /\*name*\/ */
/* NULL, /\*xtypep*\/ */
/* NULL, /\*ndimsp*\/ */
/* NULL, /\*dimidsp*\/ */
/* NULL, /\*nattsp*\/ */
/* NULL, /\*shufflep*\/ */
/* NULL, /\*deflatep*\/ */
/* NULL, /\*deflatelevelp*\/ */
/* NULL, /\*fletcher32p*\/ */
/* NULL, /\*contiguousp*\/ */
/* NULL, /\*chunksizep*\/ */
/* NULL, /\*nofillp*\/ */
/* NULL, /\*fillvaluep*\/ */
/* NULL, /\*endianp*\/ */
/* NULL, /\* idp *\/ */
/* NULL, /\* nparamsp *\/ */
/* NULL, /\* params *\/ */
/* quantize_modep, */
/* nsdp); */
return 0;
}
/** \ingroup variables
Find the endianness of a variable.

View File

@ -105,6 +105,10 @@ static const NC_Dispatch HDF5_dispatcher = {
NC4_hdf5_inq_var_filter_ids,
NC4_hdf5_inq_var_filter_info,
NC_NOTNC4_def_var_quantize,
NC_NOTNC4_inq_var_quantize,
};
const NC_Dispatch* HDF5_dispatch_table = NULL; /* moved here from ddispatch.c */

View File

@ -24,6 +24,14 @@
/** Number of bytes in 64 KB. */
#define SIXTY_FOUR_KB (65536)
/** For quantization, the allowed value of number of significant
* digits for float. */
#define MAX_FLOAT_NSD (14)
/** For quantization, the allowed value of number of significant
* digits for double. */
#define MAX_DOUBLE_NSD (14)
#ifdef LOGGING
/**
* Report the chunksizes selected for a variable.
@ -465,6 +473,8 @@ exit:
* @param no_fill Pointer to no_fill setting.
* @param fill_value Pointer to fill value.
* @param endianness Pointer to endianness setting.
* @param quantize_mode Pointer to quantization mode.
* @param nsd Pointer to number of significant digits.
*
* @returns ::NC_NOERR for success
* @returns ::NC_EBADID Bad ncid.
@ -484,7 +494,8 @@ static int
nc_def_var_extra(int ncid, int varid, int *shuffle, int *unused1,
int *unused2, int *fletcher32, int *storage,
const size_t *chunksizes, int *no_fill,
const void *fill_value, int *endianness)
const void *fill_value, int *endianness,
int *quantize_mode, int *nsd)
{
NC_GRP_INFO_T *grp;
NC_FILE_INFO_T *h5;
@ -706,6 +717,60 @@ nc_def_var_extra(int ncid, int varid, int *shuffle, int *unused1,
var->endianness = *endianness;
}
/* Remember quantization settings. They will be used when data are
* written. */
if (quantize_mode)
{
/* Only two valid mode settings. */
if (*quantize_mode != NC_NOQUANTIZE &&
*quantize_mode != NC_QUANTIZE_BITGROOM)
return NC_EINVAL;
if (*quantize_mode == NC_QUANTIZE_BITGROOM)
{
/* Only float and double types can have quantization. */
if (var->type_info->hdr.id != NC_FLOAT &&
var->type_info->hdr.id != NC_DOUBLE)
return NC_EINVAL;
/* For bitgroom, number of significant digits is required. */
if (!nsd)
return NC_EINVAL;
/* NSD must be in range. */
if (*nsd < 0)
return NC_EINVAL;
if (var->type_info->hdr.id != NC_FLOAT && *nsd > MAX_FLOAT_NSD)
return NC_EINVAL;
if (var->type_info->hdr.id != NC_DOUBLE && *nsd > MAX_DOUBLE_NSD)
return NC_EINVAL;
var->nsd = *nsd;
}
var->quantize_mode = *quantize_mode;
/* If nsd was zero, turn quantization off. */
if (*nsd == 0)
var->quantize_mode = NC_NOQUANTIZE;
/* If quantization is turned off, then set nsd to 0. */
if (*quantize_mode == NC_NOQUANTIZE)
var->nsd = 0;
}
/* Setting nsd to 0 turns off quantization. */
if (nsd && !quantize_mode)
{
if (*nsd == 0)
{
var->quantize_mode = NC_NOQUANTIZE;
var->nsd = *nsd;
}
else
return NC_EINVAL;
}
return NC_NOERR;
}
@ -737,7 +802,8 @@ NC4_def_var_deflate(int ncid, int varid, int shuffle, int deflate,
int stat = NC_NOERR;
unsigned int level = (unsigned int)deflate_level;
/* Set shuffle first */
if((stat = nc_def_var_extra(ncid, varid, &shuffle, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL))) goto done;
if((stat = nc_def_var_extra(ncid, varid, &shuffle, NULL, NULL, NULL,
NULL, NULL, NULL, NULL, NULL, NULL, NULL))) goto done;
if(deflate) {
if((stat = nc_def_var_filter(ncid, varid, H5Z_FILTER_DEFLATE,1,&level))) goto done;
} /* else ignore */
@ -746,6 +812,33 @@ done:
return stat;
}
/**
* @internal Set quantization settings on a variable. This is
* called by nc_def_var_quantize().
*
* @param ncid File ID.
* @param varid Variable ID.
* @param quantize_mode Quantization mode.
* @param nsd Number of significant digits.
*
* @returns ::NC_NOERR No error.
* @returns ::NC_EBADID Bad ncid.
* @returns ::NC_ENOTVAR Invalid variable ID.
* @returns ::NC_ENOTNC4 Attempting netcdf-4 operation on file that is
* not netCDF-4/HDF5.
* @returns ::NC_ELATEDEF Too late to change settings for this variable.
* @returns ::NC_ENOTINDEFINE Not in define mode.
* @returns ::NC_EINVAL Invalid input
* @author Ed Hartnett, Dennis Heimbigner
*/
int
NC4_def_var_quantize(int ncid, int varid, int quantize_mode, int nsd)
{
return nc_def_var_extra(ncid, varid, NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL, NULL,
&quantize_mode, &nsd);
}
#if 0
/**
* @internal Remove a filter from filter list for a variable
@ -803,7 +896,7 @@ int
NC4_def_var_fletcher32(int ncid, int varid, int fletcher32)
{
return nc_def_var_extra(ncid, varid, NULL, NULL, NULL, &fletcher32,
NULL, NULL, NULL, NULL, NULL);
NULL, NULL, NULL, NULL, NULL, NULL, NULL);
}
/**
@ -832,7 +925,7 @@ int
NC4_def_var_chunking(int ncid, int varid, int storage, const size_t *chunksizesp)
{
return nc_def_var_extra(ncid, varid, NULL, NULL, NULL, NULL,
&storage, chunksizesp, NULL, NULL, NULL);
&storage, chunksizesp, NULL, NULL, NULL, NULL, NULL);
}
/**
@ -877,7 +970,7 @@ nc_def_var_chunking_ints(int ncid, int varid, int storage, int *chunksizesp)
cs[i] = chunksizesp[i];
retval = nc_def_var_extra(ncid, varid, NULL, NULL, NULL, NULL,
&storage, cs, NULL, NULL, NULL);
&storage, cs, NULL, NULL, NULL, NULL, NULL);
if (var->ndims)
free(cs);
@ -911,7 +1004,7 @@ int
NC4_def_var_fill(int ncid, int varid, int no_fill, const void *fill_value)
{
return nc_def_var_extra(ncid, varid, NULL, NULL, NULL, NULL, NULL,
NULL, &no_fill, fill_value, NULL);
NULL, &no_fill, fill_value, NULL, NULL, NULL);
}
/**
@ -940,7 +1033,7 @@ int
NC4_def_var_endian(int ncid, int varid, int endianness)
{
return nc_def_var_extra(ncid, varid, NULL, NULL, NULL, NULL, NULL,
NULL, NULL, NULL, &endianness);
NULL, NULL, NULL, &endianness, NULL, NULL);
}
/**
@ -2053,6 +2146,8 @@ exit:
* @param nparamsp Pointer to memory to store filter parameter count.
* @param params Pointer to vector of unsigned integers into which
* to store filter parameters.
* @param quantize_modep Gets quantization mode.
* @param nsdp Gets number of significant digits, if quantization is in use.
*
* @returns ::NC_NOERR No error.
* @returns ::NC_EBADID Bad ncid.
@ -2091,6 +2186,48 @@ NC4_HDF5_inq_var_all(int ncid, int varid, char *name, nc_type *xtypep,
endiannessp, unused1, unused2, unused3);
}
/**
* @internal Get quantization information about a variable. Pass NULL
* for whatever you don't care about.
*
* @param ncid File ID.
* @param varid Variable ID.
*
* @returns ::NC_NOERR No error.
* @returns ::NC_EBADID Bad ncid.
* @returns ::NC_ENOTVAR Bad varid.
* @returns ::NC_ENOMEM Out of memory.
* @returns ::NC_EINVAL Invalid input.
* @author Ed Hartnett
*/
int
NC4_HDF5_inq_var_quantize(int ncid, int varid, int *quantize_modep,
int *nsdp)
{
NC_FILE_INFO_T *h5;
NC_GRP_INFO_T *grp;
NC_VAR_INFO_T *var = NULL;
int retval;
LOG((2, "%s: ncid 0x%x varid %d", __func__, ncid, varid));
/* Find the file, group, and var info, and do lazy att read if
* needed. */
if ((retval = nc4_hdf5_find_grp_var_att(ncid, varid, NULL, 0, 0, NULL,
&h5, &grp, &var, NULL)))
return retval;
assert(grp && h5);
/* Now that lazy atts have been read, use the libsrc4 function to
* get the answers. */
/* return NC4_inq_var_all(ncid, varid, NULL, NULL, NULL, NULL, NULL, */
/* NULL, NULL, NULL, NULL, */
/* NULL, NULL, NULL, NULL, */
/* NULL, NULL, NULL, NULL, */
/* quantize_modep, nsdp); */
return 0;
}
/**
* @internal Set chunk cache size for a variable. This is the internal
* function called by nc_set_var_chunk_cache().

View File

@ -105,6 +105,8 @@ static const NC_Dispatch NCZ_dispatcher = {
NC4_get_var_chunk_cache,
NCZ_inq_var_filter_ids,
NCZ_inq_var_filter_info,
NC_NOTNC4_def_var_quantize,
NC_NOTNC4_inq_var_quantize,
};
const NC_Dispatch* NCZ_dispatch_table = NULL; /* moved here from ddispatch.c */

View File

@ -166,6 +166,9 @@ NC3_get_var_chunk_cache,
NC_NOOP_inq_var_filter_ids,
NC_NOOP_inq_var_filter_info,
NC_NOTNC4_def_var_quantize,
NC_NOTNC4_inq_var_quantize,
};
const NC_Dispatch* NC3_dispatch_table = NULL; /*!< NC3 Dispatch table, moved here from ddispatch.c */

View File

@ -1463,6 +1463,9 @@ NC_NOTNC4_get_var_chunk_cache,
NC_NOOP_inq_var_filter_ids,
NC_NOOP_inq_var_filter_info,
NC_NOTNC4_def_var_quantize,
NC_NOTNC4_inq_var_quantize,
};
const NC_Dispatch *NCP_dispatch_table = NULL; /* moved here from ddispatch.c */