7
7
import numpy as np
8
8
import pytest
9
9
from cf_units import Unit
10
+ from iris .cube import Cube
10
11
from shapely .geometry import Polygon , mapping
11
12
12
13
import tests
@@ -377,6 +378,40 @@ def square_composite_shape(request, tmp_path):
377
378
return np .ma .masked_array (vals , mask )
378
379
379
380
381
+ def _create_sample_full_cube ():
382
+ cube = Cube (np .zeros ((4 , 180 , 360 )), var_name = 'co2' , units = 'J' )
383
+ cube .add_dim_coord (
384
+ iris .coords .DimCoord (
385
+ np .array ([10. , 40. , 70. , 110. ]),
386
+ standard_name = 'time' ,
387
+ units = Unit ('days since 1950-01-01 00:00:00' , calendar = 'gregorian' ),
388
+ ),
389
+ 0 ,
390
+ )
391
+ cube .add_dim_coord (
392
+ iris .coords .DimCoord (
393
+ np .arange (- 90. , 90. , 1. ),
394
+ standard_name = 'latitude' ,
395
+ units = 'degrees' ,
396
+ ),
397
+ 1 ,
398
+ )
399
+ cube .add_dim_coord (
400
+ iris .coords .DimCoord (
401
+ np .arange (0. , 360. , 1. ),
402
+ standard_name = 'longitude' ,
403
+ units = 'degrees' ,
404
+ ),
405
+ 2 ,
406
+ )
407
+
408
+ cube .coord ("time" ).guess_bounds ()
409
+ cube .coord ("longitude" ).guess_bounds ()
410
+ cube .coord ("latitude" ).guess_bounds ()
411
+
412
+ return cube
413
+
414
+
380
415
def test_crop_cube (make_testcube , square_shape , tmp_path ):
381
416
"""Test for cropping a cube by shape bounds."""
382
417
with fiona .open (tmp_path / 'test_shape.shp' ) as geometries :
@@ -385,6 +420,22 @@ def test_crop_cube(make_testcube, square_shape, tmp_path):
385
420
np .testing .assert_array_equal (result .data , expected )
386
421
387
422
423
+ def test_crop_cube_with_ne_file ():
424
+ """Test for cropping a cube by shape bounds."""
425
+ shp_file = "esmvalcore/preprocessor/ne_masks/ne_50m_ocean.shp"
426
+ with fiona .open (shp_file ) as geometries :
427
+ cube = _create_sample_full_cube ()
428
+ copy_bounds = list (geometries .bounds )
429
+ copy_bounds [2 ] = 370.
430
+ copy_bounds [1 ] = - 99.
431
+ copy_bounds [3 ] = 100.
432
+ result = _crop_cube (cube , * tuple (copy_bounds ))
433
+ result = (result .coord ("latitude" ).points [- 1 ],
434
+ result .coord ("longitude" ).points [- 1 ])
435
+ expected = (89. , 359. )
436
+ np .testing .assert_allclose (result , expected )
437
+
438
+
388
439
@pytest .mark .parametrize ('crop' , [True , False ])
389
440
def test_extract_shape (make_testcube , square_shape , tmp_path , crop ):
390
441
"""Test for extracting a region with shapefile"""
0 commit comments