def test_min_max(self): kw = EclKW.new("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 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 test_EclKW_min_max(self): kw = EclKW.new("TEST", 3, EclTypeEnum.ECL_INT_TYPE) with warnings.catch_warnings(): kw.min with warnings.catch_warnings(): kw.max with warnings.catch_warnings(): kw.min_max
def test_equal(self): kw1 = EclKW.new("TEST", 3, EclTypeEnum.ECL_CHAR_TYPE) kw1[0] = "Test1" kw1[1] = "Test13" kw1[2] = "Test15" kw2 = EclKW.new("TEST", 3, EclTypeEnum.ECL_CHAR_TYPE) kw2[0] = "Test1" kw2[1] = "Test13" kw2[2] = "Test15" self.assertTrue(kw1.equal(kw2)) self.assertTrue(kw1.equal_numeric(kw2)) kw2[2] = "Test15X" self.assertFalse(kw1.equal(kw2)) self.assertFalse(kw1.equal_numeric(kw2)) unrst_file_path = self.createTestPath( "Statoil/ECLIPSE/Gurbat/ECLIPSE.UNRST") unrst_file = EclFile(unrst_file_path) kw1 = unrst_file["PRESSURE"][0] kw2 = kw1.deep_copy() self.assertTrue(kw1.equal(kw2)) self.assertTrue(kw1.equal_numeric(kw2)) kw2 *= 1.00001 self.assertFalse(kw1.equal(kw2)) self.assertFalse(kw1.equal_numeric(kw2, epsilon=1e-8)) self.assertTrue(kw1.equal_numeric(kw2, epsilon=1e-2)) kw1 = unrst_file["ICON"][10] kw2 = kw1.deep_copy() self.assertTrue(kw1.equal(kw2)) self.assertTrue(kw1.equal_numeric(kw2)) kw1[-1] += 1 self.assertFalse(kw1.equal(kw2)) self.assertFalse(kw1.equal_numeric(kw2))
def test_equal(self): kw1 = EclKW.new("TEST", 3, EclTypeEnum.ECL_CHAR_TYPE) kw1[0] = "Test1" kw1[1] = "Test13" kw1[2] = "Test15" kw2 = EclKW.new("TEST", 3, EclTypeEnum.ECL_CHAR_TYPE) kw2[0] = "Test1" kw2[1] = "Test13" kw2[2] = "Test15" self.assertTrue(kw1.equal(kw2)) self.assertTrue(kw1.equal_numeric(kw2)) kw2[2] = "Test15X" self.assertFalse(kw1.equal(kw2)) self.assertFalse(kw1.equal_numeric(kw2)) unrst_file_path = self.createTestPath("Statoil/ECLIPSE/Gurbat/ECLIPSE.UNRST") unrst_file = EclFile(unrst_file_path) kw1 = unrst_file["PRESSURE"][0] kw2 = kw1.deep_copy() self.assertTrue(kw1.equal(kw2)) self.assertTrue(kw1.equal_numeric(kw2)) kw2 *= 1.00001 self.assertFalse(kw1.equal(kw2)) self.assertFalse(kw1.equal_numeric(kw2, epsilon=1e-8)) self.assertTrue(kw1.equal_numeric(kw2, epsilon=1e-2)) kw1 = unrst_file["ICON"][10] kw2 = kw1.deep_copy() self.assertTrue(kw1.equal(kw2)) self.assertTrue(kw1.equal_numeric(kw2)) kw1[-1] += 1 self.assertFalse(kw1.equal(kw2)) self.assertFalse(kw1.equal_numeric(kw2))
def createKW(self, array, kw_name, pack): """ Creates an EclKW instance based on existing 3D numpy object. The method create3D() does the inverse operation; creating a 3D numpy object from an EclKW instance. If the argument @pack is true the resulting keyword will have length 'nactive', otherwise the element will have length nx*ny*nz. """ if array.ndim == 3: dims = array.shape if dims[0] == self.nx and dims[1] == self.ny and dims[2] == self.nz: dtype = array.dtype if dtype == numpy.int32: type = EclTypeEnum.ECL_INT_TYPE elif dtype == numpy.float32: type = EclTypeEnum.ECL_FLOAT_TYPE elif dtype == numpy.float64: type = EclTypeEnum.ECL_DOUBLE_TYPE else: sys.exit("Do not know how to create ecl_kw from type:%s" % dtype) if pack: size = self.nactive else: size = self.size if len(kw_name) > 8: # Silently truncate to length 8 - ECLIPSE has it's challenges. kw_name = kw_name[0:8] kw = EclKW.new(kw_name, size, type) active_index = 0 global_index = 0 for k in range(self.nz): for j in range(self.ny): for i in range(self.nx): if pack: if self.active(global_index=global_index): kw[active_index] = array[i, j, k] active_index += 1 else: if dtype == numpy.int32: kw[global_index] = int(array[i, j, k]) else: kw[global_index] = array[i, j, k] global_index += 1 return kw raise ValueError("Wrong size / dimension on array")
def createKW( self , array , kw_name , pack): """ Creates an EclKW instance based on existing 3D numpy object. The method create3D() does the inverse operation; creating a 3D numpy object from an EclKW instance. If the argument @pack is true the resulting keyword will have length 'nactive', otherwise the element will have length nx*ny*nz. """ if array.ndim == 3: dims = array.shape if dims[0] == self.nx and dims[1] == self.ny and dims[2] == self.nz: dtype = array.dtype if dtype == numpy.int32: type = EclTypeEnum.ECL_INT_TYPE elif dtype == numpy.float32: type = EclTypeEnum.ECL_FLOAT_TYPE elif dtype == numpy.float64: type = EclTypeEnum.ECL_DOUBLE_TYPE else: sys.exit("Do not know how to create ecl_kw from type:%s" % dtype) if pack: size = self.nactive else: size = self.size if len(kw_name) > 8: # Silently truncate to length 8 - ECLIPSE has it's challenges. kw_name = kw_name[0:8] kw = EclKW.new( kw_name , size , type ) active_index = 0 global_index = 0 for k in range( self.nz ): for j in range( self.ny ): for i in range( self.nx ): if pack: if self.active( global_index = global_index ): kw[active_index] = array[i,j,k] active_index += 1 else: if dtype == numpy.int32: kw[global_index] = int( array[i,j,k] ) else: kw[global_index] = array[i,j,k] global_index += 1 return kw raise ValueError("Wrong size / dimension on array")
def kw_test(self, data_type, data, fmt): name1 = "file1.txt" name2 = "file2.txt" kw = EclKW.new("TEST", len(data), data_type) i = 0 for d in data: kw[i] = d i += 1 file1 = open(name1, "w") kw.fprintf_data(file1, fmt) file1.close() file2 = open(name2, "w") for d in data: file2.write(fmt % d) file2.close() self.assertFilesAreEqual(name1, name2)
def kw_test( self, data_type, data, fmt ): name1 = "file1.txt" name2 = "file2.txt" kw = EclKW.new("TEST", len(data), data_type) i = 0 for d in data: kw[i] = d i += 1 file1 = open(name1, "w") kw.fprintf_data(file1, fmt) file1.close() file2 = open(name2, "w") for d in data: file2.write(fmt % d) file2.close() self.assertFilesAreEqual(name1, name2) self.assertEqual( kw.getEclType() , data_type )
def test_kw_write(self): with TestAreaContext("python/ecl_kw/writing"): data = [random.random() for i in range(10000)] kw = EclKW.new("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))