From b5ad4056ca0a39f9c98438c9722de0eaf7e0f5e6 Mon Sep 17 00:00:00 2001 From: Wei Ji <23487320+weiji14@users.noreply.github.com> Date: Tue, 7 Sep 2021 12:01:15 +1200 Subject: [PATCH 01/15] Replace spaces in arguments with octal code 040 Modifying build_arg_string function to replace blank space characters with octal code 040, and added a doctest to check various combinations with single and double quotes included. --- pygmt/helpers/utils.py | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/pygmt/helpers/utils.py b/pygmt/helpers/utils.py index 177e3d23f62..fcbaccd3f5d 100644 --- a/pygmt/helpers/utils.py +++ b/pygmt/helpers/utils.py @@ -107,7 +107,7 @@ def dummy_context(arg): def build_arg_string(kwargs): - """ + r""" Transform keyword arguments into a GMT argument string. Make sure all arguments have been previously converted to a string @@ -159,6 +159,15 @@ def build_arg_string(kwargs): ... ) ... ) -BWSen -Bxaf -Byaf -I1/1p,blue -I2/0.25p,blue -JX4i -R1/2/3/4 + >>> print( + ... build_arg_string( + ... dict( + ... B=["af", "WSne+tBlank Space"], + ... F='+t"Empty Spaces"', + ... l="'Void Space'"), + ... ) + ... ) + -BWSne+tBlank\040Space -Baf -F+t"Empty\040\040Spaces" -l'Void\040Space' """ gmt_args = [] # Exclude arguments that are None and False @@ -168,11 +177,13 @@ def build_arg_string(kwargs): for key in filtered_kwargs: if is_nonstr_iter(kwargs[key]): for value in kwargs[key]: - gmt_args.append(f"-{key}{value}") + _value = str(value).replace(" ", r"\040") + gmt_args.append(rf"-{key}{_value}") elif kwargs[key] is True: gmt_args.append(f"-{key}") else: - gmt_args.append(f"-{key}{kwargs[key]}") + _value = str(kwargs[key]).replace(" ", r"\040") + gmt_args.append(rf"-{key}{_value}") return " ".join(sorted(gmt_args)) From 14648c2cdc080d44b078226fa7ac85420ca757b2 Mon Sep 17 00:00:00 2001 From: Wei Ji <23487320+weiji14@users.noreply.github.com> Date: Tue, 7 Sep 2021 12:30:04 +1200 Subject: [PATCH 02/15] Remove workarounds for spaces in fig.subplot's autolabel and title args Supersedes workaround for subplot's autolabel (-A) and title (-T) parameters in a9d167d9ab35a249660e58cfdade2eb88fd930a3, 4126c16160c67f8d2c405e7d5da56145adc989d4, and eadb8470065da5436802bc796e868e1ac28d8ac7. --- pygmt/src/subplot.py | 6 ------ 1 file changed, 6 deletions(-) diff --git a/pygmt/src/subplot.py b/pygmt/src/subplot.py index 1e713afb189..19a9e6ecfd6 100644 --- a/pygmt/src/subplot.py +++ b/pygmt/src/subplot.py @@ -148,10 +148,6 @@ def subplot(self, nrows=1, ncols=1, **kwargs): {XY} """ kwargs = self._preprocess(**kwargs) # pylint: disable=protected-access - # allow for spaces in string without needing double quotes - if isinstance(kwargs.get("A"), str): - kwargs["A"] = f'"{kwargs.get("A")}"' - kwargs["T"] = f'"{kwargs.get("T")}"' if kwargs.get("T") else None if nrows < 1 or ncols < 1: raise GMTInvalidInput("Please ensure that both 'nrows'>=1 and 'ncols'>=1.") @@ -222,8 +218,6 @@ def set_panel(self, panel=None, **kwargs): {V} """ kwargs = self._preprocess(**kwargs) # pylint: disable=protected-access - # allow for spaces in string with needing double quotes - kwargs["A"] = f'"{kwargs.get("A")}"' if kwargs.get("A") is not None else None # convert tuple or list to comma-separated str panel = ",".join(map(str, panel)) if is_nonstr_iter(panel) else panel From 924f439f3f3d90a4f4390bc3884796a315856d11 Mon Sep 17 00:00:00 2001 From: Wei Ji <23487320+weiji14@users.noreply.github.com> Date: Tue, 7 Sep 2021 12:32:12 +1200 Subject: [PATCH 03/15] Remove workaround for spaces in fig.text's -F argument --- pygmt/src/text.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pygmt/src/text.py b/pygmt/src/text.py index e8b042f4537..3eaf3d7ca78 100644 --- a/pygmt/src/text.py +++ b/pygmt/src/text.py @@ -209,7 +209,7 @@ def text_( kwargs["F"] += f"+j{justify}" if isinstance(position, str): - kwargs["F"] += f'+c{position}+t"{text}"' + kwargs["F"] += f"+c{position}+t{text}" extra_arrays = [] # If an array of transparency is given, GMT will read it from From 707745f447f9529f63f36515a9630b7e5abc358f Mon Sep 17 00:00:00 2001 From: Wei Ji <23487320+weiji14@users.noreply.github.com> Date: Tue, 7 Sep 2021 12:34:12 +1200 Subject: [PATCH 04/15] Remove double quotes around legend label test examples --- examples/gallery/embellishments/legend.py | 2 +- pygmt/tests/test_legend.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/examples/gallery/embellishments/legend.py b/examples/gallery/embellishments/legend.py index 65f8c615b00..6463b6ef0d5 100644 --- a/examples/gallery/embellishments/legend.py +++ b/examples/gallery/embellishments/legend.py @@ -19,7 +19,7 @@ pen="faint", label="Apples", ) -fig.plot(data="@Table_5_11.txt", pen="1.5p,gray", label='"My lines"') +fig.plot(data="@Table_5_11.txt", pen="1.5p,gray", label="My lines") fig.plot(data="@Table_5_11.txt", style="t0.15i", color="orange", label="Oranges") fig.legend(position="JTR+jTR+o0.2c", box=True) diff --git a/pygmt/tests/test_legend.py b/pygmt/tests/test_legend.py index 213881ed4dc..bd7274aab10 100644 --- a/pygmt/tests/test_legend.py +++ b/pygmt/tests/test_legend.py @@ -52,7 +52,7 @@ def test_legend_entries(): pen="faint", label="Apples", ) - fig.plot(data="@Table_5_11.txt", pen="1.5p,gray", label='"My lines"') + fig.plot(data="@Table_5_11.txt", pen="1.5p,gray", label="My lines") fig.plot(data="@Table_5_11.txt", style="t0.15i", color="orange", label="Oranges") fig.legend(position="JTR+jTR") From 03e4308665b0969df312fe60f9e6e07d9c391620 Mon Sep 17 00:00:00 2001 From: Wei Ji <23487320+weiji14@users.noreply.github.com> Date: Tue, 7 Sep 2021 12:39:46 +1200 Subject: [PATCH 05/15] Edit test_rose_no_sectors to remove single quotes from title --- pygmt/tests/baseline/test_rose_no_sectors.png.dvc | 4 ++-- pygmt/tests/test_rose.py | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/pygmt/tests/baseline/test_rose_no_sectors.png.dvc b/pygmt/tests/baseline/test_rose_no_sectors.png.dvc index 0eddeaf6fee..9e0184a9caf 100644 --- a/pygmt/tests/baseline/test_rose_no_sectors.png.dvc +++ b/pygmt/tests/baseline/test_rose_no_sectors.png.dvc @@ -1,4 +1,4 @@ outs: -- md5: 8e1c47b1cf6001dad3b3c0875af4562e - size: 150390 +- md5: ce2d5cd1415b7c7bbeea5bf6ff39c480 + size: 150288 path: test_rose_no_sectors.png diff --git a/pygmt/tests/test_rose.py b/pygmt/tests/test_rose.py index 4460c5f6473..8d7188fa0d1 100644 --- a/pygmt/tests/test_rose.py +++ b/pygmt/tests/test_rose.py @@ -152,7 +152,7 @@ def test_rose_no_sectors(data_fractures_compilation): region=[0, 500, 0, 360], diameter="10c", labels="180/0/90/270", - frame=["xg100", "yg45", "+t'Windrose diagram'"], + frame=["xg100", "yg45", "+tWindrose diagram"], pen="1.5p,red3", transparency=40, scale=0.5, From 095449adbae10423e65a2fa790618ebca73dab25 Mon Sep 17 00:00:00 2001 From: Wei Ji <23487320+weiji14@users.noreply.github.com> Date: Tue, 7 Sep 2021 12:55:56 +1200 Subject: [PATCH 06/15] Remove workaround for spaces in fig.psconvert prefix Doesn't work yet, as the filename will contain the 040 octal code, but committing to have the diff available for review. --- pygmt/figure.py | 2 -- pygmt/tests/test_figure.py | 1 + 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/pygmt/figure.py b/pygmt/figure.py index d670a3bae51..767b9aded92 100644 --- a/pygmt/figure.py +++ b/pygmt/figure.py @@ -192,8 +192,6 @@ def psconvert(self, **kwargs): # Default cropping the figure to True if "A" not in kwargs: kwargs["A"] = "" - # allow for spaces in figure name - kwargs["F"] = f'"{kwargs.get("F")}"' if kwargs.get("F") else None with Session() as lib: lib.call_module("psconvert", build_arg_string(kwargs)) diff --git a/pygmt/tests/test_figure.py b/pygmt/tests/test_figure.py index 4ac5d4f0592..b596d2b33d4 100644 --- a/pygmt/tests/test_figure.py +++ b/pygmt/tests/test_figure.py @@ -116,6 +116,7 @@ def test_figure_savefig_filename_with_spaces(): fig.basemap(region=[0, 1, 0, 1], projection="X1c/1c", frame=True) with GMTTempFile(prefix="pygmt-filename with spaces", suffix=".png") as imgfile: fig.savefig(imgfile.name) + assert not r"\040" in os.path.abspath(imgfile.name) assert os.path.exists(imgfile.name) From d81b80b2bcd916fed556a00fd5c83facdc62b811 Mon Sep 17 00:00:00 2001 From: Wei Ji <23487320+weiji14@users.noreply.github.com> Date: Tue, 7 Sep 2021 13:19:27 +1200 Subject: [PATCH 07/15] Format using blackdoc and fix flake8 error --- pygmt/helpers/utils.py | 3 ++- pygmt/tests/test_figure.py | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/pygmt/helpers/utils.py b/pygmt/helpers/utils.py index fcbaccd3f5d..4bffe0a8fc5 100644 --- a/pygmt/helpers/utils.py +++ b/pygmt/helpers/utils.py @@ -164,7 +164,8 @@ def build_arg_string(kwargs): ... dict( ... B=["af", "WSne+tBlank Space"], ... F='+t"Empty Spaces"', - ... l="'Void Space'"), + ... l="'Void Space'", + ... ), ... ) ... ) -BWSne+tBlank\040Space -Baf -F+t"Empty\040\040Spaces" -l'Void\040Space' diff --git a/pygmt/tests/test_figure.py b/pygmt/tests/test_figure.py index b596d2b33d4..8d4feb155b5 100644 --- a/pygmt/tests/test_figure.py +++ b/pygmt/tests/test_figure.py @@ -116,7 +116,7 @@ def test_figure_savefig_filename_with_spaces(): fig.basemap(region=[0, 1, 0, 1], projection="X1c/1c", frame=True) with GMTTempFile(prefix="pygmt-filename with spaces", suffix=".png") as imgfile: fig.savefig(imgfile.name) - assert not r"\040" in os.path.abspath(imgfile.name) + assert r"\040" not in os.path.abspath(imgfile.name) assert os.path.exists(imgfile.name) From c29e63229f6c46aeb47cfe068b9f6c959728f64a Mon Sep 17 00:00:00 2001 From: Wei Ji <23487320+weiji14@users.noreply.github.com> Date: Mon, 10 Jan 2022 10:30:39 -0500 Subject: [PATCH 08/15] Ensure spaces in pygmt.config arguments can work Also added a regression test for FORMAT_DATE_MAP="o dd". --- pygmt/src/config.py | 2 +- .../test_config_format_date_map.png.dvc | 4 ++++ pygmt/tests/test_config.py | 19 +++++++++++++++++++ 3 files changed, 24 insertions(+), 1 deletion(-) create mode 100644 pygmt/tests/baseline/test_config_format_date_map.png.dvc diff --git a/pygmt/src/config.py b/pygmt/src/config.py index 7c347e78758..7e4df67d3be 100644 --- a/pygmt/src/config.py +++ b/pygmt/src/config.py @@ -55,7 +55,7 @@ def __init__(self, **kwargs): self.old_defaults[key] = lib.get_default(key) # call gmt set to change GMT defaults - arg_str = " ".join([f"{key}={value}" for key, value in kwargs.items()]) + arg_str = " ".join([f'{key}="{value}"' for key, value in kwargs.items()]) with Session() as lib: lib.call_module("set", arg_str) diff --git a/pygmt/tests/baseline/test_config_format_date_map.png.dvc b/pygmt/tests/baseline/test_config_format_date_map.png.dvc new file mode 100644 index 00000000000..7bc946d104f --- /dev/null +++ b/pygmt/tests/baseline/test_config_format_date_map.png.dvc @@ -0,0 +1,4 @@ +outs: +- md5: 3619720cdfcd857cbdbb49ed7fe6e930 + size: 1392 + path: test_config_format_date_map.png diff --git a/pygmt/tests/test_config.py b/pygmt/tests/test_config.py index ba39ab53b79..067ee1b7289 100644 --- a/pygmt/tests/test_config.py +++ b/pygmt/tests/test_config.py @@ -64,6 +64,25 @@ def test_config_font_annot(): return fig +@pytest.mark.mpl_image_compare +def test_config_format_date_map(): + """ + Test that setting FORMAT_DATE_MAP config changes how the output date string + is plotted. + + Note the space in 'o dd', this acts as a regression test for + https://github.com/GenericMappingTools/pygmt/issues/247. + """ + fig = Figure() + with config(FORMAT_DATE_MAP="o dd"): + fig.basemap( + region=["1969-7-21T", "1969-7-23T", 0, 1], + projection="X2.5c/0.1c", + frame=["sxa1D", "S"], + ) + return fig + + @pytest.mark.mpl_image_compare def test_config_format_time_map(): """ From 83c8c3b0e8c699cd52e7d539c49dabbbf7bf384c Mon Sep 17 00:00:00 2001 From: Wei Ji <23487320+weiji14@users.noreply.github.com> Date: Wed, 12 Jan 2022 20:59:03 -0500 Subject: [PATCH 09/15] Manually handle prefix -F in psconvert So that fig.savefig won't insert `\040` characters when saving filenames with spaces. Resolves problem mentioned in https://github.com/GenericMappingTools/pygmt/pull/1487/files#r703116544 --- pygmt/figure.py | 7 ++++++- pygmt/tests/test_figure.py | 2 +- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/pygmt/figure.py b/pygmt/figure.py index 621f747127d..5b7a2a3cf3b 100644 --- a/pygmt/figure.py +++ b/pygmt/figure.py @@ -192,8 +192,13 @@ def psconvert(self, **kwargs): # Default cropping the figure to True if "A" not in kwargs: kwargs["A"] = "" + # Manually handle prefix -F argument so spaces aren't converted to \040 + # by build_arg_string function. For more information, see + # https://github.com/GenericMappingTools/pygmt/pull/1487 + prefix = kwargs.pop("F") + with Session() as lib: - lib.call_module("psconvert", build_arg_string(kwargs)) + lib.call_module("psconvert", f'-F"{prefix}" {build_arg_string(kwargs)}') def savefig( self, fname, transparent=False, crop=True, anti_alias=True, show=False, **kwargs diff --git a/pygmt/tests/test_figure.py b/pygmt/tests/test_figure.py index 3c77b146faa..73b907dfee5 100644 --- a/pygmt/tests/test_figure.py +++ b/pygmt/tests/test_figure.py @@ -138,7 +138,7 @@ def test_figure_savefig_filename_with_spaces(): fig = Figure() fig.basemap(region=[0, 1, 0, 1], projection="X1c/1c", frame=True) with GMTTempFile(prefix="pygmt-filename with spaces", suffix=".png") as imgfile: - fig.savefig(imgfile.name) + fig.savefig(fname=imgfile.name) assert r"\040" not in os.path.abspath(imgfile.name) assert os.path.exists(imgfile.name) From 3ec772740823a12f906210edeed5284aa101ff12 Mon Sep 17 00:00:00 2001 From: Wei Ji <23487320+weiji14@users.noreply.github.com> Date: Sun, 6 Mar 2022 17:00:08 -0500 Subject: [PATCH 10/15] Handle PROJ4 strings with spaces Instead of converting spaces to \040 in proj4 strings, just remove them directly. Added parametrized unit tests to basemap and grdproject to check that it works. --- pygmt/helpers/utils.py | 16 ++++++++++++--- .../test_basemap_utm_projection.png.dvc | 4 ++++ pygmt/tests/test_basemap.py | 20 +++++++++++++++++++ pygmt/tests/test_grdproject.py | 11 ++++++++-- 4 files changed, 46 insertions(+), 5 deletions(-) create mode 100644 pygmt/tests/baseline/test_basemap_utm_projection.png.dvc diff --git a/pygmt/helpers/utils.py b/pygmt/helpers/utils.py index 610a4e7d787..f0c7caee01e 100644 --- a/pygmt/helpers/utils.py +++ b/pygmt/helpers/utils.py @@ -131,6 +131,11 @@ def build_arg_string(kwargs): same command line argument. For example, the kwargs entry ``'B': ['xa', 'yaf']`` will be converted to ``-Bxa -Byaf`` in the argument string. + Note that spaces ` ` in arguments are converted to the equivalent octal + code `\040`, except in the case of -J (projection) arguments where PROJ4 + strings (e.g. "+proj=longlat +datum=WGS84") will have their spaces removed. + See https://github.com/GenericMappingTools/pygmt/pull/1487 for more info. + Parameters ---------- kwargs : dict @@ -151,7 +156,7 @@ def build_arg_string(kwargs): ... A=True, ... B=False, ... E=200, - ... J="X4c", + ... J="+proj=longlat +datum=WGS84", ... P="", ... R="1/2/3/4", ... X=None, @@ -160,7 +165,7 @@ def build_arg_string(kwargs): ... ) ... ) ... ) - -A -E200 -JX4c -P -R1/2/3/4 -Z0 + -A -E200 -J+proj=longlat+datum=WGS84 -P -R1/2/3/4 -Z0 >>> print( ... build_arg_string( ... dict( @@ -196,7 +201,12 @@ def build_arg_string(kwargs): elif kwargs[key] is True: gmt_args.append(f"-{key}") else: - _value = str(kwargs[key]).replace(" ", r"\040") + if key != "J": # non-projection parameters + _value = str(kwargs[key]).replace(" ", r"\040") + else: + # special handling if key == "J" (projection) + # remove any spaces in PROJ4 string + _value = str(kwargs[key]).replace(" ", "") gmt_args.append(rf"-{key}{_value}") return " ".join(sorted(gmt_args)) diff --git a/pygmt/tests/baseline/test_basemap_utm_projection.png.dvc b/pygmt/tests/baseline/test_basemap_utm_projection.png.dvc new file mode 100644 index 00000000000..c12f0d4026a --- /dev/null +++ b/pygmt/tests/baseline/test_basemap_utm_projection.png.dvc @@ -0,0 +1,4 @@ +outs: +- md5: e6984efed2a94673754cc7f1f1d74832 + size: 9069 + path: test_basemap_utm_projection.png diff --git a/pygmt/tests/test_basemap.py b/pygmt/tests/test_basemap.py index 726661ab165..98a7a1dae4e 100644 --- a/pygmt/tests/test_basemap.py +++ b/pygmt/tests/test_basemap.py @@ -73,6 +73,26 @@ def test_basemap_winkel_tripel(): return fig +@pytest.mark.mpl_image_compare(filename="test_basemap_utm_projection.png") +@pytest.mark.parametrize( + "projection", + [ + "EPSG:32723 +width=5", + "+proj=utm +zone=23 +south +datum=WGS84 +units=m +no_defs +width=5", + ], +) +def test_basemap_utm_projection(projection): + """ + Create a Universal Transverse Mercator (Zone 23S) basemap plot. + + Also check that providing the projection as an EPSG code or PROJ4 string + works. + """ + fig = Figure() + fig.basemap(region=[-52, -50, -12, -11], projection=projection, frame="afg") + return fig + + @pytest.mark.mpl_image_compare def test_basemap_rose(): """ diff --git a/pygmt/tests/test_grdproject.py b/pygmt/tests/test_grdproject.py index 26b2aba86ab..6e90f061c34 100644 --- a/pygmt/tests/test_grdproject.py +++ b/pygmt/tests/test_grdproject.py @@ -58,13 +58,20 @@ def test_grdproject_file_out(grid, expected_grid): xr.testing.assert_allclose(a=temp_grid, b=expected_grid) -def test_grdproject_no_outgrid(grid, expected_grid): +@pytest.mark.parametrize( + "projection", + ["M10c", "EPSG:3395 +width=10", "+proj=merc +ellps=WGS84 +units=m +width=10"], +) +def test_grdproject_no_outgrid(grid, projection, expected_grid): """ Test grdproject with no set outgrid. + + Also check that providing the projection as an EPSG code or PROJ4 string + works. """ assert grid.gmt.gtype == 1 # Geographic grid result = grdproject( - grid=grid, projection="M10c", spacing=3, region=[-53, -51, -20, -17] + grid=grid, projection=projection, spacing=3, region=[-53, -51, -20, -17] ) assert result.gmt.gtype == 0 # Rectangular grid assert result.gmt.registration == 1 # Pixel registration From f19c41b49b49af481b3bf8dd2416b0616327f117 Mon Sep 17 00:00:00 2001 From: Wei Ji <23487320+weiji14@users.noreply.github.com> Date: Sun, 6 Mar 2022 17:38:09 -0500 Subject: [PATCH 11/15] Use Modifier Letter Colon instead of regular colon to fix WIndows tests Adapted from https://stackoverflow.com/questions/10386344/how-to-get-a-file-in-windows-with-a-colon-in-the-filename/25477235#25477235. --- pygmt/tests/test_basemap.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/pygmt/tests/test_basemap.py b/pygmt/tests/test_basemap.py index 98a7a1dae4e..de8a9fb03e3 100644 --- a/pygmt/tests/test_basemap.py +++ b/pygmt/tests/test_basemap.py @@ -77,7 +77,7 @@ def test_basemap_winkel_tripel(): @pytest.mark.parametrize( "projection", [ - "EPSG:32723 +width=5", + "EPSG꞉32723 +width=5", "+proj=utm +zone=23 +south +datum=WGS84 +units=m +no_defs +width=5", ], ) @@ -88,6 +88,9 @@ def test_basemap_utm_projection(projection): Also check that providing the projection as an EPSG code or PROJ4 string works. """ + projection = projection.replace( + "EPSG꞉", "EPSG:" # workaround Windows not allowing colons in filenames + ) fig = Figure() fig.basemap(region=[-52, -50, -12, -11], projection=projection, frame="afg") return fig From 9774d5e1c8bd8bcb9d6034f3d9ad96810372f6ce Mon Sep 17 00:00:00 2001 From: Wei Ji <23487320+weiji14@users.noreply.github.com> Date: Sun, 6 Mar 2022 20:02:44 -0500 Subject: [PATCH 12/15] Try using underscore instead of Modifier Letter Colon --- pygmt/tests/test_basemap.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pygmt/tests/test_basemap.py b/pygmt/tests/test_basemap.py index de8a9fb03e3..7c05e84973a 100644 --- a/pygmt/tests/test_basemap.py +++ b/pygmt/tests/test_basemap.py @@ -77,7 +77,7 @@ def test_basemap_winkel_tripel(): @pytest.mark.parametrize( "projection", [ - "EPSG꞉32723 +width=5", + "EPSG_32723 +width=5", "+proj=utm +zone=23 +south +datum=WGS84 +units=m +no_defs +width=5", ], ) @@ -89,7 +89,7 @@ def test_basemap_utm_projection(projection): works. """ projection = projection.replace( - "EPSG꞉", "EPSG:" # workaround Windows not allowing colons in filenames + "EPSG_", "EPSG:" # workaround Windows not allowing colons in filenames ) fig = Figure() fig.basemap(region=[-52, -50, -12, -11], projection=projection, frame="afg") From ff40d27a0482503d2e3c474b9c851d7934331e3e Mon Sep 17 00:00:00 2001 From: Wei Ji <23487320+weiji14@users.noreply.github.com> Date: Thu, 10 Mar 2022 12:08:59 -0500 Subject: [PATCH 13/15] Refactor the whole function using a single loop Co-Authored-By: Dongdong Tian --- pygmt/helpers/utils.py | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/pygmt/helpers/utils.py b/pygmt/helpers/utils.py index f0c7caee01e..258c97c087a 100644 --- a/pygmt/helpers/utils.py +++ b/pygmt/helpers/utils.py @@ -189,12 +189,11 @@ def build_arg_string(kwargs): -BWSne+tBlank\040Space -Baf -F+t"Empty\040\040Spaces" -l'Void\040Space' """ gmt_args = [] - # Exclude arguments that are None and False - filtered_kwargs = { - k: v for k, v in kwargs.items() if (v is not None and v is not False) - } - for key in filtered_kwargs: - if is_nonstr_iter(kwargs[key]): + + for key in kwargs: + if kwargs[key] is None or kwargs[key] is False: + pass # Exclude arguments that are None and False + elif is_nonstr_iter(kwargs[key]): for value in kwargs[key]: _value = str(value).replace(" ", r"\040") gmt_args.append(rf"-{key}{_value}") From 801ba01b3c83a134f6dd15f756223f8e3663513a Mon Sep 17 00:00:00 2001 From: Wei Ji <23487320+weiji14@users.noreply.github.com> Date: Sat, 12 Mar 2022 20:57:33 -0500 Subject: [PATCH 14/15] Raise GMTInvalidInput if no prefix argument is passed to psconvert --- pygmt/figure.py | 7 +++++-- pygmt/tests/test_psconvert.py | 11 +++++++++++ 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/pygmt/figure.py b/pygmt/figure.py index 9739826f8aa..b922705d752 100644 --- a/pygmt/figure.py +++ b/pygmt/figure.py @@ -239,10 +239,13 @@ def psconvert(self, icc_gray=False, **kwargs): # Manually handle prefix -F argument so spaces aren't converted to \040 # by build_arg_string function. For more information, see # https://github.com/GenericMappingTools/pygmt/pull/1487 - prefix = kwargs.pop("F") + try: + prefix_arg = f'-F"{kwargs.pop("F")}"' + except KeyError as err: + raise GMTInvalidInput("The 'prefix' must be specified.") from err with Session() as lib: - lib.call_module("psconvert", f'-F"{prefix}" {build_arg_string(kwargs)}') + lib.call_module("psconvert", f"{prefix_arg} {build_arg_string(kwargs)}") def savefig( self, fname, transparent=False, crop=True, anti_alias=True, show=False, **kwargs diff --git a/pygmt/tests/test_psconvert.py b/pygmt/tests/test_psconvert.py index af610cf86bb..a18b14883f2 100644 --- a/pygmt/tests/test_psconvert.py +++ b/pygmt/tests/test_psconvert.py @@ -3,7 +3,9 @@ """ import os +import pytest from pygmt import Figure +from pygmt.exceptions import GMTInvalidInput def test_psconvert(): @@ -36,3 +38,12 @@ def test_psconvert_twice(): fname = prefix + ".png" assert os.path.exists(fname) os.remove(fname) + + +def test_psconvert_without_prefix(): + """ + Call psconvert without the 'prefix' option. + """ + fig = Figure() + with pytest.raises(GMTInvalidInput): + fig.psconvert(fmt="g") From a526d45c3cbe96cc78aaaf16bd94e3f9622d9368 Mon Sep 17 00:00:00 2001 From: Wei Ji <23487320+weiji14@users.noreply.github.com> Date: Sun, 13 Mar 2022 09:54:57 -0400 Subject: [PATCH 15/15] Fix rst syntax in pygmt/helpers/utils.py Co-Authored-By: Dongdong Tian --- pygmt/helpers/utils.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pygmt/helpers/utils.py b/pygmt/helpers/utils.py index 4617602770e..ea7a89873e7 100644 --- a/pygmt/helpers/utils.py +++ b/pygmt/helpers/utils.py @@ -131,8 +131,8 @@ def build_arg_string(kwargs): same command line argument. For example, the kwargs entry ``'B': ['xa', 'yaf']`` will be converted to ``-Bxa -Byaf`` in the argument string. - Note that spaces ` ` in arguments are converted to the equivalent octal - code `\040`, except in the case of -J (projection) arguments where PROJ4 + Note that spaces `` `` in arguments are converted to the equivalent octal + code ``\040``, except in the case of -J (projection) arguments where PROJ4 strings (e.g. "+proj=longlat +datum=WGS84") will have their spaces removed. See https://github.com/GenericMappingTools/pygmt/pull/1487 for more info.