Skip to content

Commit

Permalink
Merge pull request #1084 from Unidata/issue1083
Browse files Browse the repository at this point in the history
potential fix for issue #1083
  • Loading branch information
jswhit authored Feb 12, 2021
2 parents 7a886db + 8466946 commit 947514f
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 3 deletions.
2 changes: 2 additions & 0 deletions Changelog
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
* Add "fromcdl" and "tocdl" Dataset methods for import/export of CDL
via ncdump/ncgen called externally via the subprocess module (issue #1078).
* remove python 2.7 support.
* broadcast data (if possible)to conform to variable shape when writing to a slice
(issue #1083).

version 1.5.5.1 (tag v1.5.5.1rel)
==================================
Expand Down
6 changes: 3 additions & 3 deletions src/netCDF4/_netCDF4.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -4921,9 +4921,9 @@ cannot be safely cast to variable data type""" % attname
# and fill with scalar values.
if data.shape == ():
data = numpy.tile(data,datashape)
# reshape data array by adding extra dimensions
# if needed to conform with start,count,stride.
if len(data.shape) != len(datashape):
# reshape data array if needed to conform with start,count,stride.
if data.ndim != len(datashape) or\
(data.shape != datashape and data.ndim > 1): # issue #1083
# create a view so shape in caller is not modified (issue 90)
try: # if extra singleton dims, just reshape
data = data.view()
Expand Down
13 changes: 13 additions & 0 deletions test/tst_slicing.py
Original file line number Diff line number Diff line change
Expand Up @@ -249,5 +249,18 @@ def test_issue922(self):
assert_array_equal(f['v2'][:,1:3],np.arange(6,dtype=np.int).reshape(3,2))
assert_array_equal(f['v2'][:,0],np.arange(3,dtype=np.int))

def test_issue1083(self):
with Dataset(self.file, "w") as nc:
nc.createDimension("test", 5)
v = nc.createVariable("var", "f8", ("test", "test", "test"))
v[:] = 1 # works
v[:] = np.ones(()) # works
v[:] = np.ones((1,)) # works
v[:] = np.ones((5,)) # works
v[:] = np.ones((5,5,5)) # works
v[:] = np.ones((5,1,1)) # fails (before PR #1084)
v[:] = np.ones((5,1,5)) # fails (before PR #1084)
v[:] = np.ones((5,5,1)) # fails (before PR #1084)

if __name__ == '__main__':
unittest.main()

0 comments on commit 947514f

Please sign in to comment.