Inserting data into variable with unlimited dimension with slice(n, n+1, None) is broken
cpaulik opened this issue · 5 comments
cpaulik commented
Another side effect of #906 it seems
import netCDF4
import numpy as np
with netCDF4.Dataset('test.nc','w') as f:
f.createDimension('d1',3)
f.createDimension('d2',None)
f.createVariable('v1',np.float,('d1','d1',))
f['v1'][0:1] = np.arange(3)
Running this on 1.5.1.1
gives
1.5.1.1
(0, 3)
Traceback (most recent call last):
File "01-160948.py", line 23, in <module>
f['v1'][0:1] = np.arange(3)
File "netCDF4/_netCDF4.pyx", line 4796, in netCDF4._netCDF4.Variable.__setitem__
File "/home/cpa/.pyenv/versions/miniconda2-4.3.30/envs/pytesmo/lib/python3.7/site-packages/netCDF4/utils.py", line 374, in _StartCountStride
datashapenew = datashapenew + (datashape[i],)
IndexError: tuple index out of range
on 1.5.0.1 it works and gives
1.5.0.1
(0, 3)
(1, 3)
The following is also broken so it is not only for the first element:
import netCDF4
import numpy as np
with netCDF4.Dataset('test.nc','w') as f:
f.createDimension('d1',3)
f.createDimension('d2',None)
f.createVariable('v1',np.float,('d2','d1'))
print(f['v1'].shape)
f['v1'][0] = np.arange(3)
print(f['v1'].shape)
f['v1'][1:2] = np.arange(3)
print(f['v1'].shape)
jswhit commented
This indexing stuff is a mess, and unlimited dimensions make it even worse. I wish there were someway to use the numpy indexing machinery for this instead of re-inventing our own.
The workaround for this is to use np.arange(3).reshape(1,3)
.
jswhit commented
BTW - is there a typo in your first example?
f.createVariable('v1',np.float,('d1','d1',)) <-- should this be ('d2','d1')?
cpaulik commented
Yes the first example was copied to soon.
cpaulik commented
Thanks. The fix works for me.