netcdf-c icon indicating copy to clipboard operation
netcdf-c copied to clipboard

error renaming a variable in a group

Open jswhit opened this issue 5 years ago • 6 comments

Reported in https://github.com/Unidata/netcdf4-python/issues/999. Here's a simple C program that triggers the HDF Error. The rename only fails if the file is closed and then re-opened.

#include <netcdf.h>
#include <stdio.h>
#include <stdlib.h>
#define FILE_NAME "rename_test.nc"
#define DIM1_NAME "lon"
#define GRP1_NAME "grp"
#define VAR1_NAME "lon"
#define VAR2_NAME "longitude"
#define ERR(e) {printf("Error: %s\n", nc_strerror(e)); exit(2);}
int
main()
{
    int ncid, dimid, varid, ierr, grpid;
    /* Create a file with a variable in a group. */
    if ((ierr=nc_create(FILE_NAME, NC_NETCDF4, &ncid))) ERR(ierr);
    if ((ierr=nc_def_dim(ncid, DIM1_NAME, 1, &dimid))) ERR(ierr);
    if ((ierr=nc_def_grp(ncid, GRP1_NAME, &grpid))) ERR(ierr);
    if ((ierr=nc_def_var(grpid, VAR1_NAME, NC_FLOAT, 1, &dimid, &varid))) ERR(ierr);
    if ((ierr=nc_close(ncid))) ERR(ierr);
    /* Open the file and rename the variable. */
    if ((ierr=nc_open(FILE_NAME, NC_WRITE, &ncid))) ERR(ierr);
    if ((ierr=nc_inq_grp_ncid(ncid, GRP1_NAME, &grpid))) ERR(ierr);
    if ((ierr=nc_inq_varid(grpid, VAR1_NAME, &varid))) ERR(ierr);
    if ((ierr=nc_rename_var(grpid, varid, VAR2_NAME))) ERR(ierr);
    if ((ierr=nc_close(ncid))) ERR(ierr);
}

jswhit avatar Mar 13 '20 01:03 jswhit

Well if you would just name the variable correctly in the first place, there would be no need for a rename! ;-)

However, I would have thought this simple example would have worked. Sigh.

It might be time for a total re-write of the renaming code, as outlined at the end of the long discussion on #597.

edwardhartnett avatar Mar 13 '20 12:03 edwardhartnett

We observed the same issue in Netcdf version 4.8.1. Is there a targeted release to fix this issue?

Thanks, Nalini

nvishnoiMW avatar Dec 13 '21 16:12 nvishnoiMW

I know it doesn't look like a particularly common issue... But would be nice to know if a fix is potentially planned for 4.8.2 or if it is on a back-burner for now.

krisfed avatar Feb 24 '22 21:02 krisfed

It is definitely back-burner, because this requires a lot of work to fix.

But if you try renaming in different orders, some will work, and some won't.

edwardhartnett avatar Feb 28 '22 13:02 edwardhartnett

Makes sense, thanks for the update!

krisfed avatar Feb 28 '22 14:02 krisfed

Hi, just wanted to check if there are any new updates, or if this is still on a back-burner for now.

krisfed avatar Aug 25 '22 11:08 krisfed