From 06ff6c2004340ae570e10cb83f114bd3469ceff4 Mon Sep 17 00:00:00 2001
From: Paolo Veglio <paolo.veglio@ssec.wisc.edu>
Date: Wed, 15 Feb 2023 14:52:58 +0000
Subject: [PATCH] all tests implemented. need a second pass to make sure
 everything is good

---
 mvcm/spectral_tests.py       | 59 ++++++++++++++++--------------------
 tests/test_spectral_tests.py | 51 +++++++++++++++++++++++++++++++
 2 files changed, 77 insertions(+), 33 deletions(-)

diff --git a/mvcm/spectral_tests.py b/mvcm/spectral_tests.py
index fcf8db2..dec93a9 100644
--- a/mvcm/spectral_tests.py
+++ b/mvcm/spectral_tests.py
@@ -462,7 +462,6 @@ class CloudTests(object):
         return cmin, np.abs(1-kwargs['test_bit'])*kwargs['qa_bit']
 
     # this test needs some TLC
-    # NEED TO FIGURE OUT WHY CMIN BECOMES A TUPLE AT SOME POINT
     @run_if_test_exists_for_scene('Visible_Reflectance_Test')
     def visible_reflectance_test(self,
                                  band: str,
@@ -470,6 +469,8 @@ class CloudTests(object):
                                  **kwargs) -> np.ndarray:
 
         threshold = kwargs['thresholds']
+        logger.warning('The pytest now seems to be working now but it use to fail for this function' +
+                       'If it fails again go check the test before turning this function inside out')
 
         if (threshold['perform'] is True and self.pixels_in_scene is True):
             print(f'Testing "{self.scene_name}"\n')
@@ -478,81 +479,73 @@ class CloudTests(object):
             kwargs['confidence'][self.scene_idx] = conf.conf_test_new(rad, thr)
 
         cmin = np.fmin(cmin, kwargs['confidence'])
-        print(cmin.shape)
         return cmin, kwargs['test_bit']
 
-    @run_if_test_exists_for_scene
+    @run_if_test_exists_for_scene('GEMI_Test')
     def gemi_test(self,
                   band: str,
                   cmin: np.ndarray,
-                  test_name: str = 'GEMI_Test') -> np.ndarray:
+                  **kwargs) -> np.ndarray:
 
-        confidence = np.ones(self.data.M01.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):
             thr = preproc.gemi_thresholds(self.data, threshold, self.scene_name,
                                           self.scene_idx)
             rad = self.data[band].values[self.scene_idx]
-            confidence[self.scene_idx] = conf.conf_test_new(rad, thr)
+            kwargs['confidence'][self.scene_idx] = conf.conf_test_new(rad, 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
+    @run_if_test_exists_for_scene('1.38um_High_Cloud_Test')
     def test_1_38um_high_clouds(self,
                                 band: str,
                                 cmin: np.ndarray,
-                                test_name: str = '1.38um_High_Cloud_Test') -> np.ndarray:
+                                **kwargs) -> np.ndarray:
 
-        confidence = np.ones(self.data.M01.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
             if self.scene_name in ['Ocean_Day', 'Polar_Day_Ocean']:
                 thr = preproc.thresholds_1_38um_test(self.data, self.thresholds, self.scene_name,
                                                      self.scene_idx)
             else:
-                return cmin, test_bit
-                thr = threshold['thr']
+                # return cmin, kwargs['test_bit']
+                thr = np.full((len(self.scene_idx[0]), 4), threshold['thr'][:4]).T
 
             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)
+        cmin = np.fmin(cmin, kwargs['confidence'])
 
-        return cmin, np.abs(1-test_bit)*qa_bit
+        return cmin, np.abs(1-kwargs['test_bit'])*kwargs['qa_bit']
 
-    @run_if_test_exists_for_scene
+    @run_if_test_exists_for_scene('4-12um_BTD_Thin_Cirrus_Test')
     def thin_cirrus_4_12um_BTD_test(self,
                                     band: str,
                                     cmin: np.ndarray,
-                                    test_name: str = '4-12um_BTD_Thin_Cirrus_Test') -> np.ndarray:
+                                    **kwargs) -> np.ndarray:
 
-        confidence = np.ones(self.data.M01.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):
             thr = preproc.polar_night_thresholds(self.data, self.thresholds, self.scene_name,
-                                                 test_name, self.scene_idx)
+                                                 '4-12um_BTD_Thin_Cirrus_Test', self.scene_idx)
             rad = self.data[band].values[self.scene_idx]
-            confidence[self.scene_idx] = conf.conf_test_new(rad, thr)
+            kwargs['confidence'][self.scene_idx] = conf.conf_test_new(rad, thr)
 
-        cmin = np.fmin(cmin, confidence)
+        cmin = np.fmin(cmin, kwargs['confidence'])
 
-        return cmin, test_bit
+        return cmin, kwargs['test_bit']
 
     def single_threshold_test(self, test_name, band, cmin):
 
diff --git a/tests/test_spectral_tests.py b/tests/test_spectral_tests.py
index 44cfeff..b2c18f4 100644
--- a/tests/test_spectral_tests.py
+++ b/tests/test_spectral_tests.py
@@ -189,3 +189,54 @@ def test_16_21um_reflectance_test(data, thresholds, ref_confidence):
 
     assert np.allclose(cmin, ref_confidence.refl_16_21um_confidence.values)
 
+
+def test_visible_reflectance_test(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', 'Polar_Day_Land', 'Polar_Day_Coast',
+                       'Polar_Day_Desert', 'Polar_Day_Desert_Coast']:
+        SceneType = tst.CloudTests(data=data,
+                                   scene_name=scene_name,
+                                   thresholds=thresholds)
+
+        cmin, bit = SceneType.visible_reflectance_test('M128', cmin)
+
+    assert np.allclose(cmin, ref_confidence.vis_reflectance_confidence.values)
+
+
+def test_GEMI_test(data, thresholds, ref_confidence):
+    cmin = np.ones(data.latitude.shape)
+    for scene_name in ['Land_Day_Desert', 'Polar_Day_Desert']:
+        SceneType = tst.CloudTests(data=data,
+                                   scene_name=scene_name,
+                                   thresholds=thresholds)
+
+        cmin, bit = SceneType.gemi_test('GEMI', cmin)
+
+    assert np.allclose(cmin, ref_confidence.gemi_confidence.values)
+
+
+def test_1_38um_high_clouds_test(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',
+                       'Polar_Day_Land', 'Polar_Day_Coast', 'Polar_Day_Desert',
+                       'Polar_Day_Desert_Coast', 'Day_Snow', 'Ocean_Day', 'Polar_Day_Ocean']:
+        SceneType = tst.CloudTests(data=data,
+                                   scene_name=scene_name,
+                                   thresholds=thresholds)
+
+        cmin, bit = SceneType.test_1_38um_high_clouds('M09', cmin)
+
+    assert np.allclose(cmin, ref_confidence.hi_clouds_confidence.values)
+
+
+def test_4_12um_thin_cirrus_btd(data, thresholds, ref_confidence):
+    cmin = np.ones(data.latitude.shape)
+    for scene_name in ['Land_Night', 'Polar_Night_Land', 'Polar_Night_Snow', 'Night_Snow']:
+        SceneType = tst.CloudTests(data=data,
+                                   scene_name=scene_name,
+                                   thresholds=thresholds)
+
+        cmin, bit = SceneType.thin_cirrus_4_12um_BTD_test('M13-M16', cmin)
+
+    assert np.allclose(cmin, ref_confidence.thin_cirrus_confidence.values)
-- 
GitLab