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

grdtrack: Fix the bug when profile is given #1867

Merged
merged 20 commits into from
Jun 6, 2022
Merged

Conversation

seisman
Copy link
Member

@seisman seisman commented Apr 5, 2022

Description of proposed changes

Fixes #1827.

Reminders

  • Run make format and make check to make sure the code follows the style guide.
  • Add tests for new features or tests that would have caught the bug that you're fixing.
  • Add new public functions/methods/classes to doc/api/index.rst.
  • Write detailed docstrings for all functions/methods.
  • If wrapping a new module, open a 'Wrap new GMT module' issue and submit reasonably-sized PRs.
  • If adding new functionality, add an example to docstrings or tutorials.

Slash Commands

You can write slash commands (/command) in the first line of a comment to perform
specific operations. Supported slash commands are:

  • /format: automatically format and lint the code
  • /test-gmt-dev: run full tests on the latest GMT development version

@weiji14 weiji14 mentioned this pull request Apr 5, 2022
30 tasks
@weiji14 weiji14 added the bug Something isn't working label Apr 5, 2022
@seisman seisman added this to the 0.7.0 milestone Apr 6, 2022
@seisman seisman marked this pull request as ready for review May 7, 2022 11:50
pygmt/src/grdtrack.py Outdated Show resolved Hide resolved
Comment on lines 305 to 308
elif isinstance(grid, str):
try:
xr.open_dataarray(which(grid, download="a"))
is_a_grid = True
Copy link
Member

Choose a reason for hiding this comment

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

Does pygmt.which work with local NetCDF files? Also, not sure if I like this if-condition that requires opening a file using xr.open_dataarray to check if it is a grid. What about using grdinfo instead?

Copy link
Member Author

Choose a reason for hiding this comment

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

Does pygmt.which work with local NetCDF files?

Yes, it works.

Also, not sure if I like this if-condition that requires opening a file using xr.open_dataarray to check if it is a grid. What about using grdinfo instead?

grdinfo doesn't work. We don't catch the errors when reading a non-grid file:

pygmt.grdinfo("pygmt/tests/data/points.txt")
grdinfo (gmtapi_import_grid): Not a supported grid format [pygmt/tests/data/points.txt]
[Session pygmt-session (2)]: Error returned from GMT API: GMT_GRID_READ_ERROR (18)
[Session pygmt-session (2)]: Error returned from GMT API: GMT_GRID_READ_ERROR (18)

Copy link
Member

Choose a reason for hiding this comment

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

Hmm, so pygmt.grdinfo produces a blank string for non-grid files. What about a check like:

if pygmt.grdinfo(grid) != "":
    is_a_grid = True
else:
    is_a_grid = False

As an aside, we colud also think about making grdinfo produce proper Python errors as part of the grdinfo refactor (#593).

Copy link
Member Author

Choose a reason for hiding this comment

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

if pygmt.grdinfo(grid) != "":
    is_a_grid = True
else:
    is_a_grid = False

It may work, but users will see the errors like:

grdinfo (gmtapi_import_grid): Not a supported grid format [pygmt/tests/data/points.txt]
[Session pygmt-session (2)]: Error returned from GMT API: GMT_GRID_READ_ERROR (18)
[Session pygmt-session (2)]: Error returned from GMT API: GMT_GRID_READ_ERROR (18)

Copy link
Member

Choose a reason for hiding this comment

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

Hmm yeah, I tried pygmt.grdinfo(grid="pygmt/tests/data/points.txt", verbose="q") which removed the first error line, but still getting two [Session pygmt-session (2)]: Error returned from GMT API: GMT_GRID_READ_ERROR (18) errors printed.

Copy link
Member Author

Choose a reason for hiding this comment

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

FYI, I've changed the line to

xr.open_dataarray(which(grid, download="a")).close()

to recycle any related resources.

Co-authored-by: Wei Ji <23487320+weiji14@users.noreply.github.com>
@seisman seisman added this to In progress in Release v0.7.x via automation May 11, 2022
@seisman seisman moved this from In progress to Review in progress in Release v0.7.x May 11, 2022
pygmt/src/grdtrack.py Outdated Show resolved Hide resolved
raise GMTInvalidInput("Must give 'points' or set 'profile'.")

if points is not None and kwargs.get("E") is not None:
raise GMTInvalidInput("Can't set both 'points' and 'profile'.")
Copy link
Member

Choose a reason for hiding this comment

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

Need a unit test to cover this line?

Copy link
Member Author

Choose a reason for hiding this comment

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

I added this unit test to cover this, but it's unclear to me why the coverage report says this line is not covered.

def test_grdtrack_set_points_and_profile(dataarray, dataframe):
    """
    Run grdtrack but set both 'points' and 'profile'.
    """
    with pytest.raises(GMTInvalidInput):
        grdtrack(grid=dataarray, points=dataframe, profile="BL/TR")

Copy link
Member

@weiji14 weiji14 Jun 4, 2022

Choose a reason for hiding this comment

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

I tried running that line grdtrack(grid=dataarray, points=dataframe, profile="BL/TR") and got GMTInvalidInput: Please pass in a str to 'newcolname'. So maybe move these two lines a few lines above. See my suggested change at #1867 (comment)

pygmt/src/grdtrack.py Outdated Show resolved Hide resolved
pygmt/src/grdtrack.py Outdated Show resolved Hide resolved
pygmt/src/grdtrack.py Outdated Show resolved Hide resolved
pygmt/src/grdtrack.py Outdated Show resolved Hide resolved
pygmt/src/grdtrack.py Show resolved Hide resolved
pygmt/src/grdtrack.py Show resolved Hide resolved
Co-authored-by: Wei Ji <23487320+weiji14@users.noreply.github.com>
pygmt/src/grdtrack.py Outdated Show resolved Hide resolved
pygmt/src/grdtrack.py Outdated Show resolved Hide resolved
pygmt/src/grdtrack.py Outdated Show resolved Hide resolved
raise GMTInvalidInput("Must give 'points' or set 'profile'.")

if points is not None and kwargs.get("E") is not None:
raise GMTInvalidInput("Can't set both 'points' and 'profile'.")
Copy link
Member

@weiji14 weiji14 Jun 4, 2022

Choose a reason for hiding this comment

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

I tried running that line grdtrack(grid=dataarray, points=dataframe, profile="BL/TR") and got GMTInvalidInput: Please pass in a str to 'newcolname'. So maybe move these two lines a few lines above. See my suggested change at #1867 (comment)

pygmt/src/grdtrack.py Outdated Show resolved Hide resolved
pygmt/src/grdtrack.py Outdated Show resolved Hide resolved
Co-authored-by: Wei Ji <23487320+weiji14@users.noreply.github.com>
Copy link
Member

@weiji14 weiji14 left a comment

Choose a reason for hiding this comment

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

Should be ok if all tests pass.

Release v0.7.x automation moved this from Review in progress to Reviewer approved Jun 5, 2022
@weiji14 weiji14 added the final review call This PR requires final review and approval from a second reviewer label Jun 5, 2022
@seisman seisman merged commit ce73717 into main Jun 6, 2022
Release v0.7.x automation moved this from Reviewer approved to Done Jun 6, 2022
@seisman seisman deleted the fix-grdtrack-profile branch June 6, 2022 05:48
@seisman seisman removed the final review call This PR requires final review and approval from a second reviewer label Jun 6, 2022
sixy6e pushed a commit to sixy6e/pygmt that referenced this pull request Dec 21, 2022
Co-authored-by: Wei Ji <23487320+weiji14@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
No open projects
Development

Successfully merging this pull request may close these issues.

grdtrack not working with "profile" option
2 participants