From 0d64fe3453906303c3496305c4006e182bc4b4eb Mon Sep 17 00:00:00 2001
From: Alan De Smet <alan.desmet@ssec.wisc.edu>
Date: Wed, 3 Oct 2018 14:20:55 -0500
Subject: [PATCH] Group pairs of statistics on one file (A vs B)

---
 pyglance/glance/basereport.txt     |  1 +
 pyglance/glance/constants.py       |  1 +
 pyglance/glance/report.py          | 48 ++++++++++++++++++++++++++++++
 pyglance/glance/variablereport.txt | 10 ++++---
 4 files changed, 56 insertions(+), 4 deletions(-)

diff --git a/pyglance/glance/basereport.txt b/pyglance/glance/basereport.txt
index 0f6cf93..1a47c0f 100644
--- a/pyglance/glance/basereport.txt
+++ b/pyglance/glance/basereport.txt
@@ -16,6 +16,7 @@ Copyright (c) 2011 University of Wisconsin SSEC. All rights reserved.
     <head>
         <title>${self.title()}</title>
 		<style>
+		th.l { text-align: left; }
 		td.n,th.n { text-align: right; }
 		body { font-family: sans-serif; }
 		h4 { margin-bottom: 0; }
diff --git a/pyglance/glance/constants.py b/pyglance/glance/constants.py
index c3e85a3..e0a03e0 100644
--- a/pyglance/glance/constants.py
+++ b/pyglance/glance/constants.py
@@ -228,6 +228,7 @@ OPTIONS_LONLAT_EPSILON_KEY = 'lonlatepsilon'
 RUN_INFO_DICT_KEY          = 'runInfo'
 FILES_INFO_DICT_KEY        = 'files'
 STATS_INFO_DICT_KEY        = 'statGroups'
+GROUPED_STATS_INFO_DICT_KEY        = 'groupedStats'
 SPATIAL_INFO_DICT_KEY      = 'spatial'
 IMAGE_NAME_INFO_DICT_KEY   = 'imageNames'
 VARIABLE_NAMES_DICT_KEY    = 'varNames'
diff --git a/pyglance/glance/report.py b/pyglance/glance/report.py
index 99c58f6..ad139c8 100644
--- a/pyglance/glance/report.py
+++ b/pyglance/glance/report.py
@@ -207,6 +207,51 @@ def generate_and_save_doc_page(definitions, outputPath) :
     
     return
 
+def group_stats_by_file(statGroups):
+    """ Group statistics by file
+
+    Where there are pairs of statistics in the form *_[ab] or [ab]_*, group
+    them together. So given
+
+    { 'General Statistics': {
+            'min_a': -1,
+            'min_b': -10
+            'epsilon': 3
+        }
+    }
+
+    returns
+
+    { 'General Statistics': {
+            'min': { 'a': -1. b: -10 },
+            'epsilon': { 'both':3 }
+        }
+    }
+
+    """
+
+    def id_file(varname):
+        if varname[1] == "_":
+            return (varname[2:], varname[0])
+        if varname[-2] == "_":
+            return (varname[:-2], varname[-1])
+        return (varname, "both")
+
+    ret = {}
+
+    for groupname, group in statGroups.items():
+        ret[groupname] = {}
+        for var, val in group.items():
+            justvar, file = id_file(var)
+            #sys.stderr.write("{0} -> {1} {2}\n".format(var, justvar, file))
+            if justvar not in ret[groupname]:
+                ret[groupname][justvar] = { 'a':'', 'b':'', 'both':''}
+            ret[groupname][justvar][file] = val
+
+    return ret
+
+
+
 def generate_and_save_variable_report(files,
                                       variableRunInfo, # contains variable specific run information
                                       generalRunInfo,  # contains run information not related to the variable
@@ -313,12 +358,15 @@ def generate_and_save_variable_report(files,
     # information about the run in general
     runInfo = generalRunInfo.copy()
     runInfo.update(variableRunInfo)
+
+    groupedStats = group_stats_by_file(statGroups)
     
     # put all the info together in the kwargs
     kwargs = {
                RUN_INFO_DICT_KEY:        runInfo,
                FILES_INFO_DICT_KEY :     files,
                STATS_INFO_DICT_KEY:      statGroups,
+               GROUPED_STATS_INFO_DICT_KEY:      groupedStats,
                SPATIAL_INFO_DICT_KEY:    spatial,
                IMAGE_NAME_INFO_DICT_KEY: imageNames,
                ATTRS_INFO_DICT_KEY:      variableAttrs,
diff --git a/pyglance/glance/variablereport.txt b/pyglance/glance/variablereport.txt
index 70f08a6..ffb0c65 100644
--- a/pyglance/glance/variablereport.txt
+++ b/pyglance/glance/variablereport.txt
@@ -117,13 +117,15 @@ Copyright (c) 2011 University of Wisconsin SSEC. All rights reserved.
     
     ## list out all of the statistics groups we have
 	<table>
-    % for setName in sorted(list(statGroups)) :
-        <% dataSet = statGroups[setName] %>
-            <tr><td><h4 colspan="2">${setName}</h4></td></tr>
+    % for setName in sorted(list(groupedStats)) :
+        <% dataSet = groupedStats[setName] %>
+            <tr><th class="l">${setName}</th><th>Both</th><th>File A</th><th>File B</th></tr>
                 % for statName, statValue in sorted(list(dataSet.items())) :
                     <tr>
                         <td class="n">${statName}<%block name="statDocLink"><a href="${runInfo[constants.DOCUMENTATION_PATH_KEY]}">*</a>:</%block></td>
-                        <td class="n">${report.make_formatted_display_string(statValue)}</td>
+                        <td class="n">${report.make_formatted_display_string(statValue['both'])}</td>
+                        <td class="n">${report.make_formatted_display_string(statValue['a'])}</td>
+                        <td class="n">${report.make_formatted_display_string(statValue['b'])}</td>
                     </tr>
                 % endfor
     % endfor
-- 
GitLab