Example #1
0
def main():
    # objects = ['AGC174540', 'AGC226067', 'AGC227987', 'AGC229326', 'AGC238713', 'AGC249282', 'AGC249323', 'AGC258242', 'AGC258459', 'AGC268074', 'HI1037+21', 'HI1050+23']

    objects = OrderedDict([('AGC174540', 25.19), ('AGC226067', 24.72),
                           ('AGC227987', 24.46), ('AGC229326', 22.14),
                           ('AGC238713', 26.73), ('AGC249282', 24.14),
                           ('AGC249323', 26.42), ('AGC258242', 26.00),
                           ('AGC258459', 23.00), ('AGC268074', 23.12),
                           ('HI1037+21', 22.02), ('HI1050+23', 22.86)])
    filter_file = os.path.dirname(os.path.abspath(__file__)) + '/filter.txt'

    # plt.clf()
    fig = plt.figure(figsize=(10, 7))
    outer = gridspec.GridSpec(4, 3, wspace=0.1, hspace=0.1)
    for i, obj in enumerate(objects.keys()):
        print obj
        inner = gridspec.GridSpecFromSubplotSpec(1,
                                                 1,
                                                 subplot_spec=outer[i],
                                                 wspace=0.1,
                                                 hspace=0.1)
        # set up some filenames
        folder = '/Volumes/galileo/uchvc/targets/' + obj.lower() + '/'
        mag_file = folder + 'calibrated_mags.dat'
        fits_file_i = folder + obj + '_i.fits'
        fits_i = fits.open(fits_file_i)

        dm = objects[obj]

        # read in magnitudes, colors, and positions(x,y)
        # gxr,gyr,g_magr,g_ierrr,ixr,iyr,i_magr,i_ierrr,gmir,fwhm_sr= np.loadtxt(mag_file,usecols=(0,1,2,3,4,5,6,7,8,11),unpack=True)
        gxr, gyr, g_magr, g_ierrr, ixr, iyr, i_magr, i_ierrr, gmir = np.loadtxt(
            mag_file, usecols=(0, 1, 2, 3, 4, 5, 6, 7, 8), unpack=True)
        # print len(gxr), "total stars"
        fwhm_sr = np.ones_like(gxr)
        # filter out the things with crappy color errors
        color_error_cut = np.sqrt(2.0) * 0.2
        mag_error_cut = 0.2

        gmi_errr = [
            np.sqrt(g_ierrr[i]**2 + i_ierrr[i]**2) for i in range(len(gxr))
        ]
        gx = [
            gxr[i] for i in range(len(gxr)) if (abs(
                gmi_errr[i] < color_error_cut and i_ierrr[i] < mag_error_cut))
        ]
        gy = [
            gyr[i] for i in range(len(gxr)) if (abs(
                gmi_errr[i] < color_error_cut and i_ierrr[i] < mag_error_cut))
        ]
        g_mag = [
            g_magr[i] for i in range(len(gxr)) if (abs(
                gmi_errr[i] < color_error_cut and i_ierrr[i] < mag_error_cut))
        ]
        g_ierr = [
            g_ierrr[i] for i in range(len(gxr)) if (abs(
                gmi_errr[i] < color_error_cut and i_ierrr[i] < mag_error_cut))
        ]
        ix = [
            ixr[i] for i in range(len(gxr)) if (abs(
                gmi_errr[i] < color_error_cut and i_ierrr[i] < mag_error_cut))
        ]
        iy = [
            iyr[i] for i in range(len(gxr)) if (abs(
                gmi_errr[i] < color_error_cut and i_ierrr[i] < mag_error_cut))
        ]
        i_mag = [
            i_magr[i] for i in range(len(gxr)) if (abs(
                gmi_errr[i] < color_error_cut and i_ierrr[i] < mag_error_cut))
        ]
        i_ierr = np.array([
            i_ierrr[i] for i in range(len(gxr)) if (abs(
                gmi_errr[i] < color_error_cut and i_ierrr[i] < mag_error_cut))
        ])
        gmi = [
            gmir[i] for i in range(len(gxr)) if (abs(
                gmi_errr[i] < color_error_cut and i_ierrr[i] < mag_error_cut))
        ]
        fwhm_s = [
            fwhm_sr[i] for i in range(len(gxr)) if (abs(
                gmi_errr[i] < color_error_cut and i_ierrr[i] < mag_error_cut))
        ]
        gmi_err = np.array([
            np.sqrt(g_ierrr[i]**2 + i_ierrr[i]**2) for i in range(len(gxr)) if
            (abs(gmi_errr[i] < color_error_cut and i_ierrr[i] < mag_error_cut))
        ])

        i_ierrAVG, bedges, binid = ss.binned_statistic(i_mag,
                                                       i_ierr,
                                                       statistic='median',
                                                       bins=10,
                                                       range=[15, 25])
        gmi_errAVG, bedges, binid = ss.binned_statistic(i_mag,
                                                        gmi_err,
                                                        statistic='median',
                                                        bins=10,
                                                        range=[15, 25])

        bcenters = (bedges[:-1] + bedges[1:]) / 2
        bxvals = [3.75, 3.75, 3.75, 3.75, 3.75, 3.75, 3.75, 3.75, 3.75, 3.75]
        # print bcenters
        # print i_ierrAVG
        # print gmi_errAVG
        # print len(gx), "after color+mag error cut"
        # nid = np.loadtxt(mag_file,usecols=(0,),dtype=int,unpack=True)
        pixcrd = zip(ix, iy)

        # print "Reading WCS info from image header..."
        # Parse the WCS keywords in the primary HDU
        warnings.filterwarnings('ignore', category=UserWarning, append=True)
        w = wcs.WCS(fits_i[0].header)
        # print fits_i[0].header['naxis1'], fits_i[0].header['naxis2']
        footprint = w.calc_footprint()
        se_corner = footprint[0]
        ne_corner = footprint[1]
        nw_corner = footprint[2]
        sw_corner = footprint[3]
        # print se_corner, ne_corner, nw_corner, sw_corner
        width = (ne_corner[0] - nw_corner[0]) * 60.
        height = (ne_corner[1] - se_corner[1]) * 60.
        # print width, height

        # Print out the "name" of the WCS, as defined in the FITS header
        # print w.wcs.name

        # Print out all of the settings that were parsed from the header
        # w.wcs.print_contents()

        # Convert pixel coordinates to world coordinates
        # The second argument is "origin" -- in this case we're declaring we
        # have 1-based (Fortran-like) coordinates.
        world = w.all_pix2world(pixcrd, 1)
        ra_corner, dec_corner = w.all_pix2world(0, 0, 1)
        ra_c_d, dec_c_d = deg2HMS(ra=ra_corner, dec=dec_corner, round=True)
        # print 'Corner RA:',ra_c_d,':: Corner Dec:',dec_c_d

        fits_i.close()

        # split the ra and dec out into individual arrays and transform to arcmin from the corner
        i_ra = [
            abs((world[i, 0] - ra_corner) * 60)
            for i in range(len(world[:, 0]))
        ]
        i_dec = [
            abs((world[i, 1] - dec_corner) * 60)
            for i in range(len(world[:, 1]))
        ]
        # also preserve the decimal degrees for reference
        i_rad = [world[i, 0] for i in range(len(world[:, 0]))]
        i_decd = [world[i, 1] for i in range(len(world[:, 1]))]

        search = open(obj + '_search.txt', 'w+')

        sig_bins = []
        sig_cens = []
        sig_max = []

        dms = np.arange(22.0, 26.99, 0.01)

        for dm in dms:
            mpc = pow(10, ((dm + 5.) / 5.)) / 1000000.
            dm_string = '{:5.2f}'.format(dm).replace('.', '_')

            cm_filter, gi_iso, i_m_iso = make_filter(dm, filter_file)
            stars_f = filter_sources(i_mag,
                                     i_ierr,
                                     gmi,
                                     gmi_err,
                                     cm_filter,
                                     filter_sig=1)

            xy_points = zip(i_ra, i_dec)

            # make new vectors containing only the filtered points

            i_mag_f = [i_mag[i] for i in range(len(i_mag)) if (stars_f[i])]
            g_mag_f = [g_mag[i] for i in range(len(i_mag)) if (stars_f[i])]
            gmi_f = [gmi[i] for i in range(len(i_mag)) if (stars_f[i])]
            i_ra_f = [i_ra[i] for i in range(len(i_mag)) if (stars_f[i])]
            i_dec_f = [i_dec[i] for i in range(len(i_mag)) if (stars_f[i])]
            i_rad_f = [i_rad[i] for i in range(len(i_mag)) if (stars_f[i])]
            i_decd_f = [i_decd[i] for i in range(len(i_mag)) if (stars_f[i])]
            i_x_f = [ix[i] for i in range(len(i_mag)) if (stars_f[i])]
            i_y_f = [iy[i] for i in range(len(i_mag)) if (stars_f[i])]
            fwhm_sf = [fwhm_s[i] for i in range(len(i_mag)) if (stars_f[i])]
            n_in_filter = len(i_mag_f)

            xedges, x_cent, yedges, y_cent, S, x_cent_S, y_cent_S, pltsig, tbl = grid_smooth(
                i_ra_f, i_dec_f, 2.0, width, height)
            pct, d_bins, d_cens = distfit(n_in_filter, S[x_cent_S][y_cent_S],
                                          obj, width, height, 2.0, dm)

            sig_bins.append(d_bins)
            sig_cens.append(d_cens)
            sig_max.append(S[x_cent_S][y_cent_S])

            hi_c_ra, hi_c_dec = getHIellipse(obj,
                                             ra_corner,
                                             dec_corner,
                                             centroid=True)
            sep = dist2HIcentroid(ra_c_d, dec_c_d, hi_c_ra, hi_c_dec)
            print 'm-M = {:5.2f} | d = {:4.2f} Mpc | α = {:s}, δ = {:s}, Δʜɪ = {:5.1f}" | N = {:4d} | σ = {:6.3f} | ξ = {:6.3f}%'.format(
                dm, mpc, ra_c_d, dec_c_d, sep, n_in_filter,
                S[x_cent_S][y_cent_S], pct)
            print >> search, '{:5.2f} {:4.2f} {:s} {:s} {:5.1f} {:4d} {:6.3f} {:6.3f}'.format(
                dm, mpc, ra_c_d, dec_c_d, sep, n_in_filter,
                S[x_cent_S][y_cent_S], pct)

        ax1 = plt.Subplot(fig, inner[0])
        ax1.imshow(np.transpose(sig_bins),
                   cmap=plt.cm.Reds,
                   extent=(22, 27, 22, 2))  #, origin=origin)
        ax1.plot(dms, sig_max, linestyle='-', color='black', lw=0.5)
        ax1.set_ylabel('$\sigma$')
        ax1.set_xlabel('distance modulus')
        ax1.set_xlim(22, 27)
        ax1.set_ylim(2, 6.5)
        if obj.startswith('HI1151'):
            ax1.set_title('AGC219656', size='small')
        else:
            ax1.set_title(obj, size='small')
        ax1.set_aspect(0.4)
        fig.add_subplot(ax1)

    outer.tight_layout(fig)
    plt.savefig('non-detections_significance.pdf')

    pass
Example #2
0
def main():
    objects = OrderedDict([('AGC198511',22.91), ('AGC198606',24.72), ('AGC215417',22.69), ('HI1151+20',24.76), ('AGC249525',26.78), ('AGC268069',24.24)])
    smooths = OrderedDict([('AGC198511',3.0), ('AGC198606',2.0), ('AGC215417',3.0), ('HI1151+20',2.0), ('AGC249525',3.0), ('AGC268069',3.0)])
    filter_file = os.path.dirname(os.path.abspath(__file__))+'/filter.txt'
    
    for file_ in os.listdir("./"):
        if file_.endswith("i.fits"):
            fits_file_i = file_
 
    for file_ in os.listdir("./"):
        if file_.endswith("g.fits"):
            fits_file_g = file_
    
    # downloadSDSSgal(fits_file_g, fits_file_i)
              
    fits_i = fits.open(fits_file_i)
    # fits_g = fits.open(fits_file_g)
    # print "Opened fits files:",fits_file_g,"&",fits_file_i
    
    # objid = fits_i[0].header['OBJECT']
    title_string = fits_file_i.split('_')[0]       # get the name part of the filename.

    # set up some filenames
    mag_file = 'calibrated_mags.dat'

    # read in magnitudes, colors, and positions(x,y)
    # gxr,gyr,g_magr,g_ierrr,ixr,iyr,i_magr,i_ierrr,gmir,fwhm_sr= np.loadtxt(mag_file,usecols=(0,1,2,3,4,5,6,7,8,11),unpack=True)
    gxr,gyr,g_magr,g_ierrr,ixr,iyr,i_magr,i_ierrr,gmir= np.loadtxt(mag_file,usecols=(0,1,2,3,4,5,6,7,8),unpack=True)
    # print len(gxr), "total stars"
    fwhm_sr = np.ones_like(gxr)
    # filter out the things with crappy color errors
    color_error_cut = np.sqrt(2.0)*0.2
    mag_error_cut = 0.2
    
    gmi_errr = [np.sqrt(g_ierrr[i]**2 + i_ierrr[i]**2) for i in range(len(gxr))]
    gx = [gxr[i] for i in range(len(gxr)) if (abs(gmi_errr[i] < color_error_cut and i_ierrr[i] < mag_error_cut))]
    gy = [gyr[i] for i in range(len(gxr)) if (abs(gmi_errr[i] < color_error_cut and i_ierrr[i] < mag_error_cut))]
    g_mag = [g_magr[i] for i in range(len(gxr)) if (abs(gmi_errr[i] < color_error_cut and i_ierrr[i] < mag_error_cut))]
    g_ierr = [g_ierrr[i] for i in range(len(gxr)) if (abs(gmi_errr[i] < color_error_cut and i_ierrr[i] < mag_error_cut))]
    ix = [ixr[i] for i in range(len(gxr)) if (abs(gmi_errr[i] < color_error_cut and i_ierrr[i] < mag_error_cut))]
    iy = [iyr[i] for i in range(len(gxr)) if (abs(gmi_errr[i] < color_error_cut and i_ierrr[i] < mag_error_cut))]
    i_mag = [i_magr[i] for i in range(len(gxr)) if (abs(gmi_errr[i] < color_error_cut and i_ierrr[i] < mag_error_cut))]
    i_ierr = np.array([i_ierrr[i] for i in range(len(gxr)) if (abs(gmi_errr[i] < color_error_cut and i_ierrr[i] < mag_error_cut))])
    gmi = [gmir[i] for i in range(len(gxr)) if (abs(gmi_errr[i] < color_error_cut and i_ierrr[i] < mag_error_cut))]
    fwhm_s = [fwhm_sr[i] for i in range(len(gxr)) if (abs(gmi_errr[i] < color_error_cut and i_ierrr[i] < mag_error_cut))]
    gmi_err = np.array([np.sqrt(g_ierrr[i]**2 + i_ierrr[i]**2) for i in range(len(gxr)) if (abs(gmi_errr[i] < color_error_cut and i_ierrr[i] < mag_error_cut))])
    cutleft = [i for i in range(len(gxr)) if (abs(gmi_errr[i] < color_error_cut and i_ierrr[i] < mag_error_cut))]
    with open('cutleft.txt', 'w+') as spud:
        for i,item in enumerate(cutleft):
            print(item, file=spud)
    
    i_ierrAVG, bedges, binid = ss.binned_statistic(i_mag,i_ierr,statistic='median',bins=10,range=[15,25])
    gmi_errAVG, bedges, binid = ss.binned_statistic(i_mag,gmi_err,statistic='median',bins=10,range=[15,25])
    
    bcenters = (bedges[:-1] + bedges[1:]) / 2
    bxvals = [3.75,3.75,3.75,3.75,3.75,3.75,3.75,3.75,3.75,3.75]
    # print bcenters
    # print i_ierrAVG
    # print gmi_errAVG
    # print len(gx), "after color+mag error cut"
    # nid = np.loadtxt(mag_file,usecols=(0,),dtype=int,unpack=True)
    pixcrd = list(zip(ix,iy))
    
    
    # print "Reading WCS info from image header..."
    # Parse the WCS keywords in the primary HDU
    warnings.filterwarnings('ignore', category=UserWarning, append=True)
    w = wcs.WCS(fits_i[0].header)
    # print fits_i[0].header['naxis1'], fits_i[0].header['naxis2']
    footprint = w.calc_footprint()
    se_corner = footprint[0]
    ne_corner = footprint[1]
    nw_corner = footprint[2]
    sw_corner = footprint[3]
    # print se_corner, ne_corner, nw_corner, sw_corner
    width = (ne_corner[0]-nw_corner[0])*60.
    height = (ne_corner[1]-se_corner[1])*60.
    # print width, height
    
    # Print out the "name" of the WCS, as defined in the FITS header
    # print w.wcs.name
    
    # Print out all of the settings that were parsed from the header
    # w.wcs.print_contents()
    
    # Convert pixel coordinates to world coordinates
    # The second argument is "origin" -- in this case we're declaring we
    # have 1-based (Fortran-like) coordinates.
    world = w.all_pix2world(pixcrd, 1)
    ra_corner, dec_corner = w.all_pix2world(0,0,1)
    ra_c_d,dec_c_d = deg2HMS(ra=ra_corner, dec=dec_corner, round=True)
    # print 'Corner RA:',ra_c_d,':: Corner Dec:',dec_c_d
        
    fwhm_i = 12.0 #fits_i[0].header['FWHMPSF']
    fwhm_g = 9.0 # fits_g[0].header['FWHMPSF']
    
    # print 'Image FWHM :: g = {0:5.3f} : i = {1:5.3f}'.format(fwhm_g,fwhm_i)
    
    fits_i.close()
    # fits_g.close()
    
    # split the ra and dec out into individual arrays and transform to arcmin from the corner
    i_ra = [abs((world[i,0]-ra_corner)*60) for i in range(len(world[:,0]))]
    i_dec = [abs((world[i,1]-dec_corner)*60) for i in range(len(world[:,1]))]
    # also preserve the decimal degrees for reference
    i_rad = [world[i,0] for i in range(len(world[:,0]))]
    i_decd = [world[i,1] for i in range(len(world[:,1]))]
    
    
    i_magBright = [i_mag[i] for i in range(len(i_mag)) if (i_mag[i] < 22.75)]
    g_magBright = [g_mag[i] for i in range(len(i_mag)) if (i_mag[i] < 22.75)]
    ixBright = [ix[i] for i in range(len(i_mag)) if (i_mag[i] < 22.75)]
    iyBright = [iy[i] for i in range(len(i_mag)) if (i_mag[i] < 22.75)]
    i_radBright = [i_rad[i] for i in range(len(i_mag)) if (i_mag[i] < 22.75)]
    i_decdBright = [i_decd[i] for i in range(len(i_mag)) if (i_mag[i] < 22.75)]
    
    if not os.path.isfile('brightStars2275.reg'):
        f1 = open('brightStars2275.reg', 'w+')
        for i in range(len(i_magBright)) :
            print('{0:12.4f} {1:12.4f} {2:10.5f} {3:9.5f} {4:8.2f} {5:8.2f} {6:8.2f}'.format(ixBright[i],iyBright[i], i_radBright[i], i_decdBright[i], g_magBright[i], i_magBright[i], g_magBright[i]-i_magBright[i]), file=f1)
        f1.close()
    
    i_magRed = [i_mag[i] for i in range(len(i_mag)) if (gmi[i] > 1.75)]
    g_magRed = [g_mag[i] for i in range(len(i_mag)) if (gmi[i] > 1.75)]
    ixRed = [ix[i] for i in range(len(i_mag)) if (gmi[i] > 1.75)]
    iyRed = [iy[i] for i in range(len(i_mag)) if (gmi[i] > 1.75)]
    i_radRed = [i_rad[i] for i in range(len(i_mag)) if (gmi[i] > 1.75)]
    i_decdRed = [i_decd[i] for i in range(len(i_mag)) if (gmi[i] > 1.75)]
    
    if not os.path.isfile('redStars175.reg'):
        f1 = open('redStars175.reg', 'w+')
        for i in range(len(i_magRed)) :
            print('{0:12.4f} {1:12.4f} {2:10.5f} {3:9.5f} {4:8.2f} {5:8.2f} {6:8.2f}'.format(ixRed[i],iyRed[i], i_radRed[i], i_decdRed[i], g_magRed[i], i_magRed[i], g_magRed[i]-i_magRed[i]), file=f1)
        f1.close()
    
    dm = 22.0
    dm2 = 27.0
    fwhm = 2.0
    
    # if dm2 > 0.0 and filter_string != 'none':
    dms = np.arange(dm,dm2,0.01)
        # search = open('search_{:3.1f}.txt'.format(fwhm),'w+')
    # else:
    #     dms = [dm]
        # search = open('spud.txt'.format(fwhm),'w+')
    
    # sig_bins = []
    # sig_cens = []
    # sig_max = []

    for dm in dms:
        mpc = pow(10,((dm + 5.)/5.))/1000000.
        dm_string = '{:5.2f}'.format(dm).replace('.','_')
        
        out_file = 'anim/anim_' + dm_string + '_' + title_string + '.png'
        # mark_file = 'f_list_' + filter_string + '_' + fwhm_string + '_' + dm_string + '_' + title_string + '.reg'
        # filter_reg = 'f_reg_' + filter_string + '_' + fwhm_string + '_' + dm_string + '_' + title_string + '.reg'
        # circ_file = 'c_list_' + filter_string + '_' + fwhm_string + '_' + dm_string + '_' + title_string + '.reg'
        # fcirc_file = 'fc_list_' + filter_string + '_' + fwhm_string + '_' + dm_string + '_' + title_string + '.reg'
        # ds9_file = 'circles_' + filter_string + '_' + fwhm_string + '_' + dm_string + '_' + title_string + '.reg'
        circles_file = 'region_coords.dat'
        
        cm_filter, gi_iso, i_m_iso = make_filter(dm, filter_file)
        stars_f = filter_sources(i_mag, i_ierr, gmi, gmi_err, cm_filter, filter_sig = 1)
        
        xy_points = list(zip(i_ra,i_dec))
        
        # make new vectors containing only the filtered points
        
        i_mag_f = [i_mag[i] for i in range(len(i_mag)) if (stars_f[i])]
        g_mag_f = [g_mag[i] for i in range(len(i_mag)) if (stars_f[i])]
        gmi_f = [gmi[i] for i in range(len(i_mag)) if (stars_f[i])]
        i_ra_f = [i_ra[i] for i in range(len(i_mag)) if (stars_f[i])]
        i_dec_f = [i_dec[i] for i in range(len(i_mag)) if (stars_f[i])]
        i_rad_f = [i_rad[i] for i in range(len(i_mag)) if (stars_f[i])]
        i_decd_f = [i_decd[i] for i in range(len(i_mag)) if (stars_f[i])]
        i_x_f = [ix[i] for i in range(len(i_mag)) if (stars_f[i])]
        i_y_f = [iy[i] for i in range(len(i_mag)) if (stars_f[i])]
        fwhm_sf = [fwhm_s[i] for i in range(len(i_mag)) if (stars_f[i])]
        n_in_filter = len(i_mag_f)
        
        # xedgesg, x_centg, yedgesg, y_centg, Sg, x_cent_Sg, y_cent_Sg, pltsigg, tblg = galaxyMap(fits_file_i, fwhm, dm, filter_file)
        
        xedges, x_cent, yedges, y_cent, S, x_cent_S, y_cent_S, pltsig, tbl = grid_smooth(i_ra_f, i_dec_f, fwhm, width, height)
        # corr = signal.correlate2d(S, Sg, boundary='fill', mode='full')
        # print corr
        
        # pct, d_bins, d_cens = distfit(n_in_filter,S[x_cent_S][y_cent_S],title_string,width,height,fwhm,dm)
        # pct_hi = 0.0 #getHIcoincidence(x_cent_S, y_cent_S, title_string, ra_corner, dec_corner, width, height, dm)
        
        # sig_bins.append(d_bins)
        # sig_cens.append(d_cens)
        # sig_max.append(S[x_cent_S][y_cent_S])
        
        # if pct > 90 :
        #     pct, bj,cj = distfit(n_in_filter,S[x_cent_S][y_cent_S],title_string,width,height,fwhm,dm, samples=25000)
        
        # make a circle to highlight a certain region
        cosd = lambda x : np.cos(np.deg2rad(x))
        sind = lambda x : np.sin(np.deg2rad(x))
        x_circ = [yedges[y_cent] + 3.0*cosd(t) for t in range(0,359,1)]
        y_circ = [xedges[x_cent] + 3.0*sind(t) for t in range(0,359,1)]
        
        verts_circ = list(zip(x_circ,y_circ))
        circ_filter = Path(verts_circ)
        
        stars_circ = circ_filter.contains_points(xy_points)    
        
        i_mag_c = [i_mag[i] for i in range(len(i_mag)) if (stars_circ[i])]
        gmi_c = [gmi[i] for i in range(len(i_mag)) if (stars_circ[i])]
        i_ra_c = [i_ra[i] for i in range(len(i_mag)) if (stars_circ[i])]
        i_dec_c = [i_dec[i] for i in range(len(i_mag)) if (stars_circ[i])]
        i_rad_c = [i_rad[i] for i in range(len(i_mag)) if (stars_circ[i])]
        i_decd_c = [i_decd[i] for i in range(len(i_mag)) if (stars_circ[i])]
        i_x_c = [ix[i] for i in range(len(i_mag)) if (stars_circ[i])]
        i_y_c = [iy[i] for i in range(len(i_mag)) if (stars_circ[i])]
        fwhm_sc = [fwhm_s[i] for i in range(len(i_mag)) if (stars_circ[i])]
        
        # make a random reference cmd to compare to
        if not os.path.isfile('refCircle.center'):
            rCentx = 16.0*np.random.random()+2.0
            rCenty = 16.0*np.random.random()+2.0
            with open('refCircle.center','w+') as rc:
                print('{:8.4f} {:8.4f}'.format(rCentx, rCenty), file=rc)
        else :
            rCentx, rCenty = np.loadtxt('refCircle.center', usecols=(0,1), unpack=True)
                
        x_circr = [rCentx + 3.0*cosd(t) for t in range(0,359,1)]
        y_circr = [rCenty + 3.0*sind(t) for t in range(0,359,1)]
        
        verts_circr = list(zip(x_circr,y_circr))
        rcirc_filter = Path(verts_circr)
        
        stars_circr = rcirc_filter.contains_points(xy_points)    
        
        i_mag_cr = [i_mag[i] for i in range(len(i_mag)) if (stars_circr[i])]
        gmi_cr = [gmi[i] for i in range(len(i_mag)) if (stars_circr[i])]
        i_ra_cr = [i_ra[i] for i in range(len(i_mag)) if (stars_circr[i])]
        i_dec_cr = [i_dec[i] for i in range(len(i_mag)) if (stars_circr[i])]
        i_rad_cr = [i_rad[i] for i in range(len(i_mag)) if (stars_circr[i])]
        i_decd_cr = [i_decd[i] for i in range(len(i_mag)) if (stars_circr[i])]
        i_x_cr = [ix[i] for i in range(len(i_mag)) if (stars_circr[i])]
        i_y_cr = [iy[i] for i in range(len(i_mag)) if (stars_circr[i])]
        fwhm_scr = [fwhm_s[i] for i in range(len(i_mag)) if (stars_circr[i])]
        
        i_mag_fc = [i_mag[i] for i in range(len(i_mag)) if (stars_circ[i] and stars_f[i])]
        i_ierr_fc = [i_ierr[i] for i in range(len(i_mag)) if (stars_circ[i] and stars_f[i])]
        g_ierr_fc = [g_ierr[i] for i in range(len(i_mag)) if (stars_circ[i] and stars_f[i])]
        g_mag_fc = [i_mag[i] for i in range(len(i_mag)) if (stars_circ[i] and stars_f[i])]
        gmi_fc = [gmi[i] for i in range(len(i_mag)) if (stars_circ[i] and stars_f[i])]
        i_ra_fc = [i_ra[i] for i in range(len(i_mag)) if (stars_circ[i] and stars_f[i])]
        i_dec_fc = [i_dec[i] for i in range(len(i_mag)) if (stars_circ[i] and stars_f[i])]
        i_rad_fc = [i_rad[i] for i in range(len(i_mag)) if (stars_circ[i] and stars_f[i])]
        i_decd_fc = [i_decd[i] for i in range(len(i_mag)) if (stars_circ[i] and stars_f[i])]
        i_x_fc = [ix[i] for i in range(len(i_mag)) if (stars_circ[i] and stars_f[i])]
        i_y_fc = [iy[i] for i in range(len(i_mag)) if (stars_circ[i] and stars_f[i])]
        fwhm_sfc = [fwhm_s[i] for i in range(len(i_mag)) if (stars_circ[i] and stars_f[i])]
        index_fc = [i for i in range(len(i_mag)) if (stars_circ[i] and stars_f[i])]
        # with open('index_fc.txt', 'w+') as spud1:
        #     for i,item in enumerate(index_fc):
        #         print >> spud1, item
        
        # print len(i_mag_fc), 'filter stars in circle'
        
        # print 'max i mag in circle = ', min(i_mag_fc)
        
        rs = np.array([51, 77, 90, 180])
        for r in rs:
            x_circ = [yedges[y_cent] + r/60.*cosd(t) for t in range(0,359,1)]
            y_circ = [xedges[x_cent] + r/60.*sind(t) for t in range(0,359,1)]

            verts_circ = list(zip(x_circ,y_circ))
            circ_filter = Path(verts_circ)

            stars_circ = circ_filter.contains_points(xy_points)
            i_x_fc = [ix[i] for i in range(len(i_mag)) if (stars_circ[i] and stars_f[i])]
            i_y_fc = [iy[i] for i in range(len(i_mag)) if (stars_circ[i] and stars_f[i])]

            # fcirc_file = 'circle'+repr(r)+'.txt'
            # with open(fcirc_file,'w+') as f3:
            #     for i,x in enumerate(i_x_fc):
            #         print >> f3, i_x_fc[i], i_y_fc[i]
        
        i_mag_fcr = [i_mag[i] for i in range(len(i_mag)) if (stars_circr[i] and stars_f[i])]
        i_ierr_fcr = [i_ierr[i] for i in range(len(i_mag)) if (stars_circr[i] and stars_f[i])]
        g_ierr_fcr = [g_ierr[i] for i in range(len(i_mag)) if (stars_circr[i] and stars_f[i])]
        g_mag_fcr = [i_mag[i] for i in range(len(i_mag)) if (stars_circr[i] and stars_f[i])]
        gmi_fcr = [gmi[i] for i in range(len(i_mag)) if (stars_circr[i] and stars_f[i])]
        i_ra_fcr = [i_ra[i] for i in range(len(i_mag)) if (stars_circr[i] and stars_f[i])]
        i_dec_fcr = [i_dec[i] for i in range(len(i_mag)) if (stars_circr[i] and stars_f[i])]
        i_rad_fcr = [i_rad[i] for i in range(len(i_mag)) if (stars_circr[i] and stars_f[i])]
        i_decd_fcr = [i_decd[i] for i in range(len(i_mag)) if (stars_circr[i] and stars_f[i])]
        i_x_fcr = [ix[i] for i in range(len(i_mag)) if (stars_circr[i] and stars_f[i])]
        i_y_fcr = [iy[i] for i in range(len(i_mag)) if (stars_circr[i] and stars_f[i])]
        fwhm_sfcr = [fwhm_s[i] for i in range(len(i_mag)) if (stars_circr[i] and stars_f[i])]
        
        rcirc_c_x = ra_corner-(rCentx/60.)
        rcirc_c_y = (rCenty/60.)+dec_corner
        rcirc_pix_x, rcirc_pix_y = w.wcs_world2pix(rcirc_c_x,rcirc_c_y,1)
        ra_cr, dec_cr = w.all_pix2world(rcirc_pix_x, rcirc_pix_y,1)
        ra_cr_d,dec_cr_d = deg2HMS(ra=ra_cr, dec=dec_cr, round=False)
        
        circ_c_x = ra_corner-(yedges[y_cent]/60.)
        circ_c_y = (xedges[x_cent]/60.)+dec_corner
        circ_pix_x, circ_pix_y = w.wcs_world2pix(circ_c_x,circ_c_y,1)
        ra_c, dec_c = w.all_pix2world(circ_pix_x, circ_pix_y,1)
        ra_c_d,dec_c_d = deg2HMS(ra=ra_c, dec=dec_c, round=False)
        # print 'Peak RA:',ra_c_d,':: Peak Dec:',dec_c_d
        
        hi_x_circ, hi_y_circ = getHIellipse(title_string, ra_corner, dec_corner)
        hi_c_ra, hi_c_dec = getHIellipse(title_string, ra_corner, dec_corner, centroid=True)        
        hi_pix_x,hi_pix_y = w.wcs_world2pix(hi_c_ra,hi_c_dec,1)
        
        sep = dist2HIcentroid(ra_c_d, dec_c_d, hi_c_ra, hi_c_dec)
        
        # print "m-M = {:5.2f} | d = {:4.2f} Mpc | α = {:s}, δ = {:s}, Δʜɪ = {:5.1f}' | N = {:4d} | σ = {:6.3f} | ξ = {:6.3f}% | η = {:6.3f}%".format(dm, mpc, ra_c_d, dec_c_d, sep/60., n_in_filter, S[x_cent_S][y_cent_S], pct, pct_hi*100.)
        
        fig = plt.figure(figsize=(8,6))
        # plot
        # print "Plotting for m-M = ",dm
        ax0 = plt.subplot(2,2,1)
        plt.scatter(i_ra, i_dec,  color='black', marker='o', s=1, edgecolors='none')
        plt.plot(x_circ,y_circ,linestyle='-', color='magenta')
        plt.plot(x_circr,y_circr,linestyle='-', color='gold')
        plt.plot(hi_x_circ,hi_y_circ,linestyle='-', color='limegreen')
        # plt.scatter(i_ra_c, i_dec_c,  color='red', marker='o', s=3, edgecolors='none')
        plt.scatter(i_ra_f, i_dec_f,  c='red', marker='o', s=10, edgecolors='none')
        # plt.clim(0,2)
        # plt.colorbar()
        plt.ylabel('Dec (arcmin)')
        plt.xlim(0,max(i_ra))
        plt.ylim(0,max(i_dec))
        plt.title('sky positions')
        ax0.set_aspect('equal')
    
        ax1 = plt.subplot(2,2,2)
        
        if os.path.isfile('i_gmi_compl.gr.out'):
            gmiCompl, iCompl = np.loadtxt('i_gmi_compl.gr.out',usecols=(0,1),unpack=True)
            plt.plot(gmiCompl,iCompl, linestyle='--', color='green')
        if os.path.isfile('i_gmi_compl.gr2.out'):
            gmiCompl, iCompl = np.loadtxt('i_gmi_compl.gr2.out',usecols=(0,1),unpack=True)
            plt.plot(gmiCompl,iCompl, linestyle='--', color='red')
        if os.path.isfile('i_gmi_compl2.out'):
            iCompl,gmiCompl = np.loadtxt('i_gmi_compl2.out',usecols=(0,1),unpack=True)
            plt.plot(gmiCompl,iCompl, linestyle='--', color='blue')
        
        plt.plot(gi_iso,i_m_iso,linestyle='-', color='blue')
        plt.scatter(gmi, i_mag,  color='black', marker='o', s=1, edgecolors='none')
        plt.scatter(gmi_f, i_mag_f,  color='red', marker='o', s=15, edgecolors='none')

        # plt.scatter(gmi_c, i_mag_c,  color='red', marker='o', s=3, edgecolors='none')
        plt.errorbar(bxvals, bcenters, xerr=i_ierrAVG, yerr=gmi_errAVG, linestyle='None', color='black', capsize=0, ms=0)
        plt.tick_params(axis='y',left='on',right='off',labelleft='on',labelright='off')
        ax1.yaxis.set_label_position('left')
        plt.ylabel('$i_0$')
        plt.xlabel('$(g-i)_0$')
        plt.ylim(25,15)
        plt.xlim(-1,4)
        plt.title('m-M = ' + '{:5.2f}'.format(dm) + ' (' + '{0:4.2f}'.format(mpc) +  ' Mpc)')
        ax1.set_aspect(0.5)
    
        ax2 = plt.subplot(2,2,3)
    
        extent = [yedges[0], yedges[-1], xedges[-1], xedges[0]]
        plt.imshow(S, extent=extent, interpolation='nearest',cmap=cm.gray)
        # plt.imshow(segm, extent=extent, cmap=rand_cmap, alpha=0.5)
        cbar_S = plt.colorbar()
        cbar_S.set_label('$\sigma$ from local mean')
        # cbar_S.tick_params(labelsize=10)
        plt.plot(x_circ,y_circ,linestyle='-', color='magenta')
        plt.plot(x_circr,y_circr,linestyle='-', color='gold')
        plt.plot(hi_x_circ,hi_y_circ,linestyle='-', color='limegreen')
        # X, Y = np.meshgrid(xedges,yedges)
        # ax3.pcolormesh(X,Y,grid_gaus)
        plt.xlabel('RA (arcmin)')
        plt.ylabel('Dec (arcmin)')
        plt.title('smoothed stellar density')
        # plt.ylabel('Dec (arcmin)')
        plt.xlim(0,max(i_ra))
        plt.ylim(0,max(i_dec))
        ax2.set_aspect('equal')
    
        # ax3 = plt.subplot(2,2,4)
        ax3 = plt.subplot2grid((2,4), (1,2))
        plt.scatter(gmi_c, i_mag_c,  color='black', marker='o', s=3, edgecolors='none')
        plt.scatter(gmi_fc, i_mag_fc,  color='red', marker='o', s=15, edgecolors='none')    
        plt.tick_params(axis='y',left='on',right='on',labelleft='off',labelright='off')
        ax0.yaxis.set_label_position('left')
        plt.title('detection')
        plt.xlabel('$(g-i)_0$')
        plt.ylabel('$i_0$')
        plt.ylim(25,15)
        plt.xlim(-1,4)
        # ax3.set_aspect(0.5)    
    
        ax4 = plt.subplot2grid((2,4), (1,3), sharey=ax3)
        plt.scatter(gmi_cr, i_mag_cr,  color='black', marker='o', s=3, edgecolors='none')
        plt.scatter(gmi_fcr, i_mag_fcr,  color='red', marker='o', s=15, edgecolors='none')    
        plt.tick_params(axis='y',left='on',right='on',labelleft='off',labelright='on')
        plt.title('reference')
        ax0.yaxis.set_label_position('left')
        plt.xlabel('$(g-i)_0$')
        plt.ylim(25,15)
        plt.xlim(-1,4)
        # ax3.set _aspect(0.5)
        plt.tight_layout()
        plt.savefig(out_file)
        fig.clf()
        
        
    pass
Example #3
0
def main():
    fwhm_sm = float(sys.argv[1])
    objects = [
        'AGC174540', 'AGC198511', 'AGC198606', 'AGC215417', 'AGC226067',
        'AGC227987', 'AGC229326', 'AGC238626', 'AGC238713', 'AGC249000',
        'AGC249282', 'AGC249320'
    ]
    objects2 = [
        'AGC249323', 'AGC249525', 'AGC258237', 'AGC258242', 'AGC258459',
        'AGC268069', 'AGC268074', 'HI0932+24', 'HI0959+19', 'HI1037+21',
        'HI1050+23', 'HI1151+20'
    ]

    filter_file = os.path.dirname(os.path.abspath(__file__)) + '/filter.txt'

    # plt.clf()
    fig = plt.figure(figsize=(10, 7))

    outer = gridspec.GridSpec(4, 3, wspace=0.1, hspace=0.1)
    for i, obj in enumerate(objects2):
        print obj
        inner = gridspec.GridSpecFromSubplotSpec(1,
                                                 2,
                                                 subplot_spec=outer[i],
                                                 wspace=0.1,
                                                 hspace=0.1)
        # set up some filenames
        folder = '/Volumes/galileo/uchvc/targets/' + obj.lower() + '/'
        mag_file = folder + 'calibrated_mags.dat'
        fits_file_i = folder + obj + '_i.fits'
        dmr, mpcr, sepr, nr, sr, pctr, pct_hir = np.loadtxt(
            folder + 'search_{:3.1f}.txt'.format(fwhm_sm),
            usecols=(0, 1, 4, 5, 6, 7, 8),
            unpack=True)
        close = np.where(sepr < 480.)
        maxloc = np.argmax(pctr)
        maxclose = np.argmax(pctr[close])

        fits_i = fits.open(fits_file_i)

        # dm = dmr[maxloc]
        dm = (dmr[close])[maxclose]

        # read in magnitudes, colors, and positions(x,y)
        # gxr,gyr,g_magr,g_ierrr,ixr,iyr,i_magr,i_ierrr,gmir,fwhm_sr= np.loadtxt(mag_file,usecols=(0,1,2,3,4,5,6,7,8,11),unpack=True)
        gxr, gyr, g_magr, g_ierrr, ixr, iyr, i_magr, i_ierrr, gmir = np.loadtxt(
            mag_file, usecols=(0, 1, 2, 3, 4, 5, 6, 7, 8), unpack=True)
        # print len(gxr), "total stars"
        fwhm_sr = np.ones_like(gxr)
        # filter out the things with crappy color errors
        color_error_cut = np.sqrt(2.0) * 0.2
        mag_error_cut = 0.2

        gmi_errr = [
            np.sqrt(g_ierrr[i]**2 + i_ierrr[i]**2) for i in range(len(gxr))
        ]
        gx = [
            gxr[i] for i in range(len(gxr)) if (abs(
                gmi_errr[i] < color_error_cut and i_ierrr[i] < mag_error_cut))
        ]
        gy = [
            gyr[i] for i in range(len(gxr)) if (abs(
                gmi_errr[i] < color_error_cut and i_ierrr[i] < mag_error_cut))
        ]
        g_mag = [
            g_magr[i] for i in range(len(gxr)) if (abs(
                gmi_errr[i] < color_error_cut and i_ierrr[i] < mag_error_cut))
        ]
        g_ierr = [
            g_ierrr[i] for i in range(len(gxr)) if (abs(
                gmi_errr[i] < color_error_cut and i_ierrr[i] < mag_error_cut))
        ]
        ix = [
            ixr[i] for i in range(len(gxr)) if (abs(
                gmi_errr[i] < color_error_cut and i_ierrr[i] < mag_error_cut))
        ]
        iy = [
            iyr[i] for i in range(len(gxr)) if (abs(
                gmi_errr[i] < color_error_cut and i_ierrr[i] < mag_error_cut))
        ]
        i_mag = [
            i_magr[i] for i in range(len(gxr)) if (abs(
                gmi_errr[i] < color_error_cut and i_ierrr[i] < mag_error_cut))
        ]
        i_ierr = np.array([
            i_ierrr[i] for i in range(len(gxr)) if (abs(
                gmi_errr[i] < color_error_cut and i_ierrr[i] < mag_error_cut))
        ])
        gmi = [
            gmir[i] for i in range(len(gxr)) if (abs(
                gmi_errr[i] < color_error_cut and i_ierrr[i] < mag_error_cut))
        ]
        fwhm_s = [
            fwhm_sr[i] for i in range(len(gxr)) if (abs(
                gmi_errr[i] < color_error_cut and i_ierrr[i] < mag_error_cut))
        ]
        gmi_err = np.array([
            np.sqrt(g_ierrr[i]**2 + i_ierrr[i]**2) for i in range(len(gxr)) if
            (abs(gmi_errr[i] < color_error_cut and i_ierrr[i] < mag_error_cut))
        ])

        i_ierrAVG, bedges, binid = ss.binned_statistic(i_mag,
                                                       i_ierr,
                                                       statistic='median',
                                                       bins=10,
                                                       range=[15, 25])
        gmi_errAVG, bedges, binid = ss.binned_statistic(i_mag,
                                                        gmi_err,
                                                        statistic='median',
                                                        bins=10,
                                                        range=[15, 25])

        bcenters = (bedges[:-1] + bedges[1:]) / 2
        bxvals = [3.75, 3.75, 3.75, 3.75, 3.75, 3.75, 3.75, 3.75, 3.75, 3.75]
        # print bcenters
        # print i_ierrAVG
        # print gmi_errAVG
        # print len(gx), "after color+mag error cut"
        # nid = np.loadtxt(mag_file,usecols=(0,),dtype=int,unpack=True)
        pixcrd = zip(ix, iy)

        # print "Reading WCS info from image header..."
        # Parse the WCS keywords in the primary HDU
        warnings.filterwarnings('ignore', category=UserWarning, append=True)
        w = wcs.WCS(fits_i[0].header)
        # print fits_i[0].header['naxis1'], fits_i[0].header['naxis2']
        footprint = w.calc_footprint()
        se_corner = footprint[0]
        ne_corner = footprint[1]
        nw_corner = footprint[2]
        sw_corner = footprint[3]
        # print se_corner, ne_corner, nw_corner, sw_corner
        width = (ne_corner[0] - nw_corner[0]) * 60.
        height = (ne_corner[1] - se_corner[1]) * 60.
        # print width, height

        # Print out the "name" of the WCS, as defined in the FITS header
        # print w.wcs.name

        # Print out all of the settings that were parsed from the header
        # w.wcs.print_contents()

        # Convert pixel coordinates to world coordinates
        # The second argument is "origin" -- in this case we're declaring we
        # have 1-based (Fortran-like) coordinates.
        world = w.all_pix2world(pixcrd, 1)
        ra_corner, dec_corner = w.all_pix2world(0, 0, 1)
        ra_c_d, dec_c_d = deg2HMS(ra=ra_corner, dec=dec_corner, round=True)
        # print 'Corner RA:',ra_c_d,':: Corner Dec:',dec_c_d

        fits_i.close()

        # split the ra and dec out into individual arrays and transform to arcmin from the corner
        i_ra = [
            abs((world[i, 0] - ra_corner) * 60)
            for i in range(len(world[:, 0]))
        ]
        i_dec = [
            abs((world[i, 1] - dec_corner) * 60)
            for i in range(len(world[:, 1]))
        ]
        # also preserve the decimal degrees for reference
        i_rad = [world[i, 0] for i in range(len(world[:, 0]))]
        i_decd = [world[i, 1] for i in range(len(world[:, 1]))]

        cm_filter, gi_iso, i_m_iso = make_filter(dm, filter_file)
        stars_f = filter_sources(i_mag,
                                 i_ierr,
                                 gmi,
                                 gmi_err,
                                 cm_filter,
                                 filter_sig=1)

        xy_points = zip(i_ra, i_dec)

        # make new vectors containing only the filtered points

        i_mag_f = [i_mag[i] for i in range(len(i_mag)) if (stars_f[i])]
        g_mag_f = [g_mag[i] for i in range(len(i_mag)) if (stars_f[i])]
        gmi_f = [gmi[i] for i in range(len(i_mag)) if (stars_f[i])]
        i_ra_f = [i_ra[i] for i in range(len(i_mag)) if (stars_f[i])]
        i_dec_f = [i_dec[i] for i in range(len(i_mag)) if (stars_f[i])]
        i_rad_f = [i_rad[i] for i in range(len(i_mag)) if (stars_f[i])]
        i_decd_f = [i_decd[i] for i in range(len(i_mag)) if (stars_f[i])]
        i_x_f = [ix[i] for i in range(len(i_mag)) if (stars_f[i])]
        i_y_f = [iy[i] for i in range(len(i_mag)) if (stars_f[i])]
        fwhm_sf = [fwhm_s[i] for i in range(len(i_mag)) if (stars_f[i])]
        n_in_filter = len(i_mag_f)

        xedges, x_cent, yedges, y_cent, S, x_cent_S, y_cent_S, pltsig, tbl = grid_smooth(
            i_ra_f, i_dec_f, fwhm_sm, width, height)

        # xedges, x_cent, yedges, y_cent, S, x_cent_S, y_cent_S, pltsig, tbl = grid_smooth(i_ra, i_dec, 2.0, width, height)

        cosd = lambda x: np.cos(np.deg2rad(x))
        sind = lambda x: np.sin(np.deg2rad(x))

        hi_x_circ, hi_y_circ = getHIellipse(obj, ra_corner, dec_corner)
        # hi_c_x, hi_c_y = abs((hi_c_ra-ra_corner)*60), abs((hi_c_dec-dec_corner)*60)
        #
        # t = np.array(range(0,359,1))
        # ell = np.array([a*cosd(t) , b*sind(t)])
        # rot = np.array([[cosd(pa) , -sind(pa)],[sind(pa) , cosd(pa)]])
        # ell_rot = np.zeros((2,ell.shape[1]))
        # for i in range(ell.shape[1]):
        #     ell_rot[:,i] = np.dot(rot,ell[:,i])
        # hi_x_circ, hi_y_circ = hi_c_x+ell_rot[0,:], hi_c_y+ell_rot[1,:]
        # hi_x_circ = [hi_c_x + a*cosd(t) for t in range(0,359,1)]
        # hi_y_circ = [hi_c_y + b*sind(t) for t in range(0,359,1)]

        x_circ = [yedges[y_cent] + 3.0 * cosd(t) for t in range(0, 359, 1)]
        y_circ = [xedges[x_cent] + 3.0 * sind(t) for t in range(0, 359, 1)]

        ax1 = plt.Subplot(fig, inner[0])
        if os.path.isfile(folder + 'i_gmi_compl.gr.out'):
            gmiCompl, iCompl = np.loadtxt(folder + 'i_gmi_compl.gr.out',
                                          usecols=(0, 1),
                                          unpack=True)
            ax1.plot(gmiCompl, iCompl, linestyle='--', color='green')
        # if os.path.isfile(folder+'i_gmi_compl.gr2.out'):
        #     gmiCompl, iCompl = np.loadtxt(folder+'i_gmi_compl.gr2.out',usecols=(0,1),unpack=True)
        #     ax1.plot(gmiCompl,iCompl, linestyle='--', color='red')
        # if os.path.isfile(folder+'i_gmi_compl2.out'):
        #     iCompl,gmiCompl = np.loadtxt(folder+'i_gmi_compl2.out',usecols=(0,1),unpack=True)
        #     ax1.plot(gmiCompl,iCompl, linestyle='--', color='blue')

        ax1.plot(gi_iso, i_m_iso, linestyle='-', color='blue')
        ax1.scatter(gmi,
                    i_mag,
                    color='black',
                    marker='o',
                    s=1,
                    edgecolors='none')
        ax1.scatter(gmi_f,
                    i_mag_f,
                    color='red',
                    marker='o',
                    s=15,
                    edgecolors='none')
        ax1.errorbar(bxvals,
                     bcenters,
                     xerr=i_ierrAVG,
                     yerr=gmi_errAVG,
                     linestyle='None',
                     color='black',
                     capsize=0,
                     ms=0)
        ax1.tick_params(axis='y',
                        left='on',
                        right='off',
                        labelleft='on',
                        labelright='off')
        ax1.yaxis.set_label_position('left')
        ax1.set_xticks([-1, 0, 1, 2, 3, 4])
        ax1.set_yticks([15, 17, 19, 21, 23, 25])
        ax1.set_ylabel('$i_0$')
        ax1.set_xlabel('$(g-i)_0$')
        ax1.set_ylim(25, 15)
        ax1.set_xlim(-1, 4)
        ax1.set_aspect(0.5)
        ax1.set_title('$m-M = ${:5.2f}'.format(dm), size='small')
        fig.add_subplot(ax1)

        ax2 = plt.Subplot(fig, inner[1])
        extent = [yedges[0], yedges[-1], xedges[-1], xedges[0]]
        ax2.imshow(S, extent=extent, interpolation='nearest', cmap=cm.gray)
        # cbar_S = plt.colorbar()
        # cbar_S.set_label('$\sigma$ from local mean')
        ax2.plot(hi_x_circ, hi_y_circ, linestyle='-', color='limegreen')
        ax2.plot(x_circ, y_circ, linestyle='-', color='magenta')
        ax2.tick_params(axis='y',
                        left='off',
                        right='on',
                        labelleft='off',
                        labelright='on')
        ax2.yaxis.set_label_position('right')
        ax2.set_xticks([0, 5, 10, 15, 20])
        ax2.set_yticks([0, 5, 10, 15, 20])
        ax2.set_xlabel('RA (arcmin)')
        ax2.set_ylabel('Dec (arcmin)')
        # if obj.startswith('HI1151'):
        #     ax2.set_title('AGC219656', size='small')
        # else:
        ax2.set_title(obj, size='small')
        ax2.set_xlim(0, max(i_ra))
        ax2.set_ylim(0, max(i_dec))
        ax2.set_aspect('equal')
        fig.add_subplot(ax2)

    outer.tight_layout(fig)
    plt.savefig('detections_close_{:3.1f}.pdf'.format(fwhm_sm))

    pass
Example #4
0
def main():
    objects = OrderedDict([('AGC198606', 24.72), ('AGC215417', 22.69),
                           ('HI1151+20', 24.76), ('AGC249525', 26.07),
                           ('AGC268069', 24.24)])
    smooths = OrderedDict([('AGC198606', 2.0), ('AGC215417', 3.0),
                           ('HI1151+20', 2.0), ('AGC249525', 3.0),
                           ('AGC268069', 3.0)])
    filter_file = os.path.dirname(os.path.abspath(__file__)) + '/filter.txt'

    # plt.clf()

    for i, obj in enumerate(objects.keys()):
        # set up some filenames
        folder = '/Volumes/discovery1/uchvc/targets/' + obj.lower() + '/'
        mag_file = folder + 'calibrated_mags.dat'
        fits_file_i = folder + obj + '_i.fits'
        fits_i = fits.open(fits_file_i)

        dm = objects[obj]
        mpc = pow(10, ((dm + 5.) / 5.)) / 1000000.

        # read in magnitudes, colors, and positions(x,y)
        # gxr,gyr,g_magr,g_ierrr,ixr,iyr,i_magr,i_ierrr,gmir,fwhm_sr= np.loadtxt(mag_file,usecols=(0,1,2,3,4,5,6,7,8,11),unpack=True)
        gxr, gyr, g_magr, g_ierrr, ixr, iyr, i_magr, i_ierrr, gmir = np.loadtxt(
            mag_file, usecols=(0, 1, 2, 3, 4, 5, 6, 7, 8), unpack=True)
        # print len(gxr), "total stars"
        fwhm_sr = np.ones_like(gxr)
        # filter out the things with crappy color errors
        color_error_cut = np.sqrt(2.0) * 0.2
        mag_error_cut = 0.2

        gmi_errr = [
            np.sqrt(g_ierrr[i]**2 + i_ierrr[i]**2) for i in range(len(gxr))
        ]
        gx = [
            gxr[i] for i in range(len(gxr)) if (abs(
                gmi_errr[i] < color_error_cut and i_ierrr[i] < mag_error_cut))
        ]
        gy = [
            gyr[i] for i in range(len(gxr)) if (abs(
                gmi_errr[i] < color_error_cut and i_ierrr[i] < mag_error_cut))
        ]
        g_mag = [
            g_magr[i] for i in range(len(gxr)) if (abs(
                gmi_errr[i] < color_error_cut and i_ierrr[i] < mag_error_cut))
        ]
        g_ierr = [
            g_ierrr[i] for i in range(len(gxr)) if (abs(
                gmi_errr[i] < color_error_cut and i_ierrr[i] < mag_error_cut))
        ]
        ix = [
            ixr[i] for i in range(len(gxr)) if (abs(
                gmi_errr[i] < color_error_cut and i_ierrr[i] < mag_error_cut))
        ]
        iy = [
            iyr[i] for i in range(len(gxr)) if (abs(
                gmi_errr[i] < color_error_cut and i_ierrr[i] < mag_error_cut))
        ]
        i_mag = np.array([
            i_magr[i] for i in range(len(gxr)) if (abs(
                gmi_errr[i] < color_error_cut and i_ierrr[i] < mag_error_cut))
        ])
        i_ierr = np.array([
            i_ierrr[i] for i in range(len(gxr)) if (abs(
                gmi_errr[i] < color_error_cut and i_ierrr[i] < mag_error_cut))
        ])
        gmi = [
            gmir[i] for i in range(len(gxr)) if (abs(
                gmi_errr[i] < color_error_cut and i_ierrr[i] < mag_error_cut))
        ]
        fwhm_s = [
            fwhm_sr[i] for i in range(len(gxr)) if (abs(
                gmi_errr[i] < color_error_cut and i_ierrr[i] < mag_error_cut))
        ]
        gmi_err = np.array([
            np.sqrt(g_ierrr[i]**2 + i_ierrr[i]**2) for i in range(len(gxr)) if
            (abs(gmi_errr[i] < color_error_cut and i_ierrr[i] < mag_error_cut))
        ])

        i_ierrAVG, bedges, binid = ss.binned_statistic(i_mag,
                                                       i_ierr,
                                                       statistic='median',
                                                       bins=10,
                                                       range=[15, 25])
        gmi_errAVG, bedges, binid = ss.binned_statistic(i_mag,
                                                        gmi_err,
                                                        statistic='median',
                                                        bins=10,
                                                        range=[15, 25])

        bcenters = (bedges[:-1] + bedges[1:]) / 2
        bxvals = [3.75, 3.75, 3.75, 3.75, 3.75, 3.75, 3.75, 3.75, 3.75, 3.75]
        # print bcenters
        # print i_ierrAVG
        # print gmi_errAVG
        # print len(gx), "after color+mag error cut"
        # nid = np.loadtxt(mag_file,usecols=(0,),dtype=int,unpack=True)
        pixcrd = list(zip(ix, iy))

        # print "Reading WCS info from image header..."
        # Parse the WCS keywords in the primary HDU
        warnings.filterwarnings('ignore', category=UserWarning, append=True)
        w = wcs.WCS(fits_i[0].header)
        # print fits_i[0].header['naxis1'], fits_i[0].header['naxis2']
        footprint = w.calc_footprint()
        se_corner = footprint[0]
        ne_corner = footprint[1]
        nw_corner = footprint[2]
        sw_corner = footprint[3]
        # print se_corner, ne_corner, nw_corner, sw_corner
        width = (ne_corner[0] - nw_corner[0]) * 60.
        height = (ne_corner[1] - se_corner[1]) * 60.
        # print width, height

        # Print out the "name" of the WCS, as defined in the FITS header
        # print w.wcs.name

        # Print out all of the settings that were parsed from the header
        # w.wcs.print_contents()

        # Convert pixel coordinates to world coordinates
        # The second argument is "origin" -- in this case we're declaring we
        # have 1-based (Fortran-like) coordinates.
        world = w.all_pix2world(pixcrd, 1)
        ra_corner, dec_corner = w.all_pix2world(0, 0, 1)
        ra_c_d, dec_c_d = deg2HMS(ra=ra_corner, dec=dec_corner, round=True)
        # print 'Corner RA:',ra_c_d,':: Corner Dec:',dec_c_d

        fits_i.close()

        # split the ra and dec out into individual arrays and transform to arcmin from the corner
        i_ra = [
            abs((world[i, 0] - ra_corner) * 60)
            for i in range(len(world[:, 0]))
        ]
        i_dec = [
            abs((world[i, 1] - dec_corner) * 60)
            for i in range(len(world[:, 1]))
        ]
        # also preserve the decimal degrees for reference
        i_rad = [world[i, 0] for i in range(len(world[:, 0]))]
        i_decd = [world[i, 1] for i in range(len(world[:, 1]))]

        cm_filter, gi_iso, i_m_iso = make_filter(dm, filter_file)
        stars_f = filter_sources(i_mag,
                                 i_ierr,
                                 gmi,
                                 gmi_err,
                                 cm_filter,
                                 filter_sig=1)

        xy_points = list(zip(i_ra, i_dec))

        # make new vectors containing only the filtered points

        i_mag_f = [i_mag[i] for i in range(len(i_mag)) if (stars_f[i])]
        g_mag_f = [g_mag[i] for i in range(len(i_mag)) if (stars_f[i])]
        gmi_f = [gmi[i] for i in range(len(i_mag)) if (stars_f[i])]
        i_ra_f = [i_ra[i] for i in range(len(i_mag)) if (stars_f[i])]
        i_dec_f = [i_dec[i] for i in range(len(i_mag)) if (stars_f[i])]
        i_rad_f = [i_rad[i] for i in range(len(i_mag)) if (stars_f[i])]
        i_decd_f = [i_decd[i] for i in range(len(i_mag)) if (stars_f[i])]
        i_x_f = [ix[i] for i in range(len(i_mag)) if (stars_f[i])]
        i_y_f = [iy[i] for i in range(len(i_mag)) if (stars_f[i])]
        fwhm_sf = [fwhm_s[i] for i in range(len(i_mag)) if (stars_f[i])]
        n_in_filter = len(i_mag_f)

        fwhm = smooths[obj]
        xedges, x_cent, yedges, y_cent, S, x_cent_S, y_cent_S, pltsig, tbl = grid_smooth(
            i_ra_f, i_dec_f, fwhm, width, height)

        # xedges, x_cent, yedges, y_cent, S, x_cent_S, y_cent_S, pltsig, tbl = grid_smooth(i_ra, i_dec, 2.0, width, height)

        cosd = lambda x: np.cos(np.deg2rad(x))
        sind = lambda x: np.sin(np.deg2rad(x))

        hi_x_circ, hi_y_circ = getHIellipse(obj, ra_corner, dec_corner)
        # hi_c_x, hi_c_y = abs((hi_c_ra-ra_corner)*60), abs((hi_c_dec-dec_corner)*60)
        #
        # t = np.array(range(0,359,1))
        # ell = np.array([a*cosd(t) , b*sind(t)])
        # rot = np.array([[cosd(pa) , -sind(pa)],[sind(pa) , cosd(pa)]])
        # ell_rot = np.zeros((2,ell.shape[1]))
        # for i in range(ell.shape[1]):
        #     ell_rot[:,i] = np.dot(rot,ell[:,i])
        # hi_x_circ, hi_y_circ = hi_c_x+ell_rot[0,:], hi_c_y+ell_rot[1,:]
        # hi_x_circ = [hi_c_x + a*cosd(t) for t in range(0,359,1)]
        # hi_y_circ = [hi_c_y + b*sind(t) for t in range(0,359,1)]

        x_circ = [yedges[y_cent] + 3.0 * cosd(t) for t in range(0, 359, 1)]
        y_circ = [xedges[x_cent] + 3.0 * sind(t) for t in range(0, 359, 1)]

        verts_circ = list(zip(x_circ, y_circ))
        circ_filter = Path(verts_circ)

        stars_circ = circ_filter.contains_points(xy_points)

        i_mag_c = [i_mag[i] for i in range(len(i_mag)) if (stars_circ[i])]
        gmi_c = [gmi[i] for i in range(len(i_mag)) if (stars_circ[i])]
        i_ra_c = [i_ra[i] for i in range(len(i_mag)) if (stars_circ[i])]
        i_dec_c = [i_dec[i] for i in range(len(i_mag)) if (stars_circ[i])]
        i_rad_c = [i_rad[i] for i in range(len(i_mag)) if (stars_circ[i])]
        i_decd_c = [i_decd[i] for i in range(len(i_mag)) if (stars_circ[i])]
        i_x_c = [ix[i] for i in range(len(i_mag)) if (stars_circ[i])]
        i_y_c = [iy[i] for i in range(len(i_mag)) if (stars_circ[i])]
        fwhm_sc = [fwhm_s[i] for i in range(len(i_mag)) if (stars_circ[i])]

        # make a random reference cmd to compare to
        if not os.path.isfile(folder + 'refCircle.center'):
            rCentx = 16.0 * np.random.random() + 2.0
            rCenty = 16.0 * np.random.random() + 2.0
            with open('refCircle.center', 'w+') as rc:
                print('{:8.4f} {:8.4f}'.format(rCentx, rCenty), file=rc)
        else:
            rCentx, rCenty = np.loadtxt(folder + 'refCircle.center',
                                        usecols=(0, 1),
                                        unpack=True)

        x_circr = [rCentx + 3.0 * cosd(t) for t in range(0, 359, 1)]
        y_circr = [rCenty + 3.0 * sind(t) for t in range(0, 359, 1)]

        verts_circr = list(zip(x_circr, y_circr))
        rcirc_filter = Path(verts_circr)

        stars_circr = rcirc_filter.contains_points(xy_points)

        i_mag_cr = [i_mag[i] for i in range(len(i_mag)) if (stars_circr[i])]
        gmi_cr = [gmi[i] for i in range(len(i_mag)) if (stars_circr[i])]
        i_ra_cr = [i_ra[i] for i in range(len(i_mag)) if (stars_circr[i])]
        i_dec_cr = [i_dec[i] for i in range(len(i_mag)) if (stars_circr[i])]
        i_rad_cr = [i_rad[i] for i in range(len(i_mag)) if (stars_circr[i])]
        i_decd_cr = [i_decd[i] for i in range(len(i_mag)) if (stars_circr[i])]
        i_x_cr = [ix[i] for i in range(len(i_mag)) if (stars_circr[i])]
        i_y_cr = [iy[i] for i in range(len(i_mag)) if (stars_circr[i])]
        fwhm_scr = [fwhm_s[i] for i in range(len(i_mag)) if (stars_circr[i])]

        i_mag_fc = [
            i_mag[i] for i in range(len(i_mag))
            if (stars_circ[i] and stars_f[i])
        ]
        i_ierr_fc = [
            i_ierr[i] for i in range(len(i_mag))
            if (stars_circ[i] and stars_f[i])
        ]
        g_ierr_fc = [
            g_ierr[i] for i in range(len(i_mag))
            if (stars_circ[i] and stars_f[i])
        ]
        g_mag_fc = [
            i_mag[i] for i in range(len(i_mag))
            if (stars_circ[i] and stars_f[i])
        ]
        gmi_fc = [
            gmi[i] for i in range(len(i_mag)) if (stars_circ[i] and stars_f[i])
        ]
        i_ra_fc = [
            i_ra[i] for i in range(len(i_mag))
            if (stars_circ[i] and stars_f[i])
        ]
        i_dec_fc = [
            i_dec[i] for i in range(len(i_mag))
            if (stars_circ[i] and stars_f[i])
        ]
        i_rad_fc = [
            i_rad[i] for i in range(len(i_mag))
            if (stars_circ[i] and stars_f[i])
        ]
        i_decd_fc = [
            i_decd[i] for i in range(len(i_mag))
            if (stars_circ[i] and stars_f[i])
        ]
        i_x_fc = [
            ix[i] for i in range(len(i_mag)) if (stars_circ[i] and stars_f[i])
        ]
        i_y_fc = [
            iy[i] for i in range(len(i_mag)) if (stars_circ[i] and stars_f[i])
        ]
        fwhm_sfc = [
            fwhm_s[i] for i in range(len(i_mag))
            if (stars_circ[i] and stars_f[i])
        ]
        index_fc = [
            i for i in range(len(i_mag)) if (stars_circ[i] and stars_f[i])
        ]

        i_mag_fcr = [
            i_mag[i] for i in range(len(i_mag))
            if (stars_circr[i] and stars_f[i])
        ]
        i_ierr_fcr = [
            i_ierr[i] for i in range(len(i_mag))
            if (stars_circr[i] and stars_f[i])
        ]
        g_ierr_fcr = [
            g_ierr[i] for i in range(len(i_mag))
            if (stars_circr[i] and stars_f[i])
        ]
        g_mag_fcr = [
            i_mag[i] for i in range(len(i_mag))
            if (stars_circr[i] and stars_f[i])
        ]
        gmi_fcr = [
            gmi[i] for i in range(len(i_mag))
            if (stars_circr[i] and stars_f[i])
        ]
        i_ra_fcr = [
            i_ra[i] for i in range(len(i_mag))
            if (stars_circr[i] and stars_f[i])
        ]
        i_dec_fcr = [
            i_dec[i] for i in range(len(i_mag))
            if (stars_circr[i] and stars_f[i])
        ]
        i_rad_fcr = [
            i_rad[i] for i in range(len(i_mag))
            if (stars_circr[i] and stars_f[i])
        ]
        i_decd_fcr = [
            i_decd[i] for i in range(len(i_mag))
            if (stars_circr[i] and stars_f[i])
        ]
        i_x_fcr = [
            ix[i] for i in range(len(i_mag)) if (stars_circr[i] and stars_f[i])
        ]
        i_y_fcr = [
            iy[i] for i in range(len(i_mag)) if (stars_circr[i] and stars_f[i])
        ]
        fwhm_sfcr = [
            fwhm_s[i] for i in range(len(i_mag))
            if (stars_circr[i] and stars_f[i])
        ]

        # fig = plt.figure(figsize=(8.5,8.5))
        fig = plt.figure(figsize=(8.5, 8.5))
        outer = gridspec.GridSpec(2, 2, wspace=0.1, hspace=0.1)
        # inner = gridspec.GridSpecFromSubplotSpec(1, 4, subplot_spec=outer[i], wspace=0.1, hspace=0.1)
        ax1 = plt.Subplot(fig, outer[1])
        if os.path.isfile(folder + 'i_gmi_compl.gr.out'):
            gmiCompl, iCompl = np.loadtxt(folder + 'i_gmi_compl.gr.out',
                                          usecols=(0, 1),
                                          unpack=True)
            ax1.plot(gmiCompl, iCompl, linestyle='--', color='green')
        # if os.path.isfile(folder+'i_gmi_compl.gr2.out'):
        #     gmiCompl, iCompl = np.loadtxt(folder+'i_gmi_compl.gr2.out',usecols=(0,1),unpack=True)
        #     ax1.plot(gmiCompl,iCompl, linestyle='--', color='red')
        # if os.path.isfile(folder+'i_gmi_compl2.out'):
        #     iCompl,gmiCompl = np.loadtxt(folder+'i_gmi_compl2.out',usecols=(0,1),unpack=True)
        #     ax1.plot(gmiCompl,iCompl, linestyle='--', color='blue')

        ax1.plot(gi_iso, i_m_iso, linestyle='-', color='blue')
        ax1.scatter(gmi,
                    i_mag,
                    color='black',
                    marker='o',
                    s=1,
                    edgecolors='none')
        ax1.scatter(gmi_f,
                    i_mag_f,
                    color='red',
                    marker='o',
                    s=10,
                    edgecolors='none')
        ax1.errorbar(bxvals,
                     bcenters,
                     xerr=i_ierrAVG,
                     yerr=gmi_errAVG,
                     linestyle='None',
                     color='black',
                     capsize=0,
                     ms=0)
        ax1.tick_params(axis='y',
                        left='on',
                        right='off',
                        labelleft='on',
                        labelright='off')
        ax1.yaxis.set_label_position('left')
        ax1.set_xticks([-1, 0, 1, 2, 3, 4])
        ax1.set_yticks([15, 17, 19, 21, 23, 25])
        ax1.set_ylabel('$i_0$')
        ax1.set_xlabel('$(g-i)_0$')
        ax1.set_ylim(25, 15)
        ax1.set_xlim(-1, 4)
        ax1.set_aspect(0.5)
        ax1.set_title('$m-M = ${:5.2f} | $d = ${:4.2f} Mpc'.format(dm, mpc),
                      size='small')
        fig.add_subplot(ax1)

        ax2 = plt.Subplot(fig, outer[0])
        extent = [yedges[0], yedges[-1], xedges[-1], xedges[0]]
        im = ax2.imshow(S,
                        extent=extent,
                        interpolation='nearest',
                        cmap=cm.gray)
        # cbar_S = plt.colorbar()
        # cbar_S.set_label('$\sigma$ from local mean')
        ax2.plot(hi_x_circ, hi_y_circ, linestyle='-', color='limegreen')
        ax2.plot(x_circ, y_circ, linestyle='-', color='magenta')
        ax2.plot(x_circr, y_circr, linestyle='-', color='gold')
        ax2.tick_params(axis='y',
                        left='on',
                        right='off',
                        labelleft='on',
                        labelright='off')
        ax2.yaxis.set_label_position('left')
        ax2.set_xticks([0, 5, 10, 15, 20])
        ax2.set_yticks([0, 5, 10, 15, 20])
        ax2.set_xlabel('RA (arcmin)')
        ax2.set_ylabel('Dec (arcmin)')
        if obj.startswith('HI1151'):
            ax2.set_title('AGC219656', size='small')
        else:
            ax2.set_title(obj, size='small')
        ax2.set_xlim(0, max(i_ra))
        ax2.set_ylim(0, max(i_dec))
        # im.set_clim(-3, 3)
        ax2.set_aspect('equal')
        fig.add_subplot(ax2)

        ax3 = plt.Subplot(fig, outer[2])
        if os.path.isfile(folder + 'i_gmi_compl.gr.out'):
            gmiCompl, iCompl = np.loadtxt(folder + 'i_gmi_compl.gr.out',
                                          usecols=(0, 1),
                                          unpack=True)
            ax3.plot(gmiCompl, iCompl, linestyle='--', color='green')
        ax3.plot(gi_iso, i_m_iso, linestyle='-', lw=0.5, color='blue')
        ax3.scatter(gmi_c,
                    i_mag_c,
                    color='black',
                    marker='o',
                    s=1,
                    edgecolors='none')
        ax3.scatter(gmi_fc,
                    i_mag_fc,
                    color='red',
                    marker='o',
                    s=10,
                    edgecolors='none')
        ax3.errorbar(bxvals,
                     bcenters,
                     xerr=i_ierrAVG,
                     yerr=gmi_errAVG,
                     linestyle='None',
                     color='black',
                     capsize=0,
                     ms=0)
        ax3.tick_params(axis='y',
                        left='on',
                        right='off',
                        labelleft='on',
                        labelright='off')
        ax3.yaxis.set_label_position('left')
        ax3.set_xticks([-1, 0, 1, 2, 3, 4])
        ax3.set_yticks([15, 17, 19, 21, 23, 25])
        ax3.set_ylabel('$i_0$')
        ax3.set_xlabel('$(g-i)_0$')
        ax3.set_ylim(25, 15)
        ax3.set_xlim(-1, 4)
        ax3.set_aspect(0.5)
        ax3.set_title('detection', size='small')
        fig.add_subplot(ax3)

        ax4 = plt.Subplot(fig, outer[3])
        if os.path.isfile(folder + 'i_gmi_compl.gr.out'):
            gmiCompl, iCompl = np.loadtxt(folder + 'i_gmi_compl.gr.out',
                                          usecols=(0, 1),
                                          unpack=True)
            ax4.plot(gmiCompl, iCompl, linestyle='--', color='green')
        ax4.plot(gi_iso, i_m_iso, linestyle='-', lw=0.5, color='blue')
        ax4.scatter(gmi_cr,
                    i_mag_cr,
                    color='black',
                    marker='o',
                    s=1,
                    edgecolors='none')
        ax4.scatter(gmi_fcr,
                    i_mag_fcr,
                    color='red',
                    marker='o',
                    s=10,
                    edgecolors='none')
        ax4.errorbar(bxvals,
                     bcenters,
                     xerr=i_ierrAVG,
                     yerr=gmi_errAVG,
                     linestyle='None',
                     color='black',
                     capsize=0,
                     ms=0)
        ax4.tick_params(axis='y',
                        left='on',
                        right='off',
                        labelleft='on',
                        labelright='off')
        ax4.yaxis.set_label_position('left')
        ax4.set_xticks([-1, 0, 1, 2, 3, 4])
        ax4.set_yticks([15, 17, 19, 21, 23, 25])
        ax4.set_ylabel('$i_0$')
        ax4.set_xlabel('$(g-i)_0$')
        ax4.set_ylim(25, 15)
        ax4.set_xlim(-1, 4)
        ax4.set_aspect(0.5)
        ax4.set_title('reference', size='small')
        fig.add_subplot(ax4)

        outer.tight_layout(fig)
        # plt.savefig('detections.pdf')
        plt.savefig('{:s}.pdf'.format(obj))

    pass
Example #5
0
def main():
    objects = OrderedDict([('AGC249525',26.78)])
    filter_file = os.path.dirname(os.path.abspath(__file__))+'/filter.txt'
    
    fwhm_sm = 3.0
    fwhm_sm_string = '3.0'
    
    # plt.clf()
    fig = plt.figure(figsize=(9,4))
    outer = gridspec.GridSpec(1,1, wspace=0.1, hspace=0.1)
    for i, obj in enumerate(objects.keys()):
        print obj
        inner = gridspec.GridSpecFromSubplotSpec(1, 1, subplot_spec=outer[i], wspace=0.1, hspace=0.1)
        # set up some filenames
        folder = '/Volumes/galileo/uchvc/targets/'+obj.lower()+'/'
        mag_file = folder+'calibrated_mags.dat'
        fits_file_i = folder+obj+'_i.fits'                  
        fits_i = fits.open(fits_file_i)
        
        dm = objects[obj]
        
        # read in magnitudes, colors, and positions(x,y)
        # gxr,gyr,g_magr,g_ierrr,ixr,iyr,i_magr,i_ierrr,gmir,fwhm_sr= np.loadtxt(mag_file,usecols=(0,1,2,3,4,5,6,7,8,11),unpack=True)
        gxr,gyr,g_magr,g_ierrr,ixr,iyr,i_magr,i_ierrr,gmir= np.loadtxt(mag_file,usecols=(0,1,2,3,4,5,6,7,8),unpack=True)
        # print len(gxr), "total stars"
        fwhm_sr = np.ones_like(gxr)
        # filter out the things with crappy color errors
        color_error_cut = np.sqrt(2.0)*0.2
        mag_error_cut = 0.2
        
        gmi_errr = [np.sqrt(g_ierrr[i]**2 + i_ierrr[i]**2) for i in range(len(gxr))]
        gx = [gxr[i] for i in range(len(gxr)) if (abs(gmi_errr[i] < color_error_cut and i_ierrr[i] < mag_error_cut))]
        gy = [gyr[i] for i in range(len(gxr)) if (abs(gmi_errr[i] < color_error_cut and i_ierrr[i] < mag_error_cut))]
        g_mag = [g_magr[i] for i in range(len(gxr)) if (abs(gmi_errr[i] < color_error_cut and i_ierrr[i] < mag_error_cut))]
        g_ierr = [g_ierrr[i] for i in range(len(gxr)) if (abs(gmi_errr[i] < color_error_cut and i_ierrr[i] < mag_error_cut))]
        ix = [ixr[i] for i in range(len(gxr)) if (abs(gmi_errr[i] < color_error_cut and i_ierrr[i] < mag_error_cut))]
        iy = [iyr[i] for i in range(len(gxr)) if (abs(gmi_errr[i] < color_error_cut and i_ierrr[i] < mag_error_cut))]
        i_mag = [i_magr[i] for i in range(len(gxr)) if (abs(gmi_errr[i] < color_error_cut and i_ierrr[i] < mag_error_cut))]
        i_ierr = np.array([i_ierrr[i] for i in range(len(gxr)) if (abs(gmi_errr[i] < color_error_cut and i_ierrr[i] < mag_error_cut))])
        gmi = [gmir[i] for i in range(len(gxr)) if (abs(gmi_errr[i] < color_error_cut and i_ierrr[i] < mag_error_cut))]
        fwhm_s = [fwhm_sr[i] for i in range(len(gxr)) if (abs(gmi_errr[i] < color_error_cut and i_ierrr[i] < mag_error_cut))]
        gmi_err = np.array([np.sqrt(g_ierrr[i]**2 + i_ierrr[i]**2) for i in range(len(gxr)) if (abs(gmi_errr[i] < color_error_cut and i_ierrr[i] < mag_error_cut))])
        
        i_ierrAVG, bedges, binid = ss.binned_statistic(i_mag,i_ierr,statistic='median',bins=10,range=[15,25])
        gmi_errAVG, bedges, binid = ss.binned_statistic(i_mag,gmi_err,statistic='median',bins=10,range=[15,25])
        
        bcenters = (bedges[:-1] + bedges[1:]) / 2
        bxvals = [3.75,3.75,3.75,3.75,3.75,3.75,3.75,3.75,3.75,3.75]
        # print bcenters
        # print i_ierrAVG
        # print gmi_errAVG
        # print len(gx), "after color+mag error cut"
        # nid = np.loadtxt(mag_file,usecols=(0,),dtype=int,unpack=True)
        pixcrd = zip(ix,iy)
        
        
        # print "Reading WCS info from image header..."
        # Parse the WCS keywords in the primary HDU
        warnings.filterwarnings('ignore', category=UserWarning, append=True)
        w = wcs.WCS(fits_i[0].header)
        # print fits_i[0].header['naxis1'], fits_i[0].header['naxis2']
        footprint = w.calc_footprint()
        se_corner = footprint[0]
        ne_corner = footprint[1]
        nw_corner = footprint[2]
        sw_corner = footprint[3]
        # print se_corner, ne_corner, nw_corner, sw_corner
        width = (ne_corner[0]-nw_corner[0])*60.
        height = (ne_corner[1]-se_corner[1])*60.
        # print width, height
        
        # Print out the "name" of the WCS, as defined in the FITS header
        # print w.wcs.name
        
        # Print out all of the settings that were parsed from the header
        # w.wcs.print_contents()
        
        # Convert pixel coordinates to world coordinates
        # The second argument is "origin" -- in this case we're declaring we
        # have 1-based (Fortran-like) coordinates.
        world = w.all_pix2world(pixcrd, 1)
        ra_corner, dec_corner = w.all_pix2world(0,0,1)
        ra_c_d,dec_c_d = deg2HMS(ra=ra_corner, dec=dec_corner, round=True)
        # print 'Corner RA:',ra_c_d,':: Corner Dec:',dec_c_d
        
        fits_i.close()
        
        # split the ra and dec out into individual arrays and transform to arcmin from the corner
        i_ra = [abs((world[i,0]-ra_corner)*60) for i in range(len(world[:,0]))]
        i_dec = [abs((world[i,1]-dec_corner)*60) for i in range(len(world[:,1]))]
        # also preserve the decimal degrees for reference
        i_rad = [world[i,0] for i in range(len(world[:,0]))]
        i_decd = [world[i,1] for i in range(len(world[:,1]))]
        
        search = open(obj+'_search'+fwhm_sm_string+'.txt','w+')
        
        sig_bins = []
        sig_cens = []
        sig_max = []
        pcts = []
        
        dms = np.arange(22.0,27.0,0.01)
        
        for dm in dms:
            mpc = pow(10,((dm + 5.)/5.))/1000000.
            dm_string = '{:5.2f}'.format(dm).replace('.','_')
        
            cm_filter, gi_iso, i_m_iso = make_filter(dm, filter_file)
            stars_f = filter_sources(i_mag, i_ierr, gmi, gmi_err, cm_filter, filter_sig = 1)
        
            xy_points = zip(i_ra,i_dec)
        
            # make new vectors containing only the filtered points
            
            i_mag_f = [i_mag[i] for i in range(len(i_mag)) if (stars_f[i])]
            g_mag_f = [g_mag[i] for i in range(len(i_mag)) if (stars_f[i])]
            gmi_f = [gmi[i] for i in range(len(i_mag)) if (stars_f[i])]
            i_ra_f = [i_ra[i] for i in range(len(i_mag)) if (stars_f[i])]
            i_dec_f = [i_dec[i] for i in range(len(i_mag)) if (stars_f[i])]
            i_rad_f = [i_rad[i] for i in range(len(i_mag)) if (stars_f[i])]
            i_decd_f = [i_decd[i] for i in range(len(i_mag)) if (stars_f[i])]
            i_x_f = [ix[i] for i in range(len(i_mag)) if (stars_f[i])]
            i_y_f = [iy[i] for i in range(len(i_mag)) if (stars_f[i])]
            fwhm_sf = [fwhm_s[i] for i in range(len(i_mag)) if (stars_f[i])]
            n_in_filter = len(i_mag_f)
            
            xedges, x_cent, yedges, y_cent, S, x_cent_S, y_cent_S, pltsig, tbl = grid_smooth(i_ra_f, i_dec_f, fwhm_sm, width, height)
            pct, d_bins, d_cens = distfit(n_in_filter,S[x_cent_S][y_cent_S],obj,width,height,fwhm_sm,dm, samples=1000)
            
            sig_bins.append(d_bins)
            sig_cens.append(d_cens)
            sig_max.append(S[x_cent_S][y_cent_S])
            pcts.append(pct)
            
            circ_c_x = ra_corner-(yedges[y_cent]/60.)
            circ_c_y = (xedges[x_cent]/60.)+dec_corner
            circ_pix_x, circ_pix_y = w.wcs_world2pix(circ_c_x,circ_c_y,1)
            ra_c, dec_c = w.all_pix2world(circ_pix_x, circ_pix_y,1)
            ra_c_d,dec_c_d = deg2HMS(ra=ra_c, dec=dec_c, round=False)
            
            hi_c_ra, hi_c_dec = getHIellipse(obj, ra_corner, dec_corner, centroid=True)
            sep = dist2HIcentroid(ra_c_d, dec_c_d, hi_c_ra, hi_c_dec)
            print 'm-M = {:5.2f} | d = {:4.2f} Mpc | α = {:s}, δ = {:s}, Δʜɪ = {:5.1f}" | N = {:4d} | σ = {:6.3f} | ξ = {:6.3f}%'.format(dm, mpc, ra_c_d, dec_c_d, sep, n_in_filter, S[x_cent_S][y_cent_S], pct)
            print >> search, '{:5.2f} {:4.2f} {:s} {:s} {:5.1f} {:4d} {:6.3f} {:6.3f}'.format(dm, mpc, ra_c_d, dec_c_d, sep, n_in_filter, S[x_cent_S][y_cent_S], pct)
        
        ax1 = plt.Subplot(fig, inner[0])
        ax1.imshow(np.transpose(sig_bins), cmap=plt.cm.Reds, extent=(22, 27, 22, 2))#, origin=origin)
        ax1.scatter(dms, sig_max, linestyle='-', c=pcts, cmap=plt.cm.Blues)
        ax1.set_ylabel('$\sigma$')
        ax1.set_xlabel('distance modulus')
        ax1.set_xlim(22,27)
        ax1.set_ylim(2,6.5)
        ax1.vlines([26.07, 26.78], 2, 6.5, linestyles='dashed', lw=0.5)
        if obj.startswith('HI1151'):
            ax1.set_title('AGC219656', size='small')
        else:
            ax1.set_title(obj, size='small')
        ax1.set_aspect(0.4)
        fig.add_subplot(ax1)
                    
    outer.tight_layout(fig)
    plt.savefig('detections_significance.pdf')
        
    pass
Example #6
0
def main():
    objects = OrderedDict([('AGC249320',25.28), ('AGC258242',25.05), ('AGC268074',22.10), ('HI0959+19',23.08)])
    smooths = OrderedDict([('AGC249320',2.0),   ('AGC258242',3.0),   ('AGC268074',2.0),   ('HI0959+19',2.0)])
    filter_file = os.path.dirname(os.path.abspath(__file__))+'/filter.txt'
    young_file = os.path.dirname(os.path.abspath(__file__))+'/filter_young.txt'

    # plt.clf()
    fig = plt.figure(figsize=(8.75,8.75))
    outer = gridspec.GridSpec(4, 1, wspace=0.05, hspace=0.01)
    for i, obj in enumerate(objects.keys()):
        inner = gridspec.GridSpecFromSubplotSpec(1, 4, subplot_spec=outer[i], wspace=0.05, hspace=0.05)
        # set up some filenames
        # if os.path.isfile('/data/uchvc/targets/'+obj.lower()+'/'):
        folder = '/data/uchvc/targets/'+obj.lower()+'/'
        # else:
            # folder = '/Volumes/discovery1/uchvc/targets/'+obj.lower()+'/'
        mag_file = folder+'calibrated_mags.dat'
        fits_file_i = folder+obj+'_i.fits'                  
        fits_i = fits.open(fits_file_i)
        
        dm = objects[obj]
        mpc = pow(10,((dm + 5.)/5.))/1000000.
        
        # read in magnitudes, colors, and positions(x,y)
        # gxr,gyr,g_magr,g_ierrr,ixr,iyr,i_magr,i_ierrr,gmir,fwhm_sr= np.loadtxt(mag_file,usecols=(0,1,2,3,4,5,6,7,8,11),unpack=True)
        gxr,gyr,g_magr,g_ierrr,ixr,iyr,i_magr,i_ierrr,gmir= np.loadtxt(mag_file,usecols=(0,1,2,3,4,5,6,7,8),unpack=True)
        # print len(gxr), "total stars"
        fwhm_sr = np.ones_like(gxr)
        # filter out the things with crappy color errors
        color_error_cut = np.sqrt(2.0)*0.2
        mag_error_cut = 0.2
        
        gmi_errr = [np.sqrt(g_ierrr[i]**2 + i_ierrr[i]**2) for i in range(len(gxr))]
        gx = [gxr[i] for i in range(len(gxr)) if (abs(gmi_errr[i] < color_error_cut and i_ierrr[i] < mag_error_cut))]
        gy = [gyr[i] for i in range(len(gxr)) if (abs(gmi_errr[i] < color_error_cut and i_ierrr[i] < mag_error_cut))]
        g_mag = [g_magr[i] for i in range(len(gxr)) if (abs(gmi_errr[i] < color_error_cut and i_ierrr[i] < mag_error_cut))]
        g_ierr = [g_ierrr[i] for i in range(len(gxr)) if (abs(gmi_errr[i] < color_error_cut and i_ierrr[i] < mag_error_cut))]
        ix = [ixr[i] for i in range(len(gxr)) if (abs(gmi_errr[i] < color_error_cut and i_ierrr[i] < mag_error_cut))]
        iy = [iyr[i] for i in range(len(gxr)) if (abs(gmi_errr[i] < color_error_cut and i_ierrr[i] < mag_error_cut))]
        i_mag = np.array([i_magr[i] for i in range(len(gxr)) if (abs(gmi_errr[i] < color_error_cut and i_ierrr[i] < mag_error_cut))])
        i_ierr = np.array([i_ierrr[i] for i in range(len(gxr)) if (abs(gmi_errr[i] < color_error_cut and i_ierrr[i] < mag_error_cut))])
        gmi = [gmir[i] for i in range(len(gxr)) if (abs(gmi_errr[i] < color_error_cut and i_ierrr[i] < mag_error_cut))]
        fwhm_s = [fwhm_sr[i] for i in range(len(gxr)) if (abs(gmi_errr[i] < color_error_cut and i_ierrr[i] < mag_error_cut))]
        gmi_err = np.array([np.sqrt(g_ierrr[i]**2 + i_ierrr[i]**2) for i in range(len(gxr)) if (abs(gmi_errr[i] < color_error_cut and i_ierrr[i] < mag_error_cut))])
        
        i_ierrAVG, bedges, binid = ss.binned_statistic(i_mag,i_ierr,statistic='median',bins=10,range=[15,25])
        gmi_errAVG, bedges, binid = ss.binned_statistic(i_mag,gmi_err,statistic='median',bins=10,range=[15,25])
        
        bcenters = (bedges[:-1] + bedges[1:]) / 2
        bxvals = [3.75,3.75,3.75,3.75,3.75,3.75,3.75,3.75,3.75,3.75]
        # print bcenters
        # print i_ierrAVG
        # print gmi_errAVG
        # print len(gx), "after color+mag error cut"
        # nid = np.loadtxt(mag_file,usecols=(0,),dtype=int,unpack=True)
        pixcrd = list(zip(ix,iy))
        
        
        # print "Reading WCS info from image header..."
        # Parse the WCS keywords in the primary HDU
        warnings.filterwarnings('ignore', category=UserWarning, append=True)
        w = wcs.WCS(fits_i[0].header)
        # print fits_i[0].header['naxis1'], fits_i[0].header['naxis2']
        footprint = w.calc_footprint()
        se_corner = footprint[0]
        ne_corner = footprint[1]
        nw_corner = footprint[2]
        sw_corner = footprint[3]
        # print se_corner, ne_corner, nw_corner, sw_corner
        width = (ne_corner[0]-nw_corner[0])*60.
        height = (ne_corner[1]-se_corner[1])*60.
        # print width, height
        
        # Print out the "name" of the WCS, as defined in the FITS header
        # print w.wcs.name
        
        # Print out all of the settings that were parsed from the header
        # w.wcs.print_contents()
        
        # Convert pixel coordinates to world coordinates
        # The second argument is "origin" -- in this case we're declaring we
        # have 1-based (Fortran-like) coordinates.
        world = w.all_pix2world(pixcrd, 1)
        ra_corner, dec_corner = w.all_pix2world(0,0,1)
        ra_c_d,dec_c_d = deg2HMS(ra=ra_corner, dec=dec_corner, round=True)
        # print 'Corner RA:',ra_c_d,':: Corner Dec:',dec_c_d
        
        fits_i.close()
        
        # split the ra and dec out into individual arrays and transform to arcmin from the corner
        i_ra = [abs((world[i,0]-ra_corner)*60) for i in range(len(world[:,0]))]
        i_dec = [abs((world[i,1]-dec_corner)*60) for i in range(len(world[:,1]))]
        # also preserve the decimal degrees for reference
        i_rad = [world[i,0] for i in range(len(world[:,0]))]
        i_decd = [world[i,1] for i in range(len(world[:,1]))]
        
        cm_filter, gi_iso, i_m_iso = make_filter(dm, filter_file)
        gi_young, i_m_young = make_youngpop(dm, young_file)
        stars_f = filter_sources(i_mag, i_ierr, gmi, gmi_err, cm_filter, filter_sig = 1)
        
        xy_points = list(zip(i_ra,i_dec))
        
        # make new vectors containing only the filtered points
        
        i_mag_f = [i_mag[i] for i in range(len(i_mag)) if (stars_f[i])]
        g_mag_f = [g_mag[i] for i in range(len(i_mag)) if (stars_f[i])]
        gmi_f = [gmi[i] for i in range(len(i_mag)) if (stars_f[i])]
        i_ra_f = [i_ra[i] for i in range(len(i_mag)) if (stars_f[i])]
        i_dec_f = [i_dec[i] for i in range(len(i_mag)) if (stars_f[i])]
        i_rad_f = [i_rad[i] for i in range(len(i_mag)) if (stars_f[i])]
        i_decd_f = [i_decd[i] for i in range(len(i_mag)) if (stars_f[i])]
        i_x_f = [ix[i] for i in range(len(i_mag)) if (stars_f[i])]
        i_y_f = [iy[i] for i in range(len(i_mag)) if (stars_f[i])]
        fwhm_sf = [fwhm_s[i] for i in range(len(i_mag)) if (stars_f[i])]
        n_in_filter = len(i_mag_f)
        
        fwhm = smooths[obj]
        xedges, x_cent, yedges, y_cent, S, x_cent_S, y_cent_S, pltsig, tbl = grid_smooth(i_ra_f, i_dec_f, fwhm, width, height)
        
        # xedges, x_cent, yedges, y_cent, S, x_cent_S, y_cent_S, pltsig, tbl = grid_smooth(i_ra, i_dec, 2.0, width, height)
        
        cosd = lambda x : np.cos(np.deg2rad(x))
        sind = lambda x : np.sin(np.deg2rad(x))
        
        # hi_c_x, hi_c_y = abs((hi_c_ra-ra_corner)*60), abs((hi_c_dec-dec_corner)*60)
        # 
        # t = np.array(range(0,359,1))
        # ell = np.array([a*cosd(t) , b*sind(t)])
        # rot = np.array([[cosd(pa) , -sind(pa)],[sind(pa) , cosd(pa)]])
        # ell_rot = np.zeros((2,ell.shape[1]))
        # for i in range(ell.shape[1]):
        #     ell_rot[:,i] = np.dot(rot,ell[:,i])
        # hi_x_circ, hi_y_circ = hi_c_x+ell_rot[0,:], hi_c_y+ell_rot[1,:]
        # hi_x_circ = [hi_c_x + a*cosd(t) for t in range(0,359,1)]
        # hi_y_circ = [hi_c_y + b*sind(t) for t in range(0,359,1)]
        
        x_circ = [yedges[y_cent] + 3.0*cosd(t) for t in range(0,359,1)]
        y_circ = [xedges[x_cent] + 3.0*sind(t) for t in range(0,359,1)]
        circ_c_x = ra_corner-(yedges[y_cent]/60.)
        circ_c_y = (xedges[x_cent]/60.)+dec_corner
        circ_pix_x, circ_pix_y = w.wcs_world2pix(circ_c_x,circ_c_y,1)
        ra_c, dec_c = w.all_pix2world(circ_pix_x, circ_pix_y,1)
        ra_c_d,dec_c_d = deg2HMS(ra=ra_c, dec=dec_c, round=False)

        hi_x_circ, hi_y_circ = getHIellipse(obj, ra_corner, dec_corner)
        hi_c_ra, hi_c_dec = getHIellipse(obj, ra_corner, dec_corner, centroid=True)
        sep, sep3d = dist2HIcentroid(ra_c_d, dec_c_d, hi_c_ra, hi_c_dec, mpc)
        print(obj, sep, sep3d)
        
        verts_circ = list(zip(x_circ,y_circ))
        circ_filter = Path(verts_circ)
        
        stars_circ = circ_filter.contains_points(xy_points)    
        
        i_mag_c = [i_mag[i] for i in range(len(i_mag)) if (stars_circ[i])]
        gmi_c = [gmi[i] for i in range(len(i_mag)) if (stars_circ[i])]
        i_ra_c = [i_ra[i] for i in range(len(i_mag)) if (stars_circ[i])]
        i_dec_c = [i_dec[i] for i in range(len(i_mag)) if (stars_circ[i])]
        i_rad_c = [i_rad[i] for i in range(len(i_mag)) if (stars_circ[i])]
        i_decd_c = [i_decd[i] for i in range(len(i_mag)) if (stars_circ[i])]
        i_x_c = [ix[i] for i in range(len(i_mag)) if (stars_circ[i])]
        i_y_c = [iy[i] for i in range(len(i_mag)) if (stars_circ[i])]
        fwhm_sc = [fwhm_s[i] for i in range(len(i_mag)) if (stars_circ[i])]
        
        # make a random reference cmd to compare to
        if not os.path.isfile(folder+'refCircle.center'):
            rCentx = 16.0*np.random.random()+2.0
            rCenty = 16.0*np.random.random()+2.0
            with open('refCircle.center','w+') as rc:
                print('{:8.4f} {:8.4f}'.format(rCentx, rCenty), file=rc)
        else :
            rCentx, rCenty = np.loadtxt(folder+'refCircle.center', usecols=(0,1), unpack=True)
                
        x_circr = [rCentx + 3.0*cosd(t) for t in range(0,359,1)]
        y_circr = [rCenty + 3.0*sind(t) for t in range(0,359,1)]
        
        verts_circr = list(zip(x_circr,y_circr))
        rcirc_filter = Path(verts_circr)
        
        stars_circr = rcirc_filter.contains_points(xy_points)    
        
        i_mag_cr = [i_mag[i] for i in range(len(i_mag)) if (stars_circr[i])]
        gmi_cr = [gmi[i] for i in range(len(i_mag)) if (stars_circr[i])]
        i_ra_cr = [i_ra[i] for i in range(len(i_mag)) if (stars_circr[i])]
        i_dec_cr = [i_dec[i] for i in range(len(i_mag)) if (stars_circr[i])]
        i_rad_cr = [i_rad[i] for i in range(len(i_mag)) if (stars_circr[i])]
        i_decd_cr = [i_decd[i] for i in range(len(i_mag)) if (stars_circr[i])]
        i_x_cr = [ix[i] for i in range(len(i_mag)) if (stars_circr[i])]
        i_y_cr = [iy[i] for i in range(len(i_mag)) if (stars_circr[i])]
        fwhm_scr = [fwhm_s[i] for i in range(len(i_mag)) if (stars_circr[i])]
        
        i_mag_fc = [i_mag[i] for i in range(len(i_mag)) if (stars_circ[i] and stars_f[i])]
        i_ierr_fc = [i_ierr[i] for i in range(len(i_mag)) if (stars_circ[i] and stars_f[i])]
        g_ierr_fc = [g_ierr[i] for i in range(len(i_mag)) if (stars_circ[i] and stars_f[i])]
        g_mag_fc = [i_mag[i] for i in range(len(i_mag)) if (stars_circ[i] and stars_f[i])]
        gmi_fc = [gmi[i] for i in range(len(i_mag)) if (stars_circ[i] and stars_f[i])]
        i_ra_fc = [i_ra[i] for i in range(len(i_mag)) if (stars_circ[i] and stars_f[i])]
        i_dec_fc = [i_dec[i] for i in range(len(i_mag)) if (stars_circ[i] and stars_f[i])]
        i_rad_fc = [i_rad[i] for i in range(len(i_mag)) if (stars_circ[i] and stars_f[i])]
        i_decd_fc = [i_decd[i] for i in range(len(i_mag)) if (stars_circ[i] and stars_f[i])]
        i_x_fc = [ix[i] for i in range(len(i_mag)) if (stars_circ[i] and stars_f[i])]
        i_y_fc = [iy[i] for i in range(len(i_mag)) if (stars_circ[i] and stars_f[i])]
        fwhm_sfc = [fwhm_s[i] for i in range(len(i_mag)) if (stars_circ[i] and stars_f[i])]
        index_fc = [i for i in range(len(i_mag)) if (stars_circ[i] and stars_f[i])]
        
        i_mag_fcr = [i_mag[i] for i in range(len(i_mag)) if (stars_circr[i] and stars_f[i])]
        i_ierr_fcr = [i_ierr[i] for i in range(len(i_mag)) if (stars_circr[i] and stars_f[i])]
        g_ierr_fcr = [g_ierr[i] for i in range(len(i_mag)) if (stars_circr[i] and stars_f[i])]
        g_mag_fcr = [i_mag[i] for i in range(len(i_mag)) if (stars_circr[i] and stars_f[i])]
        gmi_fcr = [gmi[i] for i in range(len(i_mag)) if (stars_circr[i] and stars_f[i])]
        i_ra_fcr = [i_ra[i] for i in range(len(i_mag)) if (stars_circr[i] and stars_f[i])]
        i_dec_fcr = [i_dec[i] for i in range(len(i_mag)) if (stars_circr[i] and stars_f[i])]
        i_rad_fcr = [i_rad[i] for i in range(len(i_mag)) if (stars_circr[i] and stars_f[i])]
        i_decd_fcr = [i_decd[i] for i in range(len(i_mag)) if (stars_circr[i] and stars_f[i])]
        i_x_fcr = [ix[i] for i in range(len(i_mag)) if (stars_circr[i] and stars_f[i])]
        i_y_fcr = [iy[i] for i in range(len(i_mag)) if (stars_circr[i] and stars_f[i])]
        fwhm_sfcr = [fwhm_s[i] for i in range(len(i_mag)) if (stars_circr[i] and stars_f[i])]
        
        # fig = plt.figure(figsize=(8.5,8.5))
        ax1 = plt.Subplot(fig, inner[0])
        if os.path.isfile(folder+'i_gmi_compl.gr.out'):
            gmiCompl, iCompl = np.loadtxt(folder+'i_gmi_compl.gr.out',usecols=(0,1),unpack=True)
            ax1.plot(gmiCompl,iCompl, linestyle='--', color='green')
        # if os.path.isfile(folder+'i_gmi_compl.gr2.out'):
        #     gmiCompl, iCompl = np.loadtxt(folder+'i_gmi_compl.gr2.out',usecols=(0,1),unpack=True)
        #     ax1.plot(gmiCompl,iCompl, linestyle='--', color='red')
        # if os.path.isfile(folder+'i_gmi_compl2.out'):
        #     iCompl,gmiCompl = np.loadtxt(folder+'i_gmi_compl2.out',usecols=(0,1),unpack=True)
        #     ax1.plot(gmiCompl,iCompl, linestyle='--', color='blue')
        
        ax1.plot(gi_iso,i_m_iso,linestyle='-', lw=0.5, color='blue')
        ax1.plot(gi_young,i_m_young,linestyle='--', lw=0.5, color='blue')
        ax1.scatter(gmi, i_mag,  color='black', marker='o', s=1, edgecolors='none')
        ax1.scatter(gmi_f, i_mag_f,  color='red', marker='o', s=5, edgecolors='none')
        ax1.errorbar(bxvals, bcenters, xerr=i_ierrAVG, yerr=gmi_errAVG, linestyle='None', color='black', capsize=0, ms=0)
        ax1.tick_params(labelsize='small')
        ax1.yaxis.set_label_position('left')
        ax1.set_xticks([-1,0,1,2,3,4])
        ax1.set_yticks([15, 17, 19, 21, 23, 25])
        ax1.set_ylabel('$i_0$', size='small')
        ax1.set_xlabel('$(g-i)_0$', size='small')
        ax1.set_ylim(25,15)
        ax1.set_xlim(-1,4)
        ax1.set_aspect(0.5)
        ax1.set_title('$m-M = ${:5.2f} | $d = ${:4.2f} Mpc'.format(dm, mpc), size='small')
        if obj.startswith('HI1151'):
            ax1.text(-0.4, 0.75, 'AGC219656', size='small', weight='bold', rotation=90, transform=ax1.transAxes)
        else:
            ax1.text(-0.4, 0.75, obj, size='small', weight='bold', rotation=90, transform=ax1.transAxes)
        fig.add_subplot(ax1)
        
        ax2 = plt.Subplot(fig, inner[1])
        extent = [yedges[0], yedges[-1], xedges[-1], xedges[0]]
        im = ax2.imshow(S, extent=extent, interpolation='nearest',cmap=cm.gray)
        # cbar_S = plt.colorbar()
        # cbar_S.set_label('$\sigma$ from local mean')
        ax2.plot(hi_x_circ,hi_y_circ,linestyle='-', color='limegreen')
        ax2.plot(x_circ,y_circ,linestyle='-', color='magenta')
        ax2.plot(x_circr,y_circr,linestyle='-', color='gold')
        ax2.tick_params(labelsize='small')        
        ax2.yaxis.set_label_position('left')
        ax2.set_xticks([0,5,10,15,20])
        ax2.set_yticks([0,5,10,15,20])
        ax2.set_xlabel('RA (arcmin)', size='small')
        ax2.set_ylabel('Dec (arcmin)', size='small')
        ax2.set_title("stellar density map", weight='bold', size='small')
        ax2.set_xlim(0,max(i_ra))
        ax2.set_ylim(0,max(i_dec))
        # im.set_clim(-3, 3)
        ax2.set_aspect('equal')
        fig.add_subplot(ax2)

        ax3 = plt.Subplot(fig, inner[2])
        if os.path.isfile(folder+'i_gmi_compl.gr.out'):
            gmiCompl, iCompl = np.loadtxt(folder+'i_gmi_compl.gr.out',usecols=(0,1),unpack=True)
            ax3.plot(gmiCompl,iCompl, linestyle='--', color='green')
        ax3.plot(gi_iso,i_m_iso,linestyle='-', lw=0.5, color='blue')
        ax3.plot(gi_young,i_m_young,linestyle='--', lw=0.5, color='blue')
        ax3.scatter(gmi_c, i_mag_c,  color='black', marker='o', s=1, edgecolors='none')
        ax3.scatter(gmi_fc, i_mag_fc,  color='red', marker='o', s=5, edgecolors='none')
        ax3.errorbar(bxvals, bcenters, xerr=i_ierrAVG, yerr=gmi_errAVG, linestyle='None', color='black', capsize=0, ms=0)
        ax3.tick_params(labelsize='small')
        ax3.yaxis.set_label_position('left')
        ax3.set_xticks([-1,0,1,2,3,4])
        ax3.set_yticks([15, 17, 19, 21, 23, 25])
        ax3.set_ylabel('$i_0$', size='small')
        ax3.set_xlabel('$(g-i)_0$', size='small')
        ax3.set_ylim(25,15)
        ax3.set_xlim(-1,4)
        ax3.set_aspect(0.5)
        ax3.set_title('object circle', color='magenta', weight='bold', size='small')
        fig.add_subplot(ax3)

        ax4 = plt.Subplot(fig, inner[3])
        if os.path.isfile(folder+'i_gmi_compl.gr.out'):
            gmiCompl, iCompl = np.loadtxt(folder+'i_gmi_compl.gr.out',usecols=(0,1),unpack=True)
            ax4.plot(gmiCompl,iCompl, linestyle='--', color='green')
        ax4.plot(gi_iso,i_m_iso,linestyle='-', lw=0.5, color='blue')
        ax4.plot(gi_young,i_m_young,linestyle='--', lw=0.5, color='blue')
        ax4.scatter(gmi_cr, i_mag_cr,  color='black', marker='o', s=1, edgecolors='none')
        ax4.scatter(gmi_fcr, i_mag_fcr,  color='red', marker='o', s=5, edgecolors='none')
        ax4.errorbar(bxvals, bcenters, xerr=i_ierrAVG, yerr=gmi_errAVG, linestyle='None', color='black', capsize=0, ms=0)
        ax4.tick_params(labelsize='small')
        ax4.yaxis.set_label_position('left')
        ax4.set_xticks([-1,0,1,2,3,4])
        ax4.set_yticks([15, 17, 19, 21, 23, 25])
        ax4.set_ylabel('$i_0$', size='small')
        ax4.set_xlabel('$(g-i)_0$', size='small')
        ax4.set_ylim(25,15)
        ax4.set_xlim(-1,4)
        ax4.set_aspect(0.5)
        ax4.set_title('reference circle', color='gold', weight='bold', size='small')
        fig.add_subplot(ax4)
        fig.tight_layout()
    

    outer.tight_layout(fig)
    plt.savefig('marginals.pdf')
    # plt.savefig('{:s}.pdf'.format(obj))
        
    pass
Example #7
0
def main():
    fwhm_sm = float(sys.argv[1])
    objects = ['AGC174540', 'AGC198511', 'AGC198606', 'AGC215417', 'AGC226067', 'AGC227987', 'AGC229326', 'AGC238626', 'AGC238713', 'AGC249000', 'AGC249282', 'AGC249320']
    objects2 = ['AGC249323', 'AGC249525', 'AGC258237', 'AGC258242', 'AGC258459', 'AGC268069', 'AGC268074', 'HI0932+24', 'HI0959+19', 'HI1037+21', 'HI1050+23', 'HI1151+20']

    filter_file = os.path.dirname(os.path.abspath(__file__))+'/filter.txt'
    
    # plt.clf()
    fig = plt.figure(figsize=(10,7))
    
    outer = gridspec.GridSpec(4,3, wspace=0.1, hspace=0.1)
    for i, obj in enumerate(objects2):
        print obj
        inner = gridspec.GridSpecFromSubplotSpec(1, 2, subplot_spec=outer[i], wspace=0.1, hspace=0.1)
        # set up some filenames
        folder = '/Volumes/galileo/uchvc/targets/'+obj.lower()+'/'
        mag_file = folder+'calibrated_mags.dat'
        fits_file_i = folder+obj+'_i.fits'       
        dmr, mpcr, sepr, nr, sr, pctr, pct_hir = np.loadtxt(folder+'search_{:3.1f}.txt'.format(fwhm_sm), usecols=(0,1,4,5,6,7,8), unpack=True)
        close = np.where(sepr < 480.)
        maxloc = np.argmax(pctr)
        maxclose = np.argmax(pctr[close])
        
        fits_i = fits.open(fits_file_i)
        
        # dm = dmr[maxloc]
        dm = (dmr[close])[maxclose]
        
        # read in magnitudes, colors, and positions(x,y)
        # gxr,gyr,g_magr,g_ierrr,ixr,iyr,i_magr,i_ierrr,gmir,fwhm_sr= np.loadtxt(mag_file,usecols=(0,1,2,3,4,5,6,7,8,11),unpack=True)
        gxr,gyr,g_magr,g_ierrr,ixr,iyr,i_magr,i_ierrr,gmir= np.loadtxt(mag_file,usecols=(0,1,2,3,4,5,6,7,8),unpack=True)
        # print len(gxr), "total stars"
        fwhm_sr = np.ones_like(gxr)
        # filter out the things with crappy color errors
        color_error_cut = np.sqrt(2.0)*0.2
        mag_error_cut = 0.2
        
        gmi_errr = [np.sqrt(g_ierrr[i]**2 + i_ierrr[i]**2) for i in range(len(gxr))]
        gx = [gxr[i] for i in range(len(gxr)) if (abs(gmi_errr[i] < color_error_cut and i_ierrr[i] < mag_error_cut))]
        gy = [gyr[i] for i in range(len(gxr)) if (abs(gmi_errr[i] < color_error_cut and i_ierrr[i] < mag_error_cut))]
        g_mag = [g_magr[i] for i in range(len(gxr)) if (abs(gmi_errr[i] < color_error_cut and i_ierrr[i] < mag_error_cut))]
        g_ierr = [g_ierrr[i] for i in range(len(gxr)) if (abs(gmi_errr[i] < color_error_cut and i_ierrr[i] < mag_error_cut))]
        ix = [ixr[i] for i in range(len(gxr)) if (abs(gmi_errr[i] < color_error_cut and i_ierrr[i] < mag_error_cut))]
        iy = [iyr[i] for i in range(len(gxr)) if (abs(gmi_errr[i] < color_error_cut and i_ierrr[i] < mag_error_cut))]
        i_mag = [i_magr[i] for i in range(len(gxr)) if (abs(gmi_errr[i] < color_error_cut and i_ierrr[i] < mag_error_cut))]
        i_ierr = np.array([i_ierrr[i] for i in range(len(gxr)) if (abs(gmi_errr[i] < color_error_cut and i_ierrr[i] < mag_error_cut))])
        gmi = [gmir[i] for i in range(len(gxr)) if (abs(gmi_errr[i] < color_error_cut and i_ierrr[i] < mag_error_cut))]
        fwhm_s = [fwhm_sr[i] for i in range(len(gxr)) if (abs(gmi_errr[i] < color_error_cut and i_ierrr[i] < mag_error_cut))]
        gmi_err = np.array([np.sqrt(g_ierrr[i]**2 + i_ierrr[i]**2) for i in range(len(gxr)) if (abs(gmi_errr[i] < color_error_cut and i_ierrr[i] < mag_error_cut))])
        
        i_ierrAVG, bedges, binid = ss.binned_statistic(i_mag,i_ierr,statistic='median',bins=10,range=[15,25])
        gmi_errAVG, bedges, binid = ss.binned_statistic(i_mag,gmi_err,statistic='median',bins=10,range=[15,25])
        
        bcenters = (bedges[:-1] + bedges[1:]) / 2
        bxvals = [3.75,3.75,3.75,3.75,3.75,3.75,3.75,3.75,3.75,3.75]
        # print bcenters
        # print i_ierrAVG
        # print gmi_errAVG
        # print len(gx), "after color+mag error cut"
        # nid = np.loadtxt(mag_file,usecols=(0,),dtype=int,unpack=True)
        pixcrd = zip(ix,iy)
        
        
        # print "Reading WCS info from image header..."
        # Parse the WCS keywords in the primary HDU
        warnings.filterwarnings('ignore', category=UserWarning, append=True)
        w = wcs.WCS(fits_i[0].header)
        # print fits_i[0].header['naxis1'], fits_i[0].header['naxis2']
        footprint = w.calc_footprint()
        se_corner = footprint[0]
        ne_corner = footprint[1]
        nw_corner = footprint[2]
        sw_corner = footprint[3]
        # print se_corner, ne_corner, nw_corner, sw_corner
        width = (ne_corner[0]-nw_corner[0])*60.
        height = (ne_corner[1]-se_corner[1])*60.
        # print width, height
        
        # Print out the "name" of the WCS, as defined in the FITS header
        # print w.wcs.name
        
        # Print out all of the settings that were parsed from the header
        # w.wcs.print_contents()
        
        # Convert pixel coordinates to world coordinates
        # The second argument is "origin" -- in this case we're declaring we
        # have 1-based (Fortran-like) coordinates.
        world = w.all_pix2world(pixcrd, 1)
        ra_corner, dec_corner = w.all_pix2world(0,0,1)
        ra_c_d,dec_c_d = deg2HMS(ra=ra_corner, dec=dec_corner, round=True)
        # print 'Corner RA:',ra_c_d,':: Corner Dec:',dec_c_d
        
        fits_i.close()
        
        # split the ra and dec out into individual arrays and transform to arcmin from the corner
        i_ra = [abs((world[i,0]-ra_corner)*60) for i in range(len(world[:,0]))]
        i_dec = [abs((world[i,1]-dec_corner)*60) for i in range(len(world[:,1]))]
        # also preserve the decimal degrees for reference
        i_rad = [world[i,0] for i in range(len(world[:,0]))]
        i_decd = [world[i,1] for i in range(len(world[:,1]))]
        
        cm_filter, gi_iso, i_m_iso = make_filter(dm, filter_file)
        stars_f = filter_sources(i_mag, i_ierr, gmi, gmi_err, cm_filter, filter_sig = 1)
        
        xy_points = zip(i_ra,i_dec)
        
        # make new vectors containing only the filtered points
        
        i_mag_f = [i_mag[i] for i in range(len(i_mag)) if (stars_f[i])]
        g_mag_f = [g_mag[i] for i in range(len(i_mag)) if (stars_f[i])]
        gmi_f = [gmi[i] for i in range(len(i_mag)) if (stars_f[i])]
        i_ra_f = [i_ra[i] for i in range(len(i_mag)) if (stars_f[i])]
        i_dec_f = [i_dec[i] for i in range(len(i_mag)) if (stars_f[i])]
        i_rad_f = [i_rad[i] for i in range(len(i_mag)) if (stars_f[i])]
        i_decd_f = [i_decd[i] for i in range(len(i_mag)) if (stars_f[i])]
        i_x_f = [ix[i] for i in range(len(i_mag)) if (stars_f[i])]
        i_y_f = [iy[i] for i in range(len(i_mag)) if (stars_f[i])]
        fwhm_sf = [fwhm_s[i] for i in range(len(i_mag)) if (stars_f[i])]
        n_in_filter = len(i_mag_f)
        
        xedges, x_cent, yedges, y_cent, S, x_cent_S, y_cent_S, pltsig, tbl = grid_smooth(i_ra_f, i_dec_f, fwhm_sm, width, height)
        
        # xedges, x_cent, yedges, y_cent, S, x_cent_S, y_cent_S, pltsig, tbl = grid_smooth(i_ra, i_dec, 2.0, width, height)
        
        cosd = lambda x : np.cos(np.deg2rad(x))
        sind = lambda x : np.sin(np.deg2rad(x))
        
        hi_x_circ, hi_y_circ = getHIellipse(obj, ra_corner, dec_corner)
        # hi_c_x, hi_c_y = abs((hi_c_ra-ra_corner)*60), abs((hi_c_dec-dec_corner)*60)
        # 
        # t = np.array(range(0,359,1))
        # ell = np.array([a*cosd(t) , b*sind(t)])
        # rot = np.array([[cosd(pa) , -sind(pa)],[sind(pa) , cosd(pa)]])
        # ell_rot = np.zeros((2,ell.shape[1]))
        # for i in range(ell.shape[1]):
        #     ell_rot[:,i] = np.dot(rot,ell[:,i])
        # hi_x_circ, hi_y_circ = hi_c_x+ell_rot[0,:], hi_c_y+ell_rot[1,:]
        # hi_x_circ = [hi_c_x + a*cosd(t) for t in range(0,359,1)]
        # hi_y_circ = [hi_c_y + b*sind(t) for t in range(0,359,1)]
        
        x_circ = [yedges[y_cent] + 3.0*cosd(t) for t in range(0,359,1)]
        y_circ = [xedges[x_cent] + 3.0*sind(t) for t in range(0,359,1)]
        
        ax1 = plt.Subplot(fig, inner[0])
        if os.path.isfile(folder+'i_gmi_compl.gr.out'):
            gmiCompl, iCompl = np.loadtxt(folder+'i_gmi_compl.gr.out',usecols=(0,1),unpack=True)
            ax1.plot(gmiCompl,iCompl, linestyle='--', color='green')
        # if os.path.isfile(folder+'i_gmi_compl.gr2.out'):
        #     gmiCompl, iCompl = np.loadtxt(folder+'i_gmi_compl.gr2.out',usecols=(0,1),unpack=True)
        #     ax1.plot(gmiCompl,iCompl, linestyle='--', color='red')
        # if os.path.isfile(folder+'i_gmi_compl2.out'):
        #     iCompl,gmiCompl = np.loadtxt(folder+'i_gmi_compl2.out',usecols=(0,1),unpack=True)
        #     ax1.plot(gmiCompl,iCompl, linestyle='--', color='blue')
        
        ax1.plot(gi_iso,i_m_iso,linestyle='-', color='blue')
        ax1.scatter(gmi, i_mag,  color='black', marker='o', s=1, edgecolors='none')
        ax1.scatter(gmi_f, i_mag_f,  color='red', marker='o', s=15, edgecolors='none')
        ax1.errorbar(bxvals, bcenters, xerr=i_ierrAVG, yerr=gmi_errAVG, linestyle='None', color='black', capsize=0, ms=0)
        ax1.tick_params(axis='y',left='on',right='off',labelleft='on',labelright='off')
        ax1.yaxis.set_label_position('left')
        ax1.set_xticks([-1,0,1,2,3,4])
        ax1.set_yticks([15, 17, 19, 21, 23, 25])
        ax1.set_ylabel('$i_0$')
        ax1.set_xlabel('$(g-i)_0$')
        ax1.set_ylim(25,15)
        ax1.set_xlim(-1,4)
        ax1.set_aspect(0.5)
        ax1.set_title('$m-M = ${:5.2f}'.format(dm), size='small')
        fig.add_subplot(ax1)
        
        ax2 = plt.Subplot(fig, inner[1])
        extent = [yedges[0], yedges[-1], xedges[-1], xedges[0]]
        ax2.imshow(S, extent=extent, interpolation='nearest',cmap=cm.gray)
        # cbar_S = plt.colorbar()
        # cbar_S.set_label('$\sigma$ from local mean')
        ax2.plot(hi_x_circ,hi_y_circ,linestyle='-', color='limegreen')
        ax2.plot(x_circ,y_circ,linestyle='-', color='magenta')
        ax2.tick_params(axis='y',left='off',right='on',labelleft='off',labelright='on')
        ax2.yaxis.set_label_position('right')
        ax2.set_xticks([0,5,10,15,20])
        ax2.set_yticks([0,5,10,15,20])
        ax2.set_xlabel('RA (arcmin)')
        ax2.set_ylabel('Dec (arcmin)')
        # if obj.startswith('HI1151'):
        #     ax2.set_title('AGC219656', size='small')
        # else:
        ax2.set_title(obj, size='small')
        ax2.set_xlim(0,max(i_ra))
        ax2.set_ylim(0,max(i_dec))
        ax2.set_aspect('equal')
        fig.add_subplot(ax2)
        
    outer.tight_layout(fig)
    plt.savefig('detections_close_{:3.1f}.pdf'.format(fwhm_sm))
        
    pass
Example #8
0
def main():
    objects = OrderedDict([('new', 22.89), ('old', 22.89)])
    smooths = OrderedDict([('new', 2.0), ('old', 2.0)])
    filter_file = os.path.dirname(os.path.abspath(__file__)) + '/filter.txt'

    # plt.clf()
    fig = plt.figure(figsize=(4, 4))
    outer = gridspec.GridSpec(2, 1, wspace=0.1, hspace=0.1)
    for i, obj in enumerate(objects.keys()):
        inner = gridspec.GridSpecFromSubplotSpec(1,
                                                 2,
                                                 subplot_spec=outer[i],
                                                 wspace=0.1,
                                                 hspace=0.1)
        # set up some filenames
        if i == 0:
            folder = '/Volumes/galileo/uchvc/targets/agc198606/'
        if i == 1:
            folder = '/Volumes/galileo/uchvc/targets/agc198606_correct_old/'
        mag_file = folder + 'calibrated_mags.dat'
        fits_file_i = folder + 'AGC198606_i.fits'
        fits_i = fits.open(fits_file_i)

        dm = objects[obj]
        mpc = pow(10, ((dm + 5.) / 5.)) / 1000000.

        # read in magnitudes, colors, and positions(x,y)
        # gxr,gyr,g_magr,g_ierrr,ixr,iyr,i_magr,i_ierrr,gmir,fwhm_sr= np.loadtxt(mag_file,usecols=(0,1,2,3,4,5,6,7,8,11),unpack=True)
        gxr, gyr, g_magr, g_ierrr, ixr, iyr, i_magr, i_ierrr, gmir = np.loadtxt(
            mag_file, usecols=(0, 1, 2, 3, 4, 5, 6, 7, 8), unpack=True)
        print len(gxr), "total stars"
        fwhm_sr = np.ones_like(gxr)
        # filter out the things with crappy color errors
        color_error_cut = np.sqrt(2.0) * 0.2
        mag_error_cut = 0.2

        gmi_errr = [
            np.sqrt(g_ierrr[i]**2 + i_ierrr[i]**2) for i in range(len(gxr))
        ]
        gx = [
            gxr[i] for i in range(len(gxr)) if (abs(
                gmi_errr[i] < color_error_cut and i_ierrr[i] < mag_error_cut))
        ]
        gy = [
            gyr[i] for i in range(len(gxr)) if (abs(
                gmi_errr[i] < color_error_cut and i_ierrr[i] < mag_error_cut))
        ]
        g_mag = [
            g_magr[i] for i in range(len(gxr)) if (abs(
                gmi_errr[i] < color_error_cut and i_ierrr[i] < mag_error_cut))
        ]
        g_ierr = [
            g_ierrr[i] for i in range(len(gxr)) if (abs(
                gmi_errr[i] < color_error_cut and i_ierrr[i] < mag_error_cut))
        ]
        ix = [
            ixr[i] for i in range(len(gxr)) if (abs(
                gmi_errr[i] < color_error_cut and i_ierrr[i] < mag_error_cut))
        ]
        iy = [
            iyr[i] for i in range(len(gxr)) if (abs(
                gmi_errr[i] < color_error_cut and i_ierrr[i] < mag_error_cut))
        ]
        i_mag = [
            i_magr[i] for i in range(len(gxr)) if (abs(
                gmi_errr[i] < color_error_cut and i_ierrr[i] < mag_error_cut))
        ]
        i_ierr = np.array([
            i_ierrr[i] for i in range(len(gxr)) if (abs(
                gmi_errr[i] < color_error_cut and i_ierrr[i] < mag_error_cut))
        ])
        gmi = [
            gmir[i] for i in range(len(gxr)) if (abs(
                gmi_errr[i] < color_error_cut and i_ierrr[i] < mag_error_cut))
        ]
        fwhm_s = [
            fwhm_sr[i] for i in range(len(gxr)) if (abs(
                gmi_errr[i] < color_error_cut and i_ierrr[i] < mag_error_cut))
        ]
        kept_stars = [
            i for i in range(len(gxr)) if (abs(gmi_errr[i] < color_error_cut
                                               and i_ierrr[i] < mag_error_cut))
        ]
        gmi_err = np.array([
            np.sqrt(g_ierrr[i]**2 + i_ierrr[i]**2) for i in range(len(gxr)) if
            (abs(gmi_errr[i] < color_error_cut and i_ierrr[i] < mag_error_cut))
        ])

        i_ierrAVG, bedges, binid = ss.binned_statistic(i_mag,
                                                       i_ierr,
                                                       statistic='median',
                                                       bins=10,
                                                       range=[15, 25])
        gmi_errAVG, bedges, binid = ss.binned_statistic(i_mag,
                                                        gmi_err,
                                                        statistic='median',
                                                        bins=10,
                                                        range=[15, 25])

        bcenters = (bedges[:-1] + bedges[1:]) / 2
        bxvals = [3.75, 3.75, 3.75, 3.75, 3.75, 3.75, 3.75, 3.75, 3.75, 3.75]
        # print bcenters
        # print i_ierrAVG
        # print gmi_errAVG
        # print len(gx), "after color+mag error cut"
        # nid = np.loadtxt(mag_file,usecols=(0,),dtype=int,unpack=True)
        pixcrd = zip(ix, iy)

        # print "Reading WCS info from image header..."
        # Parse the WCS keywords in the primary HDU
        warnings.filterwarnings('ignore', category=UserWarning, append=True)
        w = wcs.WCS(fits_i[0].header)
        # print fits_i[0].header['naxis1'], fits_i[0].header['naxis2']
        footprint = w.calc_footprint()
        se_corner = footprint[0]
        ne_corner = footprint[1]
        nw_corner = footprint[2]
        sw_corner = footprint[3]
        # print se_corner, ne_corner, nw_corner, sw_corner
        width = (ne_corner[0] - nw_corner[0]) * 60.
        height = (ne_corner[1] - se_corner[1]) * 60.
        # print width, height

        # Print out the "name" of the WCS, as defined in the FITS header
        # print w.wcs.name

        # Print out all of the settings that were parsed from the header
        # w.wcs.print_contents()

        # Convert pixel coordinates to world coordinates
        # The second argument is "origin" -- in this case we're declaring we
        # have 1-based (Fortran-like) coordinates.
        world = w.all_pix2world(pixcrd, 1)
        ra_corner, dec_corner = w.all_pix2world(0, 0, 1)
        ra_c_d, dec_c_d = deg2HMS(ra=ra_corner, dec=dec_corner, round=True)
        # print 'Corner RA:',ra_c_d,':: Corner Dec:',dec_c_d

        fits_i.close()

        # split the ra and dec out into individual arrays and transform to arcmin from the corner
        i_ra = [
            abs((world[i, 0] - ra_corner) * 60)
            for i in range(len(world[:, 0]))
        ]
        i_dec = [
            abs((world[i, 1] - dec_corner) * 60)
            for i in range(len(world[:, 1]))
        ]
        # also preserve the decimal degrees for reference
        i_rad = [world[i, 0] for i in range(len(world[:, 0]))]
        i_decd = [world[i, 1] for i in range(len(world[:, 1]))]

        cm_filter, gi_iso, i_m_iso = make_filter(dm, filter_file)
        stars_f = filter_sources(i_mag,
                                 i_ierr,
                                 gmi,
                                 gmi_err,
                                 cm_filter,
                                 filter_sig=1)

        xy_points = zip(i_ra, i_dec)

        # make new vectors containing only the filtered points

        i_mag_f = [i_mag[i] for i in range(len(i_mag)) if (stars_f[i])]
        g_mag_f = [g_mag[i] for i in range(len(i_mag)) if (stars_f[i])]
        gmi_f = [gmi[i] for i in range(len(i_mag)) if (stars_f[i])]
        i_ra_f = [i_ra[i] for i in range(len(i_mag)) if (stars_f[i])]
        i_dec_f = [i_dec[i] for i in range(len(i_mag)) if (stars_f[i])]
        i_rad_f = [i_rad[i] for i in range(len(i_mag)) if (stars_f[i])]
        i_decd_f = [i_decd[i] for i in range(len(i_mag)) if (stars_f[i])]
        i_x_f = [ix[i] for i in range(len(i_mag)) if (stars_f[i])]
        i_y_f = [iy[i] for i in range(len(i_mag)) if (stars_f[i])]
        fwhm_sf = [fwhm_s[i] for i in range(len(i_mag)) if (stars_f[i])]
        filtered = [i for i in range(len(i_mag)) if (stars_f[i])]
        # print filtered
        n_in_filter = len(i_mag_f)

        if 'new' in obj:
            kept1 = kept_stars
            filtered1 = np.array(filtered)
            i_ra1 = np.array(i_ra)
            i_dec1 = np.array(i_dec)
            xmin1, ymin1 = np.absolute(
                w.all_world2pix((8.2 / 60) + ra_corner,
                                (8.1 / 60) + dec_corner, 1))
            xmax1, ymax1 = np.absolute(
                w.all_world2pix((14.2 / 60) + ra_corner,
                                (14.1 / 60) + dec_corner, 1))
            # print xmin1, xmax1, ymin1, ymax1
        if 'old' in obj:
            kept2 = kept_stars
            filtered2 = np.array(filtered)
            i_ra2 = np.array(i_ra)
            i_dec2 = np.array(i_dec)
            xmin2, ymin2 = np.absolute(
                w.all_world2pix((8.2 / 60) + ra_corner,
                                (8.1 / 60) + dec_corner, 1))
            xmax2, ymax2 = np.absolute(
                w.all_world2pix((14.2 / 60) + ra_corner,
                                (14.1 / 60) + dec_corner, 1))
            # print xmin2, xmax2, ymin2, ymax2

        fwhm = smooths[obj]
        xedges, x_cent, yedges, y_cent, S, x_cent_S, y_cent_S, pltsig, tbl = grid_smooth(
            i_ra_f, i_dec_f, fwhm, width, height)
        print yedges[y_cent], xedges[x_cent]

        # xedges, x_cent, yedges, y_cent, S, x_cent_S, y_cent_S, pltsig, tbl = grid_smooth(i_ra, i_dec, 2.0, width, height)

        cosd = lambda x: np.cos(np.deg2rad(x))
        sind = lambda x: np.sin(np.deg2rad(x))

        hi_x_circ, hi_y_circ = getHIellipse('AGC198606', ra_corner, dec_corner)
        # hi_c_x, hi_c_y = abs((hi_c_ra-ra_corner)*60), abs((hi_c_dec-dec_corner)*60)
        #
        # t = np.array(range(0,359,1))
        # ell = np.array([a*cosd(t) , b*sind(t)])
        # rot = np.array([[cosd(pa) , -sind(pa)],[sind(pa) , cosd(pa)]])
        # ell_rot = np.zeros((2,ell.shape[1]))
        # for i in range(ell.shape[1]):
        #     ell_rot[:,i] = np.dot(rot,ell[:,i])
        # hi_x_circ, hi_y_circ = hi_c_x+ell_rot[0,:], hi_c_y+ell_rot[1,:]
        # hi_x_circ = [hi_c_x + a*cosd(t) for t in range(0,359,1)]
        # hi_y_circ = [hi_c_y + b*sind(t) for t in range(0,359,1)]

        x_circ = [yedges[y_cent] + 3.0 * cosd(t) for t in range(0, 359, 1)]
        y_circ = [xedges[x_cent] + 3.0 * sind(t) for t in range(0, 359, 1)]

        ax1 = plt.Subplot(fig, inner[0])
        if os.path.isfile(folder + 'i_gmi_compl.gr.out'):
            gmiCompl, iCompl = np.loadtxt(folder + 'i_gmi_compl.gr.out',
                                          usecols=(0, 1),
                                          unpack=True)
            ax1.plot(gmiCompl, iCompl, linestyle='--', color='green')
        # if os.path.isfile(folder+'i_gmi_compl.gr2.out'):
        #     gmiCompl, iCompl = np.loadtxt(folder+'i_gmi_compl.gr2.out',usecols=(0,1),unpack=True)
        #     ax1.plot(gmiCompl,iCompl, linestyle='--', color='red')
        # if os.path.isfile(folder+'i_gmi_compl2.out'):
        #     iCompl,gmiCompl = np.loadtxt(folder+'i_gmi_compl2.out',usecols=(0,1),unpack=True)
        #     ax1.plot(gmiCompl,iCompl, linestyle='--', color='blue')

        ax1.plot(gi_iso, i_m_iso, linestyle='-', color='blue')
        ax1.scatter(gmi,
                    i_mag,
                    color='black',
                    marker='o',
                    s=1,
                    edgecolors='none')
        ax1.scatter(gmi_f,
                    i_mag_f,
                    color='red',
                    marker='o',
                    s=15,
                    edgecolors='none')
        ax1.errorbar(bxvals,
                     bcenters,
                     xerr=i_ierrAVG,
                     yerr=gmi_errAVG,
                     linestyle='None',
                     color='black',
                     capsize=0,
                     ms=0)
        ax1.tick_params(axis='y',
                        left='on',
                        right='off',
                        labelleft='on',
                        labelright='off')
        ax1.yaxis.set_label_position('left')
        ax1.set_xticks([-1, 0, 1, 2, 3, 4])
        ax1.set_yticks([15, 17, 19, 21, 23, 25])
        ax1.set_ylabel('$i_0$')
        ax1.set_xlabel('$(g-i)_0$')
        ax1.set_ylim(25, 15)
        ax1.set_xlim(-1, 4)
        ax1.set_aspect(0.5)
        ax1.set_title('$m-M = ${:5.2f} | $d = ${:4.2f} Mpc'.format(dm, mpc),
                      size='small')
        fig.add_subplot(ax1)

        ax2 = plt.Subplot(fig, inner[1])
        extent = [yedges[0], yedges[-1], xedges[-1], xedges[0]]
        im = ax2.imshow(S, extent=extent, interpolation='none', cmap=cm.gray)
        ax2.scatter(i_ra,
                    i_dec,
                    color='black',
                    marker='o',
                    s=1,
                    edgecolors='none')
        ax2.scatter(i_ra_f,
                    i_dec_f,
                    c='red',
                    marker='o',
                    s=10,
                    edgecolors='none')
        # cbar_S = plt.colorbar()
        # cbar_S.set_label('$\sigma$ from local mean')
        ax2.plot(hi_x_circ, hi_y_circ, linestyle='-', color='limegreen')
        ax2.plot(x_circ, y_circ, linestyle='-', color='magenta')
        ax2.tick_params(axis='y',
                        left='off',
                        right='on',
                        labelleft='off',
                        labelright='on')
        ax2.yaxis.set_label_position('right')
        ax2.set_xticks([0, 5, 10, 15, 20])
        ax2.set_yticks([0, 5, 10, 15, 20])
        ax2.set_xlabel('RA (arcmin)')
        ax2.set_ylabel('Dec (arcmin)')
        if obj.startswith('HI1151'):
            ax2.set_title('AGC219656', size='small')
        else:
            ax2.set_title(obj, size='small')
        ax2.set_xlim(8.2, 14.2)
        ax2.set_ylim(8.1, 14.1)
        # im.set_clim(-3, 3)
        ax2.set_aspect('equal')
        fig.add_subplot(ax2)

    outer.tight_layout(fig)
    plt.savefig('friend_comp.pdf')

    x1, y1, mag2x1, fwhm1, mag1x1 = np.loadtxt(
        '/Volumes/galileo/uchvc/targets/agc198606/escut_i.pos',
        usecols=(0, 1, 2, 3, 4),
        unpack=True)
    x1, y1, mag2x1, fwhm1 = x1[kept1], y1[kept1], mag2x1[kept1], fwhm1[kept1]
    box1 = [
        j for j, f in enumerate(x1)
        if (x1[j] > xmin1 and x1[j] < xmax1 and y1[j] > ymin1 and y1[j] < ymax1
            )
    ]
    keep1 = [
        j for j, f in enumerate(x1)
        if (j in filtered1 and x1[j] > xmin1 and x1[j] < xmax1
            and y1[j] > ymin1 and y1[j] < ymax1)
    ]
    # mag2x1 = mag2x1[keep1]
    # fwhm1 = fwhm1[keep1]

    x2, y2, mag2x2, fwhm2, fi = np.loadtxt(
        '/Volumes/galileo/uchvc/targets/agc198606_correct_old/point_source_info',
        usecols=(0, 1, 4, 8, 10),
        dtype=str,
        unpack=True)
    istars = [j for j, f in enumerate(fi) if (f.endswith('i'))]
    x2, y2, mag2x2, fwhm2 = x2[istars].astype(float), y2[istars].astype(
        float), mag2x2[istars].astype(float), fwhm2[istars].astype(float)
    x2, y2, mag2x2, fwhm2 = x2[kept2], y2[kept2], mag2x2[kept2], fwhm2[kept2]
    box2 = [
        j for j, f in enumerate(x2)
        if (x2[j] > xmin2 and x2[j] < xmax2 and y2[j] > ymin2 and y2[j] < ymax2
            )
    ]
    keep2 = [
        j for j, f in enumerate(x2)
        if (j in filtered2 and x2[j] > xmin2 and x2[j] < xmax2
            and y2[j] > ymin2 and y2[j] < ymax2)
    ]
    # mag2x2 = mag2x2[keep2]
    # fwhm2 = fwhm2[keep2]

    fig2 = plt.figure(figsize=(8, 4))
    ax3 = plt.subplot(121)
    ax3.scatter(i_ra1,
                i_dec1,
                c='none',
                marker='o',
                s=20,
                lw=0.5,
                edgecolors='black')
    ax3.scatter(i_ra2,
                i_dec2,
                c='black',
                marker='x',
                s=10,
                lw=0.5,
                edgecolors='none')
    ax3.scatter(i_ra1[filtered1],
                i_dec1[filtered1],
                c='none',
                marker='o',
                s=20,
                lw=1.0,
                edgecolors='red',
                label='this work')
    ax3.scatter(i_ra2[filtered2],
                i_dec2[filtered2],
                c='blue',
                marker='x',
                s=10,
                lw=1.0,
                edgecolors='none',
                label='J15')
    ax3.set_xlim(8.2, 14.2)
    ax3.set_ylim(8.1, 14.1)
    ax3.set_ylabel('Dec (arcmin)')
    ax3.set_xlabel('RA (arcmin)')
    ax1.set_title('$m-M = ${:5.2f} | $d = ${:4.2f} Mpc'.format(22.89, 0.38),
                  size='small')
    ax3.set_aspect('equal')
    plt.legend(loc='upper left')
    # plt.savefig('star_overlap.pdf')

    # fig3 = plt.figure(figsize=(6,3))
    ax4 = plt.subplot(122)
    ax4.scatter(mag2x1[box1],
                fwhm1[box1],
                c='none',
                marker='o',
                s=20,
                lw=0.5,
                edgecolors='black')
    ax4.scatter(mag2x2[box2],
                fwhm2[box2],
                c='black',
                marker='x',
                s=10,
                lw=0.5,
                edgecolors='none')
    ax4.scatter(mag2x1[keep1],
                fwhm1[keep1],
                c='none',
                marker='o',
                s=20,
                lw=1.0,
                edgecolors='red')
    ax4.scatter(mag2x2[keep2],
                fwhm2[keep2],
                c='blue',
                marker='x',
                s=10,
                lw=1.0,
                edgecolors='none')
    ax4.set_xlim(-11, 1)
    ax4.set_ylim(0, 14)
    ax4.set_ylabel('FWHM (pixels)')
    ax4.set_xlabel('inst. mag')
    plt.tight_layout()
    plt.savefig('escut_comp.pdf')

    pass
Example #9
0
def main():
    objects = OrderedDict([('AGC198511', 22.91), ('AGC198606', 24.72),
                           ('AGC215417', 22.69), ('HI1151+20', 24.76),
                           ('AGC249525', 26.78), ('AGC268069', 24.24)])
    smooths = OrderedDict([('AGC198511', 3.0), ('AGC198606', 2.0),
                           ('AGC215417', 3.0), ('HI1151+20', 2.0),
                           ('AGC249525', 3.0), ('AGC268069', 3.0)])
    filter_file = os.path.dirname(os.path.abspath(__file__)) + '/filter.txt'

    for file_ in os.listdir("./"):
        if file_.endswith("i.fits"):
            fits_file_i = file_

    for file_ in os.listdir("./"):
        if file_.endswith("g.fits"):
            fits_file_g = file_

    # downloadSDSSgal(fits_file_g, fits_file_i)

    fits_i = fits.open(fits_file_i)
    # fits_g = fits.open(fits_file_g)
    # print "Opened fits files:",fits_file_g,"&",fits_file_i

    # objid = fits_i[0].header['OBJECT']
    title_string = fits_file_i.split('_')[
        0]  # get the name part of the filename.

    # set up some filenames
    mag_file = 'calibrated_mags.dat'

    # read in magnitudes, colors, and positions(x,y)
    # gxr,gyr,g_magr,g_ierrr,ixr,iyr,i_magr,i_ierrr,gmir,fwhm_sr= np.loadtxt(mag_file,usecols=(0,1,2,3,4,5,6,7,8,11),unpack=True)
    gxr, gyr, g_magr, g_ierrr, ixr, iyr, i_magr, i_ierrr, gmir = np.loadtxt(
        mag_file, usecols=(0, 1, 2, 3, 4, 5, 6, 7, 8), unpack=True)
    # print len(gxr), "total stars"
    fwhm_sr = np.ones_like(gxr)
    # filter out the things with crappy color errors
    color_error_cut = np.sqrt(2.0) * 0.2
    mag_error_cut = 0.2

    gmi_errr = [
        np.sqrt(g_ierrr[i]**2 + i_ierrr[i]**2) for i in range(len(gxr))
    ]
    gx = [
        gxr[i] for i in range(len(gxr))
        if (abs(gmi_errr[i] < color_error_cut and i_ierrr[i] < mag_error_cut))
    ]
    gy = [
        gyr[i] for i in range(len(gxr))
        if (abs(gmi_errr[i] < color_error_cut and i_ierrr[i] < mag_error_cut))
    ]
    g_mag = [
        g_magr[i] for i in range(len(gxr))
        if (abs(gmi_errr[i] < color_error_cut and i_ierrr[i] < mag_error_cut))
    ]
    g_ierr = [
        g_ierrr[i] for i in range(len(gxr))
        if (abs(gmi_errr[i] < color_error_cut and i_ierrr[i] < mag_error_cut))
    ]
    ix = [
        ixr[i] for i in range(len(gxr))
        if (abs(gmi_errr[i] < color_error_cut and i_ierrr[i] < mag_error_cut))
    ]
    iy = [
        iyr[i] for i in range(len(gxr))
        if (abs(gmi_errr[i] < color_error_cut and i_ierrr[i] < mag_error_cut))
    ]
    i_mag = [
        i_magr[i] for i in range(len(gxr))
        if (abs(gmi_errr[i] < color_error_cut and i_ierrr[i] < mag_error_cut))
    ]
    i_ierr = np.array([
        i_ierrr[i] for i in range(len(gxr))
        if (abs(gmi_errr[i] < color_error_cut and i_ierrr[i] < mag_error_cut))
    ])
    gmi = [
        gmir[i] for i in range(len(gxr))
        if (abs(gmi_errr[i] < color_error_cut and i_ierrr[i] < mag_error_cut))
    ]
    fwhm_s = [
        fwhm_sr[i] for i in range(len(gxr))
        if (abs(gmi_errr[i] < color_error_cut and i_ierrr[i] < mag_error_cut))
    ]
    gmi_err = np.array([
        np.sqrt(g_ierrr[i]**2 + i_ierrr[i]**2) for i in range(len(gxr))
        if (abs(gmi_errr[i] < color_error_cut and i_ierrr[i] < mag_error_cut))
    ])
    cutleft = [
        i for i in range(len(gxr))
        if (abs(gmi_errr[i] < color_error_cut and i_ierrr[i] < mag_error_cut))
    ]
    with open('cutleft.txt', 'w+') as spud:
        for i, item in enumerate(cutleft):
            print(item, file=spud)

    i_ierrAVG, bedges, binid = ss.binned_statistic(i_mag,
                                                   i_ierr,
                                                   statistic='median',
                                                   bins=10,
                                                   range=[15, 25])
    gmi_errAVG, bedges, binid = ss.binned_statistic(i_mag,
                                                    gmi_err,
                                                    statistic='median',
                                                    bins=10,
                                                    range=[15, 25])

    bcenters = (bedges[:-1] + bedges[1:]) / 2
    bxvals = [3.75, 3.75, 3.75, 3.75, 3.75, 3.75, 3.75, 3.75, 3.75, 3.75]
    # print bcenters
    # print i_ierrAVG
    # print gmi_errAVG
    # print len(gx), "after color+mag error cut"
    # nid = np.loadtxt(mag_file,usecols=(0,),dtype=int,unpack=True)
    pixcrd = list(zip(ix, iy))

    # print "Reading WCS info from image header..."
    # Parse the WCS keywords in the primary HDU
    warnings.filterwarnings('ignore', category=UserWarning, append=True)
    w = wcs.WCS(fits_i[0].header)
    # print fits_i[0].header['naxis1'], fits_i[0].header['naxis2']
    footprint = w.calc_footprint()
    se_corner = footprint[0]
    ne_corner = footprint[1]
    nw_corner = footprint[2]
    sw_corner = footprint[3]
    # print se_corner, ne_corner, nw_corner, sw_corner
    width = (ne_corner[0] - nw_corner[0]) * 60.
    height = (ne_corner[1] - se_corner[1]) * 60.
    # print width, height

    # Print out the "name" of the WCS, as defined in the FITS header
    # print w.wcs.name

    # Print out all of the settings that were parsed from the header
    # w.wcs.print_contents()

    # Convert pixel coordinates to world coordinates
    # The second argument is "origin" -- in this case we're declaring we
    # have 1-based (Fortran-like) coordinates.
    world = w.all_pix2world(pixcrd, 1)
    ra_corner, dec_corner = w.all_pix2world(0, 0, 1)
    ra_c_d, dec_c_d = deg2HMS(ra=ra_corner, dec=dec_corner, round=True)
    # print 'Corner RA:',ra_c_d,':: Corner Dec:',dec_c_d

    fwhm_i = 12.0  #fits_i[0].header['FWHMPSF']
    fwhm_g = 9.0  # fits_g[0].header['FWHMPSF']

    # print 'Image FWHM :: g = {0:5.3f} : i = {1:5.3f}'.format(fwhm_g,fwhm_i)

    fits_i.close()
    # fits_g.close()

    # split the ra and dec out into individual arrays and transform to arcmin from the corner
    i_ra = [
        abs((world[i, 0] - ra_corner) * 60) for i in range(len(world[:, 0]))
    ]
    i_dec = [
        abs((world[i, 1] - dec_corner) * 60) for i in range(len(world[:, 1]))
    ]
    # also preserve the decimal degrees for reference
    i_rad = [world[i, 0] for i in range(len(world[:, 0]))]
    i_decd = [world[i, 1] for i in range(len(world[:, 1]))]

    i_magBright = [i_mag[i] for i in range(len(i_mag)) if (i_mag[i] < 22.75)]
    g_magBright = [g_mag[i] for i in range(len(i_mag)) if (i_mag[i] < 22.75)]
    ixBright = [ix[i] for i in range(len(i_mag)) if (i_mag[i] < 22.75)]
    iyBright = [iy[i] for i in range(len(i_mag)) if (i_mag[i] < 22.75)]
    i_radBright = [i_rad[i] for i in range(len(i_mag)) if (i_mag[i] < 22.75)]
    i_decdBright = [i_decd[i] for i in range(len(i_mag)) if (i_mag[i] < 22.75)]

    if not os.path.isfile('brightStars2275.reg'):
        f1 = open('brightStars2275.reg', 'w+')
        for i in range(len(i_magBright)):
            print(
                '{0:12.4f} {1:12.4f} {2:10.5f} {3:9.5f} {4:8.2f} {5:8.2f} {6:8.2f}'
                .format(ixBright[i], iyBright[i], i_radBright[i],
                        i_decdBright[i], g_magBright[i], i_magBright[i],
                        g_magBright[i] - i_magBright[i]),
                file=f1)
        f1.close()

    i_magRed = [i_mag[i] for i in range(len(i_mag)) if (gmi[i] > 1.75)]
    g_magRed = [g_mag[i] for i in range(len(i_mag)) if (gmi[i] > 1.75)]
    ixRed = [ix[i] for i in range(len(i_mag)) if (gmi[i] > 1.75)]
    iyRed = [iy[i] for i in range(len(i_mag)) if (gmi[i] > 1.75)]
    i_radRed = [i_rad[i] for i in range(len(i_mag)) if (gmi[i] > 1.75)]
    i_decdRed = [i_decd[i] for i in range(len(i_mag)) if (gmi[i] > 1.75)]

    if not os.path.isfile('redStars175.reg'):
        f1 = open('redStars175.reg', 'w+')
        for i in range(len(i_magRed)):
            print(
                '{0:12.4f} {1:12.4f} {2:10.5f} {3:9.5f} {4:8.2f} {5:8.2f} {6:8.2f}'
                .format(ixRed[i], iyRed[i], i_radRed[i], i_decdRed[i],
                        g_magRed[i], i_magRed[i], g_magRed[i] - i_magRed[i]),
                file=f1)
        f1.close()

    dm = 22.0
    dm2 = 27.0
    fwhm = 2.0

    # if dm2 > 0.0 and filter_string != 'none':
    dms = np.arange(dm, dm2, 0.01)
    # search = open('search_{:3.1f}.txt'.format(fwhm),'w+')
    # else:
    #     dms = [dm]
    # search = open('spud.txt'.format(fwhm),'w+')

    # sig_bins = []
    # sig_cens = []
    # sig_max = []

    for dm in dms:
        mpc = pow(10, ((dm + 5.) / 5.)) / 1000000.
        dm_string = '{:5.2f}'.format(dm).replace('.', '_')

        out_file = 'anim/anim_' + dm_string + '_' + title_string + '.png'
        # mark_file = 'f_list_' + filter_string + '_' + fwhm_string + '_' + dm_string + '_' + title_string + '.reg'
        # filter_reg = 'f_reg_' + filter_string + '_' + fwhm_string + '_' + dm_string + '_' + title_string + '.reg'
        # circ_file = 'c_list_' + filter_string + '_' + fwhm_string + '_' + dm_string + '_' + title_string + '.reg'
        # fcirc_file = 'fc_list_' + filter_string + '_' + fwhm_string + '_' + dm_string + '_' + title_string + '.reg'
        # ds9_file = 'circles_' + filter_string + '_' + fwhm_string + '_' + dm_string + '_' + title_string + '.reg'
        circles_file = 'region_coords.dat'

        cm_filter, gi_iso, i_m_iso = make_filter(dm, filter_file)
        stars_f = filter_sources(i_mag,
                                 i_ierr,
                                 gmi,
                                 gmi_err,
                                 cm_filter,
                                 filter_sig=1)

        xy_points = list(zip(i_ra, i_dec))

        # make new vectors containing only the filtered points

        i_mag_f = [i_mag[i] for i in range(len(i_mag)) if (stars_f[i])]
        g_mag_f = [g_mag[i] for i in range(len(i_mag)) if (stars_f[i])]
        gmi_f = [gmi[i] for i in range(len(i_mag)) if (stars_f[i])]
        i_ra_f = [i_ra[i] for i in range(len(i_mag)) if (stars_f[i])]
        i_dec_f = [i_dec[i] for i in range(len(i_mag)) if (stars_f[i])]
        i_rad_f = [i_rad[i] for i in range(len(i_mag)) if (stars_f[i])]
        i_decd_f = [i_decd[i] for i in range(len(i_mag)) if (stars_f[i])]
        i_x_f = [ix[i] for i in range(len(i_mag)) if (stars_f[i])]
        i_y_f = [iy[i] for i in range(len(i_mag)) if (stars_f[i])]
        fwhm_sf = [fwhm_s[i] for i in range(len(i_mag)) if (stars_f[i])]
        n_in_filter = len(i_mag_f)

        # xedgesg, x_centg, yedgesg, y_centg, Sg, x_cent_Sg, y_cent_Sg, pltsigg, tblg = galaxyMap(fits_file_i, fwhm, dm, filter_file)

        xedges, x_cent, yedges, y_cent, S, x_cent_S, y_cent_S, pltsig, tbl = grid_smooth(
            i_ra_f, i_dec_f, fwhm, width, height)
        # corr = signal.correlate2d(S, Sg, boundary='fill', mode='full')
        # print corr

        # pct, d_bins, d_cens = distfit(n_in_filter,S[x_cent_S][y_cent_S],title_string,width,height,fwhm,dm)
        # pct_hi = 0.0 #getHIcoincidence(x_cent_S, y_cent_S, title_string, ra_corner, dec_corner, width, height, dm)

        # sig_bins.append(d_bins)
        # sig_cens.append(d_cens)
        # sig_max.append(S[x_cent_S][y_cent_S])

        # if pct > 90 :
        #     pct, bj,cj = distfit(n_in_filter,S[x_cent_S][y_cent_S],title_string,width,height,fwhm,dm, samples=25000)

        # make a circle to highlight a certain region
        cosd = lambda x: np.cos(np.deg2rad(x))
        sind = lambda x: np.sin(np.deg2rad(x))
        x_circ = [yedges[y_cent] + 3.0 * cosd(t) for t in range(0, 359, 1)]
        y_circ = [xedges[x_cent] + 3.0 * sind(t) for t in range(0, 359, 1)]

        verts_circ = list(zip(x_circ, y_circ))
        circ_filter = Path(verts_circ)

        stars_circ = circ_filter.contains_points(xy_points)

        i_mag_c = [i_mag[i] for i in range(len(i_mag)) if (stars_circ[i])]
        gmi_c = [gmi[i] for i in range(len(i_mag)) if (stars_circ[i])]
        i_ra_c = [i_ra[i] for i in range(len(i_mag)) if (stars_circ[i])]
        i_dec_c = [i_dec[i] for i in range(len(i_mag)) if (stars_circ[i])]
        i_rad_c = [i_rad[i] for i in range(len(i_mag)) if (stars_circ[i])]
        i_decd_c = [i_decd[i] for i in range(len(i_mag)) if (stars_circ[i])]
        i_x_c = [ix[i] for i in range(len(i_mag)) if (stars_circ[i])]
        i_y_c = [iy[i] for i in range(len(i_mag)) if (stars_circ[i])]
        fwhm_sc = [fwhm_s[i] for i in range(len(i_mag)) if (stars_circ[i])]

        # make a random reference cmd to compare to
        if not os.path.isfile('refCircle.center'):
            rCentx = 16.0 * np.random.random() + 2.0
            rCenty = 16.0 * np.random.random() + 2.0
            with open('refCircle.center', 'w+') as rc:
                print('{:8.4f} {:8.4f}'.format(rCentx, rCenty), file=rc)
        else:
            rCentx, rCenty = np.loadtxt('refCircle.center',
                                        usecols=(0, 1),
                                        unpack=True)

        x_circr = [rCentx + 3.0 * cosd(t) for t in range(0, 359, 1)]
        y_circr = [rCenty + 3.0 * sind(t) for t in range(0, 359, 1)]

        verts_circr = list(zip(x_circr, y_circr))
        rcirc_filter = Path(verts_circr)

        stars_circr = rcirc_filter.contains_points(xy_points)

        i_mag_cr = [i_mag[i] for i in range(len(i_mag)) if (stars_circr[i])]
        gmi_cr = [gmi[i] for i in range(len(i_mag)) if (stars_circr[i])]
        i_ra_cr = [i_ra[i] for i in range(len(i_mag)) if (stars_circr[i])]
        i_dec_cr = [i_dec[i] for i in range(len(i_mag)) if (stars_circr[i])]
        i_rad_cr = [i_rad[i] for i in range(len(i_mag)) if (stars_circr[i])]
        i_decd_cr = [i_decd[i] for i in range(len(i_mag)) if (stars_circr[i])]
        i_x_cr = [ix[i] for i in range(len(i_mag)) if (stars_circr[i])]
        i_y_cr = [iy[i] for i in range(len(i_mag)) if (stars_circr[i])]
        fwhm_scr = [fwhm_s[i] for i in range(len(i_mag)) if (stars_circr[i])]

        i_mag_fc = [
            i_mag[i] for i in range(len(i_mag))
            if (stars_circ[i] and stars_f[i])
        ]
        i_ierr_fc = [
            i_ierr[i] for i in range(len(i_mag))
            if (stars_circ[i] and stars_f[i])
        ]
        g_ierr_fc = [
            g_ierr[i] for i in range(len(i_mag))
            if (stars_circ[i] and stars_f[i])
        ]
        g_mag_fc = [
            i_mag[i] for i in range(len(i_mag))
            if (stars_circ[i] and stars_f[i])
        ]
        gmi_fc = [
            gmi[i] for i in range(len(i_mag)) if (stars_circ[i] and stars_f[i])
        ]
        i_ra_fc = [
            i_ra[i] for i in range(len(i_mag))
            if (stars_circ[i] and stars_f[i])
        ]
        i_dec_fc = [
            i_dec[i] for i in range(len(i_mag))
            if (stars_circ[i] and stars_f[i])
        ]
        i_rad_fc = [
            i_rad[i] for i in range(len(i_mag))
            if (stars_circ[i] and stars_f[i])
        ]
        i_decd_fc = [
            i_decd[i] for i in range(len(i_mag))
            if (stars_circ[i] and stars_f[i])
        ]
        i_x_fc = [
            ix[i] for i in range(len(i_mag)) if (stars_circ[i] and stars_f[i])
        ]
        i_y_fc = [
            iy[i] for i in range(len(i_mag)) if (stars_circ[i] and stars_f[i])
        ]
        fwhm_sfc = [
            fwhm_s[i] for i in range(len(i_mag))
            if (stars_circ[i] and stars_f[i])
        ]
        index_fc = [
            i for i in range(len(i_mag)) if (stars_circ[i] and stars_f[i])
        ]
        # with open('index_fc.txt', 'w+') as spud1:
        #     for i,item in enumerate(index_fc):
        #         print >> spud1, item

        # print len(i_mag_fc), 'filter stars in circle'

        # print 'max i mag in circle = ', min(i_mag_fc)

        rs = np.array([51, 77, 90, 180])
        for r in rs:
            x_circ = [
                yedges[y_cent] + r / 60. * cosd(t) for t in range(0, 359, 1)
            ]
            y_circ = [
                xedges[x_cent] + r / 60. * sind(t) for t in range(0, 359, 1)
            ]

            verts_circ = list(zip(x_circ, y_circ))
            circ_filter = Path(verts_circ)

            stars_circ = circ_filter.contains_points(xy_points)
            i_x_fc = [
                ix[i] for i in range(len(i_mag))
                if (stars_circ[i] and stars_f[i])
            ]
            i_y_fc = [
                iy[i] for i in range(len(i_mag))
                if (stars_circ[i] and stars_f[i])
            ]

            # fcirc_file = 'circle'+repr(r)+'.txt'
            # with open(fcirc_file,'w+') as f3:
            #     for i,x in enumerate(i_x_fc):
            #         print >> f3, i_x_fc[i], i_y_fc[i]

        i_mag_fcr = [
            i_mag[i] for i in range(len(i_mag))
            if (stars_circr[i] and stars_f[i])
        ]
        i_ierr_fcr = [
            i_ierr[i] for i in range(len(i_mag))
            if (stars_circr[i] and stars_f[i])
        ]
        g_ierr_fcr = [
            g_ierr[i] for i in range(len(i_mag))
            if (stars_circr[i] and stars_f[i])
        ]
        g_mag_fcr = [
            i_mag[i] for i in range(len(i_mag))
            if (stars_circr[i] and stars_f[i])
        ]
        gmi_fcr = [
            gmi[i] for i in range(len(i_mag))
            if (stars_circr[i] and stars_f[i])
        ]
        i_ra_fcr = [
            i_ra[i] for i in range(len(i_mag))
            if (stars_circr[i] and stars_f[i])
        ]
        i_dec_fcr = [
            i_dec[i] for i in range(len(i_mag))
            if (stars_circr[i] and stars_f[i])
        ]
        i_rad_fcr = [
            i_rad[i] for i in range(len(i_mag))
            if (stars_circr[i] and stars_f[i])
        ]
        i_decd_fcr = [
            i_decd[i] for i in range(len(i_mag))
            if (stars_circr[i] and stars_f[i])
        ]
        i_x_fcr = [
            ix[i] for i in range(len(i_mag)) if (stars_circr[i] and stars_f[i])
        ]
        i_y_fcr = [
            iy[i] for i in range(len(i_mag)) if (stars_circr[i] and stars_f[i])
        ]
        fwhm_sfcr = [
            fwhm_s[i] for i in range(len(i_mag))
            if (stars_circr[i] and stars_f[i])
        ]

        rcirc_c_x = ra_corner - (rCentx / 60.)
        rcirc_c_y = (rCenty / 60.) + dec_corner
        rcirc_pix_x, rcirc_pix_y = w.wcs_world2pix(rcirc_c_x, rcirc_c_y, 1)
        ra_cr, dec_cr = w.all_pix2world(rcirc_pix_x, rcirc_pix_y, 1)
        ra_cr_d, dec_cr_d = deg2HMS(ra=ra_cr, dec=dec_cr, round=False)

        circ_c_x = ra_corner - (yedges[y_cent] / 60.)
        circ_c_y = (xedges[x_cent] / 60.) + dec_corner
        circ_pix_x, circ_pix_y = w.wcs_world2pix(circ_c_x, circ_c_y, 1)
        ra_c, dec_c = w.all_pix2world(circ_pix_x, circ_pix_y, 1)
        ra_c_d, dec_c_d = deg2HMS(ra=ra_c, dec=dec_c, round=False)
        # print 'Peak RA:',ra_c_d,':: Peak Dec:',dec_c_d

        hi_x_circ, hi_y_circ = getHIellipse(title_string, ra_corner,
                                            dec_corner)
        hi_c_ra, hi_c_dec = getHIellipse(title_string,
                                         ra_corner,
                                         dec_corner,
                                         centroid=True)
        hi_pix_x, hi_pix_y = w.wcs_world2pix(hi_c_ra, hi_c_dec, 1)

        sep = dist2HIcentroid(ra_c_d, dec_c_d, hi_c_ra, hi_c_dec)

        # print "m-M = {:5.2f} | d = {:4.2f} Mpc | α = {:s}, δ = {:s}, Δʜɪ = {:5.1f}' | N = {:4d} | σ = {:6.3f} | ξ = {:6.3f}% | η = {:6.3f}%".format(dm, mpc, ra_c_d, dec_c_d, sep/60., n_in_filter, S[x_cent_S][y_cent_S], pct, pct_hi*100.)

        fig = plt.figure(figsize=(8, 6))
        # plot
        # print "Plotting for m-M = ",dm
        ax0 = plt.subplot(2, 2, 1)
        plt.scatter(i_ra,
                    i_dec,
                    color='black',
                    marker='o',
                    s=1,
                    edgecolors='none')
        plt.plot(x_circ, y_circ, linestyle='-', color='magenta')
        plt.plot(x_circr, y_circr, linestyle='-', color='gold')
        plt.plot(hi_x_circ, hi_y_circ, linestyle='-', color='limegreen')
        # plt.scatter(i_ra_c, i_dec_c,  color='red', marker='o', s=3, edgecolors='none')
        plt.scatter(i_ra_f,
                    i_dec_f,
                    c='red',
                    marker='o',
                    s=10,
                    edgecolors='none')
        # plt.clim(0,2)
        # plt.colorbar()
        plt.ylabel('Dec (arcmin)')
        plt.xlim(0, max(i_ra))
        plt.ylim(0, max(i_dec))
        plt.title('sky positions')
        ax0.set_aspect('equal')

        ax1 = plt.subplot(2, 2, 2)

        if os.path.isfile('i_gmi_compl.gr.out'):
            gmiCompl, iCompl = np.loadtxt('i_gmi_compl.gr.out',
                                          usecols=(0, 1),
                                          unpack=True)
            plt.plot(gmiCompl, iCompl, linestyle='--', color='green')
        if os.path.isfile('i_gmi_compl.gr2.out'):
            gmiCompl, iCompl = np.loadtxt('i_gmi_compl.gr2.out',
                                          usecols=(0, 1),
                                          unpack=True)
            plt.plot(gmiCompl, iCompl, linestyle='--', color='red')
        if os.path.isfile('i_gmi_compl2.out'):
            iCompl, gmiCompl = np.loadtxt('i_gmi_compl2.out',
                                          usecols=(0, 1),
                                          unpack=True)
            plt.plot(gmiCompl, iCompl, linestyle='--', color='blue')

        plt.plot(gi_iso, i_m_iso, linestyle='-', color='blue')
        plt.scatter(gmi,
                    i_mag,
                    color='black',
                    marker='o',
                    s=1,
                    edgecolors='none')
        plt.scatter(gmi_f,
                    i_mag_f,
                    color='red',
                    marker='o',
                    s=15,
                    edgecolors='none')

        # plt.scatter(gmi_c, i_mag_c,  color='red', marker='o', s=3, edgecolors='none')
        plt.errorbar(bxvals,
                     bcenters,
                     xerr=i_ierrAVG,
                     yerr=gmi_errAVG,
                     linestyle='None',
                     color='black',
                     capsize=0,
                     ms=0)
        plt.tick_params(axis='y',
                        left='on',
                        right='off',
                        labelleft='on',
                        labelright='off')
        ax1.yaxis.set_label_position('left')
        plt.ylabel('$i_0$')
        plt.xlabel('$(g-i)_0$')
        plt.ylim(25, 15)
        plt.xlim(-1, 4)
        plt.title('m-M = ' + '{:5.2f}'.format(dm) + ' (' +
                  '{0:4.2f}'.format(mpc) + ' Mpc)')
        ax1.set_aspect(0.5)

        ax2 = plt.subplot(2, 2, 3)

        extent = [yedges[0], yedges[-1], xedges[-1], xedges[0]]
        plt.imshow(S, extent=extent, interpolation='nearest', cmap=cm.gray)
        # plt.imshow(segm, extent=extent, cmap=rand_cmap, alpha=0.5)
        cbar_S = plt.colorbar()
        cbar_S.set_label('$\sigma$ from local mean')
        # cbar_S.tick_params(labelsize=10)
        plt.plot(x_circ, y_circ, linestyle='-', color='magenta')
        plt.plot(x_circr, y_circr, linestyle='-', color='gold')
        plt.plot(hi_x_circ, hi_y_circ, linestyle='-', color='limegreen')
        # X, Y = np.meshgrid(xedges,yedges)
        # ax3.pcolormesh(X,Y,grid_gaus)
        plt.xlabel('RA (arcmin)')
        plt.ylabel('Dec (arcmin)')
        plt.title('smoothed stellar density')
        # plt.ylabel('Dec (arcmin)')
        plt.xlim(0, max(i_ra))
        plt.ylim(0, max(i_dec))
        ax2.set_aspect('equal')

        # ax3 = plt.subplot(2,2,4)
        ax3 = plt.subplot2grid((2, 4), (1, 2))
        plt.scatter(gmi_c,
                    i_mag_c,
                    color='black',
                    marker='o',
                    s=3,
                    edgecolors='none')
        plt.scatter(gmi_fc,
                    i_mag_fc,
                    color='red',
                    marker='o',
                    s=15,
                    edgecolors='none')
        plt.tick_params(axis='y',
                        left='on',
                        right='on',
                        labelleft='off',
                        labelright='off')
        ax0.yaxis.set_label_position('left')
        plt.title('detection')
        plt.xlabel('$(g-i)_0$')
        plt.ylabel('$i_0$')
        plt.ylim(25, 15)
        plt.xlim(-1, 4)
        # ax3.set_aspect(0.5)

        ax4 = plt.subplot2grid((2, 4), (1, 3), sharey=ax3)
        plt.scatter(gmi_cr,
                    i_mag_cr,
                    color='black',
                    marker='o',
                    s=3,
                    edgecolors='none')
        plt.scatter(gmi_fcr,
                    i_mag_fcr,
                    color='red',
                    marker='o',
                    s=15,
                    edgecolors='none')
        plt.tick_params(axis='y',
                        left='on',
                        right='on',
                        labelleft='off',
                        labelright='on')
        plt.title('reference')
        ax0.yaxis.set_label_position('left')
        plt.xlabel('$(g-i)_0$')
        plt.ylim(25, 15)
        plt.xlim(-1, 4)
        # ax3.set _aspect(0.5)
        plt.tight_layout()
        plt.savefig(out_file)
        fig.clf()

    pass