def test_truncate(self): kw1 = EclKW("KW1", 2, EclTypeEnum.ECL_INT_TYPE) kw2 = EclKW("KW2", 2, EclTypeEnum.ECL_INT_TYPE) 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"): kw1 = EclKW.create("KW" , 2456 , EclTypeEnum.ECL_FLOAT_TYPE) for i in range(len(kw1)): kw1[i] = randint(0,1000) with openFortIO("file" , mode = FortIO.WRITE_MODE) as f: kw1.fwrite( f ) 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_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_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 create_restart(grid, case, p1, p2=None): with openFortIO("%s.UNRST" % case, mode=FortIO.WRITE_MODE) as f: seq_hdr = EclKW("SEQNUM", 1, EclTypeEnum.ECL_FLOAT_TYPE) seq_hdr[0] = 10 p = EclKW("PRESSURE", grid.getNumActive(), EclTypeEnum.ECL_FLOAT_TYPE) for i in range(len(p1)): p[i] = p1[i] header = EclKW("INTEHEAD", 67, EclTypeEnum.ECL_INT_TYPE) header[64] = 1 header[65] = 1 header[66] = 2000 seq_hdr.fwrite(f) header.fwrite(f) p.fwrite(f) if p2: seq_hdr[0] = 20 header[66] = 2010 for i in range(len(p2)): p[i] = p2[i] seq_hdr.fwrite(f) header.fwrite(f) p.fwrite(f)
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_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_EclFile_name_property(self): with TestAreaContext("name"): kw = EclKW("TEST", 3, EclTypeEnum.ECL_INT_TYPE) with openFortIO("TEST" , mode = FortIO.WRITE_MODE) as f: kw.fwrite( f ) f = EclFile( "TEST" ) with self.assertRaises(DeprecationWarning): name = f.name
def test_EclFile_name_property(self): with TestAreaContext("name"): kw = EclKW.new("TEST", 3, EclTypeEnum.ECL_INT_TYPE) with openFortIO("TEST" , mode = FortIO.WRITE_MODE) as f: kw.fwrite( f ) f = EclFile( "TEST" ) with warnings.catch_warnings(): name = f.name
def create_init(grid, case): poro = EclKW("PORO", grid.getNumActive(), EclTypeEnum.ECL_FLOAT_TYPE) porv = poro.copy() porv.setName("PORV") for g in range(grid.getGlobalSize()): porv[g] *= grid.cell_volume(global_index=g) with openFortIO("%s.INIT" % case, mode=FortIO.WRITE_MODE) as f: poro.fwrite(f) porv.fwrite(f)
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() , EclTypeEnum.ECL_FLOAT_TYPE ) 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 open("text_file" , "w") as f: kw1.write_grdecl( f ) self.assertTrue( FortIO.isFortranFile( "fortran_file" )) self.assertFalse( FortIO.isFortranFile( "text_file" ))
def test_is_fortran_file(self): with TestAreaContext("python/fortio/guess"): kw1 = EclKW.create("KW" , 12345 , EclTypeEnum.ECL_FLOAT_TYPE) with openFortIO("fortran_file" , mode = FortIO.WRITE_MODE) as f: kw1.fwrite( f ) with 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.create( "KW1" , 100 , EclTypeEnum.ECL_INT_TYPE) kw2 = EclKW.create( "KW2" , 100 , EclTypeEnum.ECL_INT_TYPE) 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"))
def test_context(self): with TestAreaContext("python/ecl_file/context"): kw1 = EclKW("KW1", 100, EclTypeEnum.ECL_INT_TYPE) kw2 = EclKW("KW2", 100, EclTypeEnum.ECL_INT_TYPE) 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"))
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 , EclTypeEnum.ECL_INT_TYPE ) header[0] = i data1 = EclKW("DATA1" , 100 , EclTypeEnum.ECL_INT_TYPE ) data1.assign( i ) data2 = EclKW("DATA2" , 100 , EclTypeEnum.ECL_INT_TYPE ) data2.assign( i*10 ) header.fwrite( f ) data1.fwrite( f ) data2.fwrite( f ) ecl_file = EclFile("TEST") with self.assertRaises(KeyError): ecl_file.blockView("NO" , 1) with self.assertRaises(IndexError): ecl_file.blockView("HEADER" , 100) 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_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, EclTypeEnum.ECL_INT_TYPE) header[0] = i data1 = EclKW("DATA1", 100, EclTypeEnum.ECL_INT_TYPE) data1.assign(i) data2 = EclKW("DATA2", 100, EclTypeEnum.ECL_INT_TYPE) data2.assign(i * 10) header.fwrite(f) data1.fwrite(f) data2.fwrite(f) ecl_file = EclFile("TEST") with self.assertRaises(KeyError): ecl_file.blockView("NO", 1) with self.assertRaises(IndexError): ecl_file.blockView("HEADER", 100) 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_fmt(self): kw1 = EclKW( "NAME1" , 100 , EclTypeEnum.ECL_INT_TYPE) kw2 = EclKW( "NAME2" , 100 , EclTypeEnum.ECL_INT_TYPE) 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_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_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_ecl_file_block(self): with TestAreaContext("name") as t: kw = EclKW("TEST", 3, EclTypeEnum.ECL_INT_TYPE) 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 createFile( name , kw_list ): with openFortIO(name , mode = FortIO.WRITE_MODE) as f: for kw in kw_list: kw.fwrite( f )
def createFile(name, kw_list): with openFortIO(name, mode=FortIO.WRITE_MODE) as f: for kw in kw_list: kw.fwrite(f)