Exemplo n.º 1
0
from config_bonn import appendix, cluster, tag, arc, filters, filter_root, appendix_root

cluster = sys.argv[1] #'MACS0717+37'
path='/nfs/slac/g/ki/ki05/anja/SUBARU/%s/' % cluster

plot_res('./OUTPUT/photz.zout','./OUTPUT/')

type='all'

filecommand = open('record.analysis','w')

BASE="coadd"
image = BASE + '.fits'

images = []
filters.reverse()
ims = {}
ims_seg = {}
for filter in filters[0:3]:
    params = {'path':path, 
              'filter_root': filter_root, 
              'cluster':cluster, 
              'filter':filter,
              'appendix':appendix, }
    print params
    # now run sextractor to determine the seeing:              
    image = '%(path)s/%(filter)s/SCIENCE/coadd_%(cluster)s%(appendix)s/coadd.fits' %params 
    images.append(image)
    print 'reading in ' + image        
    seg_image = '/%(path)s/%(filter)s/PHOTOMETRY/coadd.apertures.fits' % params
    ims[filter] = pyfits.open(image)[0].data
Exemplo n.º 2
0
def run(cluster):
    import pyfits
    import os
    import os, sys, bashreader, commands
    from utilities import *
    from config_bonn import appendix, tag, arc, filters, filter_root, appendix_root

    print cluster
    
    path='/nfs/slac/g/ki/ki05/anja/SUBARU/%s/' % cluster
    
    type='all'
    
    filecommand = open('record.analysis','w')
    
    BASE="coadd"
    image = BASE + '.fits'

    from glob import glob
    
    images = []
    filters.reverse()
    print filters
    ims = {}
    ims_seg = {}

    params = {'path':path, 
              'filter_root': filter_root, 
              'cluster':cluster, 
              'appendix':appendix, }


    catalog = '%(path)s/PHOTOMETRY/%(cluster)s.slr.cat' %params           
    starcatalog = '%(path)s/PHOTOMETRY/%(cluster)s.stars.calibrated.cat' %params           
    import do_multiple_photoz
    filterlist = do_multiple_photoz.get_filters(catalog,'OBJECTS')
    print filterlist
    mkcolorcolor(filterlist,catalog,starcatalog,cluster)





    ffile = os.environ['sne'] + '/photoz/' + cluster + '/all.tif'
    if not glob(ffile):
        for filter in filters[0:3]:                                                                 
            params = {'path':path, 
                      'filter_root': filter_root, 
                      'cluster':cluster, 
                      'filter':filter,
                      'appendix':appendix, }
            print params
            # now run sextractor to determine the seeing:              
            image = '%(path)s/%(filter)s/SCIENCE/coadd_%(cluster)s%(appendix)s/coadd.fits' %params 
            images.append(image)
            print 'reading in ' + image        
            seg_image = '/%(path)s/%(filter)s/PHOTOMETRY/coadd.apertures.fits' % params
            ims[filter] = pyfits.open(image)[0].data
            print 'read in ' + image        
            #ims_seg[filter] = pyfits.open(seg_image)[0].data
        
        os.system('mkdir ' + os.environ['sne'] + '/photoz/' + cluster + '/')
        os.system('chmod o+rx ' + os.environ['sne'] + '/photoz/' + cluster + '/')
        
        from glob import glob
        os.system('stiff ' + reduce(lambda x,y:x + ' ' + y,images) + ' -OUTFILE_NAME ' + ffile + ' -BINNING 1 -GAMMA_FAC 1.6')
        os.system('convert ' + os.environ['sne'] + '/photoz/' + cluster + '/all.tif ' + os.environ['sne'] + '/photoz/' + cluster + '/fix.tif')
    
    print ffile
    
    import Image
    im = Image.open(os.environ['sne'] + '/photoz/' + cluster + '/fix.tif')
    
    
    
    
    
    
    

    print catalog
    p_cat = pyfits.open(catalog)[1].data
    
    outputcat = '%(path)s/PHOTOMETRY/%(cluster)s.1.spec.bpz.tab' % params 
    
    #outputcat = '%(path)s/PHOTOMETRY/all_bpz1_' % params 
    
    if glob(outputcat.replace('.tab','')):
        plot_res(outputcat.replace('.tab',''),os.environ['sne'] + '/photoz/' + cluster + '/')
    print outputcat
    
    
    spec = True 
    
    if not spec:
        outputcat = '%(path)s/PHOTOMETRY/%(cluster)s.1.all.bpz.tab' % params 
    
    print outputcat
    bpz_cat = pyfits.open(outputcat)[1].data
    
   
    print outputcat
 
    gals = [] 
    
    for i in range(len(bpz_cat)): #[75227,45311,53658, 52685, 64726]:
        print i
        line = bpz_cat[i]
        print line
        text = ''
    
    #for x,y,side,index in [[4800,4900,200,1],[5500,5500,100,2],[4500,5500,300,2]]:
    
        text += '<tr>\n'
        SeqNr = int(line['SeqNr'])
        fileNumber = str(int(line['BPZ_NUMBER']))
        params['fileNumber'] = str(fileNumber)
    
        if spec: 
            base = '%(path)s/PHOTOMETRY/%(cluster)s.1.spec' % params
            probs =  '%(path)s/PHOTOMETRY/all.spec.cat.bpz1.probs' % params
    
            #base = '%(path)s/PHOTOMETRY/specsave.cat' % params
            #probs =  '%(path)s/PHOTOMETRY/all.spec.cat.bpz1.probs' % params
    
        else:
            base = '%(path)s/PHOTOMETRY/all_bpz1_%(fileNumber)s' % params
            probs =  '%(path)s/PHOTOMETRY/all_bpz1_%(fileNumber)s.probs' % params
        print probs
        probs_f = open(probs,'r').readlines()
    
        resid = line['BPZ_Z_B'] - line['BPZ_Z_S']
    
        ODDS = line['BPZ_ODDS']
    
        if True: #0.38 < line['BPZ_Z_B'] < 0.42: #abs(resid) > 0.15: # and ODDS > 0.95:        
    
            mask = p_cat.field('SeqNr') == SeqNr                                                                                                   
            temp = p_cat[mask]
            x = int(temp.field('Xpos')[0])
            y = 10000 - int(temp.field('Ypos')[0])
    
            x_fits = int(temp.field('Xpos')[0])
            y_fits = int(temp.field('Ypos')[0])
    
            import pyraf
            from pyraf import iraf
    
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
    
            
    
            
    
    
    
           
            if line['BPZ_Z_S'] != 0: 
                resid = line['BPZ_Z_B'] - line['BPZ_Z_S']
                if resid > 0: 
                    color='green'
                    resid_str = ' <font color=' + color + '>+' + str(resid) + '</font> '
                else: 
                    color='red'        
                    resid_str = ' <font color=' + color + '>' + str(resid) + '</font> '
            else:
                resid = 'no spec z' 
                color = 'black'
                resid_str = ' <font color=' + color + '>' + str(resid) + '</font> '
    
            
            t = ['BPZ_Z_B','BPZ_ODDS','BPZ_CHI-SQUARED']
            text += '<td colspan=10>' + str(SeqNr) + ' z=' + str(line['BPZ_Z_B']) + resid_str  + ' ODDS=' + str(line['BPZ_ODDS']) + ' TYPE=' + str(line['BPZ_T_B']) + ' CHISQ=' + str(line['BPZ_CHI-SQUARED']) + ' x=' + str(x) + ' y=' + str(y) + '</td></tr><tr>\n'
            
            print x,y
                                                                                                                                                   
            index = SeqNr
                                                                                                                                                   
            outfile = os.environ['sne'] + '/photoz/' + cluster + '/' + str(index) + '.png' 
            plot_bpz_probs(index, probs_f, outfile)
                                                                                                                                                   
            #file = 'Id%09.f.spec' % index
            #outfile = os.environ['sne'] + '/photoz/' + str(index) + '.png' 
            #print outfile
            #plot(file,outfile)
            
            text += '<td align=left><img height=400px src=' + str(index)  + '.png></img>\n'
            text += '<img height=400px src=' + str(index)  + 'spec.png></img>\n'
            images = []
    
            file = '/nfs/slac/g/ki/ki04/pkelly/photoz/' + cluster + '/' +  str(SeqNr) + 'spec.png' #print 'SAVING', outdir + outimg
            command = 'python ' + os.environ['BPZPATH'] + '/plots/sedplot.py ' + base + ' ' + str(SeqNr) + ' ' + file
            print command
    
            import sedplot
    
            sys.argv = ['',base,str(SeqNr),file]
            filt_info = sedplot.run()
            print filt_info 
    
            
            #print command
            #os.system(command)

            try:
                p = im.crop([x-side,y-side,x+side,y+side])
                image = os.environ['sne'] + '/photoz/' + cluster + '/' + str(index) + '.jpg'  
                os.system('rm ' + image)
                p.save(image,'JPEG')
            except: print 'fail'
            text += '<img src=' + str(index)  + '.jpg></img></td><td colspan=20></td>\n'
            text += '</tr>\n'
            keys = ['name','wavelength','observed','flux','fluxerror','expectedflux','chioffset']
            text += '<tr><td colspan=10><table><tr>' + reduce(lambda x,y: x + y,['<td>'+n+'</td>' for n in keys]) + "</tr>"
            for f in filt_info:
                text += '<tr>' + reduce(lambda x,y: x + y,['<td>'+str(f[n])+'</td>' for n in keys]) + "</tr>"
            text += "</table></td></tr>"
    
            side = 100 #x = temp.field('Xpos')[0]
            bounds = '[' + str(int(x_fits-side)) + ':' + str(int(x_fits+side)) + ',' + str(int(y_fits-side)) + ':' + str(int(y_fits+side)) + ']'
            text += '<td colspan=20><table>\n'
            textim = ''
            textlabel = ''
    
            import string
    
            filters_b = []
    
            name_t = None
            for f in filt_info:
                print f['name']
                if string.find(f['name'],'MEGAPRIME') != -1:
                    n = f['name'][-1]
                if string.find(f['name'],'SUBARU') != -1:
                    import re
                    res = re.split('-W',f['name'])
                    print res, f['name']
                    n = 'W' + res[1]
                if n != name_t:
                    filters_b.append(n)
                    name_t = n
    
    
            for filter in filters_b[1:]:
                fitsfile = '/nfs/slac/g/ki/ki04/pkelly/photoz/' + cluster + '/' +  str(SeqNr) + 'cutout' + filter + '.fits' #print 'SAVING', outdir + outimg  
                jpg = '/nfs/slac/g/ki/ki04/pkelly/photoz/' + cluster + '/' +  str(SeqNr) + 'cutout' + filter + '.jpg' #print 'SAVING', outdir + outimg
                os.system('rm ' + fitsfile)
                os.system('rm ' + jpg)
                bigfile = '/a/wain023/g.ki.ki05/anja/SUBARU/' + cluster + '/' + filter + '/SCIENCE/coadd_' + cluster + '_all/coadd.fits'
                iraf.imcopy(bigfile + bounds,fitsfile)
                import commands
                seeing = commands.getoutput('gethead ' + bigfile + ' SEEING')
                print 'seeing', seeing 


                import os
                #os.system("ds9 " + fitsfile + " -view info no -view panner no -view magnifier no -view buttons no -view colorbar yes -view horzgraph no -view wcs no -view detector no -view amplifier no -view physical no -zoom to fit -minmax -histequ  -invert -zoom to fit -saveas jpeg " + jpg ) # -quit >& /dev/null &")        
                #os.system("xpaset -p ds9 " + fitsfile + "  -zoom to fit -view colorbar no -minmax -histequ  -invert -zoom to fit -saveas jpeg " + jpg  + " ") # -quit >& /dev/null &")        
    
                com = ['file ' + fitsfile, 'zoom to fit', 'view colorbar no', 'minmax', 'scale histequ' , 'cmap invert', 'saveimage jpeg ' + jpg] # -quit >& /dev/null &")        
                for c in com:
                    z = 'xpaset -p ds9 ' + c
                    print z
                    os.system(z)
                print jpg
                text += '<td><img height=200px src=' + str(SeqNr)  + 'cutout' + filter + '.jpg></img></td>\n'
                textlabel += '<td>' + filter + ' seeing ' + seeing + '</td>\n'
                                                                                                                                                                                                                                                                                                                                         
            text += '<tr>' + textim +  '</tr><tr>' +  textlabel + '</tr></table></td></tr><tr>'
    
    
    
    
    
    
    
    
    
    
    
            #os.system('stiff ' + image_seg + ' -OUTFILE_NAME ' + image_seg.replace('fits','tif'))
            gals.append([line['BPZ_T_B'],text])
            page = open(os.environ['sne'] + '/photoz/' + cluster + '/index.html','w')
            t = '<head><link href="http://www.slac.stanford.edu/~pkelly/photoz/table.css" rel="stylesheet" type="text/css"></head>' 
            t += '<table align=left><tr><td colspan=5 class="dark"><h1>' + cluster + '</h1></td></tr><tr><td colspan=5><a href=stars.html>Stellar Color-Color Plots</a><td></tr>\n'
            t += '<tr><td colspan=10 align=left><img height=400px src=RedshiftErrors.png></img>\n'
            t += '<img height=400px src=RedshiftScatter04.png></img>\n'
            t += '<img height=400px src=RedshiftScatter01.png></img></td></tr>\n'
            page.write(t)
            gals.sort()
            for gal in gals:
                page.write(gal[1])
            page.write('<table>\n')
            page.close()
    
            if len(gals) > 100:
                break
    
    
    
    ''' make SED plots '''
Exemplo n.º 3
0
def run(cluster):

    ratio = []
    import pyfits, cutout_bpz
    import os
    import os, sys, bashreader, commands
    from utilities import *
    from config_bonn import appendix, tag, arc, filters, filter_root, appendix_root

    type='all'
    magtype = 'APER'
    if len(sys.argv) > 2:
        for s in sys.argv:
            if s == 'spec':
                type = 'spec' 
            elif s == 'rand': 
                type = 'rand'
            elif s == 'all': 
                type = 'all'
            elif s == 'picks': 
                type = 'picks'
            elif s == 'ISO': 
                magtype = 'ISO'
            elif s == 'APER': 
                magtype = 'APER'

    print cluster
    
    path='/nfs/slac/g/ki/ki05/anja/SUBARU/%s/' % cluster
    
    
    filecommand = open('record.analysis','w')
    
    BASE="coadd"
    image = BASE + '.fits'

    from glob import glob
    
    images = []
    filters.reverse()
    print filters
    ims = {}
    ims_seg = {}

    params = {'path':path, 
              'filter_root': filter_root, 
              'cluster':cluster, 
              'appendix':appendix, }


    catalog = '%(path)s/PHOTOMETRY/%(cluster)s.slr.cat' %params           
    starcatalog = '%(path)s/PHOTOMETRY/%(cluster)s.stars.calibrated.cat' %params           
    save_file = '%(path)s/PHOTOMETRY/pat_slr.calib' %params           
    import do_multiple_photoz, os
    reload(do_multiple_photoz)
    filterlist = do_multiple_photoz.get_filters(catalog,'OBJECTS')
    print filterlist

    filters = cutout_bpz.conv_filt(filterlist)
    y = {} 
    for f in filters: 
        y[f] = 'yes'
    filters = y.keys()
    filters.sort(cutout_bpz.sort_filters)

    print filters

    os.system('mkdir -p ' + os.environ['sne'] + '/photoz/' + cluster)           
    mkcolorcolor(filterlist,catalog,starcatalog,cluster,magtype,save_file)
Exemplo n.º 4
0
def run(cluster):

    ratio = []
    import pyfits
    import os
    import os, sys, bashreader, commands
    from config_bonn import appendix, tag, arc, filters, filter_root, appendix_root

    type = 'all'
    AP_TYPE = ''
    magtype = 'APER1'
    DETECT_FILTER = ''
    SPECTRA = 'CWWSB_capak.list'
    LIST = None
    if len(sys.argv) > 2:
        for s in sys.argv:
            if s == 'spec':
                type = 'spec'
            elif s == 'rand':
                type = 'rand'
            elif s == 'all':
                type = 'all'
            elif s == 'picks':
                type = 'picks'
            elif s == 'ISO':
                magtype = 'ISO'
            elif s == 'APER1':
                magtype = 'APER1'

            import string
            if string.find(s, 'detect') != -1:
                import re
                rs = re.split('=', s)
                DETECT_FILTER = '_' + rs[1]
            if string.find(s, 'aptype') != -1:
                import re
                rs = re.split('=', s)
                AP_TYPE = '_' + rs[1]
            if string.find(s, 'spectra') != -1:
                import re
                rs = re.split('=', s)
                SPECTRA = rs[1]
            if string.find(s, 'spectra') != -1:
                import re
                rs = re.split('=', s)
                SPECTRA = rs[1]
            if string.find(s, 'list') != -1:
                import re
                rs = re.split('=', s)
                LIST = rs[1]

    print DETECT_FILTER

    print cluster

    path = '/nfs/slac/g/ki/ki05/anja/SUBARU/%s/' % cluster

    filecommand = open('record.analysis', 'w')

    BASE = "coadd"
    image = BASE + '.fits'

    from glob import glob

    images = []
    filters.reverse()
    print filters
    ims = {}
    ims_seg = {}

    params = {
        'path': path,
        'filter_root': filter_root,
        'cluster': cluster,
        'appendix': appendix,
        'DETECT_FILTER': DETECT_FILTER,
        'AP_TYPE': AP_TYPE
    }

    params['SPECTRA'] = SPECTRA
    params['type'] = type
    params['magtype'] = magtype
    outputcat = '%(path)s/PHOTOMETRY%(DETECT_FILTER)s%(AP_TYPE)s/%(cluster)s.%(magtype)s.1.%(SPECTRA)s.%(type)s.bpz.tab' % params

    catalog = '%(path)s/PHOTOMETRY%(DETECT_FILTER)s%(AP_TYPE)s/%(cluster)s.slr.cat' % params
    starcatalog = '%(path)s/PHOTOMETRY%(DETECT_FILTER)s%(AP_TYPE)s/%(cluster)s.stars.calibrated.cat' % params
    import do_multiple_photoz, os
    filterlist = do_multiple_photoz.get_filters(catalog, 'OBJECTS')
    print filterlist

    filters = conv_filt(filterlist)
    y = {}
    for f in filters:
        y[f] = 'yes'
    filters = y.keys()
    filters.sort(sort_filters)
    print filters

    stars_dict = star_num(filterlist,
                          catalog,
                          starcatalog,
                          cluster,
                          'APER1',
                          name_suffix='')

    os.system('mkdir -p ' + os.environ['sne'] + '/photoz/' + cluster + '/' +
              SPECTRA + '/')

    if False:

        pagemain = open(
            os.environ['sne'] + '/photoz/' + cluster + '/' + SPECTRA +
            '/index.html', 'w')
        pagemain.write(
            '<table align=left><tr><td colspan=5 class="dark"><h1>' + cluster +
            '</h1></td></tr><tr><td colspan=5><a href=http://www.slac.stanford.edu/~pkelly/photoz/'
            + cluster +
            '/stars.html>Stellar Color-Color Plots</a><td></tr><tr><td colspan=5><a href=redsequence.html>Red Sequence Redshifts</a><td></tr><tr><td><a href=objects.html>Photoz Plots</a><td></tr><tr><td><a href=thecorrections.html>Correction Plots</a><td></tr><tr><td><a href=zdistribution.html>Z Distribution</a><td></tr></table>\n'
        )
        pagemain.close()
        import pyfits, pylab, scipy
        p = pyfits.open(outputcat)['STDTAB'].data
        pylab.clf()
        pylab.hist(p.field('BPZ_ODDS'), bins=scipy.arange(0, 1, 0.02))
        pylab.xlabel('ODDS')
        pylab.ylabel('Number of Galaxies')
        pylab.savefig(os.environ['sne'] + '/photoz/' + cluster + '/' +
                      SPECTRA + '/odds.png')
        cut = 0.5
        p = p[p.field('BPZ_ODDS') > cut]
        pylab.clf()
        pylab.hist(p.field('BPZ_Z_B'), bins=scipy.arange(0, 4, 0.02))
        pylab.xlabel('Photometric Redshift')
        pylab.ylabel('Number of Galaxies (ODDS > ' + str(cut) + ')')
        pylab.savefig(os.environ['sne'] + '/photoz/' + cluster + '/' +
                      SPECTRA + '/zdist.png')
        pagemain = open(
            os.environ['sne'] + '/photoz/' + cluster + '/' + SPECTRA +
            '/zdistribution.html', 'w')
        pagemain.write('<img src=zdist.png></a>')
        pagemain.write('<img src=odds.png></a>')
        pagemain.close()
        pylab.clf()

        import flux_comp
        corrections, plot_dict, title_dict = reload(flux_comp).calc_comp(
            cluster,
            DETECT_FILTER.replace('_', ''),
            AP_TYPE,
            SPECTRA,
            type='all',
            plot=True)
        print corrections
        pagemain = open(
            os.environ['sne'] + '/photoz/' + cluster + '/' + SPECTRA +
            '/thecorrections.html', 'w')

        def s(a, b):
            if corrections[a] < corrections[b]:
                return 1
            else:
                return -1

        keys2 = corrections.keys()
        keys2.sort(s)

        import scipy
        from scipy import stats
        kernel = scipy.array(
            [scipy.stats.norm.pdf(i, 1.) for i in [4, 3, 2, 1, 0, 1, 2, 3, 4]])
        kernel = kernel / kernel.sum()

        pagemain.write('<br>CORRECTIONS<br><ul>')
        ims = ''
        nums = '<br><br>NUMBER OF GOOD STARS<br><ul>\n'
        for key in keys2:
            if key != 'ID' and key != 'Z_S':
                file = os.environ[
                    'sne'] + '/photoz/' + cluster + '/' + SPECTRA + '/' + key + '.png'
                pylab.clf()
                o = pylab.hist(plot_dict[key], bins=100, range=[0.5, 1.5])
                y_smooth = scipy.convolve(o[0], kernel, 'same')  #[5:-5]
                print o[1], y_smooth, len(o[1]), len(y_smooth)
                #pylab.linewidth = 4
                xs = o[1][:-1] + scipy.ones(len(
                    o[1][:-1])) * (o[1][1] - o[1][0]) * 0.5
                ys = y_smooth
                pylab.plot(xs, ys, 'r', linewidth=2)
                pylab.suptitle(title_dict[key])
                print o
                pylab.savefig(file)
                ims += '<img src=' + key + '.png></a>\n'
                a = zip(y_smooth, xs)
                a.sort()
                max = a[-1][1]
                pagemain.write(key + ' median=' + str(corrections[key]) +
                               ' smoothed peak=' + str(max) + '<br>')
                nums += key + ' ' + str(stars_dict[key]) + '<br>\n'
        pagemain.write('</ul>' + nums + '</ul>' + ims)
        pagemain.close()

    os.system('mkdir -p ' + os.environ['sne'] + '/photoz/' + cluster)

    #mkcolorcolor(filterlist,catalog,starcatalog,cluster,'APER1',name_suffix='aperapercalib')

    #mkcolorcolor(filterlist,catalog,starcatalog,cluster,',name_suffix='')
    if False:
        mkcolorcolor(filterlist,
                     catalog,
                     starcatalog,
                     cluster,
                     'APER1',
                     name_suffix='')

    #import sys
    #sys.exit(0)

    print catalog

    #catalog = '/u/ki/dapple/subaru/MACS2243-09/PHOTOMETRY_W-J-V_iso/MACS2243-09.aper.slr.cat'
    #file = mkcolorcolor(filterlist,catalog,starcatalog,cluster,'ISO',name_suffix='isomags_apercalib')

    #catalog = '/u/ki/dapple/subaru/MACS2243-09/PHOTOMETRY_W-J-V_iso/MACS2243-09.slr.cat'
    #file = mkcolorcolor(filterlist,catalog,starcatalog,cluster,'ISO',name_suffix='isomags_isocalib')

    #catalog = '/u/ki/dapple/subaru/MACS2243-09/PHOTOMETRY_W-J-V_aper/MACS2243-09.slr.cat'
    #file = mkcolorcolor(filterlist,catalog,starcatalog,cluster,'APER1',name_suffix='apermags_apercalib')

    #print file

    ffile = os.environ['sne'] + '/photoz/' + cluster + '/all.tif'

    print filters
    if not glob(ffile):

        print filters[1:4]
        for filt in filters[1:4]:
            #filter = filter.replace('MEGAPRIME-0-1-','').replace('SUBARU-10_2-1-','').replace('SUBARU-10_2-2-','').replace('SUBARU-10_1-1-','').replace('SUBARU-10_1-2-','')#.replace('SUBARU-8-1-','')
            params = {
                'path': path,
                'filter_root': filter_root,
                'cluster': cluster,
                'filter': filt,
                'DETECT_FILTER': DETECT_FILTER,
                'AP_TYPE': AP_TYPE,
                'appendix': appendix,
            }
            print params
            # now run sextractor to determine the seeing:
            image = '%(path)s/%(filter)s/SCIENCE/coadd_%(cluster)s%(appendix)s/coadd.fits' % params
            images.append(image)
            print 'reading in ' + image
            seg_image = '/%(path)s/%(filter)s/PHOTOMETRY/coadd.apertures.fits' % params
            ims[filt] = pyfits.open(image)[0].data
            print 'read in ' + image
            #ims_seg[filter] = pyfits.open(seg_image)[0].data

        print images

        os.system('mkdir ' + os.environ['sne'] + '/photoz/' + cluster + '/')
        os.system('chmod o+rx ' + os.environ['sne'] + '/photoz/' + cluster +
                  '/')

        from glob import glob
        os.system('stiff ' + reduce(lambda x, y: x + ' ' + y, images) +
                  ' -OUTFILE_NAME ' + ffile + ' -BINNING 1 -GAMMA_FAC 1.6')
        os.system('convert ' + os.environ['sne'] + '/photoz/' + cluster +
                  '/all.tif ' + os.environ['sne'] + '/photoz/' + cluster +
                  '/fix.tif')

    print ffile

    import Image
    im = Image.open(os.environ['sne'] + '/photoz/' + cluster + '/fix.tif')

    print catalog
    p_cat = pyfits.open(catalog)[1].data

    #outputcat = '%(path)s/PHOTOMETRY/all_bpz1_' % params
    #spec = True

    picks = False

    #SPECTRA =  'CWWSB4_txitxo.list'
    #SPECTRA = 'CWWSB_capak.list' # use Peter Capak's SEDs

    print SPECTRA

    outbase = os.environ['sne'] + '/photoz/' + cluster + '/'

    os.system('mkdir -p ' + outbase + '/' + SPECTRA + '/')

    SeqNr_file = open('SeqNr_file', 'w')

    if type == 'spec':
        probsout = '%(path)s/PHOTOMETRY%(DETECT_FILTER)s%(AP_TYPE)s/%(cluster)s.%(magtype)s.1.%(SPECTRA)s.%(type)s.probs' % params
        #outputcat = '%(path)s/PHOTOMETRY/%(cluster)s.1.all.bpz.tab' % params

        print probsout, outputcat.replace('.tab', '')
        f = open(probsout, 'r').readlines()
        fz_temp = open(outputcat.replace('.tab', ''), 'r').readlines()

        fz = []
        for l in fz_temp:
            if l[0] != '#': fz.append(l)

        import re

        res = re.split('\s+', f[0])
        print res[4]
        res2 = re.split('\,', res[4].replace('z=arange(', '').replace(')', ''))
        print res2
        import scipy
        increment = float(res2[2])
        #matrix = scipy.array(zs*

        print zip(f[1:], fz[:])[1]  #zs

        end = int(1. / increment)

        zs = (scipy.arange(float(res2[0]), 1. + increment, increment))

        prob_matrix = scipy.zeros((end, end))

        for l, zf in zip(f[1:], fz[:]):
            #print l, zf
            import re
            resz = re.split('\s+', zf[:-1])
            if resz[0] == '': resz = resz[1:]
            #print resz
            z_spec = float(resz[9])
            odds = float(resz[5])
            res = re.split('\s+', l[:-1])
            if res[-1] == '': res = res[:-1]
            #print res
            pdz = scipy.array([float(x) for x in res[1:]])
            from copy import copy
            zs_copy = copy(zs)
            #zs_copy = zs_copy[pdz != 0]
            #pdz = pdz[pdz != 0]
            #print zs_copy, pdz, resz[9]
            for i in range(len(zs_copy))[:end]:  #z,p in zip(zs_copy, pdz):
                if odds > 0.95 and z_spec < 1.:
                    prob_matrix[i, int(z_spec / increment)] += pdz[i]

            #print prob_matrix

            #pdz.append(
        print 'done calculating'

        X, Y = pylab.meshgrid(zs_copy, zs_copy)

        print prob_matrix.shape, X.shape, Y.shape

        pylab.clf()

        print prob_matrix.max()

        prob_matrix[prob_matrix > 1] = 1.

        pylab.pcolor(X,
                     Y,
                     -1. * prob_matrix,
                     cmap='gray',
                     alpha=0.9,
                     shading='flat',
                     edgecolors='None')

        pylab.plot([0, 1], [0, 1], color='red')
        pylab.xlabel('SpecZ')
        pylab.ylabel('PhotZ')

        pylab.savefig(outbase + '/' + SPECTRA + '/RedshiftPDZ01.png')
        #pylab.pcolor(X, Y,prob_matrix,cmap='gray',alpha=0.9,shading='flat',edgecolors='None')
        #pylab.colorbar()
        #doFormating(**formating)
        #histogram2d(zs_copy,zs_copy,prob_matrix)

    #for l in f[1:]:
    #    res = re.split('\s+',l)
    #    print res

    if True:  #glob(outputcat.replace('.tab','')):
        plot_res(outputcat.replace('.tab', ''), outbase, SPECTRA)

    print 'plot_res'

    print outputcat
    bpz_cat = pyfits.open(outputcat)[1].data

    print outputcat
    gals = []

    set = range(len(bpz_cat))
    if type == 'rand':
        set = range(100)

    f_nums = [e[:-1] for e in open('SeqNr_save', 'r').readlines()]

    for i in set:  #[0:25]: #[75227,45311,53658, 52685, 64726]:
        print i
        line = bpz_cat[i]
        print line, outputcat
        text = ''

        #for x,y,side,index in [[4800,4900,200,1],[5500,5500,100,2],[4500,5500,300,2]]:

        text += '<tr>\n'
        SeqNr = int(line['SeqNr'])
        fileNumber = str(int(line['BPZ_NUMBER']))
        params['fileNumber'] = str(fileNumber)

        base = '%(path)s/PHOTOMETRY%(DETECT_FILTER)s%(AP_TYPE)s/%(cluster)s.%(magtype)s.1.%(SPECTRA)s.%(type)s' % params
        probs = '%(path)s/PHOTOMETRY%(DETECT_FILTER)s%(AP_TYPE)s/%(cluster)s.%(magtype)s.1.%(SPECTRA)s.%(type)s.probs' % params
        print probs

        print base, probs

        resid = line['BPZ_Z_B'] - line['BPZ_Z_S']

        z = line['BPZ_Z_B']

        ODDS = line['BPZ_ODDS']

        if True:  #line['BPZ_ODDS'] > 0.5 and 0.6 < line['BPZ_Z_B'] < 0.8 and 0.2 < line['BPZ_Z_S'] < 0.4: #abs(resid) > 0.2: #abs(resid) > 0.04 and 0.4 < line['BPZ_Z_S'] < 0.6: #True: #SeqNr==24778: # SeqNr == 4441 or SeqNr==1285: #abs(resid) > 0.1:

            #        print f_nums
            #        if True: #len(filter(lambda x: int(line['SeqNr']) == int(x),f_nums)):

            probs_f = open(probs, 'r').readlines()

            print line['SeqNr']
            file = '/nfs/slac/g/ki/ki04/pkelly/photoz/' + cluster + '/' + SPECTRA + '/' + str(
                SeqNr) + 'spec.png'  #print 'SAVING', outdir + outimg
            sys.argv = ['', base, str(SeqNr), file]
            import sedplot
            print base, 'base'
            filt_info = sedplot.run()
            print filt_info
            import math

            if 1:  #filt_info[0]['flux'] > 0:
                ratio.append(filt_info[0]['expectedflux'] /
                             filt_info[0]['flux'])

            #import pylab
            #from scipy import arange
            #print ratio
            #import scipy
            #print 'ratio', scipy.median(scipy.array(ratio))
            #a, b, varp = pylab.hist(ratio,bins=arange(0,5,0.1))
            #pylab.show()

            #if line['BPZ_ODDS'] > 0.95 and abs(resid) > 0.2: #abs(resid) > 0.04 and 0.4 < line['BPZ_Z_S'] < 0.6: #True: #SeqNr==24778: # SeqNr == 4441 or SeqNr==1285: #abs(resid) > 0.1:

            #if 1:
            print line['BPZ_Z_B']
            SeqNr_file.write(str(SeqNr) + '\n')
            mask = p_cat.field('SeqNr') == SeqNr
            temp = p_cat[mask]
            x = int(temp.field('Xpos')[0])
            y = 10000 - int(temp.field('Ypos')[0])

            x_fits = int(temp.field('Xpos')[0])
            y_fits = int(temp.field('Ypos')[0])

            import pyraf
            from pyraf import iraf

            if line['BPZ_Z_S'] != 0:
                resid = line['BPZ_Z_B'] - line['BPZ_Z_S']
                if resid > 0:
                    color = 'green'
                    resid_str = ' <font color=' + color + '>+' + str(
                        resid) + '</font> '
                else:
                    color = 'red'
                    resid_str = ' <font color=' + color + '>' + str(
                        resid) + '</font> '
            else:
                resid = 'no spec z'
                color = 'black'
                resid_str = ' <font color=' + color + '>' + str(
                    resid) + '</font> '

            t = [
                'BPZ_Z_B', 'BPZ_ODDS', 'BPZ_CHI-SQUARED', 'BPZ_Z_B_MIN',
                'BPZ_Z_B_MAX'
            ]
            text += '<td colspan=10>' + str(SeqNr) + ' z=' + str(
                line['BPZ_Z_B']
            ) + ' MIN=' + str(line['BPZ_Z_B_MIN']) + ' MAX=' + str(
                line['BPZ_Z_B_MAX']) + resid_str + ' ODDS=' + str(
                    line['BPZ_ODDS']) + ' TYPE=' + str(
                        line['BPZ_T_B']) + ' CHISQ=' + str(
                            line['BPZ_CHI-SQUARED']) + ' x=' + str(
                                x) + ' y=' + str(y) + '</td></tr><tr>\n'

            print x, y

            index = SeqNr

            outfile = os.environ[
                'sne'] + '/photoz/' + cluster + '/' + SPECTRA + '/' + str(
                    index) + '.png'
            plot_bpz_probs(index, probs_f, outfile)

            #file = 'Id%09.f.spec' % index
            #outfile = os.environ['sne'] + '/photoz/' + str(index) + '.png'
            #print outfile
            #plot(file,outfile)

            text += '<td align=left><img height=400px src=' + str(
                index) + '.png></img>\n'
            text += '<img height=400px src=' + str(index) + 'spec.png></img>\n'
            images = []

            file = '/nfs/slac/g/ki/ki04/pkelly/photoz/' + cluster + '/' + str(
                SeqNr) + 'spec.png'  #print 'SAVING', outdir + outimg
            command = 'python ' + os.environ[
                'BPZPATH'] + '/plots/sedplot.py ' + base + ' ' + str(
                    SeqNr) + ' ' + file
            print command

            import sedplot

            #sys.argv = ['',base,str(SeqNr),file]
            #filt_info = sedplot.run()
            #print filt_info

            #print command
            #os.system(command)
            side = 100  #x = temp.field('Xpos')[0]

            if 1:  #try:
                p = im.crop([x - side, y - side, x + side, y + side])
                image = os.environ[
                    'sne'] + '/photoz/' + cluster + '/' + SPECTRA + '/' + str(
                        index) + '.jpg'
                os.system('rm ' + image)
                p.save(image, 'JPEG')
            #except: print 'fail'

            text += '<img src=' + str(
                index) + '.jpg></img></td><td colspan=20></td>\n'
            text += '</tr>\n'
            keys = [
                'name', 'wavelength', 'observed', 'flux', 'fluxerror',
                'expectedflux', 'chioffset'
            ]
            text += '<tr><td colspan=10><table><tr>' + reduce(
                lambda x, y: x + y, ['<td>' + n + '</td>'
                                     for n in keys]) + "</tr>"
            for f in filt_info:
                text += '<tr>' + reduce(
                    lambda x, y: x + y,
                    ['<td>' + str(f[n]) + '</td>' for n in keys]) + "</tr>"
            text += "</table></td></tr>"

            side = 100  #x = temp.field('Xpos')[0]
            #if x_fits - 100 < 0: x_fits = 101
            #if y_fits - 100 < 0: y_fits = 101
            #if x_fits + 100 > 9999: x_fits = 9899
            #if y_fits + 100 > 9999: y_fits = 9899
            xlow = int(x_fits - side)
            xhigh = int(x_fits + side)
            ylow = int(y_fits - side)
            yhigh = int(y_fits + side)

            if xlow < 0: xlow = ''
            if ylow < 0: ylow = ''
            if xhigh > 9999: xhigh = ''
            if yhigh > 9999: yhigh = ''

            bounds = '[' + str(xlow) + ':' + str(xhigh) + ',' + str(
                ylow) + ':' + str(yhigh) + ']'
            text += '<td colspan=20><table>\n'
            textim = ''
            textlabel = ''

            import string

            filters_b = conv_filt(filt_info)

            for filt in []:  #filters_b: #[1:]:
                fitsfile = '/nfs/slac/g/ki/ki04/pkelly/photoz/' + cluster + '/' + SPECTRA + '/' + str(
                    SeqNr
                ) + 'cutout' + filt + '.fits'  #print 'SAVING', outdir + outimg
                jpg = '/nfs/slac/g/ki/ki04/pkelly/photoz/' + cluster + '/' + SPECTRA + '/' + str(
                    SeqNr
                ) + 'cutout' + filt + '.jpg'  #print 'SAVING', outdir + outimg
                os.system('rm ' + fitsfile)
                os.system('rm ' + jpg)
                bigfile = '/a/wain023/g.ki.ki05/anja/SUBARU/' + cluster + '/' + filt + '/SCIENCE/coadd_' + cluster + '_all/coadd.fits'
                iraf.imcopy(bigfile + bounds, fitsfile)
                import commands
                seeing = commands.getoutput('gethead ' + bigfile + ' SEEING')
                print 'seeing', seeing

                import os
                #os.system("ds9 " + fitsfile + " -view info no -view panner no -view magnifier no -view buttons no -view colorbar yes -view horzgraph no -view wcs no -view detector no -view amplifier no -view physical no -zoom to fit -minmax -histequ  -invert -zoom to fit -saveas jpeg " + jpg ) # -quit >& /dev/null &")
                #os.system("xpaset -p ds9 " + fitsfile + "  -zoom to fit -view colorbar no -minmax -histequ  -invert -zoom to fit -saveas jpeg " + jpg  + " ") # -quit >& /dev/null &")
                print 'bounds', bigfile + bounds

                com = [
                    'file ' + fitsfile, 'zoom to fit', 'view colorbar no',
                    'minmax', 'scale histequ', 'saveimage jpeg ' + jpg
                ]  # -quit >& /dev/null &")
                for c in com:
                    z = 'xpaset -p ds9 ' + c
                    print z
                    os.system(z)
                print jpg
                text += '<td><img height=200px src=' + str(
                    SeqNr) + 'cutout' + filt + '.jpg></img></td>\n'
                textlabel += '<td>' + filt + ' seeing ' + seeing + '</td>\n'
            text += '<tr>' + textim + '</tr><tr>' + textlabel + '</tr></table></td></tr><tr>'

            #os.system('stiff ' + image_seg + ' -OUTFILE_NAME ' + image_seg.replace('fits','tif'))
            gals.append([line['BPZ_T_B'], text])

            from datetime import datetime
            t2 = datetime.now()
            file = os.environ[
                'sne'] + '/photoz/' + cluster + '/' + SPECTRA + '/objects.html'
            print file
            page = open(file, 'w')
            t = '<head><link href="http://www.slac.stanford.edu/~pkelly/photoz/table.css" rel="stylesheet" type="text/css"></head>'
            t += '<table align=left><tr><td colspan=5 class="dark"><h1>' + cluster + '</h1></td></tr><tr><td colspan=5><h4>created ' + t2.strftime(
                "%Y-%m-%d %H:%M:%S"
            ) + '</h4></td></tr><tr><td colspan=5><a href=stars.html>Stellar Color-Color Plots</a><td></tr>\n'

            if type == 'spec':
                t += '<tr><td colspan=5 class="dark"><h2>Spectroscopic Sample</h2></td></tr>\n'
                t += '<tr><td colspan=10 align=left><img height=400px src=RedshiftErrors.png></img>\n'
                t += '<img height=400px src=RedshiftScatter04.png></img>\n'
                t += '<img height=400px src=RedshiftScatter01.png></img>\n'
                t += '<img height=400px src=RedshiftPDZ01.png></img></td></tr>\n'
            if type == 'rand':
                t += '<tr><td colspan=5 class="dark"><h2>Random Sample of 100 Galaxies</h2></td></tr>\n'
            if type == 'picks':
                t += '<tr><td colspan=5 class="dark"><h2>Pick</h2></td></tr>\n'

            page.write(t)
            gals.sort()
            for gal in gals:
                page.write(gal[1])
            page.write('<table>\n')
            page.close()
            print 'CLOSED!!'
            if len(gals) > 100:
                break
    ''' make SED plots '''