def test_duplicate(self): paths = ( tests.get_data_path(['PP', 'aPPglob1', 'global.pp']), tests.get_data_path(['PP', 'aPPglob1', 'gl?bal.pp']) ) cubes = iris.load(paths) self.assertEqual(len(cubes), 2)
def test_too_many(self): paths = ( tests.get_data_path(["PP", "aPPglob1", "global.pp"]), tests.get_data_path(["PP", "aPPglob1", "gl?bal.pp"]), ) with self.assertRaises(iris.exceptions.ConstraintMismatchError): iris.load_cube(paths)
def setUp(self): self.dec_path = tests.get_data_path(['PP', 'globClim1', 'dec_subset.pp']) self.theta_path = tests.get_data_path(['PP', 'globClim1', 'theta.pp']) self.humidity = iris.Constraint(SN_SPECIFIC_HUMIDITY) self.theta = iris.Constraint(SN_AIR_POTENTIAL_TEMPERATURE) # Coord based constraints self.level_10 = iris.Constraint(model_level_number=10) self.level_22 = iris.Constraint(model_level_number=22) # Value based coord constraint self.level_30 = iris.Constraint(model_level_number=30) self.level_gt_30_le_3 = iris.Constraint(model_level_number=lambda c: ( c > 30 ) | (c <= 3)) self.invalid_inequality = iris.Constraint(coord_values={'model_level_number': lambda c: c > 1000}) # bound based coord constraint self.level_height_of_model_level_number_10 = iris.Constraint(level_height=1900) self.model_level_number_10_22 = iris.Constraint(model_level_number=[10, 22]) # Invalid constraints self.pressure_950 = iris.Constraint(model_level_number=950) self.lat_30 = iris.Constraint(latitude=30) self.lat_gt_45 = iris.Constraint(latitude=lambda c: c > 45)
def test_too_many(self): paths = ( tests.get_data_path(['PP', 'aPPglob1', 'global.pp']), tests.get_data_path(['PP', 'aPPglob1', 'gl?bal.pp']) ) with self.assertRaises(iris.exceptions.ConstraintMismatchError): iris.load_cube(paths)
def test_duplicate(self): paths = ( tests.get_data_path(["PP", "aPPglob1", "global.pp"]), tests.get_data_path(["PP", "aPPglob1", "gl?bal.pp"]), ) cubes = iris.load(paths) self.assertEqual(len(cubes), 2)
def test_no_file(self): # Test an IOError is recieved when a filename is given which doesnt match any files real_file = ['PP', 'globClim1', 'theta.pp'] non_existant_file = ['PP', 'globClim1', 'no_such_file*'] self.assertRaises(IOError, iris.load, tests.get_data_path(non_existant_file)) self.assertRaises(IOError, iris.load, [tests.get_data_path(non_existant_file), tests.get_data_path(real_file)]) self.assertRaises(IOError, iris.load, [tests.get_data_path(real_file), tests.get_data_path(non_existant_file)])
def test_pp_callback(self): fname = tests.get_data_path(["PP", "trui", "air_temp_T24", "200812011200__qwqg12ff.T24.pp"]) cube = iris.load_strict(fname, callback=truipp_filename_callback) self.assertCML(cube, ['uri_callback', 'trui_t24.cml']) fname = tests.get_data_path(["PP", "trui", "air_temp_init", "200812011200__qwqu12ff.initanl.pp"]) cube = iris.load_strict(fname, callback=truipp_filename_callback) self.assertCML(cube, ['uri_callback', 'trui_init.cml'])
def test_nonexist_wild(self): paths = ( tests.get_data_path(["PP", "aPPglob1", "global.pp"]), tests.get_data_path(["PP", "_guaranteed_non_exist_*.pp"]), ) with self.assertRaises(IOError) as error_trap: cubes = iris.load(paths) self.assertTrue(error_trap.exception.message.startswith("One or more of the files specified did not exist"))
def test_nonexist_wild(self): paths = ( tests.get_data_path(['PP', 'aPPglob1', 'global.pp']), tests.get_data_path(['PP', '_guaranteed_non_exist_*.pp']), ) with self.assertRaises(IOError) as error_trap: cubes = iris.load(paths) self.assertIn('One or more of the files specified did not exist', str(error_trap.exception))
def test_nonexist(self): paths = ( tests.get_data_path(['PP', 'aPPglob1', 'global.pp']), tests.get_data_path(['PP', '_guaranteed_non_exist.pp']), ) with self.assertRaises(IOError) as error_trap: cubes = iris.load(paths) self.assertTrue(error_trap.exception.message.startswith( 'One or more of the files specified did not exist'))
def test_no_time_coord_in_cubes(self): path0 = tests.get_data_path(('PP', 'aPPglob1', 'global.pp')) path1 = tests.get_data_path(('PP', 'aPPglob1', 'global_t_forecast.pp')) cube0 = iris.load_cube(path0) cube1 = iris.load_cube(path1) cubes = iris.cube.CubeList([cube0, cube1]) result = copy.copy(cubes) unify_time_units(result) self.assertEqual(cubes, result)
def setUp(self): filename = tests.get_data_path( ('NetCDF', 'label_and_climate', 'A1B-99999a-river-sep-2070-2099.nc')) self.cfr_start = cf.CFReader(filename) filename = tests.get_data_path( ('NetCDF', 'label_and_climate', 'small_FC_167_mon_19601101.nc')) self.cfr_end = cf.CFReader(filename)
def test_load(self): cubes = iris.load(tests.get_data_path(('GRIB', 'rotated_uk', "uk_wrongparam.grib1"))) self.assertCML(cubes, ("grib_load", "rotated.cml")) cubes = iris.load(tests.get_data_path(('GRIB', "time_processed", "time_bound.grib2"))) self.assertCML(cubes, ("grib_load", "time_bound.cml")) cubes = iris.load(tests.get_data_path(('GRIB', "3_layer_viz", "3_layer.grib2"))) cubes = iris.cube.CubeList([cubes[1], cubes[0], cubes[2]]) self.assertCML(cubes, ("grib_load", "3_layer.cml"))
def test_iris_loading(self): ff32_fname = tests.get_data_path(('FF', 'n48_multi_field.ieee32')) ff64_fname = tests.get_data_path(('FF', 'n48_multi_field')) ff32_cubes = iris.load(ff32_fname) ff64_cubes = iris.load(ff64_fname) for ff32, ff64 in zip(ff32_cubes, ff64_cubes): # load the data _, _ = ff32.data, ff64.data self.assertEqual(ff32, ff64)
def test_meanTrial_diff(self): air_temp_T00_cube = iris.load_strict(tests.get_data_path(('PP', 'trui', 'air_temp_init', '*.pp'))) self.assertCML(air_temp_T00_cube, ['trui', 'air_temp_T00.cml']) air_temp_T24_cube = iris.load_strict(tests.get_data_path(('PP', 'trui', 'air_temp_T24', '*.T24.pp'))) self.assertCML(air_temp_T24_cube, ['trui', 'air_temp_T24.cml']) air_temp_T00_cube, air_temp_T24_cube = iris.analysis.maths.intersection_of_cubes(air_temp_T00_cube, air_temp_T24_cube) delta_cube = air_temp_T00_cube - air_temp_T24_cube self.assertCML(delta_cube, ('trui', 'air_temp_trial_diff_T00_to_T24.cml')) mean_delta_cube = delta_cube.collapsed("time", iris.analysis.MEAN) self.assertCML(mean_delta_cube, ('trui', 'mean_air_temp_trial_diff_T00_to_T24.cml'))
def test_deprecated_callbacks(self): # Tests that callback functions that return values are still supported but that warnings are generated def returns_cube(cube, field, filename): return cube def returns_no_cube(cube, field, filename): return iris.io.NO_CUBE fname = tests.get_data_path(["PP", "trui", "air_temp_init", "200812011200__qwqu12ff.initanl.pp"]) # Catch all warnings for returns_cube with warnings.catch_warnings(record=True) as generated_warnings_cube: warnings.simplefilter("always") r = iris.load(fname, callback=returns_cube) # Test that our warnings are present in the generated warnings: gen_warnings_cube = [str(x.message) for x in generated_warnings_cube] self.assertIn(iris.io.CALLBACK_DEPRECATION_MSG, gen_warnings_cube, "Callback deprecation warning message not issued.") # Catch all warnings for returns_no_cube with warnings.catch_warnings(record=True) as generated_warnings_no_cube: warnings.simplefilter("always") r = iris.load(fname, callback=returns_no_cube) # Test that our warnings are present in the generated warnings: gen_warnings_no_cube = [str(x.message) for x in generated_warnings_no_cube] self.assertIn(iris.io.CALLBACK_DEPRECATION_MSG, gen_warnings_no_cube, "Callback deprecation warning message not issued.")
def test_grib_callback(self): def grib_thing_getter(cube, field, filename): cube.add_aux_coord(iris.coords.AuxCoord(field.extra_keys['_periodStartDateTime'], long_name='random element', units='no_unit')) fname = tests.get_data_path(('GRIB', 'global_t', 'global.grib2')) cube = iris.load_strict(fname, callback=grib_thing_getter) self.assertCML(cube, ['uri_callback', 'grib_global.cml'])
def test_invalid_signature_callback(self): def invalid_callback(cube, ): # should never get here pass fname = tests.get_data_path(["PP", "trui", "air_temp_init", "200812011200__qwqu12ff.initanl.pp"]) self.assertRaises(TypeError, iris.load_strict, fname, callback=invalid_callback)
def test_load_stereographic_grid(self): # Test loading a single CF-netCDF file with a stereographic # grid_mapping. cube = iris.load_cube( tests.get_data_path(('NetCDF', 'stereographic', 'toa_brightness_temperature.nc'))) self.assertCML(cube, ('netcdf', 'netcdf_stereo.cml'))
def test_invalid_return_type_callback(self): def invalid_callback(cube, field, filename): return "Not valid to return a string" fname = tests.get_data_path(["PP", "trui", "air_temp_init", "200812011200__qwqu12ff.initanl.pp"]) with self.assertRaises(TypeError): iris.load_strict(fname, callback=invalid_callback)
def test_deferred_loading(self): # Test exercising CF-netCDF deferred loading and deferred slicing. # shape (31, 161, 320) cube = iris.load_cube(tests.get_data_path( ('NetCDF', 'global', 'xyt', 'SMALL_total_column_co2.nc'))) # Consecutive index on same dimension. self.assertCML(cube[0], ('netcdf', 'netcdf_deferred_index_0.cml')) self.assertCML(cube[0][0], ('netcdf', 'netcdf_deferred_index_1.cml')) self.assertCML(cube[0][0][0], ('netcdf', 'netcdf_deferred_index_2.cml')) # Consecutive slice on same dimension. self.assertCML(cube[0:20], ('netcdf', 'netcdf_deferred_slice_0.cml')) self.assertCML(cube[0:20][0:10], ('netcdf', 'netcdf_deferred_slice_1.cml')) self.assertCML(cube[0:20][0:10][0:5], ('netcdf', 'netcdf_deferred_slice_2.cml')) # Consecutive tuple index on same dimension. self.assertCML(cube[(0, 8, 4, 2, 14, 12), ], ('netcdf', 'netcdf_deferred_tuple_0.cml')) self.assertCML(cube[(0, 8, 4, 2, 14, 12), ][(0, 2, 4, 1), ], ('netcdf', 'netcdf_deferred_tuple_1.cml')) subcube = cube[(0, 8, 4, 2, 14, 12), ][(0, 2, 4, 1), ][(1, 3), ] self.assertCML(subcube, ('netcdf', 'netcdf_deferred_tuple_2.cml')) # Consecutive mixture on same dimension. self.assertCML(cube[0:20:2][(9, 5, 8, 0), ][3], ('netcdf', 'netcdf_deferred_mix_0.cml')) self.assertCML(cube[(2, 7, 3, 4, 5, 0, 9, 10), ][2:6][3], ('netcdf', 'netcdf_deferred_mix_0.cml')) self.assertCML(cube[0][(0, 2), (1, 3)], ('netcdf', 'netcdf_deferred_mix_1.cml'))
def test_load_tmerc_grid_and_clim_bounds(self): # Test loading a single CF-netCDF file with a transverse Mercator # grid_mapping and a time variable with climatology. cube = iris.load_cube( tests.get_data_path(('NetCDF', 'transverse_mercator', 'tmean_1910_1910.nc'))) self.assertCML(cube, ('netcdf', 'netcdf_tmerc_and_climatology.cml'))
def test_load_lcc_grid(self): # Test loading a single CF-netCDF file with Lambert conformal conic # grid mapping. cube = iris.load_cube( tests.get_data_path(('NetCDF', 'lambert_conformal', 'test_lcc.nc'))) self.assertCML(cube, ('netcdf', 'netcdf_lcc.cml'))
def test_load_rotated_xy_land(self): # Test loading single xy rotated pole CF-netCDF file. cube = iris.load_cube(tests.get_data_path( ('NetCDF', 'rotated', 'xy', 'rotPole_landAreaFraction.nc'))) # Make sure the AuxCoords have lazy data. self.assertTrue(is_lazy_data(cube.coord('latitude').core_points())) self.assertCML(cube, ('netcdf', 'netcdf_rotated_xy_land.cml'))
def test_load_rotated_xyt_precipitation(self): # Test loading single xyt rotated pole CF-netCDF file. cube = iris.load_cube( tests.get_data_path(('NetCDF', 'rotated', 'xyt', 'small_rotPole_precipitation.nc'))) self.assertCML(cube, ('netcdf', 'netcdf_rotated_xyt_precipitation.cml'))
def test_plot_tmerc(self): filename = tests.get_data_path(('NetCDF', 'transverse_mercator', 'tmean_1910_1910.nc')) self.cube = iris.load_cube(filename) iplt.pcolormesh(self.cube[0]) plt.gca().coastlines() self.check_graphic()
def test_load_file_with_captured_warnings(self): test_filename = tests.get_data_path(('NetCDF', 'testing', 'units.nc')) with warnings.catch_warnings(record=True) as filtered_warnings: with suppress_unit_warnings(): iris.load(test_filename) filtered_warnings_list = [str(w.message) for w in filtered_warnings] self.assertEqual(len(filtered_warnings_list), 0)
def test_lots_of_extra_data(self): r = pp.load(tests.get_data_path(('PP', 'cf_processing', 'HadCM2_ts_SAT_ann_18602100.b.pp'))) r = list(r) self.assertEqual(r[0].lbcode.ix, 13) self.assertEqual(r[0].lbcode.iy, 23) self.assertEqual(len(r[0].lbcode), 5) self.check_pp(r, ('PP', 'extra_data_time_series.pp.txt'))
def test_unmasked(self): tif_header = 'SMALL_total_column_co2.nc.tif_header.txt' fin = tests.get_data_path(('NetCDF', 'global', 'xyt', 'SMALL_total_column_co2.nc')) cube = iris.load_cube(fin)[0] # PIL doesn't support float64 cube.data = cube.data.astype('f4') # Ensure longitude values are continuous and monotonically increasing, # and discard the 'half cells' at the top and bottom of the UM output # by extracting a subset. east = iris.Constraint(longitude=lambda cell: cell < 180) non_edge = iris.Constraint(latitude=lambda cell: -90 < cell < 90) cube = cube.extract(east & non_edge) cube.coord('longitude').guess_bounds() cube.coord('latitude').guess_bounds() self.check_tiff(cube, tif_header) # Check again with the latitude coordinate (and the corresponding # cube.data) inverted. The output should be the same as before. coord = cube.coord('latitude') coord.points = coord.points[::-1] coord.bounds = None coord.guess_bounds() cube.data = cube.data[::-1, :] self.check_tiff(cube, tif_header)
def realistic_4d_w_missing_data(): data_path = tests.get_data_path(('stock', 'stock_mdi_arrays.npz')) if not os.path.isfile(data_path): raise IOError('Test data is not available at {}.'.format(data_path)) data_archive = np.load(data_path) data = ma.masked_array(data_archive['arr_0'], mask=data_archive['arr_1']) # sort the arrays based on the order they were originally given. # The names given are of the form 'arr_1' or 'arr_10' ll_cs = GeogCS(6371229) lat = DimCoord(np.arange(20, dtype=np.float32), standard_name='grid_latitude', units='degrees', coord_system=ll_cs) lon = DimCoord(np.arange(20, dtype=np.float32), standard_name='grid_longitude', units='degrees', coord_system=ll_cs) time = DimCoord([1000., 1003., 1006.], standard_name='time', units='hours since 1970-01-01 00:00:00') forecast_period = DimCoord([0.0, 3.0, 6.0], standard_name='forecast_period', units='hours') pressure = DimCoord(np.array([800., 900., 1000.], dtype=np.float32), long_name='pressure', units='hPa') cube = iris.cube.Cube(data, long_name='missing data test data', units='K', dim_coords_and_dims=[(time, 0), (pressure, 1), (lat, 2), (lon, 3)], aux_coords_and_dims=[(forecast_period, 0)], attributes={'source': 'Iris test case'}) return cube
def setUp(self): self.original_pp_filepath = tests.get_data_path( ("PP", "ukV1", "ukVpmslont.pp") ) self.r = list(pp.load(self.original_pp_filepath))[0:5]
def test_NAMEII_timeseries(self): cubes = iris.load( tests.get_data_path(('NAME', 'NAMEII_timeseries.txt'))) self.assertCMLApproxData(cubes, ('name', 'NAMEII_timeseries.cml'))
def test_NAMEII_trajectory(self): cubes = iris.load( tests.get_data_path(('NAME', 'NAMEIII_trajectory.txt'))) self.assertCML(cubes[0], ('name', 'NAMEIII_trajectory0.cml')) self.assertCML(cubes, ('name', 'NAMEIII_trajectory.cml'), checksum=False)
def setUp(self): self._data_path = tests.get_data_path(("PP", "globClim1", "theta.pp")) self._num_cubes = 1 self._prefix = "theta"
def setUp(self): self._data_path = tests.get_data_path( ("PP", "globClim1", "dec_subset.pp") ) self._num_cubes = 4 self._prefix = "dec"
def test_load_time_processed(self): cubes = iris.load(tests.get_data_path(('GRIB', "time_processed", "time_bound.grib2"))) self.assertCML(cubes, ("grib_load", "time_bound_grib2.cml"))
def setUp(self): self.subcubes = iris.cube.CubeList() filename = tests.get_data_path(('PP', 'aPPglob1', 'global.pp')) self.template = iris.fileformats.pp.load(filename).next()
def test_regular_gg_grib2(self): cube = iris.load_cube(tests.get_data_path( ('GRIB', 'gaussian', 'regular_gg.grib2'))) self.assertCML(cube, ('grib_load', 'regular_gg_grib2.cml'))
def test_reduced_gg(self): cube = iris.load_cube(tests.get_data_path( ("GRIB", "reduced", "reduced_gg.grib2"))) self.assertCML(cube, ("grib_load", "reduced_gg_grib2.cml"))
def test_reduced_missing(self): cube = iris.load_cube(tests.get_data_path( ("GRIB", "reduced", "reduced_ll_missing.grib1"))) self.assertCML(cube, ("grib_load", "reduced_ll_missing_grib1.cml"))
def _old_compat_load(name): cube = iris.load(tests.get_data_path(('GRIB', 'ij_directions', name)))[0] return [cube]
def _old_compat_load(name): cube = iris.load(tests.get_data_path(('GRIB', 'shape_of_earth', name)))[0] return cube
def setUp(self): self.original_pp_filepath = tests.get_data_path( ("PP", "aPPglob1", "global.pp") ) self.r = list(pp.load(self.original_pp_filepath))
def setUp(self): super(TestPlotDimAndAuxCoordsKwarg, self).setUp() filename = tests.get_data_path( ('NetCDF', 'rotated', 'xy', 'rotPole_landAreaFraction.nc')) self.cube = iris.load_cube(filename)
def test_2d_positive_up(self): path = tests.get_data_path( ('NetCDF', 'testing', 'small_theta_colpex.nc')) cube = iris.load_cube(path, 'air_potential_temperature')[0, :, 42, :] qplt.pcolormesh(cube) self.check_graphic()
def test_no_std_name(self): fname = tests.get_data_path(['PP', 'simple_pp', 'bad_global.pp']) cube = iris.load_cube(fname) self.assertCML([cube], ['cube_io', 'pp', 'no_std_name.cml'])
def setUp(self): tests.GraphicsTest.setUp(self) self.cube = iris.load_cube( tests.get_data_path(('NAME', 'NAMEIII_trajectory.txt')), 'Temperature') self.draw_method = qplt.scatter
def test_2d_positive_down(self): path = tests.get_data_path(('NetCDF', 'ORCA2', 'votemper.nc')) cube = iris.load_cube(path)[0, :, 42, :] qplt.pcolormesh(cube) self.check_graphic()
def test_polar_stereo_grib1(self): cube = iris.load_cube(tests.get_data_path( ("GRIB", "polar_stereo", "ST4.2013052210.01h"))) self.assertCML(cube, ("grib_load", "polar_stereo_grib1.cml"))
def test_1d_positive_down(self): path = tests.get_data_path(('NetCDF', 'ORCA2', 'votemper.nc')) cube = iris.load_cube(path) qplt.plot(cube[0, :, 60, 80], cube.coord('depth')) self.check_graphic()
def test_load_masked(self): gribfile = tests.get_data_path( ('GRIB', 'missing_values', 'missing_values.grib2')) cubes = iris.load(gribfile) self.assertCML(cubes, ('grib_load', 'missing_values_grib2.cml'))
def setUp(self): super(Test1dScatter, self).setUp() self.cube = iris.load_cube( tests.get_data_path(('NAME', 'NAMEIII_trajectory.txt')), 'Temperature') self.draw_method = iplt.scatter
def setUp(self): self.filename = tests.get_data_path(("PP", "aPPglob1", "global.pp"))
def test_y_fastest(self): cubes = iris.load(tests.get_data_path(("GRIB", "y_fastest", "y_fast.grib2"))) self.assertCML(cubes, ("grib_load", "y_fastest.cml"))
def test_lambert_grib2(self): cube = iris.load_cube(tests.get_data_path( ("GRIB", "lambert", "lambert.grib2"))) self.assertCML(cube, ("grib_load", "lambert_grib2.cml"))
def test_load_3_layer(self): cubes = iris.load(tests.get_data_path(('GRIB', "3_layer_viz", "3_layer.grib2"))) cubes = iris.cube.CubeList([cubes[1], cubes[0], cubes[2]]) self.assertCML(cubes, ("grib_load", "3_layer.cml"))
def test_pp_no_constraint(self): filenames = [tests.get_data_path(("PP", "globClim1", "dec_subset.pp"))] pp_constraints = pp._convert_constraints(None) pp_loader = iris.fileformats.rules.Loader(pp.load, {}, convert) cubes = list(load_cubes(filenames, None, pp_loader, pp_constraints)) self.assertEqual(len(cubes), 152)
def setUp(self): self._data_path = tests.get_data_path( ("PP", "COLPEX", "small_colpex_theta_p_alt.pp") )
def test_normal(self): paths = (tests.get_data_path(['PP', 'aPPglob1', 'global.pp']), ) cubes = iris.load_cubes(paths) self.assertEqual(len(cubes), 1)
def test_load_rotated(self): cubes = iris.load(tests.get_data_path(('GRIB', 'rotated_uk', "uk_wrongparam.grib1"))) self.assertCML(cubes, ("grib_load", "rotated.cml"))