def proc_vwind(): cube = iris.load(os.environ['outputpath'] + '/extract_3D_V.grib2')[0] for i in range(0, 31): iris_grib.save_messages(tweak_grib_msg("VW", cube[i]), os.environ['outputpath'] + '/construct.grib', append=True)
def proc_ice(): cube = iris.load(os.environ['outputpath'] + '/extract_2D_ICE.grib2')[0] iris_grib.save_messages(tweak_grib_msg("CI", cube), os.environ['outputpath'] + '/construct.grib', append=True)
def proc_spech(): """ derive q using relative humidity, temperature and pressure """ cube = iris.load(os.environ['outputpath'] + '/extract_3D_RH.grib2')[0] T_3D = iris.load(os.environ['outputpath'] + '/extract_3D_T.grib2')[0] T_3D = T_3D[[ 1, 2, 3, 4, 5, 6, 8, 9, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33 ]] qcubedata = np.zeros((cube.data.shape)) for y in range(cube.data.shape[1]): for x in range(cube.data.shape[2]): qcubedata[:, y, x] = rh2sph(cube.data[:, y, x] / 100., T_3D.data[:, y, x], cube.coord(axis='z').points) qcube = cube qcube.data = qcubedata[:] # fill with new data qcube.units = 'kg kg-1' qcube.rename('specific_humidity') for i in range(0, 31): iris_grib.save_messages(tweak_grib_msg("SH", qcube[i]), os.environ['outputpath'] + '/construct.grib', append=True)
def test_save_append(self): m = mock.mock_open() with mock.patch('builtins.open', m, create=True): iris_grib.save_messages([self.grib_message], 'foo.grib2', append=True) self.assertTrue(mock.call('foo.grib2', 'ab') in m.mock_calls)
def test_save(self): m = mock.mock_open() with mock.patch('builtins.open', m, create=True): # sending a MagicMock object to gribapi raises an AssertionError # as the gribapi code does a type check # this is deemed acceptable within the scope of this unit test with self.assertRaises((AssertionError, TypeError)): iris_grib.save_messages([self.grib_message], 'foo.grib2') self.assertTrue(mock.call('foo.grib2', 'wb') in m.mock_calls)
def proc_land(): cube = iris.load(os.environ['outputpath'] + '/extract_2D_L.grib2')[0] cube.units = '1' cube.rename('land_binary_mask') iris_grib.save_messages(tweak_grib_msg("LM", cube), os.environ['outputpath'] + '/construct.grib', append=True)
def proc_orog(): """ convert orography to surface geopotential using constant g """ cube = iris.load(os.environ['outputpath'] + '/extract_2D_O.grib2')[0] cube = cube * 9.80665 cube.units = 'm2 s-2' cube.rename('geopotential') iris_grib.save_messages(tweak_grib_msg("SG", cube), os.environ['outputpath'] + '/construct.grib', append=True)
def proc_temperature(): """ subsample pressure levels so all variables are on common pressure grid (remove T > 10hPa) """ cube = iris.load(os.environ['outputpath'] + '/extract_3D_T.grib2')[0] cube = cube[[ 1, 2, 3, 4, 5, 6, 8, 9, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33 ]] for i in range(0, 31): iris_grib.save_messages(tweak_grib_msg("TA", cube[i]), os.environ['outputpath'] + '/construct.grib', append=True)
def test_save(self): if six.PY3: open_func = 'builtins.open' else: open_func = '__builtin__.open' m = mock.mock_open() with mock.patch(open_func, m, create=True): # sending a MagicMock object to gribapi raises an AssertionError # as the gribapi code does a type check # this is deemed acceptable within the scope of this unit test with self.assertRaises((AssertionError, TypeError)): iris_grib.save_messages([self.grib_message], 'foo.grib2') self.assertTrue(mock.call('foo.grib2', 'wb') in m.mock_calls)
def test_save(self): # Get save-pairs for the test data. save_pairs = save_pairs_from_cube(self.test_hp_data_cube) # Check there are 3 of them (and nothing failed !) save_pairs = list(save_pairs) self.assertEqual(len(save_pairs), 3) # Get a list of just the messages. msgs = [pair[1] for pair in save_pairs] # Save the messages to a temporary file. with self.temp_filename() as temp_path: save_messages(msgs, temp_path, append=True) # Read back as GribMessage-s. msgs = list(GribMessage.messages_from_filename(temp_path)) # Check 3 messages were saved. self.assertEqual(len(msgs), 3) # Check that the PV vector (same in all messages) is as expected. # Note: HUGE gaps here because we took model levels = (1, 51, 91). self.assertEqual(msgs[0].sections[4]['NV'], 184) pv_expected = np.zeros(184, dtype=np.float64) pv_expected[[0, 50, 90]] = [0., 18191.03, 0.003] pv_expected[[92, 142, 182]] = [0., 0.036, 0.998] self.assertArrayAllClose( msgs[0].sections[4]['pv'], pv_expected, atol=0.001) # Check message #2-of-3 has the correctly encoded hybrid pressure. msg = msgs[1] # first surface type = 119 (i.e. hybrid pressure). self.assertEqual( msg.sections[4]['typeOfFirstFixedSurface'], 119) # first surface scaling = 0. self.assertEqual( msg.sections[4]['scaleFactorOfFirstFixedSurface'], 0) # first surface value = 3 -- i.e. #2 of (1, 3, 5). self.assertEqual( msg.sections[4]['scaledValueOfFirstFixedSurface'], 51) # second surface type = "NONE" -- i.e. unbounded level. self.assertEqual( msg.sections[4]['typeOfSecondFixedSurface'], 255)
def test_save(self): # Get save-pairs for the test data. save_pairs = save_pairs_from_cube(self.test_hh_data_cube) # Check there are 3 of them (and nothing failed !) save_pairs = list(save_pairs) self.assertEqual(len(save_pairs), 3) # Get a list of just the messages. msgs = [pair[1] for pair in save_pairs] # Save the messages to a temporary file. with self.temp_filename() as temp_path: save_messages(msgs, temp_path, append=True) # Read back as GribMessage-s. msgs = list(GribMessage.messages_from_filename(temp_path)) # Check 3 messages were saved. self.assertEqual(len(msgs), 3) # Check that the PV vector (same in all messages) is as expected. # Note: gaps here are because we took model levels = (1, 3, 5). self.assertArrayAllClose( msgs[0].sections[4]['pv'], [5., 0, 45., 0, 111.667, 0, 0.999, 0, 0.995, 0, 0.987, 0], atol=0.0015) # Check message #2-of-3 has the correctly encoded hybrid height. msg = msgs[1] # first surface type = 118 (i.e. hybrid height). self.assertEqual( msg.sections[4]['typeOfFirstFixedSurface'], 118) # first surface scaling = 0. self.assertEqual( msg.sections[4]['scaleFactorOfFirstFixedSurface'], 0) # first surface value = 3 -- i.e. #2 of (1, 3, 5). self.assertEqual( msg.sections[4]['scaledValueOfFirstFixedSurface'], 3) # second surface type = "NONE" -- i.e. unbounded level. self.assertEqual( msg.sections[4]['typeOfSecondFixedSurface'], 255)
def test_save(self): # Get save-pairs for the test data. save_pairs = save_pairs_from_cube(self.test_hp_data_cube) # Check there are 3 of them (and nothing failed !) save_pairs = list(save_pairs) self.assertEqual(len(save_pairs), 3) # Get a list of just the messages. msgs = [pair[1] for pair in save_pairs] # Save the messages to a temporary file. with self.temp_filename() as temp_path: save_messages(msgs, temp_path, append=True) # Read back as GribMessage-s. msgs = list(GribMessage.messages_from_filename(temp_path)) # Check 3 messages were saved. self.assertEqual(len(msgs), 3) # Check that the PV vector (same in all messages) is as expected. # Note: HUGE gaps here because we took model levels = (1, 51, 91). self.assertEqual(msgs[0].sections[4]['NV'], 184) pv_expected = np.zeros(184, dtype=np.float64) pv_expected[[0, 50, 90]] = [0., 18191.03, 0.003] pv_expected[[92, 142, 182]] = [0., 0.036, 0.998] self.assertArrayAllClose(msgs[0].sections[4]['pv'], pv_expected, atol=0.001) # Check message #2-of-3 has the correctly encoded hybrid pressure. msg = msgs[1] # first surface type = 119 (i.e. hybrid pressure). self.assertEqual(msg.sections[4]['typeOfFirstFixedSurface'], 119) # first surface scaling = 0. self.assertEqual(msg.sections[4]['scaleFactorOfFirstFixedSurface'], 0) # first surface value = 3 -- i.e. #2 of (1, 3, 5). self.assertEqual(msg.sections[4]['scaledValueOfFirstFixedSurface'], 51) # second surface type = "NONE" -- i.e. unbounded level. self.assertEqual(msg.sections[4]['typeOfSecondFixedSurface'], 255)
def test_save(self): # Get save-pairs for the test data. save_pairs = save_pairs_from_cube(self.test_hh_data_cube) # Check there are 3 of them (and nothing failed !) save_pairs = list(save_pairs) self.assertEqual(len(save_pairs), 3) # Get a list of just the messages. msgs = [pair[1] for pair in save_pairs] # Save the messages to a temporary file. with self.temp_filename() as temp_path: save_messages(msgs, temp_path, append=True) # Read back as GribMessage-s. msgs = list(GribMessage.messages_from_filename(temp_path)) # Check 3 messages were saved. self.assertEqual(len(msgs), 3) # Check that the PV vector (same in all messages) is as expected. # Note: gaps here are because we took model levels = (1, 3, 5). self.assertArrayAllClose( msgs[0].sections[4]['pv'], [5., 0, 45., 0, 111.667, 0, 0.999, 0, 0.995, 0, 0.987, 0], atol=0.0015) # Check message #2-of-3 has the correctly encoded hybrid height. msg = msgs[1] # first surface type = 118 (i.e. hybrid height). self.assertEqual(msg.sections[4]['typeOfFirstFixedSurface'], 118) # first surface scaling = 0. self.assertEqual(msg.sections[4]['scaleFactorOfFirstFixedSurface'], 0) # first surface value = 3 -- i.e. #2 of (1, 3, 5). self.assertEqual(msg.sections[4]['scaledValueOfFirstFixedSurface'], 3) # second surface type = "NONE" -- i.e. unbounded level. self.assertEqual(msg.sections[4]['typeOfSecondFixedSurface'], 255)
def proc_soilv(): cube = iris.load(os.environ['outputpath'] + '/extract_SOIL_V.grib2') depth = iris.coords.AuxCoord(0.05 * 100., bounds=[[0 * 100., 0.1 * 100.]], standard_name='depth', long_name='depth', units='cm') cube[0].add_aux_coord(depth) depth = iris.coords.AuxCoord(0.225 * 100., bounds=[[0 * 100., 0.35 * 100.]], standard_name='depth', long_name='depth', units='cm') cube[1].add_aux_coord(depth) depth = iris.coords.AuxCoord(0.675 * 100., bounds=[[0.35 * 100., 1.0 * 100.]], standard_name='depth', long_name='depth', units='cm') cube[2].add_aux_coord(depth) depth = iris.coords.AuxCoord(2 * 100., bounds=[[1.0 * 100., 3.0 * 100.]], standard_name='depth', long_name='depth', units='cm') cube[3].add_aux_coord(depth) merged_cube = cube.merge()[0] merged_cube.units = 'm3 m-3' iris_grib.save_messages(tweak_grib_msg("V1", merged_cube[0] * 10.), os.environ['outputpath'] + '/construct.grib', append=True) iris_grib.save_messages(tweak_grib_msg("V2", merged_cube[1] * 10.), os.environ['outputpath'] + '/construct.grib', append=True) iris_grib.save_messages(tweak_grib_msg("V3", merged_cube[2] * 10.), os.environ['outputpath'] + '/construct.grib', append=True) iris_grib.save_messages(tweak_grib_msg("V4", merged_cube[3] * 10.), os.environ['outputpath'] + '/construct.grib', append=True)