From bb40936a7e1059f752a52677e80f8e84d9c7f2f1 Mon Sep 17 00:00:00 2001 From: Edward Hartnett Date: Tue, 31 Aug 2021 06:42:30 -0600 Subject: [PATCH] more testing with type conversion --- libsrc4/nc4var.c | 15 +++--- nc_test4/tst_quantize.c | 100 ++++++++++++++++++++++++++++++++++++---- 2 files changed, 96 insertions(+), 19 deletions(-) diff --git a/libsrc4/nc4var.c b/libsrc4/nc4var.c index 6f2143ceb..8caf02002 100644 --- a/libsrc4/nc4var.c +++ b/libsrc4/nc4var.c @@ -573,10 +573,10 @@ nc4_quantize_data(const void *src, void *dest, const nc_type src_type, /* Bit-Groom: alternately shave and set LSBs */ op1.fp = (float *)dest; u32_ptr = op1.ui32p; - for(idx = 0L; idx < len; idx += 2L) + for (idx = 0L; idx < len; idx += 2L) if (op1.fp[idx] != mss_val_cmp_flt) u32_ptr[idx] &= msk_f32_u32_zro; - for(idx = 1L; idx < len; idx += 2L) + for (idx = 1L; idx < len; idx += 2L) if (op1.fp[idx] != mss_val_cmp_flt && u32_ptr[idx] != 0U) /* Never quantize upwards floating point values of zero */ u32_ptr[idx] |= msk_f32_u32_one; } @@ -605,7 +605,7 @@ nc4_quantize_data(const void *src, void *dest, const nc_type src_type, /* Copy the data into our buffer. */ if (src_type == NC_FLOAT) for (fp = (float *)src, dp1 = dest; count < len; count++) - *dp1++ = *dp++; + *dp1++ = *fp++; else for (dp = (double *)src, dp1 = dest; count < len; count++) *dp1++ = *dp++; @@ -1432,12 +1432,9 @@ nc4_convert_type(const void *src, void *dest, const nc_type src_type, case NC_FLOAT: if (quantize_mode == NC_QUANTIZE_BITGROOM) { - for (dp = (double *)src, fp = dest; count < len; count++) - { - if (isgreater(*dp, X_FLOAT_MAX) || isless(*dp, X_FLOAT_MIN)) - (*range_error)++; - *fp++ = *dp++; - } + if ((ret = nc4_quantize_data(src, dest, src_type, dest_type, len, range_error, + fill_value, strict_nc3, quantize_mode, nsd))) + return ret; } else { diff --git a/nc_test4/tst_quantize.c b/nc_test4/tst_quantize.c index bc6ec983c..a4235fb14 100644 --- a/nc_test4/tst_quantize.c +++ b/nc_test4/tst_quantize.c @@ -432,9 +432,9 @@ main(int argc, char **argv) float float_in; double double_in; union FU fin; - union FU fout; + /* union FU fout; */ union DU dfin; - union DU dfout; + /* union DU dfout; */ int nsd_att_in; /* Open the file and check metadata. */ @@ -453,16 +453,96 @@ main(int argc, char **argv) /* Check the data. */ if (nc_get_var(ncid, varid1, &float_in)) ERR; if (nc_get_var(ncid, varid2, &double_in)) ERR; - fout.f = float_data[0]; + /* fout.f = (float)double_data[0]; */ fin.f = float_in; - dfout.d = double_data[0]; + /* dfout.d = float_data[0]; */ dfin.d = double_in; - printf ("\nfloat_data: %10f : 0x%x float_data_in: %10f : 0x%x\n", - float_data[0], fout.u, float_data[0], fin.u); - /* if (fin.u != 0x3f8e3000) ERR; */ - printf ("\ndouble_data: %15g : 0x%16lx double_data_in: %15g : 0x%lx\n", - double_data[0], dfout.u, double_data[0], dfin.u); - /* if (dfin.u != 0x3ff1c60000000000) ERR; */ + /* printf ("\ndouble_data: %15g : 0x%x float_data_in: %10f : 0x%x\n", */ + /* double_data[0], fout.u, float_in, fin.u); */ + if (fin.u != 0x3f8e3000) ERR; + /* printf ("\nfloat_data: %15g : 0x%16lx double_data_in: %15g : 0x%lx\n", */ + /* float_data[0], dfout.u, double_in, dfin.u); */ + if (dfin.u != 0x3ff1c60000000000) ERR; + + /* Close the file again. */ + if (nc_close(ncid)) ERR; + } + } + SUMMARIZE_ERR; + printf("**** testing more quantization values with type conversion..."); + { + int ncid, dimid, varid1, varid2; + int quantize_mode_in, nsd_in; + float float_data[DIM_LEN_5] = {1.11111111, 1.0, 9.99999999, 12345.67, .1234567}; + double double_data[DIM_LEN_5] = {1.1111111, 1.0, 9.999999999, 1234567890.12345, 123456789012345.0}; + int x; + + /* Create a netcdf-4 file with two vars. */ + if (nc_create(FILE_NAME, NC_NETCDF4|NC_CLOBBER, &ncid)) ERR; + if (nc_def_dim(ncid, DIM_NAME_1, DIM_LEN_5, &dimid)) ERR; + if (nc_def_var(ncid, VAR_NAME_1, NC_FLOAT, NDIMS1, &dimid, &varid1)) ERR; + if (nc_def_var(ncid, VAR_NAME_2, NC_DOUBLE, NDIMS1, &dimid, &varid2)) ERR; + + /* Turn on quantize for both vars. */ + if (nc_def_var_quantize(ncid, varid1, NC_QUANTIZE_BITGROOM, NSD_3)) ERR; + if (nc_def_var_quantize(ncid, varid2, NC_QUANTIZE_BITGROOM, NSD_3)) ERR; + + /* Write some data. */ + if (nc_put_var_double(ncid, varid1, double_data)) ERR; + if (nc_put_var_float(ncid, varid2, float_data)) ERR; + + /* Close the file. */ + if (nc_close(ncid)) ERR; + + { + float float_in[DIM_LEN_5]; + double double_in[DIM_LEN_5]; + union FU { + float f; + uint32_t u; + }; + + union FU fin; + /* union FU fout; */ + union FU xpect[DIM_LEN_5]; + union DU dfin; + /* union DU dfout; */ + union DU double_xpect[DIM_LEN_5]; + xpect[0].u = 0x3f8e3000; + xpect[1].u = 0x3f800fff; + xpect[2].u = 0x41200000; + xpect[3].u = 0x4e932fff; + xpect[4].u = 0x56e09000; + double_xpect[0].u = 0x3ff1c60000000000; + double_xpect[1].u = 0x3ff001ffffffffff; + double_xpect[2].u = 0x4024000000000000; + double_xpect[3].u = 0x40c81dffffffffff; + double_xpect[4].u = 0x3fbf9a0000000000; + + /* Open the file and check metadata. */ + if (nc_open(FILE_NAME, NC_WRITE, &ncid)) ERR; + if (nc_inq_var_quantize(ncid, 0, &quantize_mode_in, &nsd_in)) ERR; + if (quantize_mode_in != NC_QUANTIZE_BITGROOM || nsd_in != NSD_3) ERR; + if (nc_inq_var_quantize(ncid, 1, &quantize_mode_in, &nsd_in)) ERR; + if (quantize_mode_in != NC_QUANTIZE_BITGROOM || nsd_in != NSD_3) ERR; + + /* Check the data. */ + if (nc_get_var(ncid, varid1, float_in)) ERR; + if (nc_get_var(ncid, varid2, double_in)) ERR; + /* printf("\n"); */ + for (x = 0; x < DIM_LEN_5; x++) + { + /* fout.f = float_data[x]; */ + fin.f = float_in[x]; + /* printf ("float_data: %10f : 0x%x float_data_in: %10f : 0x%x\n", */ + /* float_data[x], fout.u, float_data[x], fin.u); */ + if (fin.u != xpect[x].u) ERR; + /* dfout.d = double_data[x]; */ + dfin.d = double_in[x]; + /* printf("double_data: %15g : 0x%16lx double_data_in: %15g : 0x%16lx\n", */ + /* double_data[x], dfout.u, double_data[x], dfin.u); */ + if (dfin.u != double_xpect[x].u) ERR; + } /* Close the file again. */ if (nc_close(ncid)) ERR;