def setUp(self, fromfile, np2str): """Create a test file handler.""" np2str.side_effect = lambda x: x m = mock.mock_open() with mock.patch('satpy.readers.ahi_hsd.open', m, create=True): # Check if file handler raises exception for invalid calibration mode with self.assertRaises(ValueError): fh = AHIHSDFileHandler(None, { 'segment_number': 8, 'total_segments': 10 }, None, calib_mode='BAD_MODE') fh = AHIHSDFileHandler(None, { 'segment_number': 8, 'total_segments': 10 }, None) fh.proj_info = { 'CFAC': 40932549, 'COFF': 5500.5, 'LFAC': 40932549, 'LOFF': 5500.5, 'blocklength': 127, 'coeff_for_sd': 1737122264.0, 'distance_from_earth_center': 42164.0, 'earth_equatorial_radius': 6378.137, 'earth_polar_radius': 6356.7523, 'hblock_number': 3, 'req2_rpol2': 1.006739501, 'req2_rpol2_req2': 0.0066943844, 'resampling_size': 4, 'resampling_types': 0, 'rpol2_req2': 0.993305616, 'spare': '', 'sub_lon': 140.7 } fh.nav_info = { 'SSP_longitude': 140.66, 'SSP_latitude': 0.03, 'distance_earth_center_to_satellite': 42165.04, 'nadir_longitude': 140.67, 'nadir_latitude': 0.04 } fh.data_info = { 'blocklength': 50, 'compression_flag_for_data': 0, 'hblock_number': 2, 'number_of_bits_per_pixel': 16, 'number_of_columns': 11000, 'number_of_lines': 1100, 'spare': '' } fh.basic_info = { 'observation_start_time': np.array([58413.12523839]), 'observation_end_time': np.array([58413.12562439]), 'observation_timeline': np.array([300]), } self.fh = fh
def setUp(self, *mocks): """Create fake data for testing.""" self.def_cali = [-0.0037, 15.20] self.upd_cali = [-0.0074, 30.40] self.bad_cali = [0.0, 0.0] fh = AHIHSDFileHandler(filetype_info={'file_type': 'hsd_b01'}) fh.calib_mode = 'NOMINAL' fh.user_calibration = None fh.is_zipped = False fh._header = { 'block5': { 'band_number': [5], 'gain_count2rad_conversion': [self.def_cali[0]], 'offset_count2rad_conversion': [self.def_cali[1]], 'central_wave_length': [10.4073], }, 'calibration': { 'coeff_rad2albedo_conversion': [0.0019255], 'speed_of_light': [299792458.0], 'planck_constant': [6.62606957e-34], 'boltzmann_constant': [1.3806488e-23], 'c0_rad2tb_conversion': [-0.116127314574], 'c1_rad2tb_conversion': [1.00099153832], 'c2_rad2tb_conversion': [-1.76961091571e-06], 'cali_gain_count2rad_conversion': [self.upd_cali[0]], 'cali_offset_count2rad_conversion': [self.upd_cali[1]] }, } self.counts = da.array(np.array([[0., 1000.], [2000., 5000.]])) self.fh = fh
def _create_fake_file_handler(in_fname, filename_info=None, filetype_info=None): if filename_info is None: filename_info = {'segment': 8, 'total_segments': 10} if filetype_info is None: filetype_info = {'file_type': 'hsd_b01'} fh = AHIHSDFileHandler(in_fname, filename_info, filetype_info) # Check that the filename is altered for bz2 format files assert in_fname != fh.filename fh.proj_info = { 'CFAC': 40932549, 'COFF': 5500.5, 'LFAC': 40932549, 'LOFF': 5500.5, 'blocklength': 127, 'coeff_for_sd': 1737122264.0, 'distance_from_earth_center': 42164.0, 'earth_equatorial_radius': 6378.137, 'earth_polar_radius': 6356.7523, 'hblock_number': 3, 'req2_rpol2': 1.006739501, 'req2_rpol2_req2': 0.0066943844, 'resampling_size': 4, 'resampling_types': 0, 'rpol2_req2': 0.993305616, 'spare': '', 'sub_lon': 140.7 } fh.nav_info = { 'SSP_longitude': 140.66, 'SSP_latitude': 0.03, 'distance_earth_center_to_satellite': 42165.04, 'nadir_longitude': 140.67, 'nadir_latitude': 0.04 } fh.data_info = { 'blocklength': 50, 'compression_flag_for_data': 0, 'hblock_number': 2, 'number_of_bits_per_pixel': 16, 'number_of_columns': 11000, 'number_of_lines': 1100, 'spare': '' } fh.basic_info = { 'observation_area': np.array(['FLDK']), 'observation_start_time': np.array([58413.12523839]), 'observation_end_time': np.array([58413.12562439]), 'observation_timeline': np.array([300]), } fh.observation_area = fh.basic_info['observation_area'] return fh
def test_region(self, fromfile, np2str): """Test region navigation.""" from pyresample.utils import proj4_radius_parameters np2str.side_effect = lambda x: x m = mock.mock_open() with mock.patch('satpy.readers.ahi_hsd.open', m, create=True): fh = AHIHSDFileHandler('somefile', { 'segment': 1, 'total_segments': 1 }, filetype_info={'file_type': 'hsd_b01'}, user_calibration=None) fh.proj_info = { 'CFAC': 40932549, 'COFF': -591.5, 'LFAC': 40932549, 'LOFF': 5132.5, 'blocklength': 127, 'coeff_for_sd': 1737122264.0, 'distance_from_earth_center': 42164.0, 'earth_equatorial_radius': 6378.137, 'earth_polar_radius': 6356.7523, 'hblock_number': 3, 'req2_rpol2': 1.006739501, 'req2_rpol2_req2': 0.0066943844, 'resampling_size': 4, 'resampling_types': 0, 'rpol2_req2': 0.993305616, 'spare': '', 'sub_lon': 140.7 } fh.data_info = { 'blocklength': 50, 'compression_flag_for_data': 0, 'hblock_number': 2, 'number_of_bits_per_pixel': 16, 'number_of_columns': 1000, 'number_of_lines': 1000, 'spare': '' } area_def = fh.get_area_def(None) proj_dict = area_def.proj_dict a, b = proj4_radius_parameters(proj_dict) self.assertEqual(a, 6378137.0) self.assertEqual(b, 6356752.3) self.assertEqual(proj_dict['h'], 35785863.0) self.assertEqual(proj_dict['lon_0'], 140.7) self.assertEqual(proj_dict['proj'], 'geos') self.assertEqual(proj_dict['units'], 'm') np.testing.assert_allclose(area_def.area_extent, (592000.0038256242, 4132000.0267018233, 1592000.0102878273, 5132000.033164027))
def test_segment(self, fromfile, np2str): """Test segment navigation.""" np2str.side_effect = lambda x: x m = mock.mock_open() with mock.patch('satpy.readers.ahi_hsd.open', m, create=True): fh = AHIHSDFileHandler(None, { 'segment_number': 8, 'total_segments': 10 }, None) fh.proj_info = { 'CFAC': 40932549, 'COFF': 5500.5, 'LFAC': 40932549, 'LOFF': 5500.5, 'blocklength': 127, 'coeff_for_sd': 1737122264.0, 'distance_from_earth_center': 42164.0, 'earth_equatorial_radius': 6378.137, 'earth_polar_radius': 6356.7523, 'hblock_number': 3, 'req2_rpol2': 1.006739501, 'req2_rpol2_req2': 0.0066943844, 'resampling_size': 4, 'resampling_types': 0, 'rpol2_req2': 0.993305616, 'spare': '', 'sub_lon': 140.7 } fh.data_info = { 'blocklength': 50, 'compression_flag_for_data': 0, 'hblock_number': 2, 'number_of_bits_per_pixel': 16, 'number_of_columns': 11000, 'number_of_lines': 1100, 'spare': '' } area_def = fh.get_area_def(None) self.assertEqual( area_def.proj_dict, { 'a': 6378137.0, 'b': 6356752.3, 'h': 35785863.0, 'lon_0': 140.7, 'proj': 'geos', 'units': 'm' }) self.assertEqual(area_def.area_extent, (-5500000.035542117, -3300000.021325271, 5500000.035542117, -2200000.0142168473))
def _create_fake_file_handler(in_fname, filename_info=None, filetype_info=None, fh_kwargs=None): if filename_info is None: filename_info = {'segment': 8, 'total_segments': 10} if filetype_info is None: filetype_info = {'file_type': 'hsd_b01'} if fh_kwargs is None: fh_kwargs = {} fh = AHIHSDFileHandler(in_fname, filename_info, filetype_info, **fh_kwargs) # Check that the filename is altered for bz2 format files assert in_fname != fh.filename return fh
def setUp(self, fromfile, np2str): """Create a test file handler.""" np2str.side_effect = lambda x: x m = mock.mock_open() with mock.patch('satpy.readers.ahi_hsd.open', m, create=True): # Check if file handler raises exception for invalid calibration mode with self.assertRaises(ValueError): AHIHSDFileHandler('somefile', {'segment': 8, 'total_segments': 10}, filetype_info={'file_type': 'hsd_b01'}, calib_mode='BAD_MODE') in_fname = 'test_file.bz2' self.fh = self._create_fake_file_handler(in_fname)
def test_calibrate(self, *mocks): """Test calibration""" fh = AHIHSDFileHandler() fh._header = { 'block5': {'gain_count2rad_conversion': [-0.0037], 'offset_count2rad_conversion': [15.20], 'central_wave_length': [10.4073], }, 'calibration': {'coeff_rad2albedo_conversion': [0.0019255], 'speed_of_light': [299792458.0], 'planck_constant': [6.62606957e-34], 'boltzmann_constant': [1.3806488e-23], 'c0_rad2tb_conversion': [-0.116127314574], 'c1_rad2tb_conversion': [1.00099153832], 'c2_rad2tb_conversion': [-1.76961091571e-06]}, } # Counts self.assertEqual(fh.calibrate(data=123, calibration='counts'), 123) # Radiance counts = da.array(np.array([[0., 1000.], [2000., 5000.]])) rad_exp = np.array([[15.2, 11.5], [7.8, 0]]) rad = fh.calibrate(data=counts, calibration='radiance') self.assertTrue(np.allclose(rad, rad_exp)) # Brightness Temperature bt_exp = np.array([[330.978979, 310.524688], [285.845017, np.nan]]) bt = fh.calibrate(data=counts, calibration='brightness_temperature') np.testing.assert_allclose(bt, bt_exp) # Reflectance refl_exp = np.array([[2.92676, 2.214325], [1.50189, 0.]]) refl = fh.calibrate(data=counts, calibration='reflectance') self.assertTrue(np.allclose(refl, refl_exp))
def test_calibrate(self, *mocks): """Test calibration.""" def_cali = [-0.0037, 15.20] upd_cali = [-0.0074, 30.40] bad_cali = [0.0, 0.0] fh = AHIHSDFileHandler() fh.calib_mode = 'NOMINAL' fh.is_zipped = False fh._header = { 'block5': { 'band_number': [5], 'gain_count2rad_conversion': [def_cali[0]], 'offset_count2rad_conversion': [def_cali[1]], 'central_wave_length': [10.4073], }, 'calibration': { 'coeff_rad2albedo_conversion': [0.0019255], 'speed_of_light': [299792458.0], 'planck_constant': [6.62606957e-34], 'boltzmann_constant': [1.3806488e-23], 'c0_rad2tb_conversion': [-0.116127314574], 'c1_rad2tb_conversion': [1.00099153832], 'c2_rad2tb_conversion': [-1.76961091571e-06], 'cali_gain_count2rad_conversion': [upd_cali[0]], 'cali_offset_count2rad_conversion': [upd_cali[1]] }, } # Counts self.assertEqual(fh.calibrate(data=123, calibration='counts'), 123) # Radiance counts = da.array(np.array([[0., 1000.], [2000., 5000.]])) rad_exp = np.array([[15.2, 11.5], [7.8, 0]]) rad = fh.calibrate(data=counts, calibration='radiance') self.assertTrue(np.allclose(rad, rad_exp)) # Brightness Temperature bt_exp = np.array([[330.978979, 310.524688], [285.845017, np.nan]]) bt = fh.calibrate(data=counts, calibration='brightness_temperature') np.testing.assert_allclose(bt, bt_exp) # Reflectance refl_exp = np.array([[2.92676, 2.214325], [1.50189, 0.]]) refl = fh.calibrate(data=counts, calibration='reflectance') self.assertTrue(np.allclose(refl, refl_exp)) # Updated calibration # Standard operation fh.calib_mode = 'UPDATE' rad_exp = np.array([[30.4, 23.0], [15.6, 0.]]) rad = fh.calibrate(data=counts, calibration='radiance') self.assertTrue(np.allclose(rad, rad_exp)) # Case for no updated calibration available (older data) fh._header = { 'block5': { 'band_number': [5], 'gain_count2rad_conversion': [def_cali[0]], 'offset_count2rad_conversion': [def_cali[1]], 'central_wave_length': [10.4073], }, 'calibration': { 'coeff_rad2albedo_conversion': [0.0019255], 'speed_of_light': [299792458.0], 'planck_constant': [6.62606957e-34], 'boltzmann_constant': [1.3806488e-23], 'c0_rad2tb_conversion': [-0.116127314574], 'c1_rad2tb_conversion': [1.00099153832], 'c2_rad2tb_conversion': [-1.76961091571e-06], 'cali_gain_count2rad_conversion': [bad_cali[0]], 'cali_offset_count2rad_conversion': [bad_cali[1]] }, } rad = fh.calibrate(data=counts, calibration='radiance') rad_exp = np.array([[15.2, 11.5], [7.8, 0]]) self.assertTrue(np.allclose(rad, rad_exp))