1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416
|
# Copyright Cartopy Contributors
#
# This file is part of Cartopy and is released under the LGPL license.
# See COPYING and COPYING.LESSER in the root of the repository for full
# licensing details.
import numpy as np
import numpy.ma as ma
from numpy.testing import assert_array_equal
import pytest
from cartopy.util import add_cyclic_point, add_cyclic, has_cyclic
class Test_add_cyclic_point:
@classmethod
def setup_class(cls):
cls.lons = np.arange(0, 360, 60)
cls.data2d = np.ones([3, 6]) * np.arange(6)
cls.data4d = np.ones([4, 6, 2, 3]) * \
np.arange(6)[..., np.newaxis, np.newaxis]
def test_data_only(self):
c_data = add_cyclic_point(self.data2d)
r_data = np.concatenate((self.data2d, self.data2d[:, :1]), axis=1)
assert_array_equal(c_data, r_data)
def test_data_and_coord(self):
c_data, c_lons = add_cyclic_point(self.data2d, coord=self.lons)
r_data = np.concatenate((self.data2d, self.data2d[:, :1]), axis=1)
r_lons = np.concatenate((self.lons, np.array([360])))
assert_array_equal(c_data, r_data)
assert_array_equal(c_lons, r_lons)
def test_data_only_with_axis(self):
c_data = add_cyclic_point(self.data4d, axis=1)
r_data = np.concatenate((self.data4d, self.data4d[:, :1]), axis=1)
assert_array_equal(c_data, r_data)
def test_data_and_coord_with_axis(self):
c_data, c_lons = add_cyclic_point(self.data4d, coord=self.lons, axis=1)
r_data = np.concatenate((self.data4d, self.data4d[:, :1]), axis=1)
r_lons = np.concatenate((self.lons, np.array([360])))
assert_array_equal(c_data, r_data)
assert_array_equal(c_lons, r_lons)
def test_masked_data(self):
new_data = ma.masked_less(self.data2d, 3)
c_data = add_cyclic_point(new_data)
r_data = ma.concatenate((self.data2d, self.data2d[:, :1]), axis=1)
assert_array_equal(c_data, r_data)
def test_invalid_coord_dimensionality(self):
lons2d = np.repeat(self.lons[np.newaxis], 3, axis=0)
with pytest.raises(ValueError):
c_data, c_lons = add_cyclic_point(self.data2d, coord=lons2d)
def test_invalid_coord_size(self):
with pytest.raises(ValueError):
c_data, c_lons = add_cyclic_point(self.data2d,
coord=self.lons[:-1])
def test_invalid_axis(self):
with pytest.raises(ValueError):
add_cyclic_point(self.data2d, axis=-3)
class TestAddCyclic:
"""
Test def add_cyclic(data, x=None, y=None, axis=-1,
cyclic=360, precision=1e-4):
- variations of data, x, and y with and without axis keyword
- different units of x - cyclic keyword
- detection of cyclic points - precision keyword
- error catching
"""
@classmethod
def setup_class(cls):
# 2d and 4d data
cls.data2d = np.ones([3, 6]) * np.arange(6)
cls.data4d = np.ones([4, 6, 2, 3]) * \
np.arange(6)[..., np.newaxis, np.newaxis]
# 1d lat (5) and lon (6)
# len(lat) != data.shape[0]
# len(lon) == data.shape[1]
cls.lons = np.arange(0, 360, 60)
cls.lats = np.arange(-90, 90, 180 / 5)
# 2d lat and lon
cls.lon2d, cls.lat2d = np.meshgrid(cls.lons, cls.lats)
# 3d lat and lon but with different 3rd dimension (4) as 4d data (2)
cls.lon3d = np.repeat(cls.lon2d, 4).reshape((*cls.lon2d.shape, 4))
cls.lat3d = np.repeat(cls.lat2d, 4).reshape((*cls.lat2d.shape, 4))
# cyclic data, lon, lat
cls.c_data2d = np.concatenate((cls.data2d, cls.data2d[:, :1]), axis=1)
cls.c_data4d = np.concatenate((cls.data4d, cls.data4d[:, :1]), axis=1)
cls.c_lons = np.concatenate((cls.lons, np.array([360])))
cls.c_lon2d = np.concatenate(
(cls.lon2d, np.full((cls.lon2d.shape[0], 1), 360)),
axis=1)
cls.c_lon3d = np.concatenate(
(cls.lon3d,
np.full((cls.lon3d.shape[0], 1, cls.lon3d.shape[2]), 360)),
axis=1)
cls.c_lats = cls.lats
cls.c_lat2d = np.concatenate((cls.lat2d, cls.lat2d[:, -1:]), axis=1)
cls.c_lat3d = np.concatenate((cls.lat3d, cls.lat3d[:, -1:, :]), axis=1)
def test_data_only(self):
'''Test only data no x given'''
c_data = add_cyclic(self.data2d)
assert_array_equal(c_data, self.c_data2d)
def test_data_only_ignore_y(self):
'''Test y given but no x'''
c_data = add_cyclic(self.data2d, y=self.lat2d)
assert_array_equal(c_data, self.c_data2d)
def test_data_and_x_1d(self):
'''Test data 2d and x 1d'''
c_data, c_lons = add_cyclic(self.data2d, x=self.lons)
assert_array_equal(c_data, self.c_data2d)
assert_array_equal(c_lons, self.c_lons)
def test_data_and_x_2d(self):
'''Test data and x 2d; no keyword name for x'''
c_data, c_lons = add_cyclic(self.data2d, self.lon2d)
assert_array_equal(c_data, self.c_data2d)
assert_array_equal(c_lons, self.c_lon2d)
def test_data_and_x_y_1d(self):
'''Test data and x and y 1d'''
c_data, c_lons, c_lats = add_cyclic(self.data2d, x=self.lons,
y=self.lats)
assert_array_equal(c_data, self.c_data2d)
assert_array_equal(c_lons, self.c_lons)
assert_array_equal(c_lats, self.c_lats)
def test_data_and_x_1d_y_2d(self):
'''Test data and x 1d and y 2d'''
c_data, c_lons, c_lats = add_cyclic(self.data2d, x=self.lons,
y=self.lat2d)
assert_array_equal(c_data, self.c_data2d)
assert_array_equal(c_lons, self.c_lons)
assert_array_equal(c_lats, self.c_lat2d)
def test_data_and_x_y_2d(self):
'''Test data, x, and y 2d; no keyword name for x and y'''
c_data, c_lons, c_lats = add_cyclic(self.data2d,
self.lon2d,
self.lat2d)
assert_array_equal(c_data, self.c_data2d)
assert_array_equal(c_lons, self.c_lon2d)
assert_array_equal(c_lats, self.c_lat2d)
def test_has_cyclic_1d(self):
'''Test detection of cyclic point 1d'''
c_data, c_lons = add_cyclic(self.c_data2d, x=self.c_lons)
assert_array_equal(c_data, self.c_data2d)
assert_array_equal(c_lons, self.c_lons)
def test_has_cyclic_2d(self):
'''Test detection of cyclic point 2d'''
c_data, c_lons = add_cyclic(self.c_data2d, x=self.c_lon2d)
assert_array_equal(c_data, self.c_data2d)
assert_array_equal(c_lons, self.c_lon2d)
def test_has_cyclic_2d_full(self):
'''Test detection of cyclic point 2d including y'''
c_data, c_lons, c_lats = add_cyclic(self.c_data2d, x=self.c_lon2d,
y=self.c_lat2d)
assert_array_equal(c_data, self.c_data2d)
assert_array_equal(c_lons, self.c_lon2d)
assert_array_equal(c_lats, self.c_lat2d)
def test_data_only_with_axis(self):
'''Test axis keyword data only'''
c_data = add_cyclic(self.data4d, axis=1)
assert_array_equal(c_data, self.c_data4d)
def test_data_and_x_with_axis_1d(self):
'''Test axis keyword data 4d, x 1d'''
c_data, c_lons = add_cyclic(self.data4d, x=self.lons, axis=1)
assert_array_equal(c_data, self.c_data4d)
assert_array_equal(c_lons, self.c_lons)
def test_data_and_x_with_axis_2d(self):
'''Test axis keyword data 4d, x 2d'''
c_data, c_lons = add_cyclic(self.data4d, x=self.lon2d,
axis=1)
assert_array_equal(c_data, self.c_data4d)
assert_array_equal(c_lons, self.c_lon2d)
def test_data_and_x_with_axis_3d(self):
'''Test axis keyword data 4d, x 3d'''
c_data, c_lons = add_cyclic(self.data4d, x=self.lon3d,
axis=1)
assert_array_equal(c_data, self.c_data4d)
assert_array_equal(c_lons, self.c_lon3d)
def test_data_and_x_y_with_axis_2d(self):
'''Test axis keyword data 4d, x and y 2d'''
c_data, c_lons, c_lats = add_cyclic(self.data4d,
x=self.lon2d,
y=self.lat2d,
axis=1)
assert_array_equal(c_data, self.c_data4d)
assert_array_equal(c_lons, self.c_lon2d)
assert_array_equal(c_lats, self.c_lat2d)
def test_data_and_x_y_with_axis_3d(self):
'''Test axis keyword data 4d, x and y 3d'''
c_data, c_lons, c_lats = add_cyclic(self.data4d,
x=self.lon3d,
y=self.lat3d,
axis=1)
assert_array_equal(c_data, self.c_data4d)
assert_array_equal(c_lons, self.c_lon3d)
assert_array_equal(c_lats, self.c_lat3d)
def test_data_and_x_y_with_axis_nd(self):
'''Test axis keyword data 4d, x 3d and y 2d'''
c_data, c_lons, c_lats = add_cyclic(self.data4d,
x=self.lon3d,
y=self.lat2d,
axis=1)
assert_array_equal(c_data, self.c_data4d)
assert_array_equal(c_lons, self.c_lon3d)
assert_array_equal(c_lats, self.c_lat2d)
def test_masked_data(self):
'''Test masked data'''
new_data = ma.masked_less(self.data2d, 3)
c_data = add_cyclic(new_data)
r_data = ma.concatenate((self.data2d, self.data2d[:, :1]), axis=1)
assert_array_equal(c_data, r_data)
assert ma.is_masked(c_data)
def test_masked_data_and_x_y_2d(self):
'''Test masked data and x'''
new_data = ma.masked_less(self.data2d, 3)
new_lon = ma.masked_less(self.lon2d, 2)
c_data, c_lons, c_lats = add_cyclic(new_data,
x=new_lon,
y=self.lat2d)
r_data = ma.concatenate((self.data2d, self.data2d[:, :1]), axis=1)
r_lons = np.concatenate((self.lon2d,
np.full((self.lon2d.shape[0], 1), 360)),
axis=1)
assert_array_equal(c_data, r_data)
assert_array_equal(c_lons, r_lons)
assert_array_equal(c_lats, self.c_lat2d)
assert ma.is_masked(c_data)
assert ma.is_masked(c_lons)
assert not ma.is_masked(c_lats)
def test_cyclic(self):
'''Test cyclic keyword with axis data 4d, x 3d and y 2d'''
new_lons = np.deg2rad(self.lon3d)
new_lats = np.deg2rad(self.lat2d)
c_data, c_lons, c_lats = add_cyclic(self.data4d, x=new_lons,
y=new_lats, axis=1,
cyclic=np.deg2rad(360))
r_lons = np.concatenate(
(new_lons,
np.full((new_lons.shape[0], 1, new_lons.shape[2]),
np.deg2rad(360))),
axis=1)
r_lats = np.concatenate((new_lats, new_lats[:, -1:]), axis=1)
assert_array_equal(c_data, self.c_data4d)
assert_array_equal(c_lons, r_lons)
assert_array_equal(c_lats, r_lats)
def test_cyclic_has_cyclic(self):
'''Test detection of cyclic point with cyclic keyword'''
new_lons = np.deg2rad(self.lon2d)
new_lats = np.deg2rad(self.lat2d)
r_data = np.concatenate((self.data2d, self.data2d[:, :1]), axis=1)
r_lons = np.concatenate(
(new_lons,
np.full((new_lons.shape[0], 1), np.deg2rad(360))),
axis=1)
r_lats = np.concatenate((new_lats, new_lats[:, -1:]), axis=1)
c_data, c_lons, c_lats = add_cyclic(r_data, x=r_lons,
y=r_lats,
cyclic=np.deg2rad(360))
assert_array_equal(c_data, self.c_data2d)
assert_array_equal(c_lons, r_lons)
assert_array_equal(c_lats, r_lats)
def test_precision_has_cyclic(self):
'''Test precision keyword detecting cyclic point'''
r_data = np.concatenate((self.data2d, self.data2d[:, :1]), axis=1)
r_lons = np.concatenate((self.lons, np.array([360 + 1e-3])))
c_data, c_lons = add_cyclic(r_data, x=r_lons, precision=1e-2)
assert_array_equal(c_data, r_data)
assert_array_equal(c_lons, r_lons)
def test_precision_has_cyclic_no(self):
'''Test precision keyword detecting no cyclic point'''
new_data = np.concatenate((self.data2d, self.data2d[:, :1]), axis=1)
new_lons = np.concatenate((self.lons, np.array([360. + 1e-3])))
c_data, c_lons = add_cyclic(new_data, x=new_lons, precision=2e-4)
r_data = np.concatenate((new_data, new_data[:, :1]), axis=1)
r_lons = np.concatenate((new_lons, np.array([360])))
assert_array_equal(c_data, r_data)
assert_array_equal(c_lons, r_lons)
def test_invalid_x_dimensionality(self):
'''Catch wrong x dimensions'''
with pytest.raises(ValueError):
c_data, c_lons = add_cyclic(self.data2d, x=self.lon3d)
def test_invalid_y_dimensionality(self):
'''Catch wrong y dimensions'''
with pytest.raises(ValueError):
c_data, c_lons, c_lats = add_cyclic(self.data2d,
x=self.lon2d,
y=self.lat3d)
def test_invalid_x_size_1d(self):
'''Catch wrong x size 1d'''
with pytest.raises(ValueError):
c_data, c_lons = add_cyclic(self.data2d,
x=self.lons[:-1])
def test_invalid_x_size_2d(self):
'''Catch wrong x size 2d'''
with pytest.raises(ValueError):
c_data, c_lons = add_cyclic(self.data2d,
x=self.lon2d[:, :-1])
def test_invalid_x_size_3d(self):
'''Catch wrong x size 3d'''
with pytest.raises(ValueError):
c_data, c_lons = add_cyclic(self.data4d,
x=self.lon3d[:, :-1, :], axis=1)
def test_invalid_y_size(self):
'''Catch wrong y size 2d'''
with pytest.raises(ValueError):
c_data, c_lons, c_lats = add_cyclic(
self.data2d, x=self.lon2d, y=self.lat2d[:, 1:])
def test_invalid_axis(self):
'''Catch wrong axis keyword'''
with pytest.raises(ValueError):
add_cyclic(self.data2d, axis=-3)
class TestHasCyclic:
"""
Test def has_cyclic(x, axis=-1, cyclic=360, precision=1e-4):
- variations of x with and without axis keyword
- different unit of x - cyclic keyword
- detection of cyclic points - precision keyword
"""
# 1d lon (6), lat (5)
lons = np.arange(0, 360, 60)
lats = np.arange(-90, 90, 180 / 5)
# 2d lon, lat
lon2d, lat2d = np.meshgrid(lons, lats)
# 3d lon
lon3d = np.repeat(lon2d, 4).reshape((*lon2d.shape, 4))
# cyclic lon 1d, 2d, 3d
c_lons = np.concatenate((lons, np.array([360])))
c_lon2d = np.concatenate(
(lon2d,
np.full((lon2d.shape[0], 1), 360)),
axis=1)
c_lon3d = np.concatenate(
(lon3d,
np.full((lon3d.shape[0], 1, lon3d.shape[2]), 360)),
axis=1)
@pytest.mark.parametrize(
"lon, clon",
[(lons, c_lons),
(lon2d, c_lon2d)])
def test_data(self, lon, clon):
'''Test lon is not cyclic, clon is cyclic'''
assert not has_cyclic(lon)
assert has_cyclic(clon)
@pytest.mark.parametrize(
"lon, clon, axis",
[(lons, c_lons, 0),
(lon2d, c_lon2d, 1),
(ma.masked_inside(lon2d, 100, 200),
ma.masked_inside(c_lon2d, 100, 200),
1)])
def test_data_axis(self, lon, clon, axis):
'''Test lon is not cyclic, clon is cyclic, with axis keyword'''
assert not has_cyclic(lon, axis=axis)
assert has_cyclic(clon, axis=axis)
def test_3d_axis(self):
'''Test 3d with axis keyword, no keyword name for axis'''
assert has_cyclic(self.c_lon3d, 1)
assert not has_cyclic(self.lon3d, 1)
def test_3d_axis_cyclic(self):
'''Test 3d with axis and cyclic keywords'''
new_clons = np.deg2rad(self.c_lon3d)
new_lons = np.deg2rad(self.lon3d)
assert has_cyclic(new_clons, axis=1, cyclic=np.deg2rad(360))
assert not has_cyclic(new_lons, axis=1, cyclic=np.deg2rad(360))
def test_1d_precision(self):
'''Test 1d with precision keyword'''
new_clons = np.concatenate((self.lons, np.array([360 + 1e-3])))
assert has_cyclic(new_clons, precision=1e-2)
assert not has_cyclic(new_clons, precision=2e-4)
|