Bug in scipy.io.netcdf #232

Closed
wants to merge 1 commit into
from

Projects

None yet

3 participants

@honnorat

Hi,

There is a bug in scipy.io.netcdf when calling sync() or flush(). When writing to disk, the whole Netcdf structure is written, but the file is not rewound. Therefore, the second call to _write() (which can happen when closing the file) just append a second version of the structure after the first one.

This patch just adds a seek(0) before any writing operation.

@pv
Member
pv commented May 29, 2012

Can you add a regression test --- i.e., write a small test program that fails previously, but works after this bugfix?

@honnorat
#!/usr/bin/env python
import os
import scipy.io.netcdf as cdf

f = cdf.netcdf_file('example.nc', mode='w')
x = f.createDimension('x',4)
v = f.createVariable('v', 'i2', ['x'])

v[:] = 1
f.flush()
print "First read  : %d" % os.path.getsize('example.nc')

v[:] = 2
f.flush()
print "Second read : %d" % os.path.getsize('example.nc')

f.close()
print "Last read   : %d" % os.path.getsize('example.nc')

Before the fix:

First read  : 80
Second read : 168
Last read   : 264

After the fix:

First read  : 80
Second read : 88
Last read   : 88
@rgommers
Member
rgommers commented Jun 3, 2012

I converted the above to a regression test. It actually didn't work with numpy 1.7.0-dev (because of http://news.gmane.org/gmane.comp.python.numeric.general), so I fixed the dtype parameter handling as well. Please check if that looks OK: rgommers@76a983e

@rgommers
Member
rgommers commented Jun 6, 2012
@rgommers
Member
rgommers commented Jun 9, 2012

I merged this PR and my fix/tests in 7ac86ab, in order to get it in for 0.11.

@rgommers
Member
rgommers commented Jun 9, 2012

Thanks for your contribution Marc.

@rgommers rgommers closed this Jun 9, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment