def _check_soil_depth(self, lbcode, blev=0.05, brsvd1=0, brlev=0.1, expect_match=True, dim=None): lbvc = 6 stash = STASH(1, 1, 1) lblev, bhlev, bhrlev, brsvd2 = None, None, None, None coords_and_dims, factories = _convert_vertical_coords(lbcode=lbcode, lbvc=lbvc, blev=blev, lblev=lblev, stash=stash, bhlev=bhlev, bhrlev=bhrlev, brsvd1=brsvd1, brsvd2=brsvd2, brlev=brlev, dim=dim) expect_result = [] if expect_match: coord = DimCoord(blev, standard_name='depth', bounds=np.vstack((brsvd1, brlev)).T, units='m', attributes={'positive': 'down'}) expect_result = [(coord, dim)] self.assertCoordsAndDimsListsMatch(coords_and_dims, expect_result) self.assertEqual(factories, [])
def test_implied_height_1m5(self): self._check_height( blev=75.2, stash=STASH(1, 3, 236), expect_normal=False, expect_fixed_height=1.5, )
def test_normal_height__absent_mixed_vector(self): data = [-1, 12.3, -1, 123.4] dim = 2 for blev in [data, np.asarray(data)]: for dim_i in [dim, (dim,)]: self._check_height(blev=blev, stash=STASH(1, 1, 1), expect_normal=False, dim=dim_i)
def _check_pressure(self, lbcode, expect_match=True): lbvc = 8 blev = 250.3 stash = STASH(1, 1, 1) lblev, bhlev, bhrlev, brsvd1, brsvd2, brlev = \ None, None, None, None, None, None coords_and_dims, factories = _convert_scalar_vertical_coords( lbcode=lbcode, lbvc=lbvc, blev=blev, lblev=lblev, stash=stash, bhlev=bhlev, bhrlev=bhrlev, brsvd1=brsvd1, brsvd2=brsvd2, brlev=brlev) if expect_match: expect_result = [(DimCoord([blev], long_name='pressure', units='hPa'), None)] else: expect_result = [] self.assertCoordsAndDimsListsMatch(coords_and_dims, expect_result) self.assertEqual(factories, [])
def _check_soil_depth(self, lbcode, expect_match=True): lbvc = 6 blev = 0.05 brsvd1, brlev = 0, 0.1 stash = STASH(1, 1, 1) lblev, bhlev, bhrlev, brsvd2 = None, None, None, None coords_and_dims, factories = _convert_scalar_vertical_coords( lbcode=lbcode, lbvc=lbvc, blev=blev, lblev=lblev, stash=stash, bhlev=bhlev, bhrlev=bhrlev, brsvd1=brsvd1, brsvd2=brsvd2, brlev=brlev) if expect_match: expect_result = [(DimCoord([blev], standard_name='depth', units='m', bounds=[[brsvd1, brlev]], attributes={'positive': 'down'}), None)] else: expect_result = [] self.assertCoordsAndDimsListsMatch(coords_and_dims, expect_result) self.assertEqual(factories, [])
def _check_potm(self, lbcode, expect_match=True): lbvc = 19 blev = 130.6 stash = STASH(1, 1, 1) lblev, bhlev, bhrlev, brsvd1, brsvd2, brlev = \ None, None, None, None, None, None coords_and_dims, factories = _convert_scalar_vertical_coords( lbcode=lbcode, lbvc=lbvc, blev=blev, lblev=lblev, stash=stash, bhlev=bhlev, bhrlev=bhrlev, brsvd1=brsvd1, brsvd2=brsvd2, brlev=brlev) if expect_match: expect_result = [ (DimCoord([blev], standard_name='air_potential_temperature', units='K', attributes={'positive': 'up'}), None) ] else: expect_result = [] self.assertCoordsAndDimsListsMatch(coords_and_dims, expect_result) self.assertEqual(factories, [])
def _check_soil_level(self, lbcode, expect_match=True): lbvc = 6 lblev = 12.3 brsvd1, brlev = 0, 0 stash = STASH(1, 1, 1) blev, bhlev, bhrlev, brsvd2 = None, None, None, None coords_and_dims, factories = _convert_scalar_vertical_coords( lbcode=lbcode, lbvc=lbvc, blev=blev, lblev=lblev, stash=stash, bhlev=bhlev, bhrlev=bhrlev, brsvd1=brsvd1, brsvd2=brsvd2, brlev=brlev) if expect_match: expect_result = [(DimCoord([lblev], long_name='soil_model_level_number', attributes={'positive': 'down'}), None)] else: expect_result = [] self.assertCoordsAndDimsListsMatch(coords_and_dims, expect_result) self.assertEqual(factories, [])
def _check_soil_level(self, lbcode, lblev=12.3, expect_match=True, dim=None): lbvc = 6 stash = STASH(1, 1, 1) brsvd1, brlev = 0, 0 if hasattr(lblev, "__iter__"): brsvd1 = [0] * len(lblev) brlev = [0] * len(lblev) blev, bhlev, bhrlev, brsvd2 = None, None, None, None coords_and_dims, factories = _convert_vertical_coords( lbcode=lbcode, lbvc=lbvc, blev=blev, lblev=lblev, stash=stash, bhlev=bhlev, bhrlev=bhrlev, brsvd1=brsvd1, brsvd2=brsvd2, brlev=brlev, dim=dim, ) expect_result = [] if expect_match: coord = DimCoord( lblev, long_name="soil_model_level_number", attributes={"positive": "down"}, ) expect_result = [(coord, dim)] self.assertCoordsAndDimsListsMatch(coords_and_dims, expect_result) self.assertEqual(factories, [])
def test_unknown_lbvc(self): lbvc = 999 blev, lblev, bhlev, bhrlev, brsvd1, brsvd2, brlev = ( None, None, None, None, None, None, None, ) lbcode = _lbcode(0) # unused stash = STASH(1, 1, 1) # unused coords_and_dims, factories = _convert_vertical_coords( lbcode=lbcode, lbvc=lbvc, blev=blev, lblev=lblev, stash=stash, bhlev=bhlev, bhrlev=bhrlev, brsvd1=brsvd1, brsvd2=brsvd2, brlev=brlev, ) self.assertEqual(coords_and_dims, []) self.assertEqual(factories, [])
def _check(self, lblev=37.0, blev=9596.3, brlev=9500.0, brsvd1=9800.0, bhlev=0.35, bhrlev=0.31, brsvd2=0.39, dim=None): lbvc = 65 lbcode = _lbcode(0) # unused stash = STASH(1, 1, 1) # unused coords_and_dims, factories = _convert_vertical_coords( lbcode=lbcode, lbvc=lbvc, blev=blev, lblev=lblev, stash=stash, bhlev=bhlev, bhrlev=bhrlev, brsvd1=brsvd1, brsvd2=brsvd2, brlev=brlev, dim=dim) expect_coords_and_dims = [ (DimCoord(lblev, standard_name='model_level_number', attributes={'positive': 'up'}), dim)] brlev = np.atleast_1d(brlev) brsvd1 = np.atleast_1d(brsvd1) expect_coords_and_dims.append( (DimCoord(blev, long_name='level_height', units='m', bounds=np.vstack((brlev, brsvd1)).T, attributes={'positive': 'up'}), dim)) bhrlev = np.atleast_1d(bhrlev) brsvd2 = np.atleast_1d(brsvd2) expect_coords_and_dims.append( (AuxCoord(bhlev, long_name='sigma', bounds=np.vstack((bhrlev, brsvd2)).T), dim)) expect_factories = [(HybridHeightFactory, [{'long_name': 'level_height'}, {'long_name': 'sigma'}, Reference('orography')])] self.assertCoordsAndDimsListsMatch(coords_and_dims, expect_coords_and_dims) self.assertEqual(factories, expect_factories)
def _check_pressure(self, lbcode, blev=250.3, expect_match=True, dim=None): lbvc = 8 stash = STASH(1, 1, 1) lblev, bhlev, bhrlev, brsvd1, brsvd2, brlev = ( None, None, None, None, None, None, ) coords_and_dims, factories = _convert_vertical_coords( lbcode=lbcode, lbvc=lbvc, blev=blev, lblev=lblev, stash=stash, bhlev=bhlev, bhrlev=bhrlev, brsvd1=brsvd1, brsvd2=brsvd2, brlev=brlev, dim=dim, ) if expect_match: expect_result = [(DimCoord(blev, long_name="pressure", units="hPa"), dim)] else: expect_result = [] self.assertCoordsAndDimsListsMatch(coords_and_dims, expect_result) self.assertEqual(factories, [])
def test_implied_height_10m(self): self._check_height( blev=75.2, stash=STASH(1, 3, 225), expect_normal=False, expect_fixed_height=10.0, )
def test_implied_height_10m__vector(self): data = list(range(10)) dim = 4 for blev in [data, np.asarray(data)]: for dim_i in [dim, (dim,)]: self._check_height(blev=blev, stash=STASH(1, 3, 225), expect_normal=False, expect_fixed_height=10.0, dim=dim_i)
def test_implied_height_1m5__vector(self): data = [1, 2, 3, 4] dim = 3 for blev in [data, np.asarray(data)]: for dim_i in [dim, (dim,)]: self._check_height(blev=blev, stash=STASH(1, 3, 236), expect_normal=False, expect_fixed_height=1.5, dim=dim_i)
def test_fc_cf_air_temp(self): lbuser = [1, 0, 0, 0, 0, 0, 0] lbfc = 16 stash = STASH(lbuser[6], lbuser[3] // 1000, lbuser[3] % 1000) field = _mock_field(lbuser=lbuser, lbfc=lbfc, stash=stash) (factories, references, standard_name, long_name, units, attributes, cell_methods, dim_coords_and_dims, aux_coords_and_dims) = convert(field) self.assertEqual(standard_name, 'air_temperature') self.assertEqual(units, 'K')
def test_no_std_name(self): lbuser = [1, 0, 0, 0, 0, 0, 0] lbfc = 0 stash = STASH(lbuser[6], lbuser[3] // 1000, lbuser[3] % 1000) field = _mock_field(lbuser=lbuser, lbfc=lbfc, stash=stash) (factories, references, standard_name, long_name, units, attributes, cell_methods, dim_coords_and_dims, aux_coords_and_dims) = convert(field) self.assertIsNone(standard_name) self.assertIsNone(units)
def _check_depth(self, lbcode, lblev=23.0, blev=123.4, brlev=0.0, brsvd1=0.0, expect_bounds=True, expect_match=True, expect_mixed=False, dim=None): lbvc = 2 stash = STASH(1, 1, 1) bhlev, bhrlev, brsvd2 = None, None, None coords_and_dims, factories = _convert_vertical_coords(lbcode=lbcode, lbvc=lbvc, blev=blev, lblev=lblev, stash=stash, bhlev=bhlev, bhrlev=bhrlev, brsvd1=brsvd1, brsvd2=brsvd2, brlev=brlev, dim=dim) if expect_match: expect_result = [(DimCoord(lblev, standard_name='model_level_number', attributes={'positive': 'down'}), dim)] if expect_bounds: brsvd1 = np.atleast_1d(brsvd1) brlev = np.atleast_1d(brlev) if expect_mixed: lower = np.where(brsvd1 == brlev, blev, brsvd1) upper = np.where(brsvd1 == brlev, blev, brlev) else: lower, upper = brsvd1, brlev bounds = np.vstack((lower, upper)).T expect_result.append((DimCoord(blev, standard_name='depth', units='m', bounds=bounds, attributes={'positive': 'down'}), dim)) else: expect_result.append((DimCoord(blev, standard_name='depth', units='m', attributes={'positive': 'down'}), dim)) else: expect_result = [] self.assertCoordsAndDimsListsMatch(coords_and_dims, expect_result) self.assertEqual(factories, [])
def _check(self, lblev=37.0, bhlev=850.1, bhrlev=810.0, brsvd2=875.0, blev=0.15, brlev=0.11, brsvd1=0.19, expect_match=True, dim=None): lbvc = 9 lbcode = _lbcode(0) # unused stash = STASH(1, 1, 1) # unused coords_and_dims, factories = _convert_vertical_coords(lbcode=lbcode, lbvc=lbvc, blev=blev, lblev=lblev, stash=stash, bhlev=bhlev, bhrlev=bhrlev, brsvd1=brsvd1, brsvd2=brsvd2, brlev=brlev, dim=dim) expect_coords_and_dims = [(DimCoord(lblev, standard_name='model_level_number', attributes={'positive': 'up'}), dim)] bhrlev = np.atleast_1d(bhrlev) brsvd2 = np.atleast_1d(brsvd2) expect_coords_and_dims.append((DimCoord(bhlev, long_name='level_pressure', units='Pa', bounds=np.vstack( (bhrlev, brsvd2)).T), dim)) brlev = np.atleast_1d(brlev) brsvd1 = np.atleast_1d(brsvd1) expect_coords_and_dims.append((AuxCoord(blev, long_name='sigma', bounds=np.vstack( (brlev, brsvd1)).T), dim)) expect_factories = [(HybridPressureFactory, [{ 'long_name': 'level_pressure' }, { 'long_name': 'sigma' }, Reference('surface_air_pressure')])] self.assertCoordsAndDimsListsMatch(coords_and_dims, expect_coords_and_dims) self.assertEqual(factories, expect_factories)
def callback_HadCM2_ts_SAT_ann_18602100_b_pp(cube, field, filename): def reset_pole(coord_name): coord = cube.coord(coord_name) coord.rename(coord.name().replace("grid_", "")) coord.coord_system = coord.coord_system.ellipsoid reset_pole("grid_latitude") reset_pole("grid_longitude") cube.standard_name = "air_temperature" cube.units = "Celsius" cube.attributes["STASH"] = STASH(1, 3, 236) # Force the height to 1.5m if cube.coords("height"): cube.remove_coord("height") height_coord = iris.coords.DimCoord(1.5, standard_name="height", units="m") cube.add_aux_coord(height_coord)
def callback_HadCM2_ts_SAT_ann_18602100_b_pp(cube, field, filename): def reset_pole(coord_name): coord = cube.coord(coord_name) coord.rename(coord.name().replace('grid_', '')) coord.coord_system = coord.coord_system.ellipsoid reset_pole('grid_latitude') reset_pole('grid_longitude') cube.standard_name = 'air_temperature' cube.units = 'Celsius' cube.attributes['STASH'] = STASH(1, 3, 236) # Force the height to 1.5m if cube.coords("height"): cube.remove_coord("height") height_coord = iris.coords.DimCoord(1.5, standard_name='height', units='m') cube.add_aux_coord(height_coord)
def test_valid(self, expect_match=True): lbvc = 65 lblev = 37.0 bhlev = 0.35 # sigma bhrlev, brsvd2 = 0.31, 0.39 # sigma bounds blev = 9596.3 # level_height brlev, brsvd1 = 9500.0, 9800.0 # level_height bounds lbcode = _lbcode(0) # unused stash = STASH(1, 1, 1) # unused coords_and_dims, factories = _convert_scalar_vertical_coords( lbcode=lbcode, lbvc=lbvc, blev=blev, lblev=lblev, stash=stash, bhlev=bhlev, bhrlev=bhrlev, brsvd1=brsvd1, brsvd2=brsvd2, brlev=brlev) if expect_match: expect_coords_and_dims = [ (DimCoord([37.0], standard_name='model_level_number', attributes={'positive': 'up'}), None), (DimCoord([9596.3], long_name='level_height', units='m', bounds=[brlev, brsvd1], attributes={'positive': 'up'}), None), (AuxCoord([0.35], long_name='sigma', bounds=[bhrlev, brsvd2]), None) ] expect_factories = [(HybridHeightFactory, [{ 'long_name': 'level_height' }, { 'long_name': 'sigma' }, Reference('orography')]) ] else: expect_coords_and_dims = [] expect_factories = [] self.assertCoordsAndDimsListsMatch(coords_and_dims, expect_coords_and_dims) self.assertEqual(factories, expect_factories)
def test_valid(self, expect_match=True): lbvc = 9 lblev = 37.0 bhlev = 850.1 # pressure bhrlev, brsvd2 = 810.0, 875.0 # pressure bounds blev = 0.15 # sigma brlev, brsvd1 = 0.11, 0.19 # sigma bounds lbcode = _lbcode(0) # unused stash = STASH(1, 1, 1) # unused coords_and_dims, factories = _convert_scalar_vertical_coords( lbcode=lbcode, lbvc=lbvc, blev=blev, lblev=lblev, stash=stash, bhlev=bhlev, bhrlev=bhrlev, brsvd1=brsvd1, brsvd2=brsvd2, brlev=brlev) if expect_match: expect_coords_and_dims = [ (DimCoord([37.0], standard_name='model_level_number', attributes={'positive': 'up'}), None), (DimCoord([850.1], long_name='level_pressure', units='Pa', bounds=[810.0, 875.0]), None), (AuxCoord([0.15], long_name='sigma', bounds=[brlev, brsvd1]), None) ] expect_factories = [(HybridPressureFactory, [{ 'long_name': 'level_pressure' }, { 'long_name': 'sigma' }, Reference('surface_air_pressure')])] else: expect_coords_and_dims = [] expect_factories = [] self.assertCoordsAndDimsListsMatch(coords_and_dims, expect_coords_and_dims) self.assertEqual(factories, expect_factories)
def _check_depth(self, lbcode, lblev, brlev=0.0, brsvd1=0.0, expect_bounds=True, expect_match=True): lbvc = 2 lblev = 23.0 blev = 123.4 stash = STASH(1, 1, 1) bhlev, bhrlev, brsvd2 = None, None, None coords_and_dims, factories = _convert_scalar_vertical_coords( lbcode=lbcode, lbvc=lbvc, blev=blev, lblev=lblev, stash=stash, bhlev=bhlev, bhrlev=bhrlev, brsvd1=brsvd1, brsvd2=brsvd2, brlev=brlev) if expect_match: expect_result = [(DimCoord([lblev], standard_name='model_level_number', attributes={'positive': 'down'}), None)] if expect_bounds: expect_result.append((DimCoord(blev, standard_name='depth', units='m', bounds=[brsvd1, brlev], attributes={'positive': 'down'}), None)) else: expect_result.append((DimCoord(blev, standard_name='depth', units='m', attributes={'positive': 'down'}), None)) else: expect_result = [] self.assertCoordsAndDimsListsMatch(coords_and_dims, expect_result) self.assertEqual(factories, [])
def _check_potm(self, lbcode, blev=130.6, expect_match=True, dim=None): lbvc = 19 stash = STASH(1, 1, 1) lblev, bhlev, bhrlev, brsvd1, brsvd2, brlev = ( None, None, None, None, None, None, ) coords_and_dims, factories = _convert_vertical_coords( lbcode=lbcode, lbvc=lbvc, blev=blev, lblev=lblev, stash=stash, bhlev=bhlev, bhrlev=bhrlev, brsvd1=brsvd1, brsvd2=brsvd2, brlev=brlev, dim=dim, ) if expect_match: expect_result = [ ( DimCoord( blev, standard_name="air_potential_temperature", units="K", attributes={"positive": "up"}, ), dim, ) ] else: expect_result = [] self.assertCoordsAndDimsListsMatch(coords_and_dims, expect_result) self.assertEqual(factories, [])
def callback_model_b_pp(cube, field, filename): cube.standard_name = 'air_temperature' cube.units = 'K' cube.attributes['STASH'] = STASH(1, 16, 203)
def test_normal_height__present(self): self._check_height(blev=12.3, stash=STASH(1, 1, 1))
def callback_000003000000_16_202_000128_1860_09_01_00_00_b_pp(cube, field, filename): cube.attributes['STASH'] = STASH(1, 16, 202) cube.standard_name = 'geopotential_height' cube.units = 'm'
def callback_model_b_pp(cube, field, filename): cube.standard_name = "air_temperature" cube.units = "K" cube.attributes["STASH"] = STASH(1, 16, 203)
def test_normal_height__present_vector(self): data = [12.3, 123.4, 1234.5] dim = 0 for blev in [data, np.asarray(data)]: for dim_i in [dim, (dim,)]: self._check_height(blev=blev, stash=STASH(1, 1, 1), dim=dim_i)
def test_normal_height__absent(self): self._check_height(blev=-1, stash=STASH(1, 1, 1), expect_normal=False)