Skip to content

Bug with x2sys_cross module not handling NaN values properly #1368

@ghost

Description

Description of the problem
When I using the x2sys_cross function, passing a pandas.DataFrame I got no result return. However, passing an ASCII file the the program works fine. I don't know what went wrong, and I tested the code of pull request#591 it worked.
I upload the ssh.txt for running the above code. See my testdata repertory. https://github.com/isLiYang/testdata

I hope someone can solve my question.

Thank You.

Full code that generated the error

import numpy as np
import pandas as pd
import pygmt
import os
from tempfile import TemporaryDirectory

# load my data.
data = np.loadtxt('ssh.txt')
df = pd.DataFrame({
    'lon': data[:, 0],
    'lat': data[:, 1],
    'ssh': data[:, 2]
})
df.columns = ['lon', 'lat', 'z']


# load test data from: Pull request #591.
# https://github.com/GenericMappingTools/pygmt/pull/591
dataframe: pd.DataFrame = pygmt.datasets.load_sample_bathymetry()
dataframe.columns = ["x", "y", "z"]  # longitude, latitude, bathymetry


# Test plll resuest #591, it works fine.
os.environ["X2SYS_HOME"] = os.getcwd()

with TemporaryDirectory(prefix="X2SYS", dir=os.environ["X2SYS_HOME"]) as tmpdir:
    tag = os.path.basename(tmpdir)
    pygmt.x2sys_init(tag=tag, fmtfile="xyz", suffix="xyz", force=True)
    output: pd.DataFrame = pygmt.x2sys_cross(tracks=[dataframe], tag=tag, coe="i", verbose="i")


# test my data.
# 1.When passing a ascii file, the calculation is fine and the ascii file and the dataframe object can be output.
os.environ["X2SYS_HOME"] = os.getcwd()
with TemporaryDirectory(prefix="X2SYS", dir=os.environ["X2SYS_HOME"]) as tmpdir:
    tag = os.path.basename(tmpdir)
    # passing file.
    pygmt.x2sys_init(tag=tag, fmtfile='geoz', suffix='txt', force=True, discontinuity='g', gap=['d100'], verbose='i')
    pygmt.x2sys_cross(tracks=['ssh.txt'], outfile='pyxovers1.dat', tag=tag, interpolation='a', verbose='i', region='g', trackvalues=True)
    xovers1 = pygmt.x2sys_cross(tracks=['ssh.txt'], tag=tag, interpolation='a', verbose='i', region='g', trackvalues=True)


# 2.When passing a dataframe, it will not output the result of the calculation or report an error.
os.environ["X2SYS_HOME"] = os.getcwd()
with TemporaryDirectory(prefix="X2SYS", dir=os.environ["X2SYS_HOME"]) as tmpdir:
    tag = os.path.basename(tmpdir)
    # passing file.
    pygmt.x2sys_init(tag=tag, fmtfile='geoz', force=True, discontinuity='g', gap=['d100'], verbose='i')
    pygmt.x2sys_cross(tracks=[df], outfile='pyxovers2.dat', tag=tag, interpolation='a', verbose='i', region='g', trackvalues=True)
    xovers2 = pygmt.x2sys_cross(tracks=[df], tag=tag, interpolation='a', verbose='i', region='g', trackvalues=True)

print('finished...')

Full error message
pyxovers2.dat will be created, but no data in there.
below are message of

pygmt.x2sys_cross(tracks=[df], outfile='pyxovers2.dat', tag=tag, interpolation='a', verbose='i', region='g', trackvalues=True)

and

    xovers2 = pygmt.x2sys_cross(tracks=[df], tag=tag, interpolation='a', verbose='i', region='g', trackvalues=True)
x2sys_cross [INFORMATION]: Files found: 1
x2sys_cross [INFORMATION]: Checking for duplicates :
x2sys_cross [INFORMATION]: 0 found
x2sys_cross [WARNING]: No time column, use dummy times
x2sys_cross [INFORMATION]: Writing Data Table to file pyxovers2.dat
x2sys_cross [INFORMATION]: Files found: 1
x2sys_cross [INFORMATION]: Checking for duplicates :
x2sys_cross [INFORMATION]: 0 found
x2sys_cross [WARNING]: No time column, use dummy times
x2sys_cross [INFORMATION]: Writing Data Table to file /tmp/pygmt-4qfsz7oy.txt
Traceback (most recent call last):
  File "/mnt/e/HY2A/tmp.py", line 50, in <module>
    xovers2 = pygmt.x2sys_cross(tracks=[df], tag=tag, interpolation='a', verbose='i', region='g', trackvalues=True)
  File "/home/liyang/anaconda3/envs/pygmt/lib/python3.9/site-packages/pygmt/helpers/decorators.py", line 414, in new_module
    return module_func(*args, **kwargs)
  File "/home/liyang/anaconda3/envs/pygmt/lib/python3.9/site-packages/pygmt/helpers/decorators.py", line 557, in new_module
    return module_func(*args, **kwargs)
  File "/home/liyang/anaconda3/envs/pygmt/lib/python3.9/site-packages/pygmt/src/x2sys_cross.py", line 228, in x2sys_cross
    table = pd.read_csv(
  File "/home/liyang/anaconda3/envs/pygmt/lib/python3.9/site-packages/pandas/io/parsers.py", line 610, in read_csv
    return _read(filepath_or_buffer, kwds)
  File "/home/liyang/anaconda3/envs/pygmt/lib/python3.9/site-packages/pandas/io/parsers.py", line 462, in _read
    parser = TextFileReader(filepath_or_buffer, **kwds)
  File "/home/liyang/anaconda3/envs/pygmt/lib/python3.9/site-packages/pandas/io/parsers.py", line 819, in __init__
    self._engine = self._make_engine(self.engine)
  File "/home/liyang/anaconda3/envs/pygmt/lib/python3.9/site-packages/pandas/io/parsers.py", line 1050, in _make_engine
    return mapping[engine](self.f, **self.options)  # type: ignore[call-arg]
  File "/home/liyang/anaconda3/envs/pygmt/lib/python3.9/site-packages/pandas/io/parsers.py", line 1873, in __init__
    self._reader = parsers.TextReader(self.handles.handle, **kwds)
  File "pandas/_libs/parsers.pyx", line 521, in pandas._libs.parsers.TextReader.__cinit__
pandas.errors.EmptyDataError: No columns to parse from file

Process finished with exit code 1

System information

PyGMT information:
  version: v0.4.0
System information:
  python: 3.9.5 (default, Jun  4 2021, 12:28:51)  [GCC 7.5.0]
  executable: /home/liyang/anaconda3/envs/pygmt/bin/python
  machine: Linux-4.4.0-19041-Microsoft-x86_64-with-glibc2.31
Dependency information:
  numpy: 1.20.2
  pandas: 1.2.5
  xarray: 0.18.2
  netCDF4: 1.5.7
  packaging: 20.9
  ghostscript: 9.54.0
  gmt: 6.2.0
GMT library information:
  binary dir: /home/liyang/anaconda3/envs/pygmt/bin
  cores: 8
  grid layout: rows
  library path: /home/liyang/anaconda3/envs/pygmt/lib/libgmt.so
  padding: 2
  plugin dir: /home/liyang/anaconda3/envs/pygmt/lib/gmt/plugins
  share dir: /home/liyang/anaconda3/envs/pygmt/share/gmt
  version: 6.2.0

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions