Skip to content

Error in renameDimension #1357

Open
Open
@veenstrajelmer

Description

@veenstrajelmer
  • netcdf version 1.6.5
  • Windows 10

Based on #817, but with the rename ordering switched just to be safe. There seems to be a bug in renameDimension:

import netCDF4 as nc

with nc.Dataset('test.nc', 'w') as fp:
    fp.createDimension('x', 3)
    ncvar = fp.createVariable('x', float, ('x',))
    ncvar[:] = [1.1, 2.2, 3.3]

with nc.Dataset('test.nc', 'r+') as fp:
    print(fp.variables['x'][:])
    fp.renameVariable('x', 'lon')
    fp.renameDimension('x', 'lon')
    print(fp.variables['lon'][:])

with nc.Dataset('test.nc', 'r') as nc:
    print(nc.variables['lon'][:])

Prints:

[1.1 2.2 3.3]
[1.1 2.2 3.3]
[-- -- --]

It seems that the data is corrupted upon saving the file. I would expect it would be just possible to rename a dimension without losing the data. My usecase can be found here: https://forum.ecmwf.int/t/new-time-format-in-era5-netcdf-files/3796/5?u=jelmer_veenstra

This only happens when the variable name is equal to the dimension name, and both have to be renamed. If we comment one of the rename actions, the data is preserved.

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