Skip to content

grdtrack: Fix the bug when profile is given - #1867

Merged
seisman merged 20 commits into
mainfrom
fix-grdtrack-profile
Jun 6, 2022
Merged

seisman merged 20 commits into
mainfrom
fix-grdtrack-profile

Conversation

@seisman

@seisman seisman commented Apr 5, 2022

Copy link
Copy Markdown
Member

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

Comment thread pygmt/src/grdtrack.py Resolved
@weiji14 weiji14 mentioned this pull request Apr 5, 2022
25 of 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
@seisman
seisman force-pushed the fix-grdtrack-profile branch from d70444f to 55662f6 Compare May 7, 2022 11:51
Comment thread pygmt/src/grdtrack.py Resolved
Comment thread pygmt/src/grdtrack.py
Comment on lines +305 to +308
elif isinstance(grid, str):
try:
xr.open_dataarray(which(grid, download="a"))
is_a_grid = True

Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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.

Comment thread pygmt/src/grdtrack.py Resolved
Comment thread pygmt/src/grdtrack.py Resolved
@seisman
seisman requested a review from weiji14 May 27, 2022 05:21
Comment thread pygmt/src/grdtrack.py Outdated
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
Copy Markdown
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
Copy Markdown
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")

@weiji14 weiji14 Jun 4, 2022 •

Copy link
Copy Markdown
Member

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)

Comment thread pygmt/src/grdtrack.py Resolved
Comment thread pygmt/src/grdtrack.py Resolved
Comment thread pygmt/src/grdtrack.py Resolved
Comment thread pygmt/src/grdtrack.py Resolved
Comment thread pygmt/src/grdtrack.py Resolved
Comment thread pygmt/src/grdtrack.py Resolved
Comment thread pygmt/src/grdtrack.py Resolved
Comment thread pygmt/src/grdtrack.py Resolved
Comment thread pygmt/src/grdtrack.py Resolved
Comment thread pygmt/src/grdtrack.py Outdated
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'.")

@weiji14 weiji14 Jun 4, 2022 •

Copy link
Copy Markdown
Member

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)

Comment thread pygmt/src/grdtrack.py Resolved
Comment thread pygmt/src/grdtrack.py Resolved

@weiji14 weiji14 left a comment

Copy link
Copy Markdown
Member

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.

@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
@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

This branch was previously deployed

1 inactive deployment
Preview — 4c809ce4 Deployed Jun 5, 2022 by vercel[bot]
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

None yet

Development

Successfully merging this pull request may close these issues.

grdtrack not working with "profile" option

2 participants