Skip to content

Commit

Permalink
Merge pull request #383 from joakim-hove/summary-last-value
Browse files Browse the repository at this point in the history
Add ecl_sum function to get the last value.
  • Loading branch information
joakim-hove authored Apr 20, 2018
2 parents 9471148 + fd09cd2 commit e3e894a
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 2 deletions.
13 changes: 13 additions & 0 deletions lib/ecl/ecl_sum.c
Original file line number Diff line number Diff line change
Expand Up @@ -1426,3 +1426,16 @@ time_t_vector_type * ecl_sum_alloc_time_solution( const ecl_sum_type * ecl_sum ,
ert_ecl_unit_enum ecl_sum_get_unit_system(const ecl_sum_type * ecl_sum) {
return ecl_smspec_get_unit_system(ecl_sum->smspec);
}

double ecl_sum_iget_last_value(const ecl_sum_type * ecl_sum, int param_index) {
return ecl_sum_data_get_last_value(ecl_sum->data, param_index);
}

double ecl_sum_get_last_value_gen_key(const ecl_sum_type * ecl_sum, const char * gen_key) {
const smspec_node_type * node = ecl_sum_get_general_var_node( ecl_sum , gen_key );
return ecl_sum_iget_last_value(ecl_sum, smspec_node_get_params_index(node));
}

double ecl_sum_get_last_value_node(const ecl_sum_type * ecl_sum, const smspec_node_type *node) {
return ecl_sum_iget_last_value(ecl_sum, smspec_node_get_params_index(node));
}
6 changes: 6 additions & 0 deletions lib/ecl/ecl_sum_data.c
Original file line number Diff line number Diff line change
Expand Up @@ -1487,3 +1487,9 @@ bool ecl_sum_data_report_step_compatible( const ecl_sum_data_type * data1 , cons
}
return compatible;
}


double ecl_sum_data_get_last_value(const ecl_sum_data_type * data, int param_index) {
const ecl_sum_tstep_type * tstep = vector_get_last_const(data->data);
return ecl_sum_tstep_iget( tstep, param_index);
}
4 changes: 4 additions & 0 deletions lib/include/ert/ecl/ecl_sum.h
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,10 @@ typedef struct ecl_sum_struct ecl_sum_type;
double_vector_type * ecl_sum_alloc_days_solution( const ecl_sum_type * ecl_sum , const char * gen_key , double cmp_value , bool rates_clamp_lower);
time_t_vector_type * ecl_sum_alloc_time_solution( const ecl_sum_type * ecl_sum , const char * gen_key , double cmp_value , bool rates_clamp_lower);

double ecl_sum_iget_last_value(const ecl_sum_type * ecl_sum, int param_index);
double ecl_sum_get_last_value_gen_key(const ecl_sum_type * ecl_sum, const char * gen_key);
double ecl_sum_get_last_value_node(const ecl_sum_type * ecl_sum, const smspec_node_type *node);

UTIL_IS_INSTANCE_HEADER( ecl_sum );

#ifdef __cplusplus
Expand Down
2 changes: 2 additions & 0 deletions lib/include/ert/ecl/ecl_sum_data.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ typedef struct ecl_sum_data_struct ecl_sum_data_type ;
bool ecl_sum_data_report_step_equal( const ecl_sum_data_type * data1 , const ecl_sum_data_type * data2);
bool ecl_sum_data_report_step_compatible( const ecl_sum_data_type * data1 , const ecl_sum_data_type * data2);
void ecl_sum_data_fwrite_interp_csv_line(const ecl_sum_data_type * data , time_t sim_time, const ecl_sum_vector_type * keylist, FILE *fp);
double ecl_sum_data_get_last_value(const ecl_sum_data_type * data, int param_index);


double_vector_type * ecl_sum_data_alloc_seconds_solution( const ecl_sum_data_type * data , const smspec_node_type * node , double value, bool rates_clamp_lower);

Expand Down
7 changes: 5 additions & 2 deletions python/ecl/summary/ecl_sum.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ class EclSum(BaseCClass):
_add_tstep = EclPrototype("ecl_sum_tstep_ref ecl_sum_add_tstep(ecl_sum, int, double)")
_export_csv = EclPrototype("void ecl_sum_export_csv(ecl_sum, char*, stringlist, char*, char*)")
_identify_var_type = EclPrototype("ecl_sum_var_type ecl_sum_identify_var_type(char*)", bind = False)

_get_last_value = EclPrototype("double ecl_sum_get_last_value_gen_key(ecl_sum, char*)")


def __init__(self, load_case, join_string=":", include_restart=True):
Expand Down Expand Up @@ -452,7 +452,10 @@ def get_last_value(self, key):
The alternative method 'last' will return a EclSumNode
instance with some extra time related information.
"""
return self[key].last_value
if not key in self:
raise KeyError("No such key:%s" % key)

return self._get_last_value(key)

def get_last(self, key):
"""
Expand Down
9 changes: 9 additions & 0 deletions python/tests/ecl_tests/test_sum.py
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,15 @@ def test_vector_select_all(self):
self.assertIn(key, ecl_sum_vector)


def test_last(self):
case = create_case()
with self.assertRaises(KeyError):
case.get_last_value("NO_SUCH_KEY")
last_fopt = case.get_last_value("FOPT")
values = case.get_values("FOPT")
self.assertEqual( last_fopt, values[-1])


def test_time_range(self):
case = create_case()
with self.assertRaises(ValueError):
Expand Down

0 comments on commit e3e894a

Please sign in to comment.