Unidata/netcdf4-python

Assignment 2d numpy array to 3d variable with first dimension of length 1 has changed behavior

cpaulik opened this issue · 5 comments

In version 1.5.1 the following code errors:

We are assigning a shape (100, 100) array to a (1, 100, 100) variable.
Not sure if this is supposed to work but it did in the past.

import netCDF4
import numpy as np

with netCDF4.Dataset('test.nc','w') as f:
    f.createDimension('time',1)
    f.createDimension('lat',100)
    f.createDimension('lon',100)

    f.createVariable('v1',np.float,('time', 'lon','lat',))
    arr = np.arange(100*100).reshape((100, 100))
    f['v1'][:] = arr
  File "01-160948.py", line 11, in <module>
    f['v1'][:] = arr
  File "netCDF4/_netCDF4.pyx", line 4796, in netCDF4._netCDF4.Variable.__setitem__
  File "/home/cpa/.pyenv/versions/miniconda2-4.3.30/envs/libsat/lib/python3.6/site-packages/netCDF4/utils.py", line 352, in _StartCountStride
    datashapenew = datashapenew + (datashape[i],)
IndexError: tuple index out of range

It works up to version 1.5.0.1

same here, example works on 1.5.0.1, fails on 1.5.1

Probably a side effect of the fix for #906

Potential fix in pull request #920 (branch issue919). Also fixes issue #918.

Going to merge this now and make a new release later today. Please reopen if problems persist.

thank you for the quick fix. Will test in the new version.