Skip to content

Reduce repeated code for managing the outgrid parameter #1400

@maxrjones

Description

@maxrjones

Description of the desired feature

The block of code below is currently used seven times throughout PyGMT modules (probably more pending Will's PRs). Based on GenericMappingTools/gmt#5563 (comment), it may need to be used up to 34 times if we continue adding functions 1:1 with GMT modules. I think it would be useful to add an build_outgrid_args() method (or something similar) that can be used throughout to build the argument string using either outgrid or tempfile.name. I expect this would be simplest to implement as a new method in the Session class, although it could be a function in utils.

if "G" not in kwargs.keys(): # if outgrid is unset, output to tempfile
kwargs.update({"G": tmpfile.name})
outgrid = kwargs["G"]
arg_str = " ".join([infile, build_arg_string(kwargs)])

Edit: The original post only mentioned managing the input arguments to call_module, but the same can be applied to the output for the current default behavior of returning a DataArray if outgrid is not set:

if outgrid == tmpfile.name: # if user did not set outgrid, return DataArray
with xr.open_dataarray(outgrid) as dataarray:
result = dataarray.load()
_ = result.gmt # load GMTDataArray accessor information
else:
result = None # if user sets an outgrid, return None

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

Metadata

Metadata

Assignees

Labels

enhancementImproving an existing featuremaintenanceBoring but important stuff for the core devs

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions