Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix rename bug name dim same as existing var #1294

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions include/nc4internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ typedef struct NC_VAR_INFO
nc_bool_t created; /* Variable has already been created (_not_ that it was just created) */
nc_bool_t written_to; /* True if variable has data written to it */
struct NC_TYPE_INFO *type_info;
int atts_not_read; /* If true, the atts have not yet been read. */
int atts_read; /* If true, the atts have been read. */
nc_bool_t meta_read; /* True if this vars metadata has been completely read. */
nc_bool_t coords_read; /* True if this var has hidden coordinates att, and it has been read. */
NCindex *att; /* NCindex<NC_ATT_INFO_T*> */
Expand Down Expand Up @@ -229,7 +229,7 @@ typedef struct NC_GRP_INFO
void *format_grp_info;
struct NC_FILE_INFO *nc4_info;
struct NC_GRP_INFO *parent;
int atts_not_read;
int atts_read; /* True if atts have been read for this group. */
NCindex* children; /* NCindex<struct NC_GRP_INFO*> */
NCindex* dim; /* NCindex<NC_DIM_INFO_T> * */
NCindex* att; /* NCindex<NC_ATT_INFO_T> * */
Expand Down
2 changes: 2 additions & 0 deletions libhdf4/hdf4file.c
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,7 @@ nc4_var_list_add_full(NC_GRP_INFO_T* grp, const char* name, int ndims, nc_type x
(*var)->created = NC_TRUE;
(*var)->written_to = NC_TRUE;
(*var)->format_var_info = format_var_info;
(*var)->atts_read = 1;

/* Fill special type_info struct for variable type information. */
if ((retval = nc4_set_var_type(xtype, endianness, type_size, type_name,
Expand Down Expand Up @@ -630,6 +631,7 @@ NC_HDF4_open(const char *path, int mode, int basepe, size_t *chunksizehintp,
assert(nc4_info && nc4_info->root_grp);
h5 = nc4_info;
h5->no_write = NC_TRUE;
h5->root_grp->atts_read = 1;

/* Allocate data to hold HDF4 specific file data. */
if (!(hdf4_file = malloc(sizeof(NC_HDF4_FILE_INFO_T))))
Expand Down
9 changes: 6 additions & 3 deletions libhdf5/hdf5attr.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,14 @@ static int
getattlist(NC_GRP_INFO_T *grp, int varid, NC_VAR_INFO_T **varp,
NCindex **attlist)
{
NC_VAR_INFO_T* var;
int retval;

assert(grp && attlist);

if (varid == NC_GLOBAL)
{
/* Do we need to read the atts? */
if (grp->atts_not_read)
if (!grp->atts_read)
if ((retval = nc4_read_atts(grp, NULL)))
return retval;

Expand All @@ -43,12 +44,14 @@ getattlist(NC_GRP_INFO_T *grp, int varid, NC_VAR_INFO_T **varp,
}
else
{
NC_VAR_INFO_T *var;

if (!(var = (NC_VAR_INFO_T *)ncindexith(grp->vars, varid)))
return NC_ENOTVAR;
assert(var->hdr.id == varid);

/* Do we need to read the atts? */
if (var->atts_not_read)
if (!var->atts_read)
if ((retval = nc4_read_atts(grp, var)))
return retval;

Expand Down
1 change: 1 addition & 0 deletions libhdf5/hdf5create.c
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ nc4_create_file(const char *path, int cmode, size_t initialsz,
BAIL(retval);
nc4_info = NC4_DATA(nc);
assert(nc4_info && nc4_info->root_grp);
nc4_info->root_grp->atts_read = 1;

/* Add struct to hold HDF5-specific file metadata. */
if (!(nc4_info->format_file_info = calloc(1, sizeof(NC_HDF5_FILE_INFO_T))))
Expand Down
2 changes: 1 addition & 1 deletion libhdf5/hdf5file.c
Original file line number Diff line number Diff line change
Expand Up @@ -727,7 +727,7 @@ NC4_inq(int ncid, int *ndimsp, int *nvarsp, int *nattsp, int *unlimdimidp)
if (nattsp)
{
/* Do we need to read the atts? */
if (grp->atts_not_read)
if (!grp->atts_read)
if ((retval = nc4_read_atts(grp, NULL)))
return retval;

Expand Down
5 changes: 5 additions & 0 deletions libhdf5/hdf5grp.c
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,11 @@ NC4_def_grp(int parent_ncid, const char *name, int *new_ncid)
return retval;
if (!(g->format_grp_info = calloc(1, sizeof(NC_HDF5_GRP_INFO_T))))
return NC_ENOMEM;

/* For new groups, there are no atts to read from file. */
g->atts_read = 1;

/* Return the ncid to the user. */
if (new_ncid)
*new_ncid = grp->nc4_info->controller->ext_ncid | g->hdr.id;

Expand Down
37 changes: 20 additions & 17 deletions libhdf5/hdf5internal.c
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,7 @@ delete_dimscale_dataset(NC_GRP_INFO_T *grp, int dimid, NC_DIM_INFO_T *dim)
* @param dim Pointer to dimension info struct.
*
* @return ::NC_NOERR No error.
* @author Quincey Koziol
* @author Quincey Koziol, Ed Hartnett
*/
int
nc4_reform_coord_var(NC_GRP_INFO_T *grp, NC_VAR_INFO_T *var, NC_DIM_INFO_T *dim)
Expand All @@ -406,14 +406,14 @@ nc4_reform_coord_var(NC_GRP_INFO_T *grp, NC_VAR_INFO_T *var, NC_DIM_INFO_T *dim)
hdf5_grp = (NC_HDF5_GRP_INFO_T *)grp->format_grp_info;
hdf5_var = (NC_HDF5_VAR_INFO_T *)var->format_var_info;

/* Detach dimscales from the [new] coordinate variable */
/* Detach dimscales from the [new] coordinate variable. */
if (var->dimscale_attached)
{
int dims_detached = 0;
int finished = 0;
int d;

/* Loop over all dimensions for variable */
/* Loop over all dimensions for variable. */
for (d = 0; d < var->ndims && !finished; d++)
{
/* Is there a dimscale attached to this axis? */
Expand All @@ -439,7 +439,8 @@ nc4_reform_coord_var(NC_GRP_INFO_T *grp, NC_VAR_INFO_T *var, NC_DIM_INFO_T *dim)

/* Find dataset ID for dimension */
if (dim1->coord_var)
dim_datasetid = ((NC_HDF5_VAR_INFO_T *)(dim1->coord_var->format_var_info))->hdf_datasetid;
dim_datasetid = ((NC_HDF5_VAR_INFO_T *)
(dim1->coord_var->format_var_info))->hdf_datasetid;
else
dim_datasetid = hdf5_dim1->hdf_dimscaleid;

Expand All @@ -450,7 +451,8 @@ nc4_reform_coord_var(NC_GRP_INFO_T *grp, NC_VAR_INFO_T *var, NC_DIM_INFO_T *dim)
if (dim_datasetid > 0)
{
LOG((3, "detaching scale from %s", var->hdr.name));
if (H5DSdetach_scale(hdf5_var->hdf_datasetid, dim_datasetid, d) < 0)
if (H5DSdetach_scale(hdf5_var->hdf_datasetid,
dim_datasetid, d) < 0)
BAIL(NC_EHDFERR);
}
var->dimscale_attached[d] = NC_FALSE;
Expand All @@ -462,7 +464,7 @@ nc4_reform_coord_var(NC_GRP_INFO_T *grp, NC_VAR_INFO_T *var, NC_DIM_INFO_T *dim)
}
} /* next variable dimension */

/* Release & reset the array tracking attached dimscales */
/* Release & reset the array tracking attached dimscales. */
free(var->dimscale_attached);
var->dimscale_attached = NULL;
need_to_reattach_scales++;
Expand All @@ -476,31 +478,32 @@ nc4_reform_coord_var(NC_GRP_INFO_T *grp, NC_VAR_INFO_T *var, NC_DIM_INFO_T *dim)
BAIL(NC_EHDFERR);
hdf5_dim->hdf_dimscaleid = 0;

/* Now delete the dimscale's dataset
(it will be recreated later, if necessary) */
/* Now delete the dimscale's dataset (it will be recreated
later, if necessary). */
if (H5Gunlink(hdf5_grp->hdf_grpid, dim->hdr.name) < 0)
return NC_EDIMMETA;
}

/* Attach variable to dimension */
/* Attach variable to dimension. */
var->dimscale = NC_TRUE;
dim->coord_var = var;

/* Check if this variable used to be a coord. var */
if (need_to_reattach_scales || (var->was_coord_var && grp != NULL))
{
/* Reattach the scale everywhere it is used. */
/* (Recall that netCDF dimscales are always 1-D) */
/* Reattach the scale everywhere it is used. (Recall that netCDF
* dimscales are always 1-D) */
if ((retval = rec_reattach_scales(grp->nc4_info->root_grp,
var->dimids[0], hdf5_var->hdf_datasetid)))
return retval;

/* Set state transition indicator (cancels earlier transition) */
/* Set state transition indicator (cancels earlier
* transition). */
var->was_coord_var = NC_FALSE;
}
else
/* Set state transition indicator */
var->became_coord_var = NC_TRUE;

/* Set state transition indicator */
var->became_coord_var = NC_TRUE;

exit:
return retval;
Expand Down Expand Up @@ -837,7 +840,7 @@ nc4_hdf5_find_grp_var_att(int ncid, int varid, const char *name, int attnum,
if (varid == NC_GLOBAL)
{
/* Do we need to read the atts? */
if (my_grp->atts_not_read)
if (!my_grp->atts_read)
if ((retval = nc4_read_atts(my_grp, NULL)))
return retval;

Expand All @@ -849,7 +852,7 @@ nc4_hdf5_find_grp_var_att(int ncid, int varid, const char *name, int attnum,
return NC_ENOTVAR;

/* Do we need to read the var attributes? */
if (my_var->atts_not_read)
if (!my_var->atts_read)
if ((retval = nc4_read_atts(my_grp, my_var)))
return retval;

Expand Down
9 changes: 3 additions & 6 deletions libhdf5/hdf5open.c
Original file line number Diff line number Diff line change
Expand Up @@ -1308,7 +1308,7 @@ read_var(NC_GRP_INFO_T *grp, hid_t datasetid, const char *obj_name,
H5Iinc_ref(hdf5_var->hdf_datasetid); /* Increment number of objects using ID */
incr_id_rc++; /* Indicate that we've incremented the ref. count (for errors) */
var->created = NC_TRUE;
var->atts_not_read = 1; /* Don't read var atts until user asks for one. */
var->atts_read = 0;

/* Try and read the dimids from the COORDINATES attribute. If it's
* not present, we will have to do dimsscale matching to locate the
Expand Down Expand Up @@ -2112,9 +2112,9 @@ nc4_read_atts(NC_GRP_INFO_T *grp, NC_VAR_INFO_T *var)

/* Remember that we have read the atts for this var or group. */
if (var)
var->atts_not_read = 0;
var->atts_read = 1;
else
grp->atts_not_read = 0;
grp->atts_read = 1;

return NC_NOERR;
}
Expand Down Expand Up @@ -2569,9 +2569,6 @@ rec_read_metadata(NC_GRP_INFO_T *grp)
BAIL(retval);
}

/* Defer the reading of global atts until someone asks for one. */
grp->atts_not_read = 1;

/* When reading existing file, mark all variables as written. */
for (i = 0; i < ncindexsize(grp->vars); i++)
((NC_VAR_INFO_T *)ncindexith(grp->vars, i))->written_to = NC_TRUE;
Expand Down
1 change: 1 addition & 0 deletions libhdf5/hdf5var.c
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,7 @@ NC4_def_var(int ncid, const char *name, nc_type xtype,

var->is_new_var = NC_TRUE;
var->meta_read = NC_TRUE;
var->atts_read = NC_TRUE;

/* Point to the type, and increment its ref. count */
var->type_info = type;
Expand Down
88 changes: 88 additions & 0 deletions nc_test4/tst_rename2.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ See \ref copyright file for more info.
#define DIM_X "x"
#define DIM_Y "y"
#define DIM_Z "z"
#define VAR_NAME_START "lon"
#define VAR_NAME_END "lo"
#define DIM_NAME_START "lon"
#define DIM_NAME_END "lo"

#define DIM1_LEN 4
#define NDIM1 1
Expand Down Expand Up @@ -147,5 +151,89 @@ main(int argc, char **argv)
SUMMARIZE_ERR;

} /* next format */

#define FILE_NAME1 "tst_dims_foo1.nc"
#define DIM_NAME "lat_T42"
#define VAR_NAME DIM_NAME
#define DIM_NAME2 "lat"
#define VAR_NAME2 DIM_NAME2
#define RANK_lat_T42 1
fprintf(stderr,"*** test renaming with sync...");
{
int ncid, dimid, varid;
char file_name[NC_MAX_NAME + 1];
char name[NC_MAX_NAME + 1];

/* Create file with dim and associated coordinate var. */
sprintf(file_name, "%s_sync.nc", TEST_NAME);
nc_set_log_level(4);
if (nc_create(file_name, NC_CLOBBER|NC_NETCDF4|NC_CLASSIC_MODEL, &ncid)) ERR;
if (nc_def_dim(ncid, DIM_NAME_END, DIM1_LEN, &dimid)) ERR;
if (nc_def_var(ncid, DIM_NAME_END, NC_INT, NDIM1, &dimid, &varid)) ERR;
if (nc_close(ncid)) ERR;

if (nc_create(file_name, NC_CLOBBER|NC_NETCDF4|NC_CLASSIC_MODEL, &ncid)) ERR;
if (nc_def_dim(ncid, DIM_NAME_START, DIM1_LEN, &dimid)) ERR;
if (nc_def_var(ncid, DIM_NAME_END, NC_INT, NDIM1, &dimid, &varid)) ERR;
if (nc_close(ncid)) ERR;

if (nc_open(file_name, NC_WRITE, &ncid)) ERR;
if (nc_rename_dim(ncid, dimid, DIM_NAME_END)) ERR;
if (nc_close(ncid)) ERR;

/* Reopen file and check, */
if (nc_open(file_name, NC_WRITE, &ncid)) ERR;
if (nc_inq_dimid(ncid, DIM_NAME_END, &dimid)) ERR;
if (nc_inq_varid(ncid, DIM_NAME_END, &varid)) ERR;
if (nc_inq_dimname(ncid, dimid, name)) ERR;
if (strcmp(name, DIM_NAME_END)) ERR;
if (nc_inq_varname(ncid, varid, name)) ERR;
if (strcmp(name, DIM_NAME_END)) ERR;
if (nc_close(ncid)) ERR;
}
SUMMARIZE_ERR;
fprintf(stderr,"*** test renaming with sync...");
{
int ncid, dimid, varid;
char file_name[NC_MAX_NAME + 1];
char name[NC_MAX_NAME + 1];

/* Create file with dim and associated coordinate var. */
sprintf(file_name, "%s_sync.nc", TEST_NAME);
if (nc_create(file_name, NC_CLOBBER|NC_NETCDF4|NC_CLASSIC_MODEL, &ncid)) ERR;
if (nc_def_dim(ncid, DIM_NAME_START, DIM1_LEN, &dimid)) ERR;
if (nc_def_var(ncid, VAR_NAME_START, NC_INT, NDIM1, &dimid, &varid)) ERR;
if (nc_close(ncid)) ERR;

nc_set_log_level(4);
/* Open the file and rename the var. */
if (nc_open(file_name, NC_WRITE, &ncid)) ERR;
if (nc_inq_dimid(ncid, DIM_NAME_START, &dimid)) ERR;
if (nc_inq_varid(ncid, VAR_NAME_START, &varid)) ERR;
if (nc_rename_var(ncid, varid, VAR_NAME_END)) ERR;

/* Sync to disk. Now the file has one dim and one var. The dim
* is a dimscale only dataset, and the var is a dataset with a
* dimscale attached pointing to the dim. */
/* if (nc_sync(ncid)) ERR; */
if (nc_close(ncid)) ERR;
if (nc_open(file_name, NC_WRITE, &ncid)) ERR;
/* Now rename the dim to the same name as the var. After this
* there will be one dataset, called DIM_NAME_END, which will be
* a dimscale. */
if (nc_rename_dim(ncid, dimid, DIM_NAME_END)) ERR;
if (nc_close(ncid)) ERR;

/* Reopen file and check, */
if (nc_open(file_name, NC_WRITE, &ncid)) ERR;
if (nc_inq_dimid(ncid, DIM_NAME_END, &dimid)) ERR;
if (nc_inq_varid(ncid, VAR_NAME_END, &varid)) ERR;
if (nc_inq_dimname(ncid, dimid, name)) ERR;
if (strcmp(name, DIM_NAME_END)) ERR;
if (nc_inq_varname(ncid, varid, name)) ERR;
if (strcmp(name, VAR_NAME_END)) ERR;
if (nc_close(ncid)) ERR;
}
SUMMARIZE_ERR;
FINAL_RESULTS;
}