netcdf-c/nc_test4/tst_fillonly.c
Dennis Heimbigner 2f0a6d22e9 Fix error where not converting fill data
re: Github Issue https://github.com/Unidata/netcdf-c/issues/1826

It turns out that the common get code (NC4_get_vars) in libhdf5
(and libnczarr) has an optimization where it does not attempt to
read from the file if the file is all fill values. Rather it
just fills the output buffer with the fill value.  The problem
is that -- in that case -- it forgets that conversion might still be
needed.  So the conversion never occurs and the raw bits of
the fill data are stored directly into the memory space.

Solution: move some code around to properly do the
conversion no matter how the data was obtained.

Added a test cases nc_test4/test_fillonly.sh and
nczarr_test/test_fillonlyz.sh
2020-09-12 14:49:59 -06:00

75 lines
1.5 KiB
C
Executable File

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "netcdf.h"
#undef DEBUG
static void
nccheck(int ret)
{
if(ret == NC_NOERR) return;
fprintf(stderr,"err=%s\n",nc_strerror(ret));
exit(1);
}
#define NCCHECK(err) nccheck(err)
int
main(int argc, char *argv[] )
{
int err, ncid, varid, dimid[1];
size_t dimlen[1];
float *fdat;
int *idat;
const char* filename = "tmp_fillonly.nc";
const char* varname = "f";
const char* dimname = "x";
size_t i;
NCCHECK(err = nc_open(filename,NC_NETCDF4,&ncid));
NCCHECK(err = nc_inq_varid(ncid, varname, &varid));
NCCHECK(err = nc_inq_dimid(ncid, dimname, dimid));
NCCHECK(err = nc_inq_dim(ncid, dimid[0], NULL, dimlen));
/* Make room for both double and floating dat */
fdat = (float *)calloc(1,sizeof(float) * dimlen[0]);
idat = (int *)calloc(1,sizeof(int) * dimlen[0]);
NCCHECK(err = nc_get_var_int(ncid, varid, idat));
NCCHECK(err = nc_get_var_float(ncid, varid, fdat));
#ifdef DEBUG
printf("int[0..%d]:",(int)dimlen[0]);
for(i=0; i<dimlen[0]; i++ ) printf(" %i", idat[i]);
printf("\n");
printf("float[0..%d]:",(int)dimlen[0]);
for(i=0; i<dimlen[0]; i++ ) printf(" %f", fdat[i]);
printf("\n");
#endif
/* Do the comparisons */
for(i=0; i<dimlen[0]; i++ ) {
if(fdat[i] != (float)(idat[i])) {
fprintf(stderr,"data mismatch [%d]: float=%f (float)int=%f int=%i\n",(int)i,fdat[i],(float)idat[i],idat[i]);
return 1;
}
}
return 0;
}