From 60d68f302e65e6eaebd56cfe2bbd39bc8e77c390 Mon Sep 17 00:00:00 2001
From: tomrink <rink@ssec.wisc.edu>
Date: Wed, 19 Jan 2022 15:45:50 -0600
Subject: [PATCH] add new method to time sort icing dicts, use it in
 `pirep_icing`

---
 modules/icing/pireps.py | 34 +++++++++++++++++++++++++++++++++-
 1 file changed, 33 insertions(+), 1 deletion(-)

diff --git a/modules/icing/pireps.py b/modules/icing/pireps.py
index 25f7216f..772a6f19 100644
--- a/modules/icing/pireps.py
+++ b/modules/icing/pireps.py
@@ -1,6 +1,6 @@
 import datetime
 from datetime import timezone
-from dateutil import tz
+import numpy as np
 import re
 from pathlib import Path
 import h5py
@@ -22,6 +22,34 @@ ATYPE = '/TP'
 RMK = '/RM'
 
 
+def time_sort_pirep_dict(the_dict):
+    new_dict = {}
+
+    keys = []
+    tidx = []
+    for ts in list(the_dict.keys()):
+        rpts = the_dict[ts]
+        for idx, tup in enumerate(rpts):
+            keys.append(ts)
+            tidx.append(idx)
+
+    sidxs = np.argsort(keys)
+    ice_keys = keys[sidxs]
+    ice_tidx = tidx[sidxs]
+
+    for idx, key in enumerate(ice_keys):
+        rpts = the_dict[key]
+        tup = rpts[ice_tidx[idx]]
+
+        n_rpts = new_dict.get(key)
+        if n_rpts is None:
+            n_rpts = []
+            new_dict[key] = n_rpts
+        n_rpts.append(tup)
+
+    return new_dict
+
+
 # Returns icing, no_icing (no icing observed), neg-icing (icing expected, but not observed)
 def pirep_icing(filename, lon_range=[-180, 180], lat_range=[-63, 63]):
 
@@ -158,6 +186,10 @@ def pirep_icing(filename, lon_range=[-180, 180], lat_range=[-63, 63]):
                 else:
                     rpts.append(tup)
 
+    ice_dict = time_sort_pirep_dict(ice_dict)
+    no_ice_dict = time_sort_pirep_dict(no_ice_dict)
+    neg_ice_dict = time_sort_pirep_dict(neg_ice_dict)
+
     return ice_dict, no_ice_dict, neg_ice_dict
 
 
-- 
GitLab