def test_distinct_xy_bounds_pole(self): # is UserWarning issued for out-of-bounds? results will be unexpected! cube = stock.simple_pp() cube = cube[:4, :4] lon = cube.coord('longitude') lat = cube.coord('latitude') lon.guess_bounds() lat.guess_bounds() from iris.util import regular_step quarter = abs(regular_step(lon) * regular_step(lat) * 0.25) half = abs(regular_step(lon) * regular_step(lat) * 0.5) top_cell_half = abs(regular_step(lon) * (90 - lat.bounds[0, 1]) * 0.5) minx = 3.7499990463256836 maxx = 7.499998092651367 miny = 84.99998474121094 maxy = 99.99998474121094 geometry = shapely.geometry.box(minx, miny, maxx, maxy) # see http://stackoverflow.com/a/3892301 to assert warnings with warnings.catch_warnings(record=True) as w: warnings.simplefilter("always") # always trigger all warnings weights = geometry_area_weights(cube, geometry) self.assertEqual( str(w[-1].message), "The geometry exceeds the " "cube's y dimension at the upper end.") self.assertTrue(issubclass(w[-1].category, UserWarning)) target = np.array([[0, top_cell_half, top_cell_half, 0], [0, half, half, 0], [0, quarter, quarter, 0], [0, 0, 0, 0]]) self.assertTrue(np.allclose(weights, target))
def test_distinct_xy_bounds(self): # cases where geometry bnds are outside cube bnds correctly handled? cube = stock.simple_pp() cube = cube[:4, :4] lon = cube.coord('longitude') lat = cube.coord('latitude') lon.guess_bounds() lat.guess_bounds() from iris.fileformats.rules import regular_step quarter = abs(regular_step(lon) * regular_step(lat) * 0.25) half = abs(regular_step(lon) * regular_step(lat) * 0.5) full = abs(regular_step(lon) * regular_step(lat)) minx = 3.7499990463256836 maxx = 13.12499619 maxx_overshoot = 15. miny = 84.99998474121094 maxy = 89.99998474121094 geometry = shapely.geometry.box(minx, miny, maxx, maxy) geometry_overshoot = shapely.geometry.box(minx, miny, maxx_overshoot, maxy) weights = geometry_area_weights(cube, geometry) weights_overshoot = geometry_area_weights(cube, geometry_overshoot) target = np.array([ [0, quarter, half, half], [0, half, full, full], [0, quarter, half, half], [0, 0, 0, 0]]) self.assertTrue(np.allclose(weights, target)) self.assertTrue(np.allclose(weights_overshoot, target))
def test_distinct_xy_bounds_pole(self): # is UserWarning issued for out-of-bounds? results will be unexpected! cube = stock.simple_pp() cube = cube[:4, :4] lon = cube.coord('longitude') lat = cube.coord('latitude') lon.guess_bounds() lat.guess_bounds() from iris.fileformats.rules import regular_step quarter = abs(regular_step(lon) * regular_step(lat) * 0.25) half = abs(regular_step(lon) * regular_step(lat) * 0.5) minx = 3.7499990463256836 maxx = 7.499998092651367 miny = 84.99998474121094 maxy = 99.99998474121094 geometry = shapely.geometry.box(minx, miny, maxx, maxy) # see http://stackoverflow.com/a/3892301 to assert warnings with warnings.catch_warnings(record=True) as w: warnings.simplefilter("always") # always trigger all warnings weights = geometry_area_weights(cube, geometry) self.assertEqual(str(w[-1].message), "The geometry exceeds the " "cube's y dimension at the upper end.") self.assertTrue(issubclass(w[-1].category, UserWarning)) target = np.array([ [0, half, half, 0], [0, half, half, 0], [0, quarter, quarter, 0], [0, 0, 0, 0]]) self.assertTrue(np.allclose(weights, target))
def test_distinct_xy_bounds(self): # cases where geometry bnds are outside cube bnds correctly handled? cube = stock.simple_pp() cube = cube[:4, :4] lon = cube.coord('longitude') lat = cube.coord('latitude') lon.guess_bounds() lat.guess_bounds() from iris.util import regular_step quarter = abs(regular_step(lon) * regular_step(lat) * 0.25) half = abs(regular_step(lon) * regular_step(lat) * 0.5) full = abs(regular_step(lon) * regular_step(lat)) minx = 3.7499990463256836 maxx = 13.12499619 maxx_overshoot = 15. miny = 84.99998474121094 maxy = 89.99998474121094 geometry = shapely.geometry.box(minx, miny, maxx, maxy) geometry_overshoot = shapely.geometry.box(minx, miny, maxx_overshoot, maxy) weights = geometry_area_weights(cube, geometry) weights_overshoot = geometry_area_weights(cube, geometry_overshoot) target = np.array([[0, quarter, half, half], [0, half, full, full], [0, quarter, half, half], [0, 0, 0, 0]]) self.assertTrue(np.allclose(weights, target)) self.assertTrue(np.allclose(weights_overshoot, target))
def test_no_realization(self): cube = stock.simple_pp() grib = mock.Mock() mock_gribapi = mock.Mock(spec=gribapi) with mock.patch(GRIB_API, mock_gribapi): identification(cube, grib) mock_gribapi.assert_has_calls([mock.call.grib_set_long(grib, "typeOfProcessedData", 2)])
def test_no_realization(self): cube = stock.simple_pp() grib = mock.Mock() mock_gribapi = mock.Mock(spec=gribapi) with mock.patch(GRIB_API, mock_gribapi): identification(cube, grib) mock_gribapi.assert_has_calls( [mock.call.grib_set_long(grib, "typeOfProcessedData", 2)])
def test_365_calendar_export(self): # test for 365 day calendar export cube = stock.simple_pp() new_unit = cf_units.Unit("hours since 1970-01-01 00:00:00", calendar=cf_units.CALENDAR_365_DAY) cube.coord("time").units = new_unit pp_field = mock.MagicMock(spec=PPField3) iris.fileformats.pp._ensure_save_rules_loaded() iris.fileformats.pp._save_rules.verify(cube, pp_field) self.assertEqual(pp_field.lbtim.ic, 4)
def test_365_calendar_export(self): # test for 365 day calendar export cube = stock.simple_pp() new_unit = iris.unit.Unit('hours since 1970-01-01 00:00:00', calendar=iris.unit.CALENDAR_365_DAY) cube.coord('time').units = new_unit pp_field = mock.MagicMock(spec=PPField3) iris.fileformats.pp._ensure_save_rules_loaded() iris.fileformats.pp._save_rules.verify(cube, pp_field) self.assertEqual(pp_field.lbtim.ic, 4)
def test_365_calendar_export(self): # test for 365 day calendar export cube = stock.simple_pp() new_unit = cf_units.Unit('hours since 1970-01-01 00:00:00', calendar=cf_units.CALENDAR_365_DAY) cube.coord('time').units = new_unit # Add an extra "fill_value" property, as used by the save rules. cube.fill_value = None pp_field = mock.MagicMock(spec=PPField3) iris.fileformats.pp_save_rules.verify(cube, pp_field) self.assertEqual(pp_field.lbtim.ic, 4)
def test_realization_n(self): cube = stock.simple_pp() realisation = iris.coords.AuxCoord((2,), standard_name="realization", units="1") cube.add_aux_coord(realisation) grib = mock.Mock() mock_gribapi = mock.Mock(spec=gribapi) with mock.patch(GRIB_API, mock_gribapi): identification(cube, grib) mock_gribapi.assert_has_calls([mock.call.grib_set_long(grib, "typeOfProcessedData", 4)])
def test_realization_n(self): cube = stock.simple_pp() realisation = iris.coords.AuxCoord((2,), standard_name='realization', units='1') cube.add_aux_coord(realisation) grib = mock.Mock() mock_gribapi = mock.Mock(spec=gribapi) with mock.patch(GRIB_API, mock_gribapi): identification(cube, grib) mock_gribapi.assert_has_calls( [mock.call.grib_set_long(grib, "typeOfProcessedData", 4)])
def test_pp_append_singles(self): # Test pp append saving - single cubes. # load 2 arrays of >2D cubes cube = stock.simple_pp() reference_txt_path = tests.get_result_path(('cube_to_pp', 'append_single.txt')) with self.cube_save_test(reference_txt_path, reference_cubes=[cube, cube]) as temp_pp_path: iris.save(cube, temp_pp_path) # Create file iris.save(cube, temp_pp_path, append=True) # Append to file reference_txt_path = tests.get_result_path(('cube_to_pp', 'replace_single.txt')) with self.cube_save_test(reference_txt_path, reference_cubes=cube) as temp_pp_path: iris.save(cube, temp_pp_path) # Create file iris.save(cube, temp_pp_path) # Replace file
def test_distinct_xy(self): cube = stock.simple_pp() cube = cube[:4, :4] lon = cube.coord('longitude') lat = cube.coord('latitude') lon.guess_bounds() lat.guess_bounds() from iris.util import regular_step quarter = abs(regular_step(lon) * regular_step(lat) * 0.25) half = abs(regular_step(lon) * regular_step(lat) * 0.5) minx = 3.7499990463256836 maxx = 7.499998092651367 miny = 84.99998474121094 maxy = 89.99998474121094 geometry = shapely.geometry.box(minx, miny, maxx, maxy) weights = geometry_area_weights(cube, geometry) target = np.array([[0, quarter, quarter, 0], [0, half, half, 0], [0, quarter, quarter, 0], [0, 0, 0, 0]]) self.assertTrue(np.allclose(weights, target))
def test_distinct_xy(self): cube = stock.simple_pp() cube = cube[:4, :4] lon = cube.coord('longitude') lat = cube.coord('latitude') lon.guess_bounds() lat.guess_bounds() from iris.fileformats.rules import regular_step quarter = abs(regular_step(lon) * regular_step(lat) * 0.25) half = abs(regular_step(lon) * regular_step(lat) * 0.5) minx = 3.7499990463256836 maxx = 7.499998092651367 miny = 84.99998474121094 maxy = 89.99998474121094 geometry = shapely.geometry.box(minx, miny, maxx, maxy) weights = geometry_area_weights(cube, geometry) target = np.array([ [0, quarter, quarter, 0], [0, half, half, 0], [0, quarter, quarter, 0], [0, 0, 0, 0]]) self.assertTrue(np.allclose(weights, target))
def setUp(self): # Load some data from a file so that we have some deferred data. self.cube = stock.simple_pp()