def mock_for_extract_field(self, fields, x=None, y=None): """ A context manager to ensure FF2PP._extract_field gets a field instance looking like the next one in the "fields" iterable from the "make_pp_field" call. """ with mock.patch("iris.fileformats._ff.FFHeader"): ff2pp = ff.FF2PP("mock") ff2pp._ff_header.lookup_table = [0, 0, len(fields)] # Fake level constants, with shape specifying just one model-level. ff2pp._ff_header.level_dependent_constants = np.zeros(1) grid = mock.Mock() grid.vectors = mock.Mock(return_value=(x, y)) ff2pp._ff_header.grid = mock.Mock(return_value=grid) open_func = "builtins.open" with mock.patch( "iris.fileformats._ff._parse_binary_stream", return_value=[0] ), mock.patch(open_func), mock.patch( "struct.unpack_from", return_value=[4] ), mock.patch( "iris.fileformats.pp.make_pp_field", side_effect=fields ), mock.patch( "iris.fileformats._ff.FF2PP._payload", return_value=(0, 0) ): yield ff2pp
def mock_for_extract_field(self, fields, x=None, y=None): """ A context manager to ensure FF2PP._extract_field gets a field instance looking like the next one in the "fields" iterable from the "make_pp_field" call. """ with mock.patch('iris.fileformats._ff.FFHeader'): ff2pp = ff.FF2PP('mock') ff2pp._ff_header.lookup_table = [0, 0, len(fields)] # Fake level constants, with shape specifying just one model-level. ff2pp._ff_header.level_dependent_constants = np.zeros(1) grid = mock.Mock() grid.vectors = mock.Mock(return_value=(x, y)) ff2pp._ff_header.grid = mock.Mock(return_value=grid) if six.PY3: open_func = 'builtins.open' else: open_func = '__builtin__.open' with mock.patch('numpy.fromfile', return_value=[0]), \ mock.patch(open_func), \ mock.patch('struct.unpack_from', return_value=[4]), \ mock.patch('iris.fileformats.pp.make_pp_field', side_effect=fields), \ mock.patch('iris.fileformats._ff.FF2PP._payload', return_value=(0, 0)): yield ff2pp
def test_call_structure(self, _FFHeader): # Check that the iter method calls the two necessary utility # functions extract_result = mock.Mock() interpret_patch = mock.patch('iris.fileformats.pp._interpret_fields', autospec=True, return_value=iter([])) extract_patch = mock.patch('iris.fileformats._ff.FF2PP._extract_field', autospec=True, return_value=extract_result) FF2PP_instance = ff.FF2PP('mock') with interpret_patch as interpret, extract_patch as extract: list(iter(FF2PP_instance)) interpret.assert_called_once_with(extract_result) extract.assert_called_once_with(FF2PP_instance)
def setUp(self): self.filename = tests.get_data_path(("FF", "n48_multi_field")) self.ff2pp = ff.FF2PP(self.filename) self.ff_header = self.ff2pp._ff_header data_shape = (73, 96) delta = np.sin(np.linspace(0, np.pi * 5, data_shape[1])) * 5 lons = np.linspace(0, 180, data_shape[1]) + delta lons = np.vstack([lons[:-1], lons[:-1] + 0.5 * np.diff(lons)]).T lons = np.reshape(lons, lons.shape, order="F") delta = np.sin(np.linspace(0, np.pi * 5, data_shape[0])) * 5 lats = np.linspace(-90, 90, data_shape[0]) + delta lats = np.vstack([lats[:-1], lats[:-1] + 0.5 * np.diff(lats)]).T lats = np.reshape(lats, lats.shape, order="F") self.ff_header.column_dependent_constants = lons self.ff_header.row_dependent_constants = lats self.U_grid_x = lons[:-1, 1] self.V_grid_y = lats[:-1, 1] self.P_grid_x = lons[:, 0] self.P_grid_y = lats[:, 0] self.orig_make_pp_field = pp.make_pp_field def new_make_pp_field(header): field = self.orig_make_pp_field(header) field.stash = self.ff2pp._custom_stash field.bdx = field.bdy = field.bmdi return field # Replace the pp module function with this new function; # this gets called in PP2FF. pp.make_pp_field = new_make_pp_field