From 60547c165256bb1b1a61d93118a6ff326c135165 Mon Sep 17 00:00:00 2001
From: Paolo Veglio <paolo.veglio@ssec.wisc.edu>
Date: Thu, 22 Aug 2024 19:56:32 +0000
Subject: [PATCH] fixed spectral tests over snow during daytime

---
 mvcm/preprocess_thresholds.py               | 14 ++++++-----
 mvcm/spectral_tests.py                      | 26 ++++++++++++++++-----
 thresholds/thresholds.mvcm.snpp.v0.0.1.yaml |  5 ++--
 3 files changed, 31 insertions(+), 14 deletions(-)

diff --git a/mvcm/preprocess_thresholds.py b/mvcm/preprocess_thresholds.py
index 4260d49..a1c1475 100644
--- a/mvcm/preprocess_thresholds.py
+++ b/mvcm/preprocess_thresholds.py
@@ -56,6 +56,8 @@ def thresholds_11_12um(
     thr_dict = prepare_11_12um_thresholds(thresholds, m15.shape[0])
 
     midpt = np.full(m15.shape[0], thr)
+    if scene in ["Day_Snow"]:
+        midpt = np.full(m15.shape[0], thr) + thr_dict["adj"]
     idx = np.nonzero((thr < 0.1) | (np.abs(schi - 99.0) < 0.0001))
     midpt[idx] = thr_dict["coeffs"][idx, 0]
     locut = midpt + (thr_dict["cmult"] * midpt)
@@ -92,13 +94,14 @@ def thresholds_11_12um(
             "Land_Night": 0.3,
             "Polar_Night_Land": 0.3,
             "Polar_Night_Snow": 0.3,
-            "Day_Snow": 0.8,
+            "Day_Snow": 0.8,  # this is not needed anymore, replaced by "adj" in thresholds file
             "Night_Snow": 0.3,
         }
 
         if scene in ["Day_Snow"]:
-            midpt = midpt + _coeffs[scene]
+            hicut = midpt - (thr_dict["cmult"] * midpt)
         else:
+            # This needs to be rewritten properly
             midpt = midpt - (_coeffs[scene] * locut)
 
         if scene in ["Polar_Night_Land", "Polar_Night_Snow", "Night_Snow"]:
@@ -109,11 +112,10 @@ def thresholds_11_12um(
             hicut = np.full(m15.shape, 1.25)
             idx = np.nonzero((m15 < thr_dict["bt1"]) & (latitude > thr_dict["lat"]))
             hicut[idx] = -0.1 - np.power(90.0 - np.abs(latitude[idx]) / 60, 4) * 1.15
-        elif scene in ["Day_Snow"]:
-            hicut = locut - (thr_dict["cmult"] * locut)
         else:
-            err_msg = "Scene name not valid"
-            raise ValueError(err_msg)
+            pass
+            # err_msg = "Scene name not valid"
+            # raise ValueError(err_msg)
     else:
         err_msg = "Scene name not valid"
         raise ValueError(err_msg)
diff --git a/mvcm/spectral_tests.py b/mvcm/spectral_tests.py
index 14f0354..e5fb9a6 100644
--- a/mvcm/spectral_tests.py
+++ b/mvcm/spectral_tests.py
@@ -525,15 +525,25 @@ class CloudTests:
                     & (self.data.M15.values < threshold["bt_cutoff"])
                 )
                 bits["qa"][qaidx] = 1
+            elif self.scene_name in ["Day_Snow"]:
+                qaidx = np.nonzero(
+                    (self.data[self.scene_name].values == 1)
+                    & (self.data.M15.values > threshold["bt_cutoff"])
+                )
+                bits["qa"][qaidx] = 1
             else:
                 bits["qa"][self.scene_idx] = 1
             if self.scene_name in ["Day_Snow"]:
+                idx = np.nonzero(
+                    (self.data[self.scene_name].values == 1)
+                    & (self.data.M15.values > threshold["bt_cutoff"])
+                )
                 thr = preproc.polar_night_thresholds(
                     self.data,
                     threshold,
                     self.scene_name,
                     "11-4um_Oceanic_Stratus_Test",
-                    self.scene_idx,
+                    idx,
                 )
             else:
                 thr = threshold["thr"]
@@ -583,15 +593,19 @@ class CloudTests:
                 bits["test"][idx] = 1
                 kwargs["confidence"][self.scene_idx] = conf.conf_test_dble(rad[self.scene_idx], thr)
             elif self.scene_name in ["Day_Snow"]:
-                f_rad = np.array(rad[self.scene_idx], dtype=np.float32)
+                idx = tuple(
+                    np.nonzero(
+                        (self.data[self.scene_name].values == 1)
+                        & (self.data.M15.values > threshold["bt_cutoff"])
+                    )
+                )
+                f_rad = np.array(rad[idx], dtype=np.float32)
                 locut = np.array(thr[0, :], dtype=np.float32)
                 midpt = np.array(thr[1, :], dtype=np.float32)
                 hicut = np.array(thr[2, :], dtype=np.float32)
                 power = np.array(thr[3, :], dtype=np.float32)
-                bits["test"][self.scene_idx] = 1
-                kwargs["confidence"][self.scene_idx] = anc.py_conf_test(
-                    f_rad, locut, hicut, power, midpt
-                )
+                bits["test"][idx] = 1
+                kwargs["confidence"][idx] = anc.py_conf_test(f_rad, locut, hicut, power, midpt)
             else:
                 kwargs["confidence"][self.scene_idx] = conf.conf_test_new(rad[self.scene_idx], thr)
 
diff --git a/thresholds/thresholds.mvcm.snpp.v0.0.1.yaml b/thresholds/thresholds.mvcm.snpp.v0.0.1.yaml
index a77fab4..a345172 100644
--- a/thresholds/thresholds.mvcm.snpp.v0.0.1.yaml
+++ b/thresholds/thresholds.mvcm.snpp.v0.0.1.yaml
@@ -406,7 +406,6 @@ Polar_Day_Snow:
     cmult: 0 # I NEED TO WORK ON THIS
     adj: 0 # I NEED TO WORK ON THIS
     perform: False
-  dpsbt1: 230.0
   Water_Vapor_High_Clouds_Test: [215.0, 220.0, 225.0, 1.0, 1.0]
   1.38um_High_Cloud_Test:
     thr: [0.060, 0.0525, 0.045, 1.0, 1.0]
@@ -420,6 +419,7 @@ Polar_Day_Snow:
     mid3: [0.0, 0.0, 0.0, 0.0]
     high: [18.0, 16.0, 14.0, 1.0]
     bt11_bounds: [230.0, 0.0, 0.0, 245.0]
+    bt_cutoff: 230.0
     perform: False
   dps11_12adj: 0.8
 
@@ -567,7 +567,7 @@ Day_Snow:
   11-12um_Cirrus_Test:
     coeffs: [3.0, 1.0]
     cmult: 0.3
-    adj: 0 # I NEED TO WORK ON THIS
+    adj: 0.8
     perform: True
   CO2_High_Clouds_Test: [222.0, 224.0, 226.0, 1.0, 1.0]
   Water_Vapor_High_Clouds_Test: [215.0, 220.0, 225.0, 1.0, 1.0]
@@ -590,6 +590,7 @@ Day_Snow:
     mid3: [0.0, 0.0, 0.0, 0.0]
     high: [18.0, 16.0, 14.0, 1.0]
     bt11_bounds: [230.0, 0.0, 0.0, 245.0]
+    bt_cutoff: 230.0
     perform: True
   ds11_12adj: 0.8
   ds11_12lcmult: 0.3
-- 
GitLab