Skip to content

Commit 947514f

Browse files
author
Jeff Whitaker
authored
Merge pull request #1084 from Unidata/issue1083
potential fix for issue #1083
2 parents 7a886db + 8466946 commit 947514f

File tree

3 files changed

+18
-3
lines changed

3 files changed

+18
-3
lines changed

Changelog

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
* Add "fromcdl" and "tocdl" Dataset methods for import/export of CDL
1010
via ncdump/ncgen called externally via the subprocess module (issue #1078).
1111
* remove python 2.7 support.
12+
* broadcast data (if possible)to conform to variable shape when writing to a slice
13+
(issue #1083).
1214

1315
version 1.5.5.1 (tag v1.5.5.1rel)
1416
==================================

src/netCDF4/_netCDF4.pyx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4921,9 +4921,9 @@ cannot be safely cast to variable data type""" % attname
49214921
# and fill with scalar values.
49224922
if data.shape == ():
49234923
data = numpy.tile(data,datashape)
4924-
# reshape data array by adding extra dimensions
4925-
# if needed to conform with start,count,stride.
4926-
if len(data.shape) != len(datashape):
4924+
# reshape data array if needed to conform with start,count,stride.
4925+
if data.ndim != len(datashape) or\
4926+
(data.shape != datashape and data.ndim > 1): # issue #1083
49274927
# create a view so shape in caller is not modified (issue 90)
49284928
try: # if extra singleton dims, just reshape
49294929
data = data.view()

test/tst_slicing.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,5 +249,18 @@ def test_issue922(self):
249249
assert_array_equal(f['v2'][:,1:3],np.arange(6,dtype=np.int).reshape(3,2))
250250
assert_array_equal(f['v2'][:,0],np.arange(3,dtype=np.int))
251251

252+
def test_issue1083(self):
253+
with Dataset(self.file, "w") as nc:
254+
nc.createDimension("test", 5)
255+
v = nc.createVariable("var", "f8", ("test", "test", "test"))
256+
v[:] = 1 # works
257+
v[:] = np.ones(()) # works
258+
v[:] = np.ones((1,)) # works
259+
v[:] = np.ones((5,)) # works
260+
v[:] = np.ones((5,5,5)) # works
261+
v[:] = np.ones((5,1,1)) # fails (before PR #1084)
262+
v[:] = np.ones((5,1,5)) # fails (before PR #1084)
263+
v[:] = np.ones((5,5,1)) # fails (before PR #1084)
264+
252265
if __name__ == '__main__':
253266
unittest.main()

0 commit comments

Comments
 (0)