Skip to content

Commit 18b0f3c

Browse files
authored
Merge pull request #1528 from NetCDF-World-Domination-Council/ejh_unlim_dim_var_same_name
Fix for subtle error involving var and unlimited dim of the same name, but unrelated, in netCDF-4
2 parents 1a6351d + b844f8f commit 18b0f3c

3 files changed

Lines changed: 68 additions & 22 deletions

File tree

libhdf5/hdf5var.c

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,24 @@
1515
#endif
1616
#include <math.h> /* For pow() used below. */
1717

18+
/** @internal Default size for unlimited dim chunksize. */
19+
#define DEFAULT_1D_UNLIM_SIZE (4096)
20+
21+
/** @internal Temp name used when renaming vars to preserve varid
22+
* order. */
23+
#define NC_TEMP_NAME "_netcdf4_temporary_variable_name_for_rename"
24+
1825
#ifdef LOGGING
26+
/**
27+
* Report the chunksizes selected for a variable.
28+
*
29+
* @param title A text title for the report.
30+
* @param var Pointer to the var of interest.
31+
*
32+
* @author Dennis Heimbigner
33+
*/
1934
static void
20-
reportchunking(const char* title, NC_VAR_INFO_T* var)
35+
reportchunking(const char *title, NC_VAR_INFO_T *var)
2136
{
2237
int i;
2338
char buf[8192];
@@ -33,17 +48,10 @@ reportchunking(const char* title, NC_VAR_INFO_T* var)
3348
snprintf(digits,sizeof(digits),"%ld",(unsigned long)var->chunksizes[i]);
3449
strlcat(buf,digits,sizeof(buf));
3550
}
36-
LOG((1,"%s",buf));
37-
}
51+
LOG((3,"%s",buf));
52+
}
3853
#endif
3954

40-
/** @internal Default size for unlimited dim chunksize. */
41-
#define DEFAULT_1D_UNLIM_SIZE (4096)
42-
43-
/** @internal Temp name used when renaming vars to preserve varid
44-
* order. */
45-
#define NC_TEMP_NAME "_netcdf4_temporary_variable_name_for_rename"
46-
4755
/**
4856
* @internal If the HDF5 dataset for this variable is open, then close
4957
* it and reopen it, with the perhaps new settings for chunk caching.
@@ -247,7 +255,7 @@ nc4_find_default_chunksizes2(NC_GRP_INFO_T *grp, NC_VAR_INFO_T *var)
247255
}
248256

249257
#ifdef LOGGING
250-
reportchunking("find_default: ",var);
258+
reportchunking("find_default: ",var);
251259
#endif
252260
return NC_NOERR;
253261
}
@@ -753,9 +761,10 @@ nc_def_var_extra(int ncid, int varid, int *shuffle, int *deflate,
753761
}
754762

755763
#ifdef LOGGING
756-
{int dfalt=(chunksizes == NULL);
757-
reportchunking(dfalt?"extra: default: ":"extra: user: ",var);
758-
}
764+
{
765+
int dfalt = (chunksizes == NULL);
766+
reportchunking(dfalt ? "extra: default: " : "extra: user: ", var);
767+
}
759768
#endif
760769

761770
/* Are we setting a fill modes? */
@@ -1653,15 +1662,15 @@ NC4_put_vars(int ncid, int varid, const size_t *startp, const size_t *countp,
16531662
#endif
16541663
if (!zero_count && endindex >= fdims[d2])
16551664
{
1656-
xtend_size[d2] = (long long unsigned)(endindex+1);
1665+
xtend_size[d2] = (long long unsigned)(endindex + 1);
16571666
need_to_extend++;
16581667
}
16591668
else
16601669
xtend_size[d2] = (long long unsigned)fdims[d2];
16611670

16621671
if (!zero_count && endindex >= dim->len)
16631672
{
1664-
dim->len = endindex+1;
1673+
dim->len = endindex + 1;
16651674
dim->extended = NC_TRUE;
16661675
}
16671676
}

libhdf5/nc4hdf.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1863,10 +1863,10 @@ write_dim(NC_DIM_INFO_T *dim, NC_GRP_INFO_T *grp, nc_bool_t write_dimid)
18631863
NC_VAR_INFO_T *v1 = NULL;
18641864

18651865
assert(dim->unlimited);
1866-
/* If this is a dimension without a variable, then update
1867-
* the secret length information at the end of the NAME
1868-
* attribute. */
1869-
v1 = (NC_VAR_INFO_T *)ncindexlookup(grp->vars, dim->hdr.name);
1866+
1867+
/* If this is a dimension with an associated coordinate var,
1868+
* then update the length of that coord var. */
1869+
v1 = dim->coord_var;
18701870
if (v1)
18711871
{
18721872
NC_HDF5_VAR_INFO_T *hdf5_v1;

nc_test4/tst_dims3.c

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22
Corporation for Atmospheric Research/Unidata See COPYRIGHT file for
33
conditions of use. See www.unidata.ucar.edu for more info.
44
5-
Test netcdf-4 dimensions inheritance.
5+
Test netcdf-4 dimensions inheritance, and dims with and without
6+
coordinate variables.
67
7-
$Id: tst_dims3.c,v 1.7 2010/05/25 13:53:04 ed Exp $
8+
Ed Hartnett
89
*/
910

1011
#include <config.h>
@@ -198,5 +199,41 @@ main(int argc, char **argv)
198199
ERR_RET;
199200
}
200201
SUMMARIZE_ERR;
202+
printf("*** testing var and unlim dim with same name, but not related...");
203+
{
204+
/* This test code based on test code from Jeff Whitaker. See
205+
* https://github.com/Unidata/netcdf4-python/issues/975 and
206+
* https://github.com/Unidata/netcdf-c/issues/1496. */
207+
int ncid, timesubset_id, time_id, timevar_id, dummyvar_id;
208+
size_t start[1] = {0};
209+
size_t count[1] = {1};
210+
double data[1] = {TEST_VAL_42};
211+
size_t len;
212+
double data_in;
213+
214+
if (nc_create(FILE_NAME, NC_CLOBBER | NC_NETCDF4, &ncid)) ERR;
215+
if (nc_def_dim(ncid, "time", NC_UNLIMITED, &time_id)) ERR;
216+
if (nc_def_dim(ncid, "time_subset", 50, &timesubset_id)) ERR;
217+
218+
/* Define vars. */
219+
if (nc_def_var(ncid, "time", NC_DOUBLE, 1, &timesubset_id, &timevar_id)) ERR;
220+
if (nc_def_var(ncid, "dummy", NC_DOUBLE, 1, &time_id, &dummyvar_id)) ERR;
221+
if (nc_enddef(ncid)) ERR;
222+
223+
/* Write some data. */
224+
if (nc_put_vara(ncid, dummyvar_id, start, count, data)) ERR;
225+
226+
/* Close the file. */
227+
if (nc_close(ncid)) ERR;
228+
229+
/* Reopen file and check. */
230+
if (nc_open(FILE_NAME, NC_WRITE, &ncid)) ERR;
231+
if (nc_inq_dim(ncid, 0, NULL, &len)) ERR;
232+
if (len != 1) ERR;
233+
if (nc_get_vara_double(ncid, 1, start, count, &data_in)) ERR;
234+
if (data_in != TEST_VAL_42) ERR;
235+
if (nc_close(ncid)) ERR;
236+
}
237+
SUMMARIZE_ERR;
201238
FINAL_RESULTS;
202239
}

0 commit comments

Comments
 (0)