|
| 1 | +# pylint: disable=redefined-outer-name |
| 2 | +""" |
| 3 | +Tests grdview |
| 4 | +""" |
| 5 | +import pytest |
| 6 | + |
| 7 | +from .. import Figure, which |
| 8 | +from ..datasets import load_earth_relief |
| 9 | +from ..exceptions import GMTInvalidInput |
| 10 | +from ..helpers import data_kind |
| 11 | + |
| 12 | + |
| 13 | +@pytest.fixture(scope="module") |
| 14 | +def grid(): |
| 15 | + "Load the grid data from the sample earth_relief file" |
| 16 | + return load_earth_relief().sel(lat=slice(-49, -42), lon=slice(-118, -107)) |
| 17 | + |
| 18 | + |
| 19 | +@pytest.mark.mpl_image_compare |
| 20 | +def test_grdview_grid_dataarray(grid): |
| 21 | + """ |
| 22 | + Run grdview by passing in a grid as an xarray.DataArray. |
| 23 | + """ |
| 24 | + fig = Figure() |
| 25 | + fig.grdview(grid=grid) |
| 26 | + return fig |
| 27 | + |
| 28 | + |
| 29 | +@pytest.mark.mpl_image_compare |
| 30 | +def test_grdview_grid_file_with_region_subset(): |
| 31 | + """ |
| 32 | + Run grdview by passing in a grid filename, and cropping it to a region. |
| 33 | + """ |
| 34 | + gridfile = which("@earth_relief_60m", download="c") |
| 35 | + |
| 36 | + fig = Figure() |
| 37 | + fig.grdview(grid=gridfile, region=[-116, -109, -47, -44]) |
| 38 | + return fig |
| 39 | + |
| 40 | + |
| 41 | +def test_grdview_wrong_kind_of_grid(grid): |
| 42 | + """ |
| 43 | + Run grdview using grid input that is not an xarray.DataArray or file. |
| 44 | + """ |
| 45 | + dataset = grid.to_dataset() # convert xarray.DataArray to xarray.Dataset |
| 46 | + assert data_kind(dataset) == "matrix" |
| 47 | + |
| 48 | + fig = Figure() |
| 49 | + with pytest.raises(GMTInvalidInput): |
| 50 | + fig.grdview(grid=dataset) |
| 51 | + |
| 52 | + |
| 53 | +@pytest.mark.mpl_image_compare |
| 54 | +def test_grdview_with_perspective(grid): |
| 55 | + """ |
| 56 | + Run grdview by passing in a grid and setting a perspective viewpoint with |
| 57 | + an azimuth from the SouthEast and an elevation angle 15 degrees from the |
| 58 | + z-plane. |
| 59 | + """ |
| 60 | + fig = Figure() |
| 61 | + fig.grdview(grid=grid, perspective=[135, 15]) |
| 62 | + return fig |
| 63 | + |
| 64 | + |
| 65 | +@pytest.mark.mpl_image_compare |
| 66 | +def test_grdview_with_perspective_and_zscale(grid): |
| 67 | + """ |
| 68 | + Run grdview by passing in a grid and setting a perspective viewpoint with |
| 69 | + an azimuth from the SouthWest and an elevation angle 30 degrees from the |
| 70 | + z-plane, plus a z-axis scaling factor of 0.005. |
| 71 | + """ |
| 72 | + fig = Figure() |
| 73 | + fig.grdview(grid=grid, perspective=[225, 30], zscale=0.005) |
| 74 | + return fig |
| 75 | + |
| 76 | + |
| 77 | +@pytest.mark.mpl_image_compare |
| 78 | +def test_grdview_with_perspective_and_zsize(grid): |
| 79 | + """ |
| 80 | + Run grdview by passing in a grid and setting a perspective viewpoint with |
| 81 | + an azimuth from the SouthWest and an elevation angle 30 degrees from the |
| 82 | + z-plane, plus a z-axis size of 10cm. |
| 83 | + """ |
| 84 | + fig = Figure() |
| 85 | + fig.grdview(grid=grid, perspective=[225, 30], zsize="10c") |
| 86 | + return fig |
| 87 | + |
| 88 | + |
| 89 | +@pytest.mark.mpl_image_compare |
| 90 | +def test_grdview_with_cmap_for_image_plot(grid): |
| 91 | + """ |
| 92 | + Run grdview by passing in a grid and setting a colormap for producing an |
| 93 | + image plot. |
| 94 | + """ |
| 95 | + fig = Figure() |
| 96 | + fig.grdview(grid=grid, cmap="oleron", surftype="i") |
| 97 | + return fig |
| 98 | + |
| 99 | + |
| 100 | +@pytest.mark.mpl_image_compare |
| 101 | +def test_grdview_with_cmap_for_surface_monochrome_plot(grid): |
| 102 | + """ |
| 103 | + Run grdview by passing in a grid and setting a colormap for producing a |
| 104 | + surface monochrome plot. |
| 105 | + """ |
| 106 | + fig = Figure() |
| 107 | + fig.grdview(grid=grid, cmap="oleron", surftype="s+m") |
| 108 | + return fig |
| 109 | + |
| 110 | + |
| 111 | +@pytest.mark.mpl_image_compare |
| 112 | +def test_grdview_with_cmap_for_perspective_surface_plot(grid): |
| 113 | + """ |
| 114 | + Run grdview by passing in a grid and setting a colormap for producing a |
| 115 | + surface plot with a 3D perspective viewpoint. |
| 116 | + """ |
| 117 | + fig = Figure() |
| 118 | + fig.grdview( |
| 119 | + grid=grid, cmap="oleron", surftype="s", perspective=[225, 30], zscale=0.005, |
| 120 | + ) |
| 121 | + return fig |
| 122 | + |
| 123 | + |
| 124 | +@pytest.mark.mpl_image_compare |
| 125 | +def test_grdview_on_a_plane(grid): |
| 126 | + """ |
| 127 | + Run grdview by passing in a grid and plotting it on a z-plane, while |
| 128 | + setting a 3D perspective viewpoint. |
| 129 | + """ |
| 130 | + fig = Figure() |
| 131 | + fig.grdview(grid=grid, plane=-4000, perspective=[225, 30], zscale=0.005) |
| 132 | + return fig |
| 133 | + |
| 134 | + |
| 135 | +@pytest.mark.mpl_image_compare |
| 136 | +def test_grdview_on_a_plane_with_colored_frontal_facade(grid): |
| 137 | + """ |
| 138 | + Run grdview by passing in a grid and plotting it on a z-plane whose frontal |
| 139 | + facade is colored gray, while setting a 3D perspective viewpoint. |
| 140 | + """ |
| 141 | + fig = Figure() |
| 142 | + fig.grdview(grid=grid, plane="-4000+ggray", perspective=[225, 30], zscale=0.005) |
| 143 | + return fig |
| 144 | + |
| 145 | + |
| 146 | +@pytest.mark.mpl_image_compare |
| 147 | +def test_grdview_with_perspective_and_zaxis_frame(grid): |
| 148 | + """ |
| 149 | + Run grdview by passing in a grid and plotting an annotated vertical |
| 150 | + z-axis frame. |
| 151 | + """ |
| 152 | + fig = Figure() |
| 153 | + fig.grdview(grid=grid, perspective=[225, 30], zscale=0.005, frame="zaf") |
| 154 | + return fig |
| 155 | + |
| 156 | + |
| 157 | +@pytest.mark.mpl_image_compare |
| 158 | +def test_grdview_surface_plot_styled_with_contourpen(grid): |
| 159 | + """ |
| 160 | + Run grdview by passing in a grid with styled contour lines plotted on top |
| 161 | + of a surface plot. |
| 162 | + """ |
| 163 | + fig = Figure() |
| 164 | + fig.grdview(grid=grid, cmap="relief", surftype="s", contourpen="0.5p,black,dash") |
| 165 | + return fig |
| 166 | + |
| 167 | + |
| 168 | +@pytest.mark.mpl_image_compare |
| 169 | +def test_grdview_surface_mesh_plot_styled_with_meshpen(grid): |
| 170 | + """ |
| 171 | + Run grdview by passing in a grid with styled mesh lines plotted on top of a |
| 172 | + surface mesh plot. |
| 173 | + """ |
| 174 | + fig = Figure() |
| 175 | + fig.grdview(grid=grid, cmap="relief", surftype="sm", meshpen="0.5p,black,dash") |
| 176 | + return fig |
| 177 | + |
| 178 | + |
| 179 | +@pytest.mark.mpl_image_compare |
| 180 | +def test_grdview_on_a_plane_styled_with_facadepen(grid): |
| 181 | + """ |
| 182 | + Run grdview by passing in a grid and plotting it on a z-plane with styled |
| 183 | + lines for the frontal facade. |
| 184 | + """ |
| 185 | + fig = Figure() |
| 186 | + fig.grdview( |
| 187 | + grid=grid, |
| 188 | + plane=-4000, |
| 189 | + perspective=[225, 30], |
| 190 | + zscale=0.005, |
| 191 | + facadepen="0.5p,blue,dash", |
| 192 | + ) |
| 193 | + return fig |
| 194 | + |
| 195 | + |
| 196 | +@pytest.mark.mpl_image_compare |
| 197 | +def test_grdview_drapegrid_dataarray(grid): |
| 198 | + """ |
| 199 | + Run grdview by passing in both a grid and drapegrid as an xarray.DataArray, |
| 200 | + setting a colormap for producing an image plot. |
| 201 | + """ |
| 202 | + drapegrid = 1.1 * grid |
| 203 | + |
| 204 | + fig = Figure() |
| 205 | + fig.grdview(grid=grid, drapegrid=drapegrid, cmap="oleron", surftype="c") |
| 206 | + return fig |
| 207 | + |
| 208 | + |
| 209 | +def test_grdview_wrong_kind_of_drapegrid(grid): |
| 210 | + """ |
| 211 | + Run grdview using drapegrid input that is not an xarray.DataArray or file. |
| 212 | + """ |
| 213 | + dataset = grid.to_dataset() # convert xarray.DataArray to xarray.Dataset |
| 214 | + assert data_kind(dataset) == "matrix" |
| 215 | + |
| 216 | + fig = Figure() |
| 217 | + with pytest.raises(GMTInvalidInput): |
| 218 | + fig.grdview(grid=grid, drapegrid=dataset) |
0 commit comments