misc.grid
Generic read module which determines format from extension.
1"""\ 2Generic read module which determines format from extension. 3""" 4import logging 5import os.path as pth 6 7 8# module specific log; add a null handler so that we won't get an 9# error if the main program hasn't set up a log 10log = logging.getLogger(__name__) # pylint: disable=invalid-name 11log.addHandler(logging.NullHandler()) 12 13 14def read_grid(filename, cache_dir=None): 15 """\ 16 :param filename: Name of the grid file to read, including path 17 :type filename: str 18 :param cache_dir: Path to a directory where a cache of the grid 19 may be stored to ensure faster read next time. 20 :type cache_dir: str 21 """ 22 # allow shortcut to home directories to be used in paths 23 fullname = pth.expanduser(filename) 24 25 # split the filename into directory, name and extension 26 base, ext = pth.splitext(fullname) 27 28 if ext.lower() == '.grdecl': 29 from misc import grdecl as grdecl 30 log.info("Reading corner point grid from \"%s\"", fullname) 31 grid = grdecl.read(fullname) 32 33 elif ext.lower() == '.egrid': 34 # in case we only have a simulation available, with the binary 35 # output from the restart, we can read this directly 36 from misc import ecl as ecl 37 log.info("Reading binary Eclipse grid from \"%s\"", fullname) 38 egrid = ecl.EclipseGrid(base) 39 grid = {'DIMENS': egrid.shape[::-1], 40 'COORD': egrid.coord, 41 'ZCORN': egrid.zcorn, 42 'ACTNUM': egrid.actnum} 43 44 elif ext.lower() == '.pickle': 45 # direct import of pickled file (should not do this, prefer to read 46 # it through a cache directory 47 import pickle 48 log.info("Reading binary grid dump from \"%s\"", fullname) 49 with open(fullname, 'rb') as f: 50 grid = pickle.load(f) 51 52 else: 53 raise ValueError( 54 "File format with extension \"{0}\" is unknown".format(ext)) 55 56 return grid
log =
<Logger misc.grid (WARNING)>
def
read_grid(filename, cache_dir=None):
15def read_grid(filename, cache_dir=None): 16 """\ 17 :param filename: Name of the grid file to read, including path 18 :type filename: str 19 :param cache_dir: Path to a directory where a cache of the grid 20 may be stored to ensure faster read next time. 21 :type cache_dir: str 22 """ 23 # allow shortcut to home directories to be used in paths 24 fullname = pth.expanduser(filename) 25 26 # split the filename into directory, name and extension 27 base, ext = pth.splitext(fullname) 28 29 if ext.lower() == '.grdecl': 30 from misc import grdecl as grdecl 31 log.info("Reading corner point grid from \"%s\"", fullname) 32 grid = grdecl.read(fullname) 33 34 elif ext.lower() == '.egrid': 35 # in case we only have a simulation available, with the binary 36 # output from the restart, we can read this directly 37 from misc import ecl as ecl 38 log.info("Reading binary Eclipse grid from \"%s\"", fullname) 39 egrid = ecl.EclipseGrid(base) 40 grid = {'DIMENS': egrid.shape[::-1], 41 'COORD': egrid.coord, 42 'ZCORN': egrid.zcorn, 43 'ACTNUM': egrid.actnum} 44 45 elif ext.lower() == '.pickle': 46 # direct import of pickled file (should not do this, prefer to read 47 # it through a cache directory 48 import pickle 49 log.info("Reading binary grid dump from \"%s\"", fullname) 50 with open(fullname, 'rb') as f: 51 grid = pickle.load(f) 52 53 else: 54 raise ValueError( 55 "File format with extension \"{0}\" is unknown".format(ext)) 56 57 return grid
Parameters
- filename: Name of the grid file to read, including path
- cache_dir: Path to a directory where a cache of the grid may be stored to ensure faster read next time.