Coverage Report

Created: 2025-10-28 07:06

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