|
| 1 | +# pylint: disable=redefined-outer-name |
| 2 | +""" |
| 3 | +Tests for makecpt |
| 4 | +""" |
| 5 | +import os |
| 6 | + |
| 7 | +import numpy as np |
| 8 | +import pytest |
| 9 | + |
| 10 | +from .. import Figure, makecpt |
| 11 | +from ..datasets import load_earth_relief |
| 12 | +from ..exceptions import GMTInvalidInput |
| 13 | +from ..helpers import GMTTempFile |
| 14 | + |
| 15 | +TEST_DATA_DIR = os.path.join(os.path.dirname(__file__), "data") |
| 16 | +POINTS_DATA = os.path.join(TEST_DATA_DIR, "points.txt") |
| 17 | + |
| 18 | + |
| 19 | +@pytest.fixture(scope="module") |
| 20 | +def points(): |
| 21 | + "Load the points data from the test file" |
| 22 | + return np.loadtxt(POINTS_DATA) |
| 23 | + |
| 24 | + |
| 25 | +@pytest.fixture(scope="module") |
| 26 | +def region(): |
| 27 | + "The data region" |
| 28 | + return [10, 70, -5, 10] |
| 29 | + |
| 30 | + |
| 31 | +@pytest.fixture(scope="module") |
| 32 | +def grid(): |
| 33 | + "Load the grid data from the sample earth_relief file" |
| 34 | + return load_earth_relief() |
| 35 | + |
| 36 | + |
| 37 | +@pytest.mark.mpl_image_compare |
| 38 | +def test_makecpt_to_plot_points(points, region): |
| 39 | + """ |
| 40 | + Use static color palette table to change color of points |
| 41 | + """ |
| 42 | + fig = Figure() |
| 43 | + makecpt(cmap="rainbow") |
| 44 | + fig.plot( |
| 45 | + x=points[:, 0], |
| 46 | + y=points[:, 1], |
| 47 | + color=points[:, 2], |
| 48 | + region=region, |
| 49 | + style="c1c", |
| 50 | + cmap=True, |
| 51 | + ) |
| 52 | + return fig |
| 53 | + |
| 54 | + |
| 55 | +@pytest.mark.mpl_image_compare |
| 56 | +def test_makecpt_to_plot_grid(grid): |
| 57 | + """ |
| 58 | + Use static color palette table to change color of grid |
| 59 | + """ |
| 60 | + fig = Figure() |
| 61 | + makecpt(cmap="relief") |
| 62 | + fig.grdimage(grid, projection="W0/6i") |
| 63 | + return fig |
| 64 | + |
| 65 | + |
| 66 | +@pytest.mark.mpl_image_compare |
| 67 | +def test_makecpt_to_plot_grid_scaled_with_series(grid): |
| 68 | + """ |
| 69 | + Use static color palette table scaled to a min/max series to change color of grid |
| 70 | + """ |
| 71 | + fig = Figure() |
| 72 | + makecpt(cmap="oleron", series="-4500/4500") |
| 73 | + fig.grdimage(grid, projection="W0/6i") |
| 74 | + return fig |
| 75 | + |
| 76 | + |
| 77 | +def test_makecpt_output_to_cpt_file(): |
| 78 | + """ |
| 79 | + Save the generated static color palette table to a .cpt file |
| 80 | + """ |
| 81 | + with GMTTempFile(suffix=".cpt") as cptfile: |
| 82 | + makecpt(output=cptfile.name) |
| 83 | + assert os.path.exists(cptfile.name) |
| 84 | + |
| 85 | + |
| 86 | +def test_makecpt_blank_output(): |
| 87 | + """ |
| 88 | + Use incorrect setting by passing in blank file name to output parameter |
| 89 | + """ |
| 90 | + with pytest.raises(GMTInvalidInput): |
| 91 | + makecpt(output="") |
| 92 | + |
| 93 | + |
| 94 | +def test_makecpt_invalid_output(): |
| 95 | + """ |
| 96 | + Use incorrect setting by passing in invalid type to output parameter |
| 97 | + """ |
| 98 | + with pytest.raises(GMTInvalidInput): |
| 99 | + makecpt(output=["some.cpt"]) |
| 100 | + |
| 101 | + |
| 102 | +@pytest.mark.mpl_image_compare |
| 103 | +def test_makecpt_truncated_to_zlow_zhigh(grid): |
| 104 | + """ |
| 105 | + Use static color palette table that is truncated to z-low and z-high |
| 106 | + """ |
| 107 | + fig = Figure() |
| 108 | + makecpt(cmap="rainbow", truncate=[0.15, 0.85], series=[-4500, 4500]) |
| 109 | + fig.grdimage(grid, projection="W0/6i") |
| 110 | + return fig |
| 111 | + |
| 112 | + |
| 113 | +@pytest.mark.mpl_image_compare |
| 114 | +def test_makecpt_truncated_at_zlow_only(grid): |
| 115 | + """ |
| 116 | + Use static color palette table that is truncated at z-low only |
| 117 | + """ |
| 118 | + fig = Figure() |
| 119 | + makecpt(cmap="rainbow", truncate=[0.5, None], series=[-4500, 4500]) |
| 120 | + fig.grdimage(grid, projection="W0/6i") |
| 121 | + return fig |
| 122 | + |
| 123 | + |
| 124 | +@pytest.mark.mpl_image_compare |
| 125 | +def test_makecpt_truncated_at_zhigh_only(grid): |
| 126 | + """ |
| 127 | + Use static color palette table that is truncated at z-high only |
| 128 | + """ |
| 129 | + fig = Figure() |
| 130 | + makecpt(cmap="rainbow", truncate=[None, 0.5], series=[-4500, 4500]) |
| 131 | + fig.grdimage(grid, projection="W0/6i") |
| 132 | + return fig |
| 133 | + |
| 134 | + |
| 135 | +@pytest.mark.mpl_image_compare |
| 136 | +def test_makecpt_reverse_color_only(grid): |
| 137 | + """ |
| 138 | + Use static color palette table with its colors reversed |
| 139 | + """ |
| 140 | + fig = Figure() |
| 141 | + makecpt(cmap="earth", reverse=True) |
| 142 | + fig.grdimage(grid, projection="W0/6i") |
| 143 | + return fig |
| 144 | + |
| 145 | + |
| 146 | +@pytest.mark.mpl_image_compare |
| 147 | +def test_makecpt_reverse_zsign_only(grid): |
| 148 | + """ |
| 149 | + Use static color palette table with its z-value sign reversed |
| 150 | + """ |
| 151 | + fig = Figure() |
| 152 | + makecpt(cmap="earth", reverse="z") |
| 153 | + fig.grdimage(grid, projection="W0/6i") |
| 154 | + return fig |
| 155 | + |
| 156 | + |
| 157 | +@pytest.mark.mpl_image_compare |
| 158 | +def test_makecpt_reverse_color_and_zsign(grid): |
| 159 | + """ |
| 160 | + Use static color palette table with both its colors and z-value sign reversed |
| 161 | + """ |
| 162 | + fig = Figure() |
| 163 | + makecpt(cmap="earth", reverse="cz") |
| 164 | + fig.grdimage(grid, projection="W0/6i") |
| 165 | + return fig |
| 166 | + |
| 167 | + |
| 168 | +@pytest.mark.mpl_image_compare |
| 169 | +def test_makecpt_continuous(grid): |
| 170 | + """ |
| 171 | + Use static color palette table that is continuous from blue to white and scaled from |
| 172 | + -4500 to 4500m. |
| 173 | + """ |
| 174 | + fig = Figure() |
| 175 | + makecpt(cmap="blue,white", continuous=True, series="-4500,4500") |
| 176 | + fig.grdimage(grid, projection="W0/6i") |
| 177 | + return fig |
0 commit comments