Skip to content

Commit

Permalink
BUG: io.netcdf: make createVariable() work better with dtype specifiers.
Browse files Browse the repository at this point in the history
Also add a regression test for the previous commit.
  • Loading branch information
rgommers committed Jun 3, 2012
1 parent 1d84542 commit 76a983e
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 4 deletions.
6 changes: 2 additions & 4 deletions scipy/io/netcdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]

Expand Down
27 changes: 27 additions & 0 deletions scipy/io/tests/test_netcdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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.