diff --git a/lib/ecl/ecl_sum.c b/lib/ecl/ecl_sum.c index 5d96892978..13ad14dc46 100644 --- a/lib/ecl/ecl_sum.c +++ b/lib/ecl/ecl_sum.c @@ -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)); +} diff --git a/lib/ecl/ecl_sum_data.c b/lib/ecl/ecl_sum_data.c index 6f38d91f21..38480568ab 100644 --- a/lib/ecl/ecl_sum_data.c +++ b/lib/ecl/ecl_sum_data.c @@ -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); +} diff --git a/lib/include/ert/ecl/ecl_sum.h b/lib/include/ert/ecl/ecl_sum.h index 00d959bfd7..49835013e6 100644 --- a/lib/include/ert/ecl/ecl_sum.h +++ b/lib/include/ert/ecl/ecl_sum.h @@ -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 diff --git a/lib/include/ert/ecl/ecl_sum_data.h b/lib/include/ert/ecl/ecl_sum_data.h index a04d5363f5..c1920083ff 100644 --- a/lib/include/ert/ecl/ecl_sum_data.h +++ b/lib/include/ert/ecl/ecl_sum_data.h @@ -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); diff --git a/python/ecl/summary/ecl_sum.py b/python/ecl/summary/ecl_sum.py index 1b7c79568a..5822ba2517 100644 --- a/python/ecl/summary/ecl_sum.py +++ b/python/ecl/summary/ecl_sum.py @@ -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): @@ -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): """ diff --git a/python/tests/ecl_tests/test_sum.py b/python/tests/ecl_tests/test_sum.py index 811e7ec6db..bab08754bb 100644 --- a/python/tests/ecl_tests/test_sum.py +++ b/python/tests/ecl_tests/test_sum.py @@ -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):