def test_output_units(self): n = 10 a = 1 grid = GridGen.createRectangular( (n,n,n), (a,a,a)) with TestAreaContext("python/ecl_grid/units"): grid.save_EGRID( "CASE.EGRID" , output_unit = EclUnitTypeEnum.ECL_FIELD_UNITS ) f = EclFile("CASE.EGRID") g = f["GRIDUNIT"][0] self.assertEqual( g[0].strip( ) , "FEET" ) g2 = EclGrid("CASE.EGRID") self.assertFloatEqual( g2.cell_volume( global_index = 0 ) , 3.28084*3.28084*3.28084) grid.save_EGRID( "CASE.EGRID" ) f = EclFile("CASE.EGRID") g = f["GRIDUNIT"][0] self.assertEqual( g[0].strip( ) , "METRES" ) grid.save_EGRID( "CASE.EGRID" , output_unit = EclUnitTypeEnum.ECL_LAB_UNITS) f = EclFile("CASE.EGRID") g = f["GRIDUNIT"][0] self.assertEqual( g[0].strip() , "CM" ) g2 = EclGrid("CASE.EGRID") self.assertFloatEqual( g2.cell_volume( global_index = 0 ) , 100*100*100 )
def test_geertsma_kernel(): grid = EclGrid.createRectangular(dims=(1, 1, 1), dV=(50, 50, 50)) with TestAreaContext("Subsidence"): p1 = [1] create_restart(grid, "TEST", p1) create_init(grid, "TEST") init = EclFile("TEST.INIT") restart_file = EclFile("TEST.UNRST") restart_view1 = restart_file.restartView( sim_time=datetime.date(2000, 1, 1)) subsidence = EclSubsidence(grid, init) subsidence.add_survey_PRESSURE("S1", restart_view1) youngs_modulus = 5E8 poisson_ratio = 0.3 seabed = 0 above = 100 topres = 2000 receiver = (1000, 1000, 0) dz = subsidence.evalGeertsma("S1", None, receiver, youngs_modulus, poisson_ratio, seabed) np.testing.assert_almost_equal(dz, 3.944214576168326e-09) receiver = (1000, 1000, topres - seabed - above) dz = subsidence.evalGeertsma("S1", None, receiver, youngs_modulus, poisson_ratio, seabed) np.testing.assert_almost_equal(dz, 5.8160298201497136e-08)
def test_fast_open(self): with TestAreaContext("index"): f0 = EclFile(self.test_file) f0.write_index("index") f1 = EclFile(self.test_file, 0, "index") for kw0, kw1 in zip(f0, f1): self.assertEqual(kw0, kw1)
def test_fast_open(self): with TestAreaContext("index"): f0 = EclFile( self.test_file ) f0.write_index("index") f1 = EclFile( self.test_file , 0 , "index") for kw0,kw1 in zip(f0,f1): self.assertEqual( kw0,kw1 )
def report_list_file_test(self, fname, rlist0): rlist = EclFile.file_report_list(fname) self.assertListEqual(rlist, rlist0) f = EclFile(fname) rlist = f.report_list self.assertListEqual(rlist, rlist0)
def test_units(self): case = create_case() self.assertEqual(case.unit_system, EclUnitTypeEnum.ECL_METRIC_UNITS) # We do not really have support for writing anything else than the # default MERIC unit system. To be able to test the read functionality # we therefor monkey-patch the summary files in place. with TestAreaContext("unit_test"): case = create_case("UNITS") case.fwrite() case2 = EclSum("UNITS") kw_list = [] f = EclFile("UNITS.SMSPEC") for kw in f: if kw.name == "INTEHEAD": kw[0] = 3 kw_list.append(kw.copy()) f.close() with openFortIO("UNITS.SMSPEC", mode = FortIO.WRITE_MODE) as f: for kw in kw_list: kw.fwrite(f) case = EclSum("UNITS") self.assertEqual(case.unit_system, EclUnitTypeEnum.ECL_LAB_UNITS)
def test_broken_file(self): with TestAreaContext("test_broken_file"): with open("CASE.FINIT", "w") as f: f.write("This - is not a ECLISPE file\nsdlcblhcdbjlwhc\naschscbasjhcasc\nascasck c s s aiasic asc") f = EclFile("CASE.FINIT") self.assertEqual(len(f), 0) kw = EclKW("HEADER", 100, EclDataType.ECL_INT) with openFortIO("FILE",mode=FortIO.WRITE_MODE) as f: kw.fwrite(f) kw.fwrite(f) with open("FILE", "a+") as f: f.write("Seom random gibberish") f = EclFile("FILE") self.assertEqual(len(f), 2) with openFortIO("FILE",mode=FortIO.WRITE_MODE) as f: kw.fwrite(f) kw.fwrite(f) file_size = os.path.getsize("FILE") with open("FILE", "a+") as f: f.truncate(file_size * 0.75) f = EclFile("FILE") self.assertEqual(len(f), 1)
def test_index(self): with TestAreaContext("python/ecl_file/truncated"): f0 = EclFile(self.test_file) f0.write_index("index") f1 = EclFile(self.test_file, index_filename="index") for kw0, kw1 in zip(f0, f1): self.assertEqual(kw0, kw1)
def test_index(self): with TestAreaContext("python/ecl_file/truncated"): f0 = EclFile( self.test_file ) f0.write_index( "index" ) f1 = EclFile( self.test_file , index_filename = "index") for kw0,kw1 in zip(f0,f1): self.assertEqual(kw0,kw1)
def test_dates(self): f = EclFile(self.u_file) dates = f.dates self.assertTrue(len(dates) == 63) f = EclFile(self.xfile0) dates = f.dates self.assertTrue(len(dates) == 1) self.assertTrue(dates[0] == datetime.datetime(2000, 1, 1))
def test_fwrite( self ): #work_area = TestArea("python/ecl_file/fwrite") with TestAreaContext("python/ecl_file/fwrite"): rst_file = EclFile(self.test_file) fortio = FortIO("ECLIPSE.UNRST", FortIO.WRITE_MODE) rst_file.fwrite(fortio) fortio.close() rst_file.close() self.assertFilesAreEqual("ECLIPSE.UNRST", self.test_file)
def test_report_list(self): rlist0 = list(range(63)) self.report_list_file_test(self.u_file, rlist0) rlist0 = [0] self.report_list_file_test(self.xfile0, rlist0) f = EclFile(self.grid_file) with self.assertRaises(ArgumentError): #argumentError wraps the expected TypeError EclFile.file_report_list(f)
def restartthinner( filename: str, numberofslices: int, quiet: bool = False, dryrun: bool = True, keep: bool = False, ) -> None: """ Thin an existing UNRST file to selected number of restarts. """ rst = EclFile(filename) restart_indices = get_restart_indices(filename) restart_dates = [ rst.iget_restart_sim_time(index) for index in range(0, len(restart_indices)) ] if numberofslices > 1: slicedates = pandas.DatetimeIndex( numpy.linspace( pandas.Timestamp(restart_dates[0]).value, pandas.Timestamp(restart_dates[-1]).value, int(numberofslices), )).values else: slicedates = [restart_dates[-1] ] # Only return last date if only one is wanted slicerstindices = list( date_slicer(slicedates, restart_dates, restart_indices).values()) slicerstindices.sort() slicerstindices = list(set(slicerstindices)) # uniquify if not quiet: print("Selected restarts:") print("-----------------------") for idx, rstidx in enumerate(restart_indices): if restart_indices[idx] in slicerstindices: slicepresent = "X" else: slicepresent = "" print("%4d: %s %s" % ( rstidx, datetime.date.strftime(restart_dates[idx], "%Y-%m-%d"), slicepresent, )) print("-----------------------") if not dryrun: if keep: if not quiet: print("Info: Backing up %s to %s" % (filename, filename + ".orig")) shutil.copyfile(filename, filename + ".orig") ecl_repacker(filename, slicerstindices, quiet) print("Written to " + filename)
def test_kw( self ): f = EclFile(self.u_file) kw1 = f["SWAT"][40] kw2 = f.restart_get_kw("SWAT", datetime.datetime(2003, 3, 1)) kw3 = f.restart_get_kw("SWAT", datetime.datetime(2003, 3, 1), copy=True) self.assertTrue(kw1.equal(kw2)) self.assertTrue(kw1.equal(kw3)) with self.assertRaises(IndexError): kw4 = f.restart_get_kw("SWAT", datetime.datetime(2009, 3, 17))
def test_report_list(self): rlist0 = list(range(63)) self.report_list_file_test(self.u_file, rlist0) rlist0 = [0] self.report_list_file_test(self.xfile0, rlist0) f = EclFile(self.grid_file) with self.assertRaises( ArgumentError): #argumentError wraps the expected TypeError EclFile.file_report_list(f)
def test_kw(self): f = EclFile(self.u_file) kw1 = f["SWAT"][40] kw2 = f.restart_get_kw("SWAT", datetime.datetime(2003, 3, 1)) kw3 = f.restart_get_kw("SWAT", datetime.datetime(2003, 3, 1), copy=True) self.assertTrue(kw1.equal(kw2)) self.assertTrue(kw1.equal(kw3)) with self.assertRaises(IndexError): kw4 = f.restart_get_kw("SWAT", datetime.datetime(2009, 3, 17))
def test_truncated(self): with TestAreaContext("python/ecl_file/truncated") as work_area: work_area.copy_file(self.test_file) size = os.path.getsize("ECLIPSE.UNRST") with open("ECLIPSE.UNRST" , "r+") as f: f.truncate( size / 2 ) with self.assertRaises(IOError): rst_file = EclFile("ECLIPSE.UNRST") with self.assertRaises(IOError): rst_file = EclFile("ECLIPSE.UNRST", flags=EclFileFlagEnum.ECL_FILE_WRITABLE)
def report_file_test(self, fname): self.assertTrue(EclFile.contains_report_step(fname, 4)) self.assertTrue(EclFile.contains_report_step(fname, 0)) self.assertTrue(EclFile.contains_report_step(fname, 62)) self.assertFalse(EclFile.contains_report_step(fname, -1)) self.assertFalse(EclFile.contains_report_step(fname, 100)) f = EclFile(fname) self.assertTrue(f.has_report_step(4)) self.assertTrue(f.has_report_step(0)) self.assertTrue(f.has_report_step(62)) self.assertFalse(f.has_report_step(-1)) self.assertFalse(f.has_report_step(100))
def test_save_kw(self): with TestAreaContext("python/ecl_file/save_kw"): data = range(1000) kw = EclKW("MY_KEY", len(data), EclDataType.ECL_INT) for index, val in enumerate(data): kw[index] = val clean_dump = "my_clean_file" fortio = FortIO(clean_dump, FortIO.WRITE_MODE) kw.fwrite(fortio) fortio.close() test_file = "my_dump_file" fortio = FortIO(test_file, FortIO.WRITE_MODE) kw.fwrite(fortio) fortio.close() self.assertFilesAreEqual(clean_dump, test_file) ecl_file = EclFile(test_file, flags=EclFileFlagEnum.ECL_FILE_WRITABLE) loaded_kw = ecl_file["MY_KEY"][0] self.assertTrue(kw.equal(loaded_kw)) ecl_file.save_kw(loaded_kw) ecl_file.close() self.assertFilesAreEqual(clean_dump, test_file) ecl_file = EclFile(test_file) loaded_kw = ecl_file["MY_KEY"][0] self.assertTrue(kw.equal(loaded_kw))
def test_restart_days(self): rst_file = EclFile(self.test_file) self.assertAlmostEqual(0.0, rst_file.iget_restart_sim_days(0)) self.assertAlmostEqual(31.0, rst_file.iget_restart_sim_days(1)) self.assertAlmostEqual(274.0, rst_file.iget_restart_sim_days(10)) with self.assertRaises(KeyError): rst_file.restart_get_kw("Missing", dtime=datetime.date(2004, 1, 1)) with self.assertRaises(IndexError): rst_file.restart_get_kw("SWAT", dtime=datetime.date(1985, 1, 1))
def test_EclFile_name_property(self): with TestAreaContext("name") as t: kw = EclKW("TEST", 3, EclDataType.ECL_INT) with openFortIO("TEST", mode=FortIO.WRITE_MODE) as f: kw.fwrite(f) f = EclFile("TEST")
def rstdates(eclfiles): """Return a list of datetime objects for the available dates in the RST file""" report_indices = EclFile.file_report_list(eclfiles.get_rstfilename()) return [ eclfiles.get_rstfile().iget_restart_sim_time(index).date() for index in range(0, len(report_indices)) ]
def test_truncate(self): kw1 = EclKW("KW1", 2, EclDataType.ECL_INT) kw2 = EclKW("KW2", 2, EclDataType.ECL_INT) kw1[0] = 99 kw1[1] = 77 kw2[0] = 113 kw2[1] = 335 with TestAreaContext("python/fortio/ftruncate") as t: with openFortIO("file", mode=FortIO.WRITE_MODE) as f: kw1.fwrite(f) pos1 = f.getPosition() kw2.fwrite(f) t.sync() # Truncate file in read mode; should fail hard. with openFortIO("file") as f: with self.assertRaises(IOError): f.truncate() with openFortIO("file", mode=FortIO.READ_AND_WRITE_MODE) as f: f.seek(pos1) f.truncate() f = EclFile("file") self.assertEqual(len(f), 1) kw1_ = f[0] self.assertEqual(kw1, kw1_)
def loadKeywords( name ): kw_list = [] f = EclFile( name ) for kw in f: kw_list.append( kw ) return kw_list
def __init__(self , grid , filename , flags = 0): """Will open an Eclipse restart file. The EclRestartFile class will open an eclipse restart file, in unified or non unified format. The constructor will infer the file type based on the filename, and will raise a ValueError exception if the file type is not ECL_RESTART_FILE or ECL_UNIFIED_RESTART_FILE. The EclRestartFile will use a grid reference to create Ecl3DKw instances for all the keyword elements which have either 'nactive' or 'nx*ny*nz' elements. """ file_type , report_step , fmt_file = EclFile.getFileType( filename ) if not file_type in [EclFileEnum.ECL_RESTART_FILE, EclFileEnum.ECL_UNIFIED_RESTART_FILE]: raise ValueError('The input filename "%s" does not correspond to a restart file. Please follow the Eclipse naming conventions' % filename) super(EclRestartFile , self).__init__( grid, filename , flags) self.rst_headers = None if file_type == EclFileEnum.ECL_RESTART_FILE: self.is_unified = False self.report_step = report_step else: self.is_unified = True
def extract_ots_context(configuration): if configuration.eclbase is not None: rstfile_path = "{}.UNRST".format(configuration.eclbase) rstfile = EclFile(rstfile_path) dates = [d.date() for d in rstfile.dates] return dates return []
def __init__( self, input_case: Union[Path, str], layers: Tuple = (), ): super().__init__() self._input_case: Path = Path(input_case) self._eclsum = EclSum(str(self._input_case)) self._init = EclFile(str(self._input_case.with_suffix(".INIT"))) self._grid = EclGrid(str(self._input_case.with_suffix(".EGRID"))) self._restart = EclFile(str(self._input_case.with_suffix(".UNRST"))) self._init = EclInitFile(self._grid, str(self._input_case.with_suffix(".INIT"))) self._wells = compdat.df(EclFiles(str(self._input_case))) self._layers = layers
def __init__(self, grid, filename, flags=0): file_type, report_step, fmt_file = EclFile.getFileType(filename) if file_type == EclFileEnum.ECL_INIT_FILE: super(EclInitFile, self).__init__(grid, filename, flags) else: err = 'The input filename "%s" does not correspond to an init file.' err += ' Please follow the Eclipse naming conventions.' raise ValueError(err % filename)
def __init__( self, input_case: Union[Path, str], perforation_handling_strategy: str = "bottom_point", ): super().__init__() self._input_case: Path = Path(input_case) self._eclsum = EclSum(str(self._input_case)) self._init = EclFile(str(self._input_case.with_suffix(".INIT"))) self._grid = EclGrid(str(self._input_case.with_suffix(".EGRID"))) self._restart = EclFile(str(self._input_case.with_suffix(".UNRST"))) self._init = EclInitFile(self._grid, str(self._input_case.with_suffix(".INIT"))) self._wells = compdat.df(EclFiles(str(self._input_case))) self._perforation_handling_strategy: str = perforation_handling_strategy
def test_broken_file(self): with TestAreaContext("test_broken_file"): with open("CASE.FINIT", "w") as f: f.write("This - is not a ECLISPE file\nsdlcblhcdbjlwhc\naschscbasjhcasc\nascasck c s s aiasic asc") with self.assertRaises(IOError): f = EclFile("CASE.FINIT")
def test_ecl_file_indexed_read(self): with TestAreaContext("ecl_file_indexed_read") as area: fortio = FortIO("ecl_file_index_test", mode=FortIO.WRITE_MODE) element_count = 100000 ecl_kw_1 = EclKW("TEST1", element_count, EclDataType.ECL_INT) ecl_kw_2 = EclKW("TEST2", element_count, EclDataType.ECL_INT) for index in range(element_count): ecl_kw_1[index] = index ecl_kw_2[index] = index + 3 ecl_kw_1.fwrite(fortio) ecl_kw_2.fwrite(fortio) fortio.close() ecl_file = EclFile("ecl_file_index_test") index_map = IntVector() index_map.append(2) index_map.append(3) index_map.append(5) index_map.append(7) index_map.append(11) index_map.append(13) index_map.append(313) index_map.append(1867) index_map.append(5227) index_map.append(7159) index_map.append(12689) index_map.append(18719) index_map.append(32321) index_map.append(37879) index_map.append(54167) index_map.append(77213) index_map.append(88843) index_map.append(99991) char_buffer_1 = ctypes.create_string_buffer( len(index_map) * ctypes.sizeof(ctypes.c_int)) char_buffer_2 = ctypes.create_string_buffer( len(index_map) * ctypes.sizeof(ctypes.c_int)) self._eclFileIndexedRead(ecl_file, "TEST2", 0, index_map, char_buffer_2) self._eclFileIndexedRead(ecl_file, "TEST1", 0, index_map, char_buffer_1) int_buffer_1 = ctypes.cast(char_buffer_1, ctypes.POINTER(ctypes.c_int)) int_buffer_2 = ctypes.cast(char_buffer_2, ctypes.POINTER(ctypes.c_int)) for index, index_map_value in enumerate(index_map): self.assertEqual(index_map_value, int_buffer_1[index]) self.assertEqual(index_map_value, int_buffer_2[index] - 3)
def test_fortio_size( self ): unrst_file_path = self.createTestPath("Equinor/ECLIPSE/Gurbat/ECLIPSE.UNRST") unrst_file = EclFile(unrst_file_path) size = 0 for kw in unrst_file: size += kw.fortIOSize() stat = os.stat(unrst_file_path) self.assertTrue(size == stat.st_size)
def test_fmu_stat_workflow(self): N = 100 global_size = 100 active_size = 50 with TestAreaContext("FMU_FILES"): for i in range(N): permx = EclKW("PERMX", active_size, EclDataType.ECL_FLOAT) poro = EclKW("PORO", active_size, EclDataType.ECL_FLOAT) porv = EclKW("PORV", global_size, EclDataType.ECL_FLOAT) porv.assign(0) for g in random.sample( range(global_size), active_size): porv[g] = 1 permx.assign(random.random()) poro.assign(random.random()) with openFortIO("TEST%d.INIT" % i, FortIO.WRITE_MODE) as f: permx.fwrite(f) poro.fwrite(f) porv.fwrite(f) mean_permx = EclKW("PERMX", global_size, EclDataType.ECL_FLOAT) std_permx = EclKW("PERMX", global_size, EclDataType.ECL_FLOAT) mean_poro = EclKW("PORO", global_size, EclDataType.ECL_FLOAT) std_poro = EclKW("PORO", global_size, EclDataType.ECL_FLOAT) count = EclKW("COUNT", global_size, EclDataType.ECL_INT) for i in range(N): f = EclFile("TEST%d.INIT" % i) porv = f["PORV"][0] permx = f["PERMX"][0] poro = f["PORO"][0] actnum = porv.create_actnum() global_permx = permx.scatter_copy( actnum ) mean_permx += global_permx std_permx.add_squared( global_permx) global_poro = poro.scatter_copy( actnum ) mean_poro += global_poro std_poro.add_squared( global_poro) count += actnum mean_permx.safe_div(count) std_permx.safe_div(count) std_permx -= mean_permx * mean_permx std_permx.isqrt() mean_poro.safe_div(count) std_poro.safe_div(count) std_poro -= mean_poro * mean_poro std_poro.isqrt()
def test_date(self): f = EclFile(self.u_file) self.assertTrue(f.has_sim_time(datetime.datetime(2001, 6, 1))) self.assertFalse(f.has_sim_time(datetime.datetime(2005, 6, 1))) self.assertFalse(f.has_sim_time(datetime.datetime(1999, 6, 1))) self.assertFalse(f.has_sim_time(datetime.datetime(2001, 6, 11))) self.assertTrue(EclFile.contains_sim_time(self.u_file, datetime.datetime(2001, 6, 1))) self.assertFalse(EclFile.contains_sim_time(self.u_file, datetime.datetime(2005, 6, 1))) self.assertFalse(EclFile.contains_sim_time(self.u_file, datetime.datetime(1999, 6, 1))) self.assertFalse(EclFile.contains_sim_time(self.u_file, datetime.datetime(2001, 6, 11)))
def test_geertsma_rporv_kernel_2_source_points_2_vintages(self): grid = EclGrid.createRectangular(dims=(2, 1, 1), dV=(100, 100, 100)) with TestAreaContext("Subsidence"): p1 = [1, 10] p2 = [10, 20] create_restart(grid, "TEST", p1, p2, rporv1=[10**5, 10**5], rporv2=[9 * 10**4, 9 * 10**4]) create_init(grid, "TEST") init = EclFile("TEST.INIT") restart_file = EclFile("TEST.UNRST") restart_view1 = restart_file.restartView( sim_time=datetime.date(2000, 1, 1)) restart_view2 = restart_file.restartView( sim_time=datetime.date(2010, 1, 1)) subsidence = EclSubsidence(grid, init) subsidence.add_survey_PRESSURE("S1", restart_view1) subsidence.add_survey_PRESSURE("S2", restart_view2) youngs_modulus = 5E8 poisson_ratio = 0.3 seabed = 0 receiver = (1000, 1000, 0) dz1 = subsidence.eval_geertsma_rporv("S1", None, receiver, youngs_modulus, poisson_ratio, seabed) dz2 = subsidence.eval_geertsma_rporv("S2", None, receiver, youngs_modulus, poisson_ratio, seabed) dz = subsidence.eval_geertsma_rporv("S1", "S2", receiver, youngs_modulus, poisson_ratio, seabed) np.testing.assert_almost_equal(dz, dz1 - dz2) self.assertTrue(dz > 0)
def get_rftfile(self): """Find and return the RFT file as an EclFile object""" if not self._rftfile: rftfilename = self._eclbase + ".RFT" if not os.path.exists(rftfilename): raise FileNotFoundError(errno.ENOENT, os.strerror(errno.ENOENT), rftfilename) logger.info("Opening RFT file: %s", rftfilename) self._rftfile = EclFile(rftfilename) return self._rftfile
def get_rstfile(self) -> EclFile: """Find and return the UNRST file as an EclFile object""" if not self._rstfile: rstfilename = self._eclbase + ".UNRST" if not Path(rstfilename).is_file(): raise FileNotFoundError(errno.ENOENT, os.strerror(errno.ENOENT), rstfilename) logger.info("Opening RST file: %s", rstfilename) self._rstfile = EclFile(rstfilename) return self._rstfile
def test_restart_view(self): f = EclFile(self.test_file) with self.assertRaises(ValueError): v = f.restartView() v = f.restartView(sim_days=274) v = f.restartView(sim_time=datetime.date(2004, 1, 1)) v = f.restartView(report_step=30) v = f.restartView(seqnum_index=30)
def test_restart_days(self): rst_file = EclFile( self.test_file ) self.assertAlmostEqual( 0.0 , rst_file.iget_restart_sim_days(0) ) self.assertAlmostEqual( 31.0 , rst_file.iget_restart_sim_days(1) ) self.assertAlmostEqual( 274.0 , rst_file.iget_restart_sim_days(10) ) with self.assertRaises(KeyError): rst_file.restart_get_kw("Missing" , dtime = datetime.date( 2004,1,1)) with self.assertRaises(IndexError): rst_file.restart_get_kw("SWAT" , dtime = datetime.date( 1985 , 1 , 1))
def test_restart_view(self): f = EclFile( self.test_file ) with self.assertRaises(ValueError): v = f.restartView( ) v = f.restartView( sim_days = 274 ) v = f.restartView( sim_time = datetime.date( 2004,1,1) ) v = f.restartView( report_step = 30 ) v = f.restartView( seqnum_index = 30 )
def test_kw_write(self): with TestAreaContext("python/ecl_kw/writing"): data = [random.random() for i in range(10000)] kw = EclKW("TEST", len(data), EclDataType.ECL_DOUBLE) i = 0 for d in data: kw[i] = d i += 1 pfx = 'EclKW(' self.assertEqual(pfx, repr(kw)[:len(pfx)]) fortio = FortIO("ECL_KW_TEST", FortIO.WRITE_MODE) kw.fwrite(fortio) fortio.close() fortio = FortIO("ECL_KW_TEST") kw2 = EclKW.fread(fortio) self.assertTrue(kw.equal(kw2)) ecl_file = EclFile("ECL_KW_TEST", flags=EclFileFlagEnum.ECL_FILE_WRITABLE) kw3 = ecl_file["TEST"][0] self.assertTrue(kw.equal(kw3)) ecl_file.save_kw(kw3) ecl_file.close() fortio = FortIO("ECL_KW_TEST", FortIO.READ_AND_WRITE_MODE) kw4 = EclKW.fread(fortio) self.assertTrue(kw.equal(kw4)) fortio.seek(0) kw4.fwrite(fortio) fortio.close() ecl_file = EclFile("ECL_KW_TEST") kw5 = ecl_file["TEST"][0] self.assertTrue(kw.equal(kw5))
def test_ecl_file_block(self): with TestAreaContext("name") as t: kw = EclKW("TEST", 3, EclDataType.ECL_INT) with openFortIO("TEST" , mode = FortIO.WRITE_MODE) as f: kw.fwrite( f ) t.sync() f = EclFile( "TEST" ) with self.assertRaises(NotImplementedError): f.select_block( "KW" , 100 ) with self.assertRaises(NotImplementedError): f.select_global( ) with self.assertRaises(NotImplementedError): f.select_restart_section( index = None , report_step = None , sim_time = None) with self.assertRaises(NotImplementedError): f.select_restart_section( ) with self.assertRaises(NotImplementedError): EclFile.restart_block( "TEST" )
def test_iget_named(self): f = EclFile(self.test_file) N = f.num_named_kw( "SWAT" ) with self.assertRaises(IndexError): s = f.iget_named_kw( "SWAT" , N + 1)
def test_name(self): f = EclFile(self.u_file) self.assertTrue(f.getFilename() == self.u_file) f = EclFile(self.xfile0) self.assertTrue(f.getFilename() == self.xfile0)
def test_save(self): #work_area = TestArea("python/ecl_file/save") with TestAreaContext("python/ecl_file/save", store_area=False) as work_area: work_area.copy_file(self.test_file) rst_file = EclFile("ECLIPSE.UNRST", flags=EclFileFlagEnum.ECL_FILE_WRITABLE) swat0 = rst_file["SWAT"][0] swat0.assign(0.75) rst_file.save_kw(swat0) rst_file.close() self.assertFilesAreNotEqual("ECLIPSE.UNRST",self.test_file) rst_file1 = EclFile(self.test_file) rst_file2 = EclFile("ECLIPSE.UNRST", flags=EclFileFlagEnum.ECL_FILE_WRITABLE) swat1 = rst_file1["SWAT"][0] swat2 = rst_file2["SWAT"][0] swat2.assign(swat1) rst_file2.save_kw(swat2) self.assertTrue(swat1.equal(swat2)) rst_file1.close() rst_file2.close() # Random failure .... self.assertFilesAreEqual("ECLIPSE.UNRST", self.test_file)
def test_block_view(self): with TestAreaContext("python/ecl_file/view"): with openFortIO("TEST" , mode = FortIO.WRITE_MODE) as f: for i in range(5): header = EclKW("HEADER" , 1 , EclDataType.ECL_INT ) header[0] = i data1 = EclKW("DATA1" , 100 , EclDataType.ECL_INT ) data1.assign( i ) data2 = EclKW("DATA2" , 100 , EclDataType.ECL_INT ) data2.assign( i*10 ) header.fwrite( f ) data1.fwrite( f ) data2.fwrite( f ) ecl_file = EclFile("TEST") pfx = 'EclFile(' self.assertEqual(pfx, repr(ecl_file)[:len(pfx)]) with self.assertRaises(KeyError): ecl_file.blockView("NO" , 1) with self.assertRaises(IndexError): ecl_file.blockView("HEADER" , 100) with self.assertRaises(IndexError): ecl_file.blockView("HEADER" , 1000) bv = ecl_file.blockView("HEADER" , -1) for i in range(5): view = ecl_file.blockView("HEADER" , i) self.assertEqual( len(view) , 3) header = view["HEADER"][0] data1 = view["DATA1"][0] data2 = view["DATA2"][0] self.assertEqual( header[0] , i ) self.assertEqual( data1[99] , i ) self.assertEqual( data2[99] , i*10 ) for i in range(5): view = ecl_file.blockView2("HEADER" , "DATA2", i ) self.assertEqual( len(view) , 2) header = view["HEADER"][0] data1 = view["DATA1"][0] self.assertEqual( header[0] , i ) self.assertEqual( data1[99] , i ) self.assertFalse( "DATA2" in view ) view = ecl_file.blockView2("HEADER" , None, 0 ) self.assertEqual( len(view) , len(ecl_file)) view = ecl_file.blockView2(None , "DATA2", 0 )
def test_ecl_index(self): with TestAreaContext("python/ecl_file/context"): kw1 = EclKW( "KW1" , 100 , EclDataType.ECL_INT) kw2 = EclKW( "KW2" , 100 , EclDataType.ECL_FLOAT) kw3 = EclKW( "KW3" , 100 , EclDataType.ECL_CHAR) kw4 = EclKW( "KW4" , 100 , EclDataType.ECL_STRING(23)) with openFortIO("TEST" , mode = FortIO.WRITE_MODE) as f: kw1.fwrite( f ) kw2.fwrite( f ) kw3.fwrite( f ) kw4.fwrite( f ) ecl_file = EclFile("TEST") ecl_file.write_index("INDEX_FILE") ecl_file.close() ecl_file_index = EclFile("TEST", 0, "INDEX_FILE") for kw in ["KW1","KW2","KW3","KW4"]: self.assertIn( kw , ecl_file_index ) with self.assertRaises(IOError): ecl_file.write_index("does-not-exist/INDEX") os.mkdir("read-only") os.chmod("read-only", 0o444) with self.assertRaises(IOError): ecl_file.write_index("read-only/INDEX") with self.assertRaises(IOError): ecl_file_index = EclFile("TEST", 0, "index_does_not_exist") shutil.copyfile( "INDEX_FILE" , "INDEX_perm_denied") os.chmod("INDEX_perm_denied", 0o000) with self.assertRaises(IOError): ecl_file_index = EclFile("TEST", 0, "INDEX_perm_denied") os.mkdir("path") shutil.copyfile("TEST" , "path/TEST") ecl_file = EclFile("path/TEST") ecl_file.write_index("path/index") with CWDContext("path"): ecl_file = EclFile("TEST" , 0 , "index")
def assertFileType(self , filename , expected): file_type , step , fmt_file = EclFile.getFileType(filename) self.assertEqual( file_type , expected[0] ) self.assertEqual( fmt_file , expected[1] ) self.assertEqual( step , expected[2] )