def test_truncate(self): kw1 = EclKW.create("KW1", 2, EclTypeEnum.ECL_INT_TYPE) kw2 = EclKW.create("KW2", 2, EclTypeEnum.ECL_INT_TYPE) kw1[0] = 99 kw1[1] = 77 kw2[0] = 113 kw2[1] = 335 with TestAreaContext("python/fortio/ftruncate"): with openFortIO("file" , mode = FortIO.WRITE_MODE) as f: kw1.fwrite(f) pos1 = f.getPosition( ) kw2.fwrite(f) # 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_long_name(self): with self.assertRaises(ValueError): EclKW.create("LONGLONGNAME", 10, EclTypeEnum.ECL_INT_TYPE) kw = EclKW.create("REGIONS", 10, EclTypeEnum.ECL_INT_TYPE) with self.assertRaises(ValueError): kw.set_name("LONGLONGNAME")
def test_long_name(self): with self.assertRaises(ValueError): EclKW("LONGLONGNAME" , 10 , EclDataType.ECL_INT) kw = EclKW("REGIONS" , 10 , EclDataType.ECL_INT) with self.assertRaises(ValueError): kw.setName("LONGLONGNAME")
def test_sum(self): kw_string = EclKW.create("STRING", 100, EclTypeEnum.ECL_CHAR_TYPE) with self.assertRaises(ValueError): kw_string.sum() kw_int = EclKW.create("INT", 4, EclTypeEnum.ECL_INT_TYPE) kw_int[0] = 1 kw_int[1] = 2 kw_int[2] = 3 kw_int[3] = 4 self.assertEqual(kw_int.sum(), 10) kw_d = EclKW.create("D", 4, EclTypeEnum.ECL_DOUBLE_TYPE) kw_d[0] = 1 kw_d[1] = 2 kw_d[2] = 3 kw_d[3] = 4 self.assertEqual(kw_d.sum(), 10) kw_f = EclKW.create("F", 4, EclTypeEnum.ECL_FLOAT_TYPE) kw_f[0] = 1 kw_f[1] = 2 kw_f[2] = 3 kw_f[3] = 4 self.assertEqual(kw_f.sum(), 10) kw_b = EclKW.create("F", 4, EclTypeEnum.ECL_BOOL_TYPE) kw_b[0] = False kw_b[1] = True kw_b[2] = False kw_b[3] = True self.assertEqual(kw_b.sum(), 2)
def test_cast(self): actnum = IntVector(default_value=1, initial_size=1000) for i in range(100): actnum[i] = 0 grid = EclGrid.createRectangular((10, 10, 10), (1, 1, 1), actnum=actnum) kw_wrong_size = EclKW("KW", 27, EclDataType.ECL_FLOAT) kw_global_size = EclKW("KW", grid.getGlobalSize(), EclDataType.ECL_FLOAT) kw_active_size = EclKW("KW", grid.getNumActive(), EclDataType.ECL_FLOAT) with self.assertRaises(ValueError): Ecl3DKW.castFromKW(kw_wrong_size, grid) Ecl3DKW.castFromKW(kw_global_size, grid) self.assertTrue(isinstance(kw_global_size, Ecl3DKW)) Ecl3DKW.castFromKW(kw_active_size, grid, default_value=66) self.assertTrue(isinstance(kw_active_size, Ecl3DKW)) self.assertEqual(kw_active_size[0, 0, 0], 66) with self.assertRaises(ValueError): kw_active_size[0, 0, 0] = 88
def test_sum( self ): kw_string = EclKW.create( "STRING" , 100 , EclTypeEnum.ECL_CHAR_TYPE ) with self.assertRaises(ValueError): kw_string.sum() kw_int = EclKW.create( "INT" , 4 , EclTypeEnum.ECL_INT_TYPE ) kw_int[0] = 1 kw_int[1] = 2 kw_int[2] = 3 kw_int[3] = 4 self.assertEqual( kw_int.sum() , 10 ) kw_d = EclKW.create( "D" , 4 , EclTypeEnum.ECL_DOUBLE_TYPE ) kw_d[0] = 1 kw_d[1] = 2 kw_d[2] = 3 kw_d[3] = 4 self.assertEqual( kw_d.sum() , 10 ) kw_f = EclKW.create( "F" , 4 , EclTypeEnum.ECL_FLOAT_TYPE ) kw_f[0] = 1 kw_f[1] = 2 kw_f[2] = 3 kw_f[3] = 4 self.assertEqual( kw_f.sum() , 10 ) kw_b = EclKW.create( "F" , 4 , EclTypeEnum.ECL_BOOL_TYPE ) kw_b[0] = False kw_b[1] = True kw_b[2] = False kw_b[3] = True self.assertEqual( kw_b.sum() , 2 )
def test_long_name(self): with self.assertRaises(ValueError): EclKW.create("LONGLONGNAME" , 10 , EclTypeEnum.ECL_INT_TYPE) kw = EclKW.create("REGIONS" , 10 , EclTypeEnum.ECL_INT_TYPE) with self.assertRaises(ValueError): kw.set_name("LONGLONGNAME")
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_truncate(self): kw1 = EclKW.create("KW1", 2, EclTypeEnum.ECL_INT_TYPE) kw2 = EclKW.create("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_kw(self): kw1 = EclKW.create("KW1", 2, EclTypeEnum.ECL_INT_TYPE) kw2 = EclKW.create("KW2", 2, EclTypeEnum.ECL_INT_TYPE) kw1[0] = 99 kw1[1] = 77 kw2[0] = 113 kw2[1] = 335 with TestAreaContext("python/fortio/write-kw"): f = FortIO.writer("test", fmt_file=False) kw1.fwrite(f) f.close() f = FortIO.open("test", mode="a") kw2.fwrite(f) f.close() f = FortIO.open("test", fmt_file=False) k1 = EclKW.fread(f) k2 = EclKW.fread(f) f.close() self.assertTrue(k1.equal(kw1)) self.assertTrue(k2.equal(kw2))
def test_sliced_set(self): kw = EclKW("REGIONS" , 10 , EclDataType.ECL_INT) kw.assign(99) kw[0:5] = 66 self.assertEqual(kw[0] , 66) self.assertEqual(kw[4] , 66) self.assertEqual(kw[5] , 99)
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_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.create("TEST1", element_count, EclTypeEnum.ECL_INT_TYPE) ecl_kw_2 = EclKW.create("TEST2", element_count, EclTypeEnum.ECL_INT_TYPE) 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)) eclFileIndexedRead(ecl_file, "TEST2", 0, index_map, char_buffer_2) 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_min_max(self): kw = EclKW("TEST", 3, EclTypeEnum.ECL_INT_TYPE) kw[0] = 10 kw[1] = 5 kw[2] = 0 self.assertEqual(10, kw.getMax()) self.assertEqual(0, kw.getMin()) self.assertEqual((0, 10), kw.getMinMax())
def loadGrid(self): grid_file = self.createTestPath("Statoil/ECLIPSE/Faults/grid.grdecl") fileH = open(grid_file, "r") specgrid = EclKW.read_grdecl(fileH, "SPECGRID", ecl_type=EclTypeEnum.ECL_INT_TYPE, strict=False) zcorn = EclKW.read_grdecl(fileH, "ZCORN") coord = EclKW.read_grdecl(fileH, "COORD") actnum = EclKW.read_grdecl(fileH, "ACTNUM", ecl_type=EclTypeEnum.ECL_INT_TYPE) return EclGrid.create(specgrid, zcorn, coord, actnum)
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_ecl_kw_grdecl_load(self): with TestAreaContext("ecl_kw/deprecate/grdecl_load"): kw = EclKW("PORO" , 1000 , EclTypeEnum.ECL_FLOAT_TYPE) with open("PORO.grdecl" , "w") as poro_file: kw.write_grdecl( poro_file ) with open("PORO.grdecl") as poro_file: with self.assertRaises(DeprecationWarning): kw = EclKW.grdecl_load( poro_file , "PORO")
def test_fault_block_layer_export(self): layer = FaultBlockLayer( self.grid , 1 ) kw1 = EclKW.create( "FAULTBLK" , self.grid.size + 1 , EclTypeEnum.ECL_INT_TYPE ) with self.assertRaises(ValueError): layer.exportKeyword( kw1 ) kw2 = EclKW.create( "FAULTBLK" , self.grid.size , EclTypeEnum.ECL_FLOAT_TYPE ) with self.assertRaises(TypeError): layer.exportKeyword(kw2)
def test_fault_block_layer_export(self): layer = FaultBlockLayer( self.grid , 1 ) kw1 = EclKW( "FAULTBLK" , self.grid.getGlobalSize() + 1 , EclDataType.ECL_INT ) with self.assertRaises(ValueError): layer.exportKeyword( kw1 ) kw2 = EclKW( "FAULTBLK" , self.grid.getGlobalSize() , EclDataType.ECL_FLOAT ) with self.assertRaises(TypeError): layer.exportKeyword(kw2)
def test_min_max(self): kw = EclKW("TEST", 3, EclTypeEnum.ECL_INT_TYPE) kw[0] = 10 kw[1] = 5 kw[2] = 0 self.assertEqual( 10 , kw.getMax()) self.assertEqual( 0 , kw.getMin()) self.assertEqual( (0,10) , kw.getMinMax())
def compressedKWCopy(self, kw): if len(kw) == self.getNumActive(): return EclKW.copy( kw ) elif len(kw) == self.getGlobalSize(): kw_copy = EclKW.create( kw.getName() , self.getNumActive() , kw.getEclType()) cfunc.compressed_kw_copy( self , kw_copy , kw) return kw_copy else: raise ValueError("The input keyword must have nx*n*nz or nactive elements. Size:%d invalid" % len(kw))
def globalKWCopy(self, kw , default_value): if len(kw) == self.getGlobalSize( ): return kw.copy( ) elif len(kw) == self.getNumActive(): kw_copy = EclKW( kw.getName() , self.getGlobalSize() , kw.getEclType()) kw_copy.assign( default_value ) self._global_kw_copy( kw_copy , kw) return kw_copy else: raise ValueError("The input keyword must have nx*n*nz or nactive elements. Size:%d invalid" % len(kw))
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_fault_block_edge(self): grid = EclGrid.createRectangular( (5,5,1) , (1,1,1) ) kw = EclKW( "FAULTBLK" , grid.getGlobalSize() , EclDataType.ECL_INT ) kw.assign( 0 ) for j in range(1,4): for i in range(1,4): g = i + j*grid.getNX() kw[g] = 1 layer = FaultBlockLayer( grid , 0 )
def test_fseek(self): file = open(self.src_file, "r") self.assertTrue(EclKW.fseek_grdecl(file, "PERMX")) self.assertFalse(EclKW.fseek_grdecl(file, "PERMY")) file.close() file = open(self.src_file, "r") kw1 = EclKW.read_grdecl(file, "PERMX") self.assertFalse(EclKW.fseek_grdecl(file, "PERMX")) self.assertTrue(EclKW.fseek_grdecl(file, "PERMX", rewind=True)) file.close()
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_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_fseek( self ): file = open(self.src_file, "r") self.assertTrue(EclKW.fseek_grdecl(file, "PERMX")) self.assertFalse(EclKW.fseek_grdecl(file, "PERMY")) file.close() file = open(self.src_file, "r") kw1 = EclKW.read_grdecl(file, "PERMX") self.assertFalse(EclKW.fseek_grdecl(file, "PERMX")) self.assertTrue(EclKW.fseek_grdecl(file, "PERMX", rewind=True)) file.close()
def setUp(self): self.grid = EclGrid.createRectangular( (10,10,10) , (1,1,1) ) self.kw = EclKW( "FAULTBLK" , self.grid.getGlobalSize() , EclDataType.ECL_INT ) self.kw.assign( 1 ) reg = EclRegion( self.grid , False ) for k in range(self.grid.getNZ()): reg.clear( ) reg.select_kslice( k , k ) self.kw.assign( k , mask = reg ) self.kw[ k * self.grid.getNX() * self.grid.getNY() + 7] = 177
def globalKWCopy(self, kw, default_value): if len(kw) == self.getGlobalSize(): return kw.copy() elif len(kw) == self.getNumActive(): kw_copy = EclKW(kw.getName(), self.getGlobalSize(), kw.data_type) kw_copy.assign(default_value) self._global_kw_copy(kw_copy, kw) return kw_copy else: raise ValueError( "The input keyword must have nx*n*nz or nactive elements. Size:%d invalid" % len(kw))
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_ecl_kw_indexed_read(self): with TestAreaContext("ecl_kw_indexed_read") as area: fortio = FortIO("index_test", mode=FortIO.WRITE_MODE) element_count = 100000 ecl_kw = EclKW.create("TEST", element_count, EclTypeEnum.ECL_INT_TYPE) for index in range(element_count): ecl_kw[index] = index ecl_kw.fwrite(fortio) fortio.close() fortio = FortIO("index_test", mode=FortIO.READ_MODE) new_ecl_kw = EclKW.fread(fortio) for index in range(element_count): self.assertEqual(new_ecl_kw[index], index) 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 = ctypes.create_string_buffer( len(index_map) * ctypes.sizeof(ctypes.c_int)) freadIndexedData(fortio, 24, EclTypeEnum.ECL_INT_TYPE, element_count, index_map, char_buffer) int_buffer = ctypes.cast(char_buffer, ctypes.POINTER(ctypes.c_int)) for index, index_map_value in enumerate(index_map): self.assertEqual(index_map_value, int_buffer[index])
def create(self, filename, load_actnum=True): fileH = open(filename, "r") specgrid = EclKW.read_grdecl(fileH, "SPECGRID", ecl_type=EclTypeEnum.ECL_INT_TYPE, strict=False) zcorn = EclKW.read_grdecl(fileH, "ZCORN") coord = EclKW.read_grdecl(fileH, "COORD") if load_actnum: actnum = EclKW.read_grdecl(fileH, "ACTNUM", ecl_type=EclTypeEnum.ECL_INT_TYPE) else: actnum = None mapaxes = EclKW.read_grdecl(fileH, "MAPAXES") grid = EclGrid.create(specgrid, zcorn, coord, actnum, mapaxes=mapaxes) return grid
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 iget_named_kw(self, kw_name, index, copy=False): """ Will return EclKW nr @index reference with header @kw_name. The keywords in a an ECLIPSE file are organized in a long linear array; keywords with the same name can occur many times. For instance a summary data[1] file might look like this: SEQHDR MINISTEP PARAMS MINISTEP PARAMS MINISTEP PARAMS .... To get the third 'PARAMS' keyword you can use the method call: params_kw = file.iget_named_kw( "PARAMS" , 2 ) The functionality of the iget_named_kw() method is also available through the __getitem__() method as: params_kw = file["PARAMS"][2] Observe that the returned EclKW instance is only a reference to the data owned by the EclFile instance. Observe that syntactically this is equivalent to file[kw_name][index], however the latter form will imply that all the keywords of this type are loaded from the file. If you know that only a few will actually be used it will be faster to use this method. [1]: For working with summary data you are probably better off using the EclSum class. """ if index < self.num_named_kw(kw_name): kw_c_ptr = cfunc.iget_named_kw(self, kw_name, index) ecl_kw = EclKW.wrap(kw_c_ptr, parent=self, data_owner=False) if copy: return EclKW.copy(ecl_kw) else: return ecl_kw else: raise KeyError( "Asked for occurence:%d of keyword:%s - max:%d" % (index, kw_name, self.num_named_kw(kw_name)) )
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.create("TEST1", element_count, EclTypeEnum.ECL_INT_TYPE) ecl_kw_2 = EclKW.create("TEST2", element_count, EclTypeEnum.ECL_INT_TYPE) 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)) eclFileIndexedRead(ecl_file, "TEST2", 0, index_map, char_buffer_2) 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 loadFromGrdecl(cls, filename): """Will create a new EclGrid instance from grdecl file. This function will scan the input file @filename and look for the keywords required to build a grid. The following keywords are required: SPECGRID ZCORN COORD In addition the function will look for and use the ACTNUM and MAPAXES keywords if they are found; if ACTNUM is not found all cells are assumed to be active. Slightly more exotic grid concepts like dual porosity, NNC mapping, LGR and coarsened cells will be completely ignored; if you need such concepts you must have an EGRID file and use the default EclGrid() constructor - that is also considerably faster. """ if os.path.isfile(filename): with open(filename) as f: specgrid = EclKW.read_grdecl(f, "SPECGRID", ecl_type=EclTypeEnum.ECL_INT_TYPE, strict=False) zcorn = EclKW.read_grdecl(f, "ZCORN") coord = EclKW.read_grdecl(f, "COORD") actnum = EclKW.read_grdecl(f, "ACTNUM", ecl_type=EclTypeEnum.ECL_INT_TYPE) mapaxes = EclKW.read_grdecl(f, "MAPAXES") if specgrid is None: raise ValueError( "The grdecl file:%s was invalid - could not find SPECGRID keyword" % filename) if zcorn is None: raise ValueError( "The grdecl file:%s was invalid - could not find ZCORN keyword" % filename) if coord is None: raise ValueError( "The grdecl file:%s was invalid - could not find COORD keyword" % filename) return EclGrid.create(specgrid, zcorn, coord, actnum, mapaxes) else: raise IOError("No such file:%s" % filename)
def loadGrid(self): grid_file = self.createTestPath("Statoil/ECLIPSE/Faults/grid.grdecl") fileH = open(grid_file, "r") specgrid = EclKW.read_grdecl(fileH, "SPECGRID", ecl_type=EclDataType.ECL_INT, strict=False) zcorn = EclKW.read_grdecl(fileH, "ZCORN") coord = EclKW.read_grdecl(fileH, "COORD") actnum = EclKW.read_grdecl(fileH, "ACTNUM", ecl_type=EclDataType.ECL_INT) return EclGrid.create(specgrid, zcorn, coord, actnum)
def test_equal(self): grid = EclGrid.createRectangular((10, 10, 1), (1, 1, 1)) kw_int = EclKW("INT", grid.getGlobalSize(), EclDataType.ECL_INT) kw_float = EclKW("FLOAT", grid.getGlobalSize(), EclDataType.ECL_FLOAT) kw_int[0:49] = 1 region = EclRegion(grid, False) region.select_equal(kw_int, 1) glist = region.getGlobalList() for g in glist: self.assertEqual(kw_int[g], 1) with self.assertRaises(ValueError): region.select_equal(kw_float, 1)
def test_ecl_kw_indexed_read(self): with TestAreaContext("ecl_kw_indexed_read") as area: fortio = FortIO("index_test", mode=FortIO.WRITE_MODE) element_count = 100000 ecl_kw = EclKW("TEST", element_count, EclDataType.ECL_INT) for index in range(element_count): ecl_kw[index] = index ecl_kw.fwrite(fortio) fortio.close() fortio = FortIO("index_test", mode=FortIO.READ_MODE) new_ecl_kw = EclKW.fread(fortio) for index in range(element_count): self.assertEqual(new_ecl_kw[index], index) 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 = ctypes.create_string_buffer(len(index_map) * ctypes.sizeof(ctypes.c_int)) self._freadIndexedData(fortio, 24, EclDataType.ECL_INT, element_count, index_map, char_buffer) int_buffer = ctypes.cast(char_buffer, ctypes.POINTER(ctypes.c_int)) for index, index_map_value in enumerate(index_map): self.assertEqual(index_map_value, int_buffer[index])
def iget_named_kw(self, kw_name, index, copy=False): """ Will return EclKW nr @index reference with header @kw_name. The keywords in a an ECLIPSE file are organized in a long linear array; keywords with the same name can occur many times. For instance a summary data[1] file might look like this: SEQHDR MINISTEP PARAMS MINISTEP PARAMS MINISTEP PARAMS .... To get the third 'PARAMS' keyword you can use the method call: params_kw = file.iget_named_kw( "PARAMS" , 2 ) The functionality of the iget_named_kw() method is also available through the __getitem__() method as: params_kw = file["PARAMS"][2] Observe that the returned EclKW instance is only a reference to the data owned by the EclFile instance. Observe that syntactically this is equivalent to file[kw_name][index], however the latter form will imply that all the keywords of this type are loaded from the file. If you know that only a few will actually be used it will be faster to use this method. [1]: For working with summary data you are probably better off using the EclSum class. """ if index < self.num_named_kw(kw_name): kw_c_ptr = cfunc.iget_named_kw(self, kw_name, index) ecl_kw = EclKW.wrap(kw_c_ptr, parent=self, data_owner=False) if copy: return EclKW.copy(ecl_kw) else: return ecl_kw else: raise KeyError("Asked for occurence:%d of keyword:%s - max:%d" % (index, kw_name, self.num_named_kw(kw_name)))
def loadGrid(path, load_actnum=True): """ @rtype: EclGrid """ with open(path, "r") as f: specgrid = EclKW.read_grdecl(f, "SPECGRID", ecl_type=EclTypeEnum.ECL_INT_TYPE, strict=False) zcorn = EclKW.read_grdecl(f, "ZCORN") coord = EclKW.read_grdecl(f, "COORD") actnum = None if load_actnum: actnum = EclKW.read_grdecl(f, "ACTNUM", ecl_type=EclTypeEnum.ECL_INT_TYPE) mapaxes = EclKW.read_grdecl(f, "MAPAXES") grid = EclGrid.create(specgrid, zcorn, coord, actnum, mapaxes=mapaxes) return grid
def test_resize(self): N = 4 kw = EclKW("KW" , N , EclTypeEnum.ECL_INT_TYPE) for i in range(N): kw[i] = i kw.resize( 2*N ) self.assertEqual( len(kw) , 2*N ) for i in range(N): self.assertEqual( kw[i] , i ) kw.resize( N/2 ) self.assertEqual( len(kw) , N/2 ) for i in range(N/2): self.assertEqual( kw[i] , i )
def make_field(rng , grid , iens): permx = EclKW.create( "PERMX" , grid.getGlobalSize( ) , EclTypeEnum.ECL_FLOAT_TYPE) permx.assign( rng.getDouble( ) ) poro = EclKW.create( "PORO" , grid.getGlobalSize( ) , EclTypeEnum.ECL_FLOAT_TYPE) poro.assign( rng.getDouble( ) ) if not os.path.isdir("fields"): os.makedirs("fields") with open("fields/permx%d.grdecl" % iens,"w") as f: permx.write_grdecl( f ) with open("fields/poro%d.grdecl" % iens ,"w") as f: poro.write_grdecl( f )
def test_Load( self ): kw = EclKW.read_grdecl(open(self.src_file, "r"), "PERMX") self.assertTrue(kw) grid = EclGrid( self.createTestPath("Statoil/ECLIPSE/Gurbat/ECLIPSE" )) kw = Ecl3DKW.read_grdecl(grid , open(self.src_file, "r"), "PERMX") self.assertTrue( isinstance( kw , Ecl3DKW ))
def restart_get_kw( self , kw_name , dtime , copy = False): """ Will return EclKW @kw_name from restart file at time @dtime. This function assumes that the current EclFile instance represents a restart file. It will then look for keyword @kw_name exactly at the time @dtime; @dtime is a datetime instance: file = EclFile( "ECLIPSE.UNRST" ) swat2010 = file.restart_get_kw( "SWAT" , datetime.datetime( 2000 , 1 , 1 )) By default the returned kw instance is a reference to the ecl_kw still contained in the EclFile instance; i.e. the kw will become a dangling reference if the EclFile instance goes out of scope. If the optional argument @copy is True the returned kw will be a true copy. If the file does not have the keyword at the specified time the function will return None. """ index = cfunc.get_restart_index( self , ctime( dtime ) ) if index >= 0: kw = self.iget_named_kw( kw_name , index ) if copy: return EclKW.copy( kw ) else: return kw else: return None
def replace_kw( self , old_kw , new_kw): """ Will replace @old_kw with @new_kw in current EclFile instance. This method can be used to replace one of the EclKW instances in the current EclFile. The @old_kw reference must be to the actual EclKW instance in the current EclFile instance (the final comparison is based on C pointer equality!), i.e. it must be a reference (not a copy) from one of the ??get_kw?? methods of the EclFile class. In the example below we replace the SWAT keyword from a restart file: swat = file.iget_named_kw( "SWAT" , 0 ) new_swat = swat * 0.25 file.replace_kw( swat , new_swat ) The C-level ecl_file_type structure takes full ownership of all installed ecl_kw instances; mixing the garbage collector into it means that this is quite low level - and potentially dangerous! """ # We ensure that this scope owns the new_kw instance; the # new_kw will be handed over to the ecl_file instance, and we # can not give away something we do not alreeady own. if not new_kw.data_owner: new_kw = EclKW.copy( new_kw ) # The ecl_file instance will take responsability for freeing # this ecl_kw instance. new_kw.data_owner = False cfunc.replace_kw( self , old_kw , new_kw , False )
def test_sliced_set(self): kw = EclKW.create("REGIONS" , 10 , EclTypeEnum.ECL_INT_TYPE) kw.assign(99) kw[0:5] = 66 self.assertEqual(kw[0] , 66) self.assertEqual(kw[4] , 66) self.assertEqual(kw[5] , 99)
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 )