def test_load_area_def(self, pesa, plsa, sad, parent_load_area_def): """Test _load_area_def().""" from satpy.readers.yaml_reader import GEOSegmentYAMLReader reader = GEOSegmentYAMLReader() dataid = MagicMock() file_handlers = MagicMock() reader._load_area_def(dataid, file_handlers) pesa.assert_called_once() plsa.assert_called_once() sad.assert_called_once() parent_load_area_def.assert_not_called() # Disable padding reader._load_area_def(dataid, file_handlers, pad_data=False) parent_load_area_def.assert_called_once_with(dataid, file_handlers)
def test_get_expected_segments(self, cfh): """Test that expected segments can come from the filename.""" from satpy.readers.yaml_reader import GEOSegmentYAMLReader reader = GEOSegmentYAMLReader() fake_fh = MagicMock() fake_fh.filename_info = {} fake_fh.filetype_info = {} cfh.return_value = {'ft1': [fake_fh]} # default (1) created_fhs = reader.create_filehandlers(['fake.nc']) es = created_fhs['ft1'][0].filetype_info['expected_segments'] self.assertEqual(es, 1) # YAML defined for each file type fake_fh.filetype_info['expected_segments'] = 2 created_fhs = reader.create_filehandlers(['fake.nc']) es = created_fhs['ft1'][0].filetype_info['expected_segments'] self.assertEqual(es, 2) # defined both in the filename and the YAML metadata # YAML has priority fake_fh.filename_info = {'total_segments': 3} fake_fh.filetype_info = {'expected_segments': 2} created_fhs = reader.create_filehandlers(['fake.nc']) es = created_fhs['ft1'][0].filetype_info['expected_segments'] self.assertEqual(es, 2) # defined in the filename fake_fh.filename_info = {'total_segments': 3} fake_fh.filetype_info = {} created_fhs = reader.create_filehandlers(['fake.nc']) es = created_fhs['ft1'][0].filetype_info['expected_segments'] self.assertEqual(es, 3) # check correct FCI chunk number reading into segment fake_fh.filename_info = {'count_in_repeat_cycle': 5} created_fhs = reader.create_filehandlers(['fake.nc']) es = created_fhs['ft1'][0].filename_info['segment'] self.assertEqual(es, 5)
def test_get_expected_segments(self): """Test that expected segments can come from the filename.""" from satpy.readers.yaml_reader import GEOSegmentYAMLReader cfh = MagicMock() # Hacky: This is setting an attribute on the MagicMock *class* # not on a MagicMock instance GEOSegmentYAMLReader.__bases__[0].create_filehandlers = cfh fake_fh = MagicMock() fake_fh.filename_info = {} fake_fh.filetype_info = {} cfh.return_value = {'ft1': [fake_fh]} reader = GEOSegmentYAMLReader() # default (1) created_fhs = reader.create_filehandlers(['fake.nc']) es = created_fhs['ft1'][0].filetype_info['expected_segments'] self.assertEqual(es, 1) # YAML defined for each file type fake_fh.filetype_info['expected_segments'] = 2 created_fhs = reader.create_filehandlers(['fake.nc']) es = created_fhs['ft1'][0].filetype_info['expected_segments'] self.assertEqual(es, 2) # defined both in the filename and the YAML metadata # YAML has priority fake_fh.filename_info = {'total_segments': 3} fake_fh.filetype_info = {'expected_segments': 2} created_fhs = reader.create_filehandlers(['fake.nc']) es = created_fhs['ft1'][0].filetype_info['expected_segments'] self.assertEqual(es, 2) # defined in the filename fake_fh.filename_info = {'total_segments': 3} fake_fh.filetype_info = {} created_fhs = reader.create_filehandlers(['fake.nc']) es = created_fhs['ft1'][0].filetype_info['expected_segments'] self.assertEqual(es, 3) # undo the hacky-ness del GEOSegmentYAMLReader.__bases__[0].create_filehandlers
def test_load_dataset(self, mss, xr, parent_load_dataset, geswh): """Test _load_dataset().""" from satpy.readers.yaml_reader import GEOSegmentYAMLReader reader = GEOSegmentYAMLReader() # Projectable is None mss.return_value = [0, 0, 0, False, None] with self.assertRaises(KeyError): res = reader._load_dataset(None, None, None) # Failure is True mss.return_value = [0, 0, 0, True, 0] with self.assertRaises(KeyError): res = reader._load_dataset(None, None, None) # Setup input, and output of mocked functions counter = 9 expected_segments = 8 seg = MagicMock(dims=['y', 'x']) slice_list = expected_segments * [ seg, ] failure = False projectable = MagicMock() mss.return_value = (counter, expected_segments, slice_list, failure, projectable) empty_segment = MagicMock() xr.full_like.return_value = empty_segment concat_slices = MagicMock() xr.concat.return_value = concat_slices dataid = MagicMock() ds_info = MagicMock() file_handlers = MagicMock() # No missing segments res = reader._load_dataset(dataid, ds_info, file_handlers) self.assertTrue( res.attrs is file_handlers[0].combine_info.return_value) self.assertTrue(empty_segment not in slice_list) # One missing segment in the middle slice_list[4] = None counter = 8 mss.return_value = (counter, expected_segments, slice_list, failure, projectable) res = reader._load_dataset(dataid, ds_info, file_handlers) self.assertTrue(slice_list[4] is empty_segment) # The last segment is missing slice_list = expected_segments * [ seg, ] slice_list[-1] = None counter = 8 mss.return_value = (counter, expected_segments, slice_list, failure, projectable) res = reader._load_dataset(dataid, ds_info, file_handlers) self.assertTrue(slice_list[-1] is empty_segment) # The last two segments are missing slice_list = expected_segments * [ seg, ] slice_list[-1] = None counter = 7 mss.return_value = (counter, expected_segments, slice_list, failure, projectable) res = reader._load_dataset(dataid, ds_info, file_handlers) self.assertTrue(slice_list[-1] is empty_segment) self.assertTrue(slice_list[-2] is empty_segment) # The first segment is missing slice_list = expected_segments * [ seg, ] slice_list[0] = None counter = 9 mss.return_value = (counter, expected_segments, slice_list, failure, projectable) res = reader._load_dataset(dataid, ds_info, file_handlers) self.assertTrue(slice_list[0] is empty_segment) # The first two segments are missing slice_list = expected_segments * [ seg, ] slice_list[0] = None slice_list[1] = None counter = 9 mss.return_value = (counter, expected_segments, slice_list, failure, projectable) res = reader._load_dataset(dataid, ds_info, file_handlers) self.assertTrue(slice_list[0] is empty_segment) self.assertTrue(slice_list[1] is empty_segment) # Check that new FCI empty segment is generated if missing in the middle and at the end fake_fh = MagicMock() fake_fh.filename_info = {} fake_fh.filetype_info = {'file_type': 'fci_l1c_fdhsi'} empty_segment.shape = (140, 5568) slice_list[4] = None counter = 7 mss.return_value = (counter, expected_segments, slice_list, failure, projectable) res = reader._load_dataset(dataid, ds_info, [fake_fh]) assert 2 == geswh.call_count # Disable padding res = reader._load_dataset(dataid, ds_info, file_handlers, pad_data=False) parent_load_dataset.assert_called_once_with(dataid, ds_info, file_handlers)
def setUp(self): """Add setup for GEOSegmentYAMLReader.""" from satpy.readers.yaml_reader import GEOSegmentYAMLReader GEOSegmentYAMLReader.__bases__ = (MagicMock, ) self.reader = GEOSegmentYAMLReader()
class TestGEOSegmentYAMLReader(unittest.TestCase): """Test GEOSegmentYAMLReader.""" def setUp(self): """Add setup for GEOSegmentYAMLReader.""" from satpy.readers.yaml_reader import GEOSegmentYAMLReader GEOSegmentYAMLReader.__bases__ = (MagicMock, ) self.reader = GEOSegmentYAMLReader() def test_get_expected_segments(self): """Test that expected segments can come from the filename.""" from satpy.readers.yaml_reader import GEOSegmentYAMLReader cfh = MagicMock() # Hacky: This is setting an attribute on the MagicMock *class* # not on a MagicMock instance GEOSegmentYAMLReader.__bases__[0].create_filehandlers = cfh fake_fh = MagicMock() fake_fh.filename_info = {} fake_fh.filetype_info = {} cfh.return_value = {'ft1': [fake_fh]} reader = GEOSegmentYAMLReader() # default (1) created_fhs = reader.create_filehandlers(['fake.nc']) es = created_fhs['ft1'][0].filetype_info['expected_segments'] self.assertEqual(es, 1) # YAML defined for each file type fake_fh.filetype_info['expected_segments'] = 2 created_fhs = reader.create_filehandlers(['fake.nc']) es = created_fhs['ft1'][0].filetype_info['expected_segments'] self.assertEqual(es, 2) # defined both in the filename and the YAML metadata # YAML has priority fake_fh.filename_info = {'total_segments': 3} fake_fh.filetype_info = {'expected_segments': 2} created_fhs = reader.create_filehandlers(['fake.nc']) es = created_fhs['ft1'][0].filetype_info['expected_segments'] self.assertEqual(es, 2) # defined in the filename fake_fh.filename_info = {'total_segments': 3} fake_fh.filetype_info = {} created_fhs = reader.create_filehandlers(['fake.nc']) es = created_fhs['ft1'][0].filetype_info['expected_segments'] self.assertEqual(es, 3) # undo the hacky-ness del GEOSegmentYAMLReader.__bases__[0].create_filehandlers @patch('satpy.readers.yaml_reader.FileYAMLReader._load_dataset') @patch('satpy.readers.yaml_reader.xr') @patch('satpy.readers.yaml_reader._find_missing_segments') def test_load_dataset(self, mss, xr, parent_load_dataset): """Test _load_dataset().""" # Projectable is None mss.return_value = [0, 0, 0, False, None] with self.assertRaises(KeyError): res = self.reader._load_dataset(None, None, None) # Failure is True mss.return_value = [0, 0, 0, True, 0] with self.assertRaises(KeyError): res = self.reader._load_dataset(None, None, None) # Setup input, and output of mocked functions counter = 9 expected_segments = 8 seg = MagicMock(dims=['y', 'x']) slice_list = expected_segments * [ seg, ] failure = False projectable = MagicMock() mss.return_value = (counter, expected_segments, slice_list, failure, projectable) empty_segment = MagicMock() xr.full_like.return_value = empty_segment concat_slices = MagicMock() xr.concat.return_value = concat_slices dsid = MagicMock() ds_info = MagicMock() file_handlers = MagicMock() # No missing segments res = self.reader._load_dataset(dsid, ds_info, file_handlers) self.assertTrue( res.attrs is file_handlers[0].combine_info.return_value) self.assertTrue(empty_segment not in slice_list) # One missing segment in the middle slice_list[4] = None counter = 8 mss.return_value = (counter, expected_segments, slice_list, failure, projectable) res = self.reader._load_dataset(dsid, ds_info, file_handlers) self.assertTrue(slice_list[4] is empty_segment) # The last segment is missing slice_list = expected_segments * [ seg, ] slice_list[-1] = None counter = 8 mss.return_value = (counter, expected_segments, slice_list, failure, projectable) res = self.reader._load_dataset(dsid, ds_info, file_handlers) self.assertTrue(slice_list[-1] is empty_segment) # The last two segments are missing slice_list = expected_segments * [ seg, ] slice_list[-1] = None counter = 7 mss.return_value = (counter, expected_segments, slice_list, failure, projectable) res = self.reader._load_dataset(dsid, ds_info, file_handlers) self.assertTrue(slice_list[-1] is empty_segment) self.assertTrue(slice_list[-2] is empty_segment) # The first segment is missing slice_list = expected_segments * [ seg, ] slice_list[0] = None counter = 9 mss.return_value = (counter, expected_segments, slice_list, failure, projectable) res = self.reader._load_dataset(dsid, ds_info, file_handlers) self.assertTrue(slice_list[0] is empty_segment) # The first two segments are missing slice_list = expected_segments * [ seg, ] slice_list[0] = None slice_list[1] = None counter = 9 mss.return_value = (counter, expected_segments, slice_list, failure, projectable) res = self.reader._load_dataset(dsid, ds_info, file_handlers) self.assertTrue(slice_list[0] is empty_segment) self.assertTrue(slice_list[1] is empty_segment) # Disable padding res = self.reader._load_dataset(dsid, ds_info, file_handlers, pad_data=False) parent_load_dataset.assert_called_once_with(dsid, ds_info, file_handlers) @patch('satpy.readers.yaml_reader._load_area_def') @patch('satpy.readers.yaml_reader._stack_area_defs') @patch('satpy.readers.yaml_reader._pad_earlier_segments_area') @patch('satpy.readers.yaml_reader._pad_later_segments_area') def test_load_area_def(self, pesa, plsa, sad, parent_load_area_def): """Test _load_area_def().""" dsid = MagicMock() file_handlers = MagicMock() self.reader._load_area_def(dsid, file_handlers) pesa.assert_called_once() plsa.assert_called_once() sad.assert_called_once() parent_load_area_def.assert_not_called() # Disable padding self.reader._load_area_def(dsid, file_handlers, pad_data=False) parent_load_area_def.assert_called_once_with(dsid, file_handlers) @patch('satpy.readers.yaml_reader.AreaDefinition') def test_pad_later_segments_area(self, AreaDefinition): """Test _pad_later_segments_area().""" from satpy.readers.yaml_reader import _pad_later_segments_area as plsa seg1_area = MagicMock() seg1_area.proj_dict = 'proj_dict' seg1_area.area_extent = [0, 1000, 200, 500] seg1_area.shape = [200, 500] get_area_def = MagicMock() get_area_def.return_value = seg1_area fh_1 = MagicMock() filetype_info = {'expected_segments': 2} filename_info = {'segment': 1} fh_1.filetype_info = filetype_info fh_1.filename_info = filename_info fh_1.get_area_def = get_area_def file_handlers = [fh_1] dsid = 'dsid' res = plsa(file_handlers, dsid) self.assertEqual(len(res), 2) seg2_extent = (0, 1500, 200, 1000) expected_call = ('fill', 'fill', 'fill', 'proj_dict', 500, 200, seg2_extent) AreaDefinition.assert_called_once_with(*expected_call) @patch('satpy.readers.yaml_reader.AreaDefinition') def test_pad_earlier_segments_area(self, AreaDefinition): """Test _pad_earlier_segments_area().""" from satpy.readers.yaml_reader import _pad_earlier_segments_area as pesa seg2_area = MagicMock() seg2_area.proj_dict = 'proj_dict' seg2_area.area_extent = [0, 1000, 200, 500] seg2_area.shape = [200, 500] get_area_def = MagicMock() get_area_def.return_value = seg2_area fh_2 = MagicMock() filetype_info = {'expected_segments': 2} filename_info = {'segment': 2} fh_2.filetype_info = filetype_info fh_2.filename_info = filename_info fh_2.get_area_def = get_area_def file_handlers = [fh_2] dsid = 'dsid' area_defs = {2: seg2_area} res = pesa(file_handlers, dsid, area_defs) self.assertEqual(len(res), 2) seg1_extent = (0, 500, 200, 0) expected_call = ('fill', 'fill', 'fill', 'proj_dict', 500, 200, seg1_extent) AreaDefinition.assert_called_once_with(*expected_call) def test_find_missing_segments(self): """Test _find_missing_segments().""" from satpy.readers.yaml_reader import _find_missing_segments as fms # Dataset with only one segment filename_info = {'segment': 1} fh_seg1 = MagicMock(filename_info=filename_info) projectable = 'projectable' get_dataset = MagicMock() get_dataset.return_value = projectable fh_seg1.get_dataset = get_dataset file_handlers = [fh_seg1] ds_info = {'file_type': []} dsid = 'dsid' res = fms(file_handlers, ds_info, dsid) counter, expected_segments, slice_list, failure, proj = res self.assertEqual(counter, 2) self.assertEqual(expected_segments, 1) self.assertTrue(projectable in slice_list) self.assertFalse(failure) self.assertTrue(proj is projectable) # Three expected segments, first and last missing filename_info = {'segment': 2} filetype_info = {'expected_segments': 3, 'file_type': 'foo'} fh_seg2 = MagicMock(filename_info=filename_info, filetype_info=filetype_info) projectable = 'projectable' get_dataset = MagicMock() get_dataset.return_value = projectable fh_seg2.get_dataset = get_dataset file_handlers = [fh_seg2] ds_info = {'file_type': ['foo']} dsid = 'dsid' res = fms(file_handlers, ds_info, dsid) counter, expected_segments, slice_list, failure, proj = res self.assertEqual(counter, 3) self.assertEqual(expected_segments, 3) self.assertEqual(slice_list, [None, projectable, None]) self.assertFalse(failure) self.assertTrue(proj is projectable)