Permalink
Browse files

BUG: io.netcdf: make createVariable() work better with dtype specifiers.

Also add a regression test for the previous commit.
  • Loading branch information...
rgommers committed Jun 3, 2012
1 parent 1d84542 commit 76a983ec625556bcc7a1f8ba76339399e8af68a4
Showing with 29 additions and 4 deletions.
  1. +2 −4 scipy/io/netcdf.py
  2. +27 −0 scipy/io/tests/test_netcdf.py
View
@@ -280,14 +280,12 @@ def createVariable(self, name, type, dimensions):
shape = tuple([self.dimensions[dim] for dim in dimensions])
shape_ = tuple([dim or 0 for dim in shape]) # replace None with 0 for numpy
- if isinstance(type, basestring): type = dtype(type)
+ type = dtype(type)
typecode, size = type.char, type.itemsize
if (typecode, size) not in REVERSE:
raise ValueError("NetCDF 3 does not support type %s" % type)
- dtype_ = '>%s' % typecode
- if size > 1: dtype_ += str(size)
- data = empty(shape_, dtype=dtype_)
+ data = empty(shape_, dtype=type)
self.variables[name] = netcdf_variable(data, typecode, size, shape, dimensions)
return self.variables[name]
@@ -14,6 +14,7 @@
import numpy as np
from numpy.compat import asbytes
+from numpy.testing import assert_
from scipy.io.netcdf import netcdf_file
@@ -150,3 +151,29 @@ def test_write_invalid_dtype():
f.createVariable, 'time', dt, ('time',)
f.close()
+
+def test_flush_rewind():
+ stream = BytesIO()
+ f = make_simple(stream, mode='w')
+ x = f.createDimension('x',4)
+ v = f.createVariable('v', 'i2', ['x'])
+ v[:] = 1
+ f.flush()
+ len_single = len(stream.getvalue())
+ f.flush()
+ len_double = len(stream.getvalue())
+ f.close()
+ assert_(len_single == len_double)
+
+
+def test_dtype_specifiers():
+ # Numpy 1.7.0-dev had a bug where 'i2' wouldn't work.
+ # Specifying np.int16 or similar only works from the same commit as this
+ # comment was made.
+ f = make_simple(BytesIO(), mode='w')
+ x = f.createDimension('x',4)
+ v1 = f.createVariable('v1', 'i2', ['x'])
+ v2 = f.createVariable('v2', np.int16, ['x'])
+ v3 = f.createVariable('v3', np.dtype(np.int16), ['x'])
+ f.close()
+

0 comments on commit 76a983e

Please sign in to comment.