Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CLN: Add types gridprop-import-xtgcpprop #1055

Merged
merged 1 commit into from
Nov 30, 2023
Merged

CLN: Add types gridprop-import-xtgcpprop #1055

merged 1 commit into from
Nov 30, 2023

Conversation

janbjorge
Copy link
Contributor

@janbjorge janbjorge commented Nov 29, 2023

Addresses #1011

@janbjorge janbjorge self-assigned this Nov 29, 2023
@codecov-commenter
Copy link

codecov-commenter commented Nov 29, 2023

Codecov Report

Attention: 16 lines in your changes are missing coverage. Please review.

Comparison is base (1ead811) 80.05% compared to head (3fc1fb5) 79.97%.
Report is 1 commits behind head on main.

Files Patch % Lines
src/xtgeo/grid3d/_gridprop_import_xtgcpprop.py 58.97% 14 Missing and 2 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1055      +/-   ##
==========================================
- Coverage   80.05%   79.97%   -0.08%     
==========================================
  Files          94       94              
  Lines       13534    13555      +21     
  Branches     2240     2245       +5     
==========================================
+ Hits        10835    10841       +6     
- Misses       1960     1973      +13     
- Partials      739      741       +2     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@@ -53,33 +71,39 @@ def import_xtgcpprop(mfile, ijrange=None, zerobased=False):
# read metadata which will be at position offet + nfloat*narr +13
pos = offset + nbyte * narr + 13

with open(mfile.file, "rb") as fhandle:
fhandle.seek(pos)
jmeta = fhandle.read().decode()
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no need to decode, json.loads can deal with bytes and strings.


# unpack header
swap, magic, nbyte, ncol, nrow, nlay = unpack("= i i i q q q", buf)
if not isinstance(mfile.file, Path):
Copy link
Contributor Author

@janbjorge janbjorge Nov 30, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Im not sure what todo in this case, i wonder if we should add a file-reader/opener context manger to the _XTGeoFile class that provides a filelike interface when reading.

Another option is that the types on _XTGeoFile.file is wrong, and it can only be Path?

Copy link
Collaborator

@mferrera mferrera Nov 30, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is valid for it to be a memory stream. I would suggest something like

if isinstance(mfile.file, Path):
    with open(mfile.file, "rb") as f:
        header = f.read(offset)
else:
    header = mfile.file.read(offset)   # might need to handle for StringIO
    mfile.file.seek(0)  # rewind

swap, magic, nbyte, ncol, nrow, nlay = unpack("= i i i q q q", header)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do think a context manager for header reads would be valuable generally, though

@janbjorge janbjorge marked this pull request as ready for review November 30, 2023 11:49
@janbjorge janbjorge requested review from jcrivenaes, eivindjahren and mferrera and removed request for eivindjahren November 30, 2023 11:49
Comment on lines 44 to 55
elif isinstance(filelike, BytesIO):
was_at = filelike.tell()
try:
yield filelike.read(offset)
finally:
filelike.seek(was_at)
elif isinstance(filelike, StringIO):
was_at = filelike.tell()
try:
yield filelike.read(offset).encode()
finally:
filelike.seek(was_at)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could be combined into one-ish, but i like this more. I tried and it just looks like a mess.

@janbjorge janbjorge requested a review from tnatt November 30, 2023 14:46
@@ -216,7 +215,7 @@ def __init__(
if isinstance(filelike, str):
filelike = pathlib.Path(filelike)

self._file = filelike
self._file: pathlib.Path | io.BytesIO | io.StringIO = filelike
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is there a reason why we don't use the FileLike defined in common.types here ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It includes str, in this case some code futher up the chain converts str to Path.

Copy link
Contributor

@tnatt tnatt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 🙂 Only wonder if this _read_filelike function should be located elsewhere due to its generic nature?

@janbjorge janbjorge marked this pull request as draft November 30, 2023 15:12
@janbjorge janbjorge marked this pull request as ready for review November 30, 2023 15:44
@janbjorge janbjorge merged commit 60c7c27 into equinor:main Nov 30, 2023
32 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants