1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559
|
/*
* Copyright 2018, University Corporation for Atmospheric Research
* See netcdf/COPYRIGHT file for copying and redistribution conditions.
*/
/**
* @file
* Functions for working with filters.
*/
#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"
#ifdef USE_HDF5
#include "hdf5internal.h"
#endif
#ifdef ENABLE_NCZARR
#include "zdispatch.h"
#endif
static void byteswap8(unsigned char*);
static void byteswap4(unsigned char*);
/*
Unified filter related code
*/
/**************************************************/
/* Per-variable filters */
/**
Find the set of filters (if any) associated with a variable.
Assumes HDF5 format using unsigned ints.
@param ncid NetCDF or group ID, from a previous call to nc_open(),
nc_create(), nc_def_grp(), or associated inquiry functions such as
nc_inq_ncid().
@param varid Variable ID
@param nfiltersp Pointer that gets the number of filters; may be zero.
@param ids return the filter ids (caller allocates)
@returns ::NC_NOERR No error.
@returns ::NC_ENOTNC4 Not a netCDF-4 file.
@returns ::NC_EBADID Bad ncid
@returns ::NC_ENOTVAR Invalid variable ID.
@returns ::NC_EINVAL Invalid arguments
@ingroup variables
@author Dennis Heimbigner
*/
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;
}
/**
Find the the param info about filter (if any)
associated with a variable and with specified id.
Assumes HDF5 format using unsigned ints.
@param ncid NetCDF or group ID, from a previous call to nc_open(),
nc_create(), nc_def_grp(), or associated inquiry functions such as
nc_inq_ncid().
@param varid Variable ID
@param id The filter id of interest
@param nparamsp (Out) Storage which will get the number of parameters to the filter
@param params (Out) Storage which will get associated parameters.
Note: the caller must allocate and free.
@returns ::NC_NOERR No error.
@returns ::NC_ENOTNC4 Not a netCDF-4 file.
@returns ::NC_EBADID Bad ncid.
@returns ::NC_ENOTVAR Invalid variable ID.
@returns ::NC_ENOFILTER Specified filter not defined for this variable.
@ingroup variables
@author Dennis Heimbigner
*/
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:
return stat;
}
/**
Define a new variable filter
Assumes HDF5 format using unsigned ints.
Only variables with chunked storage can use filters.
@param ncid File and group ID.
@param varid Variable ID.
@param id Filter ID.
@param nparams Number of filter parameters.
@param params Filter parameters.
@return ::NC_NOERR No error.
@return ::NC_EINVAL Variable must be chunked.
@return ::NC_EBADID Bad ID.
@author Dennis Heimbigner
*/
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;
int fixedsize;
nc_type xtype;
TRACE(nc_inq_var_filter);
if((stat = NC_check_id(ncid,&ncp))) return stat;
/* Get variable' type */
if((stat = nc_inq_vartype(ncid,varid,&xtype))) return stat;
/* If the variable's type is not fixed-size, then signal error */
if((stat = NC4_inq_type_fixed_size(ncid, xtype, &fixedsize))) return stat;
if(!fixedsize) return NC_EFILTER;
if((stat = ncp->dispatch->def_var_filter(ncid,varid,id,nparams,params))) goto done;
done:
return stat;
}
/**
Find the first filter (if any) associated with a variable.
Assumes HDF5 format using unsigned int.
@param ncid NetCDF or group ID, from a previous call to nc_open(),
nc_create(), nc_def_grp(), or associated inquiry functions such as
nc_inq_ncid().
@param varid Variable ID
@param idp Storage which will get the filter id; a return value of zero means variable has no filters.
@param nparamsp Storage which will get the number of parameters to the filter
@param params Storage which will get associated parameters (call allocates and frees).
This is redundant over the multi-filter API, so
it can be implemented in terms of those functions.
@returns ::NC_NOERR No error.
@returns ::NC_ENOTNC4 Not a netCDF-4 file.
@returns ::NC_EBADID Bad ncid.
@returns ::NC_ENOTVAR Invalid variable ID.
@ingroup variables
@author Dennis Heimbigner
*/
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;
}
/** Test if filter is available. Would prefer
* returning a list of all available filters, but HDF5
* does not support that capability.
*
* @param ncid ID of file for which filter list is desired
* @param id filter id of interest
*
* @return NC_NOERR if the filter is available
* @return NC_EBADID if ncid is invalid
* @return NC_ENOFILTER if filter is not available.
* @author Dennis Heimbigner
*/
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;
}
/**************************************************/
/* Support direct user defined filters */
#ifdef ENABLE_CLIENTSIDE_FILTERS
/** Register filer client.
* @note Use void* to avoid having to include hdf.h
*
* @param id Filter ID
* @param info Pointer that gets info.
*
* @return NC_NOERR if the filter is available
* @return NC_EBADID if ncid is invalid
* @return NC_ENOFILTER if filter is not available.
* @author Dennis Heimbigner
*/
EXTERNL int
nc_filter_client_register(unsigned int id, void* info)
{
int stat = NC_NOERR;
#ifdef USE_HDF5
NC_FILTER_OBJ_HDF5 client;
if(id == 0 ||info == NULL)
return NC_EINVAL;
memset(&client,0,sizeof(client));
client.hdr.format = NC_FILTER_FORMAT_HDF5;
client.sort = NC_FILTER_SORT_CLIENT;
client.u.client.id = id;
client.u.client.info = info;
/* Note use of a global function, not part of the dispatch table */
stat = nc4_global_filter_action(NCFILTER_CLIENT_REG, id, &client);
#else
stat = NC_ENOTBUILT;
#endif
return stat;
}
/** Unregister filer client.
* @note Use void* to avoid having to include hdf.h
*
* @param id Filter ID
*
* @return NC_NOERR if the filter is available
* @author Dennis Heimbigner
*/
EXTERNL int
nc_filter_client_unregister(unsigned int id)
{
int stat = NC_NOERR;
#ifdef USE_HDF5
stat = nc4_global_filter_action(NCFILTER_CLIENT_UNREG, id, NULL);
#else
stat = NC_ENOTBUILT;
#endif
return stat;
}
/** Inquire about filer client.
* @note Use void* to avoid having to include hdf.h
*
* @param id Filter ID
* @param infop Pointer that gets info.
*
* @return NC_NOERR if the filter is available
* @author Dennis Heimbigner
*/
EXTERNL int
nc_filter_client_inq(unsigned int id, void* infop)
{
int stat = NC_NOERR;
#ifdef USE_HDF5
H5Z_class2_t* hct = (H5Z_class2_t*)infop;
NC_FILTER_OBJ_HDF5 client;
if(id == 0 ||infop == NULL)
return NC_EINVAL;
memset(&client,0,sizeof(client));
client.hdr.format = NC_FILTER_FORMAT_HDF5;
client.sort = NC_FILTER_SORT_CLIENT;
client.u.client.id = id;
client.u.client.info = hct;
/* Note use of a global function, not part of the dispatch table */
stat = nc4_global_filter_action(NCFILTER_CLIENT_INQ, id, &client);
if(stat == NC_NOERR) {
*hct = *(H5Z_class2_t*)client.u.client.info;
}
#else
stat = NC_ENOTBUILT;
#endif
return stat;
}
#endif /*ENABLE_CLIENTSIDE_FILTERS*/
/**************************************************/
/* Functions for accessing standardized filters */
/**
* Turn on bzip2 compression for a variable.
*
* @param ncid File ID.
* @param varid Variable ID.
* @param level From 1 to 9. Set the block size to 100k, 200k ... 900k
* when compressing. (bzip2 default level is 9).
*
* @return 0 for success, error code otherwise.
* @author Dennis Heimbigner, Ed Hartnett
*/
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;
}
/**
* Learn whether bzip2 compression is on for a variable, and, if so,
* the level setting.
*
* @param ncid File ID.
* @param varid Variable ID.
* @param hasfilterp Pointer that gets a 0 if bzip2 is not in use for this
* var, and a 1 if it is. Ignored if NULL.
* @param levelp Pointer that gets the level setting (from 1 to 9), if
* bzip2 is in use. Ignored if NULL.
*
* @return 0 for success, error code otherwise.
* @author Dennis Heimbigner, Ed Hartnett
*/
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,¶ms))) goto done;
done:
if(levelp) *levelp = (int)params;
if(hasfilterp) *hasfilterp = hasfilter;
return stat;
}
/**
* Turn on Zstandard compression for a variable.
*
* @param ncid File ID.
* @param varid Variable ID.
* @param level From -131072 to 22 (depends on Zstandard version).
* when compressing. Regular compression levels are from 1 up to 19.
* Use levels >= 20, labeled `--ultra`, cautiously: they require more memory.
* Negative compression levels that extend the range of speed vs. ratio preferences.
* The lower the level, the faster the speed (at the cost of compression).
*
* @return 0 for success, error code otherwise.
* @author Charlie Zender, Dennis Heimbigner, Ed Hartnett
*/
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*/
}
/**
* Learn whether Zstandard compression is on for a variable, and, if so,
* the level setting.
*
* @param ncid File ID.
* @param varid Variable ID.
* @param hasfilterp Pointer that gets a 0 if Zstandard is not in use for this
* var, and a 1 if it is. Ignored if NULL.
* @param levelp Pointer that gets the level setting (from -131072 to 22), if
* Zstandard is in use. Ignored if NULL.
*
* @return 0 for success, error code otherwise.
* @author Charlie Zender, Dennis Heimbigner, Ed Hartnett
*/
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,¶ms))) goto done;
done:
if(levelp) *levelp = (int)params;
if(hasfilterp) *hasfilterp = hasfilter;
return stat;
#else
return NC_NOERR;
#endif /*HAVE_ZSTD*/
}
/**
* Turn on blosc for a variable.
*
* @param ncid File ID.
* @param varid Variable ID.
* @param subcompressor The subcompressor.
* @param level The level setting.
* @param blocksize The block size.
* @param addshuffle If non-zero, turn on shuffle.
*
* @return 0 for success, error code otherwise.
* @author Dennis Heimbigner
* @ingroup variables
*/
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
}
/**
* Learn whether Blosc compression is on for a variable, and, if so,
* the settings.
*
* @param ncid File ID.
* @param varid Variable ID.
* @param hasfilterp Pointer that gets a 0 if blosc is not in use for this
* var, and a 1 if it is. Ignored if NULL.
* @param subcompressorp Pointer that gets the subcompressor, if
* blosc is in use. Ignored if NULL.
* @param levelp Pointer that gets the level setting, if
* blosc is in use. Ignored if NULL.
* @param blocksizep Pointer that gets the block size, if
* blosc is in use. Ignored if NULL.
* @param addshufflep Pointer that gets non-zero value if shuffle is
* in use, if blosc is in use. Ignored if NULL.
*
* @return 0 for success, error code otherwise.
* @author Dennis Heimbigner
*/
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
}
|