def color_cut_per_galaxy(table='default'):
    ms_dict = ms_color_cut()
    comp90 = sfh_tests.read_completeness_table(table=table, absmag=True)
    comp90_uncert = sfh_tests.read_completeness_table(table=table, uncertainties=True)
    #if table == 'default':
    #    table = snap_src + '/tables/completeness_0.90.dat'
    #table = table.replace('.dat', '_colorcuts.dat')
    photsys = 'wfc3snap'
    #fmt = '%(target)s %(opt_colorcut).3f %(ir_colorcut).3f \n'
    
    for sgalname, dline in ms_dict.items():
        print sgalname
        for i, target in enumerate(comp90['target']):
            for band in ['opt', 'ir']:
                if band == 'opt':
                    filter1 = sfh_tests.get_filter1(target.lower())
                    filter2 = 'F814W'
                else:
                    filter1 = 'F110W'
                    filter2 = 'F160W'
                m2m = {'target': target, 'filter2': filter2, 'filter1': filter1}
                if not filter1 in dline.keys():
                    continue
                Mag1 = dline[filter1]
                Mag2 = dline[filter2]
                mag1 = rsp.astronomy_utils.Mag2mag(Mag1, filter1, photsys, **m2m)
                mag2 = rsp.astronomy_utils.Mag2mag(Mag2, filter2, photsys, **m2m)
                color = mag1 - mag2
                color_uncert = comp90_uncert[i]['%s_color' % band]
                print target, filter1, filter2, '%.2f' % (color+color_uncert)
def find_contamination_by_phases(output_files=None):
    if output_files is None:
        output_files = [ snap_src + '/models/varysfh/ddo71/caf09_s_nov13/mc/output_ddo71_caf09_s_nov13.dat',
                        #snap_src + '/models/varysfh/ddo78/caf09_s_nov13/mc/output_ddo78_caf09_s_nov13.dat',
                        snap_src + '/models/varysfh/hs117/caf09_s_nov13/mc/output_hs117_caf09_s_nov13.dat',
                        #snap_src + '/models/varysfh/kdg73/caf09_s_nov13/mc/output_kdg73_caf09_s_nov13.dat',
                        snap_src + '/models/varysfh/kkh37/caf09_s_nov13/mc/output_kkh37_caf09_s_nov13.dat']#,
                        #snap_src + '/models/varysfh/ngc2976-deep/caf09_s_nov13/mc/output_ngc2976-deep_caf09_s_nov13.dat',
                        #snap_src + '/models/varysfh/ngc404/caf09_s_nov13/mc/output_ngc404_caf09_s_nov13.dat']

    for output_file in output_files:
        target = output_file.split('output_')[1].split('_')[0]
        print target
        filter1 = sfh_tests.get_filter1(target)

        ds = sfh_tests.Diagnostics(VarySFH_kw={'target': target})
        ds.mc = False

        sgal = rsp.Galaxies.simgalaxy(output_file, filter1=filter1,
                                      filter2='F814W')
        sgal.target = target

        sopt_rgb, sopt_agb, sir_rgb, sir_agb = \
            ds.do_normalization(filter1=filter1, trilegal_output=output_file,
                                hist_it_up=False, dry_run=True)

        ds.contamination_by_phases(sopt_rgb, sopt_agb, sir_rgb, sir_agb)

    return
def completeness_table_absmag(table='default'):
    '''
    convert the completeness table mags to abs mag.
    outfile = [table]_absmag.dat
    '''
    comp90 = sfh_tests.read_completeness_table(table)
    if table == 'default':
        table = snap_src + '/tables/completeness_0.90.dat'
    table = table.replace('.dat', '_absmag.dat')
    photsys = 'wfc3snap'
    fmt = '%(target)s %(opt_filter1).3f %(opt_filter2).3f %(ir_filter1).3f %(ir_filter2).3f \n'
    
    with open(table, 'w') as out:
        for i, target in enumerate(comp90['target']):
            dline = {'target': target}
            for band in ['opt', 'ir']:
                if band == 'opt':
                    filter1 = sfh_tests.get_filter1(target.lower())
                    filter2 = 'F814W'
                else:
                    filter1 = 'F110W'
                    filter2 = 'F160W'
                m2m = {'target': target, 'filter2': filter2, 'filter1': filter1}
                
                compf1 = comp90[i]['%s_filter1' % band]
                dline['%s_filter1' % band] = rsp.astronomy_utils.mag2Mag(compf1, filter1, photsys, **m2m)
                
                compf2 = comp90[i]['%s_filter2' % band]
                dline['%s_filter2' % band] = rsp.astronomy_utils.mag2Mag(compf2, filter2, photsys, **m2m)
            out.write(fmt % dline)
    print 'wrote %s' % table