diff --git a/pyglance/glance/compare.py b/pyglance/glance/compare.py index 6e5416e052cfeed6ab0ae5f8a46a075cb012344c..a28de59ffa3093c3d82047ab7c2c22fe09f9406f 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 09db3140b16069736f1939616ff9b45a0e36a9e9..f0e0594d21047d5622582febda3c6f37f97b48ed 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 2e98a0b0a20cceb86bae555a914f6bfc51a3d7e5..8ff9e461a6688c49838a469d82722467778f9963 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 9bbd890872f67c9ccdb66844704b155c95c66915..a58c9a7afe7bc51b732f1550231c53440c115d2c 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('.'),