Issue using crossprofile in grdtrack for pygmt

Hello,

I seem to be having trouble using the crossprofile feature in pygmt. Here is a snippet of the code I am using:

track = pygmt.grdtrack(points=points, grid=grid, newcolname="bathymetry", crossprofile='400k/2k')

And I obtain the following error:

  File "/home/aberne/anaconda3/envs/ge167/lib/python3.10/site-packages/pygmt/helpers/decorators.py", line 594, in new_module
    return module_func(*args, **kwargs)
  File "/home/aberne/anaconda3/envs/ge167/lib/python3.10/site-packages/pygmt/helpers/decorators.py", line 734, in new_module
    return module_func(*args, **kwargs)
  File "/home/aberne/anaconda3/envs/ge167/lib/python3.10/site-packages/pygmt/src/grdtrack.py", line 355, in grdtrack
    result = pd.read_csv(tmpfile.name, sep="\t", names=column_names)
  File "/home/aberne/anaconda3/envs/ge167/lib/python3.10/site-packages/pandas/util/_decorators.py", line 311, in wrapper
    return func(*args, **kwargs)
  File "/home/aberne/anaconda3/envs/ge167/lib/python3.10/site-packages/pandas/io/parsers/readers.py", line 678, in read_csv
    return _read(filepath_or_buffer, kwds)
  File "/home/aberne/anaconda3/envs/ge167/lib/python3.10/site-packages/pandas/io/parsers/readers.py", line 581, in _read
    return parser.read(nrows)
  File "/home/aberne/anaconda3/envs/ge167/lib/python3.10/site-packages/pandas/io/parsers/readers.py", line 1253, in read
    index, columns, col_dict = self._engine.read(nrows)
  File "/home/aberne/anaconda3/envs/ge167/lib/python3.10/site-packages/pandas/io/parsers/c_parser_wrapper.py", line 225, in read
    chunks = self._reader.read_low_memory(nrows)
  File "pandas/_libs/parsers.pyx", line 805, in pandas._libs.parsers.TextReader.read_low_memory
  File "pandas/_libs/parsers.pyx", line 861, in pandas._libs.parsers.TextReader._read_rows
  File "pandas/_libs/parsers.pyx", line 847, in pandas._libs.parsers.TextReader._tokenize_rows
  File "pandas/_libs/parsers.pyx", line 1960, in pandas._libs.parsers.raise_parser_error
pandas.errors.ParserError: Error tokenizing data. C error: Expected 3 fields in line 2, saw 5

I am unsure whether I am inputting the arguments into crossprofile incorrectly, or if perhaps I am missing an argument. Any suggestions?

Hello @aberne,

Welcome to the GMT forum :slightly_smiling_face:.

Currently, it is difficult to help you in detail, as your code example is incomplete, especially we do not know what your variables points and grid are. Maybe you can expand your code example and also provide your input data, please.

Based on the error message, your problem seems to be related to pandas:

pandas.errors.ParserError: Error tokenizing data. C error: Expected 3 fields in line 2, saw 5

It looks like something is not correct with your input data, and pandas can’t find the same number of columns in each row.

Hi Yvonne,

Thanks for the reply! Here is a more complete version of the code I am trying to run:

grid_topo = pygmt.xyz2grd(
    x=grid_lon_vec, y=grid_lat_vec, z=Shape_grid, spacing=spacing, region=region
) 

grid = grid_topo 

num_points=1000
numpy_points=np.zeros((num_points,2)) #1 is longitude, 2 is latitude

lon=np.zeros(num_points)
for i in range(len(lon)):
	lon[i]=90

lat=np.linspace(70,-70,num_points)

numpy_points[:,0]= lon
numpy_points[:,1]= lat

points = pd.DataFrame(numpy_points, columns = ['longitude','latitude'])

And then the main portion of the code:

pygmt.grdtrack(points=points, grid=grid, newcolname="bathymetry",outfile = 'Profile.txt')
track = pygmt.grdtrack(points=points, grid=grid, newcolname="bathymetry", crossprofile='400k/2k')

My pandas version is 1.4.4

Any thoughts?

Best!

Alex