def test_filedescriptors(): """Test that filedescriptors are properly closed""" fd_dir = Path("/proc/") / str(os.getpid()) / "fd" if not fd_dir.exists(): print("Counting file descriptors on non-Linux not supported") return pre_fd_count = len(list(fd_dir.glob("*"))) eclfiles = EclFiles(EIGHTCELLS) # No opened files yet: assert len(list(fd_dir.glob("*"))) == pre_fd_count eclfiles.close() # No change, no files to close: assert len(list(fd_dir.glob("*"))) == pre_fd_count eclfiles.get_egrid() # This should not leave any file descriptor open assert len(list(fd_dir.glob("*"))) == pre_fd_count eclfiles.get_initfile() assert len(list(fd_dir.glob("*"))) == pre_fd_count assert eclfiles._initfile is not None eclfiles.close() assert len(list(fd_dir.glob("*"))) == pre_fd_count assert eclfiles._initfile is None eclfiles.get_rstfile() # Automatically closed by libecl assert len(list(fd_dir.glob("*"))) == pre_fd_count assert eclfiles._rstfile is not None eclfiles.close() assert len(list(fd_dir.glob("*"))) == pre_fd_count assert eclfiles._rstfile is None eclfiles.get_eclsum() assert len(list(fd_dir.glob("*"))) == pre_fd_count + 1 eclfiles.close() assert len(list(fd_dir.glob("*"))) == pre_fd_count eclfiles.get_egridfile() assert len(list(fd_dir.glob("*"))) == pre_fd_count assert eclfiles._egridfile is not None eclfiles.close() assert len(list(fd_dir.glob("*"))) == pre_fd_count assert eclfiles._egridfile is None eclfiles.get_rftfile() assert len(list(fd_dir.glob("*"))) == pre_fd_count assert eclfiles._rftfile is not None eclfiles.close() assert len(list(fd_dir.glob("*"))) == pre_fd_count assert eclfiles._rftfile is None eclfiles.get_ecldeck() # This should not leave any file descriptor open assert len(list(fd_dir.glob("*"))) == pre_fd_count
def check_applicability(eclfiles: ecl2df.EclFiles) -> None: """Check that the input is relevant for usage with check_swatinit. This function may raise exceptions, SystemExit or only give warnings""" deck = eclfiles.get_ecldeck() init = eclfiles.get_initfile() if ( "SWATINIT" not in [initheader[0] for initheader in init.headers] and "SWATINIT" not in deck ): logger.warning( "INIT-file/deck does not have SWATINIT, this tool has limited use." ) if "RPTRST" not in deck: logger.warning( "RPTRST not found in DATA-file, UNRST file is expected to be missing" ) try: eclfiles.get_rstfile() except FileNotFoundError as exception: if "UNIFOUT" not in deck: sys.exit( "Only unified RESTARTs are supported. Add UNIFOUT to your DATA file." ) logger.error(str(exception)) sys.exit( "No UNRST file found. This is required to get the initial water saturation" )
def df(eclfiles: EclFiles, coords: bool = False, pillars: bool = False) -> pd.DataFrame: """Produce a Pandas Dataframe with NNC information A NNC is a pair of cells that are not next to each other in the index space (I, J, K), and are associated to a non-zero transmissibility. Columns: I1, J1, K1 (first cell in cell pair) I2, J2, K2 (second cell in cell pair), TRAN (transmissibility between the two cells) Args: eclfiles: object that can serve EclFile and EclGrid on demand coords: Set to True if you want the midpoint of the two connected cells to be computed and added to the columns X, Y and Z. pillars: Set to True if you want to filter to vertical (along pillars) connections only. Returns: Empty if no NNC information found. """ egrid_file = eclfiles.get_egridfile() egrid_grid = eclfiles.get_egrid() init_file = eclfiles.get_initfile() if not ("NNC1" in egrid_file and "NNC2" in egrid_file): logger.warning("No NNC data in EGRID") return pd.DataFrame() if "TRANNNC" not in init_file: logger.warning("No TRANNNC data in INIT (E300 parallell run?)") return pd.DataFrame() # Grid indices for first cell in cell pairs, into a vertical # vector. The indices are "global" in libecl terms, and are # 1-based (FORTRAN). Convert to zero-based before sending to get_ijk() nnc1 = egrid_file["NNC1"][0].numpy_view().reshape(-1, 1) logger.info( "NNC1: len: %d, min: %d, max: %d (global indices)", len(nnc1), min(nnc1), max(nnc1), ) idx_cols1 = ["I1", "J1", "K1"] nnc1_df = pd.DataFrame( columns=idx_cols1, data=[egrid_grid.get_ijk(global_index=int(x) - 1) for x in nnc1], ) # Returned indices from get_ijk are zero-based, convert to 1-based indices nnc1_df[idx_cols1] = nnc1_df[idx_cols1] + 1 # Grid indices for second cell in cell pairs nnc2 = egrid_file["NNC2"][0].numpy_view().reshape(-1, 1) logger.info( "NNC2: len: %d, min: %d, max: %d (global indices)", len(nnc2), min(nnc2), max(nnc2), ) idx_cols2 = ["I2", "J2", "K2"] nnc2_df = pd.DataFrame( columns=idx_cols2, data=[egrid_grid.get_ijk(global_index=int(x) - 1) for x in nnc2], ) nnc2_df[idx_cols2] = nnc2_df[idx_cols2] + 1 # Obtain transmissibility value, corresponding to the cell pairs above. tran = init_file["TRANNNC"][0].numpy_view().reshape(-1, 1) logger.info( "TRANNNC: len: %d, min: %f, max: %f, mean=%f", len(tran), min(tran), max(tran), tran.mean(), ) tran_df = pd.DataFrame(columns=["TRAN"], data=tran) nncdf = pd.concat([nnc1_df, nnc2_df, tran_df], axis=1) if pillars: nncdf = filter_vertical(nncdf) if coords: nncdf = add_nnc_coords(nncdf, eclfiles) return nncdf