def get_fake_file_handler(start_time, nlines, ncols, projection_longitude=0, orbit_polynomials=ORBIT_POLYNOMIALS): """Create a mocked SEVIRI HRIT file handler.""" prologue = get_fake_prologue(projection_longitude, orbit_polynomials) mda = get_fake_mda(nlines=nlines, ncols=ncols, start_time=start_time) filename_info = get_fake_filename_info(start_time) epilogue = get_fake_epilogue() m = mock.mock_open() with mock.patch('satpy.readers.seviri_l1b_hrit.np.fromfile') as fromfile, \ mock.patch('satpy.readers.hrit_base.open', m, create=True) as newopen, \ mock.patch('satpy.readers.seviri_l1b_hrit.CHANNEL_NAMES'), \ mock.patch.object(HRITMSGFileHandler, '_get_hd', new=new_get_hd), \ mock.patch.object(HRITMSGPrologueFileHandler, 'read_prologue', new=get_new_read_prologue(prologue)): fromfile.return_value = np.array([(1, 2)], dtype=[('total_header_length', int), ('hdr_id', int)]) newopen.return_value.__enter__.return_value.tell.return_value = 1 prologue = HRITMSGPrologueFileHandler( filename='dummy_prologue_filename', filename_info=filename_info, filetype_info={}) epilogue = mock.MagicMock(epilogue=epilogue) reader = HRITMSGFileHandler('filename', filename_info, {'filetype': 'info'}, prologue, epilogue) reader.mda.update(mda) return reader
def setUp(self, fromfile): """Set up the hrit file handler for testing.""" m = mock.mock_open() fromfile.return_value = np.array([(1, 2)], dtype=[('total_header_length', int), ('hdr_id', int)]) with mock.patch('satpy.readers.hrit_base.open', m, create=True) as newopen: with mock.patch('satpy.readers.seviri_l1b_hrit.CHANNEL_NAMES'): with mock.patch.object(HRITMSGFileHandler, '_get_hd', new=new_get_hd): newopen.return_value.__enter__.return_value.tell.return_value = 1 prologue = mock.MagicMock() prologue.prologue = {"SatelliteStatus": {"SatelliteDefinition": {"SatelliteId": 324, "NominalLongitude": 47}}, 'GeometricProcessing': {'EarthModel': {'TypeOfEarthModel': 2, 'NorthPolarRadius': 10, 'SouthPolarRadius': 10, 'EquatorialRadius': 10}}, 'ImageDescription': {'ProjectionDescription': {'LongitudeOfSSP': 0.0}, 'Level15ImageProduction': {'ImageProcDirection': 1}}} prologue.get_satpos.return_value = None, None, None prologue.get_earth_radii.return_value = None, None self.reader = HRITMSGFileHandler( 'filename', {'platform_shortname': 'MSG3', 'start_time': datetime(2016, 3, 3, 0, 0), 'service': 'MSG'}, {'filetype': 'info'}, prologue, mock.MagicMock()) ncols = 3712 nlines = 464 nbits = 10 self.reader.mda['number_of_bits_per_pixel'] = nbits self.reader.mda['number_of_lines'] = nlines self.reader.mda['number_of_columns'] = ncols self.reader.mda['data_field_length'] = nlines * ncols * nbits self.reader.mda['cfac'] = 5 self.reader.mda['lfac'] = 5 self.reader.mda['coff'] = 10 self.reader.mda['loff'] = 10 self.reader.mda['projection_parameters'] = {} self.reader.mda['projection_parameters']['a'] = 6378169.0 self.reader.mda['projection_parameters']['b'] = 6356583.8 self.reader.mda['projection_parameters']['h'] = 35785831.0 self.reader.mda['projection_parameters']['SSP_longitude'] = 44 self.reader.mda['projection_parameters']['SSP_latitude'] = 0.0 self.reader.mda['orbital_parameters'] = {} self.reader.mda['orbital_parameters']['satellite_nominal_longitude'] = 47 self.reader.mda['orbital_parameters']['satellite_nominal_latitude'] = 0.0 self.reader.mda['orbital_parameters']['satellite_actual_longitude'] = 47.5 self.reader.mda['orbital_parameters']['satellite_actual_latitude'] = -0.5 self.reader.mda['orbital_parameters']['satellite_actual_altitude'] = 35783328 tline = np.zeros(nlines, dtype=[('days', '>u2'), ('milliseconds', '>u4')]) tline['days'][1:-1] = 21246 * np.ones(nlines-2) # 2016-03-03 tline['milliseconds'][1:-1] = np.arange(nlines-2) self.reader.mda['image_segment_line_quality'] = {'line_mean_acquisition': tline}
def file_handler(self): """Create a mocked file handler.""" prolog = { 'RadiometricProcessing': { 'Level15ImageCalibration': { 'CalSlope': self.gains_nominal, 'CalOffset': self.offsets_nominal, }, 'MPEFCalFeedback': { 'GSICSCalCoeff': self.gains_gsics, 'GSICSOffsetCount': self.offsets_gsics, } }, 'ImageDescription': { 'Level15ImageProduction': { 'PlannedChanProcessing': self.radiance_types } } } epilog = { 'ImageProductionStats': { 'ActualScanningSummary': { 'ForwardScanStart': self.scan_time } } } mda = { 'image_segment_line_quality': { 'line_validity': np.zeros(2), 'line_radiometric_quality': np.zeros(2), 'line_geometric_quality': np.zeros(2) }, } with mock.patch( 'satpy.readers.seviri_l1b_hrit.HRITMSGFileHandler.__init__', return_value=None ): fh = HRITMSGFileHandler() fh.platform_id = self.platform_id fh.mda = mda fh.prologue = prolog fh.epilogue = epilog return fh
def setUp(self, fromfile): """Setup the hrit file handler for testing.""" m = mock.mock_open() fromfile.return_value = np.array([(1, 2)], dtype=[('total_header_length', int), ('hdr_id', int)]) with mock.patch('satpy.readers.hrit_base.open', m, create=True) as newopen: with mock.patch('satpy.readers.seviri_l1b_hrit.CHANNEL_NAMES'): with mock.patch.object(HRITMSGFileHandler, '_get_hd', new=new_get_hd): newopen.return_value.__enter__.return_value.tell.return_value = 1 prologue = mock.MagicMock() prologue.prologue = {"SatelliteStatus": {"SatelliteDefinition": {"SatelliteId": 324}}, 'GeometricProcessing': {'EarthModel': {'TypeOfEarthModel': 2, 'NorthPolarRadius': 10, 'SouthPolarRadius': 10, 'EquatorialRadius': 10}}, 'ImageDescription': {'ProjectionDescription': {'LongitudeOfSSP': 0.0}}} self.reader = HRITMSGFileHandler( 'filename', {'platform_shortname': 'MSG3', 'start_time': datetime(2016, 3, 3, 0, 0), 'service': 'MSG'}, {'filetype': 'info'}, prologue, mock.MagicMock()) ncols = 3712 nlines = 464 nbits = 10 self.reader.mda['number_of_bits_per_pixel'] = nbits self.reader.mda['number_of_lines'] = nlines self.reader.mda['number_of_columns'] = ncols self.reader.mda['data_field_length'] = nlines * ncols * nbits self.reader.mda['cfac'] = 5 self.reader.mda['lfac'] = 5 self.reader.mda['coff'] = 10 self.reader.mda['loff'] = 10 self.reader.mda['projection_parameters'] = {} self.reader.mda['projection_parameters']['a'] = 6378169.0 self.reader.mda['projection_parameters']['b'] = 6356583.8 self.reader.mda['projection_parameters']['h'] = 35785831.0 self.reader.mda['projection_parameters']['SSP_longitude'] = 44
def test_calibrate(self, _convert_to_radiance, get_header, *mocks): """Test selection of calibration coefficients""" shp = (10, 10) counts = xr.DataArray(np.zeros(shp)) nominal_gain = np.arange(1, 13) nominal_offset = np.arange(-1, -13, -1) gsics_gain = np.arange(0.1, 1.3, 0.1) gsics_offset = np.arange(-0.1, -1.3, -0.1) # Mock prologue & epilogue pro = mock.MagicMock( prologue={ 'RadiometricProcessing': { 'Level15ImageCalibration': { 'CalSlope': nominal_gain, 'CalOffset': nominal_offset }, 'MPEFCalFeedback': { 'GSICSCalCoeff': gsics_gain, 'GSICSOffsetCount': gsics_offset } } }) epi = mock.MagicMock(epilogue=None) # Mock header readout mda = { 'image_segment_line_quality': { 'line_validity': np.zeros(shp[0]), 'line_radiometric_quality': np.zeros(shp[0]), 'line_geometric_quality': np.zeros(shp[0]) } } def get_header_patched(self): self.mda = mda get_header.side_effect = get_header_patched # Test selection of calibration coefficients # # a) Default: Nominal calibration reader = HRITMSGFileHandler(filename=None, filename_info=None, filetype_info=None, prologue=pro, epilogue=epi) for ch_id, ch_name in CHANNEL_NAMES.items(): reader.channel_name = ch_name reader.mda['spectral_channel_id'] = ch_id reader.calibrate(data=counts, calibration='radiance') _convert_to_radiance.assert_called_with(mock.ANY, nominal_gain[ch_id - 1], nominal_offset[ch_id - 1]) # b) GSICS calibration for IR channels, nominal calibration for VIS channels reader = HRITMSGFileHandler(filename=None, filename_info=None, filetype_info=None, prologue=pro, epilogue=epi, calib_mode='GSICS') for ch_id, ch_name in CHANNEL_NAMES.items(): if ch_name in VIS_CHANNELS: gain, offset = nominal_gain[ch_id - 1], nominal_offset[ch_id - 1] else: gain, offset = gsics_gain[ch_id - 1], gsics_offset[ch_id - 1] reader.channel_name = ch_name reader.mda['spectral_channel_id'] = ch_id reader.calibrate(data=counts, calibration='radiance') _convert_to_radiance.assert_called_with(mock.ANY, gain, offset) # c) External calibration coefficients for selected channels, GSICS coefs for remaining # IR channels, nominal coefs for remaining VIS channels coefs = { 'VIS006': { 'gain': 1.234, 'offset': -0.1 }, 'IR_108': { 'gain': 2.345, 'offset': -0.2 } } reader = HRITMSGFileHandler(filename=None, filename_info=None, filetype_info=None, prologue=pro, epilogue=epi, ext_calib_coefs=coefs, calib_mode='GSICS') for ch_id, ch_name in CHANNEL_NAMES.items(): if ch_name in coefs.keys(): gain, offset = coefs[ch_name]['gain'], coefs[ch_name]['offset'] elif ch_name not in VIS_CHANNELS: gain, offset = gsics_gain[ch_id - 1], gsics_offset[ch_id - 1] else: gain, offset = nominal_gain[ch_id - 1], nominal_offset[ch_id - 1] reader.channel_name = ch_name reader.mda['spectral_channel_id'] = ch_id reader.calibrate(data=counts, calibration='radiance') _convert_to_radiance.assert_called_with(mock.ANY, gain, offset) # d) Invalid mode self.assertRaises(ValueError, HRITMSGFileHandler, filename=None, filename_info=None, filetype_info=None, prologue=pro, epilogue=epi, calib_mode='invalid')