Skip to content

Commit

Permalink
ecl_sum.from_pandas: takes into account key can have both wgname and …
Browse files Browse the repository at this point in the history
…nums.
  • Loading branch information
Steinar Foss committed Nov 21, 2018
1 parent 041112b commit 1a41876
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 26 deletions.
47 changes: 23 additions & 24 deletions python/ecl/summary/ecl_sum.py
Original file line number Diff line number Diff line change
Expand Up @@ -532,43 +532,42 @@ def _compile_headers_list(headers, dims):
lst = re.split(':', key)
kw = lst[0]
wgname = None
num = 0
num = 0;
unit = "UNIT"
if len(lst) > 1:
if lst[1][0].isdigit():
nums = []
if lst[1][0].isdigit():
nums = re.split(',', lst[1])
if len(nums) > 1:
i = int(nums[0])-1
j = int(nums[1])-1
k = int(nums[2])-1
num = i + j * dims[0] + k * dims[0]*dims[1] + 1
else:
num = int(nums[0])
else:
wgname = lst[1]
wgname = lst[1]
if len(lst) == 3:
nums = re.split(",", lst[2])
if len(nums) == 3:
i = int(nums[0])-1
j = int(nums[1])-1
k = int(nums[2])-1
if dims is None:
raise ValueError("For key %s When using indexing i,j,k you must supply a valid value for the dims argument" % key)
num = i + j * dims[0] + k * dims[0]*dims[1] + 1
elif len(nums) == 1:
num = int(nums[0])

var_list.append( [kw, wgname, num, unit] )
return var_list

@classmethod
def from_pandas(cls, case, frame, dims = (1,1,1), headers = None):

def from_pandas(cls, case, frame, dims = None, headers = None):
start_time = frame.index[0]

ecl_sum = EclSum.writer(case,
start_time.to_pydatetime(),
dims[0], dims[1], dims[2])

var_list = []

frame_header_list = EclSum._compile_headers_list( frame.columns.values, dims )
if headers:
header_list = EclSum._compile_headers_list( headers, dims )
var_list = []
if headers is None:
header_list = EclSum._compile_headers_list( frame.columns.values, dims )
else:
header_list = []

for kw, wgname, num, unit in frame_header_list:
if (not headers) or [kw, wgname, num, unit] in header_list:
var_list.append( ecl_sum.addVariable( kw , wgname = wgname , num = num, unit =unit).getKey1() )
header_list = EclSum._compile_headers_list( headers, dims )
for kw, wgname, num, unit in header_list:
var_list.append( ecl_sum.addVariable( kw , wgname = wgname , num = num, unit =unit).getKey1() )

for i, time in enumerate(frame.index):
days = (time - start_time).days
Expand Down
4 changes: 2 additions & 2 deletions python/tests/ecl_tests/test_sum.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ def create_case(case = "CSV", restart_case = None, restart_step = -1, data_start

def create_case2(case = "CSV", restart_case = None, restart_step = -1, data_start = None):
length = 100
return createEclSum(case , [("WOPT", "OPX" , 0, "SM3") , ("FOPR" , None , 0, "SM3/DAY"), ("BPR" , None , 10, "SM3"), ("RPR", None, 3, "BARS")],
return createEclSum(case , [("WOPT", "OPX" , 0, "SM3") , ("FOPR" , None , 0, "SM3/DAY"), ("BPR" , None , 10, "SM3"), ("RPR", None, 3, "BARS"), ("COPR", "OPX", 421, "BARS")],
sim_length_days = length,
num_report_step = 10,
num_mini_step = 10,
Expand Down Expand Up @@ -577,7 +577,7 @@ def test_csv_load(self):
df = ecl_sum.pandas_frame()
assert_frame_equal(frame, df)

ecl_sum_less = EclSum.from_pandas("PANDAS", frame, dims=[20,10,5], headers=['RPR:3,1,1', 'GNAFS:5', 'ALPHA', 'BPR:10'])
ecl_sum_less = EclSum.from_pandas("PANDAS", frame, dims=[20,10,5], headers=['BPR:10', 'RPR:3,1,1', 'COPR:OPX:1,2,3'])
del frame['WOPT:OPX']
del frame['FOPR']
df_less = ecl_sum_less.pandas_frame()
Expand Down

0 comments on commit 1a41876

Please sign in to comment.