1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
|
from ..constants import _file_to_fh
from ..functions import open_files_threshold_exceeded, close_one_file
from .umread.umfile import File, UMFileException
_file_to_UM = _file_to_fh.setdefault('UM', {})
def _open_um_file(filename, aggregate=True):
'''Open a UM fields file or PP file and read it into a
`umread.umfile.File` object.
If there is already a `umread.umfile.File` object for the file then it
is returned with an open file descriptor.
:Parameters:
filename : str
The file to be opened.
:Returns:
out : umread.umfile.File
The opened file with an open file descriptor.
:Examples:
'''
f = _file_to_UM.get(filename, None)
if f is not None:
if f.fd is None:
if open_files_threshold_exceeded():
# Close a random data array file to make way for this
# one
close_one_file()
f.open_fd()
#--- End: if
return f
if open_files_threshold_exceeded():
# Close a random data array file to make way for this one
close_one_file()
try:
f = File(filename)
except Exception as error:
try:
f.close_fd()
except:
pass
raise error
# Add a close method to the file object
f.close = f.close_fd
# Update the _file_to_UM dictionary
_file_to_UM[filename] = f
return f
#--- End: def
def _close_um_file(filename):
'''Close a PP or UM fields file.
Does nothing if the file is already closed.
:Parameters:
filename : str
The file to be closed.
:Returns:
None
:Examples:
'''
f = _file_to_UM.pop(filename, None)
if f is not None:
f.close_fd()
#--- End: def
|