From f30cedb5d07cba5dac416a931b5b86877739f510 Mon Sep 17 00:00:00 2001
From: tomrink <rink@ssec.wisc.edu>
Date: Tue, 21 Jun 2022 16:53:27 -0500
Subject: [PATCH] nlcomp for VIIRS

---
 modules/util/util.py | 43 +++++++++++++++++++++++++++++++++++++++++--
 1 file changed, 41 insertions(+), 2 deletions(-)

diff --git a/modules/util/util.py b/modules/util/util.py
index 0d9374ce..7305369a 100644
--- a/modules/util/util.py
+++ b/modules/util/util.py
@@ -659,7 +659,26 @@ def make_for_full_domain_predict(h5f, name_list=None, satellite='GOES16', domain
     return grd_dct, ll, cc
 
 
-def make_for_full_domain_predict_viirs_clavrx(h5f, name_list=None, res_fac=1):
+rho_water = 1.
+rho_ice = 0.917
+
+
+def compute_lwc_iwc(iphase, reff, opd):
+    lwp_dcomp = np.zeros(reff.shape[0])
+    iwp_dcomp = np.zeros(reff.shape[0])
+    lwp_dcomp[:] = np.nan
+    iwp_dcomp[:] = np.nan
+
+    ice = iphase == 1
+    no_ice = iphase != 1
+
+    iwp_dcomp[ice] = 0.667 * opd[ice] * rho_ice * reff[ice]
+    lwp_dcomp[no_ice] = 0.55 * opd[no_ice] * rho_water * reff[no_ice]
+
+    return lwp_dcomp, iwp_dcomp
+
+
+def make_for_full_domain_predict_viirs_clavrx(h5f, name_list=None, res_fac=1, day_night='DAY', lunar=False):
     w_x = 16
     w_y = 16
     i_0 = 0
@@ -670,11 +689,23 @@ def make_for_full_domain_predict_viirs_clavrx(h5f, name_list=None, res_fac=1):
     ylen = h5f['scan_lines_along_track_direction'].shape[0]
     xlen = h5f['pixel_elements_along_scan_direction'].shape[0]
 
+    use_nl_comp = False
+    if day_night == 'NIGHT' and lunar:
+        use_nl_comp = True
+
     grd_dct = {name: None for name in name_list}
 
     cnt_a = 0
     for ds_name in name_list:
-        gvals = get_grid_values(h5f, ds_name, j_0, i_0, None, num_j=ylen, num_i=xlen)
+        name = ds_name
+
+        if use_nl_comp:
+            if ds_name == 'cld_reff_dcomp':
+                name = 'cld_reff_nlcomp'
+            elif ds_name == 'cld_opd_dcomp':
+                name = 'cld_opd_nlcomp'
+
+        gvals = get_grid_values(h5f, name, j_0, i_0, None, num_j=ylen, num_i=xlen)
         if gvals is not None:
             grd_dct[ds_name] = gvals
             cnt_a += 1
@@ -682,6 +713,14 @@ def make_for_full_domain_predict_viirs_clavrx(h5f, name_list=None, res_fac=1):
     if cnt_a > 0 and cnt_a != len(name_list):
         raise GenericException('weirdness')
 
+    if use_nl_comp:
+        cld_phase = get_grid_values(h5f, 'cloud_phase', j_0, i_0, None, num_j=ylen, num_i=xlen)
+        reff = grd_dct['cld_reff_dcomp']
+        opd = grd_dct['cld_opd_dcomp']
+        lwc_nlcomp, iwc_nlcomp = compute_lwc_iwc(cld_phase, reff, opd)
+        grd_dct['iwc_dcomp'] = iwc_nlcomp
+        grd_dct['lwc_dcomp'] = lwc_nlcomp
+
     grd_dct_n = {name: [] for name in name_list}
 
     n_x = int(xlen/s_x) - 1
-- 
GitLab