def test_lbpack_SplittableInt(self): lbpack = mock.Mock(spec_set=SplittableInt) proxy = PPDataProxy(None, None, None, None, None, lbpack, None, None) self.assertEqual(proxy.lbpack, lbpack) self.assertIs(proxy.lbpack, lbpack)
def _make_test_message(self, sections): raw_message = mock.Mock(sections=sections, _message_id=self.message_id) file_ref = mock.Mock(open_file=self.grib_fh) return GribMessage(raw_message, None, file_ref=file_ref)
def test_other_lbtim_ib(self): # lbtim.ib = 5 -> non-specific aggregation field = mock.MagicMock(lbproc=4096, lbtim=mock.Mock(ia=24, ib=5, ic=3)) res = _all_other_rules(field)[CELL_METHODS_INDEX] expected = [CellMethod('minimum', 'time')] self.assertEqual(res, expected)
def test_depth_c(self): new_depth_c = mock.Mock(units=Unit('m'), nbounds=0, shape=(1, )) self.factory.update(self.depth_c, new_depth_c) self.assertIs(self.factory.depth_c, new_depth_c)
def test_depth_c_incompatible_units(self): new_depth_c = mock.Mock(units=Unit('Pa'), nbounds=0, shape=(1, )) with self.assertRaises(ValueError): self.factory.update(self.depth_c, new_depth_c)
def test_depth(self): new_depth = mock.Mock(units=Unit('m'), nbounds=0) self.factory.update(self.depth, new_depth) self.assertIs(self.factory.depth, new_depth)
def test_b(self): new_b = mock.Mock(units=Unit('1'), nbounds=0, shape=(1, )) self.factory.update(self.b, new_b) self.assertIs(self.factory.b, new_b)
def check_attribute_compliance_call(self, value): self.set_attribute(value) with Saver(mock.Mock(), 'NETCDF4') as saver: saver.check_attribute_compliance(self.container, self.data)
def test_datetimelike_point(self): point = mock.Mock(timetuple=mock.Mock()) cell = Cell(point=object(), bound=[object(), object()]) with self.assertRaisesRegexp(TypeError, 'bounded region for datetime'): cell.contains_point(point)
def test_zlev_same_boundedness(self): new_zlev = mock.Mock(units=Unit('m'), nbounds=2) with self.assertRaises(ValueError): self.factory.update(self.zlev, new_zlev)
def test_edition_1(self): grib_message = 'regular_ll' grib_fh = mock.Mock(tell=self.tell) wrapper = GribWrapper(grib_message, grib_fh) self.assertEqual(wrapper.grib_message, grib_message)
def test_zlev_too_many_bounds(self): new_zlev = mock.Mock(units=Unit('m'), nbounds=4) with self.assertRaises(ValueError): self.factory.update(self.zlev, new_zlev)
def test_zlev(self): new_zlev = mock.Mock(units=Unit('m'), nbounds=0) self.factory.update(self.zlev, new_zlev) self.assertIs(self.factory.zlev, new_zlev)
def test_sigma_zlev_same_boundedness(self): new_sigma = mock.Mock(units=Unit('1'), nbounds=2) with self.assertRaises(ValueError): self.factory.update(self.sigma, new_sigma)
def test_eta(self): new_eta = mock.Mock(units=Unit('m'), nbounds=0) self.factory.update(self.eta, new_eta) self.assertIs(self.factory.eta, new_eta)
def setUp(self): self.cube = stock.simple_2d() self.extrapolation = 'extrapolation_mode' self.scheme = mock.Mock(name='linear scheme')
def test_eta_incompatible_units(self): new_eta = mock.Mock(units=Unit('Pa'), nbounds=0) with self.assertRaises(ValueError): self.factory.update(self.eta, new_eta)
def test_hybrid_pressure_round_trip(self): # Use pp.load_cubes() to convert fake PPFields into Cubes. # NB. Use MagicMock so that SplittableInt header items, such as # LBCODE, support len(). def field_with_data(scale=1): x, y = 40, 30 mock_data = np.arange(1200).reshape(y, x) * scale mock_core_data = mock.MagicMock(return_value=mock_data) field = mock.MagicMock(core_data=mock_core_data, realised_dtype=mock_data.dtype, lbcode=[1], lbnpt=x, lbrow=y, bzx=350, bdx=1.5, bzy=40, bdy=1.5, lbuser=[0] * 7, lbrsvd=[0] * 4) field._x_coord_name = lambda: 'longitude' field._y_coord_name = lambda: 'latitude' field.coord_system = lambda: None return field # Make a fake reference surface field. pressure_field = field_with_data(10) pressure_field.stash = iris.fileformats.pp.STASH(1, 0, 409) pressure_field.lbuser[3] = 409 # Make a fake data field which needs the reference surface. model_level = 5678 sigma_lower, sigma, sigma_upper = 0.85, 0.9, 0.95 delta_lower, delta, delta_upper = 0.05, 0.1, 0.15 data_field = field_with_data() data_field.configure_mock(lbvc=9, lblev=model_level, bhlev=delta, bhrlev=delta_lower, blev=sigma, brlev=sigma_lower, brsvd=[sigma_upper, delta_upper]) # Convert both fields to cubes. load = mock.Mock(return_value=iter([pressure_field, data_field])) with mock.patch('iris.fileformats.pp.load', new=load) as load: pressure_cube, data_cube = iris.fileformats.pp.load_cubes('DUMMY') # Check the reference surface cube looks OK. self.assertEqual(pressure_cube.standard_name, 'surface_air_pressure') self.assertEqual(pressure_cube.units, 'Pa') # Check the data cube is set up to use hybrid-pressure. self._test_coord(data_cube, model_level, standard_name='model_level_number') self._test_coord(data_cube, delta, [delta_lower, delta_upper], long_name='level_pressure') self._test_coord(data_cube, sigma, [sigma_lower, sigma_upper], long_name='sigma') aux_factories = data_cube.aux_factories self.assertEqual(len(aux_factories), 1) surface_coord = aux_factories[0].dependencies['surface_air_pressure'] self.assertArrayEqual(surface_coord.points, np.arange(12000, step=10).reshape(30, 40)) # Now use the save rules to convert the Cubes back into PPFields. pressure_field = iris.fileformats.pp.PPField3() pressure_field.lbfc = 0 pressure_field.lbvc = 0 pressure_field.brsvd = [None, None] pressure_field.lbuser = [None] * 7 pressure_field = verify(pressure_cube, pressure_field) data_field = iris.fileformats.pp.PPField3() data_field.lbfc = 0 data_field.lbvc = 0 data_field.brsvd = [None, None] data_field.lbuser = [None] * 7 data_field = verify(data_cube, data_field) # The reference surface field should have STASH=409 self.assertArrayEqual(pressure_field.lbuser, [None, None, None, 409, None, None, 1]) # Check the data field has the vertical coordinate as originally # specified. self.assertEqual(data_field.lbvc, 9) self.assertEqual(data_field.lblev, model_level) self.assertEqual(data_field.bhlev, delta) self.assertEqual(data_field.bhrlev, delta_lower) self.assertEqual(data_field.blev, sigma) self.assertEqual(data_field.brlev, sigma_lower) self.assertEqual(data_field.brsvd, [sigma_upper, delta_upper])
def test_a(self): new_a = mock.Mock(units=Unit('1'), nbounds=0, shape=(1, )) self.factory.update(self.a, new_a) self.assertIs(self.factory.a, new_a)
def test_hybrid_height_round_trip_no_reference(self): # Use pp.load_cubes() to convert fake PPFields into Cubes. # NB. Use MagicMock so that SplittableInt header items, such as # LBCODE, support len(). def field_with_data(scale=1): x, y = 40, 30 mock_data = np.arange(1200).reshape(y, x) * scale mock_core_data = mock.MagicMock(return_value=mock_data) field = mock.MagicMock(core_data=mock_core_data, realised_dtype=mock_data.dtype, lbcode=[1], lbnpt=x, lbrow=y, bzx=350, bdx=1.5, bzy=40, bdy=1.5, lbuser=[0] * 7, lbrsvd=[0] * 4) field._x_coord_name = lambda: 'longitude' field._y_coord_name = lambda: 'latitude' field.coord_system = lambda: None return field # Make a fake data field which needs the reference surface. model_level = 5678 sigma_lower, sigma, sigma_upper = 0.85, 0.9, 0.95 delta_lower, delta, delta_upper = 0.05, 0.1, 0.15 data_field = field_with_data() data_field.configure_mock(lbvc=65, lblev=model_level, bhlev=sigma, bhrlev=sigma_lower, blev=delta, brlev=delta_lower, brsvd=[delta_upper, sigma_upper]) # Convert field to a cube. load = mock.Mock(return_value=iter([data_field])) with mock.patch('iris.fileformats.pp.load', new=load) as load, \ mock.patch('warnings.warn') as warn: data_cube, = iris.fileformats.pp.load_cubes('DUMMY') msg = "Unable to create instance of HybridHeightFactory. " \ "The source data contains no field(s) for 'orography'." warn.assert_called_once_with(msg) # Check the data cube is set up to use hybrid height. self._test_coord(data_cube, model_level, standard_name='model_level_number') self._test_coord(data_cube, delta, [delta_lower, delta_upper], long_name='level_height') self._test_coord(data_cube, sigma, [sigma_lower, sigma_upper], long_name='sigma') # Check that no aux factory is created (due to missing # reference surface). aux_factories = data_cube.aux_factories self.assertEqual(len(aux_factories), 0) # Now use the save rules to convert the Cube back into a PPField. data_field = iris.fileformats.pp.PPField3() data_field.lbfc = 0 data_field.lbvc = 0 data_field.brsvd = [None, None] data_field.lbuser = [None] * 7 data_field = verify(data_cube, data_field) # Check the data field has the vertical coordinate as originally # specified. self.assertEqual(data_field.lbvc, 65) self.assertEqual(data_field.lblev, model_level) self.assertEqual(data_field.bhlev, sigma) self.assertEqual(data_field.bhrlev, sigma_lower) self.assertEqual(data_field.blev, delta) self.assertEqual(data_field.brlev, delta_lower) self.assertEqual(data_field.brsvd, [delta_upper, sigma_upper])
def test_b_non_scalar(self): new_b = mock.Mock(units=Unit('1'), nbounds=0, shape=(10, )) with self.assertRaises(ValueError): self.factory.update(self.b, new_b)
def update(config): config['cf_profile'] = mock.Mock(name='cf_profile') config['cf_patch'] = mock.Mock(name='cf_patch') config['cf_patch_conventions'] = cf_patch_conventions
def test_depth_c_non_scalar(self): new_depth_c = mock.Mock(units=Unit('m'), nbounds=0, shape=(10, )) with self.assertRaises(ValueError): self.factory.update(self.depth_c, new_depth_c)
def test_fp_shape(self): frt = mock.Mock(shape=(1,)) fp = mock.Mock(shape=(2,)) emsg = 'scalar forecast period' with self.assertRaisesRegexp(ValueError, emsg): validity_time_coord(frt, fp)
def test_coord_system(self): self.coord_to_change.coord_system = mock.Mock() self._props_fail('air_temperature')
def test_s(self): new_s = mock.Mock(units=Unit('1'), nbounds=0) self.factory.update(self.s, new_s) self.assertIs(self.factory.s, new_s)
def test_climatology_max(self): field = mock.MagicMock(lbproc=4096, lbtim=mock.Mock(ia=24, ib=3, ic=3)) res = _all_other_rules(field)[CELL_METHODS_INDEX] expected = [CellMethod('minimum', 'time')] self.assertEqual(res, expected)
def test_s_too_many_bounds(self): new_s = mock.Mock(units=Unit('1'), nbounds=4) with self.assertRaises(ValueError): self.factory.update(self.s, new_s)
def test_hourly_mean(self): # lbtim.ia = 1 -> hourly field = mock.MagicMock(lbproc=128, lbtim=mock.Mock(ia=1, ib=2, ic=3)) res = _all_other_rules(field)[CELL_METHODS_INDEX] expected = [CellMethod('mean', 'time', '1 hour')] self.assertEqual(res, expected)
def test_c(self): new_c = mock.Mock(units=Unit('1'), nbounds=0) self.factory.update(self.c, new_c) self.assertIs(self.factory.c, new_c)