|
9 | 9 | import iris.tests as tests # isort:skip
|
10 | 10 |
|
11 | 11 | import collections
|
| 12 | +from datetime import datetime |
12 | 13 | from unittest import mock
|
13 | 14 | import warnings
|
14 | 15 |
|
| 16 | +import cf_units |
15 | 17 | import dask.array as da
|
16 | 18 | import numpy as np
|
17 | 19 | import pytest
|
@@ -236,6 +238,163 @@ def test_points_inside_bounds_outside_wrong_name_2(self):
|
236 | 238 | self.assertArrayEqual(lat.bounds, [[-120, -40], [-40, 35], [35, 105]])
|
237 | 239 |
|
238 | 240 |
|
| 241 | +def test_guess_bounds_monthly_and_yearly(): |
| 242 | + units = cf_units.Unit("days since epoch", calendar="gregorian") |
| 243 | + points = units.date2num( |
| 244 | + [ |
| 245 | + datetime(1990, 1, 1), |
| 246 | + datetime(1990, 2, 1), |
| 247 | + datetime(1990, 3, 1), |
| 248 | + ] |
| 249 | + ) |
| 250 | + coord = iris.coords.AuxCoord(points=points, units=units, standard_name="time") |
| 251 | + with pytest.raises( |
| 252 | + ValueError, |
| 253 | + match="Cannot guess monthly and yearly bounds simultaneously.", |
| 254 | + ): |
| 255 | + coord.guess_bounds(monthly=True, yearly=True) |
| 256 | + |
| 257 | + |
| 258 | +class Test_Guess_Bounds_Monthly: |
| 259 | + def test_monthly_multiple_points_in_month(self): |
| 260 | + units = cf_units.Unit("days since epoch", calendar="gregorian") |
| 261 | + points = units.date2num( |
| 262 | + [ |
| 263 | + datetime(1990, 1, 3), |
| 264 | + datetime(1990, 1, 28), |
| 265 | + datetime(1990, 2, 13), |
| 266 | + ] |
| 267 | + ) |
| 268 | + coord = iris.coords.AuxCoord(points=points, units=units, standard_name="time") |
| 269 | + with pytest.raises( |
| 270 | + ValueError, |
| 271 | + match="Cannot guess monthly bounds for a coordinate with multiple points " |
| 272 | + "in a month.", |
| 273 | + ): |
| 274 | + coord.guess_bounds(monthly=True) |
| 275 | + |
| 276 | + def test_monthly_non_contiguous(self): |
| 277 | + units = cf_units.Unit("days since epoch", calendar="gregorian") |
| 278 | + expected = units.date2num( |
| 279 | + [ |
| 280 | + [datetime(1990, 1, 1), datetime(1990, 2, 1)], |
| 281 | + [datetime(1990, 2, 1), datetime(1990, 3, 1)], |
| 282 | + [datetime(1990, 5, 1), datetime(1990, 6, 1)], |
| 283 | + ] |
| 284 | + ) |
| 285 | + points = expected.mean(axis=1) |
| 286 | + coord = iris.coords.AuxCoord(points=points, units=units, standard_name="time") |
| 287 | + with pytest.raises( |
| 288 | + ValueError, match="Cannot guess bounds for a non-contiguous coordinate." |
| 289 | + ): |
| 290 | + coord.guess_bounds(monthly=True) |
| 291 | + |
| 292 | + def test_monthly_end_of_month(self): |
| 293 | + units = cf_units.Unit("days since epoch", calendar="gregorian") |
| 294 | + expected = units.date2num( |
| 295 | + [ |
| 296 | + [datetime(1990, 1, 1), datetime(1990, 2, 1)], |
| 297 | + [datetime(1990, 2, 1), datetime(1990, 3, 1)], |
| 298 | + [datetime(1990, 3, 1), datetime(1990, 4, 1)], |
| 299 | + ] |
| 300 | + ) |
| 301 | + points = units.date2num( |
| 302 | + [ |
| 303 | + datetime(1990, 1, 31), |
| 304 | + datetime(1990, 2, 28), |
| 305 | + datetime(1990, 3, 31), |
| 306 | + ] |
| 307 | + ) |
| 308 | + coord = iris.coords.AuxCoord(points=points, units=units, standard_name="time") |
| 309 | + coord.guess_bounds(monthly=True) |
| 310 | + dates = units.num2date(coord.bounds) |
| 311 | + expected_dates = units.num2date(expected) |
| 312 | + np.testing.assert_array_equal(dates, expected_dates) |
| 313 | + |
| 314 | + def test_monthly_multiple_years(self): |
| 315 | + units = cf_units.Unit("days since epoch", calendar="gregorian") |
| 316 | + expected = [ |
| 317 | + [datetime(1990, 10, 1), datetime(1990, 11, 1)], |
| 318 | + [datetime(1990, 11, 1), datetime(1990, 12, 1)], |
| 319 | + [datetime(1990, 12, 1), datetime(1991, 1, 1)], |
| 320 | + ] |
| 321 | + expected_points = units.date2num(expected) |
| 322 | + points = expected_points.mean(axis=1) |
| 323 | + coord = iris.coords.AuxCoord(points=points, units=units, standard_name="time") |
| 324 | + coord.guess_bounds(monthly=True) |
| 325 | + dates = units.num2date(coord.bounds) |
| 326 | + np.testing.assert_array_equal(dates, expected) |
| 327 | + |
| 328 | + def test_monthly_single_point(self): |
| 329 | + units = cf_units.Unit("days since epoch", calendar="gregorian") |
| 330 | + expected = [ |
| 331 | + [datetime(1990, 1, 1), datetime(1990, 2, 1)], |
| 332 | + ] |
| 333 | + expected_points = units.date2num(expected) |
| 334 | + points = expected_points.mean(axis=1) |
| 335 | + coord = iris.coords.AuxCoord(points=points, units=units, standard_name="time") |
| 336 | + coord.guess_bounds(monthly=True) |
| 337 | + dates = units.num2date(coord.bounds) |
| 338 | + np.testing.assert_array_equal(dates, expected) |
| 339 | + |
| 340 | + |
| 341 | +class Test_Guess_Bounds_Yearly: |
| 342 | + def test_yearly_multiple_points_in_year(self): |
| 343 | + units = cf_units.Unit("days since epoch", calendar="gregorian") |
| 344 | + points = units.date2num( |
| 345 | + [ |
| 346 | + datetime(1990, 1, 1), |
| 347 | + datetime(1990, 2, 1), |
| 348 | + datetime(1991, 1, 1), |
| 349 | + ] |
| 350 | + ) |
| 351 | + coord = iris.coords.AuxCoord(points=points, units=units, standard_name="time") |
| 352 | + with pytest.raises( |
| 353 | + ValueError, |
| 354 | + match="Cannot guess yearly bounds for a coordinate with multiple points " |
| 355 | + "in a year.", |
| 356 | + ): |
| 357 | + coord.guess_bounds(yearly=True) |
| 358 | + |
| 359 | + def test_yearly_non_contiguous(self): |
| 360 | + units = cf_units.Unit("days since epoch", calendar="gregorian") |
| 361 | + expected = units.date2num( |
| 362 | + [ |
| 363 | + [datetime(1990, 1, 1), datetime(1990, 1, 1)], |
| 364 | + [datetime(1991, 1, 1), datetime(1991, 1, 1)], |
| 365 | + [datetime(1994, 1, 1), datetime(1994, 1, 1)], |
| 366 | + ] |
| 367 | + ) |
| 368 | + points = expected.mean(axis=1) |
| 369 | + coord = iris.coords.AuxCoord(points=points, units=units, standard_name="time") |
| 370 | + with pytest.raises( |
| 371 | + ValueError, match="Cannot guess bounds for a non-contiguous coordinate." |
| 372 | + ): |
| 373 | + coord.guess_bounds(yearly=True) |
| 374 | + |
| 375 | + def test_yearly_end_of_year(self): |
| 376 | + units = cf_units.Unit("days since epoch", calendar="gregorian") |
| 377 | + expected = units.date2num( |
| 378 | + [ |
| 379 | + [datetime(1990, 1, 1), datetime(1991, 1, 1)], |
| 380 | + [datetime(1991, 1, 1), datetime(1992, 1, 1)], |
| 381 | + [datetime(1992, 1, 1), datetime(1993, 1, 1)], |
| 382 | + ] |
| 383 | + ) |
| 384 | + points = units.date2num( |
| 385 | + [ |
| 386 | + datetime(1990, 12, 31), |
| 387 | + datetime(1991, 12, 31), |
| 388 | + datetime(1992, 12, 31), |
| 389 | + ] |
| 390 | + ) |
| 391 | + coord = iris.coords.AuxCoord(points=points, units=units, standard_name="time") |
| 392 | + coord.guess_bounds(yearly=True) |
| 393 | + dates = units.num2date(coord.bounds) |
| 394 | + expected_dates = units.num2date(expected) |
| 395 | + np.testing.assert_array_equal(dates, expected_dates) |
| 396 | + |
| 397 | + |
239 | 398 | class Test_cell(tests.IrisTest):
|
240 | 399 | def _mock_coord(self):
|
241 | 400 | coord = mock.Mock(
|
|
0 commit comments