Skip to content

HDF error when unlimited dimension and a variable share the same name  #975

@oceandatalab

Description

@oceandatalab

It seems there is an issue when a variable shares the same name as an unlimited dimension

Here a a simple test script which creates:

  • an unlimited dimension called "time"
  • a fixed-size dimension called "time_subsample"
  • a variable whose name is passed to the script with the dimension "time_subsample"
  • a variable named "dummy" with the dimension "time"
# test_netcdf_bug.py
import netCDF4
import sys

time_var_name = sys.argv[1]

nc_file = netCDF4.Dataset('test.nc', mode='w', format='NETCDF4', clobber=True)
nc_file.createDimension('time', None)  # unlimited
nc_file.createDimension('time_subsample', 50)

time_var = nc_file.createVariable(time_var_name, 'f8', ('time_subsample',))
time_var[:] = [i for i in range(0, 50)]

dummy_var = nc_file.createVariable('dummy', 'int', ('time', ))
dummy_var[:] = [i for i in range(0, 500)]

nc_file.close()

Calling the script with any name other than "time" works fine:

python test_netcdf_bug.py anything
python test_netcdf_bug.py time_subsample
python test_netcdf_bug.py odl

But if you create a variable named "time", you get an HDF error:

python test_netcdf_bug.py time
Traceback (most recent call last):
  File "test_netcdf_bug.py", line 16, in <module>
    nc_file.close()
  File "netCDF4/_netCDF4.pyx", line 2485, in netCDF4._netCDF4.Dataset.close
  File "netCDF4/_netCDF4.pyx", line 2449, in netCDF4._netCDF4.Dataset._close
  File "netCDF4/_netCDF4.pyx", line 1887, in netCDF4._netCDF4._ensure_nc_success
RuntimeError: NetCDF: HDF error

Creating another unlimited dimension and assigning it to the time variable generates no error:

# test2_netcdf_bug.py
import netCDF4

nc_file = netCDF4.Dataset('test.nc', mode='w', format='NETCDF4', clobber=True)
nc_file.createDimension('time', None)  # unlimited
nc_file.createDimension('nolimit', None)  # unlimited
nc_file.createDimension('time_subsample', 50)

time_var = nc_file.createVariable('time', 'f8', ('nolimit',))
time_var[:] = [i for i in range(0, 50)]

dummy_var = nc_file.createVariable('dummy', 'int', ('time', ))
dummy_var[:] = [i for i in range(0, 500)]

nc_file.close()
python test2_netcdf_bug.py

So it seems impossible to have an unlimited dimension and a variable with the same name if the variable does not have an unlimited dimension too.

I also tried to replicate the issue with the C++ bindings but it managed to create the NetCDF file just fine.

Tested on Archlinux with the following package/library versions:

>>> import sys
>>> import netCDF4
>>> sys.version
'3.7.4 (default, Jul 16 2019, 07:12:58) \n[GCC 9.1.0]'
>>> netCDF4.__version__
'1.5.2'
>>> netCDF4.__hdf5libversion__
'1.10.2'
>>> netCDF4.__netcdf4libversion__
'4.6.3

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions