From 27f5d20b85cd1bbfd237ef8e7e0455d07a6efb4d Mon Sep 17 00:00:00 2001 From: Will Schlitzer Date: Fri, 11 Dec 2020 09:59:50 +0000 Subject: [PATCH 01/56] Add cyl_transverse_mercator.py --- .../cyl/cyl_transverse_mercator.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 examples/projections/cyl/cyl_transverse_mercator.py diff --git a/examples/projections/cyl/cyl_transverse_mercator.py b/examples/projections/cyl/cyl_transverse_mercator.py new file mode 100644 index 00000000000..fc8bb2198b9 --- /dev/null +++ b/examples/projections/cyl/cyl_transverse_mercator.py @@ -0,0 +1,19 @@ +""" +Transverse Mercator +=================== + +``T[lon0/][lat0/]width``: Give central meridian ``lon0``, the latitude of the +origin ``lat0`` (optional), and the figure width. +""" +import pygmt + +fig = pygmt.Figure() +fig.coast( + region=[20, 50, 30, 45], + projection="T35/12c", + land="lightbrown", + water="seashell", + shorelines="thinnest", + frame="afg", +) +fig.show() From d64983ff1ae3e046ba76697f8980e12c4600475c Mon Sep 17 00:00:00 2001 From: Will Schlitzer Date: Fri, 11 Dec 2020 10:40:06 +0000 Subject: [PATCH 02/56] Add cyl_universal_transverse_mercator.py --- .../cyl/cyl_universal_transverse_mercator.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 examples/projections/cyl/cyl_universal_transverse_mercator.py diff --git a/examples/projections/cyl/cyl_universal_transverse_mercator.py b/examples/projections/cyl/cyl_universal_transverse_mercator.py new file mode 100644 index 00000000000..47259c5bd6c --- /dev/null +++ b/examples/projections/cyl/cyl_universal_transverse_mercator.py @@ -0,0 +1,19 @@ +""" +Universal Transverse Mercator +============================= + +``U[UTM Zone/][lat0/]width``: Give UTM Zone ``UTM Zone``, and the figure width. +""" +import pygmt + +fig = pygmt.Figure() +# UTM Zone is set to 52R +fig.coast( + region=[127.5, 128.5, 26, 27], + projection="U52R/12c", + land="lightgreen", + water="lightblue", + shorelines="thinnest", + frame="afg", +) +fig.show() From 6ba8185f68bc7dbc76a2787540083287204a7ea4 Mon Sep 17 00:00:00 2001 From: Will Schlitzer Date: Fri, 11 Dec 2020 10:48:29 +0000 Subject: [PATCH 03/56] Changing land color to red in cyl_mercator.py to match GMT docs example; changing units from US to SI --- examples/projections/cyl/cyl_mercator.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/projections/cyl/cyl_mercator.py b/examples/projections/cyl/cyl_mercator.py index d2e54886e6f..bfe22ba87a9 100644 --- a/examples/projections/cyl/cyl_mercator.py +++ b/examples/projections/cyl/cyl_mercator.py @@ -8,5 +8,5 @@ import pygmt fig = pygmt.Figure() -fig.coast(region=[0, 360, -80, 80], frame="afg", land="gray", projection="M0/0/8i") +fig.coast(region=[0, 360, -80, 80], frame="afg", land="red", projection="M0/0/12c") fig.show() From d0d697e9546b5ed2275654f9a41d4cb521e791d4 Mon Sep 17 00:00:00 2001 From: Will Schlitzer Date: Sat, 23 Jan 2021 11:08:06 +0000 Subject: [PATCH 04/56] Wrap grd2cpt and add it to pygmt init file --- pygmt/__init__.py | 2 +- pygmt/gridops.py | 51 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+), 1 deletion(-) diff --git a/pygmt/__init__.py b/pygmt/__init__.py index a565677f6c2..34b53e0d920 100644 --- a/pygmt/__init__.py +++ b/pygmt/__init__.py @@ -16,7 +16,7 @@ from pygmt.figure import Figure from pygmt.filtering import blockmedian from pygmt.gridding import surface -from pygmt.gridops import grdcut, grdfilter +from pygmt.gridops import grd2cpt, grdcut, grdfilter from pygmt.mathops import makecpt from pygmt.modules import GMTDataArrayAccessor, config, grdinfo, info, which from pygmt.sampling import grdtrack diff --git a/pygmt/gridops.py b/pygmt/gridops.py index 697eccc3fe8..a45d3438dc9 100644 --- a/pygmt/gridops.py +++ b/pygmt/gridops.py @@ -254,3 +254,54 @@ def grdfilter(grid, **kwargs): result = None # if user sets an outgrid, return None return result + + +@fmt_docstring +@use_alias( + A="transparency", + C="cmap", + D="background", + F="color_model", + G="truncate", + H="output", + I="reverse", + L="limit", + M="overrule_bg", + N="no_bg", + Q="log", + R="region", + T="series", + V="verbose", + W="categorical", + Ww="cyclic", + Z="continuous", +) +@kwargs_to_strings(T="sequence", G="sequence", L="sequence") +def grd2cpt(grid, **kwargs): + """ + Function to create cpt from grd input. + """ + kind = data_kind(grid) + + with GMTTempFile(suffix=".nc"): + with Session() as lib: + if kind == "file": + file_context = dummy_context(grid) + elif kind == "grid": + file_context = lib.virtualfile_from_grid(grid) + else: + raise GMTInvalidInput("Unrecognized data type: {}".format(type(grid))) + + with file_context as infile: + if "W" in kwargs and "Ww" in kwargs: + raise GMTInvalidInput( + "Set only categorical or cyclic to True, not both." + ) + if "H" not in kwargs.keys(): # if no output is set + arg_str = build_arg_string(kwargs) + elif "H" in kwargs.keys(): # if output is set + outfile = kwargs.pop("H") + if not outfile or not isinstance(outfile, str): + raise GMTInvalidInput("'output' should be a proper file name.") + arg_str = " ".join([infile, build_arg_string(kwargs)]) + lib.call_module("grd2cpt", arg_str) From cb77714b89abb431e71e0871aa5effb38920ca2a Mon Sep 17 00:00:00 2001 From: Will Schlitzer Date: Sat, 23 Jan 2021 11:20:03 +0000 Subject: [PATCH 05/56] Add grd2cpt doc string --- pygmt/gridops.py | 125 +++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 121 insertions(+), 4 deletions(-) diff --git a/pygmt/gridops.py b/pygmt/gridops.py index a45d3438dc9..9ad7b6f7335 100644 --- a/pygmt/gridops.py +++ b/pygmt/gridops.py @@ -279,7 +279,125 @@ def grdfilter(grid, **kwargs): @kwargs_to_strings(T="sequence", G="sequence", L="sequence") def grd2cpt(grid, **kwargs): """ - Function to create cpt from grd input. + Make GMT color palette tables from a grid file. + + This is a module that will help you make static color palette tables + (CPTs). By default, the CPT will simply be saved to the current session, + but you can use *output* to save it to a file. You define an equidistant + set of contour intervals or pass your own z-table or list, and create a new + CPT based on an existing master (dynamic) CPT. The resulting CPT can be + reversed relative to the master cpt, and can be made continuous or + discrete. For color tables beyond the standard GMT offerings, visit + `cpt-city `_ and + `Scientific Colour-Maps `_. + + The CPT includes three additional colors beyond the range of z-values. + These are the background color (B) assigned to values lower than the lowest + *z*-value, the foreground color (F) assigned to values higher than the + highest *z*-value, and the NaN color (N) painted wherever values are + undefined. + + If the master CPT includes B, F, and N entries, these will be copied into + the new master file. If not, the parameters :gmt-term:`COLOR_BACKGROUND`, + :gmt-term:`COLOR_FOREGROUND`, and :gmt-term:`COLOR_NAN` from the + :gmt-docs:`gmt.conf ` file or the command line will be used. This + default behavior can be overruled using the options *background*, + *overrule_bg* or *no_bg*. + + The color model (RGB, HSV or CMYK) of the palette created by **grdcpt** + will be the same as specified in the header of the master CPT. When there + is no :gmt-term:`COLOR_MODEL` entry in the master CPT, the + :gmt-term:`COLOR_MODEL` specified in the :gmt-docs:`gmt.conf ` + file or on the command line will be used. + + Full option list at :gmt-docs:`grd2cpt.html` + + {aliases} + + Parameters + ---------- + grid : str or xarray.DataArray + The file name of the input grid or the grid loaded as a DataArray. + transparency : str + Sets a constant level of transparency (0-100) for all color slices. + Append **+a** to also affect the fore-, back-, and nan-colors + [Default is no transparency, i.e., 0 (opaque)]. + cmap : str + Selects the master color palette table (CPT) to use in the + interpolation. Full list of built-in color palette tables can be found + at :gmt-docs:`cookbook/cpts.html#built-in-color-palette-tables-cpt`. + background : bool or str + Select the back- and foreground colors to match the colors for lowest + and highest *z*-values in the output CPT [Default (``background=True`` + or ``background='o'``) uses the colors specified in the master file, or + those defined by the parameters :gmt-term:`COLOR_BACKGROUND`, + :gmt-term:`COLOR_FOREGROUND`, and :gmt-term:`COLOR_NAN`]. Use + ``background='i'`` to match the colors for the lowest and highest + values in the input (instead of the output) CPT. + color_model : + ``[R|r|h|c][+c[label]]``. + Force output CPT to be written with r/g/b codes, gray-scale values or + color name (**R**, default) or r/g/b codes only (**r**), or h-s-v codes + (**h**), or c/m/y/k codes (**c**). Optionally or alternatively, append + **+c** to write discrete palettes in categorical format. If *label* is + appended then we create labels for each category to be used when the + CPT is plotted. The *label* may be a comma-separated list of category + names (you can skip a category by not giving a name), or give + *start*[-], where we automatically build monotonically increasing + labels from *start* (a single letter or an integer). Append - to build + ranges *start*-*start+1* instead. + series : list or str + ``[min/max/inc[+b|l|n]|file|list]``. + Defines the range of the new CPT by giving the lowest and highest + z-value (and optionally an interval). If this is not given, the + existing range in the master CPT will be used intact. The values + produced defines the color slice boundaries. If **+n** is used it + refers to the number of such boundaries and not the number of slices. + For details on array creation, see + :gmt-docs:`makecpt.html#generate-1d-array`. + truncate : list or str + ``zlo/zhi``. + Truncate the incoming CPT so that the lowest and highest z-levels are + to *zlo* and *zhi*. If one of these equal NaN then we leave that end of + the CPT alone. The truncation takes place before any resampling. See + also :gmt-docs:`cookbook/features.html#manipulating-cpts`. + output : str + Optional. The file name with extension .cpt to store the generated CPT + file. If not given or False (default), saves the CPT as the session + current CPT. + reverse : str + Set this to True or c [Default] to reverse the sense of color + progression in the master CPT. Set this to z to reverse the sign of + z-values in the color table. Note that this change of z-direction + happens before *truncate* and *series* values are used so the latter + must be compatible with the changed *z*-range. See also + :gmt-docs:`cookbook/features.html#manipulating-cpts`. + overrule_bg : + Overrule background, foreground, and NaN colors specified in the master + CPT with the values of the parameters :gmt-term:`COLOR_BACKGROUND`, + :gmt-term:`COLOR_FOREGROUND`, and :gmt-term:`COLOR_NAN` specified in + the :gmt-docs:`gmt.conf ` file or on the command line. When + combined with **background**, only :gmt-term:`COLOR_NAN` is considered. + no_bg : bool + Do not write out the background, foreground, and NaN-color fields + [Default will write them, i.e. ``no_bg=False``]. + log : bool + For logarithmic interpolation scheme with input given as logarithms. + Expects input z-values provided via **series** to be log10(*z*), + assigns colors, and writes out *z*. + continuous : bool + Force a continuous CPT when building from a list of colors and a list + of z-values [Default is None, i.e. discrete values]. + {V} + categorical : bool + Do not interpolate the input color table but pick the output colors + starting at the beginning of the color table, until colors for all + intervals are assigned. This is particularly useful in combination with + a categorical color table, like ``cmap='categorical'``. + cyclic : bool + Produce a wrapped (cyclic) color table that endlessly repeats its + range. Note that ``cyclic=True`` cannot be set together with + ``categorical=True``. """ kind = data_kind(grid) @@ -297,9 +415,8 @@ def grd2cpt(grid, **kwargs): raise GMTInvalidInput( "Set only categorical or cyclic to True, not both." ) - if "H" not in kwargs.keys(): # if no output is set - arg_str = build_arg_string(kwargs) - elif "H" in kwargs.keys(): # if output is set + + if "H" in kwargs.keys(): # if output file is set outfile = kwargs.pop("H") if not outfile or not isinstance(outfile, str): raise GMTInvalidInput("'output' should be a proper file name.") From e6753b8cbdd4141327ea678b9dee7f1abeb079cc Mon Sep 17 00:00:00 2001 From: Will Schlitzer Date: Sat, 23 Jan 2021 13:36:05 +0000 Subject: [PATCH 06/56] Update api/index.rst --- doc/api/index.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/doc/api/index.rst b/doc/api/index.rst index 0598fd103f8..0e35e92d0a3 100644 --- a/doc/api/index.rst +++ b/doc/api/index.rst @@ -74,6 +74,7 @@ Operations on grids: grdcut grdfilter grdtrack + grd2cpt Crossover analysis with x2sys: From be38238ed2f06afcbf1d1caaed02178ac1283bf5 Mon Sep 17 00:00:00 2001 From: Will Schlitzer Date: Sat, 23 Jan 2021 14:12:16 +0000 Subject: [PATCH 07/56] Write first test in test_grd2cpt.py --- pygmt/tests/test_grd2cpt.py | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 pygmt/tests/test_grd2cpt.py diff --git a/pygmt/tests/test_grd2cpt.py b/pygmt/tests/test_grd2cpt.py new file mode 100644 index 00000000000..74139f60972 --- /dev/null +++ b/pygmt/tests/test_grd2cpt.py @@ -0,0 +1,35 @@ +""" +Tests for grd2cpt. +""" +import os + +import numpy as np +import pytest +from pygmt import Figure, makecpt, grd2cpt +from pygmt.datasets import load_earth_relief +from pygmt.exceptions import GMTInvalidInput +from pygmt.helpers import GMTTempFile +from pygmt.helpers.testing import check_figures_equal + + +@pytest.fixture(scope="module", name="grid") +def fixture_grid(): + """ + Load the grid data from the sample earth_relief file. + """ + return load_earth_relief() + +@check_figures_equal() +def test_grd2cpt(grid): + """ + Test the basic function of grd2cpt to create a CPT based off a grid input. + """ + fig_ref, fig_test = Figure(), Figure() + fig_ref.basemap(R="0/10/0/10", J="X15c", B="a") + grd2cpt(grid=grid) + fig_ref.colorbar(frame="a2000") + fig_test.basemap(region=[0, 10, 0, 10], projection="X15c", frame="a") + grd2cpt(grid=grid) + fig_test.colorbar(frame="a2000") + return fig_ref, fig_test + From 5047a245f6bf97412d85d9d5be6d1566c451e266 Mon Sep 17 00:00:00 2001 From: Will Schlitzer Date: Sat, 23 Jan 2021 14:41:54 +0000 Subject: [PATCH 08/56] Add tests for invalid and blank outputs --- pygmt/tests/test_grd2cpt.py | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/pygmt/tests/test_grd2cpt.py b/pygmt/tests/test_grd2cpt.py index 74139f60972..d9fe8eb9b8e 100644 --- a/pygmt/tests/test_grd2cpt.py +++ b/pygmt/tests/test_grd2cpt.py @@ -5,7 +5,7 @@ import numpy as np import pytest -from pygmt import Figure, makecpt, grd2cpt +from pygmt import Figure, grd2cpt from pygmt.datasets import load_earth_relief from pygmt.exceptions import GMTInvalidInput from pygmt.helpers import GMTTempFile @@ -33,3 +33,18 @@ def test_grd2cpt(grid): fig_test.colorbar(frame="a2000") return fig_ref, fig_test +def test_grd2cpt_blank_output(grid): + """ + Use incorrect setting by passing in blank file name to output parameter. + """ + with pytest.raises(GMTInvalidInput): + grd2cpt(grid=grid, output="") + + +def test_grd2cpt_invalid_output(grid): + """ + Use incorrect setting by passing in invalid type to output parameter. + """ + with pytest.raises(GMTInvalidInput): + grd2cpt(grid=grid, output=["some.cpt"]) + From dbf04d717eb25d6b9adb43a071f14970f97fe5fa Mon Sep 17 00:00:00 2001 From: Will Schlitzer Date: Sat, 23 Jan 2021 14:59:21 +0000 Subject: [PATCH 09/56] Add test_grd2cpt_to_plot_points --- pygmt/tests/test_grd2cpt.py | 59 ++++++++++++++++++++++++++++++++++++- 1 file changed, 58 insertions(+), 1 deletion(-) diff --git a/pygmt/tests/test_grd2cpt.py b/pygmt/tests/test_grd2cpt.py index d9fe8eb9b8e..f7bf10def0c 100644 --- a/pygmt/tests/test_grd2cpt.py +++ b/pygmt/tests/test_grd2cpt.py @@ -12,12 +12,32 @@ from pygmt.helpers.testing import check_figures_equal +TEST_DATA_DIR = os.path.join(os.path.dirname(__file__), "data") +POINTS_DATA = os.path.join(TEST_DATA_DIR, "points.txt") + + +@pytest.fixture(scope="module", name="points") +def fixture_points(): + """ + Load the points data from the test file. + """ + return np.loadtxt(POINTS_DATA) + + +@pytest.fixture(scope="module", name="region") +def fixture_region(): + """ + The data region. + """ + return [10, 70, -5, 10] + + @pytest.fixture(scope="module", name="grid") def fixture_grid(): """ Load the grid data from the sample earth_relief file. """ - return load_earth_relief() + return load_earth_relief(registration="gridline") @check_figures_equal() def test_grd2cpt(grid): @@ -33,6 +53,34 @@ def test_grd2cpt(grid): fig_test.colorbar(frame="a2000") return fig_ref, fig_test +@check_figures_equal() +def test_grd2cpt_to_plot_points(points, region, grid): + """ + Use color palette table to change color of points. + """ + fig_ref, fig_test = Figure(), Figure() + fig_ref.basemap(R=region, J="X15c", B="a") + grd2cpt(grid=grid, C="rainbow") + fig_ref.plot( + x=points[:, 0], + y=points[:, 1], + color=points[:, 2], + region=region, + style="c1c", + cmap=True, + ) + fig_test.basemap(region=region, projection="X15c", frame="a") + grd2cpt(grid=grid, cmap="rainbow") + fig_test.plot( + x=points[:, 0], + y=points[:, 1], + color=points[:, 2], + region=region, + style="c1c", + cmap=True, + ) + return fig_ref, fig_test + def test_grd2cpt_blank_output(grid): """ Use incorrect setting by passing in blank file name to output parameter. @@ -48,3 +96,12 @@ def test_grd2cpt_invalid_output(grid): with pytest.raises(GMTInvalidInput): grd2cpt(grid=grid, output=["some.cpt"]) +def test_grd2cpt_output_to_cpt_file(grid): + """ + Save the generated static color palette table to a .cpt file. + """ + with GMTTempFile(suffix=".cpt") as cptfile: + grd2cpt(grid=grid, output=cptfile.name) + assert os.path.exists(cptfile.name) + + From d7b57df859d8a0da943e67a705f7dc6c8e1d0ca4 Mon Sep 17 00:00:00 2001 From: Will Schlitzer Date: Sat, 23 Jan 2021 15:01:01 +0000 Subject: [PATCH 10/56] Fix formatting typos --- pygmt/gridops.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pygmt/gridops.py b/pygmt/gridops.py index 9ad7b6f7335..f592b15692d 100644 --- a/pygmt/gridops.py +++ b/pygmt/gridops.py @@ -377,13 +377,13 @@ def grd2cpt(grid, **kwargs): CPT with the values of the parameters :gmt-term:`COLOR_BACKGROUND`, :gmt-term:`COLOR_FOREGROUND`, and :gmt-term:`COLOR_NAN` specified in the :gmt-docs:`gmt.conf ` file or on the command line. When - combined with **background**, only :gmt-term:`COLOR_NAN` is considered. + combined with ``background``, only :gmt-term:`COLOR_NAN` is considered. no_bg : bool Do not write out the background, foreground, and NaN-color fields [Default will write them, i.e. ``no_bg=False``]. log : bool For logarithmic interpolation scheme with input given as logarithms. - Expects input z-values provided via **series** to be log10(*z*), + Expects input z-values provided via ``series`` to be log10(*z*), assigns colors, and writes out *z*. continuous : bool Force a continuous CPT when building from a list of colors and a list From 89eb39baac77b389779a07e9dfba6f92ade2a594 Mon Sep 17 00:00:00 2001 From: Will Schlitzer Date: Sat, 23 Jan 2021 15:02:17 +0000 Subject: [PATCH 11/56] Move verbose string to end of grd2cpt docstring --- pygmt/gridops.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pygmt/gridops.py b/pygmt/gridops.py index f592b15692d..312ce7823b0 100644 --- a/pygmt/gridops.py +++ b/pygmt/gridops.py @@ -388,7 +388,6 @@ def grd2cpt(grid, **kwargs): continuous : bool Force a continuous CPT when building from a list of colors and a list of z-values [Default is None, i.e. discrete values]. - {V} categorical : bool Do not interpolate the input color table but pick the output colors starting at the beginning of the color table, until colors for all @@ -398,6 +397,7 @@ def grd2cpt(grid, **kwargs): Produce a wrapped (cyclic) color table that endlessly repeats its range. Note that ``cyclic=True`` cannot be set together with ``categorical=True``. + {V} """ kind = data_kind(grid) From 172566719793405eecd743de426d00b20913b50b Mon Sep 17 00:00:00 2001 From: Will Schlitzer Date: Sat, 23 Jan 2021 15:31:21 +0000 Subject: [PATCH 12/56] Update sequences for kwargs_to_strings for grd2cpt --- pygmt/gridops.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/pygmt/gridops.py b/pygmt/gridops.py index 312ce7823b0..f04665bf491 100644 --- a/pygmt/gridops.py +++ b/pygmt/gridops.py @@ -276,7 +276,12 @@ def grdfilter(grid, **kwargs): Ww="cyclic", Z="continuous", ) -@kwargs_to_strings(T="sequence", G="sequence", L="sequence") +@kwargs_to_strings( + T="sequence", + G="sequence", + L="sequence", + R="sequence" +) def grd2cpt(grid, **kwargs): """ Make GMT color palette tables from a grid file. From 12f97e2d08887308128214494027a1ba517df7da Mon Sep 17 00:00:00 2001 From: Will Schlitzer Date: Sat, 23 Jan 2021 15:31:35 +0000 Subject: [PATCH 13/56] Add test_grd2cpt_truncated_to_zlow_zhigh --- pygmt/tests/test_grd2cpt.py | 31 ++++++++++++++++++++++++------- 1 file changed, 24 insertions(+), 7 deletions(-) diff --git a/pygmt/tests/test_grd2cpt.py b/pygmt/tests/test_grd2cpt.py index f7bf10def0c..1408c842050 100644 --- a/pygmt/tests/test_grd2cpt.py +++ b/pygmt/tests/test_grd2cpt.py @@ -11,7 +11,6 @@ from pygmt.helpers import GMTTempFile from pygmt.helpers.testing import check_figures_equal - TEST_DATA_DIR = os.path.join(os.path.dirname(__file__), "data") POINTS_DATA = os.path.join(TEST_DATA_DIR, "points.txt") @@ -29,7 +28,7 @@ def fixture_region(): """ The data region. """ - return [10, 70, -5, 10] + return [0, 10, 0, 10] @pytest.fixture(scope="module", name="grid") @@ -39,27 +38,29 @@ def fixture_grid(): """ return load_earth_relief(registration="gridline") + @check_figures_equal() -def test_grd2cpt(grid): +def test_grd2cpt(grid, region): """ Test the basic function of grd2cpt to create a CPT based off a grid input. """ fig_ref, fig_test = Figure(), Figure() - fig_ref.basemap(R="0/10/0/10", J="X15c", B="a") + fig_ref.basemap(R=region, J="X15c", B="a") grd2cpt(grid=grid) fig_ref.colorbar(frame="a2000") - fig_test.basemap(region=[0, 10, 0, 10], projection="X15c", frame="a") + fig_test.basemap(region=region, projection="X15c", frame="a") grd2cpt(grid=grid) fig_test.colorbar(frame="a2000") return fig_ref, fig_test + @check_figures_equal() def test_grd2cpt_to_plot_points(points, region, grid): """ Use color palette table to change color of points. """ fig_ref, fig_test = Figure(), Figure() - fig_ref.basemap(R=region, J="X15c", B="a") + fig_ref.basemap(R=[10, 70, -5, 10], J="X15c", B="a") grd2cpt(grid=grid, C="rainbow") fig_ref.plot( x=points[:, 0], @@ -69,7 +70,7 @@ def test_grd2cpt_to_plot_points(points, region, grid): style="c1c", cmap=True, ) - fig_test.basemap(region=region, projection="X15c", frame="a") + fig_test.basemap(region=[10, 70, -5, 10], projection="X15c", frame="a") grd2cpt(grid=grid, cmap="rainbow") fig_test.plot( x=points[:, 0], @@ -81,6 +82,7 @@ def test_grd2cpt_to_plot_points(points, region, grid): ) return fig_ref, fig_test + def test_grd2cpt_blank_output(grid): """ Use incorrect setting by passing in blank file name to output parameter. @@ -96,6 +98,7 @@ def test_grd2cpt_invalid_output(grid): with pytest.raises(GMTInvalidInput): grd2cpt(grid=grid, output=["some.cpt"]) + def test_grd2cpt_output_to_cpt_file(grid): """ Save the generated static color palette table to a .cpt file. @@ -105,3 +108,17 @@ def test_grd2cpt_output_to_cpt_file(grid): assert os.path.exists(cptfile.name) +@check_figures_equal() +def test_grd2cpt_truncated_to_zlow_zhigh(grid, region): + """ + Test the basic function of grd2cpt to create a CPT based off a grid input. + """ + fig_ref, fig_test = Figure(), Figure() + fig_ref.basemap(R=region, J="X15c", B="a") + grd2cpt(grid=grid, cmap="rainbow", truncate="0.15/0.85", series="-4500/4500") + fig_ref.colorbar(frame="a2000") + + fig_test.basemap(region=region, projection="X15c", frame="a") + grd2cpt(grid=grid, cmap="rainbow", truncate="0.15/0.85", series="-4500/4500") + fig_test.colorbar(frame="a2000") + return fig_ref, fig_test From b28556ba14e7c6dd3d2c36ba066d8b3f205a4a92 Mon Sep 17 00:00:00 2001 From: Will Schlitzer Date: Sat, 23 Jan 2021 16:00:07 +0000 Subject: [PATCH 14/56] Fix test_grd2cpt_truncated_to_zlow_zhigh --- pygmt/tests/test_grd2cpt.py | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/pygmt/tests/test_grd2cpt.py b/pygmt/tests/test_grd2cpt.py index 1408c842050..2bed068c95f 100644 --- a/pygmt/tests/test_grd2cpt.py +++ b/pygmt/tests/test_grd2cpt.py @@ -109,16 +109,30 @@ def test_grd2cpt_output_to_cpt_file(grid): @check_figures_equal() -def test_grd2cpt_truncated_to_zlow_zhigh(grid, region): +def test_grd2cpt_set_cpt(grid, region): """ - Test the basic function of grd2cpt to create a CPT based off a grid input. + Test function grd2cpt to create a CPT based off a grid input and a set CPT. """ fig_ref, fig_test = Figure(), Figure() fig_ref.basemap(R=region, J="X15c", B="a") - grd2cpt(grid=grid, cmap="rainbow", truncate="0.15/0.85", series="-4500/4500") + grd2cpt(grid=grid, cmap="rainbow") fig_ref.colorbar(frame="a2000") fig_test.basemap(region=region, projection="X15c", frame="a") - grd2cpt(grid=grid, cmap="rainbow", truncate="0.15/0.85", series="-4500/4500") + grd2cpt(grid=grid, cmap="rainbow") fig_test.colorbar(frame="a2000") return fig_ref, fig_test + +@check_figures_equal() +def test_grd2cpt_truncated_to_zlow_zhigh(grid, region): + """ + Test the basic function of grd2cpt to create a CPT based off a grid input. + """ + fig_ref, fig_test = Figure(), Figure() + fig_ref.basemap(region=[0, 10, 0, 10], projection="X15c", frame="a") + grd2cpt(grid=grid, cmap="rainbow", truncate="0.15/0.85", series="-4500/4500/500") + fig_ref.colorbar(frame="a2000") + fig_test.basemap(region=[0, 10, 0, 10], projection="X15c", frame="a") + grd2cpt(grid=grid, cmap="rainbow", truncate="0.15/0.85", series="-4500/4500/500") + fig_test.colorbar(frame="a2000") + return fig_ref, fig_test \ No newline at end of file From 31745151ebc0e3a660219130a1f408d51768b983 Mon Sep 17 00:00:00 2001 From: Will Schlitzer Date: Sat, 23 Jan 2021 16:11:49 +0000 Subject: [PATCH 15/56] Add failing test test_grd2cpt_grdimage --- pygmt/gridops.py | 7 +------ pygmt/tests/test_grd2cpt.py | 18 +++++++++++++++++- 2 files changed, 18 insertions(+), 7 deletions(-) diff --git a/pygmt/gridops.py b/pygmt/gridops.py index f04665bf491..3c5790a5b9d 100644 --- a/pygmt/gridops.py +++ b/pygmt/gridops.py @@ -276,12 +276,7 @@ def grdfilter(grid, **kwargs): Ww="cyclic", Z="continuous", ) -@kwargs_to_strings( - T="sequence", - G="sequence", - L="sequence", - R="sequence" -) +@kwargs_to_strings(T="sequence", G="sequence", L="sequence", R="sequence") def grd2cpt(grid, **kwargs): """ Make GMT color palette tables from a grid file. diff --git a/pygmt/tests/test_grd2cpt.py b/pygmt/tests/test_grd2cpt.py index 2bed068c95f..f9fb3dcd0b6 100644 --- a/pygmt/tests/test_grd2cpt.py +++ b/pygmt/tests/test_grd2cpt.py @@ -54,6 +54,21 @@ def test_grd2cpt(grid, region): return fig_ref, fig_test +@check_figures_equal() +def test_grd2cpt_grdimage(grid, region): + """ + Test creating a CPT with grd2cpt and plot it with grdimage. + """ + fig_ref, fig_test = Figure(), Figure() + grd2cpt(grid=grid, cmap="rainbow", continuous=True) + fig_ref.grdimage(grid, projection="W0/15c") + fig_ref.colorbar(frame="a2000") + grd2cpt(grid=grid, cmap="rainbow", continuous=True) + fig_test.grdimage(grid, projection="W0/15c") + fig_test.colorbar(frame="a2000") + return fig_ref, fig_test + + @check_figures_equal() def test_grd2cpt_to_plot_points(points, region, grid): """ @@ -123,6 +138,7 @@ def test_grd2cpt_set_cpt(grid, region): fig_test.colorbar(frame="a2000") return fig_ref, fig_test + @check_figures_equal() def test_grd2cpt_truncated_to_zlow_zhigh(grid, region): """ @@ -135,4 +151,4 @@ def test_grd2cpt_truncated_to_zlow_zhigh(grid, region): fig_test.basemap(region=[0, 10, 0, 10], projection="X15c", frame="a") grd2cpt(grid=grid, cmap="rainbow", truncate="0.15/0.85", series="-4500/4500/500") fig_test.colorbar(frame="a2000") - return fig_ref, fig_test \ No newline at end of file + return fig_ref, fig_test From 2b72baa95d68010a982b7ef90069eb3d772fa0cb Mon Sep 17 00:00:00 2001 From: Will Schlitzer Date: Sat, 23 Jan 2021 16:34:29 +0000 Subject: [PATCH 16/56] Update test_grd2cpt_grdimage --- pygmt/tests/test_grd2cpt.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/pygmt/tests/test_grd2cpt.py b/pygmt/tests/test_grd2cpt.py index f9fb3dcd0b6..53f8c30f078 100644 --- a/pygmt/tests/test_grd2cpt.py +++ b/pygmt/tests/test_grd2cpt.py @@ -55,16 +55,18 @@ def test_grd2cpt(grid, region): @check_figures_equal() -def test_grd2cpt_grdimage(grid, region): +def test_grd2cpt_grdimage(grid): """ Test creating a CPT with grd2cpt and plot it with grdimage. """ fig_ref, fig_test = Figure(), Figure() + fig_ref.basemap(frame="a", projection="W0/15c", region="d") grd2cpt(grid=grid, cmap="rainbow", continuous=True) - fig_ref.grdimage(grid, projection="W0/15c") + fig_ref.grdimage(grid) fig_ref.colorbar(frame="a2000") + fig_test.basemap(frame="a", projection="W0/15c", region="d") grd2cpt(grid=grid, cmap="rainbow", continuous=True) - fig_test.grdimage(grid, projection="W0/15c") + fig_test.grdimage(grid) fig_test.colorbar(frame="a2000") return fig_ref, fig_test From e02eea3f5fd2bad73fb9680afa13d57cda358e47 Mon Sep 17 00:00:00 2001 From: Will Schlitzer Date: Sat, 23 Jan 2021 16:38:25 +0000 Subject: [PATCH 17/56] Fix style for unused variable --- pygmt/tests/test_grd2cpt.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pygmt/tests/test_grd2cpt.py b/pygmt/tests/test_grd2cpt.py index 53f8c30f078..426eb790b28 100644 --- a/pygmt/tests/test_grd2cpt.py +++ b/pygmt/tests/test_grd2cpt.py @@ -147,10 +147,10 @@ def test_grd2cpt_truncated_to_zlow_zhigh(grid, region): Test the basic function of grd2cpt to create a CPT based off a grid input. """ fig_ref, fig_test = Figure(), Figure() - fig_ref.basemap(region=[0, 10, 0, 10], projection="X15c", frame="a") + fig_ref.basemap(region=region, projection="X15c", frame="a") grd2cpt(grid=grid, cmap="rainbow", truncate="0.15/0.85", series="-4500/4500/500") fig_ref.colorbar(frame="a2000") - fig_test.basemap(region=[0, 10, 0, 10], projection="X15c", frame="a") + fig_test.basemap(region=region, projection="X15c", frame="a") grd2cpt(grid=grid, cmap="rainbow", truncate="0.15/0.85", series="-4500/4500/500") fig_test.colorbar(frame="a2000") return fig_ref, fig_test From cef25c021f702c34fc4b7e5b10fa6f084c0fd9b9 Mon Sep 17 00:00:00 2001 From: Will Schlitzer Date: Sat, 23 Jan 2021 17:07:09 +0000 Subject: [PATCH 18/56] Remove redundant test --- pygmt/tests/test_grd2cpt.py | 75 +++++++++++++++---------------------- 1 file changed, 30 insertions(+), 45 deletions(-) diff --git a/pygmt/tests/test_grd2cpt.py b/pygmt/tests/test_grd2cpt.py index 426eb790b28..77619098169 100644 --- a/pygmt/tests/test_grd2cpt.py +++ b/pygmt/tests/test_grd2cpt.py @@ -28,7 +28,7 @@ def fixture_region(): """ The data region. """ - return [0, 10, 0, 10] + return [10, 70, -5, 10] @pytest.fixture(scope="module", name="grid") @@ -39,25 +39,12 @@ def fixture_grid(): return load_earth_relief(registration="gridline") -@check_figures_equal() -def test_grd2cpt(grid, region): - """ - Test the basic function of grd2cpt to create a CPT based off a grid input. - """ - fig_ref, fig_test = Figure(), Figure() - fig_ref.basemap(R=region, J="X15c", B="a") - grd2cpt(grid=grid) - fig_ref.colorbar(frame="a2000") - fig_test.basemap(region=region, projection="X15c", frame="a") - grd2cpt(grid=grid) - fig_test.colorbar(frame="a2000") - return fig_ref, fig_test - @check_figures_equal() -def test_grd2cpt_grdimage(grid): +def test_grd2cpt(grid): """ - Test creating a CPT with grd2cpt and plot it with grdimage. + Test creating a CPT with grd2cpt to create a CPT based off a grid input + and plot it with grdimage. """ fig_ref, fig_test = Figure(), Figure() fig_ref.basemap(frame="a", projection="W0/15c", region="d") @@ -77,7 +64,7 @@ def test_grd2cpt_to_plot_points(points, region, grid): Use color palette table to change color of points. """ fig_ref, fig_test = Figure(), Figure() - fig_ref.basemap(R=[10, 70, -5, 10], J="X15c", B="a") + fig_ref.basemap(R=region, J="X15c", B="a") grd2cpt(grid=grid, C="rainbow") fig_ref.plot( x=points[:, 0], @@ -87,7 +74,7 @@ def test_grd2cpt_to_plot_points(points, region, grid): style="c1c", cmap=True, ) - fig_test.basemap(region=[10, 70, -5, 10], projection="X15c", frame="a") + fig_test.basemap(region=region, projection="X15c", frame="a") grd2cpt(grid=grid, cmap="rainbow") fig_test.plot( x=points[:, 0], @@ -99,32 +86,6 @@ def test_grd2cpt_to_plot_points(points, region, grid): ) return fig_ref, fig_test - -def test_grd2cpt_blank_output(grid): - """ - Use incorrect setting by passing in blank file name to output parameter. - """ - with pytest.raises(GMTInvalidInput): - grd2cpt(grid=grid, output="") - - -def test_grd2cpt_invalid_output(grid): - """ - Use incorrect setting by passing in invalid type to output parameter. - """ - with pytest.raises(GMTInvalidInput): - grd2cpt(grid=grid, output=["some.cpt"]) - - -def test_grd2cpt_output_to_cpt_file(grid): - """ - Save the generated static color palette table to a .cpt file. - """ - with GMTTempFile(suffix=".cpt") as cptfile: - grd2cpt(grid=grid, output=cptfile.name) - assert os.path.exists(cptfile.name) - - @check_figures_equal() def test_grd2cpt_set_cpt(grid, region): """ @@ -154,3 +115,27 @@ def test_grd2cpt_truncated_to_zlow_zhigh(grid, region): grd2cpt(grid=grid, cmap="rainbow", truncate="0.15/0.85", series="-4500/4500/500") fig_test.colorbar(frame="a2000") return fig_ref, fig_test + +def test_grd2cpt_blank_output(grid): + """ + Use incorrect setting by passing in blank file name to output parameter. + """ + with pytest.raises(GMTInvalidInput): + grd2cpt(grid=grid, output="") + + +def test_grd2cpt_invalid_output(grid): + """ + Use incorrect setting by passing in invalid type to output parameter. + """ + with pytest.raises(GMTInvalidInput): + grd2cpt(grid=grid, output=["some.cpt"]) + + +def test_grd2cpt_output_to_cpt_file(grid): + """ + Save the generated static color palette table to a .cpt file. + """ + with GMTTempFile(suffix=".cpt") as cptfile: + grd2cpt(grid=grid, output=cptfile.name) + assert os.path.exists(cptfile.name) \ No newline at end of file From 77ed1fd332c8444cf1c53cdd0979b3fd942f49e4 Mon Sep 17 00:00:00 2001 From: Will Schlitzer Date: Sat, 23 Jan 2021 17:08:27 +0000 Subject: [PATCH 19/56] Reduce arguments tests in grd2cpt; use single-letter aliases for ref --- pygmt/tests/test_grd2cpt.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/pygmt/tests/test_grd2cpt.py b/pygmt/tests/test_grd2cpt.py index 77619098169..915ea10f9e1 100644 --- a/pygmt/tests/test_grd2cpt.py +++ b/pygmt/tests/test_grd2cpt.py @@ -47,12 +47,12 @@ def test_grd2cpt(grid): and plot it with grdimage. """ fig_ref, fig_test = Figure(), Figure() - fig_ref.basemap(frame="a", projection="W0/15c", region="d") - grd2cpt(grid=grid, cmap="rainbow", continuous=True) + fig_ref.basemap(B="a", J="W0/15c", R="d") + grd2cpt(grid=grid) fig_ref.grdimage(grid) - fig_ref.colorbar(frame="a2000") + fig_ref.colorbar(B="a2000") fig_test.basemap(frame="a", projection="W0/15c", region="d") - grd2cpt(grid=grid, cmap="rainbow", continuous=True) + grd2cpt(grid=grid) fig_test.grdimage(grid) fig_test.colorbar(frame="a2000") return fig_ref, fig_test From 06fc8c6402d4086fe4cd877ccd4342620df5e498 Mon Sep 17 00:00:00 2001 From: Will Schlitzer Date: Sat, 23 Jan 2021 17:10:42 +0000 Subject: [PATCH 20/56] Update tests to use basemap in test_grd2cpt.py --- pygmt/tests/test_grd2cpt.py | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/pygmt/tests/test_grd2cpt.py b/pygmt/tests/test_grd2cpt.py index 915ea10f9e1..f563191c97f 100644 --- a/pygmt/tests/test_grd2cpt.py +++ b/pygmt/tests/test_grd2cpt.py @@ -92,12 +92,13 @@ def test_grd2cpt_set_cpt(grid, region): Test function grd2cpt to create a CPT based off a grid input and a set CPT. """ fig_ref, fig_test = Figure(), Figure() - fig_ref.basemap(R=region, J="X15c", B="a") + fig_ref.basemap(B="a", J="W0/15c", R="d") grd2cpt(grid=grid, cmap="rainbow") - fig_ref.colorbar(frame="a2000") - - fig_test.basemap(region=region, projection="X15c", frame="a") + fig_ref.grdimage(grid) + fig_ref.colorbar(B="a2000") + fig_test.basemap(frame="a", projection="W0/15c", region="d") grd2cpt(grid=grid, cmap="rainbow") + fig_test.grdimage(grid) fig_test.colorbar(frame="a2000") return fig_ref, fig_test @@ -108,11 +109,13 @@ def test_grd2cpt_truncated_to_zlow_zhigh(grid, region): Test the basic function of grd2cpt to create a CPT based off a grid input. """ fig_ref, fig_test = Figure(), Figure() - fig_ref.basemap(region=region, projection="X15c", frame="a") - grd2cpt(grid=grid, cmap="rainbow", truncate="0.15/0.85", series="-4500/4500/500") - fig_ref.colorbar(frame="a2000") - fig_test.basemap(region=region, projection="X15c", frame="a") - grd2cpt(grid=grid, cmap="rainbow", truncate="0.15/0.85", series="-4500/4500/500") + fig_ref.basemap(B="a", J="W0/15c", R="d") + grd2cpt(grid=grid, cmap="rainbow", G="0.15/0.85", T="-4500/4500/500") + fig_ref.grdimage(grid) + fig_ref.colorbar(B="a2000") + fig_test.basemap(frame="a", projection="W0/15c", region="d") + grd2cpt(grid=grid, cmap="rainbow", truncate=[0.15,0.85], series=[-4500,4500,500]) + fig_test.grdimage(grid) fig_test.colorbar(frame="a2000") return fig_ref, fig_test From 683262a83f7a3db61f3fac9cd5c7aa8bd29cbc3e Mon Sep 17 00:00:00 2001 From: Will Schlitzer Date: Sat, 23 Jan 2021 17:18:25 +0000 Subject: [PATCH 21/56] Add test_grd2cpt_unrecognized_data_type and test_grd2cpt_categorical_and_cyclic --- pygmt/tests/test_grd2cpt.py | 28 +++++++++++++++++++++++----- 1 file changed, 23 insertions(+), 5 deletions(-) diff --git a/pygmt/tests/test_grd2cpt.py b/pygmt/tests/test_grd2cpt.py index f563191c97f..550c731ed4a 100644 --- a/pygmt/tests/test_grd2cpt.py +++ b/pygmt/tests/test_grd2cpt.py @@ -39,12 +39,11 @@ def fixture_grid(): return load_earth_relief(registration="gridline") - @check_figures_equal() def test_grd2cpt(grid): """ - Test creating a CPT with grd2cpt to create a CPT based off a grid input - and plot it with grdimage. + Test creating a CPT with grd2cpt to create a CPT based off a grid input and + plot it with grdimage. """ fig_ref, fig_test = Figure(), Figure() fig_ref.basemap(B="a", J="W0/15c", R="d") @@ -86,6 +85,7 @@ def test_grd2cpt_to_plot_points(points, region, grid): ) return fig_ref, fig_test + @check_figures_equal() def test_grd2cpt_set_cpt(grid, region): """ @@ -114,11 +114,12 @@ def test_grd2cpt_truncated_to_zlow_zhigh(grid, region): fig_ref.grdimage(grid) fig_ref.colorbar(B="a2000") fig_test.basemap(frame="a", projection="W0/15c", region="d") - grd2cpt(grid=grid, cmap="rainbow", truncate=[0.15,0.85], series=[-4500,4500,500]) + grd2cpt(grid=grid, cmap="rainbow", truncate=[0.15, 0.85], series=[-4500, 4500, 500]) fig_test.grdimage(grid) fig_test.colorbar(frame="a2000") return fig_ref, fig_test + def test_grd2cpt_blank_output(grid): """ Use incorrect setting by passing in blank file name to output parameter. @@ -141,4 +142,21 @@ def test_grd2cpt_output_to_cpt_file(grid): """ with GMTTempFile(suffix=".cpt") as cptfile: grd2cpt(grid=grid, output=cptfile.name) - assert os.path.exists(cptfile.name) \ No newline at end of file + assert os.path.exists(cptfile.name) + + +def test_grd2cpt_unrecognized_data_type(): + """ + Test that an error will be raised if an invalid data type is passed to + grid. + """ + with pytest.raises(GMTInvalidInput): + grd2cpt(grid=0) + + +def test_grd2cpt_categorical_and_cyclic(grid): + """ + Use incorrect setting by setting both categorical and cyclic to True. + """ + with pytest.raises(GMTInvalidInput): + grd2cpt(grid=grid, cmap="batlow", categorical=True, cyclic=True) From b2ce4c7219c5aec7df3db1e213ef1ce2dc7a3c1d Mon Sep 17 00:00:00 2001 From: Will Schlitzer Date: Sat, 23 Jan 2021 19:24:01 +0000 Subject: [PATCH 22/56] Add test_grd2cpt_scaled_with_series, test_grd2cpt_truncated_to_zlow_only, test_grd2cpt_truncated_to_zhigh_only --- pygmt/tests/test_grd2cpt.py | 79 ++++++++++++++++++++++++++++++------- 1 file changed, 65 insertions(+), 14 deletions(-) diff --git a/pygmt/tests/test_grd2cpt.py b/pygmt/tests/test_grd2cpt.py index 550c731ed4a..9afe4f3b93e 100644 --- a/pygmt/tests/test_grd2cpt.py +++ b/pygmt/tests/test_grd2cpt.py @@ -46,13 +46,14 @@ def test_grd2cpt(grid): plot it with grdimage. """ fig_ref, fig_test = Figure(), Figure() + # Use single-character arguments for the reference image fig_ref.basemap(B="a", J="W0/15c", R="d") grd2cpt(grid=grid) - fig_ref.grdimage(grid) + fig_ref.grdimage(grid=grid) fig_ref.colorbar(B="a2000") fig_test.basemap(frame="a", projection="W0/15c", region="d") grd2cpt(grid=grid) - fig_test.grdimage(grid) + fig_test.grdimage(grid=grid) fig_test.colorbar(frame="a2000") return fig_ref, fig_test @@ -60,18 +61,19 @@ def test_grd2cpt(grid): @check_figures_equal() def test_grd2cpt_to_plot_points(points, region, grid): """ - Use color palette table to change color of points. + Use CPT to change color of points. """ fig_ref, fig_test = Figure(), Figure() + # Use single-character arguments for the reference image fig_ref.basemap(R=region, J="X15c", B="a") grd2cpt(grid=grid, C="rainbow") fig_ref.plot( x=points[:, 0], y=points[:, 1], - color=points[:, 2], - region=region, - style="c1c", - cmap=True, + G=points[:, 2], + R=region, + S="c1c", + C=True, ) fig_test.basemap(region=region, projection="X15c", frame="a") grd2cpt(grid=grid, cmap="rainbow") @@ -87,35 +89,84 @@ def test_grd2cpt_to_plot_points(points, region, grid): @check_figures_equal() -def test_grd2cpt_set_cpt(grid, region): +def test_grd2cpt_set_cpt(grid): """ Test function grd2cpt to create a CPT based off a grid input and a set CPT. """ fig_ref, fig_test = Figure(), Figure() + # Use single-character arguments for the reference image fig_ref.basemap(B="a", J="W0/15c", R="d") grd2cpt(grid=grid, cmap="rainbow") - fig_ref.grdimage(grid) + fig_ref.grdimage(grid=grid) fig_ref.colorbar(B="a2000") fig_test.basemap(frame="a", projection="W0/15c", region="d") grd2cpt(grid=grid, cmap="rainbow") - fig_test.grdimage(grid) + fig_test.grdimage(grid=grid) + fig_test.colorbar(frame="a2000") + return fig_ref, fig_test + +@check_figures_equal() +def test_grd2cpt_scaled_with_series(grid): + """ + Create CPT scaled to a min/max series to change color of grid. + """ + fig_ref, fig_test = Figure(), Figure() + fig_ref.basemap(B="a", J="W0/15c", R="d") + grd2cpt(grid=grid, cmap="rainbow", T="-4500/4500/500") + fig_ref.grdimage(grid=grid) + fig_ref.colorbar(B="a2000") + fig_test.basemap(frame="a", projection="W0/15c", region="d") + grd2cpt(grid=grid, cmap="rainbow", series=[-4500, 4500, 500]) + fig_test.grdimage(grid=grid) fig_test.colorbar(frame="a2000") return fig_ref, fig_test @check_figures_equal() -def test_grd2cpt_truncated_to_zlow_zhigh(grid, region): +def test_grd2cpt_truncated_to_zlow_zhigh(grid): """ - Test the basic function of grd2cpt to create a CPT based off a grid input. + Create CPT that is truncated to z-low and z-high. """ fig_ref, fig_test = Figure(), Figure() fig_ref.basemap(B="a", J="W0/15c", R="d") grd2cpt(grid=grid, cmap="rainbow", G="0.15/0.85", T="-4500/4500/500") - fig_ref.grdimage(grid) + fig_ref.grdimage(grid=grid) fig_ref.colorbar(B="a2000") fig_test.basemap(frame="a", projection="W0/15c", region="d") grd2cpt(grid=grid, cmap="rainbow", truncate=[0.15, 0.85], series=[-4500, 4500, 500]) - fig_test.grdimage(grid) + fig_test.grdimage(grid=grid) + fig_test.colorbar(frame="a2000") + return fig_ref, fig_test + +@check_figures_equal() +def test_grd2cpt_truncated_to_zlow_only(grid): + """ + Create CPT that is truncated at z-low only. + """ + fig_ref, fig_test = Figure(), Figure() + fig_ref.basemap(B="a", J="W0/15c", R="d") + grd2cpt(grid=grid, cmap="rainbow", G="0.5/NaN", T="-4500/4500/500") + fig_ref.grdimage(grid=grid) + fig_ref.colorbar(B="a2000") + fig_test.basemap(frame="a", projection="W0/15c", region="d") + grd2cpt(grid=grid, cmap="rainbow", truncate=[0.5, None], series=[-4500, 4500, 500]) + fig_test.grdimage(grid=grid) + fig_test.colorbar(frame="a2000") + return fig_ref, fig_test + +@check_figures_equal() +def test_grd2cpt_truncated_to_zhigh_only(grid): + """ + Create CPT that is truncated at z-high only. + """ + fig_ref, fig_test = Figure(), Figure() + fig_ref.basemap(B="a", J="W0/15c", R="d") + grd2cpt(grid=grid, cmap="rainbow", G="NaN/0.5", T="-4500/4500/500") + fig_ref.grdimage(grid=grid) + fig_ref.colorbar(B="a2000") + fig_test.basemap(frame="a", projection="W0/15c", region="d") + grd2cpt(grid=grid, cmap="rainbow", truncate=[None, 0.5], series=[-4500, 4500, 500]) + fig_test.grdimage(grid=grid) fig_test.colorbar(frame="a2000") return fig_ref, fig_test From 305fb91f96a30c6216cf4c6c0c8d708a6ea40b7b Mon Sep 17 00:00:00 2001 From: Will Schlitzer Date: Sat, 23 Jan 2021 19:32:07 +0000 Subject: [PATCH 23/56] Add test_grd2cpt_reverse_color_only --- pygmt/tests/test_grd2cpt.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/pygmt/tests/test_grd2cpt.py b/pygmt/tests/test_grd2cpt.py index 9afe4f3b93e..1d5270d6fac 100644 --- a/pygmt/tests/test_grd2cpt.py +++ b/pygmt/tests/test_grd2cpt.py @@ -105,6 +105,7 @@ def test_grd2cpt_set_cpt(grid): fig_test.colorbar(frame="a2000") return fig_ref, fig_test + @check_figures_equal() def test_grd2cpt_scaled_with_series(grid): """ @@ -138,6 +139,7 @@ def test_grd2cpt_truncated_to_zlow_zhigh(grid): fig_test.colorbar(frame="a2000") return fig_ref, fig_test + @check_figures_equal() def test_grd2cpt_truncated_to_zlow_only(grid): """ @@ -154,6 +156,7 @@ def test_grd2cpt_truncated_to_zlow_only(grid): fig_test.colorbar(frame="a2000") return fig_ref, fig_test + @check_figures_equal() def test_grd2cpt_truncated_to_zhigh_only(grid): """ @@ -171,6 +174,24 @@ def test_grd2cpt_truncated_to_zhigh_only(grid): return fig_ref, fig_test +@check_figures_equal() +def test_grd2cpt_reverse_color_only(grid): + """ + Create CPT with its colors reversed. + """ + fig_ref, fig_test = Figure(), Figure() + # Use single-character arguments for the reference image + fig_ref.basemap(B="a", J="W0/15c", R="d") + grd2cpt(grid=grid, C="rainbow", I=True) + fig_ref.grdimage(grid=grid) + fig_ref.colorbar(B="a2000") + fig_test.basemap(frame="a", projection="W0/15c", region="d") + grd2cpt(grid=grid, cmap="rainbow", reverse=True) + fig_test.grdimage(grid=grid) + fig_test.colorbar(frame="a2000") + return fig_ref, fig_test + + def test_grd2cpt_blank_output(grid): """ Use incorrect setting by passing in blank file name to output parameter. From 426e6814f928caab1cf9a394af05d22c460d549f Mon Sep 17 00:00:00 2001 From: Will Schlitzer Date: Sat, 23 Jan 2021 19:35:23 +0000 Subject: [PATCH 24/56] Add test_grd2cpt_reverse_zsign_only --- pygmt/tests/test_grd2cpt.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/pygmt/tests/test_grd2cpt.py b/pygmt/tests/test_grd2cpt.py index 1d5270d6fac..4e4ac0a8107 100644 --- a/pygmt/tests/test_grd2cpt.py +++ b/pygmt/tests/test_grd2cpt.py @@ -191,6 +191,22 @@ def test_grd2cpt_reverse_color_only(grid): fig_test.colorbar(frame="a2000") return fig_ref, fig_test +@check_figures_equal() +def test_grd2cpt_reverse_zsign_only(grid): + """ + Create CPT with its z-values reversed. + """ + fig_ref, fig_test = Figure(), Figure() + # Use single-character arguments for the reference image + fig_ref.basemap(B="a", J="W0/15c", R="d") + grd2cpt(grid=grid, C="earth", I="z") + fig_ref.grdimage(grid=grid) + fig_ref.colorbar(B="a2000") + fig_test.basemap(frame="a", projection="W0/15c", region="d") + grd2cpt(grid=grid, cmap="earth", reverse="z") + fig_test.grdimage(grid=grid) + fig_test.colorbar(frame="a2000") + return fig_ref, fig_test def test_grd2cpt_blank_output(grid): """ From 7a777bd9595f2051231b522ddc9cacd6e180941f Mon Sep 17 00:00:00 2001 From: Will Schlitzer Date: Sat, 23 Jan 2021 19:36:33 +0000 Subject: [PATCH 25/56] Add test_grd2cpt_reverse_color_and_zsign --- pygmt/tests/test_grd2cpt.py | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/pygmt/tests/test_grd2cpt.py b/pygmt/tests/test_grd2cpt.py index 4e4ac0a8107..8369e66c651 100644 --- a/pygmt/tests/test_grd2cpt.py +++ b/pygmt/tests/test_grd2cpt.py @@ -194,7 +194,7 @@ def test_grd2cpt_reverse_color_only(grid): @check_figures_equal() def test_grd2cpt_reverse_zsign_only(grid): """ - Create CPT with its z-values reversed. + Create CPT with its z-value sign reversed. """ fig_ref, fig_test = Figure(), Figure() # Use single-character arguments for the reference image @@ -208,6 +208,23 @@ def test_grd2cpt_reverse_zsign_only(grid): fig_test.colorbar(frame="a2000") return fig_ref, fig_test +@check_figures_equal() +def test_grd2cpt_reverse_color_and_zsign(grid): + """ + Create CPT with its colors and z-value sign reversed. + """ + fig_ref, fig_test = Figure(), Figure() + # Use single-character arguments for the reference image + fig_ref.basemap(B="a", J="W0/15c", R="d") + grd2cpt(grid=grid, C="earth", I="cz") + fig_ref.grdimage(grid=grid) + fig_ref.colorbar(B="a2000") + fig_test.basemap(frame="a", projection="W0/15c", region="d") + grd2cpt(grid=grid, cmap="earth", reverse="cz") + fig_test.grdimage(grid=grid) + fig_test.colorbar(frame="a2000") + return fig_ref, fig_test + def test_grd2cpt_blank_output(grid): """ Use incorrect setting by passing in blank file name to output parameter. From 477eefe2d4872726d597c4d723f65f5917326d65 Mon Sep 17 00:00:00 2001 From: Will Schlitzer Date: Sat, 23 Jan 2021 19:44:14 +0000 Subject: [PATCH 26/56] Add test_grd2cpt_continuous, test_grd2cpt_categorical, and test_grd2cpt_cyclic --- pygmt/tests/test_grd2cpt.py | 57 +++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) diff --git a/pygmt/tests/test_grd2cpt.py b/pygmt/tests/test_grd2cpt.py index 8369e66c651..65bcc6c91bb 100644 --- a/pygmt/tests/test_grd2cpt.py +++ b/pygmt/tests/test_grd2cpt.py @@ -191,6 +191,7 @@ def test_grd2cpt_reverse_color_only(grid): fig_test.colorbar(frame="a2000") return fig_ref, fig_test + @check_figures_equal() def test_grd2cpt_reverse_zsign_only(grid): """ @@ -208,6 +209,7 @@ def test_grd2cpt_reverse_zsign_only(grid): fig_test.colorbar(frame="a2000") return fig_ref, fig_test + @check_figures_equal() def test_grd2cpt_reverse_color_and_zsign(grid): """ @@ -225,6 +227,61 @@ def test_grd2cpt_reverse_color_and_zsign(grid): fig_test.colorbar(frame="a2000") return fig_ref, fig_test + +@check_figures_equal() +def test_grd2cpt_continuous(grid): + """ + Create a CPT with that is continuous. + """ + fig_ref, fig_test = Figure(), Figure() + # Use single-character arguments for the reference image + fig_ref.basemap(B="a", J="W0/15c", R="d") + grd2cpt(grid=grid, C="blue,white", Z=True) + fig_ref.grdimage(grid=grid) + fig_ref.colorbar(B="a2000") + fig_test.basemap(frame="a", projection="W0/15c", region="d") + grd2cpt(grid=grid, cmap="blue,white", continuous=True) + fig_test.grdimage(grid=grid) + fig_test.colorbar(frame="a2000") + return fig_ref, fig_test + + +@check_figures_equal() +def test_grd2cpt_categorical(grid): + """ + Create a CPT with that is categorical. + """ + fig_ref, fig_test = Figure(), Figure() + # Use single-character arguments for the reference image + fig_ref.basemap(B="a", J="W0/15c", R="d") + grd2cpt(grid=grid, C="geo", W=True) + fig_ref.grdimage(grid=grid) + fig_ref.colorbar(B="a2000") + fig_test.basemap(frame="a", projection="W0/15c", region="d") + grd2cpt(grid=grid, cmap="geo", categorical=True) + fig_test.grdimage(grid=grid) + fig_test.colorbar(frame="a2000") + return fig_ref, fig_test + + +@check_figures_equal() +def test_grd2cpt_cyclic(grid): + """ + Create a CPT with that is cyclic. + """ + fig_ref, fig_test = Figure(), Figure() + # Use single-character arguments for the reference image + fig_ref.basemap(B="a", J="W0/15c", R="d") + grd2cpt(grid=grid, C="geo", Ww=True) + fig_ref.grdimage(grid=grid) + fig_ref.colorbar(B="a2000") + fig_test.basemap(frame="a", projection="W0/15c", region="d") + grd2cpt(grid=grid, cmap="geo", cyclic=True) + fig_test.grdimage(grid=grid) + fig_test.colorbar(frame="a2000") + return fig_ref, fig_test + + def test_grd2cpt_blank_output(grid): """ Use incorrect setting by passing in blank file name to output parameter. From 20296565c45811ddae152001430a1c9695698774 Mon Sep 17 00:00:00 2001 From: Will Schlitzer Date: Sat, 23 Jan 2021 19:47:51 +0000 Subject: [PATCH 27/56] Add test_grd2cpt_limit --- pygmt/tests/test_grd2cpt.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/pygmt/tests/test_grd2cpt.py b/pygmt/tests/test_grd2cpt.py index 65bcc6c91bb..0999390fe86 100644 --- a/pygmt/tests/test_grd2cpt.py +++ b/pygmt/tests/test_grd2cpt.py @@ -282,6 +282,24 @@ def test_grd2cpt_cyclic(grid): return fig_ref, fig_test +@check_figures_equal() +def test_grd2cpt_limit(grid): + """ + Create a CPT with that sets a min/max limit. + """ + fig_ref, fig_test = Figure(), Figure() + # Use single-character arguments for the reference image + fig_ref.basemap(B="a", J="W0/15c", R="d") + grd2cpt(grid=grid, C="haxby", L="-1000/1000") + fig_ref.grdimage(grid=grid) + fig_ref.colorbar(B="a2000") + fig_test.basemap(frame="a", projection="W0/15c", region="d") + grd2cpt(grid=grid, cmap="haxby", limit=[-1000, 1000]) + fig_test.grdimage(grid=grid) + fig_test.colorbar(frame="a2000") + return fig_ref, fig_test + + def test_grd2cpt_blank_output(grid): """ Use incorrect setting by passing in blank file name to output parameter. From 7940dd0ea243386160f0a9bcad7f8c9b93124657 Mon Sep 17 00:00:00 2001 From: Will Schlitzer Date: Mon, 25 Jan 2021 11:57:17 +0000 Subject: [PATCH 28/56] Remove grdimage from grd2cpt tests --- pygmt/tests/test_grd2cpt.py | 28 +--------------------------- 1 file changed, 1 insertion(+), 27 deletions(-) diff --git a/pygmt/tests/test_grd2cpt.py b/pygmt/tests/test_grd2cpt.py index 0999390fe86..5f5a08eea40 100644 --- a/pygmt/tests/test_grd2cpt.py +++ b/pygmt/tests/test_grd2cpt.py @@ -43,17 +43,15 @@ def fixture_grid(): def test_grd2cpt(grid): """ Test creating a CPT with grd2cpt to create a CPT based off a grid input and - plot it with grdimage. + plot it with a color bar. """ fig_ref, fig_test = Figure(), Figure() # Use single-character arguments for the reference image fig_ref.basemap(B="a", J="W0/15c", R="d") grd2cpt(grid=grid) - fig_ref.grdimage(grid=grid) fig_ref.colorbar(B="a2000") fig_test.basemap(frame="a", projection="W0/15c", region="d") grd2cpt(grid=grid) - fig_test.grdimage(grid=grid) fig_test.colorbar(frame="a2000") return fig_ref, fig_test @@ -97,11 +95,9 @@ def test_grd2cpt_set_cpt(grid): # Use single-character arguments for the reference image fig_ref.basemap(B="a", J="W0/15c", R="d") grd2cpt(grid=grid, cmap="rainbow") - fig_ref.grdimage(grid=grid) fig_ref.colorbar(B="a2000") fig_test.basemap(frame="a", projection="W0/15c", region="d") grd2cpt(grid=grid, cmap="rainbow") - fig_test.grdimage(grid=grid) fig_test.colorbar(frame="a2000") return fig_ref, fig_test @@ -114,11 +110,9 @@ def test_grd2cpt_scaled_with_series(grid): fig_ref, fig_test = Figure(), Figure() fig_ref.basemap(B="a", J="W0/15c", R="d") grd2cpt(grid=grid, cmap="rainbow", T="-4500/4500/500") - fig_ref.grdimage(grid=grid) fig_ref.colorbar(B="a2000") fig_test.basemap(frame="a", projection="W0/15c", region="d") grd2cpt(grid=grid, cmap="rainbow", series=[-4500, 4500, 500]) - fig_test.grdimage(grid=grid) fig_test.colorbar(frame="a2000") return fig_ref, fig_test @@ -131,11 +125,9 @@ def test_grd2cpt_truncated_to_zlow_zhigh(grid): fig_ref, fig_test = Figure(), Figure() fig_ref.basemap(B="a", J="W0/15c", R="d") grd2cpt(grid=grid, cmap="rainbow", G="0.15/0.85", T="-4500/4500/500") - fig_ref.grdimage(grid=grid) fig_ref.colorbar(B="a2000") fig_test.basemap(frame="a", projection="W0/15c", region="d") grd2cpt(grid=grid, cmap="rainbow", truncate=[0.15, 0.85], series=[-4500, 4500, 500]) - fig_test.grdimage(grid=grid) fig_test.colorbar(frame="a2000") return fig_ref, fig_test @@ -148,11 +140,9 @@ def test_grd2cpt_truncated_to_zlow_only(grid): fig_ref, fig_test = Figure(), Figure() fig_ref.basemap(B="a", J="W0/15c", R="d") grd2cpt(grid=grid, cmap="rainbow", G="0.5/NaN", T="-4500/4500/500") - fig_ref.grdimage(grid=grid) fig_ref.colorbar(B="a2000") fig_test.basemap(frame="a", projection="W0/15c", region="d") grd2cpt(grid=grid, cmap="rainbow", truncate=[0.5, None], series=[-4500, 4500, 500]) - fig_test.grdimage(grid=grid) fig_test.colorbar(frame="a2000") return fig_ref, fig_test @@ -165,11 +155,9 @@ def test_grd2cpt_truncated_to_zhigh_only(grid): fig_ref, fig_test = Figure(), Figure() fig_ref.basemap(B="a", J="W0/15c", R="d") grd2cpt(grid=grid, cmap="rainbow", G="NaN/0.5", T="-4500/4500/500") - fig_ref.grdimage(grid=grid) fig_ref.colorbar(B="a2000") fig_test.basemap(frame="a", projection="W0/15c", region="d") grd2cpt(grid=grid, cmap="rainbow", truncate=[None, 0.5], series=[-4500, 4500, 500]) - fig_test.grdimage(grid=grid) fig_test.colorbar(frame="a2000") return fig_ref, fig_test @@ -183,11 +171,9 @@ def test_grd2cpt_reverse_color_only(grid): # Use single-character arguments for the reference image fig_ref.basemap(B="a", J="W0/15c", R="d") grd2cpt(grid=grid, C="rainbow", I=True) - fig_ref.grdimage(grid=grid) fig_ref.colorbar(B="a2000") fig_test.basemap(frame="a", projection="W0/15c", region="d") grd2cpt(grid=grid, cmap="rainbow", reverse=True) - fig_test.grdimage(grid=grid) fig_test.colorbar(frame="a2000") return fig_ref, fig_test @@ -201,11 +187,9 @@ def test_grd2cpt_reverse_zsign_only(grid): # Use single-character arguments for the reference image fig_ref.basemap(B="a", J="W0/15c", R="d") grd2cpt(grid=grid, C="earth", I="z") - fig_ref.grdimage(grid=grid) fig_ref.colorbar(B="a2000") fig_test.basemap(frame="a", projection="W0/15c", region="d") grd2cpt(grid=grid, cmap="earth", reverse="z") - fig_test.grdimage(grid=grid) fig_test.colorbar(frame="a2000") return fig_ref, fig_test @@ -219,11 +203,9 @@ def test_grd2cpt_reverse_color_and_zsign(grid): # Use single-character arguments for the reference image fig_ref.basemap(B="a", J="W0/15c", R="d") grd2cpt(grid=grid, C="earth", I="cz") - fig_ref.grdimage(grid=grid) fig_ref.colorbar(B="a2000") fig_test.basemap(frame="a", projection="W0/15c", region="d") grd2cpt(grid=grid, cmap="earth", reverse="cz") - fig_test.grdimage(grid=grid) fig_test.colorbar(frame="a2000") return fig_ref, fig_test @@ -237,11 +219,9 @@ def test_grd2cpt_continuous(grid): # Use single-character arguments for the reference image fig_ref.basemap(B="a", J="W0/15c", R="d") grd2cpt(grid=grid, C="blue,white", Z=True) - fig_ref.grdimage(grid=grid) fig_ref.colorbar(B="a2000") fig_test.basemap(frame="a", projection="W0/15c", region="d") grd2cpt(grid=grid, cmap="blue,white", continuous=True) - fig_test.grdimage(grid=grid) fig_test.colorbar(frame="a2000") return fig_ref, fig_test @@ -255,11 +235,9 @@ def test_grd2cpt_categorical(grid): # Use single-character arguments for the reference image fig_ref.basemap(B="a", J="W0/15c", R="d") grd2cpt(grid=grid, C="geo", W=True) - fig_ref.grdimage(grid=grid) fig_ref.colorbar(B="a2000") fig_test.basemap(frame="a", projection="W0/15c", region="d") grd2cpt(grid=grid, cmap="geo", categorical=True) - fig_test.grdimage(grid=grid) fig_test.colorbar(frame="a2000") return fig_ref, fig_test @@ -273,11 +251,9 @@ def test_grd2cpt_cyclic(grid): # Use single-character arguments for the reference image fig_ref.basemap(B="a", J="W0/15c", R="d") grd2cpt(grid=grid, C="geo", Ww=True) - fig_ref.grdimage(grid=grid) fig_ref.colorbar(B="a2000") fig_test.basemap(frame="a", projection="W0/15c", region="d") grd2cpt(grid=grid, cmap="geo", cyclic=True) - fig_test.grdimage(grid=grid) fig_test.colorbar(frame="a2000") return fig_ref, fig_test @@ -291,11 +267,9 @@ def test_grd2cpt_limit(grid): # Use single-character arguments for the reference image fig_ref.basemap(B="a", J="W0/15c", R="d") grd2cpt(grid=grid, C="haxby", L="-1000/1000") - fig_ref.grdimage(grid=grid) fig_ref.colorbar(B="a2000") fig_test.basemap(frame="a", projection="W0/15c", region="d") grd2cpt(grid=grid, cmap="haxby", limit=[-1000, 1000]) - fig_test.grdimage(grid=grid) fig_test.colorbar(frame="a2000") return fig_ref, fig_test From 81e6e903f0c2731aa193ba5efe92e83c57656fef Mon Sep 17 00:00:00 2001 From: Will Schlitzer Date: Wed, 3 Feb 2021 18:15:11 +0000 Subject: [PATCH 29/56] Remove unnecessary with statement --- pygmt/gridops.py | 42 ++++++++++++++++++++---------------------- 1 file changed, 20 insertions(+), 22 deletions(-) diff --git a/pygmt/gridops.py b/pygmt/gridops.py index 3c5790a5b9d..48457e80fe1 100644 --- a/pygmt/gridops.py +++ b/pygmt/gridops.py @@ -400,25 +400,23 @@ def grd2cpt(grid, **kwargs): {V} """ kind = data_kind(grid) - - with GMTTempFile(suffix=".nc"): - with Session() as lib: - if kind == "file": - file_context = dummy_context(grid) - elif kind == "grid": - file_context = lib.virtualfile_from_grid(grid) - else: - raise GMTInvalidInput("Unrecognized data type: {}".format(type(grid))) - - with file_context as infile: - if "W" in kwargs and "Ww" in kwargs: - raise GMTInvalidInput( - "Set only categorical or cyclic to True, not both." - ) - - if "H" in kwargs.keys(): # if output file is set - outfile = kwargs.pop("H") - if not outfile or not isinstance(outfile, str): - raise GMTInvalidInput("'output' should be a proper file name.") - arg_str = " ".join([infile, build_arg_string(kwargs)]) - lib.call_module("grd2cpt", arg_str) + with Session() as lib: + if kind == "file": + file_context = dummy_context(grid) + elif kind == "grid": + file_context = lib.virtualfile_from_grid(grid) + else: + raise GMTInvalidInput("Unrecognized data type: {}".format(type(grid))) + + with file_context as infile: + if "W" in kwargs and "Ww" in kwargs: + raise GMTInvalidInput( + "Set only categorical or cyclic to True, not both." + ) + + if "H" in kwargs.keys(): # if output file is set + outfile = kwargs.pop("H") + if not outfile or not isinstance(outfile, str): + raise GMTInvalidInput("'output' should be a proper file name.") + arg_str = " ".join([infile, build_arg_string(kwargs)]) + lib.call_module("grd2cpt", arg_str) From c0ac4923586551be9bbe0c9996147cd181cdaf19 Mon Sep 17 00:00:00 2001 From: Will Schlitzer Date: Sat, 6 Feb 2021 09:28:12 +0000 Subject: [PATCH 30/56] Apply suggestions from code review Co-authored-by: Wei Ji <23487320+weiji14@users.noreply.github.com> --- pygmt/tests/test_grd2cpt.py | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/pygmt/tests/test_grd2cpt.py b/pygmt/tests/test_grd2cpt.py index 5f5a08eea40..699e464ced6 100644 --- a/pygmt/tests/test_grd2cpt.py +++ b/pygmt/tests/test_grd2cpt.py @@ -36,7 +36,7 @@ def fixture_grid(): """ Load the grid data from the sample earth_relief file. """ - return load_earth_relief(registration="gridline") + return load_earth_relief() @check_figures_equal() @@ -48,7 +48,7 @@ def test_grd2cpt(grid): fig_ref, fig_test = Figure(), Figure() # Use single-character arguments for the reference image fig_ref.basemap(B="a", J="W0/15c", R="d") - grd2cpt(grid=grid) + grd2cpt(grid="@earth_relief_01d") fig_ref.colorbar(B="a2000") fig_test.basemap(frame="a", projection="W0/15c", region="d") grd2cpt(grid=grid) @@ -66,12 +66,7 @@ def test_grd2cpt_to_plot_points(points, region, grid): fig_ref.basemap(R=region, J="X15c", B="a") grd2cpt(grid=grid, C="rainbow") fig_ref.plot( - x=points[:, 0], - y=points[:, 1], - G=points[:, 2], - R=region, - S="c1c", - C=True, + x=points[:, 0], y=points[:, 1], G=points[:, 2], R=region, S="c1c", C=True ) fig_test.basemap(region=region, projection="X15c", frame="a") grd2cpt(grid=grid, cmap="rainbow") From b99f6666a66300d510dd0d6b084da93e094997d3 Mon Sep 17 00:00:00 2001 From: Will Schlitzer Date: Sat, 6 Feb 2021 09:39:11 +0000 Subject: [PATCH 31/56] Move grd2cpt function from gridops.py to grd2cpt.py --- pygmt/__init__.py | 3 +- pygmt/gridops.py | 166 -------------------------------- pygmt/src/__init__.py | 2 + pygmt/src/grd2cpt.py | 182 ++++++++++++++++++++++++++++++++++++ pygmt/tests/test_grd2cpt.py | 3 +- 5 files changed, 188 insertions(+), 168 deletions(-) create mode 100644 pygmt/src/grd2cpt.py diff --git a/pygmt/__init__.py b/pygmt/__init__.py index 34b53e0d920..56572a7f5dd 100644 --- a/pygmt/__init__.py +++ b/pygmt/__init__.py @@ -16,12 +16,13 @@ from pygmt.figure import Figure from pygmt.filtering import blockmedian from pygmt.gridding import surface -from pygmt.gridops import grd2cpt, grdcut, grdfilter +from pygmt.gridops import grdcut, grdfilter from pygmt.mathops import makecpt from pygmt.modules import GMTDataArrayAccessor, config, grdinfo, info, which from pygmt.sampling import grdtrack from pygmt.session_management import begin as _begin from pygmt.session_management import end as _end +from pygmt.src import grd2cpt from pygmt.x2sys import x2sys_cross, x2sys_init # Get semantic version through setuptools-scm diff --git a/pygmt/gridops.py b/pygmt/gridops.py index 48457e80fe1..697eccc3fe8 100644 --- a/pygmt/gridops.py +++ b/pygmt/gridops.py @@ -254,169 +254,3 @@ def grdfilter(grid, **kwargs): result = None # if user sets an outgrid, return None return result - - -@fmt_docstring -@use_alias( - A="transparency", - C="cmap", - D="background", - F="color_model", - G="truncate", - H="output", - I="reverse", - L="limit", - M="overrule_bg", - N="no_bg", - Q="log", - R="region", - T="series", - V="verbose", - W="categorical", - Ww="cyclic", - Z="continuous", -) -@kwargs_to_strings(T="sequence", G="sequence", L="sequence", R="sequence") -def grd2cpt(grid, **kwargs): - """ - Make GMT color palette tables from a grid file. - - This is a module that will help you make static color palette tables - (CPTs). By default, the CPT will simply be saved to the current session, - but you can use *output* to save it to a file. You define an equidistant - set of contour intervals or pass your own z-table or list, and create a new - CPT based on an existing master (dynamic) CPT. The resulting CPT can be - reversed relative to the master cpt, and can be made continuous or - discrete. For color tables beyond the standard GMT offerings, visit - `cpt-city `_ and - `Scientific Colour-Maps `_. - - The CPT includes three additional colors beyond the range of z-values. - These are the background color (B) assigned to values lower than the lowest - *z*-value, the foreground color (F) assigned to values higher than the - highest *z*-value, and the NaN color (N) painted wherever values are - undefined. - - If the master CPT includes B, F, and N entries, these will be copied into - the new master file. If not, the parameters :gmt-term:`COLOR_BACKGROUND`, - :gmt-term:`COLOR_FOREGROUND`, and :gmt-term:`COLOR_NAN` from the - :gmt-docs:`gmt.conf ` file or the command line will be used. This - default behavior can be overruled using the options *background*, - *overrule_bg* or *no_bg*. - - The color model (RGB, HSV or CMYK) of the palette created by **grdcpt** - will be the same as specified in the header of the master CPT. When there - is no :gmt-term:`COLOR_MODEL` entry in the master CPT, the - :gmt-term:`COLOR_MODEL` specified in the :gmt-docs:`gmt.conf ` - file or on the command line will be used. - - Full option list at :gmt-docs:`grd2cpt.html` - - {aliases} - - Parameters - ---------- - grid : str or xarray.DataArray - The file name of the input grid or the grid loaded as a DataArray. - transparency : str - Sets a constant level of transparency (0-100) for all color slices. - Append **+a** to also affect the fore-, back-, and nan-colors - [Default is no transparency, i.e., 0 (opaque)]. - cmap : str - Selects the master color palette table (CPT) to use in the - interpolation. Full list of built-in color palette tables can be found - at :gmt-docs:`cookbook/cpts.html#built-in-color-palette-tables-cpt`. - background : bool or str - Select the back- and foreground colors to match the colors for lowest - and highest *z*-values in the output CPT [Default (``background=True`` - or ``background='o'``) uses the colors specified in the master file, or - those defined by the parameters :gmt-term:`COLOR_BACKGROUND`, - :gmt-term:`COLOR_FOREGROUND`, and :gmt-term:`COLOR_NAN`]. Use - ``background='i'`` to match the colors for the lowest and highest - values in the input (instead of the output) CPT. - color_model : - ``[R|r|h|c][+c[label]]``. - Force output CPT to be written with r/g/b codes, gray-scale values or - color name (**R**, default) or r/g/b codes only (**r**), or h-s-v codes - (**h**), or c/m/y/k codes (**c**). Optionally or alternatively, append - **+c** to write discrete palettes in categorical format. If *label* is - appended then we create labels for each category to be used when the - CPT is plotted. The *label* may be a comma-separated list of category - names (you can skip a category by not giving a name), or give - *start*[-], where we automatically build monotonically increasing - labels from *start* (a single letter or an integer). Append - to build - ranges *start*-*start+1* instead. - series : list or str - ``[min/max/inc[+b|l|n]|file|list]``. - Defines the range of the new CPT by giving the lowest and highest - z-value (and optionally an interval). If this is not given, the - existing range in the master CPT will be used intact. The values - produced defines the color slice boundaries. If **+n** is used it - refers to the number of such boundaries and not the number of slices. - For details on array creation, see - :gmt-docs:`makecpt.html#generate-1d-array`. - truncate : list or str - ``zlo/zhi``. - Truncate the incoming CPT so that the lowest and highest z-levels are - to *zlo* and *zhi*. If one of these equal NaN then we leave that end of - the CPT alone. The truncation takes place before any resampling. See - also :gmt-docs:`cookbook/features.html#manipulating-cpts`. - output : str - Optional. The file name with extension .cpt to store the generated CPT - file. If not given or False (default), saves the CPT as the session - current CPT. - reverse : str - Set this to True or c [Default] to reverse the sense of color - progression in the master CPT. Set this to z to reverse the sign of - z-values in the color table. Note that this change of z-direction - happens before *truncate* and *series* values are used so the latter - must be compatible with the changed *z*-range. See also - :gmt-docs:`cookbook/features.html#manipulating-cpts`. - overrule_bg : - Overrule background, foreground, and NaN colors specified in the master - CPT with the values of the parameters :gmt-term:`COLOR_BACKGROUND`, - :gmt-term:`COLOR_FOREGROUND`, and :gmt-term:`COLOR_NAN` specified in - the :gmt-docs:`gmt.conf ` file or on the command line. When - combined with ``background``, only :gmt-term:`COLOR_NAN` is considered. - no_bg : bool - Do not write out the background, foreground, and NaN-color fields - [Default will write them, i.e. ``no_bg=False``]. - log : bool - For logarithmic interpolation scheme with input given as logarithms. - Expects input z-values provided via ``series`` to be log10(*z*), - assigns colors, and writes out *z*. - continuous : bool - Force a continuous CPT when building from a list of colors and a list - of z-values [Default is None, i.e. discrete values]. - categorical : bool - Do not interpolate the input color table but pick the output colors - starting at the beginning of the color table, until colors for all - intervals are assigned. This is particularly useful in combination with - a categorical color table, like ``cmap='categorical'``. - cyclic : bool - Produce a wrapped (cyclic) color table that endlessly repeats its - range. Note that ``cyclic=True`` cannot be set together with - ``categorical=True``. - {V} - """ - kind = data_kind(grid) - with Session() as lib: - if kind == "file": - file_context = dummy_context(grid) - elif kind == "grid": - file_context = lib.virtualfile_from_grid(grid) - else: - raise GMTInvalidInput("Unrecognized data type: {}".format(type(grid))) - - with file_context as infile: - if "W" in kwargs and "Ww" in kwargs: - raise GMTInvalidInput( - "Set only categorical or cyclic to True, not both." - ) - - if "H" in kwargs.keys(): # if output file is set - outfile = kwargs.pop("H") - if not outfile or not isinstance(outfile, str): - raise GMTInvalidInput("'output' should be a proper file name.") - arg_str = " ".join([infile, build_arg_string(kwargs)]) - lib.call_module("grd2cpt", arg_str) diff --git a/pygmt/src/__init__.py b/pygmt/src/__init__.py index cdbabf1468a..a6e618df888 100644 --- a/pygmt/src/__init__.py +++ b/pygmt/src/__init__.py @@ -1,5 +1,7 @@ """ Source code for PyGMT modules. """ +from pygmt.src.grd2cpt import grd2cpt + # pylint: disable=import-outside-toplevel from pygmt.src.meca import meca diff --git a/pygmt/src/grd2cpt.py b/pygmt/src/grd2cpt.py new file mode 100644 index 00000000000..d29abe2092f --- /dev/null +++ b/pygmt/src/grd2cpt.py @@ -0,0 +1,182 @@ +""" +grd2cpt - Create a CPT from a grid file. +""" + +import xarray as xr +from pygmt.clib import Session +from pygmt.exceptions import GMTInvalidInput +from pygmt.helpers import ( + GMTTempFile, + build_arg_string, + data_kind, + dummy_context, + fmt_docstring, + kwargs_to_strings, + use_alias, +) + + +@fmt_docstring +@use_alias( + A="transparency", + C="cmap", + D="background", + F="color_model", + G="truncate", + H="output", + I="reverse", + L="limit", + M="overrule_bg", + N="no_bg", + Q="log", + R="region", + T="series", + V="verbose", + W="categorical", + Ww="cyclic", + Z="continuous", +) +@kwargs_to_strings(T="sequence", G="sequence", L="sequence", R="sequence") +def grd2cpt(grid, **kwargs): + """ + Make GMT color palette tables from a grid file. + + This is a module that will help you make static color palette tables + (CPTs). By default, the CPT will simply be saved to the current session, + but you can use *output* to save it to a file. You define an equidistant + set of contour intervals or pass your own z-table or list, and create a new + CPT based on an existing master (dynamic) CPT. The resulting CPT can be + reversed relative to the master cpt, and can be made continuous or + discrete. For color tables beyond the standard GMT offerings, visit + `cpt-city `_ and + `Scientific Colour-Maps `_. + + The CPT includes three additional colors beyond the range of z-values. + These are the background color (B) assigned to values lower than the lowest + *z*-value, the foreground color (F) assigned to values higher than the + highest *z*-value, and the NaN color (N) painted wherever values are + undefined. + + If the master CPT includes B, F, and N entries, these will be copied into + the new master file. If not, the parameters :gmt-term:`COLOR_BACKGROUND`, + :gmt-term:`COLOR_FOREGROUND`, and :gmt-term:`COLOR_NAN` from the + :gmt-docs:`gmt.conf ` file or the command line will be used. This + default behavior can be overruled using the options *background*, + *overrule_bg* or *no_bg*. + + The color model (RGB, HSV or CMYK) of the palette created by **grdcpt** + will be the same as specified in the header of the master CPT. When there + is no :gmt-term:`COLOR_MODEL` entry in the master CPT, the + :gmt-term:`COLOR_MODEL` specified in the :gmt-docs:`gmt.conf ` + file or on the command line will be used. + + Full option list at :gmt-docs:`grd2cpt.html` + + {aliases} + + Parameters + ---------- + grid : str or xarray.DataArray + The file name of the input grid or the grid loaded as a DataArray. + transparency : str + Sets a constant level of transparency (0-100) for all color slices. + Append **+a** to also affect the fore-, back-, and nan-colors + [Default is no transparency, i.e., 0 (opaque)]. + cmap : str + Selects the master color palette table (CPT) to use in the + interpolation. Full list of built-in color palette tables can be found + at :gmt-docs:`cookbook/cpts.html#built-in-color-palette-tables-cpt`. + background : bool or str + Select the back- and foreground colors to match the colors for lowest + and highest *z*-values in the output CPT [Default (``background=True`` + or ``background='o'``) uses the colors specified in the master file, or + those defined by the parameters :gmt-term:`COLOR_BACKGROUND`, + :gmt-term:`COLOR_FOREGROUND`, and :gmt-term:`COLOR_NAN`]. Use + ``background='i'`` to match the colors for the lowest and highest + values in the input (instead of the output) CPT. + color_model : + ``[R|r|h|c][+c[label]]``. + Force output CPT to be written with r/g/b codes, gray-scale values or + color name (**R**, default) or r/g/b codes only (**r**), or h-s-v codes + (**h**), or c/m/y/k codes (**c**). Optionally or alternatively, append + **+c** to write discrete palettes in categorical format. If *label* is + appended then we create labels for each category to be used when the + CPT is plotted. The *label* may be a comma-separated list of category + names (you can skip a category by not giving a name), or give + *start*[-], where we automatically build monotonically increasing + labels from *start* (a single letter or an integer). Append - to build + ranges *start*-*start+1* instead. + series : list or str + ``[min/max/inc[+b|l|n]|file|list]``. + Defines the range of the new CPT by giving the lowest and highest + z-value (and optionally an interval). If this is not given, the + existing range in the master CPT will be used intact. The values + produced defines the color slice boundaries. If **+n** is used it + refers to the number of such boundaries and not the number of slices. + For details on array creation, see + :gmt-docs:`makecpt.html#generate-1d-array`. + truncate : list or str + ``zlo/zhi``. + Truncate the incoming CPT so that the lowest and highest z-levels are + to *zlo* and *zhi*. If one of these equal NaN then we leave that end of + the CPT alone. The truncation takes place before any resampling. See + also :gmt-docs:`cookbook/features.html#manipulating-cpts`. + output : str + Optional. The file name with extension .cpt to store the generated CPT + file. If not given or False (default), saves the CPT as the session + current CPT. + reverse : str + Set this to True or c [Default] to reverse the sense of color + progression in the master CPT. Set this to z to reverse the sign of + z-values in the color table. Note that this change of z-direction + happens before *truncate* and *series* values are used so the latter + must be compatible with the changed *z*-range. See also + :gmt-docs:`cookbook/features.html#manipulating-cpts`. + overrule_bg : + Overrule background, foreground, and NaN colors specified in the master + CPT with the values of the parameters :gmt-term:`COLOR_BACKGROUND`, + :gmt-term:`COLOR_FOREGROUND`, and :gmt-term:`COLOR_NAN` specified in + the :gmt-docs:`gmt.conf ` file or on the command line. When + combined with ``background``, only :gmt-term:`COLOR_NAN` is considered. + no_bg : bool + Do not write out the background, foreground, and NaN-color fields + [Default will write them, i.e. ``no_bg=False``]. + log : bool + For logarithmic interpolation scheme with input given as logarithms. + Expects input z-values provided via ``series`` to be log10(*z*), + assigns colors, and writes out *z*. + continuous : bool + Force a continuous CPT when building from a list of colors and a list + of z-values [Default is None, i.e. discrete values]. + categorical : bool + Do not interpolate the input color table but pick the output colors + starting at the beginning of the color table, until colors for all + intervals are assigned. This is particularly useful in combination with + a categorical color table, like ``cmap='categorical'``. + cyclic : bool + Produce a wrapped (cyclic) color table that endlessly repeats its + range. Note that ``cyclic=True`` cannot be set together with + ``categorical=True``. + {V} + """ + kind = data_kind(grid) + with Session() as lib: + if kind == "file": + file_context = dummy_context(grid) + elif kind == "grid": + file_context = lib.virtualfile_from_grid(grid) + else: + raise GMTInvalidInput("Unrecognized data type: {}".format(type(grid))) + + with file_context as infile: + if "W" in kwargs and "Ww" in kwargs: + raise GMTInvalidInput( + "Set only categorical or cyclic to True, not both." + ) + + if "H" in kwargs.keys(): # if output file is set + outfile = kwargs.pop("H") + if not outfile or not isinstance(outfile, str): + raise GMTInvalidInput("'output' should be a proper file name.") + arg_str = " ".join([infile, build_arg_string(kwargs)]) + lib.call_module("grd2cpt", arg_str) diff --git a/pygmt/tests/test_grd2cpt.py b/pygmt/tests/test_grd2cpt.py index 699e464ced6..82cf0f9e66c 100644 --- a/pygmt/tests/test_grd2cpt.py +++ b/pygmt/tests/test_grd2cpt.py @@ -5,11 +5,12 @@ import numpy as np import pytest -from pygmt import Figure, grd2cpt +from pygmt import Figure from pygmt.datasets import load_earth_relief from pygmt.exceptions import GMTInvalidInput from pygmt.helpers import GMTTempFile from pygmt.helpers.testing import check_figures_equal +from pygmt.src.grd2cpt import grd2cpt TEST_DATA_DIR = os.path.join(os.path.dirname(__file__), "data") POINTS_DATA = os.path.join(TEST_DATA_DIR, "points.txt") From 1812b93313a0d03fe75f0c3fc70ef0ed42afbbe6 Mon Sep 17 00:00:00 2001 From: Will Schlitzer Date: Sat, 6 Feb 2021 09:41:24 +0000 Subject: [PATCH 32/56] Move grd2cpt location in index.rst --- doc/api/index.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/api/index.rst b/doc/api/index.rst index ced3cf01d1a..be3bf3e111d 100644 --- a/doc/api/index.rst +++ b/doc/api/index.rst @@ -45,6 +45,7 @@ Color palette table generation: :toctree: generated makecpt + grd2cpt Saving and displaying the figure: @@ -75,7 +76,6 @@ Operations on grids: grdcut grdfilter grdtrack - grd2cpt Crossover analysis with x2sys: From 1fe882cc799351be9d99bf76557eba805f024051 Mon Sep 17 00:00:00 2001 From: Will Schlitzer Date: Sat, 6 Feb 2021 09:43:00 +0000 Subject: [PATCH 33/56] Run make format --- pygmt/src/__init__.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/pygmt/src/__init__.py b/pygmt/src/__init__.py index a04002994dd..ca3b7123ba4 100644 --- a/pygmt/src/__init__.py +++ b/pygmt/src/__init__.py @@ -1,10 +1,9 @@ """ Source code for PyGMT modules. """ -from pygmt.src.grd2cpt import grd2cpt - # pylint: disable=import-outside-toplevel from pygmt.src.blockmedian import blockmedian +from pygmt.src.grd2cpt import grd2cpt from pygmt.src.grdcut import grdcut from pygmt.src.grdfilter import grdfilter from pygmt.src.grdtrack import grdtrack From c6714f01b478ad1540e546d6ff4e47117c024505 Mon Sep 17 00:00:00 2001 From: Will Schlitzer Date: Sat, 6 Feb 2021 09:47:23 +0000 Subject: [PATCH 34/56] Remove unnecessary imports --- pygmt/src/grd2cpt.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/pygmt/src/grd2cpt.py b/pygmt/src/grd2cpt.py index d29abe2092f..1afdc2a884f 100644 --- a/pygmt/src/grd2cpt.py +++ b/pygmt/src/grd2cpt.py @@ -2,11 +2,9 @@ grd2cpt - Create a CPT from a grid file. """ -import xarray as xr from pygmt.clib import Session from pygmt.exceptions import GMTInvalidInput from pygmt.helpers import ( - GMTTempFile, build_arg_string, data_kind, dummy_context, From 0bdd8a5dcc29bc48a0d51192dcc05bfd20236b4e Mon Sep 17 00:00:00 2001 From: Will Schlitzer Date: Sun, 7 Feb 2021 14:14:29 +0000 Subject: [PATCH 35/56] Apply suggestions from code review Co-authored-by: Wei Ji <23487320+weiji14@users.noreply.github.com> --- pygmt/src/grd2cpt.py | 33 +++++++++++++++++++-------------- 1 file changed, 19 insertions(+), 14 deletions(-) diff --git a/pygmt/src/grd2cpt.py b/pygmt/src/grd2cpt.py index 1afdc2a884f..565b5b7cd13 100644 --- a/pygmt/src/grd2cpt.py +++ b/pygmt/src/grd2cpt.py @@ -41,19 +41,23 @@ def grd2cpt(grid, **kwargs): This is a module that will help you make static color palette tables (CPTs). By default, the CPT will simply be saved to the current session, - but you can use *output* to save it to a file. You define an equidistant - set of contour intervals or pass your own z-table or list, and create a new - CPT based on an existing master (dynamic) CPT. The resulting CPT can be - reversed relative to the master cpt, and can be made continuous or - discrete. For color tables beyond the standard GMT offerings, visit - `cpt-city `_ and - `Scientific Colour-Maps `_. + but you can use *output* to save it to a file. The CPT is based on an + existing dynamic master CPT of your choice, and the mapping from data value + to colors is through the data's cumulative distribution function (CDF), so + that the colors are histogram equalized. Thus if the grid(s) and the + resulting CPT are used in :meth:`pygmt.Figure.grdimage` with a linear + projection, the colors will be uniformly distributed in area on the plot. + Let z be the data values in the grid. Define CDF(Z) = (# of z < Z) / (# of + z in grid). (NaNs are ignored). These z-values are then normalized to the + master CPT and colors are sampled at the desired intervals. The CPT includes three additional colors beyond the range of z-values. These are the background color (B) assigned to values lower than the lowest *z*-value, the foreground color (F) assigned to values higher than the highest *z*-value, and the NaN color (N) painted wherever values are - undefined. + undefined. For color tables beyond the standard GMT offerings, visit + `cpt-city `_ and + `Scientific Colour-Maps `_. If the master CPT includes B, F, and N entries, these will be copied into the new master file. If not, the parameters :gmt-term:`COLOR_BACKGROUND`, @@ -62,11 +66,12 @@ def grd2cpt(grid, **kwargs): default behavior can be overruled using the options *background*, *overrule_bg* or *no_bg*. - The color model (RGB, HSV or CMYK) of the palette created by **grdcpt** - will be the same as specified in the header of the master CPT. When there - is no :gmt-term:`COLOR_MODEL` entry in the master CPT, the - :gmt-term:`COLOR_MODEL` specified in the :gmt-docs:`gmt.conf ` - file or on the command line will be used. + The color model (RGB, HSV or CMYK) of the palette created by + :meth:`pygmt.grd2cpt` will be the same as specified in the header of the + master CPT. When there is no :gmt-term:`COLOR_MODEL` entry in the master + CPT, the :gmt-term:`COLOR_MODEL` specified in the + :gmt-docs:`gmt.conf ` file or the *color_model* option will be + used. Full option list at :gmt-docs:`grd2cpt.html` @@ -93,7 +98,7 @@ def grd2cpt(grid, **kwargs): ``background='i'`` to match the colors for the lowest and highest values in the input (instead of the output) CPT. color_model : - ``[R|r|h|c][+c[label]]``. + [**R**\|\ **r**\|\ **h**\|\ **c**][**+c**\ [*label*]]. Force output CPT to be written with r/g/b codes, gray-scale values or color name (**R**, default) or r/g/b codes only (**r**), or h-s-v codes (**h**), or c/m/y/k codes (**c**). Optionally or alternatively, append From 8aa3ba2ec48e722c7e70a272bce214b0984cd11f Mon Sep 17 00:00:00 2001 From: Will Schlitzer Date: Mon, 8 Feb 2021 10:53:09 +0000 Subject: [PATCH 36/56] Run make format --- pygmt/src/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pygmt/src/__init__.py b/pygmt/src/__init__.py index c7087f8f8e0..c3dc05bdefe 100644 --- a/pygmt/src/__init__.py +++ b/pygmt/src/__init__.py @@ -4,10 +4,10 @@ # pylint: disable=import-outside-toplevel from pygmt.src.basemap import basemap from pygmt.src.blockmedian import blockmedian -from pygmt.src.grd2cpt import grd2cpt from pygmt.src.coast import coast from pygmt.src.colorbar import colorbar from pygmt.src.contour import contour +from pygmt.src.grd2cpt import grd2cpt from pygmt.src.grdcontour import grdcontour from pygmt.src.grdcut import grdcut from pygmt.src.grdfilter import grdfilter From 3e4b017002139b7a3fd7760f93c82249d43f41ab Mon Sep 17 00:00:00 2001 From: Will Schlitzer Date: Mon, 8 Feb 2021 11:06:31 +0000 Subject: [PATCH 37/56] Wrap nlelvels function --- pygmt/src/grd2cpt.py | 5 +++++ pygmt/tests/test_grd2cpt.py | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+) diff --git a/pygmt/src/grd2cpt.py b/pygmt/src/grd2cpt.py index 565b5b7cd13..65e41334c40 100644 --- a/pygmt/src/grd2cpt.py +++ b/pygmt/src/grd2cpt.py @@ -20,6 +20,7 @@ C="cmap", D="background", F="color_model", + E="nlevels", G="truncate", H="output", I="reverse", @@ -109,6 +110,10 @@ def grd2cpt(grid, **kwargs): *start*[-], where we automatically build monotonically increasing labels from *start* (a single letter or an integer). Append - to build ranges *start*-*start+1* instead. + nlevels : bool or int or str + Set to ``True`` to create a linear color table by using the grid + z-range as the new limits in the CPT. Alternatively, set *nlevels* + and to resample the color table into nlevels equidistant slices. series : list or str ``[min/max/inc[+b|l|n]|file|list]``. Defines the range of the new CPT by giving the lowest and highest diff --git a/pygmt/tests/test_grd2cpt.py b/pygmt/tests/test_grd2cpt.py index 82cf0f9e66c..0a4451bb884 100644 --- a/pygmt/tests/test_grd2cpt.py +++ b/pygmt/tests/test_grd2cpt.py @@ -310,3 +310,35 @@ def test_grd2cpt_categorical_and_cyclic(grid): """ with pytest.raises(GMTInvalidInput): grd2cpt(grid=grid, cmap="batlow", categorical=True, cyclic=True) + + +@check_figures_equal() +def test_grd2cpt_nlevels_number(grid): + """ + Set number of equidistant slices with nlevels alias in CPT. + """ + fig_ref, fig_test = Figure(), Figure() + # Use single-character arguments for the reference image + fig_ref.basemap(B="a", J="W0/15c", R="d") + grd2cpt(grid="@earth_relief_01d", E="10") + fig_ref.colorbar(B="a2000") + fig_test.basemap(frame="a", projection="W0/15c", region="d") + grd2cpt(grid=grid, nlevels=10) + fig_test.colorbar(frame="a2000") + return fig_ref, fig_test + + +@check_figures_equal() +def test_grd2cpt_nlevels_bool(grid): + """ + Set equidistant slices with nlevels alias in CPT. + """ + fig_ref, fig_test = Figure(), Figure() + # Use single-character arguments for the reference image + fig_ref.basemap(B="a", J="W0/15c", R="d") + grd2cpt(grid="@earth_relief_01d", E="") + fig_ref.colorbar(B="a2000") + fig_test.basemap(frame="a", projection="W0/15c", region="d") + grd2cpt(grid=grid, nlevels=True) + fig_test.colorbar(frame="a2000") + return fig_ref, fig_test From 5924a8ee88212eaf384cbcc965a2dc15da339b32 Mon Sep 17 00:00:00 2001 From: Will Schlitzer Date: Mon, 8 Feb 2021 11:47:26 +0000 Subject: [PATCH 38/56] Format GMT arguments --- pygmt/src/grd2cpt.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/pygmt/src/grd2cpt.py b/pygmt/src/grd2cpt.py index 65e41334c40..658541e42bf 100644 --- a/pygmt/src/grd2cpt.py +++ b/pygmt/src/grd2cpt.py @@ -115,7 +115,7 @@ def grd2cpt(grid, **kwargs): z-range as the new limits in the CPT. Alternatively, set *nlevels* and to resample the color table into nlevels equidistant slices. series : list or str - ``[min/max/inc[+b|l|n]|file|list]``. + [*min/max/inc*\ [**+b**\|\**l**\|\**n**\]|\*file*\|\*list*\]. Defines the range of the new CPT by giving the lowest and highest z-value (and optionally an interval). If this is not given, the existing range in the master CPT will be used intact. The values @@ -124,15 +124,15 @@ def grd2cpt(grid, **kwargs): For details on array creation, see :gmt-docs:`makecpt.html#generate-1d-array`. truncate : list or str - ``zlo/zhi``. + *zlo/zhi*. Truncate the incoming CPT so that the lowest and highest z-levels are to *zlo* and *zhi*. If one of these equal NaN then we leave that end of the CPT alone. The truncation takes place before any resampling. See also :gmt-docs:`cookbook/features.html#manipulating-cpts`. output : str - Optional. The file name with extension .cpt to store the generated CPT - file. If not given or False (default), saves the CPT as the session - current CPT. + Optional argument to set the file name with extension .cpt to store + the generated CPT file. If not given or False (default), saves the CPT + as the session current CPT. reverse : str Set this to True or c [Default] to reverse the sense of color progression in the master CPT. Set this to z to reverse the sign of From a84f4242567be2157b893a2a7db8f519c5bd877a Mon Sep 17 00:00:00 2001 From: Will Schlitzer Date: Mon, 8 Feb 2021 11:50:03 +0000 Subject: [PATCH 39/56] Add raw string --- pygmt/src/grd2cpt.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pygmt/src/grd2cpt.py b/pygmt/src/grd2cpt.py index 658541e42bf..7fc0613419d 100644 --- a/pygmt/src/grd2cpt.py +++ b/pygmt/src/grd2cpt.py @@ -37,7 +37,7 @@ ) @kwargs_to_strings(T="sequence", G="sequence", L="sequence", R="sequence") def grd2cpt(grid, **kwargs): - """ + r""" Make GMT color palette tables from a grid file. This is a module that will help you make static color palette tables From e12ea34023a8a27c8c7ba897afda6e74d254dcca Mon Sep 17 00:00:00 2001 From: Will Schlitzer Date: Mon, 8 Feb 2021 16:54:20 +0000 Subject: [PATCH 40/56] Format fix --- pygmt/src/grd2cpt.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pygmt/src/grd2cpt.py b/pygmt/src/grd2cpt.py index 7fc0613419d..325be74fadf 100644 --- a/pygmt/src/grd2cpt.py +++ b/pygmt/src/grd2cpt.py @@ -115,7 +115,7 @@ def grd2cpt(grid, **kwargs): z-range as the new limits in the CPT. Alternatively, set *nlevels* and to resample the color table into nlevels equidistant slices. series : list or str - [*min/max/inc*\ [**+b**\|\**l**\|\**n**\]|\*file*\|\*list*\]. + [*min/max/inc*\ [**+b**\|\ **l**\|\ **n**\]|\ *file*\|\ *list*\]. Defines the range of the new CPT by giving the lowest and highest z-value (and optionally an interval). If this is not given, the existing range in the master CPT will be used intact. The values From dae83e49b5b1f62158292af08f40e8ee00ac798f Mon Sep 17 00:00:00 2001 From: Will Schlitzer Date: Wed, 10 Feb 2021 13:57:38 +0000 Subject: [PATCH 41/56] Apply suggestions from code review Co-authored-by: Dongdong Tian --- doc/api/index.rst | 2 +- pygmt/src/grd2cpt.py | 12 ++++++------ 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/doc/api/index.rst b/doc/api/index.rst index be3bf3e111d..338adfefb1a 100644 --- a/doc/api/index.rst +++ b/doc/api/index.rst @@ -44,8 +44,8 @@ Color palette table generation: .. autosummary:: :toctree: generated - makecpt grd2cpt + makecpt Saving and displaying the figure: diff --git a/pygmt/src/grd2cpt.py b/pygmt/src/grd2cpt.py index 325be74fadf..a1d799107bb 100644 --- a/pygmt/src/grd2cpt.py +++ b/pygmt/src/grd2cpt.py @@ -42,7 +42,7 @@ def grd2cpt(grid, **kwargs): This is a module that will help you make static color palette tables (CPTs). By default, the CPT will simply be saved to the current session, - but you can use *output* to save it to a file. The CPT is based on an + but you can use ``output`` to save it to a file. The CPT is based on an existing dynamic master CPT of your choice, and the mapping from data value to colors is through the data's cumulative distribution function (CDF), so that the colors are histogram equalized. Thus if the grid(s) and the @@ -71,7 +71,7 @@ def grd2cpt(grid, **kwargs): :meth:`pygmt.grd2cpt` will be the same as specified in the header of the master CPT. When there is no :gmt-term:`COLOR_MODEL` entry in the master CPT, the :gmt-term:`COLOR_MODEL` specified in the - :gmt-docs:`gmt.conf ` file or the *color_model* option will be + :gmt-docs:`gmt.conf ` file or the ``color_model`` option will be used. Full option list at :gmt-docs:`grd2cpt.html` @@ -82,7 +82,7 @@ def grd2cpt(grid, **kwargs): ---------- grid : str or xarray.DataArray The file name of the input grid or the grid loaded as a DataArray. - transparency : str + transparency : int or float or str Sets a constant level of transparency (0-100) for all color slices. Append **+a** to also affect the fore-, back-, and nan-colors [Default is no transparency, i.e., 0 (opaque)]. @@ -107,8 +107,8 @@ def grd2cpt(grid, **kwargs): appended then we create labels for each category to be used when the CPT is plotted. The *label* may be a comma-separated list of category names (you can skip a category by not giving a name), or give - *start*[-], where we automatically build monotonically increasing - labels from *start* (a single letter or an integer). Append - to build + *start*\[-], where we automatically build monotonically increasing + labels from *start* (a single letter or an integer). Append ``-`` to build ranges *start*-*start+1* instead. nlevels : bool or int or str Set to ``True`` to create a linear color table by using the grid @@ -174,7 +174,7 @@ def grd2cpt(grid, **kwargs): elif kind == "grid": file_context = lib.virtualfile_from_grid(grid) else: - raise GMTInvalidInput("Unrecognized data type: {}".format(type(grid))) + raise GMTInvalidInput(f"Unrecognized data type: {type(grid)}") with file_context as infile: if "W" in kwargs and "Ww" in kwargs: From 1ef9fb97a12d3d669b5bdd121a034c8adbb503d0 Mon Sep 17 00:00:00 2001 From: Will Schlitzer Date: Wed, 10 Feb 2021 14:00:09 +0000 Subject: [PATCH 42/56] Fix line length issue --- pygmt/src/grd2cpt.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pygmt/src/grd2cpt.py b/pygmt/src/grd2cpt.py index a1d799107bb..c7813fb0ee8 100644 --- a/pygmt/src/grd2cpt.py +++ b/pygmt/src/grd2cpt.py @@ -108,8 +108,8 @@ def grd2cpt(grid, **kwargs): CPT is plotted. The *label* may be a comma-separated list of category names (you can skip a category by not giving a name), or give *start*\[-], where we automatically build monotonically increasing - labels from *start* (a single letter or an integer). Append ``-`` to build - ranges *start*-*start+1* instead. + labels from *start* (a single letter or an integer). Append ``-`` to + build ranges *start*-*start+1* instead. nlevels : bool or int or str Set to ``True`` to create a linear color table by using the grid z-range as the new limits in the CPT. Alternatively, set *nlevels* From 72130a99d6bf560f2df4114fd841d089651a6a66 Mon Sep 17 00:00:00 2001 From: Will Schlitzer Date: Wed, 10 Feb 2021 14:33:45 +0000 Subject: [PATCH 43/56] Remove unnecessary test --- pygmt/tests/test_grd2cpt.py | 9 --------- 1 file changed, 9 deletions(-) diff --git a/pygmt/tests/test_grd2cpt.py b/pygmt/tests/test_grd2cpt.py index 0a4451bb884..c13e263b75f 100644 --- a/pygmt/tests/test_grd2cpt.py +++ b/pygmt/tests/test_grd2cpt.py @@ -286,15 +286,6 @@ def test_grd2cpt_invalid_output(grid): grd2cpt(grid=grid, output=["some.cpt"]) -def test_grd2cpt_output_to_cpt_file(grid): - """ - Save the generated static color palette table to a .cpt file. - """ - with GMTTempFile(suffix=".cpt") as cptfile: - grd2cpt(grid=grid, output=cptfile.name) - assert os.path.exists(cptfile.name) - - def test_grd2cpt_unrecognized_data_type(): """ Test that an error will be raised if an invalid data type is passed to From 1013c3f53f267d9d077687f29f678401b0ef430a Mon Sep 17 00:00:00 2001 From: Will Schlitzer Date: Wed, 10 Feb 2021 14:37:15 +0000 Subject: [PATCH 44/56] Fixed saving CPT issue --- pygmt/src/grd2cpt.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/pygmt/src/grd2cpt.py b/pygmt/src/grd2cpt.py index c7813fb0ee8..bb19c6a62d5 100644 --- a/pygmt/src/grd2cpt.py +++ b/pygmt/src/grd2cpt.py @@ -181,8 +181,9 @@ def grd2cpt(grid, **kwargs): raise GMTInvalidInput( "Set only categorical or cyclic to True, not both." ) - - if "H" in kwargs.keys(): # if output file is set + if "H" not in kwargs.keys(): # if no output is set + arg_str = build_arg_string(kwargs) + elif "H" in kwargs.keys(): # if output is set outfile = kwargs.pop("H") if not outfile or not isinstance(outfile, str): raise GMTInvalidInput("'output' should be a proper file name.") From 201f0f657a4ac80b442c80169ad5581c34ba4f46 Mon Sep 17 00:00:00 2001 From: Will Schlitzer Date: Wed, 10 Feb 2021 14:40:31 +0000 Subject: [PATCH 45/56] Correct arg_string building --- pygmt/src/grd2cpt.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pygmt/src/grd2cpt.py b/pygmt/src/grd2cpt.py index bb19c6a62d5..b34d288caf8 100644 --- a/pygmt/src/grd2cpt.py +++ b/pygmt/src/grd2cpt.py @@ -182,10 +182,10 @@ def grd2cpt(grid, **kwargs): "Set only categorical or cyclic to True, not both." ) if "H" not in kwargs.keys(): # if no output is set - arg_str = build_arg_string(kwargs) + arg_str = " ".join([infile, build_arg_string(kwargs)]) elif "H" in kwargs.keys(): # if output is set outfile = kwargs.pop("H") if not outfile or not isinstance(outfile, str): raise GMTInvalidInput("'output' should be a proper file name.") - arg_str = " ".join([infile, build_arg_string(kwargs)]) + arg_str = " ".join([infile, build_arg_string(kwargs), f"-H > {outfile}"]) lib.call_module("grd2cpt", arg_str) From c8c88118ef1224fbd9cc4e6d37f9c4fd4881b75a Mon Sep 17 00:00:00 2001 From: Will Schlitzer Date: Wed, 10 Feb 2021 14:41:34 +0000 Subject: [PATCH 46/56] Run make format --- pygmt/src/grd2cpt.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/pygmt/src/grd2cpt.py b/pygmt/src/grd2cpt.py index b34d288caf8..ef6aa7246b1 100644 --- a/pygmt/src/grd2cpt.py +++ b/pygmt/src/grd2cpt.py @@ -187,5 +187,7 @@ def grd2cpt(grid, **kwargs): outfile = kwargs.pop("H") if not outfile or not isinstance(outfile, str): raise GMTInvalidInput("'output' should be a proper file name.") - arg_str = " ".join([infile, build_arg_string(kwargs), f"-H > {outfile}"]) + arg_str = " ".join( + [infile, build_arg_string(kwargs), f"-H > {outfile}"] + ) lib.call_module("grd2cpt", arg_str) From cfcb78cce69f34e8d511149b887287b80cbd8311 Mon Sep 17 00:00:00 2001 From: Will Schlitzer Date: Wed, 10 Feb 2021 14:47:20 +0000 Subject: [PATCH 47/56] Fix style issue --- pygmt/tests/test_grd2cpt.py | 1 - 1 file changed, 1 deletion(-) diff --git a/pygmt/tests/test_grd2cpt.py b/pygmt/tests/test_grd2cpt.py index c13e263b75f..a07ce0bd840 100644 --- a/pygmt/tests/test_grd2cpt.py +++ b/pygmt/tests/test_grd2cpt.py @@ -8,7 +8,6 @@ from pygmt import Figure from pygmt.datasets import load_earth_relief from pygmt.exceptions import GMTInvalidInput -from pygmt.helpers import GMTTempFile from pygmt.helpers.testing import check_figures_equal from pygmt.src.grd2cpt import grd2cpt From 93fc6861b92daf7fb3ab99ef8a7ee9b446f2b8de Mon Sep 17 00:00:00 2001 From: Will Schlitzer Date: Wed, 10 Feb 2021 15:47:17 +0000 Subject: [PATCH 48/56] Move GMTInvalidInput raising statement outside of context manager --- pygmt/src/grd2cpt.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/pygmt/src/grd2cpt.py b/pygmt/src/grd2cpt.py index ef6aa7246b1..3b565403015 100644 --- a/pygmt/src/grd2cpt.py +++ b/pygmt/src/grd2cpt.py @@ -167,6 +167,10 @@ def grd2cpt(grid, **kwargs): ``categorical=True``. {V} """ + if "W" in kwargs and "Ww" in kwargs: + raise GMTInvalidInput( + "Set only categorical or cyclic to True, not both." + ) kind = data_kind(grid) with Session() as lib: if kind == "file": @@ -175,12 +179,7 @@ def grd2cpt(grid, **kwargs): file_context = lib.virtualfile_from_grid(grid) else: raise GMTInvalidInput(f"Unrecognized data type: {type(grid)}") - with file_context as infile: - if "W" in kwargs and "Ww" in kwargs: - raise GMTInvalidInput( - "Set only categorical or cyclic to True, not both." - ) if "H" not in kwargs.keys(): # if no output is set arg_str = " ".join([infile, build_arg_string(kwargs)]) elif "H" in kwargs.keys(): # if output is set From 9cd968491b6ff114c2632251b7d653a5e74bbc68 Mon Sep 17 00:00:00 2001 From: Will Schlitzer Date: Wed, 10 Feb 2021 15:47:46 +0000 Subject: [PATCH 49/56] Revert "Remove unnecessary test" This reverts commit 72130a99 --- pygmt/tests/test_grd2cpt.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/pygmt/tests/test_grd2cpt.py b/pygmt/tests/test_grd2cpt.py index a07ce0bd840..268c70073d7 100644 --- a/pygmt/tests/test_grd2cpt.py +++ b/pygmt/tests/test_grd2cpt.py @@ -285,6 +285,15 @@ def test_grd2cpt_invalid_output(grid): grd2cpt(grid=grid, output=["some.cpt"]) +def test_grd2cpt_output_to_cpt_file(grid): + """ + Save the generated static color palette table to a .cpt file. + """ + with GMTTempFile(suffix=".cpt") as cptfile: + grd2cpt(grid=grid, output=cptfile.name) + assert os.path.exists(cptfile.name) + + def test_grd2cpt_unrecognized_data_type(): """ Test that an error will be raised if an invalid data type is passed to From e0ad7f7ee7b6610f823f05678c898f543676df63 Mon Sep 17 00:00:00 2001 From: Will Schlitzer Date: Wed, 10 Feb 2021 15:56:52 +0000 Subject: [PATCH 50/56] Change test to determine if CPT file size is greater than zero --- pygmt/src/grd2cpt.py | 4 +--- pygmt/tests/test_grd2cpt.py | 3 ++- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/pygmt/src/grd2cpt.py b/pygmt/src/grd2cpt.py index 3b565403015..667b50bafe8 100644 --- a/pygmt/src/grd2cpt.py +++ b/pygmt/src/grd2cpt.py @@ -168,9 +168,7 @@ def grd2cpt(grid, **kwargs): {V} """ if "W" in kwargs and "Ww" in kwargs: - raise GMTInvalidInput( - "Set only categorical or cyclic to True, not both." - ) + raise GMTInvalidInput("Set only categorical or cyclic to True, not both.") kind = data_kind(grid) with Session() as lib: if kind == "file": diff --git a/pygmt/tests/test_grd2cpt.py b/pygmt/tests/test_grd2cpt.py index 268c70073d7..3c39f7fb3e5 100644 --- a/pygmt/tests/test_grd2cpt.py +++ b/pygmt/tests/test_grd2cpt.py @@ -8,6 +8,7 @@ from pygmt import Figure from pygmt.datasets import load_earth_relief from pygmt.exceptions import GMTInvalidInput +from pygmt.helpers import GMTTempFile from pygmt.helpers.testing import check_figures_equal from pygmt.src.grd2cpt import grd2cpt @@ -291,7 +292,7 @@ def test_grd2cpt_output_to_cpt_file(grid): """ with GMTTempFile(suffix=".cpt") as cptfile: grd2cpt(grid=grid, output=cptfile.name) - assert os.path.exists(cptfile.name) + assert os.path.getsize(cptfile.name) > 0 def test_grd2cpt_unrecognized_data_type(): From 33602588dd9b67e56d3ed37a215338dd5e9c5e3f Mon Sep 17 00:00:00 2001 From: Dongdong Tian Date: Thu, 11 Feb 2021 11:08:01 -0500 Subject: [PATCH 51/56] Update pygmt/src/grd2cpt.py --- pygmt/src/grd2cpt.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pygmt/src/grd2cpt.py b/pygmt/src/grd2cpt.py index 667b50bafe8..bdfab2f75c8 100644 --- a/pygmt/src/grd2cpt.py +++ b/pygmt/src/grd2cpt.py @@ -64,8 +64,8 @@ def grd2cpt(grid, **kwargs): the new master file. If not, the parameters :gmt-term:`COLOR_BACKGROUND`, :gmt-term:`COLOR_FOREGROUND`, and :gmt-term:`COLOR_NAN` from the :gmt-docs:`gmt.conf ` file or the command line will be used. This - default behavior can be overruled using the options *background*, - *overrule_bg* or *no_bg*. + default behavior can be overruled using the options ``background``, + ``overrule_bg`` or ``no_bg``. The color model (RGB, HSV or CMYK) of the palette created by :meth:`pygmt.grd2cpt` will be the same as specified in the header of the From bf68c8545d2fcdf445d3b1a29503c87af9827ccd Mon Sep 17 00:00:00 2001 From: Will Schlitzer Date: Fri, 12 Feb 2021 06:49:20 +0000 Subject: [PATCH 52/56] Remove tests in test_grd2cpt.py --- pygmt/tests/test_grd2cpt.py | 245 ------------------------------------ 1 file changed, 245 deletions(-) diff --git a/pygmt/tests/test_grd2cpt.py b/pygmt/tests/test_grd2cpt.py index 3c39f7fb3e5..a532d8ac283 100644 --- a/pygmt/tests/test_grd2cpt.py +++ b/pygmt/tests/test_grd2cpt.py @@ -57,219 +57,6 @@ def test_grd2cpt(grid): return fig_ref, fig_test -@check_figures_equal() -def test_grd2cpt_to_plot_points(points, region, grid): - """ - Use CPT to change color of points. - """ - fig_ref, fig_test = Figure(), Figure() - # Use single-character arguments for the reference image - fig_ref.basemap(R=region, J="X15c", B="a") - grd2cpt(grid=grid, C="rainbow") - fig_ref.plot( - x=points[:, 0], y=points[:, 1], G=points[:, 2], R=region, S="c1c", C=True - ) - fig_test.basemap(region=region, projection="X15c", frame="a") - grd2cpt(grid=grid, cmap="rainbow") - fig_test.plot( - x=points[:, 0], - y=points[:, 1], - color=points[:, 2], - region=region, - style="c1c", - cmap=True, - ) - return fig_ref, fig_test - - -@check_figures_equal() -def test_grd2cpt_set_cpt(grid): - """ - Test function grd2cpt to create a CPT based off a grid input and a set CPT. - """ - fig_ref, fig_test = Figure(), Figure() - # Use single-character arguments for the reference image - fig_ref.basemap(B="a", J="W0/15c", R="d") - grd2cpt(grid=grid, cmap="rainbow") - fig_ref.colorbar(B="a2000") - fig_test.basemap(frame="a", projection="W0/15c", region="d") - grd2cpt(grid=grid, cmap="rainbow") - fig_test.colorbar(frame="a2000") - return fig_ref, fig_test - - -@check_figures_equal() -def test_grd2cpt_scaled_with_series(grid): - """ - Create CPT scaled to a min/max series to change color of grid. - """ - fig_ref, fig_test = Figure(), Figure() - fig_ref.basemap(B="a", J="W0/15c", R="d") - grd2cpt(grid=grid, cmap="rainbow", T="-4500/4500/500") - fig_ref.colorbar(B="a2000") - fig_test.basemap(frame="a", projection="W0/15c", region="d") - grd2cpt(grid=grid, cmap="rainbow", series=[-4500, 4500, 500]) - fig_test.colorbar(frame="a2000") - return fig_ref, fig_test - - -@check_figures_equal() -def test_grd2cpt_truncated_to_zlow_zhigh(grid): - """ - Create CPT that is truncated to z-low and z-high. - """ - fig_ref, fig_test = Figure(), Figure() - fig_ref.basemap(B="a", J="W0/15c", R="d") - grd2cpt(grid=grid, cmap="rainbow", G="0.15/0.85", T="-4500/4500/500") - fig_ref.colorbar(B="a2000") - fig_test.basemap(frame="a", projection="W0/15c", region="d") - grd2cpt(grid=grid, cmap="rainbow", truncate=[0.15, 0.85], series=[-4500, 4500, 500]) - fig_test.colorbar(frame="a2000") - return fig_ref, fig_test - - -@check_figures_equal() -def test_grd2cpt_truncated_to_zlow_only(grid): - """ - Create CPT that is truncated at z-low only. - """ - fig_ref, fig_test = Figure(), Figure() - fig_ref.basemap(B="a", J="W0/15c", R="d") - grd2cpt(grid=grid, cmap="rainbow", G="0.5/NaN", T="-4500/4500/500") - fig_ref.colorbar(B="a2000") - fig_test.basemap(frame="a", projection="W0/15c", region="d") - grd2cpt(grid=grid, cmap="rainbow", truncate=[0.5, None], series=[-4500, 4500, 500]) - fig_test.colorbar(frame="a2000") - return fig_ref, fig_test - - -@check_figures_equal() -def test_grd2cpt_truncated_to_zhigh_only(grid): - """ - Create CPT that is truncated at z-high only. - """ - fig_ref, fig_test = Figure(), Figure() - fig_ref.basemap(B="a", J="W0/15c", R="d") - grd2cpt(grid=grid, cmap="rainbow", G="NaN/0.5", T="-4500/4500/500") - fig_ref.colorbar(B="a2000") - fig_test.basemap(frame="a", projection="W0/15c", region="d") - grd2cpt(grid=grid, cmap="rainbow", truncate=[None, 0.5], series=[-4500, 4500, 500]) - fig_test.colorbar(frame="a2000") - return fig_ref, fig_test - - -@check_figures_equal() -def test_grd2cpt_reverse_color_only(grid): - """ - Create CPT with its colors reversed. - """ - fig_ref, fig_test = Figure(), Figure() - # Use single-character arguments for the reference image - fig_ref.basemap(B="a", J="W0/15c", R="d") - grd2cpt(grid=grid, C="rainbow", I=True) - fig_ref.colorbar(B="a2000") - fig_test.basemap(frame="a", projection="W0/15c", region="d") - grd2cpt(grid=grid, cmap="rainbow", reverse=True) - fig_test.colorbar(frame="a2000") - return fig_ref, fig_test - - -@check_figures_equal() -def test_grd2cpt_reverse_zsign_only(grid): - """ - Create CPT with its z-value sign reversed. - """ - fig_ref, fig_test = Figure(), Figure() - # Use single-character arguments for the reference image - fig_ref.basemap(B="a", J="W0/15c", R="d") - grd2cpt(grid=grid, C="earth", I="z") - fig_ref.colorbar(B="a2000") - fig_test.basemap(frame="a", projection="W0/15c", region="d") - grd2cpt(grid=grid, cmap="earth", reverse="z") - fig_test.colorbar(frame="a2000") - return fig_ref, fig_test - - -@check_figures_equal() -def test_grd2cpt_reverse_color_and_zsign(grid): - """ - Create CPT with its colors and z-value sign reversed. - """ - fig_ref, fig_test = Figure(), Figure() - # Use single-character arguments for the reference image - fig_ref.basemap(B="a", J="W0/15c", R="d") - grd2cpt(grid=grid, C="earth", I="cz") - fig_ref.colorbar(B="a2000") - fig_test.basemap(frame="a", projection="W0/15c", region="d") - grd2cpt(grid=grid, cmap="earth", reverse="cz") - fig_test.colorbar(frame="a2000") - return fig_ref, fig_test - - -@check_figures_equal() -def test_grd2cpt_continuous(grid): - """ - Create a CPT with that is continuous. - """ - fig_ref, fig_test = Figure(), Figure() - # Use single-character arguments for the reference image - fig_ref.basemap(B="a", J="W0/15c", R="d") - grd2cpt(grid=grid, C="blue,white", Z=True) - fig_ref.colorbar(B="a2000") - fig_test.basemap(frame="a", projection="W0/15c", region="d") - grd2cpt(grid=grid, cmap="blue,white", continuous=True) - fig_test.colorbar(frame="a2000") - return fig_ref, fig_test - - -@check_figures_equal() -def test_grd2cpt_categorical(grid): - """ - Create a CPT with that is categorical. - """ - fig_ref, fig_test = Figure(), Figure() - # Use single-character arguments for the reference image - fig_ref.basemap(B="a", J="W0/15c", R="d") - grd2cpt(grid=grid, C="geo", W=True) - fig_ref.colorbar(B="a2000") - fig_test.basemap(frame="a", projection="W0/15c", region="d") - grd2cpt(grid=grid, cmap="geo", categorical=True) - fig_test.colorbar(frame="a2000") - return fig_ref, fig_test - - -@check_figures_equal() -def test_grd2cpt_cyclic(grid): - """ - Create a CPT with that is cyclic. - """ - fig_ref, fig_test = Figure(), Figure() - # Use single-character arguments for the reference image - fig_ref.basemap(B="a", J="W0/15c", R="d") - grd2cpt(grid=grid, C="geo", Ww=True) - fig_ref.colorbar(B="a2000") - fig_test.basemap(frame="a", projection="W0/15c", region="d") - grd2cpt(grid=grid, cmap="geo", cyclic=True) - fig_test.colorbar(frame="a2000") - return fig_ref, fig_test - - -@check_figures_equal() -def test_grd2cpt_limit(grid): - """ - Create a CPT with that sets a min/max limit. - """ - fig_ref, fig_test = Figure(), Figure() - # Use single-character arguments for the reference image - fig_ref.basemap(B="a", J="W0/15c", R="d") - grd2cpt(grid=grid, C="haxby", L="-1000/1000") - fig_ref.colorbar(B="a2000") - fig_test.basemap(frame="a", projection="W0/15c", region="d") - grd2cpt(grid=grid, cmap="haxby", limit=[-1000, 1000]) - fig_test.colorbar(frame="a2000") - return fig_ref, fig_test - - def test_grd2cpt_blank_output(grid): """ Use incorrect setting by passing in blank file name to output parameter. @@ -310,35 +97,3 @@ def test_grd2cpt_categorical_and_cyclic(grid): """ with pytest.raises(GMTInvalidInput): grd2cpt(grid=grid, cmap="batlow", categorical=True, cyclic=True) - - -@check_figures_equal() -def test_grd2cpt_nlevels_number(grid): - """ - Set number of equidistant slices with nlevels alias in CPT. - """ - fig_ref, fig_test = Figure(), Figure() - # Use single-character arguments for the reference image - fig_ref.basemap(B="a", J="W0/15c", R="d") - grd2cpt(grid="@earth_relief_01d", E="10") - fig_ref.colorbar(B="a2000") - fig_test.basemap(frame="a", projection="W0/15c", region="d") - grd2cpt(grid=grid, nlevels=10) - fig_test.colorbar(frame="a2000") - return fig_ref, fig_test - - -@check_figures_equal() -def test_grd2cpt_nlevels_bool(grid): - """ - Set equidistant slices with nlevels alias in CPT. - """ - fig_ref, fig_test = Figure(), Figure() - # Use single-character arguments for the reference image - fig_ref.basemap(B="a", J="W0/15c", R="d") - grd2cpt(grid="@earth_relief_01d", E="") - fig_ref.colorbar(B="a2000") - fig_test.basemap(frame="a", projection="W0/15c", region="d") - grd2cpt(grid=grid, nlevels=True) - fig_test.colorbar(frame="a2000") - return fig_ref, fig_test From ea4244bc3d30db190a5bbf7844fb9fadadd9daa7 Mon Sep 17 00:00:00 2001 From: Will Schlitzer Date: Fri, 12 Feb 2021 07:35:03 +0000 Subject: [PATCH 53/56] Update pygmt/tests/test_grd2cpt.py Co-authored-by: Dongdong Tian --- pygmt/tests/test_grd2cpt.py | 19 ------------------- 1 file changed, 19 deletions(-) diff --git a/pygmt/tests/test_grd2cpt.py b/pygmt/tests/test_grd2cpt.py index a532d8ac283..5f5b50f0b3d 100644 --- a/pygmt/tests/test_grd2cpt.py +++ b/pygmt/tests/test_grd2cpt.py @@ -12,25 +12,6 @@ from pygmt.helpers.testing import check_figures_equal from pygmt.src.grd2cpt import grd2cpt -TEST_DATA_DIR = os.path.join(os.path.dirname(__file__), "data") -POINTS_DATA = os.path.join(TEST_DATA_DIR, "points.txt") - - -@pytest.fixture(scope="module", name="points") -def fixture_points(): - """ - Load the points data from the test file. - """ - return np.loadtxt(POINTS_DATA) - - -@pytest.fixture(scope="module", name="region") -def fixture_region(): - """ - The data region. - """ - return [10, 70, -5, 10] - @pytest.fixture(scope="module", name="grid") def fixture_grid(): From 826c8f94b3ea81487a8405c423741d3d34fbd44f Mon Sep 17 00:00:00 2001 From: Will Schlitzer Date: Fri, 12 Feb 2021 08:05:17 +0000 Subject: [PATCH 54/56] Remove unused import in test_grd2cpt.py --- pygmt/tests/test_grd2cpt.py | 1 - 1 file changed, 1 deletion(-) diff --git a/pygmt/tests/test_grd2cpt.py b/pygmt/tests/test_grd2cpt.py index 5f5b50f0b3d..26a83cced36 100644 --- a/pygmt/tests/test_grd2cpt.py +++ b/pygmt/tests/test_grd2cpt.py @@ -3,7 +3,6 @@ """ import os -import numpy as np import pytest from pygmt import Figure from pygmt.datasets import load_earth_relief From ecb7fdc92ac20df3fa0dc7ed2e66e6721b72e32f Mon Sep 17 00:00:00 2001 From: Will Schlitzer Date: Fri, 12 Feb 2021 08:30:24 +0000 Subject: [PATCH 55/56] Apply suggestions from code review Co-authored-by: Wei Ji <23487320+weiji14@users.noreply.github.com> --- pygmt/src/grd2cpt.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/pygmt/src/grd2cpt.py b/pygmt/src/grd2cpt.py index bdfab2f75c8..569a3232060 100644 --- a/pygmt/src/grd2cpt.py +++ b/pygmt/src/grd2cpt.py @@ -35,7 +35,7 @@ Ww="cyclic", Z="continuous", ) -@kwargs_to_strings(T="sequence", G="sequence", L="sequence", R="sequence") +@kwargs_to_strings(G="sequence", L="sequence", R="sequence", T="sequence") def grd2cpt(grid, **kwargs): r""" Make GMT color palette tables from a grid file. @@ -112,8 +112,8 @@ def grd2cpt(grid, **kwargs): build ranges *start*-*start+1* instead. nlevels : bool or int or str Set to ``True`` to create a linear color table by using the grid - z-range as the new limits in the CPT. Alternatively, set *nlevels* - and to resample the color table into nlevels equidistant slices. + z-range as the new limits in the CPT. Alternatively, set *nlevels* + to resample the color table into *nlevels* equidistant slices. series : list or str [*min/max/inc*\ [**+b**\|\ **l**\|\ **n**\]|\ *file*\|\ *list*\]. Defines the range of the new CPT by giving the lowest and highest @@ -140,7 +140,7 @@ def grd2cpt(grid, **kwargs): happens before *truncate* and *series* values are used so the latter must be compatible with the changed *z*-range. See also :gmt-docs:`cookbook/features.html#manipulating-cpts`. - overrule_bg : + overrule_bg : str Overrule background, foreground, and NaN colors specified in the master CPT with the values of the parameters :gmt-term:`COLOR_BACKGROUND`, :gmt-term:`COLOR_FOREGROUND`, and :gmt-term:`COLOR_NAN` specified in From e0fd4ab0a2a0ad38cd0502d5a9443b8237513fcd Mon Sep 17 00:00:00 2001 From: Will Schlitzer Date: Fri, 12 Feb 2021 08:32:25 +0000 Subject: [PATCH 56/56] Add string input in overrule_bg docstring in makecpt.py --- pygmt/src/makecpt.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pygmt/src/makecpt.py b/pygmt/src/makecpt.py index e4c9f7190a0..bdad3f07952 100644 --- a/pygmt/src/makecpt.py +++ b/pygmt/src/makecpt.py @@ -118,7 +118,7 @@ def makecpt(**kwargs): happens before *truncate* and *series* values are used so the latter must be compatible with the changed *z*-range. See also :gmt-docs:`cookbook/features.html#manipulating-cpts`. - overrule_bg : + overrule_bg : str Overrule background, foreground, and NaN colors specified in the master CPT with the values of the parameters :gmt-term:`COLOR_BACKGROUND`, :gmt-term:`COLOR_FOREGROUND`, and :gmt-term:`COLOR_NAN` specified in