Skip to content

Commit

Permalink
BUG: netcdf_file.flush() was trying to access non-existing attribute.
Browse files Browse the repository at this point in the history
Backport of r6540.
  • Loading branch information
rgommers committed Jun 18, 2010
1 parent 7383df6 commit 8d72c76
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 14 deletions.
24 changes: 12 additions & 12 deletions scipy/io/netcdf.py
Expand Up @@ -4,7 +4,7 @@
This module implements the Scientific.IO.NetCDF API to read and create
NetCDF files. The same API is also used in the PyNIO and pynetcdf
modules, allowing these modules to be used interchangebly when working
with NetCDF files. The major advantage of ``scipy.io.netcdf`` over other
with NetCDF files. The major advantage of ``scipy.io.netcdf`` over other
modules is that it doesn't require the code to be linked to the NetCDF
libraries as the other modules do.
Expand All @@ -28,7 +28,7 @@
and they are stored at the end of the file per record, ie
A[0], B[0], ..., A[1], B[1], ..., etc,
so that new data can be appended to the file without changing its original
structure. Non-record data are padded to a 4n bytes boundary. Record data
are also padded, unless there is exactly one record variable in the file,
Expand Down Expand Up @@ -86,7 +86,7 @@
from numpy import little_endian as LITTLE_ENDIAN


ABSENT = '\x00\x00\x00\x00\x00\x00\x00\x00'
ABSENT = '\x00\x00\x00\x00\x00\x00\x00\x00'
ZERO = '\x00\x00\x00\x00'
NC_BYTE = '\x00\x00\x00\x01'
NC_CHAR = '\x00\x00\x00\x02'
Expand Down Expand Up @@ -193,7 +193,7 @@ def __setattr__(self, attr, value):
def close(self):
if not self.fp.closed:
try:
self.flush()
self.flush()
finally:
self.fp.close()
__del__ = close
Expand All @@ -203,7 +203,7 @@ def createDimension(self, name, length):
self._dims.append(name)

def createVariable(self, name, type, dimensions):
shape = tuple([self.dimensions[dim] for dim in 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)
Expand All @@ -216,7 +216,7 @@ def createVariable(self, name, type, dimensions):
return self.variables[name]

def flush(self):
if self.mode is 'w':
if hasattr(self, 'mode') and self.mode is 'w':
self._write()
sync = flush

Expand Down Expand Up @@ -266,7 +266,7 @@ def _write_var_array(self):
self.fp.write(NC_VARIABLE)
self._pack_int(len(self.variables))

# Sort variables non-recs first, then recs. We use a DSU
# Sort variables non-recs first, then recs. We use a DSU
# since some people use pupynere with Python 2.3.x.
deco = [ (v._shape and not v.isrec, k) for (k, v) in self.variables.items() ]
deco.sort()
Expand Down Expand Up @@ -321,7 +321,7 @@ def _write_var_metadata(self, name):

def _write_var_data(self, name):
var = self.variables[name]

# Set begin in file header.
the_beguine = self.fp.tell()
self.fp.seek(var._begin)
Expand All @@ -330,7 +330,7 @@ def _write_var_data(self, name):

# Write data.
if not var.isrec:
self.fp.write(var.data.tostring())
self.fp.write(var.data.tostring())
count = var.data.size * var.data.itemsize
self.fp.write('0' * (var._vsize - count))
else: # record variable
Expand Down Expand Up @@ -379,7 +379,7 @@ def _write_values(self, values):
dtype_ = '>%s' % typecode
if size > 1: dtype_ += str(size)

values = asarray(values, dtype=dtype_)
values = asarray(values, dtype=dtype_)

self.fp.write(nc_type)

Expand Down Expand Up @@ -527,7 +527,7 @@ def _read_var(self):
dimensions = []
shape = []
dims = self._unpack_int()

for i in range(dims):
dimid = self._unpack_int()
dimname = self._dims[dimid]
Expand Down Expand Up @@ -565,7 +565,7 @@ def _read_values(self):
values = fromstring(values, dtype='>%s%d' % (typecode, size))
if values.shape == (1,): values = values[0]
else:
values = values.rstrip('\x00')
values = values.rstrip('\x00')
return values

def _pack_begin(self, begin):
Expand Down
4 changes: 2 additions & 2 deletions scipy/io/tests/test_netcdf.py
Expand Up @@ -38,7 +38,7 @@ def gen_for_simple(ncfileobj):
yield assert_equal, str(time.units), 'days since 2008-01-01'
yield assert_equal, time.shape, (N_EG_ELS,)
yield assert_equal, time[-1], N_EG_ELS-1


def test_read_write_files():
# test round trip for example file
Expand Down Expand Up @@ -118,4 +118,4 @@ def test_read_example_data():
for fname in glob(pjoin(TEST_DATA_PATH, '*.nc')):
f = netcdf_file(fname, 'r')
f = netcdf_file(fname, 'r', mmap=False)

0 comments on commit 8d72c76

Please sign in to comment.