Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Inserting data into variable with unlimited dimension with slice(n, n+1, None) is broken #922

Closed
cpaulik opened this issue May 3, 2019 · 5 comments

Comments

@cpaulik
Copy link
Contributor

cpaulik commented May 3, 2019

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
Copy link
Collaborator

jswhit commented May 3, 2019

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
Copy link
Collaborator

jswhit commented May 3, 2019

BTW - is there a typo in your first example?

f.createVariable('v1',np.float,('d1','d1',)) <-- should this be ('d2','d1')?

@cpaulik
Copy link
Contributor Author

cpaulik commented May 3, 2019

Yes the first example was copied to soon.

jswhit added a commit that referenced this issue May 3, 2019
@jswhit
Copy link
Collaborator

jswhit commented May 3, 2019

Fix in pull request #924. Keep trying to break it!

@cpaulik
Copy link
Contributor Author

cpaulik commented May 6, 2019

Thanks. The fix works for me.

jswhit added a commit that referenced this issue May 6, 2019
@jswhit jswhit closed this as completed May 6, 2019
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants