Skip to content

Figure.psconvert: Refactor the dpi/gs_path/gs_option/prefix parameters using the new alias system #4056

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Aug 20, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 39 additions & 31 deletions pygmt/src/psconvert.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,25 @@
psconvert - Convert [E]PS file(s) to other formats using Ghostscript.
"""

from collections.abc import Sequence
from pathlib import Path

from pygmt.alias import Alias, AliasSystem
from pygmt.clib import Session
from pygmt.exceptions import GMTValueError
from pygmt.helpers import build_arg_list, fmt_docstring, kwargs_to_strings, use_alias
from pygmt.helpers import build_arg_list, fmt_docstring, use_alias


@fmt_docstring
@use_alias(
A="crop",
C="gs_option",
E="dpi",
F="prefix",
G="gs_path",
I="resize",
N="bb_style",
T="fmt",
Q="anti_aliasing",
V="verbose",
)
@kwargs_to_strings()
def psconvert(self, **kwargs):
@use_alias(A="crop", I="resize", N="bb_style", T="fmt", Q="anti_aliasing", V="verbose")
def psconvert(
self,
prefix: str | None = None,
dpi: int | None = None,
gs_option: str | Sequence[str] | None = None,
gs_path: str | None = None,
**kwargs,
):
r"""
Convert [E]PS file(s) to other formats using Ghostscript.

Expand All @@ -37,6 +34,10 @@ def psconvert(self, **kwargs):
Full GMT docs at :gmt-docs:`psconvert.html`.

{aliases}
- C = gs_option
- E = dpi
- F = prefix
- G = gs_path

Parameters
----------
Expand All @@ -49,19 +50,20 @@ def psconvert(self, **kwargs):
creating very small images where the difference of one pixel
might matter. If ``verbose`` is used we also report the
dimensions of the final illustration.
gs_path : str
gs_path
Full path to the Ghostscript executable.
gs_option : str
Specify a single, custom option that will be passed on to
Ghostscript as is.
dpi : int
Set raster resolution in dpi. Default is 720 for PDF, 300 for
others.
prefix : str
Force the output file name. By default output names are constructed
using the input names as base, which are appended with an
appropriate extension. Use this option to provide a different name,
but without extension. Extension is still determined automatically.
gs_option
Specify one or a list of custom options that will be passed on to Ghostscript
as is.
dpi
Set raster resolution in dpi [Default is 720 for PDF, 300 for others]. **Note**:
Ghostscript limits the final width and height pixel dimensions of a raster file
to be less than or equal to 65536.
prefix
Force the output file name. By default output names are constructed using the
input names as base, which are appended with an appropriate extension. Use this
parameter to provide a different name, but without extension. Extension is still
determined automatically.
resize : str
[**+m**\ *margins*][**+s**\ [**m**]\ *width*\
[/\ *height*]][**+S**\ *scale*].
Expand Down Expand Up @@ -113,7 +115,6 @@ def psconvert(self, **kwargs):
if kwargs.get("A") is None:
kwargs["A"] = ""

prefix = kwargs.get("F")
if prefix in {"", None, False, True}:
raise GMTValueError(
prefix,
Expand All @@ -122,10 +123,17 @@ def psconvert(self, **kwargs):
)

# Check if the parent directory exists
prefix_path = Path(prefix).parent
prefix_path = Path(prefix).parent # type: ignore[arg-type]
if not prefix_path.exists():
msg = f"No such directory: '{prefix_path}', please create it first."
msg = f"No such directory: {prefix_path!r}, please create it first."
raise FileNotFoundError(msg)

aliasdict = AliasSystem(
C=Alias(gs_option, name="gs_option"),
E=Alias(dpi, name="dpi"),
F=Alias(prefix, name="prefix"),
G=Alias(gs_path, name="gs_path"),
).merge(kwargs)

with Session() as lib:
lib.call_module(module="psconvert", args=build_arg_list(kwargs))
lib.call_module(module="psconvert", args=build_arg_list(aliasdict))
Loading