NetCDF  4.9.3
dfilter.c
Go to the documentation of this file.
1 /*
2  * Copyright 2018, University Corporation for Atmospheric Research
3  * See netcdf/COPYRIGHT file for copying and redistribution conditions.
4  */
10 #include "config.h"
11 #include <stdlib.h>
12 #include <stdio.h>
13 #include <string.h>
14 #ifdef _MSC_VER
15 #include <io.h>
16 #endif
17 
18 #include "netcdf.h"
19 #include "netcdf_filter.h"
20 #include "ncdispatch.h"
21 #include "nc4internal.h"
22 #include "nclog.h"
23 
24 #ifdef USE_HDF5
25 #include "hdf5internal.h"
26 #endif
27 
28 #ifdef NETCDF_ENABLE_NCZARR
29 #include "zdispatch.h"
30 #endif
31 
32 /*
33 Unified filter related code
34 */
35 
36 /**************************************************/
37 /* Per-variable filters */
38 
58 EXTERNL int
59 nc_inq_var_filter_ids(int ncid, int varid, size_t* nfiltersp, unsigned int* ids)
60 {
61  NC* ncp;
62  int stat = NC_check_id(ncid,&ncp);
63  if(stat != NC_NOERR) return stat;
64  TRACE(nc_inq_var_filter_ids);
65  if((stat = ncp->dispatch->inq_var_filter_ids(ncid,varid,nfiltersp,ids))) goto done;
66 
67 done:
68  return stat;
69 }
70 
94 EXTERNL int
95 nc_inq_var_filter_info(int ncid, int varid, unsigned int id, size_t* nparamsp, unsigned int* params)
96 {
97  NC* ncp;
98  int stat = NC_check_id(ncid,&ncp);
99  if(stat != NC_NOERR) return stat;
100  TRACE(nc_inq_var_filter_info);
101  if((stat = ncp->dispatch->inq_var_filter_info(ncid,varid,id,nparamsp,params))) goto done;
102 
103 done:
104  if(stat == NC_ENOFILTER) nclog(NCLOGWARN,"Undefined filter: %u",(unsigned)id);
105  return stat;
106 }
107 
125 EXTERNL int
126 nc_def_var_filter(int ncid, int varid, unsigned int id, size_t nparams, const unsigned int* params)
127 {
128  int stat = NC_NOERR;
129  NC* ncp;
130 
131  TRACE(nc_inq_var_filter);
132  if((stat = NC_check_id(ncid,&ncp))) return stat;
133  if((stat = ncp->dispatch->def_var_filter(ncid,varid,id,nparams,params))) goto done;
134 done:
135  if(stat == NC_ENOFILTER) nclog(NCLOGWARN,"Undefined filter: %u",(unsigned)id);
136  return stat;
137 }
138 
166 EXTERNL int
167 nc_inq_var_filter(int ncid, int varid, unsigned int* idp, size_t* nparamsp, unsigned int* params)
168 {
169  NC* ncp;
170  size_t nfilters;
171  unsigned int* ids = NULL;
172  int stat = NC_check_id(ncid,&ncp);
173 
174  if(stat != NC_NOERR) return stat;
175  TRACE(nc_inq_var_filter);
176 
177  /* Get the number of filters on this variable */
178  if((stat = nc_inq_var_filter_ids(ncid,varid,&nfilters, NULL))) goto done;
179  /* If no filters, then return zero */
180  if(nfilters == 0) {
181  if(idp) *idp = 0;
182  goto done;
183  }
184  /* Get the filter ids */
185  if((ids = calloc(sizeof(unsigned int),nfilters)) == NULL) {stat = NC_ENOMEM; goto done;}
186  if((stat = nc_inq_var_filter_ids(ncid,varid,&nfilters, ids))) goto done;
187  /* Get params for the first filter */
188  if((stat = nc_inq_var_filter_info(ncid,varid,ids[0],nparamsp,params))) goto done;
189  if(idp) *idp = ids[0];
190  done:
191  nullfree(ids);
192  return stat;
193 }
194 
208 EXTERNL int
209 nc_inq_filter_avail(int ncid, unsigned id)
210 {
211  int stat = NC_NOERR;
212  NC* ncp;
213 
214  stat = NC_check_id(ncid,&ncp);
215  if(stat != NC_NOERR) return stat;
216  if((stat = ncp->dispatch->inq_filter_avail(ncid,id))) goto done;
217 done:
218  return stat;
219 }
220 
221 /**************************************************/
222 /* Functions for accessing standardized filters */
223 
235 int
236 nc_def_var_bzip2(int ncid, int varid, int level)
237 {
238  int stat = NC_NOERR;
239  unsigned ulevel;
240 
241  if((stat = nc_inq_filter_avail(ncid,H5Z_FILTER_BZIP2))) goto done;
242  /* Filter is available */
243  /* 1 <= Level <= 9 */
244  if (level < 1 || level > 9)
245  return NC_EINVAL;
246  ulevel = (unsigned) level; /* Keep bit pattern */
247  if((stat = nc_def_var_filter(ncid,varid,H5Z_FILTER_BZIP2,1,&ulevel))) goto done;
248 done:
249  return stat;
250 }
251 
266 int
267 nc_inq_var_bzip2(int ncid, int varid, int* hasfilterp, int *levelp)
268 {
269  int stat = NC_NOERR;
270  size_t nparams;
271  unsigned params = 0;
272  int hasfilter = 0;
273 
274  if((stat = nc_inq_filter_avail(ncid,H5Z_FILTER_BZIP2))) goto done;
275  /* Filter is available */
276  /* Get filter info */
277  stat = nc_inq_var_filter_info(ncid,varid,H5Z_FILTER_BZIP2,&nparams,NULL);
278  if(stat == NC_ENOFILTER) {stat = NC_NOERR; hasfilter = 0; goto done;}
279  if(stat != NC_NOERR) goto done;
280  hasfilter = 1;
281  if(nparams != 1) {stat = NC_EFILTER; goto done;}
282  if((stat = nc_inq_var_filter_info(ncid,varid,H5Z_FILTER_BZIP2,&nparams,&params))) goto done;
283 done:
284  if(levelp) *levelp = (int)params;
285  if(hasfilterp) *hasfilterp = hasfilter;
286  return stat;
287 }
288 
303 int
304 nc_def_var_zstandard(int ncid, int varid, int level)
305 {
306 #ifdef HAVE_ZSTD
307  int stat = NC_NOERR;
308  unsigned ulevel;
309 
310  if((stat = nc_inq_filter_avail(ncid,H5Z_FILTER_ZSTD))) goto done;
311  /* Filter is available */
312  /* Level must be between -131072 and 22 on Zstandard v. 1.4.5 (~202009)
313  Earlier versions have fewer levels (especially fewer negative levels) */
314  if (level < -131072 || level > 22)
315  return NC_EINVAL;
316  ulevel = (unsigned) level; /* Keep bit pattern */
317  if((stat = nc_def_var_filter(ncid,varid,H5Z_FILTER_ZSTD,1,&ulevel))) goto done;
318 done:
319  return stat;
320 #else
321  return NC_NOERR;
322 #endif /*HAVE_ZSTD*/
323 }
324 
339 int
340 nc_inq_var_zstandard(int ncid, int varid, int* hasfilterp, int *levelp)
341 {
342 #ifdef HAVE_ZSTD
343  int stat = NC_NOERR;
344  size_t nparams;
345  unsigned params = 0;
346  int hasfilter = 0;
347 
348  if((stat = nc_inq_filter_avail(ncid,H5Z_FILTER_ZSTD))) goto done;
349  /* Filter is available */
350  /* Get filter info */
351  stat = nc_inq_var_filter_info(ncid,varid,H5Z_FILTER_ZSTD,&nparams,NULL);
352  if(stat == NC_ENOFILTER) {stat = NC_NOERR; hasfilter = 0; goto done;}
353  if(stat != NC_NOERR) goto done;
354  hasfilter = 1;
355  if(nparams != 1) {stat = NC_EFILTER; goto done;}
356  if((stat = nc_inq_var_filter_info(ncid,varid,H5Z_FILTER_ZSTD,&nparams,&params))) goto done;
357 done:
358  if(levelp) *levelp = (int)params;
359  if(hasfilterp) *hasfilterp = hasfilter;
360  return stat;
361 #else
362  return NC_NOERR;
363 #endif /*HAVE_ZSTD*/
364 }
365 
380 int
381 nc_def_var_blosc(int ncid, int varid, unsigned subcompressor, unsigned level, unsigned blocksize, unsigned addshuffle)
382 {
383 #ifdef HAVE_BLOSC
384  int stat = NC_NOERR;
385  unsigned params[7];;
386 
387  if((stat = nc_inq_filter_avail(ncid,H5Z_FILTER_BLOSC))) goto done;
388  /* Filter is available */
389 
390  /* Verify parameters */
391  if(addshuffle > (unsigned)BLOSC_BITSHUFFLE) {stat = NC_EINVAL; goto done;}
392  if(subcompressor > (unsigned)BLOSC_ZSTD) {stat = NC_EINVAL; goto done;}
393 
394  /* Set the parameters */
395  params[0] = 0;
396  params[1] = 0;
397  params[2] = 0;
398  params[3] = blocksize;
399  params[4] = level;
400  params[5] = addshuffle;
401  params[6] = subcompressor;
402  if((stat = nc_def_var_filter(ncid,varid,H5Z_FILTER_BLOSC,7,params))) goto done;
403 done:
404  return stat;
405 #else
406  return NC_NOERR;
407 #endif
408 }
409 
430 int
431 nc_inq_var_blosc(int ncid, int varid, int* hasfilterp, unsigned* subcompressorp,
432  unsigned* levelp, unsigned* blocksizep, unsigned* addshufflep)
433 {
434 #ifdef HAVE_BLOSC
435  int stat = NC_NOERR;
436  size_t nparams;
437  unsigned params[7];
438  int hasfilter = 0;
439 
440  if((stat = nc_inq_filter_avail(ncid,H5Z_FILTER_BLOSC))) goto done;
441  /* Filter is available */
442 
443  /* Get filter info */
444  stat = nc_inq_var_filter_info(ncid,varid,H5Z_FILTER_BLOSC,&nparams,NULL);
445  if(stat == NC_ENOFILTER) {stat = NC_NOERR; hasfilter = 0; goto done;}
446  if(stat != NC_NOERR) goto done;
447  hasfilter = 1;
448  if(nparams != 7) {stat = NC_EFILTER; goto done;}
449  if((stat = nc_inq_var_filter_info(ncid,varid,H5Z_FILTER_BLOSC,&nparams,params))) goto done;
450  if(blocksizep) *blocksizep = params[3];
451  if(levelp) *levelp = params[4];
452  if(addshufflep) *addshufflep = params[5];
453  if(subcompressorp) *subcompressorp = params[6];
454 done:
455  if(hasfilterp) *hasfilterp = hasfilter;
456  return stat;
457 #else
458  return NC_NOERR;
459 #endif
460 }
#define NC_ENOMEM
Memory allocation (malloc) failure.
Definition: netcdf.h:458
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:126
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:340
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:381
Main header file for the C API.
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:267
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:59
#define NC_EFILTER
Filter operation failed.
Definition: netcdf.h:523
#define NC_EINVAL
Invalid Argument.
Definition: netcdf.h:388
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:95
EXTERNL int nc_inq_var_filter(int ncid, int varid, unsigned int *idp, size_t *nparamsp, unsigned int *params)
Find the first filter (if any) associated with a variable.
Definition: dfilter.c:167
#define EXTERNL
Needed for DLL build.
Definition: netcdf.h:571
#define NC_ENOFILTER
Filter not defined on variable.
Definition: netcdf.h:527
int nc_def_var_zstandard(int ncid, int varid, int level)
Turn on Zstandard compression for a variable.
Definition: dfilter.c:304
#define NC_NOERR
No Error.
Definition: netcdf.h:378
int nc_def_var_bzip2(int ncid, int varid, int level)
Turn on bzip2 compression for a variable.
Definition: dfilter.c:236
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:431
EXTERNL int nc_inq_filter_avail(int ncid, unsigned id)
Test if filter is available.
Definition: dfilter.c:209