From e61d252fa95c130f4887d142e7a43817f6454276 Mon Sep 17 00:00:00 2001
From: "(no author)" <(no author)@8a9318a1-56ba-4d59-b755-99d26321be01>
Date: Thu, 7 May 2009 20:24:50 +0000
Subject: [PATCH] added sdr_cris spectral comparison summary

git-svn-id: https://svn.ssec.wisc.edu/repos/glance/trunk@4 8a9318a1-56ba-4d59-b755-99d26321be01
---
 pyglance/glance/compare.py | 24 ++++++++++++++++++++
 pyglance/glance/io.py      | 29 +++++++++++++++++++++++-
 pyglance/glance/plot.py    | 45 ++++++++++++++++++++++++++++++++++++++
 pyglance/setup.py          |  2 +-
 4 files changed, 98 insertions(+), 2 deletions(-)

diff --git a/pyglance/glance/compare.py b/pyglance/glance/compare.py
index 6e5416e..a28de59 100644
--- a/pyglance/glance/compare.py
+++ b/pyglance/glance/compare.py
@@ -14,6 +14,7 @@ from pprint import pprint, pformat
 
 import glance.io as io
 import glance.delta as delta
+import glance.plot as plot
 
 LOG = logging.getLogger(__name__)
 
@@ -98,6 +99,29 @@ python -m glance.compare stats A.hdf B.hdf '.*_prof_retr_.*:1e-4' 'nwp_._index:0
             lal.sort()
             print fn + ': ' + ('\n  ' + ' '*len(fn)).join(lal)
     
+    def sdr_cris(*args):
+        """compare sdr_cris output
+        parameters are variable name followed by detector number
+        sdr_cris desired.h5 actual.h5 ESRealLW 0
+        """
+        afn,bfn = args[:2]
+        LOG.info("opening %s" % afn)
+        a = io.open(afn)
+        LOG.info("opening %s" % bfn)
+        b = io.open(bfn)
+        # shape is [scanline, field, detector, wnum]
+        vname = '/All_Data/CrIS-SDR_All/' + args[2]
+        det_idx = int(args[3])
+        def get(f):
+            spc = f[vname][:,:,det_idx,:]
+            nsl,nfor,nwn = spc.shape
+            return spc.reshape( (nsl*nfor,nwn) )
+        aspc = get(a)
+        bspc = get(b)
+        plot.compare_spectra(bspc,aspc)
+        plot.show()
+        
+    
     def stats(*args):
         """create statistics summary of variables
         Summarize difference statistics between listed variables.
diff --git a/pyglance/glance/io.py b/pyglance/glance/io.py
index 09db314..f0e0594 100644
--- a/pyglance/glance/io.py
+++ b/pyglance/glance/io.py
@@ -10,6 +10,10 @@ Copyright (c) 2009 University of Wisconsin SSEC. All rights reserved.
 import os, sys, logging
 
 from pyhdf.SD import SD,SDC
+try:
+    import h5py
+except ImportError:
+    pass
 
 LOG = logging.getLogger(__name__)
 
@@ -33,8 +37,31 @@ class hdf(SD):
         return getattr(self.select(name),'_FillValue',None)
         
 
+FIXME_IDPS = [ '/All_Data/CrIS-SDR_All/ES' + ri + band for ri in ['Real','Imag'] for band in ['LW','MW','SW'] ] 
+
+
 class h5(object):
-    pass
+    """wrapper for HDF5 datasets
+    """
+    _h5 = None
+    
+    def __init__(self,filename):
+        self._h5 = h5py.File(filename,'r')
+    
+    def __call__(self):
+        "FIXME: this should return the real list of variables, which will include slashes"
+        return set(FIXME_IDPS)
+    
+    @staticmethod
+    def trav(h5,pth): 
+        return reduce( lambda x,a: x[a] if a else x, pth.split('/'), h5)
+        
+    def __getitem__(self,name):
+        return h5.trav(self._h5, name)
+    
+    def missing_value(self, name):
+        return None
+        
 
 def open(pathname):
     cls = globals()[os.path.splitext(pathname)[1][1:]]
diff --git a/pyglance/glance/plot.py b/pyglance/glance/plot.py
index 2e98a0b..8ff9e46 100644
--- a/pyglance/glance/plot.py
+++ b/pyglance/glance/plot.py
@@ -8,11 +8,56 @@ Copyright (c) 2009 University of Wisconsin SSEC. All rights reserved.
 """
 
 import os, sys, logging
+from pylab import *
 
 LOG = logging.getLogger(__name__)
 
 
 
+def spectral_diff_plot( mean_diff, std_diff, max_diff, min_diff, acceptable_diff=None, x=None ):
+    """plot spectral difference in current axes, wrapped in std
+    >>> x = arange(0,9,0.1)
+    >>> y1 = sin(x)
+    >>> y2 = sin(x+0.05)
+    >>> d = y2-y1
+    >>> s = std(d)
+    >>> spectral_diff_plot( d, s, array([0.1] * len(d)), x=x )
+    """
+    cla()
+    if x is None: x = range(len(mean_diff))
+    if acceptable_diff is not None:
+        plot(x, acceptable_diff, 'g.', hold=True, alpha=0.2)
+        plot(x, -acceptable_diff, 'g.', hold=True, alpha=0.2)
+    plot(x, mean_diff+std_diff, 'r', hold=True, alpha=0.5)
+    plot(x,min_diff,'b')
+    plot(x,max_diff,'b')
+    plot(x, mean_diff-std_diff, 'r', hold=True, alpha=0.5)
+    plot(x, mean_diff, 'k', hold=True)
+
+def compare_spectra(actual, desired=None, acceptable=None, x=None):
+    """ given an actual[spectrum][wnum], desired[spectrum][wnum], plot comparisons of differences
+    """
+    delta = actual-desired if (desired is not None) else actual
+    d_mean = mean(delta,axis=0)
+    d_max = delta.max(axis=0)
+    d_min = delta.min(axis=0)
+    d_std = std(delta,axis=0)
+    des_mean = mean(desired,axis=0)
+    subplot(211)
+    cla()
+    if x is None: x = range(len(des_mean))
+    # plot(x,des_mean+d_max,'b')
+    # plot(x,des_mean+d_min,'b')
+    plot(x,des_mean,'k')
+    grid()
+    title("mean spectrum")
+    subplot(212)
+    spectral_diff_plot(d_mean, d_std, d_max, d_min, acceptable, x)
+    grid()
+    title("difference min-max (blue), mean (black), mean +/- std (red)")
+    
+    
+
 
 if __name__=='__main__':
     import doctest
diff --git a/pyglance/setup.py b/pyglance/setup.py
index 9bbd890..a58c9a7 100644
--- a/pyglance/setup.py
+++ b/pyglance/setup.py
@@ -18,7 +18,7 @@ easy_install -d $HOME/Library/Python -vi http://larch.ssec.wisc.edu/eggs/repos g
 from setuptools import setup, find_packages
 
 setup( name="glance", 
-       version="0.2.2", 
+       version="0.2.3", 
        zip_safe = True,
        entry_points = { 'console_scripts': [ 'glance = glance.compare:main' ] },
        packages = find_packages('.'),
-- 
GitLab