Skip to content

Commit

Permalink
Merge pull request #488 from stefoss23/export_corners
Browse files Browse the repository at this point in the history
WIP: Export corners
  • Loading branch information
joakim-hove authored Sep 28, 2018
2 parents b67dd26 + ec81c4c commit b709556
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 15 deletions.
20 changes: 18 additions & 2 deletions lib/ecl/ecl_grid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7113,7 +7113,7 @@ void ecl_grid_export_index(const ecl_grid_type * grid, int * global_index, int *

//This function is meant to be used w/ pandas datafram and numpy
//Note: index_size must equal allocated size of output
void ecl_grid_export_data_as_int( const ecl_grid_type * grid, int index_size, const int * data_index, const ecl_kw_type * kw, int * output) {
void ecl_grid_export_data_as_int( int index_size, const int * data_index, const ecl_kw_type * kw, int * output) {
int * input = ecl_kw_get_int_ptr(kw);
for (int i=0; i < index_size; i++) {
int di = data_index[i];
Expand All @@ -7124,7 +7124,7 @@ void ecl_grid_export_data_as_int( const ecl_grid_type * grid, int index_size, co

//This function is meant to be used w/ pandas datafram and numpy
//Note: index_size must equal allocated size of output
void ecl_grid_export_data_as_double( const ecl_grid_type * grid, int index_size, const int * data_index, const ecl_kw_type * kw, double * output) {
void ecl_grid_export_data_as_double( int index_size, const int * data_index, const ecl_kw_type * kw, double * output) {
for (int i=0; i < index_size; i++) {
int di = data_index[i];
if (di >= 0)
Expand All @@ -7149,4 +7149,20 @@ void ecl_grid_export_position( const ecl_grid_type * grid, int index_size, const
}
}

//This function is meant to be used w/ pandas dataframe and numpy
void export_corners( const ecl_grid_type * grid, int index_size, const int * global_index, double * output) {
double x[8], y[8], z[8];
int pos = 0;
for (int i = 0; i < index_size; i++) {
int g = global_index[i];
ecl_grid_export_cell_corners1(grid, g, x, y, z);
for (int j = 0; j < 8; j++) {
output[pos++] = x[j];
output[pos++] = y[j];
output[pos++] = z[j];
}
}
}


//
5 changes: 3 additions & 2 deletions lib/include/ert/ecl/ecl_grid.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -262,10 +262,11 @@ extern "C" {

ert_ecl_unit_enum ecl_grid_get_unit_system(const ecl_grid_type * grid);
void ecl_grid_export_index(const ecl_grid_type * grid, int * global_index, int * index_data , bool active_only);
void ecl_grid_export_data_as_int( const ecl_grid_type * grid, int index_size, const int * global_index, const ecl_kw_type * kw, int * output);
void ecl_grid_export_data_as_double( const ecl_grid_type * grid, int index_size, const int * data_index, const ecl_kw_type * kw, double * output);
void ecl_grid_export_data_as_int( int index_size, const int * global_index, const ecl_kw_type * kw, int * output);
void ecl_grid_export_data_as_double( int index_size, const int * data_index, const ecl_kw_type * kw, double * output);
void ecl_grid_export_volume( const ecl_grid_type * grid, int index_size, const int * global_index, double * output );
void ecl_grid_export_position( const ecl_grid_type * grid, int index_size, const int * global_index, double * output);
void export_corners( const ecl_grid_type * grid, int index_size, const int * global_index, double * output);

UTIL_IS_INSTANCE_HEADER( ecl_grid );
UTIL_SAFE_CAST_HEADER( ecl_grid );
Expand Down
24 changes: 17 additions & 7 deletions python/ecl/grid/ecl_grid.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,10 +118,11 @@ class EclGrid(BaseCClass):
_export_mapaxes = EclPrototype("ecl_kw_obj ecl_grid_alloc_mapaxes_kw(ecl_grid)")
_get_unit_system = EclPrototype("ecl_unit_enum ecl_grid_get_unit_system(ecl_grid)")
_export_index_frame = EclPrototype("void ecl_grid_export_index(ecl_grid, int*, int*, bool)")
_export_data_as_int = EclPrototype("void ecl_grid_export_data_as_int(ecl_grid, int, int*, ecl_kw, int*)")
_export_data_as_double = EclPrototype("void ecl_grid_export_data_as_double(ecl_grid, int, int*, ecl_kw, double*)")
_export_data_as_int = EclPrototype("void ecl_grid_export_data_as_int(int, int*, ecl_kw, int*)", bind = False)
_export_data_as_double = EclPrototype("void ecl_grid_export_data_as_double(int, int*, ecl_kw, double*)", bind = False)
_export_volume = EclPrototype("void ecl_grid_export_volume(ecl_grid, int, int*, double*)")
_export_position = EclPrototype("void ecl_grid_export_position(ecl_grid, int, int*, double*)")
_export_corners = EclPrototype("void export_corners(ecl_grid, int, int*, double*)")



Expand Down Expand Up @@ -1271,7 +1272,7 @@ def export_index(self, active_only = False):
df = pandas.DataFrame(data=data, index=indx, columns=['i', 'j', 'k', 'active'])
return df

def export_data(self, index_frame, kw):
def export_data(self, index_frame, kw, default = 0):
if not isinstance(index_frame, pandas.DataFrame):
raise TypeError("index_frame must be pandas.DataFrame")
if len(kw) == self.get_global_size():
Expand All @@ -1282,18 +1283,18 @@ def export_data(self, index_frame, kw):
raise ValueError("The keyword must have a 3D compatible length")

if kw.type is EclTypeEnum.ECL_INT_TYPE:
data = numpy.zeros( len(index), dtype=numpy.int32 )
data = numpy.full( len(index), default, dtype=numpy.int32 )
self._export_data_as_int( len(index),
index.ctypes.data_as(ctypes.POINTER(ctypes.c_int32)),
kw,
data.ctypes.data_as(ctypes.POINTER(ctypes.c_int32)) )
data.ctypes.data_as(ctypes.POINTER(ctypes.c_int32)) )
return data
elif kw.type is EclTypeEnum.ECL_FLOAT_TYPE or kw.type is EclTypeEnum.ECL_DOUBLE_TYPE:
data = numpy.zeros( len(index), dtype=numpy.float64 )
data = numpy.full( len(index), default, dtype=numpy.float64 )
self._export_data_as_double( len(index),
index.ctypes.data_as(ctypes.POINTER(ctypes.c_int32)),
kw,
data.ctypes.data_as(ctypes.POINTER(ctypes.c_double)) )
data.ctypes.data_as(ctypes.POINTER(ctypes.c_double)) )
return data
else:
raise TypeError("Keyword must be either int, float or double.")
Expand All @@ -1314,6 +1315,15 @@ def export_position(self, index_frame):
data.ctypes.data_as(ctypes.POINTER(ctypes.c_double)) )
return data

def export_corners(self, index_frame):
index = numpy.array( index_frame.index, dtype=numpy.int32 )
data = numpy.zeros( [len(index), 24], dtype=numpy.float64 )
self._export_corners( len(index),
index.ctypes.data_as(ctypes.POINTER(ctypes.c_int32)),
data.ctypes.data_as(ctypes.POINTER(ctypes.c_double)) )
return data


def export_coord(self):
return self._export_coord()

Expand Down
16 changes: 12 additions & 4 deletions python/tests/ecl_tests/test_grid_pandas.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,11 @@ def test_dataframe_actnum(self):
global_index = df.index;
assert( np.array_equal(global_index, np.array([0, 1, 2, 3, 4, 5])) )

data = grid.export_data(df, kw_int_active)
assert( np.array_equal(data, np.array([9, 8, 0, 0, 7, 6])) )
data = grid.export_data(df, kw_int_active, 9999)
assert( np.array_equal(data, np.array([9, 8, 9999, 9999, 7, 6])) )

data = grid.export_data(df, kw_float_active)
assert( np.array_equal(data, np.array([10.5, 9.25, 0.0, 0.0, 2.0, 1.625])) )
data = grid.export_data(df, kw_float_active, 2222.0)
assert( np.array_equal(data, np.array([10.5, 9.25, 2222.0, 2222.0, 2.0, 1.625])) )


def test_dataframe_grid_data(self):
Expand All @@ -102,5 +102,13 @@ def test_dataframe_grid_data(self):
assert( np.array_equal(y_pos, np.array([0.5, 0.5, 1.5, 1.5, 2.5, 2.5])) )
assert( np.array_equal(z_pos, np.array([0.5, 0.5, 0.5, 0.5, 0.5, 0.5])) )

corner_data = grid.export_corners(index_frame)
compare = np.array([[0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 1.0, 1.0, 0.0, 0.0, 0.0, 1.0, 1.0, 0.0, 1.0, 0.0, 1.0, 1.0, 1.0, 1.0, 1.0],
[1.0, 0.0, 0.0, 2.0, 0.0, 0.0, 1.0, 1.0, 0.0, 2.0, 1.0, 0.0, 1.0, 0.0, 1.0, 2.0, 0.0, 1.0, 1.0, 1.0, 1.0, 2.0, 1.0, 1.0],
[0.0, 1.0, 0.0, 1.0, 1.0, 0.0, 0.0, 2.0, 0.0, 1.0, 2.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0, 1.0, 0.0, 2.0, 1.0, 1.0, 2.0, 1.0],
[1.0, 1.0, 0.0, 2.0, 1.0, 0.0, 1.0, 2.0, 0.0, 2.0, 2.0, 0.0, 1.0, 1.0, 1.0, 2.0, 1.0, 1.0, 1.0, 2.0, 1.0, 2.0, 2.0, 1.0],
[0.0, 2.0, 0.0, 1.0, 2.0, 0.0, 0.0, 3.0, 0.0, 1.0, 3.0, 0.0, 0.0, 2.0, 1.0, 1.0, 2.0, 1.0, 0.0, 3.0, 1.0, 1.0, 3.0, 1.0],
[1.0, 2.0, 0.0, 2.0, 2.0, 0.0, 1.0, 3.0, 0.0, 2.0, 3.0, 0.0, 1.0, 2.0, 1.0, 2.0, 2.0, 1.0, 1.0, 3.0, 1.0, 2.0, 3.0, 1.0]])
assert( np.array_equal(corner_data, compare) )


0 comments on commit b709556

Please sign in to comment.