-
Notifications
You must be signed in to change notification settings - Fork 229
Allow for grids with negative lat/lon increments #369
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
Allow for grids with negative lat/lon increments #369
Conversation
💖 Thanks for opening this pull request! 💖 Please make sure you read our contributing guidelines and abide by our code of conduct. A few things to keep in mind:
|
Right, I've taken a hard look at this, and you've definitely done a good job at getting the functionality right! I think we can improve the readability of the code though, I'm not sure if you're familiar with writing tests, but could you add some for the pygmt/pygmt/tests/test_clib.py Lines 767 to 786 in 55af9b6
To get you started on the unit tests, I recommend constructing a diagonal array like this, which you can flip around in the x/y directions by changing the start/stop values: data = np.diag(v=np.arange(3))
x = np.linspace(start=4, stop=0, num=3)
y = np.linspace(start=5, stop=9, num=3)
grid = xr.DataArray(data, coords=[("y", y), ("x", x)]) Just give me a shout out if you need any help 😄 |
pygmt/clib/conversion.py
Outdated
@@ -102,7 +104,19 @@ def dataarray_to_matrix(grid): | |||
) | |||
region.extend([coord.min(), coord.max()]) | |||
inc.append(coord_inc) | |||
matrix = as_c_contiguous(grid.values[::-1]) | |||
if inc[1] < 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 this uses xarray's builtin sort mechanism that simplifies the code by heaps.
if inc[1] < 0: | |
if any([i < 0 for i in inc]): # Sort grid properly if there are negative increments | |
inc = [abs(i) for i in inc] | |
grid = grid.sortby(variables=list(grid.dims), ascending=True) | |
matrix = as_c_contiguous(grid.values[::-1]) |
Note
Also, it would be good to see some unit tests to make sure it actually works for every possible combination of flipped latitude/longitude coordinates.
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 works. I suspect that the sorting operation is slower than simply flipping the array, but compared to everything involved with the projection routines, this is certainly not a limiting operation.
As for the tests, I'd be happy to try, but I'm not sure what the point would be. It seems that the test would be to check if xarray works properly, not pygmt. What kind of test were you thinking of?
Notes
- It appears that this works even if the DataSet doesn't supply coordinates (the input grid is unmodified). Though this probably won't occur in pygmt.
The only problem I could envision is if the user defined the longitude coordinates asThis scenario is caught when testing if the coordinate increments are all the same or not.[0, 90, 180, 270, 0]
. In this case the sort operation would provide incorrect results. It is hard to imagine someone doing this. If there is a test for this scenario, I would probably redefine the last coordinate somewhere else in the code. As part of such a test, you might also want to check that the two data values are the same (I think that gmt might already do this).
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 speed is the concern, it would be best to pass in a file anyway. You're right that the test is more of a check on xarray than pygmt, but we're actually not testing that dataarray_to_matrix
properly now so it's a good time as any to add it.
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 speed is the concern, it would be best to pass in a file anyway.
I'm not sure even GMT handles this properly. So passing in a file probably doesn't help much.
@leouieda, do you want to take a look at this |
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.
As for the tests, I'd be happy to try, but I'm not sure what the point would be. It seems that the test would be to check if xarray works properly, not pygmt. What kind of test were you thinking of?
@MarkWieczorek for the test, the thinking to check that if we pass in an array with flipped latitude or longitude that dataarray_to_matrix
really does flip the matrix
as expected (like what @weiji14 suggested). So we're not checking xarray, but checking that we wrote the sortby
correctly (and that future changes don't break the functionality).
Just checking in here @MarkWieczorek. Have you had a chance to write the tests yet? I'd be keen to merge this PR as soon as possible as I've been hitting the same flipped lat/lon problem a lot recently too 😂 |
Sorry but no. I all likelihood, this will probably take a week or two (I'm busy, plus I've never written pytests before). If you do this for me, I promise instead to improve the web documentation later... Also, I think that this issue is probably related: #375 I'm not sure exactly what is going on, but I think that |
Deal, I'll write up the tests for this now. I did take a look at that other issue, but it will require a bit more investigation. |
Checks that dataarray_to_matrix works for standard case where longitude (x) and latitude (y) dimensions are in positive increments (ascending order). Also check that they are flipped correctly when the x and/or y axis are in negative increments (descending order).
🎉🎉🎉 Congrats on merging your first pull request and welcome to the team! 🎉🎉🎉 Please open a new pull request to add yourself to the |
Thanks @MarkWieczorek! Hopefully this resolves your original issue at #368, I know it'll definitely help my workshop tutorials run more smoothly tomorrow. Definitely feel free to contribute more in the future, I can see you've got a lot of great ideas 😄 |
Thank you for the contribution Mark! 🎉 |
pygmt assumes that both the latitudes and longitudes of an xarray Dataset increase with increasing indices of the array. However, it is common for the latitudes to be arranged with decreasing values such that the first row corresponds to 90 N and the last to 90 S. Furthermore, in some weird and very old cases, planetary longitude has been defined as increasing in the westward direction.
Here, I have modified the function
dataarray_to_matrix
to check whether the longitude and latitude increments are positive. If they are negative, the array is flipped on the corresponding dimension.There are four possible scenarios, and I have attempted to avoid creating an additional array. If there is a better way to do this, please let me know!
Fixes # #368
Reminders
make format
andmake check
to make sure the code follows the style guide.doc/api/index.rst
.