|
7 | 7 |
|
8 | 8 | import numpy as np
|
9 | 9 | from pygmt.clib import Session
|
10 |
| -from pygmt.datatypes import _GMT_DATASET, _GMT_GRID |
| 10 | +from pygmt.datatypes import _GMT_DATASET, _GMT_GRID, _GMT_IMAGE |
11 | 11 | from pygmt.helpers import GMTTempFile
|
12 | 12 |
|
13 | 13 |
|
@@ -143,3 +143,81 @@ def test_clib_read_data_image_as_grid():
|
143 | 143 | assert header.wesn[:] == [-180.0, 180.0, -90.0, 90.0]
|
144 | 144 |
|
145 | 145 | assert image.data
|
| 146 | + |
| 147 | + |
| 148 | +def test_clib_read_data_image(): |
| 149 | + """ |
| 150 | + Test the Session.read_data method for images. |
| 151 | + """ |
| 152 | + with Session() as lib: |
| 153 | + data_ptr = lib.read_data( |
| 154 | + "GMT_IS_IMAGE", |
| 155 | + "GMT_IS_SURFACE", |
| 156 | + "GMT_CONTAINER_AND_DATA", |
| 157 | + None, |
| 158 | + "@earth_day_01d_p", |
| 159 | + None, |
| 160 | + ) |
| 161 | + image = ctp.cast(data_ptr, ctp.POINTER(_GMT_IMAGE)).contents |
| 162 | + header = image.header.contents |
| 163 | + assert header.n_rows == 180 |
| 164 | + assert header.n_columns == 360 |
| 165 | + assert header.n_bands == 3 |
| 166 | + assert header.wesn[:] == [-180.0, 180.0, -90.0, 90.0] |
| 167 | + |
| 168 | + assert image.data |
| 169 | + |
| 170 | + |
| 171 | +def test_clib_read_data_image_two_steps(): |
| 172 | + """ |
| 173 | + Test the Session.read_data method for images in two steps, first reading the header |
| 174 | + and then the data. |
| 175 | + """ |
| 176 | + family, geometry = "GMT_IS_IMAGE", "GMT_IS_SURFACE" |
| 177 | + infile = "@earth_day_01d_p" |
| 178 | + with Session() as lib: |
| 179 | + # Read the header first |
| 180 | + data_ptr = lib.read_data( |
| 181 | + family, geometry, "GMT_CONTAINER_ONLY", None, infile, None |
| 182 | + ) |
| 183 | + image = ctp.cast(data_ptr, ctp.POINTER(_GMT_IMAGE)).contents |
| 184 | + header = image.header.contents |
| 185 | + assert header.n_rows == 180 |
| 186 | + assert header.n_columns == 360 |
| 187 | + assert header.n_bands == 3 |
| 188 | + assert header.wesn[:] == [-180.0, 180.0, -90.0, 90.0] |
| 189 | + |
| 190 | + assert not image.data # The data is not read yet |
| 191 | + |
| 192 | + # Read the data |
| 193 | + data_ptr = lib.read_data( |
| 194 | + family, geometry, "GMT_DATA_ONLY", None, infile, data_ptr |
| 195 | + ) |
| 196 | + image = ctp.cast(data_ptr, ctp.POINTER(_GMT_GRID)).contents |
| 197 | + |
| 198 | + assert image.data is not None # The data is read |
| 199 | + |
| 200 | + |
| 201 | +def test_clib_read_data_grid_as_image(): |
| 202 | + """ |
| 203 | + Test the Session.read_data method for reading a grid as an image. |
| 204 | +
|
| 205 | + Same as test_clib_read_data_image() but with a grid file. |
| 206 | + """ |
| 207 | + with Session() as lib: |
| 208 | + data_ptr = lib.read_data( |
| 209 | + "GMT_IS_IMAGE", |
| 210 | + "GMT_IS_SURFACE", |
| 211 | + "GMT_CONTAINER_AND_DATA", |
| 212 | + None, |
| 213 | + "@earth_relief_01d_p", |
| 214 | + None, |
| 215 | + ) |
| 216 | + image = ctp.cast(data_ptr, ctp.POINTER(_GMT_IMAGE)).contents |
| 217 | + header = image.header.contents |
| 218 | + assert header.n_rows == 180 |
| 219 | + assert header.n_columns == 360 |
| 220 | + assert header.n_bands == 1 |
| 221 | + assert header.wesn[:] == [-179.5, 179.5, -89.5, 89.5] |
| 222 | + |
| 223 | + assert image.data |
0 commit comments