From ade6b7cf49441b2c1c665bbf05a091a38e8e242b Mon Sep 17 00:00:00 2001 From: Paolo Veglio <paolo.veglio@ssec.wisc.edu> Date: Fri, 3 Feb 2023 20:42:53 +0000 Subject: [PATCH] added more tests --- mvcm/spectral_tests.py | 106 ++++++++++++++++------------------- tests/test_spectral_tests.py | 38 +++++++++++++ 2 files changed, 85 insertions(+), 59 deletions(-) diff --git a/mvcm/spectral_tests.py b/mvcm/spectral_tests.py index 1599327..e237b4f 100644 --- a/mvcm/spectral_tests.py +++ b/mvcm/spectral_tests.py @@ -143,81 +143,74 @@ class CloudTests(object): # return cmin, np.abs(1-test_bit)*qa_bit return cmin, kwargs['test_bit'] - @run_if_test_exists_for_scene + @run_if_test_exists_for_scene('8.6-11um_Test') def bt_diff_86_11um(self, band: str, cmin: np.ndarray, - test_name: str = '8.6-11um_Test') -> np.ndarray: + **kwargs) -> np.ndarray: - confidence = np.ones(self.data[band].shape) - qa_bit = np.zeros(self.data[band].shape) - test_bit = np.zeros(self.data[band].shape) - threshold = self.thresholds[self.scene_name][test_name] + threshold = kwargs['thresholds'] if (threshold['perform'] is True and self.pixels_in_scene is True): - qa_bit[self.scene_idx] = 1 + kwargs['qa_bit'][self.scene_idx] = 1 print(f'Testing "{self.scene_name}"\n') rad = self.data[band].values[self.scene_idx] idx = np.nonzero((self.data[band].values < threshold['thr'][1]) & (self.data[self.scene_name] == 1)) - test_bit[idx] = 1 - confidence[self.scene_idx] = conf.conf_test_new(rad, threshold['thr']) + kwargs['test_bit'][idx] = 1 + kwargs['confidence'][self.scene_idx] = conf.conf_test_new(rad, threshold['thr']) - cmin = np.fmin(cmin, confidence) + cmin = np.fmin(cmin, kwargs['confidence']) - return cmin, test_bit # np.abs(1-test_bit)*qa_bit + return cmin, kwargs['test_bit'] # np.abs(1-test_bit)*qa_bit - @run_if_test_exists_for_scene + @run_if_test_exists_for_scene('11-12um_Cirrus_Test') def test_11_12um_diff(self, band: str, cmin: np.ndarray, - test_name: str = '11-12um_Cirrus_Test') -> np.ndarray: + **kwargs) -> np.ndarray: - confidence = np.ones(self.data.M15.shape) - qa_bit = np.zeros(self.data[band].shape) - test_bit = np.zeros(self.data[band].shape) - threshold = self.thresholds[self.scene_name][test_name] + threshold = kwargs['thresholds'] if (threshold['perform'] is True and self.pixels_in_scene is True): - qa_bit[self.scene_idx] = 1 + kwargs['qa_bit'][self.scene_idx] = 1 thr = preproc.thresholds_11_12um(self.data, threshold, self.scene_name, self.scene_idx) print(f'Testing "{self.scene_name}"\n') rad = self.data[band].values[self.scene_idx] idx = np.nonzero((rad <= thr[1, :]) & (self.data[self.scene_name].values[self.scene_idx] == 1)) - tmp_bit = test_bit[self.scene_idx] + tmp_bit = kwargs['test_bit'][self.scene_idx] tmp_bit[idx] = 1 - test_bit[self.scene_idx] = tmp_bit - confidence[self.scene_idx] = conf.conf_test_new(rad, thr) + kwargs['test_bit'][self.scene_idx] = tmp_bit + kwargs['confidence'][self.scene_idx] = conf.conf_test_new(rad, thr) - cmin = np.fmin(cmin, confidence) - test_bit = test_bit.reshape(self.data[band].shape) + cmin = np.fmin(cmin, kwargs['confidence']) + kwargs['test_bit'] = kwargs['test_bit'].reshape(self.data[band].shape) # return cmin, np.abs(1-test_bit)*qa_bit - return cmin, test_bit + return cmin, kwargs['test_bit'] - @run_if_test_exists_for_scene + # THIS NEEDS TO BE TESTED + @run_if_test_exists_for_scene('11-4um_BT_Difference_Test') def bt_difference_11_4um_test(self, band: str, cmin: np.ndarray, - test_name: str = '11-4um_BT_Difference_Test') -> np.ndarray: + **kwargs) -> np.ndarray: - confidence = np.ones(self.data.M15.shape) - qa_bit = np.zeros(self.data[band].shape) - test_bit = np.zeros(self.data[band].shape) - threshold = self.thresholds[self.scene_name][test_name] + threshold = kwargs['thresholds'] if (threshold['perform'] is True and self.pixels_in_scene is True): - qa_bit[self.scene_idx] = 1 + kwargs['qa_bit'][self.scene_idx] = 1 print(f'Testing "{self.scene_name}"\n') thr = preproc.bt_diff_11_4um_thresholds(self.data, threshold, self.scene_idx) rad = self.data[band].values[self.scene_idx] - test_bit[self.scene_idx] = 1 # THIS NEEDS TO BE CALCULATED PROPERLY - confidence[self.scene_idx] = conf.conf_test_dble(rad, thr) + kwargs['test_bit'][self.scene_idx] = 1 # THIS NEEDS TO BE CALCULATED PROPERLY + kwargs['confidence'][self.scene_idx] = conf.conf_test_dble(rad, thr) - cmin = np.fmin(cmin, confidence) + cmin = np.fmin(cmin, kwargs['confidence']) - return cmin, test_bit + return cmin, kwargs['test_bit'] + # THIS NEEDS TO BE TESTED @run_if_test_exists_for_scene def bt_difference_11_4um_test_land(self, band1: str, @@ -311,21 +304,18 @@ class CloudTests(object): def water_vapor_cloud_test(): pass - @run_if_test_exists_for_scene + @run_if_test_exists_for_scene('11um_Variability_Test') def variability_11um_test(self, band: str, cmin: np.ndarray, - test_name: str = '11um_Variability_Test') -> np.ndarray: + **kwargs) -> np.ndarray: - confidence = np.ones(self.data.M15.shape) - qa_bit = np.zeros(self.data[band].shape) - test_bit = np.zeros(self.data[band].shape) - threshold = self.thresholds[self.scene_name][test_name] + threshold = kwargs['thresholds'] var = np.zeros([self.data.M15.shape[0], self.data.M15.shape[1], 9]) radshape = self.data.M15.shape if (threshold['perform'] is True and self.pixels_in_scene is True): - qa_bit[self.scene_idx] = 1 + kwargs['qa_bit'][self.scene_idx] = 1 print(f'Testing "{self.scene_name}"\n') rad = self.data[band].values @@ -335,35 +325,33 @@ class CloudTests(object): var = var.sum(axis=2) idx = np.where(var == 9) - test_bit[idx] = 1 + kwargs['test_bit'][idx] = 1 thr = np.array(threshold['thr']) - confidence[idx] = conf.conf_test_new(rad[idx], thr) + kwargs['confidence'][idx] = conf.conf_test_new(rad[idx], thr) - cmin = np.fmin(cmin, confidence) + cmin = np.fmin(cmin, kwargs['confidence']) - return cmin, test_bit + return cmin, kwargs['test_bit'] - @run_if_test_exists_for_scene + # NEED TO CHECK WHAT'S GOING ON WITH THIS TEST + @run_if_test_exists_for_scene('11-4um_Oceanic_Stratus_Test') def oceanic_stratus_11_4um_test(self, band: str, cmin: np.ndarray, - test_name: str = '11-4um_Oceanic_Stratus_Test') -> np.ndarray: + **kwargs) -> np.ndarray: - confidence = np.ones(self.data.M15.shape) - qa_bit = np.zeros(self.data[band].shape) - test_bit = np.zeros(self.data[band].shape) - threshold = self.thresholds[self.scene_name][test_name] + threshold = kwargs['thresholds'] if (threshold['perform'] is True and self.pixels_in_scene is True): - qa_bit[self.scene_idx] = 1 + kwargs['qa_bit'][self.scene_idx] = 1 print(f'Testing "{self.scene_name}"\n') rad = self.data[band].values[self.scene_idx] thr = threshold['thr'] if self.scene_name in ['Land_Day_Desert', 'Land_Day_Desert_Coast', 'Polar_Day_Desert', 'Polar_Day_Desert_Coast']: - confidence[self.scene_idx] = conf.conf_test_dble(rad, thr) + kwargs['confidence'][self.scene_idx] = conf.conf_test_dble(rad, thr) - # these scenes have not been implemented yet + # these scenes have not been implemented yet... and it might not be necessary # elif self.scene_name in ['Land_Night', 'Polar_Night_Land', 'Polar_Day_Snow', 'Polar_Night_Snow', # 'Day_Snow', 'Night_Snow', 'Antarctic_Day']: # pass @@ -372,14 +360,14 @@ class CloudTests(object): idx = np.nonzero((self.data[band].values >= threshold['thr'][1]) & (self.data[self.scene_name] == 1) & (scene_flag['sunglint'] == 0)) - test_bit[idx] = 1 + kwargs['test_bit'][idx] = 1 idx = np.nonzero((self.data[self.scene_name] == 1) & (scene_flag['sunglint'] == 0)) - confidence[idx] = conf.conf_test_new(self.data[band].values[idx], thr) + kwargs['confidence'][idx] = conf.conf_test_new(self.data[band].values[idx], thr) - cmin = np.fmin(cmin, confidence) + cmin = np.fmin(cmin, kwargs['confidence']) - return cmin, test_bit + return cmin, kwargs['test_bit'] @run_if_test_exists_for_scene def nir_reflectance_test(self, diff --git a/tests/test_spectral_tests.py b/tests/test_spectral_tests.py index 74b6ccc..814f539 100644 --- a/tests/test_spectral_tests.py +++ b/tests/test_spectral_tests.py @@ -91,3 +91,41 @@ def test_sst_test(data, thresholds, ref_confidence): assert np.allclose(cmin, ref_confidence.sst_confidence.values) +def test_bt_diff_86_11um(data, thresholds, ref_confidence): + cmin = np.ones(data.latitude.shape) + for scene_name in ['Ocean_Day', 'Ocean_Night', 'Polar_Day_Ocean', 'Polar_Night_Ocean']: + SceneType = tst.CloudTests(data=data, + scene_name=scene_name, + thresholds=thresholds) + + cmin, bit = SceneType.bt_diff_86_11um('M14-M15', cmin) + + assert np.allclose(cmin, ref_confidence.diff86_11um_confidence.values) + + +def test_11_12um_diff(data, thresholds, ref_confidence): + cmin = np.ones(data.latitude.shape) + for scene_name in ['Land_Day', 'Land_Day_Coast', 'Land_Day_Desert', 'Land_Day_Desert_Coast', + 'Ocean_Day', 'Ocean_Night', 'Polar_Day_Ocean', 'Polar_Night_Ocean', + 'Polar_Day_Land', 'Polar_Day_Coast', 'Polar_Day_Desert', + 'Polar_Day_Desert_Coast', 'Polar_Day_Snow', 'Land_Night', 'Polar_Night_Land', + 'Polar_Night_Snow', 'Day_Snow', 'Night_Snow']: + SceneType = tst.CloudTests(data=data, + scene_name=scene_name, + thresholds=thresholds) + + cmin, bit = SceneType.test_11_12um_diff('M15-M16', cmin) + + assert np.allclose(cmin, ref_confidence.diff11_12um_confidence.values) + + +def test_variability_11um_test(data, thresholds, ref_confidence): + cmin = np.ones(data.latitude.shape) + for scene_name in ['Polar_Day_Ocean', 'Polar_Night_Ocean']: + SceneType = tst.CloudTests(data=data, + scene_name=scene_name, + thresholds=thresholds) + + cmin, bit = SceneType.variability_11um_test('M15', cmin) + + assert np.allclose(cmin, ref_confidence.var11um_confidence.values) -- GitLab