Skip to content

Commit 752e7bd

Browse files
authored
Merge pull request #16 from GeoStat-Framework/msh_read_fix
MSH: use engine='python' as fallback in pandas to read ogs mesh
2 parents b3052a2 + a6107fb commit 752e7bd

File tree

6 files changed

+24
-26
lines changed

6 files changed

+24
-26
lines changed

CHANGELOG.md

+9-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,13 @@
33
All notable changes to **ogs5py** will be documented in this file.
44

55

6+
## [1.2.2] - 2022-05-25
7+
8+
### Bugfixes
9+
* `MSH.load` now uses `engine="python"` as fallback in pandas to successfully read ogs meshes in some corner cases [#16](https://github.com/GeoStat-Framework/ogs5py/pull/16)
10+
* removed redundant `from io import open` which were there for py2 compatibility [#16](https://github.com/GeoStat-Framework/ogs5py/pull/16)
11+
12+
613
## [1.2.1] - 2022-05-15
714

815
### Enhancements
@@ -178,7 +185,8 @@ All notable changes to **ogs5py** will be documented in this file.
178185
First release of ogs5py.
179186

180187

181-
[Unreleased]: https://github.com/GeoStat-Framework/ogs5py/compare/v1.2.1...HEAD
188+
[Unreleased]: https://github.com/GeoStat-Framework/ogs5py/compare/v1.2.2...HEAD
189+
[1.2.2]: https://github.com/GeoStat-Framework/ogs5py/compare/v1.2.1...v1.2.2
182190
[1.2.1]: https://github.com/GeoStat-Framework/ogs5py/compare/v1.2.0...v1.2.1
183191
[1.2.0]: https://github.com/GeoStat-Framework/ogs5py/compare/v1.1.1...v1.2.0
184192
[1.1.1]: https://github.com/GeoStat-Framework/ogs5py/compare/v1.1.0...v1.1.1

ogs5py/fileclasses/base.py

-6
Original file line numberDiff line numberDiff line change
@@ -343,9 +343,6 @@ def read_file(self, path, encoding=None, verbose=False):
343343
verbose : bool, optional
344344
Print information of the reading process. Default: False
345345
"""
346-
# in python3 open was replaced with io.open
347-
# so we can use encoding keyword in python2
348-
from io import open
349346

350347
self.reset()
351348
try:
@@ -941,9 +938,6 @@ def read_file(self, path, encoding=None, verbose=False):
941938
verbose : bool, optional
942939
Print information of the reading process. Default: False
943940
"""
944-
# in python3 open was replaced with io.open
945-
# so we can use encoding key word in python2
946-
from io import open
947941

948942
self.reset()
949943

ogs5py/fileclasses/gem/core.py

-3
Original file line numberDiff line numberDiff line change
@@ -303,9 +303,6 @@ def read_file(self, path, encoding=None, verbose=False):
303303
-----
304304
This also reads the given files in the lst-file. (dch, ipm, dbr)
305305
"""
306-
# in python3 open was replaced with io.open
307-
# so we can use encoding key word in python2
308-
from io import open
309306

310307
root = os.path.dirname(path)
311308

ogs5py/fileclasses/gli/tools.py

-2
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,6 @@ def load_ogs5gli(filepath, verbose=True, encoding=None):
7070
- ``LAYER`` (int or None)
7171
7272
"""
73-
# in python3 open was replaced with io.open
74-
from io import open
7573

7674
out = dcp(EMPTY_GLI)
7775

ogs5py/fileclasses/ic/core.py

-2
Original file line numberDiff line numberDiff line change
@@ -309,8 +309,6 @@ def save(self, path, **kwargs):
309309

310310
def read_file(self, path, encoding=None, verbose=False):
311311
"""Write the actual RFR input file to the given folder."""
312-
# in python3 open was replaced with io.open
313-
from io import open
314312

315313
headers = []
316314
variables = []

ogs5py/fileclasses/msh/msh_io.py

+15-12
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,6 @@ def load_ogs5msh(
7474
The $AREA keyword within the Nodes definition is NOT supported
7575
and will violate the read data if present.
7676
"""
77-
# in python3 open was replaced with io.open
78-
from io import open
7977

8078
import pandas as pd
8179

@@ -186,13 +184,16 @@ def load_ogs5msh(
186184
filepos = msh.tell()
187185
# read the elements with pandas
188186
# names=range(max_node_no) to assure rectangular shape by cols
189-
tmp = pd.read_csv(
190-
msh,
191-
engine="c",
187+
pd_kwargs = dict(
192188
delim_whitespace=True,
193189
nrows=no_elements,
194190
names=range(max_node_no + 4), # +4 for the "-1" entry
195-
).values
191+
)
192+
try:
193+
tmp = pd.read_csv(msh, engine="c", **pd_kwargs).values
194+
except pd.errors.ParserError:
195+
msh.seek(filepos)
196+
tmp = pd.read_csv(msh, engine="python", **pd_kwargs).values
196197
# check if all given element-typs are OGS known
197198
pos_ele = 2 # can be shift to right, if "-1" occures
198199
check_elem = np.in1d(tmp[:, pos_ele], ELEM_NAMES)
@@ -301,8 +302,6 @@ def load_ogs5msh_old(filepath, verbose=True, max_node_no=8, encoding=None):
301302
element_id : dict
302303
contains element ids for each element sorted by element types
303304
"""
304-
# in python3 open was replaced with io.open
305-
from io import open
306305

307306
import pandas as pd
308307

@@ -336,13 +335,17 @@ def load_ogs5msh_old(filepath, verbose=True, max_node_no=8, encoding=None):
336335
print(no_elements)
337336
# read the elements with pandas
338337
# names=range(max_node_no) to assure rectangular shape by cols
339-
tmp = pd.read_csv(
340-
msh,
341-
engine="c",
338+
filepos = msh.tell()
339+
pd_kwargs = dict(
342340
delim_whitespace=True,
343341
nrows=no_elements,
344342
names=range(max_node_no + 4), # +4 for the "-1" entry
345-
).values
343+
)
344+
try:
345+
tmp = pd.read_csv(msh, engine="c", **pd_kwargs).values
346+
except pd.errors.ParserError:
347+
msh.seek(filepos)
348+
tmp = pd.read_csv(msh, engine="python", **pd_kwargs).values
346349
# check if all given element-typs are OGS known
347350
pos_ele = 2 # can be shift to right, if "-1" occures
348351
check_elem = np.in1d(tmp[:, pos_ele], ELEM_NAMES)

0 commit comments

Comments
 (0)