Index: /branches/sj_ippTests_branch_20080929/ippTests/compIPPphoto.py
===================================================================
--- /branches/sj_ippTests_branch_20080929/ippTests/compIPPphoto.py	(revision 20471)
+++ /branches/sj_ippTests_branch_20080929/ippTests/compIPPphoto.py	(revision 20472)
@@ -64,16 +64,16 @@
 
 plotcol_1frame_tlist = [
-    (['d_sky'],['d_mag'],'scatter')
+    (['x_psf','objc_colc'],['y_psf','objc_rowc'],'scatter')
+    ,(['psfinstmag_sdss'],['d_mag'],'scatter')
+    ,(['psfinstmag_sdss'],['d_sky'],'scatter')
     ,(['d_x'],['d_y'],'scatter')
     ,(['sky_ps1'],['d_mag'],'scatter')
-    ,(['psfinstmag_sdss'],['d_mag'],'scatter')
-    ,(['psfinstmag_sdss'],['d_sky'],'scatter')
+    ,(['d_sky'],['d_mag'],'scatter')
+    ,(['x_psf'],['objc_colc'],'histogram')
     ,(['m_rr_cc_psf'],['d_mag'],'scatter')
     ,(['sky_sdss'],['sky_ps1'],'scatter')
+    ,(['y_psf'],['objc_rowc'],'histogram')
+    ,(['sky_sdss'],['sky_ps1'],'histogram')
     ,(['psfinstmag_sdss'],['psf_inst_mag'],'histogram')
-    ,(['sky_sdss'],['sky_ps1'],'histogram')
-    ,(['x_psf'],['objc_colc'],'histogram')
-    ,(['y_psf'],['objc_rowc'],'histogram')
-    ,(['x_psf','objc_colc'],['y_psf','objc_rowc'],'scatter')
     ]
 
@@ -81,13 +81,17 @@
     (['d_mag_median'],['d_sky_median'],'scatter')
     ,(['d_x_median'],['d_y_median'],'scatter')
+    ,(['d_x_median'],['d_y_median'],'histogram')
     ,(['d_mag_median'],['N_SDSS'],'scatter')
+    ,(['N_PS1'],['N_SDSS'],'scatter')
     ,(['field','field'],['d_mag_median','d_mag_mean'],'scatter')
     ]
 
-def mergePlots(plotcol_1frame_tlist=plotcol_1frame_tlist,filtlist=['u','g','r','i','z'],workdir='/IPP/data/SDSS/stripe82/coadd/compare'):
+def mergePlots(summaryTable,plotcol_1frame_tlist=plotcol_1frame_tlist,filtlist=['u','g','r','i','z'],workdir='/IPP/data/SDSS/stripe82/coadd/compare'):
     from subprocess import call
     from glob import glob
+    import re
     import os
     os.chdir(workdir)
+    outroot = re.sub('(\.[sc]mf|\.fits?)$','',summaryTable)
     for tup in plotcol_1frame_tlist:
         t1,t2,t3=tup
@@ -95,9 +99,10 @@
             globstr = getOutnameStatsOnefile("match_%s_*"%(filt),t3,t1,t2)
             filelist = glob(globstr)
-            outname = getOutnameStatsOnefile("merged_%s" %(filt),t3,t1,t2)
+            outname = getOutnameStatsOnefile("merged_%s_%s" %(outroot,filt),t3,t1,t2)
             cmdstr = "gs -dNOPAUSE -sDEVICE=pswrite -sOutputFile=%s %s -c quit" %(outname,' '.join(filelist))
             print "Making %s" %(outname)
             call(cmdstr,shell=True)
-            call("gzip %s" %(outname),shell=True)
+            print "gzipping %s" %(outname)
+            call("gzip -f %s" %(outname),shell=True)
 
 def compIPPphoto(summaryTable,mode,plotcol_1frame_tlist=plotcol_1frame_tlist,\
@@ -144,5 +149,5 @@
         vallist,keylist = valuesKeysSortedByKeys(stats_hash)
         rowtuple_list.append(vallist)
-        plotStatsOnefile(column_hash,goodrow_hash,matchtable,plotcol_1frame_tlist,bandindex)
+        plotStatsOnefile(column_hash,goodrow_hash,matchtable,plotcol_1frame_tlist,bandindex,bandid=filter_name)
         # return column_hash,goodrow_hash
     newrows = numpy.rec.array(rowtuple_list,names=keylist)
@@ -159,6 +164,7 @@
     sumgoodrows_hash = summaryhash.fromkeys(summaryhash.keys(),numpy.ones((nrows,),bool))
     for filt in filters:
+        print bandindex_hash[filt], filt
         plotStatsOnefile(summaryhash,sumgoodrows_hash,summaryTable,plotcol_summary_tlist,bandindex_hash[filt],bandid=filt)
-    mergePlots(plotcol_1frame_tlist=plotcol_1frame_tlist,filtlist=filters)
+    mergePlots(summaryTable,plotcol_1frame_tlist=plotcol_1frame_tlist,filtlist=filters)
     return column_hash,goodrow_hash
 
@@ -175,9 +181,9 @@
     return outhash
 
-def getOutnameStatsOnefile(matchtable,kind,col1_l,col2_l=None,format='eps',bandid=''):
+def getOutnameStatsOnefile(matchtable,kind,col1_l,col2_l=None,format='eps',bandid='',useBandid=False):
     import re
     # Construct output filename
     root = re.sub('(\.[sc]mf|\.fits?)$','',matchtable)
-    if bandid != '':
+    if useBandid and bandid != '':
         root = "%s_%s" %(root,bandid)
     if isNone(col2_l):
@@ -191,6 +197,7 @@
     
 def plotStatsOnefile(values_hash,goodrow_hash,matchtable,plotcol_tlist,bandindex,format='eps',bandid=''):
-    """Make diagnostic plots for a single table, based on values
-    in values_hash"""
+    """Make diagnostic plots for a single table, based on values in
+    values_hash"""
+    from subprocess import call
     from numpy import concatenate
     from sm import cvar,angle
@@ -201,4 +208,6 @@
     angle_l = [0,45,0,45,0,90] 
     ptype_l = [ 41, 41, 40, 40, 30,30]
+    outname_l = []
+    nplots = 0
     for troika in plotcol_tlist:
         col1name_l = troika[0]
@@ -206,4 +215,5 @@
         plottype = troika[2]
         outname = getOutnameStatsOnefile(matchtable,plottype,col1name_l,col2name_l,format=format,bandid=bandid)
+        outname_l.append(outname)
         smOpenPlot(outname,format=format)
         firstplot = True
@@ -259,5 +269,17 @@
             firstplot = False
             i+=1
-    smClosePlot()
+        smClosePlot()
+        nplots += 1
+    all_outname = "nup_" + re.sub('(\.[sc]mf|\.fits?)$','',matchtable) +".eps"
+    tmp_outname = "tmp_" + re.sub('(\.[sc]mf|\.fits?)$','',matchtable) +".eps"
+    print "creating ", all_outname
+    mergecmd = "gs -dNOPAUSE -sDEVICE=pswrite -sOutputFile=%s %s -c quit" %(tmp_outname,' '.join(outname_l))
+    call(mergecmd,shell=True)
+    psnupcmd = "psnup -pletter -%i %s %s"%(nplots,tmp_outname,all_outname)
+    print "calling ", psnupcmd
+    call(psnupcmd,shell=True)
+    os.remove(tmp_outname)
+    return all_outname
+        
 
 def valuesKeysSortedByKeys(hash):
@@ -513,5 +535,5 @@
     return array(good1_l),array(good2_l),array(good3_l)
     
-def makePlan(lastcamcol=6):
+def makePlan(lastcamcol=1):
     """Make paired list of which fpObjc file to compare to which IPP
     .cmf file. Could also think about constructing the fpObjc filename
@@ -598,8 +620,11 @@
     # Filtering out objects that can't be primary, i.e.
     # !BRIGHT && (!BLENDED || NODEBLEND || nchild == 0)
-    sdssfilt += ';select !((flags[%s]&1<<1)!=0)&&(!((flags[%s]&1<<3)!=0)||((flags[%s]&1<<6)!=0)||nchild==0)' % \
-        (bandindex,bandindex,bandindex)
-    # add check for BINNED1 !!! bit28 in flags1
-    
+    # Get flag values by left-shifting a 1 by the correct number of bits. Bit N needs to be left-shifted N times.
+    bright = 1<<1
+    blended = 1<<3
+    nodeblend = 1<<6
+    binned1 = 1<<28
+    sdssfilt += ';select ((flags[%s]&%s)==0)&&(!((flags[%s]&%s)!=0)||((flags[%s]&%s)!=0)||nchild==0)&&((flags[%s]&%s)!=0)' % \
+        (bandindex,bright,bandindex,blended,bandindex,nodeblend,bandindex,binned1)
     # Check flag 0x4000 (extended) to compare against prob_psf in SDSS
     # - for some reason, 'flags' and the other integer columns get
