Repository navigation
Description
Description of the problem
grdtrack works well for netCDF file, but produces random results for xarray.DataArray grid. If I understand #1304 correctly, it works on Linux, but not on macOS and Windows. I believe it's an upstream GMT bug.
Full code that generated the error
from pygmt import grdtrack, grdcut
from pygmt.datasets import load_earth_relief
# load the earth_relief_01d_g file
grid = load_earth_relief(registration="gridline")
# prepare a subgrid in xarray.DataArray
dataarray = grid.sel(lat=slice(-49, -42), lon=slice(-118, -107))
# prepare a subgrid in netcdf
grdcut(grid, region=[-118, -107, -49, -42], outgrid="earth_subset.nc")
# run grdtrack on the netCDF file
output = grdtrack(points="@ridge.txt", grid="earth_subset.nc", newcolname="bathymetry")
print(output)
# run grdtrack on the xarray.DataArray
output = grdtrack(points="@ridge.txt", grid=dataarray, newcolname="bathymetry")
print(output)Full output
The first output is correct, and the second output is wrong.
grdtrack [WARNING]: Some input points were outside the grid domain(s).
0 1 2
0 -110.9536 -42.2489 -2797.394987
1 -111.3500 -42.2033 -2715.359945
2 -111.7309 -43.1706 -2711.704748
3 -112.2196 -44.3819 -2787.948153
4 -112.5505 -45.4878 -2746.303734
5 -112.6736 -45.8347 -2685.852247
6 -112.4235 -45.8561 -2785.099618
7 -112.9507 -47.2270 -2672.932596
8 -113.3124 -48.3208 -2706.345811
grdtrack [WARNING]: Some input points were outside the grid domain(s).
0 1 2
0 -110.9536 -42.2489 -2.974656e+03
1 -111.3500 -42.2033 -2.859961e+03
2 -111.7309 -43.1706 -2.711705e+03
3 -112.2196 -44.3819 -2.787948e+03
4 -112.5505 -45.4878 -2.746304e+03
5 -112.6736 -45.8347 -2.685852e+03
6 -112.4235 -45.8561 -2.785100e+03
7 -112.9507 -47.2270 -2.672933e+03
8 -113.3124 -48.3208 1.123369e+28
System information
Please paste the output of python -c "import pygmt; pygmt.show_versions()":
PyGMT information:
version: v0.3.2.dev168+ga8472925
System information:
python: 3.9.2 | packaged by conda-forge | (default, Feb 21 2021, 05:02:20) [Clang 11.0.1 ]
executable: /Users/seisman/.miniconda/envs/pygmt/bin/python
machine: macOS-11.2.3-x86_64-i386-64bit
Dependency information:
numpy: 1.20.1
pandas: 1.2.3
xarray: 0.18.0
netCDF4: 1.5.6
packaging: 20.9
ghostscript: 9.54.0
gmt: 6.2.0_922af84_2021.03.17
GMT library information:
binary dir: /Users/seisman/.miniconda/envs/pygmt/bin
cores: 8
grid layout: rows
library path: /Users/seisman/opt/GMT-master/lib/libgmt.dylib
padding: 2
plugin dir: /Users/seisman/opt/GMT-master/lib/gmt/plugins
share dir: /Users/seisman/opt/GMT-master/share
version: 6.2.0
@meghanrjones @PaulWessel Please see if you can debug it.
Activity
PaulWessel commentedon May 31, 2021
I get the first output but on the 2nd one I get a crash:
Key problem is that grdtrack assumes bicubic sampling which requires padding and your matrix has none. The other place this is an issue is in grdimage and there we create a padded grid if resampling is needed. Not sure if we should do the same here, but if we don't then you will never get to use bicubic. The alternative is to drop to bilinear and then no pads are needed. Perhaps the right thing to do is to see if -n is set of defaults to anything but bilinear and you have no pad then we must create that padded grid.
PaulWessel commentedon May 31, 2021
I also wonder if the more general solution to these are to possibly add a flag to the mode passed to GMT_Read_Data that means "this grid needs to have 2 boundary pads and if not then make it so". This flag would not do much for the case of reading from a data file, but in the case of a read-only input matrix with no pad it would basically allocate the grid, copy in the matrix, and from there on out we are working with the GMT-allocated grid. So, this flag would be set based on need, as follows:
need_to_project = (gmt_M_is_nonlinear_graticule (GMT) || Ctrl->E.dpi > 0);is true then pass GMT_GRID_NEEDS_PAD in any GMT_Read_Data calls, else not.I think this is the best solution rather than adding ad-hoc stuff like currently done in gmt_grd_project and what would be needed in grdtrack, etc. However, this may not be a super-quick fix, so perhaps in the interest of 6.2.0 we could temporarily impose -nl if no pad on the incoming grid? If so, should this be done in PyGMT?
PaulWessel commentedon May 31, 2021
If you have a way of passing -nl to grdtrack in pygmt then see if the script runs as is.
seisman commentedon May 31, 2021
Yes, adding
n="l"gives the same results for the two grdtrack calls.Does it means that wrappers like PyGMT must know the technical details to decide if
GMT_GRID_NEEDS_PADSis necessary?PaulWessel commentedon May 31, 2021
No, only the C modules need to know what they need, so easy for the wrappers: No changes (after implementing the GMT_GRID_NEEDS_PADS). I think @joa-quim may agree that this is a good enhancement. So in that case the GMT_GRID_NEEDS_PADS is not an API addition but just another internal developer constant.
seisman commentedon May 31, 2021
It sounds good.
PaulWessel commentedon May 31, 2021
So I think until then the simple thing is to check in grdtrack that if pad is < 2 then we set the -n interpolant to bilinear.
For now, restrict interpolation on grids with no pad to be bilinear
For now, restrict interpolation on grids with no pad to be bilinear (#…
seisman commentedon May 31, 2021
FYI, the issue has been temporarily fixed in GenericMappingTools/gmt#5283 and should be available in GMT 6.2.0.
seisman commentedon Oct 28, 2021
FYI, the better solution mentioned in #1309 (comment) has been implemented in GenericMappingTools/gmt#5893 and will be available in GMT 6.3.0. The temporary fix in GenericMappingTools/gmt#5283 is reverted in GenericMappingTools/gmt#5911.
weiji14 commentedon Oct 28, 2021
Ok, I'll reopen this issue since we'll need to fix the breaking tests since the
grdtrackvalues have changed. See https://github.com/GenericMappingTools/pygmt/runs/4028911441?check_suite_focus=true#step:15:566.=================================== FAILURES =================================== _________________ test_grdtrack_input_dataframe_and_dataarray __________________ dataarray = <xarray.DataArray 'elevation' (lat: 8, lon: 12)> array([[-3385. , -3201.5, -3104. , -2953. , -2754. , -2782. , -2957.5...elevation relative to the geoid units: meters vertical_datum: EMG96 horizontal_datum: WGS84 dataframe = longitude latitude 0 -32.2971 37.4118 1 -32.3909 37.1394 2 -32.6448 37.1760 3 -32.706...6.6762 -45.6414 4143 -75.8527 -45.5741 4144 -75.7026 -46.0728 4145 -75.5410 -46.5777 [4146 rows x 2 columns] def test_grdtrack_input_dataframe_and_dataarray(dataarray, dataframe): """ Run grdtrack by passing in a pandas.DataFrame and xarray.DataArray as inputs. """ output = grdtrack(points=dataframe, grid=dataarray, newcolname="bathymetry") assert isinstance(output, pd.DataFrame) assert output.columns.to_list() == ["longitude", "latitude", "bathymetry"] > npt.assert_allclose(output.iloc[0], [-110.9536, -42.2489, -2790.488422]) E AssertionError: E Not equal to tolerance rtol=1e-07, atol=0 E E Mismatched elements: 1 / 3 (33.3%) E Max absolute difference: 6.90656549 E Max relative difference: 0.00247504 E x: array([ -110.9536 , -42.2489 , -2797.394987]) E y: array([ -110.9536 , -42.2489 , -2790.488422]) /usr/share/miniconda3/envs/pygmt/lib/python3.9/site-packages/pygmt/tests/test_grdtrack.py:60: AssertionError ----------------------------- Captured stderr call ----------------------------- Warning: [WARNING]: Some input points were outside the grid domain(s). __________________ test_grdtrack_input_csvfile_and_dataarray ___________________ dataarray = <xarray.DataArray 'elevation' (lat: 8, lon: 12)> array([[-3385. , -3201.5, -3104. , -2953. , -2754. , -2782. , -2957.5...elevation relative to the geoid units: meters vertical_datum: EMG96 horizontal_datum: WGS84 csvfile = '/home/runner/.gmt/cache/ridge.txt' def test_grdtrack_input_csvfile_and_dataarray(dataarray, csvfile): """ Run grdtrack by passing in a csvfile and xarray.DataArray as inputs. """ try: output = grdtrack(points=csvfile, grid=dataarray, outfile=TEMP_TRACK) assert output is None # check that output is None since outfile is set assert os.path.exists(path=TEMP_TRACK) # check that outfile exists at path track = pd.read_csv(TEMP_TRACK, sep="\t", header=None, comment=">") > npt.assert_allclose(track.iloc[0], [-110.9536, -42.2489, -2790.488422]) E AssertionError: E Not equal to tolerance rtol=1e-07, atol=0 E E Mismatched elements: 1 / 3 (33.3%) E Max absolute difference: 6.90656549 E Max relative difference: 0.00247504 E x: array([ -110.9536 , -42.2489 , -2797.394987]) E y: array([ -110.9536 , -42.2489 , -2790.488422]) /usr/share/miniconda3/envs/pygmt/lib/python3.9/site-packages/pygmt/tests/test_grdtrack.py:75: AssertionError