From d3a4b536d2deaa7d259ea30e73b1e09918609204 Mon Sep 17 00:00:00 2001 From: Steinar Foss Date: Mon, 23 Dec 2019 11:35:52 +0100 Subject: [PATCH] python FieldProperties can return numpy double arrays. --- python/cxx/field_props.cpp | 27 +++++++++++++++++---------- python/tests/test_field_props.py | 6 +++--- python/tests/test_props.py | 3 +++ 3 files changed, 23 insertions(+), 13 deletions(-) diff --git a/python/cxx/field_props.cpp b/python/cxx/field_props.cpp index 418690507..094f5cb20 100644 --- a/python/cxx/field_props.cpp +++ b/python/cxx/field_props.cpp @@ -9,15 +9,6 @@ namespace { - py::list getitem( const FieldPropsManager& m, const std::string& kw) { - if (m.has(kw)) - return iterable_to_pylist( m.get(kw) ); - if (m.has(kw)) - return iterable_to_pylist( m.get(kw) ); - - throw py::key_error( "no such grid property " + kw ); - } - bool contains( const FieldPropsManager& manager, const std::string& kw) { if (manager.has(kw)) return true; @@ -26,6 +17,21 @@ namespace { return false; } + + py::array_t get_double_array(const FieldPropsManager& m, const std::string& kw) { + if (m.has(kw)) + return convert::numpy_array( m.get(kw) ); + else + throw std::invalid_argument("Keyword '" + kw + "'is not of type double."); + } + + py::array_t get_int_array(const FieldPropsManager& m, const std::string& kw) { + if (m.has(kw)) + return convert::numpy_array( m.get(kw) ); + else + throw std::invalid_argument("Keyword '" + kw + "'is not of type int."); + } + } @@ -33,7 +39,8 @@ void python::common::export_FieldProperties(py::module& module) { py::class_< FieldPropsManager >( module, "FieldProperties") .def( "__contains__", &contains ) - .def( "__getitem__", &getitem ) + .def( "get_double_array", &get_double_array ) + .def( "get_int_array", &get_int_array ) ; } diff --git a/python/tests/test_field_props.py b/python/tests/test_field_props.py index 18eb2a391..d40eae540 100644 --- a/python/tests/test_field_props.py +++ b/python/tests/test_field_props.py @@ -32,11 +32,11 @@ if (test_field_props()): def test_getitem(self): p = self.props - poro = p['PORO'] + poro = p.get_double_array('PORO') self.assertEqual(324, len(poro)) self.assertEqual(0.13, poro[0]) self.assertTrue( 'PERMX' in p ) - px = p['PERMX'] + px = p.get_double_array('PERMX') print(len(px)) self.assertEqual(324, len(px)) @@ -47,7 +47,7 @@ if (test_field_props()): field_props = self.props grid = self.spe3.grid() - permx = field_props['PERMX'] + permx = field_props.get_double_array('PERMX') print('set(PERMX) = %s' % set(permx)) # 130mD, 40mD, 20mD, and 150mD, respectively, top to bottom darcys = {0:md2si(130), 1:md2si(40), 2:md2si(20), 3:md2si(150)} diff --git a/python/tests/test_props.py b/python/tests/test_props.py index 6d270e8b5..ca8b86d8f 100644 --- a/python/tests/test_props.py +++ b/python/tests/test_props.py @@ -19,6 +19,9 @@ class TestProps(unittest.TestCase): def setUp(self): parser = Parser() deck = parser.parse(test_path('spe3/SPE3CASE1.DATA')) + #int_array = np.array([0, 1, 2, 3]) + #hbnum_kw = DeckKeyword( parser["HBNUM"], int_array) + #deck.add(hbnum_kw) self.spe3 = EclipseState(deck) self.props = self.spe3.ecl3d_props() if (not test_field_props()):