Loading...
Searching...
No Matches
dfilter.c
Go to the documentation of this file.
/*
* Copyright 2018, University Corporation for Atmospheric Research
* See netcdf/COPYRIGHT file for copying and redistribution conditions.
*/
#include "config.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#ifdef _MSC_VER
#include <io.h>
#endif
#include "netcdf.h"
#include "netcdf_filter.h"
#include "ncdispatch.h"
#include "nc4internal.h"
#include "nclog.h"
#ifdef USE_HDF5
#include "hdf5internal.h"
#endif
#ifdef NETCDF_ENABLE_NCZARR
#include "zdispatch.h"
#endif
static void byteswap8(unsigned char*);
static void byteswap4(unsigned char*);
/*
Unified filter related code
*/
/**************************************************/
/* Per-variable filters */
EXTERNL int
nc_inq_var_filter_ids(int ncid, int varid, size_t* nfiltersp, unsigned int* ids)
{
NC* ncp;
int stat = NC_check_id(ncid,&ncp);
if(stat != NC_NOERR) return stat;
TRACE(nc_inq_var_filter_ids);
if((stat = ncp->dispatch->inq_var_filter_ids(ncid,varid,nfiltersp,ids))) goto done;
done:
return stat;
}
EXTERNL int
nc_inq_var_filter_info(int ncid, int varid, unsigned int id, size_t* nparamsp, unsigned int* params)
{
NC* ncp;
int stat = NC_check_id(ncid,&ncp);
if(stat != NC_NOERR) return stat;
TRACE(nc_inq_var_filter_info);
if((stat = ncp->dispatch->inq_var_filter_info(ncid,varid,id,nparamsp,params))) goto done;
done:
if(stat == NC_ENOFILTER) nclog(NCLOGWARN,"Undefined filter: %u",(unsigned)id);
return stat;
}
EXTERNL int
nc_def_var_filter(int ncid, int varid, unsigned int id, size_t nparams, const unsigned int* params)
{
int stat = NC_NOERR;
NC* ncp;
TRACE(nc_inq_var_filter);
if((stat = NC_check_id(ncid,&ncp))) return stat;
if((stat = ncp->dispatch->def_var_filter(ncid,varid,id,nparams,params))) goto done;
done:
if(stat == NC_ENOFILTER) nclog(NCLOGWARN,"Undefined filter: %u",(unsigned)id);
return stat;
}
EXTERNL int
nc_inq_var_filter(int ncid, int varid, unsigned int* idp, size_t* nparamsp, unsigned int* params)
{
NC* ncp;
size_t nfilters;
unsigned int* ids = NULL;
int stat = NC_check_id(ncid,&ncp);
if(stat != NC_NOERR) return stat;
TRACE(nc_inq_var_filter);
/* Get the number of filters on this variable */
if((stat = nc_inq_var_filter_ids(ncid,varid,&nfilters, NULL))) goto done;
/* If no filters, then return zero */
if(nfilters == 0) {
if(idp) *idp = 0;
goto done;
}
/* Get the filter ids */
if((ids = calloc(sizeof(unsigned int),nfilters)) == NULL) {stat = NC_ENOMEM; goto done;}
if((stat = nc_inq_var_filter_ids(ncid,varid,&nfilters, ids))) goto done;
/* Get params for the first filter */
if((stat = nc_inq_var_filter_info(ncid,varid,ids[0],nparamsp,params))) goto done;
if(idp) *idp = ids[0];
done:
nullfree(ids);
return stat;
}
EXTERNL int
nc_inq_filter_avail(int ncid, unsigned id)
{
int stat = NC_NOERR;
NC* ncp;
stat = NC_check_id(ncid,&ncp);
if(stat != NC_NOERR) return stat;
if((stat = ncp->dispatch->inq_filter_avail(ncid,id))) goto done;
done:
return stat;
}
/**************************************************/
/* Functions for accessing standardized filters */
int
nc_def_var_bzip2(int ncid, int varid, int level)
{
int stat = NC_NOERR;
unsigned ulevel;
if((stat = nc_inq_filter_avail(ncid,H5Z_FILTER_BZIP2))) goto done;
/* Filter is available */
/* 1 <= Level <= 9 */
if (level < 1 || level > 9)
return NC_EINVAL;
ulevel = (unsigned) level; /* Keep bit pattern */
if((stat = nc_def_var_filter(ncid,varid,H5Z_FILTER_BZIP2,1,&ulevel))) goto done;
done:
return stat;
}
int
nc_inq_var_bzip2(int ncid, int varid, int* hasfilterp, int *levelp)
{
int stat = NC_NOERR;
size_t nparams;
unsigned params = 0;
int hasfilter = 0;
if((stat = nc_inq_filter_avail(ncid,H5Z_FILTER_BZIP2))) goto done;
/* Filter is available */
/* Get filter info */
stat = nc_inq_var_filter_info(ncid,varid,H5Z_FILTER_BZIP2,&nparams,NULL);
if(stat == NC_ENOFILTER) {stat = NC_NOERR; hasfilter = 0; goto done;}
if(stat != NC_NOERR) goto done;
hasfilter = 1;
if(nparams != 1) {stat = NC_EFILTER; goto done;}
if((stat = nc_inq_var_filter_info(ncid,varid,H5Z_FILTER_BZIP2,&nparams,&params))) goto done;
done:
if(levelp) *levelp = (int)params;
if(hasfilterp) *hasfilterp = hasfilter;
return stat;
}
int
nc_def_var_zstandard(int ncid, int varid, int level)
{
#ifdef HAVE_ZSTD
int stat = NC_NOERR;
unsigned ulevel;
if((stat = nc_inq_filter_avail(ncid,H5Z_FILTER_ZSTD))) goto done;
/* Filter is available */
/* Level must be between -131072 and 22 on Zstandard v. 1.4.5 (~202009)
Earlier versions have fewer levels (especially fewer negative levels) */
if (level < -131072 || level > 22)
return NC_EINVAL;
ulevel = (unsigned) level; /* Keep bit pattern */
if((stat = nc_def_var_filter(ncid,varid,H5Z_FILTER_ZSTD,1,&ulevel))) goto done;
done:
return stat;
#else
return NC_NOERR;
#endif /*HAVE_ZSTD*/
}
int
nc_inq_var_zstandard(int ncid, int varid, int* hasfilterp, int *levelp)
{
#ifdef HAVE_ZSTD
int stat = NC_NOERR;
size_t nparams;
unsigned params = 0;
int hasfilter = 0;
if((stat = nc_inq_filter_avail(ncid,H5Z_FILTER_ZSTD))) goto done;
/* Filter is available */
/* Get filter info */
stat = nc_inq_var_filter_info(ncid,varid,H5Z_FILTER_ZSTD,&nparams,NULL);
if(stat == NC_ENOFILTER) {stat = NC_NOERR; hasfilter = 0; goto done;}
if(stat != NC_NOERR) goto done;
hasfilter = 1;
if(nparams != 1) {stat = NC_EFILTER; goto done;}
if((stat = nc_inq_var_filter_info(ncid,varid,H5Z_FILTER_ZSTD,&nparams,&params))) goto done;
done:
if(levelp) *levelp = (int)params;
if(hasfilterp) *hasfilterp = hasfilter;
return stat;
#else
return NC_NOERR;
#endif /*HAVE_ZSTD*/
}
int
nc_def_var_blosc(int ncid, int varid, unsigned subcompressor, unsigned level, unsigned blocksize, unsigned addshuffle)
{
#ifdef HAVE_BLOSC
int stat = NC_NOERR;
unsigned params[7];;
if((stat = nc_inq_filter_avail(ncid,H5Z_FILTER_BLOSC))) goto done;
/* Filter is available */
/* Verify parameters */
if(addshuffle > (unsigned)BLOSC_BITSHUFFLE) {stat = NC_EINVAL; goto done;}
if(subcompressor > (unsigned)BLOSC_ZSTD) {stat = NC_EINVAL; goto done;}
/* Set the parameters */
params[0] = 0;
params[1] = 0;
params[2] = 0;
params[3] = blocksize;
params[4] = level;
params[5] = addshuffle;
params[6] = subcompressor;
if((stat = nc_def_var_filter(ncid,varid,H5Z_FILTER_BLOSC,7,params))) goto done;
done:
return stat;
#else
return NC_NOERR;
#endif
}
int
nc_inq_var_blosc(int ncid, int varid, int* hasfilterp, unsigned* subcompressorp,
unsigned* levelp, unsigned* blocksizep, unsigned* addshufflep)
{
#ifdef HAVE_BLOSC
int stat = NC_NOERR;
size_t nparams;
unsigned params[7];
int hasfilter = 0;
if((stat = nc_inq_filter_avail(ncid,H5Z_FILTER_BLOSC))) goto done;
/* Filter is available */
/* Get filter info */
stat = nc_inq_var_filter_info(ncid,varid,H5Z_FILTER_BLOSC,&nparams,NULL);
if(stat == NC_ENOFILTER) {stat = NC_NOERR; hasfilter = 0; goto done;}
if(stat != NC_NOERR) goto done;
hasfilter = 1;
if(nparams != 7) {stat = NC_EFILTER; goto done;}
if((stat = nc_inq_var_filter_info(ncid,varid,H5Z_FILTER_BLOSC,&nparams,params))) goto done;
if(blocksizep) *blocksizep = params[3];
if(levelp) *levelp = params[4];
if(addshufflep) *addshufflep = params[5];
if(subcompressorp) *subcompressorp = params[6];
done:
if(hasfilterp) *hasfilterp = hasfilter;
return stat;
#else
return NC_NOERR;
#endif
}
nc_inq_var_blosc
int nc_inq_var_blosc(int ncid, int varid, int *hasfilterp, unsigned *subcompressorp, unsigned *levelp, unsigned *blocksizep, unsigned *addshufflep)
Learn whether Blosc compression is on for a variable, and, if so, the settings.
Definition dfilter.c:434
nc_def_var_zstandard
int nc_def_var_zstandard(int ncid, int varid, int level)
Turn on Zstandard compression for a variable.
Definition dfilter.c:307
nc_inq_var_bzip2
int nc_inq_var_bzip2(int ncid, int varid, int *hasfilterp, int *levelp)
Learn whether bzip2 compression is on for a variable, and, if so, the level setting.
Definition dfilter.c:270
nc_def_var_bzip2
int nc_def_var_bzip2(int ncid, int varid, int level)
Turn on bzip2 compression for a variable.
Definition dfilter.c:239
nc_inq_filter_avail
EXTERNL int nc_inq_filter_avail(int ncid, unsigned id)
Test if filter is available.
Definition dfilter.c:212
nc_inq_var_zstandard
int nc_inq_var_zstandard(int ncid, int varid, int *hasfilterp, int *levelp)
Learn whether Zstandard compression is on for a variable, and, if so, the level setting.
Definition dfilter.c:343
nc_def_var_filter
EXTERNL int nc_def_var_filter(int ncid, int varid, unsigned int id, size_t nparams, const unsigned int *params)
Define a new variable filter Assumes HDF5 format using unsigned ints.
Definition dfilter.c:129
nc_def_var_blosc
int nc_def_var_blosc(int ncid, int varid, unsigned subcompressor, unsigned level, unsigned blocksize, unsigned addshuffle)
Turn on blosc for a variable.
Definition dfilter.c:384
nc_inq_var_filter_info
EXTERNL int nc_inq_var_filter_info(int ncid, int varid, unsigned int id, size_t *nparamsp, unsigned int *params)
Find the the param info about filter (if any) associated with a variable and with specified id.
Definition dfilter.c:98
nc_inq_var_filter
EXTERNL int nc_inq_var_filter(int ncid, int varid, unsigned int *idp, size_t *nparams, unsigned int *params)
Find the first filter (if any) associated with a variable.
Definition dfilter.c:170
nc_inq_var_filter_ids
EXTERNL int nc_inq_var_filter_ids(int ncid, int varid, size_t *nfiltersp, unsigned int *ids)
Find the set of filters (if any) associated with a variable.
Definition dfilter.c:62
netcdf.h
Main header file for the C API.
NC_EFILTER
#define NC_EFILTER
Filter operation failed.
Definition netcdf.h:523
NC_ENOMEM
#define NC_ENOMEM
Memory allocation (malloc) failure.
Definition netcdf.h:458
EXTERNL
#define EXTERNL
Needed for DLL build.
Definition netcdf.h:571
NC_EINVAL
#define NC_EINVAL
Invalid Argument.
Definition netcdf.h:388
NC_NOERR
#define NC_NOERR
No Error.
Definition netcdf.h:378
NC_ENOFILTER
#define NC_ENOFILTER
Filter not defined on variable.
Definition netcdf.h:527
netcdf_filter.h