netcdf4-python
netcdf4-python copied to clipboard
cannot rename variable in a group
netcdf4 version: 1.5.1.2 python versions tested: 3.5.6, 3.7.3 OS: Ubuntu18.04
I am able to rename a variable as long as it is in the "root" group. If the variable is in a sub-group, the variable rename fails. I can rename attributes in sub-groups, though.
Attached is a simple program that creates a netcdf file with two variables, one in the root group and one in a sub-group. It then reopens the file and attempts to rename the attributes and variables.
Confirmed. Looks like the error occurs only if close and re-open the file before the rename. Will need to try to reproduce this with a simple C program to see if this is an issue in the C lib or the python interface.
Minimal python example
from netCDF4 import Dataset
filename = 'test_rename_variable.nc'
nc_file = Dataset(filename, 'w')
nc_file.createDimension('bands',60)
bandgrp=nc_file.createGroup('sensor_band_parameters')
wave=bandgrp.createVariable ('waves','f4',('bands'))
nc_file.close()
nc_file = Dataset(filename, 'a')
ngid = nc_file.groups['sensor_band_parameters']
ngid.renameVariable('waves','wavelength')
nc_file.close()
Here's a minimal C example:
#include <netcdf.h>
#include <stdio.h>
#include <stdlib.h>
#define DIM1_NAME "lon"
#define GRP1_NAME "grp"
#define VAR1_NAME "lon"
#define VAR2_NAME "longitude"
#define DIM1_LEN 1
#define NDIM1 1
#define ERR(e) {printf("Error: %s\n", nc_strerror(e)); exit(2);}
int
main()
{
int ncid, dimid, varid, ierr, grpid;
if ((ierr=nc_create("rename_test.nc", NC_NETCDF4, &ncid))) ERR(ierr);
if ((ierr=nc_def_dim(ncid, DIM1_NAME, DIM1_LEN, &dimid))) ERR(ierr);
if ((ierr=nc_def_grp(ncid, GRP1_NAME, &grpid))) ERR(ierr);
if ((ierr=nc_def_var(grpid, VAR1_NAME, NC_FLOAT, NDIM1, &dimid, &varid))) ERR(ierr);
if ((ierr=nc_close(ncid))) ERR(ierr);
/* Open the file and rename the variable. */
if ((ierr=nc_open("rename_test.nc", 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);
}
I will open a netcdf-c issue.
Thanks! The work-around of creating an entirely new file and copying everything over is, well, a bit inefficient :)
Not suggesting that as a workaround. Hopefully the netcdf-c dev team will come up with a fix soon.
Oh, no, I wouldn't recommend it either, but it is what we had to do...so, yes, hopefully they will come up with a fix soon!