Permalink
Browse files

Merge pull request #471 from oth/upstream/pr/471

BUG: io: Convert outgoing netCDF arrays to big-endian
  • Loading branch information...
2 parents 0184491 + 48ccda4 commit cf784e72fa69d895942bef13eb773c51e7cd18f2 @pv pv committed Mar 24, 2013
Showing with 26 additions and 2 deletions.
  1. +1 −1 scipy/io/netcdf.py
  2. +25 −1 scipy/io/tests/test_netcdf.py
View
2 scipy/io/netcdf.py
@@ -290,7 +290,7 @@ def createVariable(self, name, type, dimensions):
if (typecode, size) not in REVERSE:
raise ValueError("NetCDF 3 does not support type %s" % type)
- data = empty(shape_, dtype=type)
+ data = empty(shape_, dtype=type.newbyteorder("B")) #convert to big endian always for NetCDF 3
self.variables[name] = netcdf_variable(data, typecode, size, shape, dimensions)
return self.variables[name]
View
26 scipy/io/tests/test_netcdf.py
@@ -11,7 +11,7 @@
from glob import glob
import numpy as np
-from numpy.testing import dec, assert_
+from numpy.testing import dec, assert_, assert_allclose
from scipy.io.netcdf import netcdf_file
@@ -175,3 +175,27 @@ def test_dtype_specifiers():
v3 = f.createVariable('v3', np.dtype(np.int16), ['x'])
f.close()
+
+def test_ticket_1720():
+ io = BytesIO()
+
+ items = [0,0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9]
+
+ f = netcdf_file(io, 'w')
+ f.history = 'Created for a test'
+ f.createDimension('float_var', 10)
+ float_var = f.createVariable('float_var', 'f', ('float_var',))
+ float_var[:] = items
+ float_var.units = 'metres'
+ f.flush()
+ contents = io.getvalue()
+ f.close()
+
+ io = BytesIO(contents)
+ f = netcdf_file(io, 'r')
+ assert_equal(f.history, b'Created for a test')
+ float_var = f.variables['float_var']
+ assert_equal(float_var.units, b'metres')
+ assert_equal(float_var.shape, (10,))
+ assert_allclose(float_var[:], items)
+ f.close()

0 comments on commit cf784e7

Please sign in to comment.