Index: /branches/sj_ippTests_branch_20080929/ippTests/compIPPphoto.py
===================================================================
--- /branches/sj_ippTests_branch_20080929/ippTests/compIPPphoto.py	(revision 20141)
+++ /branches/sj_ippTests_branch_20080929/ippTests/compIPPphoto.py	(revision 20142)
@@ -27,6 +27,4 @@
 #     + magdiff vs. sky
 #     + magdiff vs. skydiff
-#     + magdiff vs. stellar density
-#     + magdiff vs. presence of bright stars
 #     + something about PSF sizes; use Michigan moments for photo -
 #       M_rr_cc_psf is the size of the reconstructed PSF, and m_rr_cc is the size of the object
@@ -38,10 +36,27 @@
 #     + histograms of everything - recovery fractions, means, medians, quartiles
 #     + Trends with field number, seeing etc.
-
+#     + magdiff vs. stellar density
+#     + magdiff vs. presence of bright stars (e.g. number of bright
+#       stars above some typical S/N or counts threshold - 50%
+#       saturation?)
+#
+# XXX Todo:
+#
+# - Merge identical plots into single .eps file
+#
+# - Make html page with jpeg/gif versions of plots
+#
+# - Need mechanism to specify default plotting ranges, since outliers
+#   mess up axis ranges. Different quantities need different
+#   ranges. Comparing different plots is also easier if the plot
+#   ranges are the same.
+#
+# - second tlist for summary plots (same or different plot routine?)
+#
+# - towards generality, want to read these things and colname_hash and
+#   label_l from config files
+#
 # Convention: for scatter, plot second against first; for histogram, plot both histograms
-# 
-# XXX: Need mechanism to specify default plotting ranges, since
-# outliers mess up axis ranges. E.g. 0.1 and 0.9 percentiles?
-plotcol_tlist = [
+plotcol_1frame_tlist = [
     (['d_sky'],['d_mag'],'scatter')
     ,(['d_x'],['d_y'],'scatter')
@@ -56,5 +71,14 @@
     ]
 
-def compIPPphoto(summaryTable,mode,plotcol_tlist=plotcol_tlist,skip=False):
+plotcol_summary_tlist = [
+    (['d_mag_median'],['d_sky_median'],'scatter')
+    ,(['d_x_median'],['d_y_median'],'scatter')
+    ,(['d_mag_median'],['N_SDSS'],'scatter')
+    ,(['d_mag_median','d_mag_mean'],['field','field'],'scatter')
+    ]
+
+
+def compIPPphoto(summaryTable,mode,plotcol_1frame_tlist=plotcol_1frame_tlist,\
+                     plotcol_summary_tlist=plotcol_summary_tlist,skip=True):
     """summaryTable: .fits table for output
     mode: new or append for creating summaryTable new or appending current run's output to it.
@@ -93,5 +117,5 @@
         vallist,keylist = valuesKeysSortedByKeys(stats_hash)
         rowtuple_list.append(vallist)
-        plotStatsOnefile(column_hash,goodrow_hash,matchtable,plotcol_tlist,bandindex)
+        plotStatsOnefile(column_hash,goodrow_hash,matchtable,plotcol_1frame_tlist,bandindex)
         # return column_hash,goodrow_hash
     newrows = numpy.rec.array(rowtuple_list,names=keylist)
@@ -99,7 +123,24 @@
     if mode == 'new':
         tabhdu.writeto(summaryTable,clobber=True)
+        nrows = tabhdu.header['NAXIS2']
+        summaryhash = hashFromPyfitsTable(tabhdu)
     else:
-        appendFitsTable(summaryTable,tabhdu)
+        newtabhdu = appendFitsTable(summaryTable,tabhdu)
+        nrows = newtabhdu.header['NAXIS2']
+        summaryhash = hashFromPyfitsTable(newtabhdu)
+    # Fake a goodrows hash
+    sumgoodrows_hash = summaryhash.fromkeys(summaryhash.keys(),numpy.ones((nrows,),bool))
+    plotStatsOnefile(summaryhash,sumgoodrows_hash,summaryTable,plotcol_summary_tlist,bandindex)
     return column_hash,goodrow_hash
+
+def hashFromPyfitsTable(pyfitsTableHDU):
+    """Return a hash wrapping the table data so a column can be
+    addressed as hash[colname] instead of tabledata.field(colname)"""
+    from numpy import array
+    outhash = {}
+    for col in pyfitsTableHDU.columns.names:
+        # Column names are uppercase, we want lowercase hash keys
+        outhash[col.lower()] = array(pyfitsTableHDU.data.field(col))
+    return outhash
 
 def getOutnameStatsOnefile(matchtable,kind,col1_l,col2_l=None,format='eps'):
@@ -124,4 +165,6 @@
     histo_min_ntile = 0.03
     histo_max_ntile = 1-histo_min_ntile
+    angle_l = [0,45,0,45,0,90] 
+    ptype_l = [ 41, 41, 40, 40, 30,30]
     for troika in plotcol_tlist:
         col1name_l = troika[0]
@@ -131,9 +174,10 @@
         smOpenPlot(outname,format=format)
         firstplot = True
+        i = 0
         for col1name,col2name in zip(col1name_l,col2name_l):
-            values1 = values_hash[col1name]
-            goodrows1 = goodrow_hash[col1name]
-            values2 = values_hash[col2name]
-            goodrows2 = goodrow_hash[col2name]
+            values1 = values_hash[col1name.lower()]
+            goodrows1 = goodrow_hash[col1name.lower()]
+            values2 = values_hash[col2name.lower()]
+            goodrows2 = goodrow_hash[col2name.lower()]
             # Slice out depth for current filter if necessary
             if len(values1.shape) > 1:
@@ -150,6 +194,9 @@
                     # Avoid plotting outliers
                     if not re.search('objc',col2name):
-                        [xmin,xmax] = stats_med(values1[goodrows],[histo_min_ntile,histo_max_ntile])
-                        [ymin,ymax] = stats_med(values2[goodrows],[histo_min_ntile,histo_max_ntile])
+                        try:
+                            [xmin,xmax] = stats_med(values1[goodrows],[histo_min_ntile,histo_max_ntile])
+                            [ymin,ymax] = stats_med(values2[goodrows],[histo_min_ntile,histo_max_ntile])
+                        except IndexError:
+                            print col1name, goodrows, type(goodrows), len(goodrows), len(values1)
                         # print "Huhu", outname, min(values1),max(values2),xmin,xmax
                         smScatterPlot(values1,values2,logical=goodrows,xlab=col1name,ylab=col2name,\
@@ -159,6 +206,6 @@
                         smScatterPlot(values1,values2,logical=goodrows,xlab=col1name,ylab=col2name)
                 else:
-                    angle(45)
-                    smScatterPlot(values1,values2,logical=goodrows,append=True)
+                    angle(angle_l[i%len(angle_l)])
+                    smScatterPlot(values1,values2,ptype=ptype_l[i%len(ptype_l)],logical=goodrows,append=True)
                     angle(0)
             if plottype == 'histogram':
@@ -177,4 +224,5 @@
                             ltype=2)
             firstplot = False
+            i+=1
     smClosePlot()
 
@@ -196,5 +244,6 @@
         newtabhdu.data.field(col)[nrows_old:] = tabHDU.data.field(col)
     oldtabhdulist.close()
-    return newtabhdu.writeto(fitsfile,clobber=True)
+    newtabhdu.writeto(fitsfile,clobber=True)
+    return newtabhdu
 
         
@@ -247,5 +296,6 @@
     outl = []
     for ntile in ntiles:
-        outl.append(sortarr[ntile*(l-1)])
+        idx = max(int(ntile*(l-1)),0)
+        outl.append(sortarr[idx])
     return outl
 
