def setUp(self, xr_): """Set up the tests.""" from satpy.readers.goes_imager_nc import CALIB_COEFS, GOESNCBaseFileHandler self.coefs = CALIB_COEFS['GOES-15'] # Mock file access to return a fake dataset. self.time = datetime.datetime(2018, 8, 16, 16, 7) self.dummy3d = np.zeros((1, 2, 2)) self.dummy2d = np.zeros((2, 2)) self.band = 1 self.nc = xr.Dataset( { 'data': xr.DataArray(self.dummy3d, dims=('time', 'yc', 'xc')), 'lon': xr.DataArray(data=self.dummy2d, dims=('yc', 'xc')), 'lat': xr.DataArray(data=self.dummy2d, dims=('yc', 'xc')), 'time': xr.DataArray(data=np.array([self.time], dtype='datetime64[ms]'), dims=('time', )), 'bands': xr.DataArray(data=np.array([self.band])) }, attrs={'Satellite Sensor': 'G-15'}) xr_.open_dataset.return_value = self.nc # Instantiate reader using the mocked open_dataset() method. Also, make # the reader believe all abstract methods have been implemented. self.reader = GOESNCBaseFileHandler(filename='dummy', filename_info={}, filetype_info={})
class GOESNCBaseFileHandlerTest(unittest.TestCase): """Testing the file handler.""" longMessage = True @mock.patch('satpy.readers.goes_imager_nc.xr') @mock.patch.multiple('satpy.readers.goes_imager_nc.GOESNCBaseFileHandler', __abstractmethods__=set(), _get_sector=mock.MagicMock()) def setUp(self, xr_): """Set up the tests.""" from satpy.readers.goes_imager_nc import CALIB_COEFS, GOESNCBaseFileHandler self.coefs = CALIB_COEFS['GOES-15'] # Mock file access to return a fake dataset. self.time = datetime.datetime(2018, 8, 16, 16, 7) self.dummy3d = np.zeros((1, 2, 2)) self.dummy2d = np.zeros((2, 2)) self.band = 1 self.nc = xr.Dataset( { 'data': xr.DataArray(self.dummy3d, dims=('time', 'yc', 'xc')), 'lon': xr.DataArray(data=self.dummy2d, dims=('yc', 'xc')), 'lat': xr.DataArray(data=self.dummy2d, dims=('yc', 'xc')), 'time': xr.DataArray(data=np.array([self.time], dtype='datetime64[ms]'), dims=('time', )), 'bands': xr.DataArray(data=np.array([self.band])) }, attrs={'Satellite Sensor': 'G-15'}) xr_.open_dataset.return_value = self.nc # Instantiate reader using the mocked open_dataset() method. Also, make # the reader believe all abstract methods have been implemented. self.reader = GOESNCBaseFileHandler(filename='dummy', filename_info={}, filetype_info={}) def test_init(self): """Tests reader initialization.""" self.assertEqual(self.reader.nlines, self.dummy2d.shape[0]) self.assertEqual(self.reader.ncols, self.dummy2d.shape[1]) self.assertEqual(self.reader.platform_name, 'GOES-15') self.assertEqual(self.reader.platform_shortname, 'goes15') self.assertEqual(self.reader.gvar_channel, self.band) self.assertIsInstance(self.reader.geo_data, xr.Dataset) def test_get_nadir_pixel(self): """Test identification of the nadir pixel.""" from satpy.readers.goes_imager_nc import FULL_DISC earth_mask = np.array([[0, 0, 0, 0], [0, 1, 0, 0], [1, 1, 1, 0], [0, 1, 0, 0], [0, 0, 0, 0]]) nadir_row, nadir_col = self.reader._get_nadir_pixel( earth_mask=earth_mask, sector=FULL_DISC) self.assertEqual((nadir_row, nadir_col), (2, 1), msg='Incorrect nadir pixel') def test_get_earth_mask(self): """Test identification of earth/space pixels.""" lat = xr.DataArray([-100, -90, -45, 0, 45, 90, 100]) expected = np.array([0, 1, 1, 1, 1, 1, 0]) mask = self.reader._get_earth_mask(lat) self.assertTrue(np.all(mask == expected), msg='Incorrect identification of earth/space pixel') def test_is_yaw_flip(self): """Test yaw flip identification.""" lat_asc = xr.DataArray([[1, 1, 1], [2, 2, 2], [3, 3, 3]]) lat_dsc = xr.DataArray([[3, 3, 3], [2, 2, 3], [1, 1, 1]]) self.assertEqual(self.reader._is_yaw_flip(lat_asc, delta=1), True, msg='Yaw flip not identified') self.assertEqual(self.reader._is_yaw_flip(lat_dsc, delta=1), False, msg='Yaw flip false alarm') def test_viscounts2radiance(self): """Test conversion from VIS counts to radiance.""" # Reference data is for detector #1 slope = self.coefs['00_7']['slope'][0] offset = self.coefs['00_7']['offset'][0] counts = xr.DataArray([0, 100, 200, 500, 1000, 1023]) rad_expected = xr.DataArray( [0., 41.54896, 100.06862, 275.6276, 568.2259, 581.685422]) rad = self.reader._viscounts2radiance(counts=counts, slope=slope, offset=offset) self.assertTrue(np.allclose(rad.data, rad_expected.data, atol=1E-6), msg='Incorrect conversion from VIS counts to ' 'radiance') def test_ircounts2radiance(self): """Test conversion from IR counts to radiance.""" # Test counts counts = xr.DataArray([0, 100, 500, 1000, 1023]) # Reference Radiance from NOAA lookup tables (same for detectors 1 and # 2, see [IR]) rad_expected = { '03_9': np.array([0, 0.140, 1.899, 4.098, 4.199]), '06_5': np.array([0, 1.825, 12.124, 24.998, 25.590]), '10_7': np.array([0, 16.126, 92.630, 188.259, 192.658]), '13_3': np.array([0, 15.084, 87.421, 177.842, 182.001]) } # The input counts are exact, but the accuracy of the output radiance is # limited to 3 digits atol = 1E-3 for ch in sorted(rad_expected.keys()): coefs = self.coefs[ch] rad = self.reader._ircounts2radiance(counts=counts, scale=coefs['scale'], offset=coefs['offset']) self.assertTrue(np.allclose(rad.data, rad_expected[ch], atol=atol), msg='Incorrect conversion from IR counts to ' 'radiance in channel {}'.format(ch)) def test_calibrate_vis(self): """Test VIS calibration.""" rad = xr.DataArray([0, 1, 10, 100, 500]) refl_expected = xr.DataArray([0., 0.188852, 1.88852, 18.8852, 94.426]) refl = self.reader._calibrate_vis(radiance=rad, k=self.coefs['00_7']['k']) self.assertTrue(np.allclose(refl.data, refl_expected.data, atol=1E-6), msg='Incorrect conversion from radiance to ' 'reflectance') def test_calibrate_ir(self): """Test IR calibration.""" # Test radiance values and corresponding BT from NOAA lookup tables # rev. H (see [IR]). rad = { '03_9': xr.DataArray([0, 0.1, 2, 3.997, 4.199]), '06_5': xr.DataArray([0, 0.821, 12.201, 25.590, 100]), '10_7': xr.DataArray([0, 11.727, 101.810, 189.407, 192.658]), '13_3': xr.DataArray([0, 22.679, 90.133, 182.001, 500]) } bt_expected = { '03_9': np.array([[np.nan, 253.213, 319.451, 339.983, np.nan], [np.nan, 253.213, 319.451, 339.983, np.nan]]), '06_5': np.array([[np.nan, 200.291, 267.860, 294.988, np.nan], [np.nan, 200.308, 267.879, 295.008, np.nan]]), '10_7': np.array([[np.nan, 200.105, 294.437, 339.960, np.nan], [np.nan, 200.097, 294.429, 339.953, np.nan]]), '13_3': np.array([[np.nan, 200.006, 267.517, 321.986, np.nan], [np.nan, 200.014, 267.524, 321.990, np.nan]]) } # first row is for detector 1, second for detector 2. # The accuracy of the input radiance is limited to 3 digits so that # the results differ slightly. atol = {'03_9': 0.04, '06_5': 0.03, '10_7': 0.01, '13_3': 0.01} for ch in sorted(rad.keys()): coefs = self.coefs[ch] for det in [0, 1]: bt = self.reader._calibrate_ir(radiance=rad[ch], coefs={ 'a': coefs['a'][det], 'b': coefs['b'][det], 'n': coefs['n'][det], 'btmin': coefs['btmin'], 'btmax': coefs['btmax'] }) self.assertTrue( np.allclose(bt.data, bt_expected[ch][det], equal_nan=True, atol=atol[ch]), msg='Incorrect conversion from radiance to brightness ' 'temperature in channel {} detector {}'.format(ch, det)) def test_start_time(self): """Test dataset start time stamp.""" self.assertEqual(self.reader.start_time, self.time) def test_end_time(self): """Test dataset end time stamp.""" from satpy.readers.goes_imager_nc import FULL_DISC, SCAN_DURATION, UNKNOWN_SECTOR expected = { UNKNOWN_SECTOR: self.time, FULL_DISC: self.time + SCAN_DURATION[FULL_DISC] } for sector, end_time in expected.items(): self.reader.sector = sector self.assertEqual(self.reader.end_time, end_time)