-
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
Let load_earth_relief() support all resolutions and optional subregion #542
Conversation
924862e
to
eab3c58
Compare
1a91063
to
d5f8ce0
Compare
@weiji14 I think the PR is ready for review, but the I need some help with:
|
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.
Co-authored-by: Wei Ji <23487320+weiji14@users.noreply.github.com>
Co-authored-by: Wei Ji <23487320+weiji14@users.noreply.github.com>
pygmt/datasets/earth_relief.py
Outdated
if resolution in non_tiled_resolutions: | ||
fname = which(f"@earth_relief_{resolution}{reg}", download="a") | ||
with xr.open_dataarray(fname) as dataarray: | ||
grid = dataarray.load() | ||
_ = grid.gmt # load GMTDataArray accessor information | ||
elif resolution in tiled_resolutions: | ||
# Titled grid can't be sliced. | ||
# See https://github.com/GenericMappingTools/pygmt/issues/524 | ||
if region is None: | ||
raise GMTInvalidInput( | ||
"region is required for high resolution (<='05m') Earth relief grid" | ||
) | ||
grid = grdcut(f"@earth_relief_{resolution}{reg}", region=region) |
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, one more thing. What if a user wants to get a non_tiled_resolution grid with a region? The current code doesn't seem to handle this case.
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.
The initial design was to allow users to specify a subregion for all resolutions. However, due to #524, the returned grids can't support slice operation, which makes three or more tests fail. So I have to do different ways for tiled and non-tiled resolutions, to keep all tests pass.
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.
If a user really want it, they need to call load_earth_relief
, then grdcut
, or using xarray slice operation.
I'm also OK to add "region" to all resolutions, and declare that "load_earth_relief() grids don't support slice operation" as a bug.
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.
Right, that's tricky. But, an unsuspecting user might pass in a region
argument for resolutions 06m
and above, and it would just be silently ignored (and a full grid is returned)! We should at least raise a warning or NotImplementedError
for this case.
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, slow reply. A workaround might be to just set the registration and gtype
directly after calling grdcut
, since we know it from the user input to load_earth_relief
.
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.
that
05m
(and higher resolution) grids which aregrdcut
won't work withgrdimage
orgrdview
, but we can fix this before doing a proper release.
I don't understand it. Why do you say it doesn't work?
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 don't understand it. Why do you say it doesn't work?
Sorry, it will work actually, I forgot for a second that we applied the workaround in #539.
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.
Actually, if we remove/pop the temp.nc file from
grid.encoding["source"]
after runninggrdcut
, we can then set the registration and gtype without getting an error. Do you think it's wise to apply this hack?
You mean applying the hack only in load_earth_relief()
function? Sounds a good idea.
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.
Implemented locally, but I'm getting this error:
_________________________________________ [doctest] pygmt.clib.conversion.dataarray_to_matrix _________________________________________
060 >>> matrix.flags.c_contiguous
061 True
062 >>> # Using a slice of the grid, the matrix will be copied to guarantee
063 >>> # that it's C-contiguous in memory. The increment should be unchanged.
064 >>> matrix, region, inc = dataarray_to_matrix(grid[10:41,30:101])
065 >>> matrix.flags.c_contiguous
066 True
067 >>> print(matrix.shape)
068 (31, 71)
069 >>> print(region)
Expected:
[-150.0, -79.0, -80.0, -49.0]
Got:
[-149.5, -79.5, -79.5, -49.5]
This is because grid.encoding["source"]
is removed, then the sliced new grid has the default registration and gtype.
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 don't see an easy fix for #524. Perhaps the best compromise is
raise a warning or NotImplementedError for this case.
RIght, let's just raise a warning here then, having layers and layers of workarounds isn't good anyway.
Description of proposed changes
See #489 for the reason of the function redesign. In summary, Earth relief data >05m are stored as single grids, and we can use the
which
function to get its full path and load it as DataArray. It doesn't work for earth relief data <=05m, which are split into smaller tiles. As suggested in #489, it's easier to usegrdcut @earth_relief_xxy -Rregion -Goutput.grd
. Thegrdcut
function works for all resolutions, but due to the issue #524, the DataArray returned doesn't support slice operations.Changes in this PR:
which
function. The returned DataArray still supports slice operations, so all tests passgrdcut
function. The returned DataArray doesn't support slice operation (see GMTDataArrayAccessor doesn't work for temporary files that have been sliced #524). The bug is also documented.region
argument to select a subregion._shape_from_resolution
tiled_resolutions
andnon_tiled_resolutions
, instead of the old_is_valid_resolution
function01s
and03s
from the test, because these two resolutions are validFixes #489.
Reminders
make format
andmake check
to make sure the code follows the style guide.doc/api/index.rst
.