-
Notifications
You must be signed in to change notification settings - Fork 224
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
GMTDataArrayAccessor: Fallback to default grid registration and gtype if the grid source file doesn't exist #2009
Conversation
Ping @maxrjones and @weiji14 for reviewing. |
Co-authored-by: Wei Ji <[email protected]>
I've added a unit test in 37869d4, and the test helps find a new bug. At the end of the test, I manually set the registration and gtype information and expect the two
|
The documentation of xarray accessor explains why they don't work as expected:
So every time |
c769028
to
7b0520a
Compare
The accessor |
OK. I've removed this test and added a simpler test instead. The old complicated test was moved to #1984 so that we can work on this later. |
pygmt/accessors.py
Outdated
try: | ||
self._source = self._obj.encoding["source"] # filepath to NetCDF source | ||
if not Path(self._source).exists(): | ||
raise FileNotFoundError( | ||
f"Grid source file {self._source} doesn't exist." | ||
) | ||
# Get grid registration and grid type from the last two columns of | ||
# the shortened summary information of `grdinfo`. | ||
self._registration, self._gtype = map( | ||
int, grdinfo(self._source, per_column="n").split()[-2:] | ||
) | ||
except (KeyError, ValueError): | ||
except (KeyError, ValueError, FileNotFoundError): | ||
self._registration = 0 # Default to Gridline registration | ||
self._gtype = 0 # Default to Cartesian grid type |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder if we should change these lines to the following codes, which I think are more readable:
self._source = self._obj.encoding.get("source")
if self._source is not None and Path(self._source).exists():
try:
# Get grid registration and grid type from the last two columns of
# the shortened summary information of `grdinfo`.
self._registration, self._gtype = map(
int, grdinfo(self._source, per_column="n").split()[-2:]
)
except ValueError:
self._registration = 0
self._gtype = 0
else:
self._registration = 0
self._gtype = 0
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So we will silently fallback to registration=0 and gtype=0 if grdinfo
cannot determine the grid registration and type? Ok I guess, but please add the code comments back 🙂
Ping @GenericMappingTools/pygmt-maintainers for review. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, 2 more typos, otherwise ok.
Co-authored-by: Wei Ji <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Only minor consistency changes, otherwise looks good!
Co-authored-by: Michael Grund <[email protected]>
Description of proposed changes
If the netCDF source file doesn't exist, running
grdinfo
on the netCDF source file gives the following error:The error is unfriendly and confusing. Actually, it makes no sense if the grid source file doesn't exist. In this case, it should fallback to registration=0 and gtype=0 unless we implement the idea in #2194 (comment).
Partially address #1984 and #524 (comment)
Please note that this PR doesn't solve the problems in #1984 and #524, because the registration and gtype information are incorrect, but at least it doesn't report confusing errors.