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_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_fortio_creation(self): with TestAreaContext("python/fortio/create"): w = FortIO("test", FortIO.WRITE_MODE) rw = FortIO("test", FortIO.READ_AND_WRITE_MODE) r = FortIO("test", FortIO.READ_MODE) a = FortIO("test", FortIO.APPEND_MODE) w.close() w.close() # should not fail
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_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_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_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_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 loadFromFile(cls, filename): """ Will inspect the @filename argument and create a new EclGrid instance. """ if FortIO.isFortranFile(filename): return EclGrid(filename) else: return EclGrid.loadFromGrdecl(filename)
def loadFromFile(cls , filename): """ Will inspect the @filename argument and create a new EclGrid instance. """ if FortIO.isFortranFile( filename ): return EclGrid( filename ) else: return EclGrid.loadFromGrdecl( filename )
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("test", FortIO.WRITE_MODE, fmt_file=False) kw1.fwrite(f) f = FortIO("test", FortIO.APPEND_MODE) kw2.fwrite(f) f = FortIO("test", fmt_file=False) k1 = EclKW.fread(f) k2 = EclKW.fread(f) self.assertTrue(k1.equal(kw1)) self.assertTrue(k2.equal(kw2))
def test_fortio_read_and_write(self): with TestAreaContext("python/fortio/read_and_write"): f = FortIO("test", FortIO.WRITE_MODE) record_size = 4000 for i, c in enumerate("abcdefghijklmnopqrstuvwxyz"): data = bytearray(c * record_size) f.writeRecord(data) position = f.getPosition() self.assertEqual(position, (i + 1) * (record_size + 8)) f = FortIO("test", FortIO.READ_MODE) for c in "abcdefghijklmnopqrstuvwxyz": record = f.readRecordAsString(record_size) self.assertEqual(record, c * record_size)
def test_kw_write(self): with TestAreaContext("python/ecl_kw/writing"): data = [random.random() for i in range(10000)] kw = EclKW("TEST", len(data), EclTypeEnum.ECL_DOUBLE_TYPE) i = 0 for d in data: kw[i] = d i += 1 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_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 test_open_read(self): f = FortIO.reader(self.unrst_file) self.assertTrue(f)
def test_noex(self): with self.assertRaises(IOError): f = FortIO.reader("/tmp/does/notExist")
def test_open_read(self): f = FortIO(self.unrst_file, FortIO.READ_MODE) self.assertIsNotNone(f)
def test_open_write(self): with TestAreaContext("python/fortio/write"): f = FortIO("newfile", FortIO.WRITE_MODE) self.assertTrue(os.path.exists("newfile"))
def test_noex(self): with self.assertRaises(IOError): f = FortIO("odes_not_exist", FortIO.READ_MODE)
def test_fortio_read_and_write_and_rewrite(self): with TestAreaContext("python/fortio/read_and_write_and_rewrite"): record_size = 4000 f = FortIO("complete", FortIO.WRITE_MODE) for c in "abcdefghijklmnopqrstuvwxyz": data = bytearray(c * record_size) f.writeRecord(data) f = FortIO("test", FortIO.WRITE_MODE) positions = {} for c in "abcdefghij-lmnopqrstuvwxyz": data = bytearray(c * record_size) f.writeRecord(data) positions[c] = f.getPosition() f = FortIO("test", FortIO.READ_AND_WRITE_MODE) f.seek(positions["j"]) new_data = bytearray("k" * record_size) f.writeRecord(new_data) f.close() self.assertFilesAreEqual("test", "complete")
def test_open_write(self): with TestAreaContext("python/fortio/write"): f = FortIO.writer("newfile") f.close() self.assertTrue(True)