def __init__(self, filename, filename_info, filetype_info, **kwargs): """Initialize the base reader.""" BaseFileHandler.__init__(self, filename, filename_info, filetype_info) try: self.sd = SD(self.filename) except HDF4Error as err: error_message = "Could not load data from file {}: {}".format( self.filename, err) raise ValueError(error_message) self.metadata = self._load_all_metadata_attributes()
def test_file_is_kept_intact(self): """Test that the file object passed (string, path, or other) is kept intact.""" open_file = mock.MagicMock() bfh = BaseFileHandler(open_file, {'filename_info': 'bla'}, 'filetype_info') assert bfh.filename == open_file from pathlib import Path filename = Path('/bla/bla.nc') bfh = BaseFileHandler(filename, {'filename_info': 'bla'}, 'filetype_info') assert isinstance(bfh.filename, Path)
def __init__(self, filename, filename_info, filetype_info): BaseFileHandler.__init__(self, filename, filename_info, filetype_info) try: self.sd = SD(self.filename) except HDF4Error as err: error_message = "Could not load data from file {}: {}".format( self.filename, err) raise ValueError(error_message) # Read metadata self.metadata = self.read_mda(self.sd.attributes()['CoreMetadata.0']) self.metadata.update( self.read_mda(self.sd.attributes()['StructMetadata.0'])) self.metadata.update( self.read_mda(self.sd.attributes()['ArchiveMetadata.0']))
def setUp(self): """Setup the test.""" self._old_set = BaseFileHandler.__abstractmethods__ BaseFileHandler._abstractmethods__ = set() self.fh = BaseFileHandler( 'filename', {'filename_info': 'bla'}, 'filetype_info')
class TestBaseFileHandler(unittest.TestCase): """Test the BaseFileHandler.""" def setUp(self): """Setup the test.""" self._old_set = BaseFileHandler.__abstractmethods__ BaseFileHandler._abstractmethods__ = set() self.fh = BaseFileHandler( 'filename', {'filename_info': 'bla'}, 'filetype_info') def test_combine_times(self): """Combine times.""" info1 = {'start_time': 1} info2 = {'start_time': 2} res = self.fh.combine_info([info1, info2]) exp = {'start_time': 1} self.assertDictEqual(res, exp) res = self.fh.combine_info([info2, info1]) exp = {'start_time': 1} self.assertDictEqual(res, exp) info1 = {'end_time': 1} info2 = {'end_time': 2} res = self.fh.combine_info([info1, info2]) exp = {'end_time': 2} self.assertDictEqual(res, exp) res = self.fh.combine_info([info2, info1]) exp = {'end_time': 2} self.assertDictEqual(res, exp) def test_combine_orbits(self): """Combine orbits.""" info1 = {'start_orbit': 1} info2 = {'start_orbit': 2} res = self.fh.combine_info([info1, info2]) exp = {'start_orbit': 1} self.assertDictEqual(res, exp) res = self.fh.combine_info([info2, info1]) exp = {'start_orbit': 1} self.assertDictEqual(res, exp) info1 = {'end_orbit': 1} info2 = {'end_orbit': 2} res = self.fh.combine_info([info1, info2]) exp = {'end_orbit': 2} self.assertDictEqual(res, exp) res = self.fh.combine_info([info2, info1]) exp = {'end_orbit': 2} self.assertDictEqual(res, exp) @mock.patch('satpy.readers.file_handlers.SwathDefinition') def test_combine_area(self, sdef): """Combine area.""" area1 = mock.MagicMock() area1.lons = np.arange(5) area1.lats = np.arange(5) area1.name = 'area1' area2 = mock.MagicMock() area2.lons = np.arange(5) area2.lats = np.arange(5) area2.name = 'area2' info1 = {'area': area1} info2 = {'area': area2} self.fh.combine_info([info1, info2]) self.assertTupleEqual(sdef.call_args[1]['lons'].shape, (2, 5)) self.assertTupleEqual(sdef.call_args[1]['lats'].shape, (2, 5)) self.assertEqual(sdef.return_value.name, 'area1_area2') def tearDown(self): """Tear down the test.""" BaseFileHandler.__abstractmethods__ = self._old_set
def setUp(self): """Set up the test.""" self._old_set = BaseFileHandler.__abstractmethods__ BaseFileHandler._abstractmethods__ = set() self.fh = BaseFileHandler('filename', {'filename_info': 'bla'}, 'filetype_info')
class TestBaseFileHandler(unittest.TestCase): """Test the BaseFileHandler.""" def setUp(self): """Set up the test.""" self._old_set = BaseFileHandler.__abstractmethods__ BaseFileHandler._abstractmethods__ = set() self.fh = BaseFileHandler('filename', {'filename_info': 'bla'}, 'filetype_info') def test_combine_times(self): """Combine times.""" info1 = {'start_time': 1} info2 = {'start_time': 2} res = self.fh.combine_info([info1, info2]) exp = {'start_time': 1} self.assertDictEqual(res, exp) res = self.fh.combine_info([info2, info1]) exp = {'start_time': 1} self.assertDictEqual(res, exp) info1 = {'end_time': 1} info2 = {'end_time': 2} res = self.fh.combine_info([info1, info2]) exp = {'end_time': 2} self.assertDictEqual(res, exp) res = self.fh.combine_info([info2, info1]) exp = {'end_time': 2} self.assertDictEqual(res, exp) def test_combine_orbits(self): """Combine orbits.""" info1 = {'start_orbit': 1} info2 = {'start_orbit': 2} res = self.fh.combine_info([info1, info2]) exp = {'start_orbit': 1} self.assertDictEqual(res, exp) res = self.fh.combine_info([info2, info1]) exp = {'start_orbit': 1} self.assertDictEqual(res, exp) info1 = {'end_orbit': 1} info2 = {'end_orbit': 2} res = self.fh.combine_info([info1, info2]) exp = {'end_orbit': 2} self.assertDictEqual(res, exp) res = self.fh.combine_info([info2, info1]) exp = {'end_orbit': 2} self.assertDictEqual(res, exp) @mock.patch('satpy.readers.file_handlers.SwathDefinition') def test_combine_area(self, sdef): """Combine area.""" area1 = mock.MagicMock() area1.lons = np.arange(5) area1.lats = np.arange(5) area1.name = 'area1' area2 = mock.MagicMock() area2.lons = np.arange(5) area2.lats = np.arange(5) area2.name = 'area2' info1 = {'area': area1} info2 = {'area': area2} self.fh.combine_info([info1, info2]) self.assertTupleEqual(sdef.call_args[1]['lons'].shape, (2, 5)) self.assertTupleEqual(sdef.call_args[1]['lats'].shape, (2, 5)) self.assertEqual(sdef.return_value.name, 'area1_area2') def test_combine_orbital_parameters(self): """Combine orbital parameters.""" info1 = { 'orbital_parameters': { 'projection_longitude': 1, 'projection_latitude': 1, 'projection_altitude': 1, 'satellite_nominal_longitude': 1, 'satellite_nominal_latitude': 1, 'satellite_actual_longitude': 1, 'satellite_actual_latitude': 1, 'satellite_actual_altitude': 1, 'nadir_longitude': 1, 'nadir_latitude': 1, 'only_in_1': False } } info2 = { 'orbital_parameters': { 'projection_longitude': 2, 'projection_latitude': 2, 'projection_altitude': 2, 'satellite_nominal_longitude': 2, 'satellite_nominal_latitude': 2, 'satellite_actual_longitude': 2, 'satellite_actual_latitude': 2, 'satellite_actual_altitude': 2, 'nadir_longitude': 2, 'nadir_latitude': 2, 'only_in_2': True } } exp = { 'orbital_parameters': { 'projection_longitude': 1.5, 'projection_latitude': 1.5, 'projection_altitude': 1.5, 'satellite_nominal_longitude': 1.5, 'satellite_nominal_latitude': 1.5, 'satellite_actual_longitude': 1.5, 'satellite_actual_latitude': 1.5, 'satellite_actual_altitude': 1.5, 'nadir_longitude': 1.5, 'nadir_latitude': 1.5, 'only_in_1': False, 'only_in_2': True } } res = self.fh.combine_info([info1, info2]) self.assertDictEqual(res, exp) # Identity self.assertEqual(self.fh.combine_info([info1]), info1) # Empty self.fh.combine_info([{}]) def test_combine_time_parameters(self): """Combine times in 'time_parameters.""" time_params1 = { 'nominal_start_time': datetime(2020, 1, 1, 12, 0, 0), 'nominal_end_time': datetime(2020, 1, 1, 12, 2, 30), 'observation_start_time': datetime(2020, 1, 1, 12, 0, 2, 23821), 'observation_end_time': datetime(2020, 1, 1, 12, 2, 23, 12348), } time_params2 = {} time_shift = timedelta(seconds=1.5) for key, value in time_params1.items(): time_params2[key] = value + time_shift res = self.fh.combine_info([{ 'time_parameters': time_params1 }, { 'time_parameters': time_params2 }]) res_time_params = res['time_parameters'] assert res_time_params['nominal_start_time'] == datetime( 2020, 1, 1, 12, 0, 0) assert res_time_params['nominal_end_time'] == datetime( 2020, 1, 1, 12, 2, 31, 500000) assert res_time_params['observation_start_time'] == datetime( 2020, 1, 1, 12, 0, 2, 23821) assert res_time_params['observation_end_time'] == datetime( 2020, 1, 1, 12, 2, 24, 512348) def test_file_is_kept_intact(self): """Test that the file object passed (string, path, or other) is kept intact.""" open_file = mock.MagicMock() bfh = BaseFileHandler(open_file, {'filename_info': 'bla'}, 'filetype_info') assert bfh.filename == open_file from pathlib import Path filename = Path('/bla/bla.nc') bfh = BaseFileHandler(filename, {'filename_info': 'bla'}, 'filetype_info') assert isinstance(bfh.filename, Path) def tearDown(self): """Tear down the test.""" BaseFileHandler.__abstractmethods__ = self._old_set
class TestBaseFileHandler(unittest.TestCase): """Test the BaseFileHandler.""" def setUp(self): """Setup the test.""" self._old_set = BaseFileHandler.__abstractmethods__ BaseFileHandler._abstractmethods__ = set() self.fh = BaseFileHandler('filename', {'filename_info': 'bla'}, 'filetype_info') def test_combine_times(self): """Combine times.""" info1 = {'start_time': 1} info2 = {'start_time': 2} res = self.fh.combine_info([info1, info2]) exp = {'start_time': 1} self.assertDictEqual(res, exp) res = self.fh.combine_info([info2, info1]) exp = {'start_time': 1} self.assertDictEqual(res, exp) info1 = {'end_time': 1} info2 = {'end_time': 2} res = self.fh.combine_info([info1, info2]) exp = {'end_time': 2} self.assertDictEqual(res, exp) res = self.fh.combine_info([info2, info1]) exp = {'end_time': 2} self.assertDictEqual(res, exp) def test_combine_orbits(self): """Combine orbits.""" info1 = {'start_orbit': 1} info2 = {'start_orbit': 2} res = self.fh.combine_info([info1, info2]) exp = {'start_orbit': 1} self.assertDictEqual(res, exp) res = self.fh.combine_info([info2, info1]) exp = {'start_orbit': 1} self.assertDictEqual(res, exp) info1 = {'end_orbit': 1} info2 = {'end_orbit': 2} res = self.fh.combine_info([info1, info2]) exp = {'end_orbit': 2} self.assertDictEqual(res, exp) res = self.fh.combine_info([info2, info1]) exp = {'end_orbit': 2} self.assertDictEqual(res, exp) @mock.patch('satpy.readers.file_handlers.SwathDefinition') def test_combine_area(self, sdef): """Combine area.""" area1 = mock.MagicMock() area1.lons = np.arange(5) area1.lats = np.arange(5) area1.name = 'area1' area2 = mock.MagicMock() area2.lons = np.arange(5) area2.lats = np.arange(5) area2.name = 'area2' info1 = {'area': area1} info2 = {'area': area2} self.fh.combine_info([info1, info2]) self.assertTupleEqual(sdef.call_args[1]['lons'].shape, (2, 5)) self.assertTupleEqual(sdef.call_args[1]['lats'].shape, (2, 5)) self.assertEqual(sdef.return_value.name, 'area1_area2') def tearDown(self): """Tear down the test.""" BaseFileHandler.__abstractmethods__ = self._old_set
class TestBaseFileHandler(unittest.TestCase): """Test the BaseFileHandler.""" def setUp(self): """Setup the test.""" self._old_set = BaseFileHandler.__abstractmethods__ BaseFileHandler._abstractmethods__ = set() self.fh = BaseFileHandler('filename', {'filename_info': 'bla'}, 'filetype_info') def test_combine_times(self): """Combine times.""" info1 = {'start_time': 1} info2 = {'start_time': 2} res = self.fh.combine_info([info1, info2]) exp = {'start_time': 1} self.assertDictEqual(res, exp) res = self.fh.combine_info([info2, info1]) exp = {'start_time': 1} self.assertDictEqual(res, exp) info1 = {'end_time': 1} info2 = {'end_time': 2} res = self.fh.combine_info([info1, info2]) exp = {'end_time': 2} self.assertDictEqual(res, exp) res = self.fh.combine_info([info2, info1]) exp = {'end_time': 2} self.assertDictEqual(res, exp) def test_combine_orbits(self): """Combine orbits.""" info1 = {'start_orbit': 1} info2 = {'start_orbit': 2} res = self.fh.combine_info([info1, info2]) exp = {'start_orbit': 1} self.assertDictEqual(res, exp) res = self.fh.combine_info([info2, info1]) exp = {'start_orbit': 1} self.assertDictEqual(res, exp) info1 = {'end_orbit': 1} info2 = {'end_orbit': 2} res = self.fh.combine_info([info1, info2]) exp = {'end_orbit': 2} self.assertDictEqual(res, exp) res = self.fh.combine_info([info2, info1]) exp = {'end_orbit': 2} self.assertDictEqual(res, exp) @mock.patch('satpy.readers.file_handlers.SwathDefinition') def test_combine_area(self, sdef): """Combine area.""" area1 = mock.MagicMock() area1.lons = np.arange(5) area1.lats = np.arange(5) area1.name = 'area1' area2 = mock.MagicMock() area2.lons = np.arange(5) area2.lats = np.arange(5) area2.name = 'area2' info1 = {'area': area1} info2 = {'area': area2} self.fh.combine_info([info1, info2]) self.assertTupleEqual(sdef.call_args[1]['lons'].shape, (2, 5)) self.assertTupleEqual(sdef.call_args[1]['lats'].shape, (2, 5)) self.assertEqual(sdef.return_value.name, 'area1_area2') def test_combine_orbital_parameters(self): """Combine orbital parameters.""" info1 = { 'orbital_parameters': { 'projection_longitude': 1, 'projection_latitude': 1, 'projection_altitude': 1, 'satellite_nominal_longitude': 1, 'satellite_nominal_latitude': 1, 'satellite_actual_longitude': 1, 'satellite_actual_latitude': 1, 'satellite_actual_altitude': 1, 'nadir_longitude': 1, 'nadir_latitude': 1, 'only_in_1': False } } info2 = { 'orbital_parameters': { 'projection_longitude': 2, 'projection_latitude': 2, 'projection_altitude': 2, 'satellite_nominal_longitude': 2, 'satellite_nominal_latitude': 2, 'satellite_actual_longitude': 2, 'satellite_actual_latitude': 2, 'satellite_actual_altitude': 2, 'nadir_longitude': 2, 'nadir_latitude': 2, 'only_in_2': True } } exp = { 'orbital_parameters': { 'projection_longitude': 1.5, 'projection_latitude': 1.5, 'projection_altitude': 1.5, 'satellite_nominal_longitude': 1.5, 'satellite_nominal_latitude': 1.5, 'satellite_actual_longitude': 1.5, 'satellite_actual_latitude': 1.5, 'satellite_actual_altitude': 1.5, 'nadir_longitude': 1.5, 'nadir_latitude': 1.5, 'only_in_1': False, 'only_in_2': True } } res = self.fh.combine_info([info1, info2]) self.assertDictEqual(res, exp) # Identity self.assertEqual(self.fh.combine_info([info1]), info1) # Empty self.fh.combine_info([{}]) def tearDown(self): """Tear down the test.""" BaseFileHandler.__abstractmethods__ = self._old_set