It's probably simple, but I haven't been able to find a solution online... I'm trying to work with a series of datasets stored as netcdf files. I open each one up, read in some keys points, then move onto the next file. I am finding that I constantly hit mmap errors/the script slows down as more files are being read in. I believe it may be because the netcdf files are not being properly closed by the .close() command.
I've been testing this:
from scipy.io.netcdf import netcdf_file as ncfile
f=ncfile(netcdf_file,mode='r')
f.close()
then if I try
>>>f
<scipy.io.netcdf.netcdf_file object at 0x24d29e10>
and
>>>f.variables['temperature'][:]
array([ 1234.68034431, 1387.43136567, 1528.35794546, ..., 3393.91061952,
3378.2844357 , 3433.06715226])
So it appears the file is still open? What does close() actually do? how do I know it has worked? Is there a way to close/clear all open files from python?
Software: Python 2.7.6, scipy 0.13.2, netcdf 4.0.1
The code for
f.close
is:f.fp
is the file object. SoBut I see from playing around with the
f
, that I can still create dimensions and variables. Butf.flush()
returns an error.It does not look like it uses
mmap
during data writes, just during read.I don't have much experience with
mmap
. It looks like it sets up ammap
object based on a block of bytes in the file, and uses that as the data buffer for the variable. I don't know what happens to that access if the underlying file is closed. I wouldn't be surprised if there is some sort ofmmap
error.If the file is opened with
mmap=False
, then the whole variable is read into memory, and accessed like a regularnumpy
array.My guess is that if you open a file without specifying the
mmap
mode, read an variable from it, and then close the file, that it is unsafe to reference that variable and its data later. Any reference that requires loading more data could result in ammap
error.But if you open the file with
mmap=False
, you should be able slice the variable even after closing the file.I don't see how the
mmap
for one file or variable could interfer with access to other files and variables. But I'd have to read more onmmap
to be sure of that.And from the
netcdf
docs: