From 83001e089bfce8d1b7930b226c77d9f4d307d372 Mon Sep 17 00:00:00 2001
From: tomrink <rink@ssec.wisc.edu>
Date: Thu, 27 May 2021 20:08:48 -0500
Subject: [PATCH] remove duplicated code

---
 modules/amv/aeolus.py | 83 +++----------------------------------------
 1 file changed, 5 insertions(+), 78 deletions(-)

diff --git a/modules/amv/aeolus.py b/modules/amv/aeolus.py
index db0b18be..cbb705e4 100644
--- a/modules/amv/aeolus.py
+++ b/modules/amv/aeolus.py
@@ -10,6 +10,7 @@ import h5py
 from netCDF4 import Dataset
 
 from aeolus.aeolus_amv import get_aeolus_time_dict
+from aeolus.datasource import CLAVRx
 from util.lon_lat_grid import earth_to_indexs
 from util.geos_nav import GEOSNavigation
 from util.util import bin_data_by, get_bin_ranges
@@ -63,77 +64,6 @@ num_elems = 5424
 num_lines = 5424
 
 
-def get_filelist():
-    filelist = []
-    for i in range(len(datadirs)):
-        dir = datadirs[i]
-        dirpath = os.path.join(datapath, dir)
-        list = glob.glob(os.path.join(dirpath, "*.nc"))
-        filelist = filelist + list
-    return filelist
-
-
-def get_datetime(pathname):
-    fname = os.path.split(pathname)[1]
-    toks = fname.split('_')
-    dtstr = toks[4]
-    dtstr = dtstr[1:12]
-    dto = datetime.datetime.strptime(dtstr, '%Y%j%H%M').replace(tzinfo=timezone.utc)
-
-    return dto
-
-
-def get_datetime_2(pathname):
-    fname = os.path.split(pathname)[1]
-    toks = fname.split('.')
-    dstr = toks[2]
-    tstr = toks[3]
-    dtstr = dstr+'.'+tstr
-    dto = datetime.datetime.strptime(dtstr, '%Y%j.%H%M').replace(tzinfo=timezone.utc)
-
-    return dto
-
-
-def get_datetime_3(pathname):
-    fname = os.path.split(pathname)[1]
-    toks = fname.split('_')
-    dstr = toks[4]
-    tstr = toks[5]
-    dtstr = dstr+tstr
-    dto = datetime.datetime.strptime(dtstr, '%Y%j%H%M').replace(tzinfo=timezone.utc)
-
-    return dto
-
-
-def get_file_containing_time(timestamp, files_path, dto_func, file_time_span):
-    global first_time, ftimes, flist
-
-    if first_time is True:
-
-        if files_path is not None:
-            flist = glob.glob(files_path + '*.nc')
-        else:
-            flist = get_filelist()
-
-        for pname in flist:  # TODO: make better with regular expressions (someday)
-            dto = dto_func(pname)
-            dto_start = dto
-            dto_end = dto + datetime.timedelta(minutes=file_time_span)
-            ftimes.append((dto_start.timestamp(), dto_end.timestamp()))
-
-        first_time = False
-
-    k = -1
-    for i in range(len(ftimes)):
-        if (timestamp >= ftimes[i][0]) and (timestamp < ftimes[i][1]):
-            k = i
-            break
-    if k < 0:
-        return None, None, None
-
-    return flist[k], ftimes[k], k
-
-
 def time_dict_to_cld_layers(time_dict):
     time_dict_layers = {}
 
@@ -203,11 +133,8 @@ def get_cloud_layers_dict(filename, lon360=False):
 
 
 def compare_aeolus_max_height(aeolus_dict, files_path, grid_value_name='cld_height_acha', one_cld_layer_only=False, highest_layer=False):
-    if files_path is not None:
-        f_list = glob.glob(files_path + '*.nc')
-    else:
-        f_list = get_filelist()
-    num_files = len(f_list)
+    clvrx_files = CLAVRx(files_path)
+    num_files = len(clvrx_files.flist)
 
     keys = list(aeolus_dict.keys())
 
@@ -218,7 +145,7 @@ def compare_aeolus_max_height(aeolus_dict, files_path, grid_value_name='cld_heig
     a_times = [[] for i in range(num_files)]
 
     for key in keys:
-        _, _, f_idx = get_file_containing_time(key, files_path, get_datetime, 10)
+        _, _, f_idx = clvrx_files.get_file_containing_time(key)
         if f_idx is None:
             continue
         layers = aeolus_dict.get(key)
@@ -265,7 +192,7 @@ def compare_aeolus_max_height(aeolus_dict, files_path, grid_value_name='cld_heig
     num_hits = 0
     total = 0
 
-    for f_idx, file in enumerate(f_list):
+    for f_idx, file in enumerate(clvrx_files.flist):
         try:
             h5f = h5py.File(file, 'r')
         except Exception:
-- 
GitLab