Coverage Report

Created: 2022-11-18 06:58

/src/netcdf-c/libsrc4/nc4var.c
Line
Count
Source (jump to first uncovered line)
1
/* Copyright 2003-2018, University Corporation for Atmospheric
2
 * Research. See COPYRIGHT file for copying and redistribution
3
 * conditions.*/
4
/**
5
 * @file
6
 * @internal This file is part of netcdf-4, a netCDF-like interface
7
 * for HDF5, or a HDF5 backend for netCDF, depending on your point of
8
 * view. This file handles the NetCDF-4 variable functions.
9
 *
10
 * @author Ed Hartnett, Dennis Heimbigner, Ward Fisher
11
 */
12
13
#include "config.h"
14
#include "nc4internal.h"
15
#include "nc4dispatch.h"
16
#ifdef USE_HDF5
17
#include "hdf5internal.h"
18
#endif
19
#include <math.h>
20
21
/** @internal Default size for unlimited dim chunksize. */
22
0
#define DEFAULT_1D_UNLIM_SIZE (4096)
23
24
/* Define log_e for 10 and 2. Prefer constants defined in math.h,
25
 * however, GCC environments can have hard time defining M_LN10/M_LN2
26
 * despite finding math.h */
27
#ifndef M_LN10
28
# define M_LN10         2.30258509299404568402  /**< log_e 10 */
29
#endif /* M_LN10 */
30
#ifndef M_LN2
31
# define M_LN2          0.69314718055994530942  /**< log_e 2 */
32
#endif /* M_LN2 */
33
34
/** Used in quantize code. Number of explicit bits in significand for
35
 * floats. Bits 0-22 of SP significands are explicit. Bit 23 is
36
 * implicitly 1. Currently redundant with NC_QUANTIZE_MAX_FLOAT_NSB
37
 * and with limits.h/climit (FLT_MANT_DIG-1) */
38
0
#define BIT_XPL_NBR_SGN_FLT (23)
39
40
/** Used in quantize code. Number of explicit bits in significand for
41
 * doubles. Bits 0-51 of DP significands are explicit. Bit 52 is
42
 * implicitly 1. Currently redundant with NC_QUANTIZE_MAX_DOUBLE_NSB 
43
 * and with limits.h/climit (DBL_MANT_DIG-1) */
44
0
#define BIT_XPL_NBR_SGN_DBL (52) 
45
  
46
/** Pointer union for floating point and bitmask types. */
47
typedef union { /* ptr_unn */
48
  float *fp;
49
  double *dp;
50
  unsigned int *ui32p;
51
  unsigned long long *ui64p;
52
  void *vp;
53
} ptr_unn;
54
55
/**
56
 * @internal This is called by nc_get_var_chunk_cache(). Get chunk
57
 * cache size for a variable.
58
 *
59
 * @param ncid File ID.
60
 * @param varid Variable ID.
61
 * @param sizep Gets size in bytes of cache.
62
 * @param nelemsp Gets number of element slots in cache.
63
 * @param preemptionp Gets cache swapping setting.
64
 *
65
 * @returns ::NC_NOERR No error.
66
 * @returns ::NC_EBADID Bad ncid.
67
 * @returns ::NC_ENOTVAR Invalid variable ID.
68
 * @returns ::NC_ENOTNC4 Not a netCDF-4 file.
69
 * @author Ed Hartnett
70
 */
71
int
72
NC4_get_var_chunk_cache(int ncid, int varid, size_t *sizep,
73
                        size_t *nelemsp, float *preemptionp)
74
0
{
75
0
    NC *nc;
76
0
    NC_GRP_INFO_T *grp;
77
0
    NC_FILE_INFO_T *h5;
78
0
    NC_VAR_INFO_T *var;
79
0
    int retval;
80
81
    /* Find info for this file and group, and set pointer to each. */
82
0
    if ((retval = nc4_find_nc_grp_h5(ncid, &nc, &grp, &h5)))
83
0
        return retval;
84
0
    assert(nc && grp && h5);
85
86
    /* Find the var. */
87
0
    var = (NC_VAR_INFO_T*)ncindexith(grp->vars,varid);
88
0
    if(!var)
89
0
        return NC_ENOTVAR;
90
0
    assert(var && var->hdr.id == varid);
91
92
    /* Give the user what they want. */
93
0
    if (sizep)
94
0
        *sizep = var->chunkcache.size;
95
0
    if (nelemsp)
96
0
        *nelemsp = var->chunkcache.nelems;
97
0
    if (preemptionp)
98
0
        *preemptionp = var->chunkcache.preemption;
99
100
0
    return NC_NOERR;
101
0
}
102
103
/**
104
 * @internal A wrapper for NC4_get_var_chunk_cache(), we need this
105
 * version for fortran.
106
 *
107
 * @param ncid File ID.
108
 * @param varid Variable ID.
109
 * @param sizep Gets size in MB of cache.
110
 * @param nelemsp Gets number of element slots in cache.
111
 * @param preemptionp Gets cache swapping setting.
112
 *
113
 * @returns ::NC_NOERR No error.
114
 * @returns ::NC_EBADID Bad ncid.
115
 * @returns ::NC_ENOTVAR Invalid variable ID.
116
 * @returns ::NC_ENOTNC4 Not a netCDF-4 file.
117
 * @author Ed Hartnett
118
 */
119
int
120
nc_get_var_chunk_cache_ints(int ncid, int varid, int *sizep,
121
                            int *nelemsp, int *preemptionp)
122
0
{
123
0
    size_t real_size, real_nelems;
124
0
    float real_preemption;
125
0
    int ret;
126
127
0
    if ((ret = NC4_get_var_chunk_cache(ncid, varid, &real_size,
128
0
                                       &real_nelems, &real_preemption)))
129
0
        return ret;
130
131
0
    if (sizep)
132
0
        *sizep = real_size / MEGABYTE;
133
0
    if (nelemsp)
134
0
        *nelemsp = (int)real_nelems;
135
0
    if(preemptionp)
136
0
        *preemptionp = (int)(real_preemption * 100);
137
138
0
    return NC_NOERR;
139
0
}
140
141
/**
142
 * @internal Get all the information about a variable. Pass NULL for
143
 * whatever you don't care about. This is the internal function called
144
 * by nc_inq_var(), nc_inq_var_deflate(), nc_inq_var_fletcher32(),
145
 * nc_inq_var_chunking(), nc_inq_var_chunking_ints(),
146
 * nc_inq_var_fill(), nc_inq_var_endian(), nc_inq_var_filter(), and
147
 * nc_inq_var_szip().
148
 *
149
 * @param ncid File ID.
150
 * @param varid Variable ID.
151
 * @param name Gets name.
152
 * @param xtypep Gets type.
153
 * @param ndimsp Gets number of dims.
154
 * @param dimidsp Gets array of dim IDs.
155
 * @param nattsp Gets number of attributes.
156
 * @param shufflep Gets shuffle setting.
157
 * @param deflatep Gets deflate setting.
158
 * @param deflate_levelp Gets deflate level.
159
 * @param fletcher32p Gets fletcher32 setting.
160
 * @param storagep Gets storage setting.
161
 * @param chunksizesp Gets chunksizes.
162
 * @param no_fill Gets fill mode.
163
 * @param fill_valuep Gets fill value.
164
 * @param endiannessp Gets one of ::NC_ENDIAN_BIG ::NC_ENDIAN_LITTLE
165
 * ::NC_ENDIAN_NATIVE
166
 * @param idp Pointer to memory to store filter id.
167
 * @param nparamsp Pointer to memory to store filter parameter count.
168
 * @param params Pointer to vector of unsigned integers into which
169
 * to store filter parameters.
170
 *
171
 * @returns ::NC_NOERR No error.
172
 * @returns ::NC_EBADID Bad ncid.
173
 * @returns ::NC_ENOTVAR Bad varid.
174
 * @returns ::NC_ENOMEM Out of memory.
175
 * @returns ::NC_EINVAL Invalid input.
176
 * @author Ed Hartnett, Dennis Heimbigner
177
 */
178
int
179
NC4_inq_var_all(int ncid, int varid, char *name, nc_type *xtypep,
180
                int *ndimsp, int *dimidsp, int *nattsp,
181
                int *shufflep, int *deflatep, int *deflate_levelp,
182
                int *fletcher32p, int *storagep, size_t *chunksizesp,
183
                int *no_fill, void *fill_valuep, int *endiannessp,
184
                unsigned int *idp, size_t *nparamsp, unsigned int *params)
185
0
{
186
0
    NC_GRP_INFO_T *grp;
187
0
    NC_FILE_INFO_T *h5;
188
0
    NC_VAR_INFO_T *var;
189
0
    int d;
190
0
    int retval;
191
192
0
    LOG((2, "%s: ncid 0x%x varid %d", __func__, ncid, varid));
193
194
    /* Find info for this file and group, and set pointer to each. */
195
0
    if ((retval = nc4_find_nc_grp_h5(ncid, NULL, &grp, &h5)))
196
0
        return retval;
197
0
    assert(grp && h5);
198
199
    /* If the varid is -1, find the global atts and call it a day. */
200
0
    if (varid == NC_GLOBAL && nattsp)
201
0
    {
202
0
        *nattsp = ncindexcount(grp->att);
203
0
        return NC_NOERR;
204
0
    }
205
206
    /* Find the var. */
207
0
    if (!(var = (NC_VAR_INFO_T *)ncindexith(grp->vars, varid)))
208
0
        return NC_ENOTVAR;
209
0
    assert(var && var->hdr.id == varid);
210
211
    /* Copy the data to the user's data buffers. */
212
0
    if (name)
213
0
        strcpy(name, var->hdr.name);
214
0
    if (xtypep)
215
0
        *xtypep = var->type_info->hdr.id;
216
0
    if (ndimsp)
217
0
        *ndimsp = var->ndims;
218
0
    if (dimidsp)
219
0
        for (d = 0; d < var->ndims; d++)
220
0
            dimidsp[d] = var->dimids[d];
221
0
    if (nattsp)
222
0
        *nattsp = ncindexcount(var->att);
223
224
    /* Did the user want the chunksizes? */
225
0
    if (var->storage == NC_CHUNKED && chunksizesp)
226
0
    {
227
0
        for (d = 0; d < var->ndims; d++)
228
0
        {
229
0
            chunksizesp[d] = var->chunksizes[d];
230
0
            LOG((4, "chunksizesp[%d]=%d", d, chunksizesp[d]));
231
0
        }
232
0
    }
233
234
    /* Did the user inquire about the storage? */
235
0
    if (storagep)
236
0
  *storagep = var->storage;
237
238
    /* Filter stuff. */
239
0
    if (shufflep) {
240
0
  retval = nc_inq_var_filter_info(ncid,varid,H5Z_FILTER_SHUFFLE,0,NULL);
241
0
  if(retval && retval != NC_ENOFILTER) return retval;
242
0
  *shufflep = (retval == NC_NOERR?1:0);
243
0
    }
244
0
    if (fletcher32p) {
245
0
  retval = nc_inq_var_filter_info(ncid,varid,H5Z_FILTER_FLETCHER32,0,NULL);
246
0
  if(retval && retval != NC_ENOFILTER) return retval;
247
0
        *fletcher32p = (retval == NC_NOERR?1:0);
248
0
    }
249
0
    if (deflatep)
250
0
  return NC_EFILTER;
251
252
0
    if (idp) {
253
0
  return NC_EFILTER;
254
0
    }
255
256
    /* Fill value stuff. */
257
0
    if (no_fill)
258
0
        *no_fill = (int)var->no_fill;
259
260
    /* Don't do a thing with fill_valuep if no_fill mode is set for
261
     * this var, or if fill_valuep is NULL. */
262
0
    if (!var->no_fill && fill_valuep)
263
0
    {
264
        /* Do we have a fill value for this var? */
265
0
        if (var->fill_value)
266
#ifdef SEPDATA
267
        {
268
            if (var->type_info->nc_type_class == NC_STRING)
269
            {
270
                assert(*(char **)var->fill_value);
271
                /* This will allocate memory and copy the string. */
272
                if (!(*(char **)fill_valuep = strdup(*(char **)var->fill_value)))
273
                {
274
                    free(*(char **)fill_valuep);
275
                    return NC_ENOMEM;
276
                }
277
            }
278
            else
279
            {
280
                assert(var->type_info->size);
281
                memcpy(fill_valuep, var->fill_value, var->type_info->size);
282
            }
283
        }
284
#else
285
0
        {
286
0
      int xtype = var->type_info->hdr.id;
287
0
      if((retval = nc_copy_data(ncid,xtype,var->fill_value,1,fill_valuep))) return retval;
288
0
  }
289
0
#endif
290
0
        else
291
0
        {
292
#ifdef SEPDATA
293
      if (var->type_info->nc_type_class == NC_STRING)
294
            {
295
                if (!(*(char **)fill_valuep = calloc(1, sizeof(char *))))
296
                    return NC_ENOMEM;
297
298
                if ((retval = nc4_get_default_fill_value(var->type_info->hdr.ud, (char **)fill_valuep)))
299
                {
300
                    free(*(char **)fill_valuep);
301
                    return retval;
302
                }
303
            }
304
            else
305
            {
306
                if ((retval = nc4_get_default_fill_value(var->type_info->hdr.id, fill_valuep)))
307
                    return retval;
308
            }
309
#else
310
0
            if ((retval = nc4_get_default_fill_value(var->type_info, fill_valuep)))
311
0
                    return retval;
312
0
#endif
313
0
        }
314
0
    }
315
316
    /* Does the user want the endianness of this variable? */
317
0
    if (endiannessp)
318
0
        *endiannessp = var->endianness;
319
320
0
    return NC_NOERR;
321
0
}
322
323
/**
324
 * @internal Inquire about chunking settings for a var. This is used
325
 * by the fortran API.
326
 *
327
 * @param ncid File ID.
328
 * @param varid Variable ID.
329
 * @param storagep Gets contiguous setting.
330
 * @param chunksizesp Gets chunksizes.
331
 *
332
 * @returns ::NC_NOERR No error.
333
 * @returns ::NC_EBADID Bad ncid.
334
 * @returns ::NC_ENOTVAR Invalid variable ID.
335
 * @returns ::NC_EINVAL Invalid input
336
 * @returns ::NC_ENOMEM Out of memory.
337
 * @author Ed Hartnett
338
 */
339
int
340
nc_inq_var_chunking_ints(int ncid, int varid, int *storagep, int *chunksizesp)
341
0
{
342
0
    NC_VAR_INFO_T *var;
343
0
    size_t *cs = NULL;
344
0
    int i, retval;
345
346
    /* Get pointer to the var. */
347
0
    if ((retval = nc4_find_grp_h5_var(ncid, varid, NULL, NULL, &var)))
348
0
        return retval;
349
0
    assert(var);
350
351
    /* Allocate space for the size_t copy of the chunksizes array. */
352
0
    if (var->ndims)
353
0
        if (!(cs = malloc(var->ndims * sizeof(size_t))))
354
0
            return NC_ENOMEM;
355
356
    /* Call the netcdf-4 version directly. */
357
0
    retval = NC4_inq_var_all(ncid, varid, NULL, NULL, NULL, NULL, NULL,
358
0
                             NULL, NULL, NULL, NULL, storagep, cs, NULL,
359
0
                             NULL, NULL, NULL, NULL, NULL);
360
361
    /* Copy from size_t array. */
362
0
    if (!retval && chunksizesp && var->storage == NC_CHUNKED)
363
0
    {
364
0
        for (i = 0; i < var->ndims; i++)
365
0
        {
366
0
            chunksizesp[i] = (int)cs[i];
367
0
            if (cs[i] > NC_MAX_INT)
368
0
                retval = NC_ERANGE;
369
0
        }
370
0
    }
371
372
0
    if (var->ndims)
373
0
        free(cs);
374
0
    return retval;
375
0
}
376
377
/**
378
 * @internal Find the ID of a variable, from the name. This function
379
 * is called by nc_inq_varid().
380
 *
381
 * @param ncid File ID.
382
 * @param name Name of the variable.
383
 * @param varidp Gets variable ID.
384
385
 * @returns ::NC_NOERR No error.
386
 * @returns ::NC_EBADID Bad ncid.
387
 * @returns ::NC_ENOTVAR Bad variable ID.
388
 */
389
int
390
NC4_inq_varid(int ncid, const char *name, int *varidp)
391
0
{
392
0
    NC *nc;
393
0
    NC_GRP_INFO_T *grp;
394
0
    NC_VAR_INFO_T *var;
395
0
    char norm_name[NC_MAX_NAME + 1];
396
0
    int retval;
397
398
0
    if (!name)
399
0
        return NC_EINVAL;
400
0
    if (!varidp)
401
0
        return NC_NOERR;
402
403
0
    LOG((2, "%s: ncid 0x%x name %s", __func__, ncid, name));
404
405
    /* Find info for this file and group, and set pointer to each. */
406
0
    if ((retval = nc4_find_nc_grp_h5(ncid, &nc, &grp, NULL)))
407
0
        return retval;
408
409
    /* Normalize name. */
410
0
    if ((retval = nc4_normalize_name(name, norm_name)))
411
0
        return retval;
412
413
    /* Find var of this name. */
414
0
    var = (NC_VAR_INFO_T*)ncindexlookup(grp->vars,norm_name);
415
0
    if(var)
416
0
    {
417
0
        *varidp = var->hdr.id;
418
0
        return NC_NOERR;
419
0
    }
420
0
    return NC_ENOTVAR;
421
0
}
422
423
/**
424
 * @internal
425
 *
426
 * This function will change the parallel access of a variable from
427
 * independent to collective.
428
 *
429
 * @param ncid File ID.
430
 * @param varid Variable ID.
431
 * @param par_access NC_COLLECTIVE or NC_INDEPENDENT.
432
 *
433
 * @returns ::NC_NOERR No error.
434
 * @returns ::NC_EBADID Invalid ncid passed.
435
 * @returns ::NC_ENOTVAR Invalid varid passed.
436
 * @returns ::NC_ENOPAR LFile was not opened with nc_open_par/nc_create_par.
437
 * @returns ::NC_EINVAL Invalid par_access specified.
438
 * @returns ::NC_NOERR for success
439
 * @author Ed Hartnett, Dennis Heimbigner
440
 */
441
int
442
NC4_var_par_access(int ncid, int varid, int par_access)
443
0
{
444
0
#ifndef USE_PARALLEL4
445
0
    NC_UNUSED(ncid);
446
0
    NC_UNUSED(varid);
447
0
    NC_UNUSED(par_access);
448
0
    return NC_ENOPAR;
449
#else
450
    NC *nc;
451
    NC_GRP_INFO_T *grp;
452
    NC_FILE_INFO_T *h5;
453
    NC_VAR_INFO_T *var;
454
    int retval;
455
456
    LOG((1, "%s: ncid 0x%x varid %d par_access %d", __func__, ncid,
457
         varid, par_access));
458
459
    if (par_access != NC_INDEPENDENT && par_access != NC_COLLECTIVE)
460
        return NC_EINVAL;
461
462
    /* Find info for this file and group, and set pointer to each. */
463
    if ((retval = nc4_find_nc_grp_h5(ncid, &nc, &grp, &h5)))
464
        return retval;
465
466
    /* This function only for files opened with nc_open_par or nc_create_par. */
467
    if (!h5->parallel)
468
        return NC_ENOPAR;
469
470
    /* Find the var, and set its preference. */
471
    var = (NC_VAR_INFO_T*)ncindexith(grp->vars,varid);
472
    if (!var) return NC_ENOTVAR;
473
    assert(var->hdr.id == varid);
474
475
    /* If zlib, shuffle, or fletcher32 filters are in use, then access
476
     * must be collective. Fail an attempt to set such a variable to
477
     * independent access. */
478
    if (nclistlength((NClist*)var->filters) > 0 &&
479
        par_access == NC_INDEPENDENT)
480
        return NC_EINVAL;
481
482
    if (par_access)
483
        var->parallel_access = NC_COLLECTIVE;
484
    else
485
        var->parallel_access = NC_INDEPENDENT;
486
    return NC_NOERR;
487
#endif /* USE_PARALLEL4 */
488
0
}
489
490
/**
491
 * @internal Copy data from one buffer to another, performing
492
 * appropriate data conversion.
493
 *
494
 * This function will copy data from one buffer to another, in
495
 * accordance with the types. Range errors will be noted, and the fill
496
 * value used (or the default fill value if none is supplied) for
497
 * values that overflow the type.
498
 *
499
 * This function applies quantization to float and double data, if
500
 * desired. The code to do this is derived from the corresponding 
501
 * filter in the CCR project (e.g., 
502
 * https://github.com/ccr/ccr/blob/master/hdf5_plugins/BITGROOM/src/H5Zbitgroom.c).
503
 *
504
 * @param src Pointer to source of data.
505
 * @param dest Pointer that gets data.
506
 * @param src_type Type ID of source data.
507
 * @param dest_type Type ID of destination data.
508
 * @param len Number of elements of data to copy.
509
 * @param range_error Pointer that gets 1 if there was a range error.
510
 * @param fill_value The fill value.
511
 * @param strict_nc3 Non-zero if strict model in effect.
512
 * @param quantize_mode May be ::NC_NOQUANTIZE, ::NC_QUANTIZE_BITGROOM, 
513
 * ::NC_QUANTIZE_GRANULARBR, or ::NC_QUANTIZE_BITROUND.
514
 * @param nsd Number of significant digits for quantize. Ignored
515
 * unless quantize_mode is ::NC_QUANTIZE_BITGROOM, 
516
 * ::NC_QUANTIZE_GRANULARBR, or ::NC_QUANTIZE_BITROUND
517
 * 
518
 * @returns ::NC_NOERR No error.
519
 * @returns ::NC_EBADTYPE Type not found.
520
 * @author Ed Hartnett, Dennis Heimbigner
521
 */
522
int
523
nc4_convert_type(const void *src, void *dest, const nc_type src_type,
524
                 const nc_type dest_type, const size_t len, int *range_error,
525
                 const void *fill_value, int strict_nc3, int quantize_mode,
526
     int nsd)
527
0
{
528
    /* These vars are used with quantize feature. */
529
0
    const double bit_per_dgt = M_LN10 / M_LN2; /* 3.32 [frc] Bits per decimal digit of precision  = log2(10) */
530
0
    const double dgt_per_bit= M_LN2 / M_LN10; /* 0.301 [frc] Decimal digits per bit of precision = log10(2) */
531
0
    double mnt; /* [frc] Mantissa, 0.5 <= mnt < 1.0 */
532
0
    double mnt_fabs; /* [frc] fabs(mantissa) */
533
0
    double mnt_log10_fabs; /* [frc] log10(fabs(mantissa))) */
534
0
    double val; /* [frc] Copy of input value to avoid indirection */
535
0
    double mss_val_cmp_dbl; /* Missing value for comparison to double precision values */
536
0
    float mss_val_cmp_flt; /* Missing value for comparison to single precision values */
537
0
    int bit_xpl_nbr_zro; /* [nbr] Number of explicit bits to zero */
538
0
    int dgt_nbr; /* [nbr] Number of digits before decimal point */
539
0
    int qnt_pwr; /* [nbr] Power of two in quantization mask: qnt_msk = 2^qnt_pwr */
540
0
    int xpn_bs2; /* [nbr] Binary exponent xpn_bs2 in val = sign(val) * 2^xpn_bs2 * mnt, 0.5 < mnt <= 1.0 */
541
0
    size_t idx;
542
0
    unsigned int *u32_ptr;
543
0
    unsigned int msk_f32_u32_zro;
544
0
    unsigned int msk_f32_u32_one;
545
0
    unsigned int msk_f32_u32_hshv;
546
0
    unsigned long long int *u64_ptr;
547
0
    unsigned long long int msk_f64_u64_zro;
548
0
    unsigned long long int msk_f64_u64_one;
549
0
    unsigned long long int msk_f64_u64_hshv;
550
0
    unsigned short prc_bnr_xpl_rqr; /* [nbr] Explicitly represented binary digits required to retain */
551
0
    ptr_unn op1; /* I/O [frc] Values to quantize */
552
    
553
0
    char *cp, *cp1;
554
0
    float *fp, *fp1;
555
0
    double *dp, *dp1;
556
0
    int *ip, *ip1;
557
0
    short *sp, *sp1;
558
0
    signed char *bp, *bp1;
559
0
    unsigned char *ubp, *ubp1;
560
0
    unsigned short *usp, *usp1;
561
0
    unsigned int *uip, *uip1;
562
0
    long long *lip, *lip1;
563
0
    unsigned long long *ulip, *ulip1;
564
0
    size_t count = 0;
565
566
0
    *range_error = 0;
567
0
    LOG((3, "%s: len %d src_type %d dest_type %d", __func__, len, src_type,
568
0
         dest_type));
569
570
    /* If quantize is in use, set up some values. Quantize can only be
571
     * used when the destination type is NC_FLOAT or NC_DOUBLE. */
572
0
    if (quantize_mode != NC_NOQUANTIZE)
573
0
      {
574
0
        assert(dest_type == NC_FLOAT || dest_type == NC_DOUBLE);
575
576
  /* Parameters shared by all quantization codecs */
577
0
        if (dest_type == NC_FLOAT)
578
0
    {
579
            /* Determine the fill value. */
580
0
            if (fill_value)
581
0
        mss_val_cmp_flt = *(float *)fill_value;
582
0
            else
583
0
        mss_val_cmp_flt = NC_FILL_FLOAT;
584
585
0
    }
586
0
        else
587
0
    {
588
  
589
            /* Determine the fill value. */
590
0
            if (fill_value)
591
0
        mss_val_cmp_dbl = *(double *)fill_value;
592
0
            else
593
0
        mss_val_cmp_dbl = NC_FILL_DOUBLE;
594
595
0
    }
596
597
  /* Set parameters used by BitGroom and BitRound here, outside value loop.
598
     Equivalent parameters used by GranularBR are set inside value loop,
599
     since keep bits and thus masks can change for every value. */
600
0
  if (quantize_mode == NC_QUANTIZE_BITGROOM ||
601
0
      quantize_mode == NC_QUANTIZE_BITROUND )
602
0
    {
603
604
0
      if (quantize_mode == NC_QUANTIZE_BITGROOM){
605
606
        /* BitGroom interprets nsd as number of significant decimal digits
607
         * Must convert that to number of significant bits to preserve
608
         * How many bits to preserve? Being conservative, we round up the
609
         * exact binary digits of precision. Add one because the first bit
610
         * is implicit not explicit but corner cases prevent our taking
611
         * advantage of this. */
612
0
        prc_bnr_xpl_rqr = (unsigned short)ceil(nsd * bit_per_dgt) + 1;
613
614
0
      }else if (quantize_mode == NC_QUANTIZE_BITROUND){
615
616
        /* BitRound interprets nsd as number of significant binary digits (bits) */
617
0
        prc_bnr_xpl_rqr = nsd;
618
        
619
0
      }
620
      
621
0
      if (dest_type == NC_FLOAT)
622
0
        {
623
624
0
    bit_xpl_nbr_zro = BIT_XPL_NBR_SGN_FLT - prc_bnr_xpl_rqr;
625
626
    /* Create mask */
627
0
    msk_f32_u32_zro = 0u; /* Zero all bits */
628
0
    msk_f32_u32_zro = ~msk_f32_u32_zro; /* Turn all bits to ones */
629
    
630
    /* BitShave mask for AND: Left shift zeros into bits to be
631
     * rounded, leave ones in untouched bits. */
632
0
    msk_f32_u32_zro <<= bit_xpl_nbr_zro;
633
    
634
    /* BitSet mask for OR: Put ones into bits to be set, zeros in
635
     * untouched bits. */
636
0
    msk_f32_u32_one = ~msk_f32_u32_zro;
637
638
    /* BitRound mask for ADD: Set one bit: the MSB of LSBs */
639
0
    msk_f32_u32_hshv=msk_f32_u32_one & (msk_f32_u32_zro >> 1);
640
641
0
        }
642
0
      else
643
0
        {
644
645
0
    bit_xpl_nbr_zro = BIT_XPL_NBR_SGN_DBL - prc_bnr_xpl_rqr;
646
    /* Create mask. */
647
0
    msk_f64_u64_zro = 0ul; /* Zero all bits. */
648
0
    msk_f64_u64_zro = ~msk_f64_u64_zro; /* Turn all bits to ones. */
649
    
650
    /* BitShave mask for AND: Left shift zeros into bits to be
651
     * rounded, leave ones in untouched bits. */
652
0
    msk_f64_u64_zro <<= bit_xpl_nbr_zro;
653
    
654
    /* BitSet mask for OR: Put ones into bits to be set, zeros in
655
     * untouched bits. */
656
0
    msk_f64_u64_one =~ msk_f64_u64_zro;
657
658
    /* BitRound mask for ADD: Set one bit: the MSB of LSBs */
659
0
    msk_f64_u64_hshv = msk_f64_u64_one & (msk_f64_u64_zro >> 1);
660
661
0
        }
662
663
0
    }
664
    
665
0
      } /* endif quantize */
666
      
667
    /* OK, this is ugly. If you can think of anything better, I'm open
668
       to suggestions!
669
670
       Note that we don't use a default fill value for type
671
       NC_BYTE. This is because Lord Voldemort cast a nofilleramous spell
672
       at Harry Potter, but it bounced off his scar and hit the netcdf-4
673
       code.
674
    */
675
0
    switch (src_type)
676
0
    {
677
0
    case NC_CHAR:
678
0
        switch (dest_type)
679
0
        {
680
0
        case NC_CHAR:
681
0
            for (cp = (char *)src, cp1 = dest; count < len; count++)
682
0
                *cp1++ = *cp++;
683
0
            break;
684
0
        default:
685
0
            LOG((0, "%s: Unknown destination type.", __func__));
686
0
        }
687
0
        break;
688
689
0
    case NC_BYTE:
690
0
        switch (dest_type)
691
0
        {
692
0
        case NC_BYTE:
693
0
            for (bp = (signed char *)src, bp1 = dest; count < len; count++)
694
0
                *bp1++ = *bp++;
695
0
            break;
696
0
        case NC_UBYTE:
697
0
            for (bp = (signed char *)src, ubp = dest; count < len; count++)
698
0
            {
699
0
                if (*bp < 0)
700
0
                    (*range_error)++;
701
0
                *ubp++ = *bp++;
702
0
            }
703
0
            break;
704
0
        case NC_SHORT:
705
0
            for (bp = (signed char *)src, sp = dest; count < len; count++)
706
0
                *sp++ = *bp++;
707
0
            break;
708
0
        case NC_USHORT:
709
0
            for (bp = (signed char *)src, usp = dest; count < len; count++)
710
0
            {
711
0
                if (*bp < 0)
712
0
                    (*range_error)++;
713
0
                *usp++ = *bp++;
714
0
            }
715
0
            break;
716
0
        case NC_INT:
717
0
            for (bp = (signed char *)src, ip = dest; count < len; count++)
718
0
                *ip++ = *bp++;
719
0
            break;
720
0
        case NC_UINT:
721
0
            for (bp = (signed char *)src, uip = dest; count < len; count++)
722
0
            {
723
0
                if (*bp < 0)
724
0
                    (*range_error)++;
725
0
                *uip++ = *bp++;
726
0
            }
727
0
            break;
728
0
        case NC_INT64:
729
0
            for (bp = (signed char *)src, lip = dest; count < len; count++)
730
0
                *lip++ = *bp++;
731
0
            break;
732
0
        case NC_UINT64:
733
0
            for (bp = (signed char *)src, ulip = dest; count < len; count++)
734
0
            {
735
0
                if (*bp < 0)
736
0
                    (*range_error)++;
737
0
                *ulip++ = *bp++;
738
0
            }
739
0
            break;
740
0
        case NC_FLOAT:
741
0
      for (bp = (signed char *)src, fp = dest; count < len; count++)
742
0
    *fp++ = *bp++;
743
0
            break;
744
0
        case NC_DOUBLE:
745
0
            for (bp = (signed char *)src, dp = dest; count < len; count++)
746
0
                *dp++ = *bp++;
747
0
            break;
748
0
        default:
749
0
            LOG((0, "%s: unexpected dest type. src_type %d, dest_type %d",
750
0
                 __func__, src_type, dest_type));
751
0
            return NC_EBADTYPE;
752
0
        }
753
0
        break;
754
755
0
    case NC_UBYTE:
756
0
        switch (dest_type)
757
0
        {
758
0
        case NC_BYTE:
759
0
            for (ubp = (unsigned char *)src, bp = dest; count < len; count++)
760
0
            {
761
0
                if (!strict_nc3 && *ubp > X_SCHAR_MAX)
762
0
                    (*range_error)++;
763
0
                *bp++ = *ubp++;
764
0
            }
765
0
            break;
766
0
        case NC_SHORT:
767
0
            for (ubp = (unsigned char *)src, sp = dest; count < len; count++)
768
0
                *sp++ = *ubp++;
769
0
            break;
770
0
        case NC_UBYTE:
771
0
            for (ubp = (unsigned char *)src, ubp1 = dest; count < len; count++)
772
0
                *ubp1++ = *ubp++;
773
0
            break;
774
0
        case NC_USHORT:
775
0
            for (ubp = (unsigned char *)src, usp = dest; count < len; count++)
776
0
                *usp++ = *ubp++;
777
0
            break;
778
0
        case NC_INT:
779
0
            for (ubp = (unsigned char *)src, ip = dest; count < len; count++)
780
0
                *ip++ = *ubp++;
781
0
            break;
782
0
        case NC_UINT:
783
0
            for (ubp = (unsigned char *)src, uip = dest; count < len; count++)
784
0
                *uip++ = *ubp++;
785
0
            break;
786
0
        case NC_INT64:
787
0
            for (ubp = (unsigned char *)src, lip = dest; count < len; count++)
788
0
                *lip++ = *ubp++;
789
0
            break;
790
0
        case NC_UINT64:
791
0
            for (ubp = (unsigned char *)src, ulip = dest; count < len; count++)
792
0
                *ulip++ = *ubp++;
793
0
            break;
794
0
        case NC_FLOAT:
795
0
            for (ubp = (unsigned char *)src, fp = dest; count < len; count++)
796
0
                *fp++ = *ubp++;
797
0
            break;
798
0
        case NC_DOUBLE:
799
0
            for (ubp = (unsigned char *)src, dp = dest; count < len; count++)
800
0
                *dp++ = *ubp++;
801
0
            break;
802
0
        default:
803
0
            LOG((0, "%s: unexpected dest type. src_type %d, dest_type %d",
804
0
                 __func__, src_type, dest_type));
805
0
            return NC_EBADTYPE;
806
0
        }
807
0
        break;
808
809
0
    case NC_SHORT:
810
0
        switch (dest_type)
811
0
        {
812
0
        case NC_UBYTE:
813
0
            for (sp = (short *)src, ubp = dest; count < len; count++)
814
0
            {
815
0
                if (*sp > X_UCHAR_MAX || *sp < 0)
816
0
                    (*range_error)++;
817
0
                *ubp++ = *sp++;
818
0
            }
819
0
            break;
820
0
        case NC_BYTE:
821
0
            for (sp = (short *)src, bp = dest; count < len; count++)
822
0
            {
823
0
                if (*sp > X_SCHAR_MAX || *sp < X_SCHAR_MIN)
824
0
                    (*range_error)++;
825
0
                *bp++ = *sp++;
826
0
            }
827
0
            break;
828
0
        case NC_SHORT:
829
0
            for (sp = (short *)src, sp1 = dest; count < len; count++)
830
0
                *sp1++ = *sp++;
831
0
            break;
832
0
        case NC_USHORT:
833
0
            for (sp = (short *)src, usp = dest; count < len; count++)
834
0
            {
835
0
                if (*sp < 0)
836
0
                    (*range_error)++;
837
0
                *usp++ = *sp++;
838
0
            }
839
0
            break;
840
0
        case NC_INT:
841
0
            for (sp = (short *)src, ip = dest; count < len; count++)
842
0
                *ip++ = *sp++;
843
0
            break;
844
0
        case NC_UINT:
845
0
            for (sp = (short *)src, uip = dest; count < len; count++)
846
0
            {
847
0
                if (*sp < 0)
848
0
                    (*range_error)++;
849
0
                *uip++ = *sp++;
850
0
            }
851
0
            break;
852
0
        case NC_INT64:
853
0
            for (sp = (short *)src, lip = dest; count < len; count++)
854
0
                *lip++ = *sp++;
855
0
            break;
856
0
        case NC_UINT64:
857
0
            for (sp = (short *)src, ulip = dest; count < len; count++)
858
0
            {
859
0
                if (*sp < 0)
860
0
                    (*range_error)++;
861
0
                *ulip++ = *sp++;
862
0
            }
863
0
            break;
864
0
        case NC_FLOAT:
865
0
            for (sp = (short *)src, fp = dest; count < len; count++)
866
0
                *fp++ = *sp++;
867
0
            break;
868
0
        case NC_DOUBLE:
869
0
            for (sp = (short *)src, dp = dest; count < len; count++)
870
0
                *dp++ = *sp++;
871
0
            break;
872
0
        default:
873
0
            LOG((0, "%s: unexpected dest type. src_type %d, dest_type %d",
874
0
                 __func__, src_type, dest_type));
875
0
            return NC_EBADTYPE;
876
0
        }
877
0
        break;
878
879
0
    case NC_USHORT:
880
0
        switch (dest_type)
881
0
        {
882
0
        case NC_UBYTE:
883
0
            for (usp = (unsigned short *)src, ubp = dest; count < len; count++)
884
0
            {
885
0
                if (*usp > X_UCHAR_MAX)
886
0
                    (*range_error)++;
887
0
                *ubp++ = *usp++;
888
0
            }
889
0
            break;
890
0
        case NC_BYTE:
891
0
            for (usp = (unsigned short *)src, bp = dest; count < len; count++)
892
0
            {
893
0
                if (*usp > X_SCHAR_MAX)
894
0
                    (*range_error)++;
895
0
                *bp++ = *usp++;
896
0
            }
897
0
            break;
898
0
        case NC_SHORT:
899
0
            for (usp = (unsigned short *)src, sp = dest; count < len; count++)
900
0
            {
901
0
                if (*usp > X_SHORT_MAX)
902
0
                    (*range_error)++;
903
0
                *sp++ = *usp++;
904
0
            }
905
0
            break;
906
0
        case NC_USHORT:
907
0
            for (usp = (unsigned short *)src, usp1 = dest; count < len; count++)
908
0
                *usp1++ = *usp++;
909
0
            break;
910
0
        case NC_INT:
911
0
            for (usp = (unsigned short *)src, ip = dest; count < len; count++)
912
0
                *ip++ = *usp++;
913
0
            break;
914
0
        case NC_UINT:
915
0
            for (usp = (unsigned short *)src, uip = dest; count < len; count++)
916
0
                *uip++ = *usp++;
917
0
            break;
918
0
        case NC_INT64:
919
0
            for (usp = (unsigned short *)src, lip = dest; count < len; count++)
920
0
                *lip++ = *usp++;
921
0
            break;
922
0
        case NC_UINT64:
923
0
            for (usp = (unsigned short *)src, ulip = dest; count < len; count++)
924
0
                *ulip++ = *usp++;
925
0
            break;
926
0
        case NC_FLOAT:
927
0
            for (usp = (unsigned short *)src, fp = dest; count < len; count++)
928
0
                *fp++ = *usp++;
929
0
            break;
930
0
        case NC_DOUBLE:
931
0
            for (usp = (unsigned short *)src, dp = dest; count < len; count++)
932
0
                *dp++ = *usp++;
933
0
            break;
934
0
        default:
935
0
            LOG((0, "%s: unexpected dest type. src_type %d, dest_type %d",
936
0
                 __func__, src_type, dest_type));
937
0
            return NC_EBADTYPE;
938
0
        }
939
0
        break;
940
941
0
    case NC_INT:
942
0
        switch (dest_type)
943
0
        {
944
0
        case NC_UBYTE:
945
0
            for (ip = (int *)src, ubp = dest; count < len; count++)
946
0
            {
947
0
                if (*ip > X_UCHAR_MAX || *ip < 0)
948
0
                    (*range_error)++;
949
0
                *ubp++ = *ip++;
950
0
            }
951
0
            break;
952
0
        case NC_BYTE:
953
0
            for (ip = (int *)src, bp = dest; count < len; count++)
954
0
            {
955
0
                if (*ip > X_SCHAR_MAX || *ip < X_SCHAR_MIN)
956
0
                    (*range_error)++;
957
0
                *bp++ = *ip++;
958
0
            }
959
0
            break;
960
0
        case NC_SHORT:
961
0
            for (ip = (int *)src, sp = dest; count < len; count++)
962
0
            {
963
0
                if (*ip > X_SHORT_MAX || *ip < X_SHORT_MIN)
964
0
                    (*range_error)++;
965
0
                *sp++ = *ip++;
966
0
            }
967
0
            break;
968
0
        case NC_USHORT:
969
0
            for (ip = (int *)src, usp = dest; count < len; count++)
970
0
            {
971
0
                if (*ip > X_USHORT_MAX || *ip < 0)
972
0
                    (*range_error)++;
973
0
                *usp++ = *ip++;
974
0
            }
975
0
            break;
976
0
        case NC_INT: /* src is int */
977
0
            for (ip = (int *)src, ip1 = dest; count < len; count++)
978
0
            {
979
0
                if (*ip > X_INT_MAX || *ip < X_INT_MIN)
980
0
                    (*range_error)++;
981
0
                *ip1++ = *ip++;
982
0
            }
983
0
            break;
984
0
        case NC_UINT:
985
0
            for (ip = (int *)src, uip = dest; count < len; count++)
986
0
            {
987
0
                if (*ip > X_UINT_MAX || *ip < 0)
988
0
                    (*range_error)++;
989
0
                *uip++ = *ip++;
990
0
            }
991
0
            break;
992
0
        case NC_INT64:
993
0
            for (ip = (int *)src, lip = dest; count < len; count++)
994
0
                *lip++ = *ip++;
995
0
            break;
996
0
        case NC_UINT64:
997
0
            for (ip = (int *)src, ulip = dest; count < len; count++)
998
0
            {
999
0
                if (*ip < 0)
1000
0
                    (*range_error)++;
1001
0
                *ulip++ = *ip++;
1002
0
            }
1003
0
            break;
1004
0
        case NC_FLOAT:
1005
0
            for (ip = (int *)src, fp = dest; count < len; count++)
1006
0
                *fp++ = *ip++;
1007
0
            break;
1008
0
        case NC_DOUBLE:
1009
0
            for (ip = (int *)src, dp = dest; count < len; count++)
1010
0
                *dp++ = *ip++;
1011
0
            break;
1012
0
        default:
1013
0
            LOG((0, "%s: unexpected dest type. src_type %d, dest_type %d",
1014
0
                 __func__, src_type, dest_type));
1015
0
            return NC_EBADTYPE;
1016
0
        }
1017
0
        break;
1018
1019
0
    case NC_UINT:
1020
0
        switch (dest_type)
1021
0
        {
1022
0
        case NC_UBYTE:
1023
0
            for (uip = (unsigned int *)src, ubp = dest; count < len; count++)
1024
0
            {
1025
0
                if (*uip > X_UCHAR_MAX)
1026
0
                    (*range_error)++;
1027
0
                *ubp++ = *uip++;
1028
0
            }
1029
0
            break;
1030
0
        case NC_BYTE:
1031
0
            for (uip = (unsigned int *)src, bp = dest; count < len; count++)
1032
0
            {
1033
0
                if (*uip > X_SCHAR_MAX)
1034
0
                    (*range_error)++;
1035
0
                *bp++ = *uip++;
1036
0
            }
1037
0
            break;
1038
0
        case NC_SHORT:
1039
0
            for (uip = (unsigned int *)src, sp = dest; count < len; count++)
1040
0
            {
1041
0
                if (*uip > X_SHORT_MAX)
1042
0
                    (*range_error)++;
1043
0
                *sp++ = *uip++;
1044
0
            }
1045
0
            break;
1046
0
        case NC_USHORT:
1047
0
            for (uip = (unsigned int *)src, usp = dest; count < len; count++)
1048
0
            {
1049
0
                if (*uip > X_USHORT_MAX)
1050
0
                    (*range_error)++;
1051
0
                *usp++ = *uip++;
1052
0
            }
1053
0
            break;
1054
0
        case NC_INT:
1055
0
            for (uip = (unsigned int *)src, ip = dest; count < len; count++)
1056
0
            {
1057
0
                if (*uip > X_INT_MAX)
1058
0
                    (*range_error)++;
1059
0
                *ip++ = *uip++;
1060
0
            }
1061
0
            break;
1062
0
        case NC_UINT:
1063
0
            for (uip = (unsigned int *)src, uip1 = dest; count < len; count++)
1064
0
            {
1065
0
                if (*uip > X_UINT_MAX)
1066
0
                    (*range_error)++;
1067
0
                *uip1++ = *uip++;
1068
0
            }
1069
0
            break;
1070
0
        case NC_INT64:
1071
0
            for (uip = (unsigned int *)src, lip = dest; count < len; count++)
1072
0
                *lip++ = *uip++;
1073
0
            break;
1074
0
        case NC_UINT64:
1075
0
            for (uip = (unsigned int *)src, ulip = dest; count < len; count++)
1076
0
                *ulip++ = *uip++;
1077
0
            break;
1078
0
        case NC_FLOAT:
1079
0
            for (uip = (unsigned int *)src, fp = dest; count < len; count++)
1080
0
                *fp++ = *uip++;
1081
0
            break;
1082
0
        case NC_DOUBLE:
1083
0
            for (uip = (unsigned int *)src, dp = dest; count < len; count++)
1084
0
                *dp++ = *uip++;
1085
0
            break;
1086
0
        default:
1087
0
            LOG((0, "%s: unexpected dest type. src_type %d, dest_type %d",
1088
0
                 __func__, src_type, dest_type));
1089
0
            return NC_EBADTYPE;
1090
0
        }
1091
0
        break;
1092
1093
0
    case NC_INT64:
1094
0
        switch (dest_type)
1095
0
        {
1096
0
        case NC_UBYTE:
1097
0
            for (lip = (long long *)src, ubp = dest; count < len; count++)
1098
0
            {
1099
0
                if (*lip > X_UCHAR_MAX || *lip < 0)
1100
0
                    (*range_error)++;
1101
0
                *ubp++ = *lip++;
1102
0
            }
1103
0
            break;
1104
0
        case NC_BYTE:
1105
0
            for (lip = (long long *)src, bp = dest; count < len; count++)
1106
0
            {
1107
0
                if (*lip > X_SCHAR_MAX || *lip < X_SCHAR_MIN)
1108
0
                    (*range_error)++;
1109
0
                *bp++ = *lip++;
1110
0
            }
1111
0
            break;
1112
0
        case NC_SHORT:
1113
0
            for (lip = (long long *)src, sp = dest; count < len; count++)
1114
0
            {
1115
0
                if (*lip > X_SHORT_MAX || *lip < X_SHORT_MIN)
1116
0
                    (*range_error)++;
1117
0
                *sp++ = *lip++;
1118
0
            }
1119
0
            break;
1120
0
        case NC_USHORT:
1121
0
            for (lip = (long long *)src, usp = dest; count < len; count++)
1122
0
            {
1123
0
                if (*lip > X_USHORT_MAX || *lip < 0)
1124
0
                    (*range_error)++;
1125
0
                *usp++ = *lip++;
1126
0
            }
1127
0
            break;
1128
0
        case NC_UINT:
1129
0
            for (lip = (long long *)src, uip = dest; count < len; count++)
1130
0
            {
1131
0
                if (*lip > X_UINT_MAX || *lip < 0)
1132
0
                    (*range_error)++;
1133
0
                *uip++ = *lip++;
1134
0
            }
1135
0
            break;
1136
0
        case NC_INT:
1137
0
            for (lip = (long long *)src, ip = dest; count < len; count++)
1138
0
            {
1139
0
                if (*lip > X_INT_MAX || *lip < X_INT_MIN)
1140
0
                    (*range_error)++;
1141
0
                *ip++ = *lip++;
1142
0
            }
1143
0
            break;
1144
0
        case NC_INT64:
1145
0
            for (lip = (long long *)src, lip1 = dest; count < len; count++)
1146
0
                *lip1++ = *lip++;
1147
0
            break;
1148
0
        case NC_UINT64:
1149
0
            for (lip = (long long *)src, ulip = dest; count < len; count++)
1150
0
            {
1151
0
                if (*lip < 0)
1152
0
                    (*range_error)++;
1153
0
                *ulip++ = *lip++;
1154
0
            }
1155
0
            break;
1156
0
        case NC_FLOAT:
1157
0
            for (lip = (long long *)src, fp = dest; count < len; count++)
1158
0
                *fp++ = *lip++;
1159
0
            break;
1160
0
        case NC_DOUBLE:
1161
0
            for (lip = (long long *)src, dp = dest; count < len; count++)
1162
0
                *dp++ = *lip++;
1163
0
            break;
1164
0
        default:
1165
0
            LOG((0, "%s: unexpected dest type. src_type %d, dest_type %d",
1166
0
                 __func__, src_type, dest_type));
1167
0
            return NC_EBADTYPE;
1168
0
        }
1169
0
        break;
1170
1171
0
    case NC_UINT64:
1172
0
        switch (dest_type)
1173
0
        {
1174
0
        case NC_UBYTE:
1175
0
            for (ulip = (unsigned long long *)src, ubp = dest; count < len; count++)
1176
0
            {
1177
0
                if (*ulip > X_UCHAR_MAX)
1178
0
                    (*range_error)++;
1179
0
                *ubp++ = *ulip++;
1180
0
            }
1181
0
            break;
1182
0
        case NC_BYTE:
1183
0
            for (ulip = (unsigned long long *)src, bp = dest; count < len; count++)
1184
0
            {
1185
0
                if (*ulip > X_SCHAR_MAX)
1186
0
                    (*range_error)++;
1187
0
                *bp++ = *ulip++;
1188
0
            }
1189
0
            break;
1190
0
        case NC_SHORT:
1191
0
            for (ulip = (unsigned long long *)src, sp = dest; count < len; count++)
1192
0
            {
1193
0
                if (*ulip > X_SHORT_MAX)
1194
0
                    (*range_error)++;
1195
0
                *sp++ = *ulip++;
1196
0
            }
1197
0
            break;
1198
0
        case NC_USHORT:
1199
0
            for (ulip = (unsigned long long *)src, usp = dest; count < len; count++)
1200
0
            {
1201
0
                if (*ulip > X_USHORT_MAX)
1202
0
                    (*range_error)++;
1203
0
                *usp++ = *ulip++;
1204
0
            }
1205
0
            break;
1206
0
        case NC_UINT:
1207
0
            for (ulip = (unsigned long long *)src, uip = dest; count < len; count++)
1208
0
            {
1209
0
                if (*ulip > X_UINT_MAX)
1210
0
                    (*range_error)++;
1211
0
                *uip++ = *ulip++;
1212
0
            }
1213
0
            break;
1214
0
        case NC_INT:
1215
0
            for (ulip = (unsigned long long *)src, ip = dest; count < len; count++)
1216
0
            {
1217
0
                if (*ulip > X_INT_MAX)
1218
0
                    (*range_error)++;
1219
0
                *ip++ = *ulip++;
1220
0
            }
1221
0
            break;
1222
0
        case NC_INT64:
1223
0
            for (ulip = (unsigned long long *)src, lip = dest; count < len; count++)
1224
0
            {
1225
0
                if (*ulip > X_INT64_MAX)
1226
0
                    (*range_error)++;
1227
0
                *lip++ = *ulip++;
1228
0
            }
1229
0
            break;
1230
0
        case NC_UINT64:
1231
0
            for (ulip = (unsigned long long *)src, ulip1 = dest; count < len; count++)
1232
0
                *ulip1++ = *ulip++;
1233
0
            break;
1234
0
        case NC_FLOAT:
1235
0
            for (ulip = (unsigned long long *)src, fp = dest; count < len; count++)
1236
0
                *fp++ = *ulip++;
1237
0
            break;
1238
0
        case NC_DOUBLE:
1239
0
            for (ulip = (unsigned long long *)src, dp = dest; count < len; count++)
1240
0
                *dp++ = *ulip++;
1241
0
            break;
1242
0
        default:
1243
0
            LOG((0, "%s: unexpected dest type. src_type %d, dest_type %d",
1244
0
                 __func__, src_type, dest_type));
1245
0
            return NC_EBADTYPE;
1246
0
        }
1247
0
        break;
1248
1249
0
    case NC_FLOAT:
1250
0
        switch (dest_type)
1251
0
        {
1252
0
        case NC_UBYTE:
1253
0
            for (fp = (float *)src, ubp = dest; count < len; count++)
1254
0
            {
1255
0
                if (*fp > X_UCHAR_MAX || *fp < 0)
1256
0
                    (*range_error)++;
1257
0
                *ubp++ = *fp++;
1258
0
            }
1259
0
            break;
1260
0
        case NC_BYTE:
1261
0
            for (fp = (float *)src, bp = dest; count < len; count++)
1262
0
            {
1263
0
                if (*fp > (double)X_SCHAR_MAX || *fp < (double)X_SCHAR_MIN)
1264
0
                    (*range_error)++;
1265
0
                *bp++ = *fp++;
1266
0
            }
1267
0
            break;
1268
0
        case NC_SHORT:
1269
0
            for (fp = (float *)src, sp = dest; count < len; count++)
1270
0
            {
1271
0
                if (*fp > (double)X_SHORT_MAX || *fp < (double)X_SHORT_MIN)
1272
0
                    (*range_error)++;
1273
0
                *sp++ = *fp++;
1274
0
            }
1275
0
            break;
1276
0
        case NC_USHORT:
1277
0
            for (fp = (float *)src, usp = dest; count < len; count++)
1278
0
            {
1279
0
                if (*fp > X_USHORT_MAX || *fp < 0)
1280
0
                    (*range_error)++;
1281
0
                *usp++ = *fp++;
1282
0
            }
1283
0
            break;
1284
0
        case NC_UINT:
1285
0
            for (fp = (float *)src, uip = dest; count < len; count++)
1286
0
            {
1287
0
                if (*fp > X_UINT_MAX || *fp < 0)
1288
0
                    (*range_error)++;
1289
0
                *uip++ = *fp++;
1290
0
            }
1291
0
            break;
1292
0
        case NC_INT:
1293
0
            for (fp = (float *)src, ip = dest; count < len; count++)
1294
0
            {
1295
0
                if (*fp > (double)X_INT_MAX || *fp < (double)X_INT_MIN)
1296
0
                    (*range_error)++;
1297
0
                *ip++ = *fp++;
1298
0
            }
1299
0
            break;
1300
0
        case NC_INT64:
1301
0
            for (fp = (float *)src, lip = dest; count < len; count++)
1302
0
            {
1303
0
                if (*fp > X_INT64_MAX || *fp <X_INT64_MIN)
1304
0
                    (*range_error)++;
1305
0
                *lip++ = *fp++;
1306
0
            }
1307
0
            break;
1308
0
        case NC_UINT64:
1309
0
            for (fp = (float *)src, lip = dest; count < len; count++)
1310
0
            {
1311
0
                if (*fp > X_UINT64_MAX || *fp < 0)
1312
0
                    (*range_error)++;
1313
0
                *lip++ = *fp++;
1314
0
            }
1315
0
            break;
1316
0
        case NC_FLOAT:
1317
0
            for (fp = (float *)src, fp1 = dest; count < len; count++)
1318
0
                *fp1++ = *fp++;
1319
0
            break;
1320
0
        case NC_DOUBLE:
1321
0
            for (fp = (float *)src, dp = dest; count < len; count++)
1322
0
                *dp++ = *fp++;
1323
0
            break;
1324
0
        default:
1325
0
            LOG((0, "%s: unexpected dest type. src_type %d, dest_type %d",
1326
0
                 __func__, src_type, dest_type));
1327
0
            return NC_EBADTYPE;
1328
0
        }
1329
0
        break;
1330
1331
0
    case NC_DOUBLE:
1332
0
        switch (dest_type)
1333
0
        {
1334
0
        case NC_UBYTE:
1335
0
            for (dp = (double *)src, ubp = dest; count < len; count++)
1336
0
            {
1337
0
                if (*dp > X_UCHAR_MAX || *dp < 0)
1338
0
                    (*range_error)++;
1339
0
                *ubp++ = *dp++;
1340
0
            }
1341
0
            break;
1342
0
        case NC_BYTE:
1343
0
            for (dp = (double *)src, bp = dest; count < len; count++)
1344
0
            {
1345
0
                if (*dp > X_SCHAR_MAX || *dp < X_SCHAR_MIN)
1346
0
                    (*range_error)++;
1347
0
                *bp++ = *dp++;
1348
0
            }
1349
0
            break;
1350
0
        case NC_SHORT:
1351
0
            for (dp = (double *)src, sp = dest; count < len; count++)
1352
0
            {
1353
0
                if (*dp > X_SHORT_MAX || *dp < X_SHORT_MIN)
1354
0
                    (*range_error)++;
1355
0
                *sp++ = *dp++;
1356
0
            }
1357
0
            break;
1358
0
        case NC_USHORT:
1359
0
            for (dp = (double *)src, usp = dest; count < len; count++)
1360
0
            {
1361
0
                if (*dp > X_USHORT_MAX || *dp < 0)
1362
0
                    (*range_error)++;
1363
0
                *usp++ = *dp++;
1364
0
            }
1365
0
            break;
1366
0
        case NC_UINT:
1367
0
            for (dp = (double *)src, uip = dest; count < len; count++)
1368
0
            {
1369
0
                if (*dp > X_UINT_MAX || *dp < 0)
1370
0
                    (*range_error)++;
1371
0
                *uip++ = *dp++;
1372
0
            }
1373
0
            break;
1374
0
        case NC_INT:
1375
0
            for (dp = (double *)src, ip = dest; count < len; count++)
1376
0
            {
1377
0
                if (*dp > X_INT_MAX || *dp < X_INT_MIN)
1378
0
                    (*range_error)++;
1379
0
                *ip++ = *dp++;
1380
0
            }
1381
0
            break;
1382
0
        case NC_INT64:
1383
0
            for (dp = (double *)src, lip = dest; count < len; count++)
1384
0
            {
1385
0
                if (*dp > X_INT64_MAX || *dp < X_INT64_MIN)
1386
0
                    (*range_error)++;
1387
0
                *lip++ = *dp++;
1388
0
            }
1389
0
            break;
1390
0
        case NC_UINT64:
1391
0
            for (dp = (double *)src, lip = dest; count < len; count++)
1392
0
            {
1393
0
                if (*dp > X_UINT64_MAX || *dp < 0)
1394
0
                    (*range_error)++;
1395
0
                *lip++ = *dp++;
1396
0
            }
1397
0
            break;
1398
0
        case NC_FLOAT:
1399
0
            for (dp = (double *)src, fp = dest; count < len; count++)
1400
0
            {
1401
0
                if (isgreater(*dp, X_FLOAT_MAX) || isless(*dp, X_FLOAT_MIN))
1402
0
                    (*range_error)++;
1403
0
                *fp++ = *dp++;
1404
0
            }
1405
0
            break;
1406
0
        case NC_DOUBLE:
1407
0
            for (dp = (double *)src, dp1 = dest; count < len; count++)
1408
0
                *dp1++ = *dp++;
1409
0
            break;
1410
0
        default:
1411
0
            LOG((0, "%s: unexpected dest type. src_type %d, dest_type %d",
1412
0
                 __func__, src_type, dest_type));
1413
0
            return NC_EBADTYPE;
1414
0
        }
1415
0
        break;
1416
1417
0
    default:
1418
0
        LOG((0, "%s: unexpected src type. src_type %d, dest_type %d",
1419
0
             __func__, src_type, dest_type));
1420
0
        return NC_EBADTYPE;
1421
0
    }
1422
1423
    /* If quantize is in use, determine masks, copy the data, do the
1424
     * quantization. */
1425
0
    if (quantize_mode == NC_QUANTIZE_BITGROOM)
1426
0
    {
1427
0
        if (dest_type == NC_FLOAT)
1428
0
        {
1429
            /* BitGroom: alternately shave and set LSBs */
1430
0
            op1.fp = (float *)dest;
1431
0
            u32_ptr = op1.ui32p;
1432
0
            for (idx = 0L; idx < len; idx += 2L)
1433
0
                if (op1.fp[idx] != mss_val_cmp_flt)
1434
0
                    u32_ptr[idx] &= msk_f32_u32_zro;
1435
0
            for (idx = 1L; idx < len; idx += 2L)
1436
0
                if (op1.fp[idx] != mss_val_cmp_flt && u32_ptr[idx] != 0U) /* Never quantize upwards floating point values of zero */
1437
0
                    u32_ptr[idx] |= msk_f32_u32_one;
1438
0
        }
1439
0
        else
1440
0
        {
1441
            /* BitGroom: alternately shave and set LSBs. */
1442
0
            op1.dp = (double *)dest;
1443
0
            u64_ptr = op1.ui64p;
1444
0
            for (idx = 0L; idx < len; idx += 2L)
1445
0
                if (op1.dp[idx] != mss_val_cmp_dbl)
1446
0
                    u64_ptr[idx] &= msk_f64_u64_zro;
1447
0
            for (idx = 1L; idx < len; idx += 2L)
1448
0
                if (op1.dp[idx] != mss_val_cmp_dbl && u64_ptr[idx] != 0ULL) /* Never quantize upwards floating point values of zero */
1449
0
                    u64_ptr[idx] |= msk_f64_u64_one;
1450
0
        }
1451
0
    } /* endif BitGroom */
1452
1453
0
    if (quantize_mode == NC_QUANTIZE_BITROUND)
1454
0
      {
1455
0
        if (dest_type == NC_FLOAT)
1456
0
    {
1457
            /* BitRound: Quantize to user-specified NSB with IEEE-rounding */
1458
0
            op1.fp = (float *)dest;
1459
0
            u32_ptr = op1.ui32p;
1460
0
            for (idx = 0L; idx < len; idx++){
1461
0
        if (op1.fp[idx] != mss_val_cmp_flt){
1462
0
    u32_ptr[idx] += msk_f32_u32_hshv; /* Add 1 to the MSB of LSBs, carry 1 to mantissa or even exponent */
1463
0
    u32_ptr[idx] &= msk_f32_u32_zro; /* Shave it */
1464
0
        }
1465
0
      }
1466
0
    }
1467
0
        else
1468
0
    {
1469
            /* BitRound: Quantize to user-specified NSB with IEEE-rounding */
1470
0
            op1.dp = (double *)dest;
1471
0
            u64_ptr = op1.ui64p;
1472
0
            for (idx = 0L; idx < len; idx++){
1473
0
        if (op1.dp[idx] != mss_val_cmp_dbl){
1474
0
    u64_ptr[idx] += msk_f64_u64_hshv; /* Add 1 to the MSB of LSBs, carry 1 to mantissa or even exponent */
1475
0
    u64_ptr[idx] &= msk_f64_u64_zro; /* Shave it */
1476
0
        }
1477
0
      }
1478
0
    }
1479
0
      } /* endif BitRound */
1480
    
1481
0
    if (quantize_mode == NC_QUANTIZE_GRANULARBR)
1482
0
    {
1483
0
        if (dest_type == NC_FLOAT)
1484
0
        {
1485
            /* Granular BitRound */
1486
0
            op1.fp = (float *)dest;
1487
0
            u32_ptr = op1.ui32p;
1488
0
            for (idx = 0L; idx < len; idx++)
1489
0
        {
1490
        
1491
0
    if((val = op1.fp[idx]) != mss_val_cmp_flt && u32_ptr[idx] != 0U)
1492
0
      {
1493
0
        mnt = frexp(val, &xpn_bs2); /* DGG19 p. 4102 (8) */
1494
0
        mnt_fabs = fabs(mnt);
1495
0
        mnt_log10_fabs = log10(mnt_fabs);
1496
        /* 20211003 Continuous determination of dgt_nbr improves CR by ~10% */
1497
0
        dgt_nbr = (int)floor(xpn_bs2 * dgt_per_bit + mnt_log10_fabs) + 1; /* DGG19 p. 4102 (8.67) */
1498
0
        qnt_pwr = (int)floor(bit_per_dgt * (dgt_nbr - nsd)); /* DGG19 p. 4101 (7) */
1499
0
        prc_bnr_xpl_rqr = mnt_fabs == 0.0 ? 0 : abs((int)floor(xpn_bs2 - bit_per_dgt*mnt_log10_fabs) - qnt_pwr); /* Protect against mnt = -0.0 */
1500
0
        prc_bnr_xpl_rqr--; /* 20211003 Reduce formula result by 1 bit: Passes all tests, improves CR by ~10% */
1501
1502
0
        bit_xpl_nbr_zro = BIT_XPL_NBR_SGN_FLT - prc_bnr_xpl_rqr;
1503
0
        msk_f32_u32_zro = 0u; /* Zero all bits */
1504
0
        msk_f32_u32_zro = ~msk_f32_u32_zro; /* Turn all bits to ones */
1505
        /* Bit Shave mask for AND: Left shift zeros into bits to be rounded, leave ones in untouched bits */
1506
0
        msk_f32_u32_zro <<= bit_xpl_nbr_zro;
1507
        /* Bit Set   mask for OR:  Put ones into bits to be set, zeros in untouched bits */
1508
0
        msk_f32_u32_one = ~msk_f32_u32_zro;
1509
0
        msk_f32_u32_hshv = msk_f32_u32_one & (msk_f32_u32_zro >> 1); /* Set one bit: the MSB of LSBs */
1510
0
        u32_ptr[idx] += msk_f32_u32_hshv; /* Add 1 to the MSB of LSBs, carry 1 to mantissa or even exponent */
1511
0
        u32_ptr[idx] &= msk_f32_u32_zro; /* Shave it */
1512
1513
0
      } /* !mss_val_cmp_flt */
1514
1515
0
        } 
1516
0
        }
1517
0
        else
1518
0
        {
1519
            /* Granular BitRound */
1520
0
            op1.dp = (double *)dest;
1521
0
            u64_ptr = op1.ui64p;
1522
0
            for (idx = 0L; idx < len; idx++)
1523
0
        {
1524
1525
0
    if((val = op1.dp[idx]) != mss_val_cmp_dbl && u64_ptr[idx] != 0ULL)
1526
0
      {
1527
0
        mnt = frexp(val, &xpn_bs2); /* DGG19 p. 4102 (8) */
1528
0
        mnt_fabs = fabs(mnt);
1529
0
        mnt_log10_fabs = log10(mnt_fabs);
1530
        /* 20211003 Continuous determination of dgt_nbr improves CR by ~10% */
1531
0
        dgt_nbr = (int)floor(xpn_bs2 * dgt_per_bit + mnt_log10_fabs) + 1; /* DGG19 p. 4102 (8.67) */
1532
0
        qnt_pwr = (int)floor(bit_per_dgt * (dgt_nbr - nsd)); /* DGG19 p. 4101 (7) */
1533
0
        prc_bnr_xpl_rqr = mnt_fabs == 0.0 ? 0 : abs((int)floor(xpn_bs2 - bit_per_dgt*mnt_log10_fabs) - qnt_pwr); /* Protect against mnt = -0.0 */
1534
0
        prc_bnr_xpl_rqr--; /* 20211003 Reduce formula result by 1 bit: Passes all tests, improves CR by ~10% */
1535
1536
0
        bit_xpl_nbr_zro = BIT_XPL_NBR_SGN_DBL - prc_bnr_xpl_rqr;
1537
0
        msk_f64_u64_zro = 0ull; /* Zero all bits */
1538
0
        msk_f64_u64_zro = ~msk_f64_u64_zro; /* Turn all bits to ones */
1539
        /* Bit Shave mask for AND: Left shift zeros into bits to be rounded, leave ones in untouched bits */
1540
0
        msk_f64_u64_zro <<= bit_xpl_nbr_zro;
1541
        /* Bit Set   mask for OR:  Put ones into bits to be set, zeros in untouched bits */
1542
0
        msk_f64_u64_one = ~msk_f64_u64_zro;
1543
0
        msk_f64_u64_hshv = msk_f64_u64_one & (msk_f64_u64_zro >> 1); /* Set one bit: the MSB of LSBs */
1544
0
        u64_ptr[idx] += msk_f64_u64_hshv; /* Add 1 to the MSB of LSBs, carry 1 to mantissa or even exponent */
1545
0
        u64_ptr[idx] &= msk_f64_u64_zro; /* Shave it */
1546
1547
0
      } /* !mss_val_cmp_dbl */
1548
1549
0
        }
1550
0
        }
1551
0
    } /* endif GranularBR */
1552
1553
0
    return NC_NOERR;
1554
0
}
1555
1556
/**
1557
 * @internal What fill value should be used for a variable?
1558
 *
1559
 * @param h5 Pointer to HDF5 file info struct.
1560
 * @param var Pointer to variable info struct.
1561
 * @param fillp Pointer that gets pointer to fill value.
1562
 *
1563
 * @returns NC_NOERR No error.
1564
 * @returns NC_ENOMEM Out of memory.
1565
 * @author Ed Hartnett
1566
 */
1567
int
1568
nc4_get_fill_value(NC_FILE_INFO_T *h5, NC_VAR_INFO_T *var, void **fillp)
1569
0
{
1570
0
    size_t size;
1571
0
    int retval;
1572
1573
    /* Find out how much space we need for this type's fill value. */
1574
0
    if (var->type_info->nc_type_class == NC_VLEN)
1575
0
        size = sizeof(nc_vlen_t);
1576
0
    else if (var->type_info->nc_type_class == NC_STRING)
1577
0
        size = sizeof(char *);
1578
0
    else
1579
0
    {
1580
0
        if ((retval = nc4_get_typelen_mem(h5, var->type_info->hdr.id, &size)))
1581
0
            return retval;
1582
0
    }
1583
0
    assert(size);
1584
1585
    /* Allocate the space. */
1586
0
    if (!((*fillp) = calloc(1, size)))
1587
0
        return NC_ENOMEM;
1588
1589
    /* If the user has set a fill_value for this var, use, otherwise
1590
     * find the default fill value. */
1591
0
    if (var->fill_value)
1592
0
    {
1593
0
        LOG((4, "Found a fill value for var %s", var->hdr.name));
1594
0
        if (var->type_info->nc_type_class == NC_VLEN)
1595
0
        {
1596
0
            nc_vlen_t *in_vlen = (nc_vlen_t *)(var->fill_value), *fv_vlen = (nc_vlen_t *)(*fillp);
1597
0
            size_t basetypesize = 0;
1598
1599
0
            if((retval=nc4_get_typelen_mem(h5, var->type_info->u.v.base_nc_typeid, &basetypesize)))
1600
0
                return retval;
1601
1602
0
            fv_vlen->len = in_vlen->len;
1603
0
            if (!(fv_vlen->p = malloc(basetypesize * in_vlen->len)))
1604
0
            {
1605
0
                free(*fillp);
1606
0
                *fillp = NULL;
1607
0
                return NC_ENOMEM;
1608
0
            }
1609
0
            memcpy(fv_vlen->p, in_vlen->p, in_vlen->len * basetypesize);
1610
0
        }
1611
0
        else if (var->type_info->nc_type_class == NC_STRING)
1612
0
        {
1613
0
            if (*(char **)var->fill_value)
1614
0
                if (!(**(char ***)fillp = strdup(*(char **)var->fill_value)))
1615
0
                {
1616
0
                    free(*fillp);
1617
0
                    *fillp = NULL;
1618
0
                    return NC_ENOMEM;
1619
0
                }
1620
0
        }
1621
0
        else
1622
0
            memcpy((*fillp), var->fill_value, size);
1623
0
    }
1624
0
    else
1625
0
    {
1626
0
        if (nc4_get_default_fill_value(var->type_info, *fillp))
1627
0
        {
1628
            /* Note: release memory, but don't return error on failure */
1629
0
            free(*fillp);
1630
0
            *fillp = NULL;
1631
0
        }
1632
0
    }
1633
1634
0
    return NC_NOERR;
1635
0
}
1636
1637
/**
1638
 * @internal Get the length, in bytes, of one element of a type in
1639
 * memory.
1640
 *
1641
 * @param h5 Pointer to HDF5 file info struct.
1642
 * @param xtype NetCDF type ID.
1643
 * @param len Pointer that gets length in bytes.
1644
 *
1645
 * @returns NC_NOERR No error.
1646
 * @returns NC_EBADTYPE Type not found.
1647
 * @author Ed Hartnett
1648
 */
1649
int
1650
nc4_get_typelen_mem(NC_FILE_INFO_T *h5, nc_type xtype, size_t *len)
1651
0
{
1652
0
    NC_TYPE_INFO_T *type;
1653
0
    int retval;
1654
1655
0
    LOG((4, "%s xtype: %d", __func__, xtype));
1656
0
    assert(len);
1657
1658
    /* If this is an atomic type, the answer is easy. */
1659
0
    switch (xtype)
1660
0
    {
1661
0
    case NC_BYTE:
1662
0
    case NC_CHAR:
1663
0
    case NC_UBYTE:
1664
0
        *len = sizeof(char);
1665
0
        return NC_NOERR;
1666
0
    case NC_SHORT:
1667
0
    case NC_USHORT:
1668
0
        *len = sizeof(short);
1669
0
        return NC_NOERR;
1670
0
    case NC_INT:
1671
0
    case NC_UINT:
1672
0
        *len = sizeof(int);
1673
0
        return NC_NOERR;
1674
0
    case NC_FLOAT:
1675
0
        *len = sizeof(float);
1676
0
        return NC_NOERR;
1677
0
    case NC_DOUBLE:
1678
0
        *len = sizeof(double);
1679
0
        return NC_NOERR;
1680
0
    case NC_INT64:
1681
0
    case NC_UINT64:
1682
0
        *len = sizeof(long long);
1683
0
        return NC_NOERR;
1684
0
    case NC_STRING:
1685
0
        *len = sizeof(char *);
1686
0
        return NC_NOERR;
1687
0
    }
1688
1689
    /* See if var is compound type. */
1690
0
    if ((retval = nc4_find_type(h5, xtype, &type)))
1691
0
        return retval;
1692
1693
0
    if (!type)
1694
0
        return NC_EBADTYPE;
1695
1696
0
    *len = type->size;
1697
1698
0
    LOG((5, "type->size: %d", type->size));
1699
1700
0
    return NC_NOERR;
1701
0
}
1702
1703
1704
/**
1705
 * @internal Check a set of chunksizes to see if they specify a chunk
1706
 * that is too big.
1707
 *
1708
 * @param grp Pointer to the group info.
1709
 * @param var Pointer to the var info.
1710
 * @param chunksizes Array of chunksizes to check.
1711
 *
1712
 * @returns ::NC_NOERR No error.
1713
 * @returns ::NC_EBADID Bad ncid.
1714
 * @returns ::NC_ENOTVAR Invalid variable ID.
1715
 * @returns ::NC_EBADCHUNK Bad chunksize.
1716
 */
1717
int
1718
nc4_check_chunksizes(NC_GRP_INFO_T *grp, NC_VAR_INFO_T *var, const size_t *chunksizes)
1719
0
{
1720
0
    double dprod;
1721
0
    size_t type_len;
1722
0
    int d;
1723
0
    int retval;
1724
1725
0
    if ((retval = nc4_get_typelen_mem(grp->nc4_info, var->type_info->hdr.id, &type_len)))
1726
0
        return retval;
1727
0
    if (var->type_info->nc_type_class == NC_VLEN)
1728
0
        dprod = (double)sizeof(nc_vlen_t);
1729
0
    else
1730
0
        dprod = (double)type_len;
1731
0
    for (d = 0; d < var->ndims; d++)
1732
0
        dprod *= (double)chunksizes[d];
1733
1734
0
    if (dprod > (double) NC_MAX_UINT)
1735
0
        return NC_EBADCHUNK;
1736
1737
0
    return NC_NOERR;
1738
0
}
1739
1740
/**
1741
 * @internal Determine some default chunksizes for a variable.
1742
 *
1743
 * @param grp Pointer to the group info.
1744
 * @param var Pointer to the var info.
1745
 *
1746
 * @returns ::NC_NOERR for success
1747
 * @returns ::NC_EBADID Bad ncid.
1748
 * @returns ::NC_ENOTVAR Invalid variable ID.
1749
 * @author Ed Hartnett, Dennis Heimbigner
1750
 */
1751
int
1752
nc4_find_default_chunksizes2(NC_GRP_INFO_T *grp, NC_VAR_INFO_T *var)
1753
0
{
1754
0
    int d;
1755
0
    size_t type_size;
1756
0
    float num_values = 1, num_unlim = 0;
1757
0
    int retval;
1758
0
    size_t suggested_size;
1759
#ifdef LOGGING
1760
    double total_chunk_size;
1761
#endif
1762
1763
0
    if (var->type_info->nc_type_class == NC_STRING)
1764
0
        type_size = sizeof(char *);
1765
0
    else
1766
0
        type_size = var->type_info->size;
1767
1768
#ifdef LOGGING
1769
    /* Later this will become the total number of bytes in the default
1770
     * chunk. */
1771
    total_chunk_size = (double) type_size;
1772
#endif
1773
1774
0
    if(var->chunksizes == NULL) {
1775
0
        if((var->chunksizes = calloc(1,sizeof(size_t)*var->ndims)) == NULL)
1776
0
            return NC_ENOMEM;
1777
0
    }
1778
1779
    /* How many values in the variable (or one record, if there are
1780
     * unlimited dimensions). */
1781
0
    for (d = 0; d < var->ndims; d++)
1782
0
    {
1783
0
        assert(var->dim[d]);
1784
0
        if (! var->dim[d]->unlimited)
1785
0
            num_values *= (float)var->dim[d]->len;
1786
0
        else {
1787
0
            num_unlim++;
1788
0
            var->chunksizes[d] = 1; /* overwritten below, if all dims are unlimited */
1789
0
        }
1790
0
    }
1791
    /* Special case to avoid 1D vars with unlim dim taking huge amount
1792
       of space (DEFAULT_CHUNK_SIZE bytes). Instead we limit to about
1793
       4KB */
1794
0
    if (var->ndims == 1 && num_unlim == 1) {
1795
0
        if (DEFAULT_CHUNK_SIZE / type_size <= 0)
1796
0
            suggested_size = 1;
1797
0
        else if (DEFAULT_CHUNK_SIZE / type_size > DEFAULT_1D_UNLIM_SIZE)
1798
0
            suggested_size = DEFAULT_1D_UNLIM_SIZE;
1799
0
        else
1800
0
            suggested_size = DEFAULT_CHUNK_SIZE / type_size;
1801
0
        var->chunksizes[0] = suggested_size / type_size;
1802
0
        LOG((4, "%s: name %s dim %d DEFAULT_CHUNK_SIZE %d num_values %f type_size %d "
1803
0
             "chunksize %ld", __func__, var->hdr.name, d, DEFAULT_CHUNK_SIZE, num_values, type_size, var->chunksizes[0]));
1804
0
    }
1805
0
    if (var->ndims > 1 && var->ndims == num_unlim) { /* all dims unlimited */
1806
0
        suggested_size = pow((double)DEFAULT_CHUNK_SIZE/type_size, 1.0/(double)(var->ndims));
1807
0
        for (d = 0; d < var->ndims; d++)
1808
0
        {
1809
0
            var->chunksizes[d] = suggested_size ? suggested_size : 1;
1810
0
            LOG((4, "%s: name %s dim %d DEFAULT_CHUNK_SIZE %d num_values %f type_size %d "
1811
0
                 "chunksize %ld", __func__, var->hdr.name, d, DEFAULT_CHUNK_SIZE, num_values, type_size, var->chunksizes[d]));
1812
0
        }
1813
0
    }
1814
1815
    /* Pick a chunk length for each dimension, if one has not already
1816
     * been picked above. */
1817
0
    for (d = 0; d < var->ndims; d++)
1818
0
        if (!var->chunksizes[d])
1819
0
        {
1820
0
            suggested_size = (pow((double)DEFAULT_CHUNK_SIZE/(num_values * type_size),
1821
0
                                  1.0/(double)(var->ndims - num_unlim)) * var->dim[d]->len - .5);
1822
0
            if (suggested_size > var->dim[d]->len)
1823
0
                suggested_size = var->dim[d]->len;
1824
0
            var->chunksizes[d] = suggested_size ? suggested_size : 1;
1825
0
            LOG((4, "%s: name %s dim %d DEFAULT_CHUNK_SIZE %d num_values %f type_size %d "
1826
0
                 "chunksize %ld", __func__, var->hdr.name, d, DEFAULT_CHUNK_SIZE, num_values, type_size, var->chunksizes[d]));
1827
0
        }
1828
1829
#ifdef LOGGING
1830
    /* Find total chunk size. */
1831
    for (d = 0; d < var->ndims; d++)
1832
        total_chunk_size *= (double) var->chunksizes[d];
1833
    LOG((4, "total_chunk_size %f", total_chunk_size));
1834
#endif
1835
1836
    /* But did this result in a chunk that is too big? */
1837
0
    retval = nc4_check_chunksizes(grp, var, var->chunksizes);
1838
0
    if (retval)
1839
0
    {
1840
        /* Other error? */
1841
0
        if (retval != NC_EBADCHUNK)
1842
0
            return retval;
1843
1844
        /* Chunk is too big! Reduce each dimension by half and try again. */
1845
0
        for ( ; retval == NC_EBADCHUNK; retval = nc4_check_chunksizes(grp, var, var->chunksizes))
1846
0
            for (d = 0; d < var->ndims; d++)
1847
0
                var->chunksizes[d] = var->chunksizes[d]/2 ? var->chunksizes[d]/2 : 1;
1848
0
    }
1849
1850
    /* Do we have any big data overhangs? They can be dangerous to
1851
     * babies, the elderly, or confused campers who have had too much
1852
     * beer. */
1853
0
    for (d = 0; d < var->ndims; d++)
1854
0
    {
1855
0
        size_t num_chunks;
1856
0
        size_t overhang;
1857
0
        assert(var->chunksizes[d] > 0);
1858
0
        num_chunks = (var->dim[d]->len + var->chunksizes[d] - 1) / var->chunksizes[d];
1859
0
        if(num_chunks > 0) {
1860
0
            overhang = (num_chunks * var->chunksizes[d]) - var->dim[d]->len;
1861
0
            var->chunksizes[d] -= overhang / num_chunks;
1862
0
        }
1863
0
    }
1864
1865
1866
0
    return NC_NOERR;
1867
0
}
1868
1869
/**
1870
 * @internal Get the default fill value for an atomic type. Memory for
1871
 * fill_value must already be allocated, or you are DOOMED!
1872
 *
1873
 * @param xtype type id
1874
 * @param fill_value Pointer that gets the default fill value.
1875
 *
1876
 * @returns NC_NOERR No error.
1877
 * @returns NC_EINVAL Can't find atomic type.
1878
 * @author Ed Hartnett
1879
 */
1880
int
1881
nc4_get_default_fill_value(NC_TYPE_INFO_T* tinfo, void *fill_value)
1882
0
{
1883
0
    if(tinfo->hdr.id > NC_NAT && tinfo->hdr.id <= NC_MAX_ATOMIC_TYPE)
1884
0
        return nc4_get_default_atomic_fill_value(tinfo->hdr.id,fill_value);
1885
0
#ifdef USE_NETCDF4
1886
0
    switch(tinfo->nc_type_class) {
1887
0
    case NC_ENUM:
1888
0
  return nc4_get_default_atomic_fill_value(tinfo->u.e.base_nc_typeid,fill_value);
1889
0
    case NC_OPAQUE:
1890
0
    case NC_VLEN:
1891
0
    case NC_COMPOUND:
1892
0
  if(fill_value)
1893
0
      memset(fill_value,0,tinfo->size);
1894
0
  break;  
1895
0
    default: return NC_EBADTYPE;
1896
0
    }
1897
0
#endif
1898
0
    return NC_NOERR;
1899
0
}
1900
1901
/**
1902
 * @internal Get the default fill value for an atomic type. Memory for
1903
 * fill_value must already be allocated, or you are DOOMED!
1904
 *
1905
 * @param xtype type id
1906
 * @param fill_value Pointer that gets the default fill value.
1907
 *
1908
 * @returns NC_NOERR No error.
1909
 * @returns NC_EINVAL Can't find atomic type.
1910
 * @author Ed Hartnett
1911
 */
1912
int
1913
nc4_get_default_atomic_fill_value(nc_type xtype, void *fill_value)
1914
0
{
1915
0
    switch (xtype)
1916
0
    {
1917
0
    case NC_CHAR:
1918
0
        *(char *)fill_value = NC_FILL_CHAR;
1919
0
        break;
1920
1921
0
    case NC_STRING:
1922
0
        *(char **)fill_value = strdup(NC_FILL_STRING);
1923
0
        break;
1924
1925
0
    case NC_BYTE:
1926
0
        *(signed char *)fill_value = NC_FILL_BYTE;
1927
0
        break;
1928
1929
0
    case NC_SHORT:
1930
0
        *(short *)fill_value = NC_FILL_SHORT;
1931
0
        break;
1932
1933
0
    case NC_INT:
1934
0
        *(int *)fill_value = NC_FILL_INT;
1935
0
        break;
1936
1937
0
    case NC_UBYTE:
1938
0
        *(unsigned char *)fill_value = NC_FILL_UBYTE;
1939
0
        break;
1940
1941
0
    case NC_USHORT:
1942
0
        *(unsigned short *)fill_value = NC_FILL_USHORT;
1943
0
        break;
1944
1945
0
    case NC_UINT:
1946
0
        *(unsigned int *)fill_value = NC_FILL_UINT;
1947
0
        break;
1948
1949
0
    case NC_INT64:
1950
0
        *(long long *)fill_value = NC_FILL_INT64;
1951
0
        break;
1952
1953
0
    case NC_UINT64:
1954
0
        *(unsigned long long *)fill_value = NC_FILL_UINT64;
1955
0
        break;
1956
1957
0
    case NC_FLOAT:
1958
0
        *(float *)fill_value = NC_FILL_FLOAT;
1959
0
        break;
1960
1961
0
    case NC_DOUBLE:
1962
0
        *(double *)fill_value = NC_FILL_DOUBLE;
1963
0
        break;
1964
1965
0
    default:
1966
0
        return NC_EINVAL;
1967
0
    }
1968
0
    return NC_NOERR;
1969
0
}
1970