Skip to content

Wrapper for gmtselect #1427

@weiji14

Description

@weiji14

Description of the desired feature

Implement gmtselect which "Select data table subsets based on multiple spatial criteria".

Mentioned at https://twitter.com/ATiredGradStdnt/status/1417189659365498884, also partially used before at https://forum.generic-mapping-tools.org/t/gmt-example-18-with-pygmt-volumes-and-spatial-selections/1585.

This function will probably be named select, i.e. called using pygmt.select(). As a table in, table out function, the implementation would be similar to already wrapped functions like blockmedian/blockmean. The code snippet would look something like so:

with GMTTempFile(suffix=".csv") as tmpfile:
with Session() as lib:
# Choose how data will be passed into the module
table_context = lib.virtualfile_from_data(
check_kind="vector", data=table, x=x, y=y, z=z
)
# Run blockm* on data table
with table_context as infile:
if outfile is None:
outfile = tmpfile.name
arg_str = " ".join([infile, build_arg_string(kwargs), "->" + outfile])
lib.call_module(module=block_method, args=arg_str)
# Read temporary csv output to a pandas table
if outfile == tmpfile.name: # if user did not set outfile, return pd.DataFrame
try:
column_names = table.columns.to_list()
result = pd.read_csv(tmpfile.name, sep="\t", names=column_names)
except AttributeError: # 'str' object has no attribute 'columns'
result = pd.read_csv(tmpfile.name, sep="\t", header=None, comment=">")
elif outfile != tmpfile.name: # return None if outfile set, output in outfile
result = None
return result

Xref table I/O discussion at #1268 and #1318. Best to wrap this module after #1426.

Are you willing to help implement and maintain this feature? Yes

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions