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_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 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 test_context(self): with TestAreaContext("python/fortio/context") as t: kw1 = EclKW("KW", 2456, EclDataType.ECL_FLOAT) for i in range(len(kw1)): kw1[i] = randint(0, 1000) with openFortIO("file", mode=FortIO.WRITE_MODE) as f: kw1.fwrite(f) self.assertEqual(f.filename(), "file") with openFortIO("file") as f: kw2 = EclKW.fread(f) self.assertTrue(kw1 == kw2)
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 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") with self.assertRaises(IOError): ecl_file_index = EclFile("TEST", 0, "index_does_not_exist") 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 test_string_write_read_formatted(self): for str_len in range(1000): with TestAreaContext("my_space"): kw = EclKW("TEST_KW", 10, EclDataType.ECL_STRING(str_len)) for i in range(10): kw[i] = str(i)*str_len file_name = "ecl_kw_test" with openFortIO(file_name, mode=FortIO.WRITE_MODE, fmt_file=True) as fortio: kw.fwrite(fortio) with openFortIO(file_name, fmt_file=True) as fortio: loaded_kw = EclKW.fread(fortio) self.assertEqual(kw, loaded_kw)
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_invalid(self): case = create_case() with TestAreaContext("sum_invalid"): case.fwrite( ) with open("CASE.txt", "w") as f: f.write("No - this is not EclKW file ....") with self.assertRaises( IOError ): case2 = EclSum.load( "CSV.SMSPEC" , "CASE.txt" ) with self.assertRaises( IOError ): case2 = EclSum.load( "CASE.txt" , "CSV.UNSMRY" ) kw1 = EclKW("TEST1", 30, EclDataType.ECL_INT) kw2 = EclKW("TEST2", 30, EclDataType.ECL_INT) with openFortIO( "CASE.KW" , FortIO.WRITE_MODE) as f: kw1.fwrite( f ) kw2.fwrite( f ) with self.assertRaises( IOError ): case2 = EclSum.load( "CSV.SMSPEC" , "CASE.KW") with self.assertRaises( IOError ): case2 = EclSum.load( "CASE.KW" , "CSV.UNSMRY" )
def test_context(self): with TestAreaContext("python/fortio/context") as t: kw1 = EclKW("KW" , 2456 , EclDataType.ECL_FLOAT) for i in range(len(kw1)): kw1[i] = randint(0,1000) with openFortIO("file" , mode = FortIO.WRITE_MODE) as f: kw1.fwrite( f ) self.assertEqual( f.filename() , "file") t.sync( ) with openFortIO("file") as f: kw2 = EclKW.fread( f ) self.assertTrue( kw1 == kw2 )
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_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 ) t.sync() f = EclFile( "TEST" )
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_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_is_fortran_file(self): with TestAreaContext("python/fortio/guess"): kw1 = EclKW("KW" , 12345 , EclDataType.ECL_FLOAT) with openFortIO("fortran_file" , mode = FortIO.WRITE_MODE) as f: kw1.fwrite( f ) with cwrap.open("text_file" , "w") as f: kw1.write_grdecl( f ) self.assertTrue( FortIO.isFortranFile( "fortran_file" )) self.assertFalse( FortIO.isFortranFile( "text_file" ))
def test_create(self): # The init file created here only contains a PORO field. More # properties must be added to this before it can be used for # any usefull gravity calculations. poro = EclKW( "PORO" , self.grid.getGlobalSize() , EclDataType.ECL_FLOAT ) with TestAreaContext("grav_init"): with openFortIO( "TEST.INIT" , mode = FortIO.WRITE_MODE ) as f: poro.fwrite( f ) self.init = EclFile( "TEST.INIT") grav = EclGrav( self.grid , self.init )
def test_create(self): # The init file created here only contains a PORO field. More # properties must be added to this before it can be used for # any usefull gravity calculations. poro = EclKW("PORO", self.grid.getGlobalSize(), EclDataType.ECL_FLOAT) with TestAreaContext("grav_init"): with openFortIO("TEST.INIT", mode=FortIO.WRITE_MODE) as f: poro.fwrite(f) self.init = EclFile("TEST.INIT") grav = EclGrav(self.grid, self.init)
def test_is_fortran_file(self): with TestAreaContext("python/fortio/guess"): kw1 = EclKW("KW", 12345, EclDataType.ECL_FLOAT) with openFortIO("fortran_file", mode=FortIO.WRITE_MODE) as f: kw1.fwrite(f) with cwrap.open("text_file", "w") as f: kw1.write_grdecl(f) self.assertTrue(FortIO.isFortranFile("fortran_file")) self.assertFalse(FortIO.isFortranFile("text_file"))
def test_context( self ): with TestAreaContext("python/ecl_file/context"): kw1 = EclKW( "KW1" , 100 , EclDataType.ECL_INT) kw2 = EclKW( "KW2" , 100 , EclDataType.ECL_INT) with openFortIO("TEST" , mode = FortIO.WRITE_MODE) as f: kw1.fwrite( f ) kw2.fwrite( f ) with openEclFile("TEST") as ecl_file: self.assertEqual( len(ecl_file) , 2 ) self.assertTrue( ecl_file.has_kw("KW1")) self.assertTrue( ecl_file.has_kw("KW2")) self.assertEqual(ecl_file[1], ecl_file[-1])
def test_fmt(self): kw1 = EclKW("NAME1", 100, EclDataType.ECL_INT) kw2 = EclKW("NAME2", 100, EclDataType.ECL_INT) for i in range(len(kw1)): kw1[i] = i + 1 kw2[i] = len(kw1) - kw1[i] with TestAreaContext("ecl_kw/fmt") as ta: with openFortIO("TEST.FINIT", FortIO.WRITE_MODE, fmt_file=True) as f: kw1.fwrite(f) kw2.fwrite(f) with openFortIO("TEST.FINIT", fmt_file=True) as f: kw1b = EclKW.fread(f) kw2b = EclKW.fread(f) self.assertTrue(kw1 == kw1b) self.assertTrue(kw2 == kw2b) f = EclFile("TEST.FINIT") self.assertTrue(kw1 == f[0]) self.assertTrue(kw2 == f[1])
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 test_missing_smspec_keyword(self): with TestAreaContext("EclSum/truncated_data") as ta: ta.copy_file(self.test_file) ta.copy_file(self.createTestPath("Statoil/ECLIPSE/Gurbat/ECLIPSE.UNSMRY")) with openEclFile("ECLIPSE.SMSPEC") as f: kw_list = [] for kw in f: kw_list.append(EclKW.copy(kw)) with openFortIO("ECLIPSE.SMSPEC", mode=FortIO.WRITE_MODE) as f: for kw in kw_list: if kw.getName() == "KEYWORDS": continue kw.fwrite(f) with self.assertRaises(IOError): EclSum("ECLIPSE")
def test_missing_smspec_keyword(self): with TestAreaContext("EclSum/truncated_data") as ta: ta.copy_file(self.test_file) ta.copy_file(self.createTestPath("Equinor/ECLIPSE/Gurbat/ECLIPSE.UNSMRY")) with openEclFile("ECLIPSE.SMSPEC") as f: kw_list = [] for kw in f: kw_list.append(EclKW.copy(kw)) with openFortIO("ECLIPSE.SMSPEC", mode=FortIO.WRITE_MODE) as f: for kw in kw_list: if kw.getName() == "KEYWORDS": continue kw.fwrite(f) with self.assertRaises(IOError): EclSum("ECLIPSE")
def test_missing_unsmry_keyword(self): with TestAreaContext("EclSum/truncated_data") as ta: ta.copy_file(self.test_file) ta.copy_file( self.createTestPath("Statoil/ECLIPSE/Gurbat/ECLIPSE.UNSMRY")) with openEclFile("ECLIPSE.UNSMRY") as f: kw_list = [] for kw in f: kw_list.append(EclKW.copy(kw)) with openFortIO("ECLIPSE.UNSMRY", mode=FortIO.WRITE_MODE) as f: c = 0 for kw in kw_list: if kw.getName() == "PARAMS": if c % 5 == 0: continue c += 1 kw.fwrite(f) with self.assertRaises(IOError): EclSum("ECLIPSE")
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_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 createFile( name , kw_list ): with openFortIO(name , mode = FortIO.WRITE_MODE) as f: for kw in kw_list: kw.fwrite( f )
case = create_case() with TestAreaContext("sum_invalid"): case.fwrite() with open("CASE.txt", "w") as f: f.write("No - this is not EclKW file ....") with self.assertRaises(IOError): case2 = EclSum.load("CSV.SMSPEC", "CASE.txt") with self.assertRaises(IOError): case2 = EclSum.load("CASE.txt", "CSV.UNSMRY") kw1 = EclKW("TEST1", 30, EclDataType.ECL_INT) kw2 = EclKW("TEST2", 30, EclDataType.ECL_INT) with openFortIO("CASE.KW", FortIO.WRITE_MODE) as f: kw1.fwrite(f) kw2.fwrite(f) with self.assertRaises(IOError): case2 = EclSum.load("CSV.SMSPEC", "CASE.KW") with self.assertRaises(IOError): case2 = EclSum.load("CASE.KW", "CSV.UNSMRY") def test_kw_vector(self): case1 = create_case() case2 = createEclSum("CSV", [("FOPR", None, 0, "SM3/DAY"), ("FOPT", None, 0, "SM3"), ("FWPT", None, 0, "SM3")], sim_length_days=100,
with TestAreaContext("sum_invalid"): case.fwrite() with open("CASE.txt", "w") as f: f.write("No - this is not EclKW file ....") with self.assertRaises(IOError): case2 = EclSum.load("CSV.SMSPEC", "CASE.txt") with self.assertRaises(IOError): case2 = EclSum.load("CASE.txt", "CSV.UNSMRY") kw1 = EclKW("TEST1", 30, EclDataType.ECL_INT) kw2 = EclKW("TEST2", 30, EclDataType.ECL_INT) with openFortIO("CASE.KW", FortIO.WRITE_MODE) as f: kw1.fwrite(f) kw2.fwrite(f) with self.assertRaises(IOError): case2 = EclSum.load("CSV.SMSPEC", "CASE.KW") with self.assertRaises(IOError): case2 = EclSum.load("CASE.KW", "CSV.UNSMRY") def test_kw_vector(self): case1 = createEclSum("CSV", [("FOPT", None, 0), ("FOPR", None, 0), ("FGPT", None, 0)], sim_length_days=100, num_report_step=10, num_mini_step=10,