Issue with x2sys in PyGMT solved

Hi,

I am using x2sys_init and x2sys_cross to make cross-overs of satellite passes. I regularly update Python and PyGMT in a conda environment, but I have not been able to run the cross-overs with success since PyGMT v0.11.1.dev4. Now I am running PyGMT v0.16.0. I really would like to be able to run it in the newer versions and have found a simple workaround for the issue, and would like to share it if others are in the same situation.

My input to x2sys_cross is a pandas dataframe with columns lon, lat, t, h, and the call to x2sys_cross is:

for tt1, tt2 in itertools.combinations(df_tracks.groupby('Pass'), 2):
    try:
        df_crossovers: pd.DataFrame = pygmt.x2sys_cross(
                    tracks=[tt1,tt2],
                    tag=tagName,
                    interpolation="l",  # linear interpolation
                    coe="e",  
                    override="N",  
                    trackvalues=True, 
                    verbose="d",
                )
        ...
        ...

I got an error message in line 258 in x2sys_cross:

File “my_env/lib/python3.13/site-packages/pygmt/src/x2sys_cross.py”, line 258, in x2sys_cross
if columns[0][0] == “t”: # “t” or “i”:
~~~~~~~^^^

This was because my tracks did not cross, and therefore the dataframe was empty.

I did solve it by:

  with Session() as lib:
        with lib.virtualfile_out(kind="dataset", fname=outfile) as vouttbl:
            with contextlib.ExitStack() as stack:
                fnames = [stack.enter_context(c) for c in file_contexts]
                lib.call_module(
                    module="x2sys_cross",
                    args=build_arg_list(kwargs, infile=fnames, outfile=vouttbl),
                )
                result = lib.virtualfile_to_dataset(
                    vfname=vouttbl, output_type=output_type, header=2
                )

            if output_type == "file":
                return result
            elif isinstance(result, pd.DataFrame) and result.empty: ## This is a new line
                return pd.DataFrame()               ## This is a new line 

You can probably do it in a more Pythonic or GMT-like way, but this works for me.

Thanks @Sistjerne for the bug report, and also glad to see that someone is using x2sys!

This appears to be an oversight with a refactor of x2sys_cross in PyGMT v0.13.0. I’ve opened a pull request at x2sys_cross: Fix IndexError to allow empty dataframe outputs by weiji14 · Pull Request #4060 · GenericMappingTools/pygmt · GitHub that should hopefully fix this. Specifically, an empty pandas.DataFrame will be returned instead of an IndexError. Hopefully it’ll be merged in time for PyGMT v0.17.0.