Exemple #1
0
def repeat_sat_cleanup(sagatable):


	sats = sagatable['SATS'] == 1 #& s['ZQUALITY'] > 2

	for s in sagatable[sats]:
		if s['REMOVE'] == -1:

			rsat    = s['RA']
			dsat    = s['DEC']

			m1,m2,d = sm.spherematch(sagatable['RA'], sagatable['DEC'],\
			                     [rsat],[dsat], 10./3600)

			m = d != 0
			if np.sum(m):
				r = m1[m]
				k = m1[d ==0]
				sagatable['REMOVE'][r] = 3
				sagatable['REMOVE'][k] = -1

				sagatable['SATS'][r] = -99
				sagatable['SATS'][k] = 1


	return sagatable
Exemple #2
0
def gri_completeness(base, spec):



	gmag = base['g'] - base['EXTINCTION_G']
	rmag = base['r'] - base['EXTINCTION_R']
	imag = base['i'] - base['EXTINCTION_I']

	gr = gmag - rmag
	ri = rmag - imag

	grerr = np.sqrt(base['g_err']**2 + base['r_err']**2)
	rierr = np.sqrt(base['r_err']**2 + base['i_err']**2)


	cgmr = gr - 2.*grerr
	crmi = ri - 2.*rierr


	msk1 = cgmr < 0.85
	msk2 = crmi < 0.55
	grimsk = msk1&msk2
	ngri = np.sum(msk1&msk2)


	id1,id2,d = sm.spherematch(base['RA'][grimsk], base['DEC'][grimsk],spec['RA'], spec['DEC'],1./3600,nnearest=1)
	ngri_spec = np.size(d)

	p = 100.*ngri_spec/ngri
	
	compl = '{:02.0f}% ({}/{})'.format(p,ngri_spec,ngri)
	return compl
Exemple #3
0
def photoflags(addlist, sagatable):
    """ 
	SET REMOVE FLAG = 3 for bad SDSS Photo Flags
	SET BACK TO REMOVE = -1 if in by-hand list  
	"""
    binned1 = sagatable['BINNED1'] == 0
    saturated = sagatable['SATURATED'] != 0
    baderr = sagatable['BAD_COUNTS_ERROR'] != 0

    # SET REMOVE FOR BAD PHOTOFLAGS
    flgs = binned1 | saturated | baderr
    sagatable['REMOVE'][flgs] = 3

    # SET REMOVE FLAG FOR BRIGHT POORLY MEASURED OBJECTS
    rdiff = np.abs(sagatable['PETRORAD_R'] - sagatable['PETRORAD_G'])
    msk = (rdiff > 40) & (sagatable['r'] < 18)
    sagatable['REMOVE'][msk] = 4

    rdiff = np.abs(sagatable['PETRORAD_R'] - sagatable['PETRORAD_I'])
    msk = (rdiff > 40) & (sagatable['r'] < 18)
    sagatable['REMOVE'][msk] = 4

    raderr = np.average([
        sagatable['PETRORADERR_G'], sagatable['PETRORADERR_R'],
        sagatable['PETRORADERR_I']
    ], 0)
    msk = (sagatable['SB_EXP_R'] > 24) & (raderr == -1000)
    sagatable['REMOVE'][msk] = 5

    # LOOSER CONSTRAINT FOR BRIGHT
    raderr = np.median([
        sagatable['PETRORADERR_G'], sagatable['PETRORADERR_R'],
        sagatable['PETRORADERR_I']
    ], 0)
    msk = (sagatable['SB_EXP_R'] > 24) & (raderr
                                          == -1000) & (sagatable['r'] < 18)
    sagatable['REMOVE'][msk] = 5

    # SET REMOVE FOR BAD PHOTOMETRIC ERRORS
    err = [sagatable['g_err'], sagatable['r_err'], sagatable['i_err']]
    merr = np.median(err, 0)
    msk = np.abs(merr) > 0.5
    sagatable['REMOVE'][msk] = 3

    merr_zero = sagatable['r_err'] <= 0
    print 'ZERO ERROR = ', np.sum(merr_zero)

    # MATCH sql OBJECTS TO THOSE IN GOOGLE DOC ADD LIST
    id1,id2,d = sm.spherematch(sagatable['RA'], sagatable['DEC'],\
                addlist.field('Targ_RA'), addlist.field('Targ_Dec'),\
                1./3600,nnearest=1)

    nmatch = np.size((d > 0.0).nonzero())
    print "add list objects = ", nmatch

    # SET REMOVED FLAG BACK TO -1
    if (nmatch > 0):
        sagatable['REMOVE'][id1] = -1

    return sagatable
Exemple #4
0
def gri_completeness(base, spec):

    gmag = base['g'] - base['EXTINCTION_G']
    rmag = base['r'] - base['EXTINCTION_R']
    imag = base['i'] - base['EXTINCTION_I']

    gr = gmag - rmag
    ri = rmag - imag

    grerr = np.sqrt(base['g_err']**2 + base['r_err']**2)
    rierr = np.sqrt(base['r_err']**2 + base['i_err']**2)

    cgmr = gr - 2. * grerr
    crmi = ri - 2. * rierr

    msk1 = cgmr < 0.85
    msk2 = crmi < 0.55
    grimsk = msk1 & msk2
    ngri = np.sum(msk1 & msk2)

    id1, id2, d = sm.spherematch(base['RA'][grimsk],
                                 base['DEC'][grimsk],
                                 spec['RA'],
                                 spec['DEC'],
                                 1. / 3600,
                                 nnearest=1)
    ngri_spec = np.size(d)

    p = 100. * ngri_spec / ngri

    compl = '{:02.0f}% ({}/{})'.format(p, ngri_spec, ngri)
    return compl
Exemple #5
0
def photoflags(addlist,sagatable):
	""" 
	SET REMOVE FLAG = 3 for bad SDSS Photo Flags
	SET BACK TO REMOVE = -1 if in by-hand list  
	"""
	binned1   = sagatable['BINNED1'] == 0
	saturated = sagatable['SATURATED'] != 0
	baderr    = sagatable['BAD_COUNTS_ERROR'] != 0 

	flgs = binned1 | saturated | baderr
	sagatable['REMOVE'][flgs] = 3


   # MATCH sql OBJECTS TO THOSE IN GOOGLE DOC ADD LIST
	id1,id2,d = sm.spherematch(sagatable['RA'], sagatable['DEC'],\
		           addlist.field('Targ_RA'), addlist.field('Targ_Dec'),\
		           1./3600,nnearest=1)

	nmatch = np.size((d > 0.0).nonzero())
	print "add list objects = ",nmatch

  # SET REMOVED FLAG BACK TO -1
  	if (nmatch > 0):
		sagatable['REMOVE'][id1] = -1


	return sagatable
Exemple #6
0
def repeat_sat_cleanup(sagatable):

    # FIND REPEAT SATELLITES
    sats = sagatable['SATS'] == 1  #& s['ZQUALITY'] > 2
    for s in sagatable[sats]:
        if s['REMOVE'] == -1:

            rsat = s['RA']
            dsat = s['DEC']

            m1,m2,d = sm.spherematch(sagatable['RA'], sagatable['DEC'],\
                                 [rsat],[dsat], 20./3600)

            m = d != 0
            if np.sum(m):
                r = m1[m]
                k = m1[d == 0]
                sagatable['REMOVE'][r] = 3
                sagatable['REMOVE'][k] = -1

                sagatable['SATS'][r] = -99
                sagatable['SATS'][k] = 1

# FIND REPEAT LOWZ satellites
    sats = sagatable['SATS'] == 2  #& s['ZQUALITY'] > 2
    for s in sagatable[sats]:
        if s['REMOVE'] == -1:

            rsat = s['RA']
            dsat = s['DEC']

            m1,m2,d = sm.spherematch(sagatable['RA'], sagatable['DEC'],\
                                 [rsat],[dsat], 20./3600)

            m = d != 0
            if np.sum(m):
                r = m1[m]
                k = m1[d == 0]
                sagatable['REMOVE'][r] = 3
                sagatable['REMOVE'][k] = -1

                sagatable['SATS'][r] = -99
                sagatable['SATS'][k] = 2

    return sagatable
Exemple #7
0
def ML_completeness(base, spec, prob):

	ML_msk = base['PCLASS_1'] > prob
	nML = np.sum(ML_msk)


	id1,id2,d = sm.spherematch(base['RA'][ML_msk], base['DEC'][ML_msk],spec['RA'], spec['DEC'],1./3600,nnearest=1)
	nML_spec = np.size(d)

	p = 100.*nML_spec/nML
	
	compl = '{:02.0f}% ({}/{})'.format(p,nML_spec,nML)
	return compl
Exemple #8
0
def rm_removelist_obj(removelist,sagatable):

	
   # MATCH sql OBJECTS TO THOSE IN GOOGLE DOC REMOVE LIST
	id1,id2,d = sm.spherematch(sagatable['RA'], sagatable['DEC'],\
		           removelist.field('Targ_RA'), removelist.field('Targ_Dec'),\
		           1./3600,nnearest=1)

	nmatch = np.size((d > 0.0).nonzero())

  # SET REMOVED FLAG TO 1
  	if (nmatch > 0):
		sagatable['REMOVE'][id1] = 1
	print "Cleaning remove list objects = ",nmatch
	return sagatable
Exemple #9
0
 def onclick(event):
     if event.button !=3:
        coords = np.vstack((event.xdata,667-event.ydata)).T
        world = w.wcs_pix2world(coords,1)
        result = psm.spherematch(world[:,0],world[:,1], self.ra_all, self.dec_all, nnearest=1)
        if (result[0] < 1./3600.):
           xpix,ypix = w.wcs_world2pix(self.ra_all[result[1].astype(int)], self.dec_all[result[1].astype(int)],1)
           if (((xpix == self.xpix) & (ypix == self.ypix)).any() == False):
              self.xpix = np.append(self.xpix,xpix)
              self.ypix = np.append(self.ypix,ypix)
              self.ra = np.append(self.ra, self.ra_all[result[1].astype(int)])
              self.dec = np.append(self.dec, self.dec_all[result[1].astype(int)])
              self.gr_color = np.append(self.gr_color, self.gr_color_all[result[1].astype(int)])
              self.gi_color = np.append(self.gi_color, self.gi_color_all[result[1].astype(int)])
              self.ri_color = np.append(self.ri_color, self.ri_color_all[result[1].astype(int)])
              self.iz_color = np.append(self.iz_color, self.iz_color_all[result[1].astype(int)])
              self.rz_color = np.append(self.rz_color, self.rz_color_all[result[1].astype(int)])
              self.g_mag = np.append(self.g_mag, galaxy['mag_auto_g'][result[1].astype(int)])
              self.r_mag = np.append(self.r_mag, galaxy['mag_auto_r'][result[1].astype(int)])
              self.i_mag = np.append(self.i_mag, galaxy['mag_auto_i'][result[1].astype(int)])
              self.z_mag = np.append(self.z_mag, galaxy['mag_auto_z'][result[1].astype(int)])
           else:
              keep = where(((xpix == self.xpix) & (ypix == self.ypix))==False)
              self.xpix = self.xpix[keep]
              self.ypix = self.ypix[keep]
              self.ra = self.ra[keep]
              self.dec = self.dec[keep]
       	     self.gr_color = self.gr_color[keep]
              self.gi_color = self.gi_color[keep]
              self.ri_color = self.ri_color[keep]
              self.iz_color = self.iz_color[keep]
              self.rz_color = self.rz_color[keep]
              self.g_mag = self.g_mag[keep]
              self.r_mag = self.r_mag[keep]
              self.i_mag = self.i_mag[keep]
              self.z_mag = self.z_mag[keep]
           self.temp_xpix=xpix
           self.temp_ypix=ypix
           self.temp_gr_color=self.gr_color_all[result[1].astype(int)]
           self.temp_gi_color=self.gi_color_all[result[1].astype(int)]
           self.temp_ri_color=self.ri_color_all[result[1].astype(int)]
           self.temp_iz_color=self.iz_color_all[result[1].astype(int)]
           self.temp_rz_color=self.rz_color_all[result[1].astype(int)]
           self.temp_r_mag=galaxy['mag_auto_r'][result[1].astype(int)]
           self.temp_i_mag=galaxy['mag_auto_i'][result[1].astype(int)]
           display_ax0()
           display_bx()
Exemple #10
0
def gri_completeness(base, spec):

	gmr  = base['g'] - base['r'] - 2*base['g_err'] - 2*base['r_err']
	rmi  = base['r'] - base['i'] - 2*base['r_err'] - 2*base['i_err']

	msk1 = gmr < 1.0
	msk2 = rmi < 0.5
	grimsk = msk1&msk2
	ngri = np.sum(msk1&msk2)


	id1,id2,d = sm.spherematch(base['RA'][grimsk], base['DEC'][grimsk],spec['RA'], spec['DEC'],1./3600,nnearest=1)
	ngri_spec = np.size(d)

	p = 100.*ngri_spec/ngri
	
	compl = '{:02.0f}% ({}/{})'.format(p,ngri_spec,ngri)
	return compl
Exemple #11
0
def rm_removelist_obj(removelist, sagatable):

    # MATCH sql OBJECTS TO THOSE IN GOOGLE DOC REMOVE LIST
    id1,id2,d = sm.spherematch(sagatable['RA'], sagatable['DEC'],\
                removelist.field('Targ_RA'), removelist.field('Targ_Dec'),\
                1./3600,nnearest=1)

    nmatch = np.size((d > 0.0).nonzero())

    # SET REMOVED FLAG TO 1
    if (nmatch > 0):
        sagatable['REMOVE'][id1] = 1
    print "Cleaning remove list objects = ", nmatch

    # CATCH THINGS CLOSE TO HOST
    m = sagatable['RHOST_KPC'] < 10
    sagatable['REMOVE'][m] = 1

    return sagatable
Exemple #12
0
def add_saga_spec(sagaspec,sqltable):

	
   # MATCH sql OBJECTS TO THOSE IN SAGA SPEC
	id1,id2,d = sm.spherematch(sqltable['RA'], sqltable['DEC'],\
				   sagaspec['RA'], sagaspec['DEC'],\
		           1./3600,nnearest=1)

	nmatch = np.size((d >= 0.0).nonzero())
	print "adding existing SAGA SPECTRA = ",nmatch

  	if (nmatch > 0):
		sqltable['SPEC_Z'][id1]   = sagaspec['SPEC_Z'][id2]
		sqltable['ZQUALITY'][id1] = sagaspec['ZQUALITY'][id2]
		sqltable['TELNAME'][id1]  = sagaspec['TELNAME'][id2]
		sqltable['MASKNAME'][id1] = sagaspec['MASKNAME'][id2]
		sqltable['SPECOBJID'][id1]    = sagaspec['SPECOBJID'][id2]
		sqltable['SPEC_REPEAT'][id1]  = sagaspec['SPEC_REPEAT'][id2]
		sqltable['SATS'][id1]  = sagaspec['SATS'][id2]
#		sqltable['REMOVE'][id1]  = sagaspec['REMOVE'][id2]


	return sqltable
Exemple #13
0
def add_saga_spec(sagaspec, sqltable):

    # MATCH sql OBJECTS TO THOSE IN SAGA SPEC
    id1,id2,d = sm.spherematch(sqltable['RA'], sqltable['DEC'],\
          sagaspec['RA'], sagaspec['DEC'],\
                1./3600,nnearest=1)

    nmatch = np.size((d >= 0.0).nonzero())
    print "adding existing SAGA SPECTRA = ", nmatch

    if (nmatch > 0):
        sqltable['SPEC_Z'][id1] = sagaspec['SPEC_Z'][id2]
        sqltable['ZQUALITY'][id1] = sagaspec['ZQUALITY'][id2]
        sqltable['TELNAME'][id1] = sagaspec['TELNAME'][id2]
        sqltable['MASKNAME'][id1] = sagaspec['MASKNAME'][id2]
        sqltable['SPECOBJID'][id1] = sagaspec['SPECOBJID'][id2]
        sqltable['SPEC_REPEAT'][id1] = sagaspec['SPEC_REPEAT'][id2]
        sqltable['SPEC_SN'][id1] = sagaspec['SPEC_SN'][id2]
        sqltable['SPEC_HA_EW'][id1] = sagaspec['SPEC_HA_EW'][id2]
        sqltable['SPEC_HA_EWERR'][id1] = sagaspec['SPEC_HA_EWERR'][id2]
        sqltable['SATS'][id1] = sagaspec['SATS'][id2]
        sqltable['REMOVE'][id1] = sagaspec['REMOVE'][id2]

    return sqltable
Exemple #14
0
def nsa_cleanup(nsa,sagatable):

   # MATCH NSA to SAGA, BEGIN WITH SMALLER RADIUS
	id2,id1,d = sm.spherematch(nsa['RA'], nsa['DEC'],\
							sagatable['RA'], sagatable['DEC'],\
							5./3600,nnearest = 1)

	nmatch = np.size(id1)

    # FOR EACH UNIQUE NSA MATCH
	for i in range(0,nmatch-1):

		nid = id2[i]
		sid = id1[i]
		nra = nsa['RA'][nid]
		ndec= nsa['DEC'][nid]

	# FIRST PASS USING SPHEREMATCH
		reff_nsa = 3*nsa['PETROTH90'][nid]		
		m1,m2,dd = sm.spherematch(sagatable['RA'], sagatable['DEC'],\
			[nra],[ndec],reff_nsa/3600)


	# FIND POINTS INSIDE ELLIPSE
		a = 2.*nsa['PETROTH90'][nid]
		b = nsa['SERSIC_BA'][nid] * a
		th = (np.pi/180) * (nsa['SERSIC_PHI'][nid]+270)
		
		sth = np.sin(th)
		cth = np.cos(th)

		x = 3600*(sagatable['RA'][m1] - nra)
		y = 3600*(sagatable['DEC'][m1] - ndec)
		tmp1 = (x*cth) - (y*sth)  
		tmp2 = (x*sth) + (y*cth)
		
		rem = (tmp1/a)**2 + (tmp2/b)**2 <= 1.  # true for points inside ellipse
		

      # REMOVE SHREDDED OBJECTS NEAR NSA 
		sagatable['REMOVE'][m1[rem]] = 2     

      # EXPLICITLY ADD NSA PROPERTIES BACK INTO DATABASE
		sagatable['REMOVE'][sid]   = -1   
		sagatable['ZQUALITY'][sid] = 4
		sagatable['TELNAME'][sid]  = 'NSA'
		sagatable['PHOTPTYPE'][sid]= 3
		sagatable['PHOT_SG'][sid]  = 'GALAXY'
		sagatable['RA'][sid]       = nra
		sagatable['DEC'][sid]      = ndec
		sagatable['SPEC_Z'][sid]   = nsa['Z'][nid]
		sagatable['SPEC_Z_WARN'][sid] = 0
		sagatable['MASKNAME'][sid] = nsa['ZSRC'][nid]
		sagatable['OBJ_NSAID'][sid]= nsa['NSAID'][nid]

		# REPLACE PHOTOMETRY
		mag = 22.5 - 2.5*np.log10(nsa['SERSICFLUX'][nid])
		mag[np.isnan(mag)]= -99

		sagatable['u'][sid]= mag[2]
		sagatable['g'][sid]= mag[3]
		sagatable['r'][sid]= mag[4]
		sagatable['i'][sid]= mag[5]
		sagatable['z'][sid]= mag[6]



#		sagatable['expRad_r'][sid] = nsa['SERSIC_TH50'][nid]
#		sagatable['sb_exp_r'][sid] = -99#nsa['PETROTH90']
#		sagatable['petroR90_r'][sid] = nsa['PETROTH90'][nid]#
#		sagatable['petroR50_r'][sid] = nsa['PETROTH50'][nid]
#		sagatable['petroMag_r'][sid] = -99#nsa['PETROTH90']


	return sagatable
Exemple #15
0
def nsa_cleanup(nsa, sagatable):

    # MATCH NSA to SAGA, BEGIN WITH SMALLER RADIUS
    id2,id1,d = sm.spherematch(nsa['RA'], nsa['DEC'],\
          sagatable['RA'], sagatable['DEC'],\
          5./3600,nnearest = 1)

    nmatch = np.size(id1)

    # FOR EACH UNIQUE NSA MATCH
    for i in range(0, nmatch):

        nid = id2[i]
        sid = id1[i]
        nra = nsa['RA'][nid]
        ndec = nsa['DEC'][nid]

        # FIRST PASS USING SPHEREMATCH
        reff_nsa = 3 * nsa['PETROTH90'][nid]
        m1,m2,dd = sm.spherematch(sagatable['RA'], sagatable['DEC'],\
         [nra],[ndec],reff_nsa/3600)

        # FIND POINTS INSIDE ELLIPSE
        a = 2. * nsa['PETROTH90'][nid]
        b = nsa['SERSIC_BA'][nid] * a
        th = (np.pi / 180) * (nsa['SERSIC_PHI'][nid] + 270)

        sth = np.sin(th)
        cth = np.cos(th)

        x = 3600 * (sagatable['RA'][m1] - nra)
        y = 3600 * (sagatable['DEC'][m1] - ndec)
        tmp1 = (x * cth) - (y * sth)
        tmp2 = (x * sth) + (y * cth)

        rem = (tmp1 / a)**2 + (tmp2 /
                               b)**2 <= 1.  # true for points inside ellipse

        # REMOVE SHREDDED OBJECTS NEAR NSA
        sagatable['REMOVE'][m1[rem]] = 2

        # EXPLICITLY ADD NSA PROPERTIES BACK INTO DATABASE
        sagatable['REMOVE'][sid] = -1
        sagatable['ZQUALITY'][sid] = 4
        sagatable['TELNAME'][sid] = 'NSA'
        sagatable['PHOTPTYPE'][sid] = 3
        sagatable['PHOT_SG'][sid] = 'GALAXY'
        sagatable['RA'][sid] = nra
        sagatable['DEC'][sid] = ndec
        sagatable['SPEC_Z'][sid] = nsa['Z'][nid]
        sagatable['SPEC_HA_EW'][sid] = nsa['HAEW'][nid]
        sagatable['SPEC_HA_EWERR'][sid] = nsa['HAEWERR'][nid]
        sagatable['SPEC_REPEAT'][sid] = 'SDSS+NSA'
        sagatable['MASKNAME'][sid] = nsa['ZSRC'][nid]
        sagatable['OBJ_NSAID'][sid] = nsa['NSAID'][nid]

        # REPLACE PHOTOMETRY
        mag = 22.5 - 2.5 * np.log10(nsa['SERSICFLUX'][nid])
        var = 1. / nsa['SERSICFLUX_IVAR'][nid]
        tmp1 = 2.5 / (nsa['SERSICFLUX'][nid] * np.log(10.))
        mag_err = np.sqrt(var * tmp1**2)  # IS THIS RIGHT??

        mag[np.isnan(mag)] = -99
        mag_err[np.isnan(mag_err)] = -99

        sagatable['u'][sid] = mag[2]
        sagatable['g'][sid] = mag[3]
        sagatable['r'][sid] = mag[4]
        sagatable['i'][sid] = mag[5]
        sagatable['z'][sid] = mag[6]

        sagatable['u_err'][sid] = mag_err[2]
        sagatable['g_err'][sid] = mag_err[3]
        sagatable['r_err'][sid] = mag_err[4]
        sagatable['i_err'][sid] = mag_err[5]
        sagatable['z_err'][sid] = mag_err[6]

        #		print nsa['NSAID'][nid],np.sum(rem)

        #		sagatable['expRad_r'][sid] = nsa['SERSIC_TH50'][nid]
        sagatable['SB_EXP_R'][sid] = mag[4] + 2.5 * np.log10(
            2 * np.pi * nsa['PETROTH50'][nid]**2 + 1e-20)
        sagatable['PETROR90_R'][sid] = nsa['PETROTH90'][nid]  #
        sagatable['PETROR50_R'][sid] = nsa['PETROTH50'][nid]

    return sagatable
                              cat2x='glon_cen',
                              cat2y='glat_cen')

matchnames_peak_v1 = []
matchdist_peak_v1 = []
matchxdiff_peak_v1 = []
matchydiff_peak_v1 = []
matchcatnum_peak_v1 = []
v1matches_peak = bolocatv1.flux * 0

glonv1_peak = bolocatv1.glon_peak - (360 * (bolocatv1.glon_peak > 345))
glonv2_peak = bgpsv2_nonew.glon_peak - (360 * (bgpsv2_nonew.glon_peak > 345))

# find nearest v1 source to each v2 source
indv2v1, matchindv2v1, distv2v1 = pyspherematch.spherematch(
    bgpsv2_nonew['glon_peak'], bgpsv2_nonew['glat_peak'],
    bolocatv1['glon_peak'], bolocatv1['glat_peak'])

for name, catnum, glon_peak, glat_peak, ii, mi, dd in \
        zip(bgpsv2_nonew['name'], bgpsv2_nonew['cnum'], glonv2_peak,
                bgpsv2_nonew['glat_peak'], indv2v1, matchindv2v1, distv2v1):
    # finding nearest v1 to v2

    dx = (glonv1_peak[mi] - glon_peak)
    dy = (bolocatv1['glat_peak'][mi] - glat_peak)
    #dd = ( dx**2 + dy**2 ) **0.5
    #closest = dd.argmin()

    matchnames_peak_v1.append(bolocatv1.name[mi])
    matchdist_peak_v1.append(dd)
    matchxdiff_peak_v1.append(dx)
def match_zspec_zphoto_cmass(): 
    ''' Match photometric redshift to spectroscopic redshift. Spherematch 
    photometric and spectroscopic catalogs to determine redshift errors of 
    photmetric redshifts wrt spectroscopic redshifts. 
    
    Returns [z_spec, z_photo]

    --------------------------------------------------------------------------
    Parameters 
    --------------------------------------------------------------------------
    None

    --------------------------------------------------------------------------
    Notes
    --------------------------------------------------------------------------
    * Using pyspherematch
    * What is photometric redshift error in photometric catalog? 
    * Details on CMASS redshifts in: https://trac.sdss3.org/wiki/BOSS/clustering/WGCatalogCode
    
    '''

    # read in photometric catalog. See notes for 
    # catalog details 
    photo_file = ''.join([
        '/mount/riachuelo1/hahn/photoz/',
        'DR8LRG_CMASS_phtz_VF.cat'
        ]) 
    photo_ra, photo_dec, photo_z, photo_zerr = np.loadtxt(
            photo_file, 
            unpack=True, 
            skiprows=1, 
            usecols=[0,1,2,3]) 

    # read in spectroscopic catalog. DR12v4 Full 
    # catalog. 
    spec_file = ''.join([
        '/mount/riachuelo1/hahn/data/CMASS/',
        'cmass-dr12v4-N-Reid-full.dat.fits'
        ]) 
    spec = mrdfits(spec_file) 
    spec_ra     = spec.ra
    spec_dec    = spec.dec
    spec_z      = spec.z
    spec_imatch = spec.imatch
    
    # spherematch spectroscopic catalog RA and Decl to 
    # photometric catalog RA and Decl. Afterwards, loop
    # through each of the matches and only keep galaxies
    # that have spectroscopic redshifts. This way we can 
    # calculate the errors in photometric redshift. 
    match_spec, match_photo, d = pysph.spherematch(
            spec_ra, spec_dec, photo_ra, photo_dec, tol=None) 
    
    zspec, zphoto = [], [] 

    for i_spec, i_m in enumerate(match_spec):     
        i_match = spec_imatch[i_m]
        # imatch==0:  Not matched to a redshift.
        # imatch==1:  Matched to a new BOSS redshift.
        # imatch==2:  Matched to an old SDSS redshift.
        # imatch==3:  Fiber collision corrected (using fiberc_zfail)
        # imatch==4:  Star (remove from target list when computing completeness).
        # imatch==5:  Redshift failure corrected (using fiberc_zfail)
        # imatch==6:  Fiber collision, but we cannot find a galaxy* 
        #       redshift for it, this is used for flagging the collision, but if it is not 
        #       overwritten by fiberc_zfail as we find a nearest neighbor (imatch=3), 
        #       it will remain to be 6.
        # imatch==7:  Redshift failure, but we cannot find a galaxy* 
        #       redshift for it, this is initially used for flagging the redshift failures, 
        #       but if it is not overwritten by fiberc_zfail as we find a nearest neighbor 
        #       (imatch=5), it will remain to be 7. 

        if i_match not in (1,2):
            continue 

        if d[i_m] > 10**-10: 
            continue 
        
        zspec.append(spec_z[i_spec])
        zphoto.append(photo_z[match_photo[i_m]]) 
    
    zspec = np.array(zspec)
    zphoto = np.array(zphoto) 

    return [zspec, zphoto]
def compile_saga_spectra(flagged_obs_hosts=False):

    # RUN EITHER FULL HOST LIST OR JUST FLAG ZERO HOSTS, default to flag zero
    if flagged_obs_hosts:
        sheet = GoogleSheets('1GJYuhqfKeuJr-IyyGF_NDLb_ezL6zBiX2aeZFHHPr_s', 0)
    else:
        sheet = GoogleSheets('1b3k2eyFjHFDtmHce1xi6JKuj3ATOWYduTBFftx5oPp8',
                             448084634)
        hostdata = sheet.load()

        # READ ALL NON-SDSS SPECTRA AND COMBINE INTO SINGLE TABLE
        gama_table = read_saga_spectra.read_gama()
        mmt_table = read_saga_spectra.read_mmt()
        aat_table = read_saga_spectra.read_aat()
        aat_table_mz = read_saga_spectra.read_aat_mz()
        imacs_table = read_saga_spectra.read_imacs()
        wiyn_table = read_saga_spectra.read_wiyn()
        deimos_table = read_saga_spectra.read_deimos()

        # LEAST TO MOST IMPORTANT
        sagaspec = table.vstack([wiyn_table,imacs_table,gama_table,deimos_table,aat_table,mmt_table,aat_table_mz],\
                  metadata_conflicts='silent')

        # WRITE FILE WITH ALL SPECTRA
        file = os.path.join(SAGA_DIR, 'data', 'saga_spectra_raw.fits.gz')
        write_fits(sagaspec, file)

        # COMBINE MULTIPLE SPECTRA
        #	read_saga_spectra.spec_combine(sagaspec)
        spec = sagaspec
        sagaspec = read_saga_spectra.find_uniques(spec)

        # FOR EACH FLAG ZERO HOST OR NON-FLAG ZERO OBSERVED HOST
        # READ BASE SDSS FILE
        #  KEEP ONLY SDSS SPECTRA AND MATCHES FROM OTHER SOURCES
        nhost = 0
        for host in hostdata:
            flag = host['flag']  # SELECT ONLY NON_FLAG0#
            if flag == 0:
                nra = host['RA']
                ndec = host['Dec']
                nsaid = host['NSAID']  # NAME OF BASE SQL FILES
                basefile = os.path.join(
                    SAGA_DIR, 'base_catalogs',
                    'base_sql_nsa{0}.fits.gz'.format(nsaid))
                basetable = Table.read(basefile)
                print nsaid

                # CALCULATE OBJECT DISTANCE FROM HOST
                catsc = SkyCoord(u.Quantity(sagaspec['RA'], u.deg),
                                 u.Quantity(sagaspec['DEC'], u.deg))
                hostcoords = SkyCoord(nra * u.deg, ndec * u.deg)
                seps = catsc.separation(hostcoords)
                rhost = seps.to(u.deg).value
                host_spec_objs = rhost < 1.0

                # COMBINE SDSS SPECTRA AND SAGA SPECTRA
                sdss_table = read_saga_spectra.read_sdss(basetable)
                hostspec = table.vstack([sdss_table, sagaspec[host_spec_objs]])

                tmp = hostspec
                hostspec = read_saga_spectra.find_uniques(tmp)

                id2, id1, d = sm.spherematch(hostspec['RA'],
                                             hostspec['DEC'],
                                             basetable['RA'],
                                             basetable['DEC'],
                                             3. / 3600,
                                             nnearest=1)
                basetable['TELNAME'][id1] = hostspec['TELNAME'][id2]
                basetable['MASKNAME'][id1] = hostspec['MASKNAME'][id2]
                basetable['ZQUALITY'][id1] = hostspec['ZQUALITY'][id2]
                basetable['SPEC_Z'][id1] = hostspec['SPEC_Z'][id2]
                basetable['SPEC_Z_ERR'][id1] = hostspec['SPEC_Z_ERR'][id2]
                basetable['SPECOBJID'][id1] = hostspec['specobjid'][id2]
                basetable['SPEC_REPEAT'][id1] = hostspec['SPEC_REPEAT'][id2]

                m = basetable['SPEC_Z'] != -1

                m2 = basetable['TELNAME'][m] == ''
                print 'number with telname = ', np.sum(m2)

                # COMBINE INTO SINGLE ALLSPEC FILE
                if (nhost == 0):
                    allspec = basetable[m]
                if (nhost > 0):
                    allspec = table.vstack([allspec, basetable[m]])  # APPEND
                nhost = nhost + 1

# INITIALIZE SATS ARRAY (3=primary, 2=lowz, 1=satellite)
        allspec = saga_tools.fill_sats_array(allspec)

        # CLEAN UP REPEAT ENTRY OF SATELLITES
        allspec = saga_tools.repeat_sat_cleanup(allspec)

        # WRITE ALL SPECTRA TAKEN
        file = os.path.join(SAGA_DIR, 'data', 'saga_spectra_dirty.fits.gz')
        write_fits(allspec, file)

        # WRITE ALL GOOD SPECTRA
        # KEEP ALL GOOD SPECTRA WHICH ARE GALAXIES
        zql = allspec['ZQUALITY'] >= 3
        rml = allspec['REMOVE'] == -1
        galonly = allspec['PHOTPTYPE'] == 3
        clean = zql & rml & galonly
        allclean = allspec[clean]
        file = os.path.join(SAGA_DIR, 'data', 'saga_spectra_clean.fits.gz')
        write_fits(allclean, file)

        # WRITE TEXT FILE OF SATELLITES WHICH CAN BE EDITED
        #	file  = os.path.join(SAGA_DIR, 'data', 'saga_satellites.dat')
        #	allsats = allclean['SATS' == 1]
        #	write_satellite_file(allsats,file)

        return allspec
def create_base_catalog(nsaid, host,nowise,noML,nosaga):
    """
    Create single base catalog from SQL request
    with value-added quantities    
    """

    # READ SQL FILE
    sqlfile  = os.path.join(SAGA_DIR, 'base_catalogs', 'sql_nsa{0}.fits.gz'.format(nsaid))
    sqltable = Table.read(sqlfile)

    # GET BASIC HOST PARAMETERS FROM GOOGLE HOST LIST
    hostra   = host['RA']        #RA
    hostdec  = host['Dec']       #DEC
    hostdist = host['distance']  #DISTANCE
    hostv    = host['vhelio']    #VHELIO
    hostMK   = host['M_K'] 
    hostMr   = host['M_r'] 
    hostMg   = host['M_g'] 

#    hostflag = host['flag']      #HOST FLAG



    # CALCULATE OBJECT DISTANCE FROM HOST
    catsc = SkyCoord(u.Quantity(sqltable['RA'], u.deg), u.Quantity(sqltable['DEC'], u.deg))
    hostcoords = SkyCoord(hostra, hostdec,unit='deg')
    seps = catsc.separation(hostcoords)

    rhost_arcm = seps.to(u.arcmin).value
    rhost_kpc  = 1000.*hostdist*np.sin(np.deg2rad(rhost_arcm/60.))



    # ADD EXTRA COLUMNS -
    size = len(sqltable)
    cols = [_filled_column('W1',-1.,size),
            _filled_column('W1ERR',-1.,size),
            _filled_column('W2',-1.,size),
            _filled_column('W2ERR',-1.,size),
            _filled_column('HOST_RA', hostra, size), #HOST
            _filled_column('HOST_DEC', hostdec, size),
            _filled_column('HOST_DIST', hostdist, size),
            _filled_column('HOST_VHOST', hostv, size),
            _filled_column('HOST_MK', hostMK, size),
            _filled_column('HOST_MR', hostMr, size),
            _filled_column('HOST_MG', hostMg, size),
            _filled_column('HOST_NSAID', nsaid, size),
            _filled_column('HOST_SAGA_NAME', ' '*48, size),
            _filled_column('HOST_NGC_NAME', ' '*48, size),
            Column(rhost_arcm, 'RHOST_ARCM'), #OBJECT
            Column(rhost_kpc, 'RHOST_KPC'),
            _filled_column('OBJ_NSAID', -1, size),
            _filled_column('SATS', -1, size),
            _filled_column('PROBABILITY_CLASS1', -1., size),
            _filled_column('RESCALED_PROBABILITY_CLASS1', -1., size),
            _filled_column('REMOVE', -1, size), # -1 = Good source; 1 = On remove list; 2 = Overlaps with NSA GALAXY
            _filled_column('TELNAME', ' '*6, size), #SPECTRA
            _filled_column('MASKNAME', ' '*48, size),
            _filled_column('ZQUALITY', -1, size),
            _filled_column('SPEC_CLASS', ' '*2, size),
            _filled_column('SPECOBJID', ' '*48L, size),
            _filled_column('SPEC_REPEAT', ' '*48, size),
            _filled_column('SPEC_SN', -99., size),
            _filled_column('SPEC_HA_EW', -99., size),
            _filled_column('SPEC_HA_EWERR', -99., size)]


    sqltable.add_columns(cols)
    del cols


    # ADD WISE NUMBERS
    if not nowise:
        wbasefile = os.path.join(SAGA_DIR, 'unwise', 'unwise_{0}.fits.gz'.format(nsaid))
        print 'Read WISE catalog: ', wbasefile

        wbasetable = FitsTable(wbasefile).load()
        wbasetable['W1_MAG'][np.isnan(wbasetable['W1_MAG'])]=-1
        wbasetable['W1_MAG_ERR'][np.isnan(wbasetable['W1_MAG_ERR'])]=-1
        wbasetable['W2_MAG'][np.isnan(wbasetable['W2_MAG'])]=-1
        wbasetable['W2_MAG_ERR'][np.isnan(wbasetable['W2_MAG_ERR'])]=-1
        id1, id2, d = sm.spherematch(sqltable['RA'], sqltable['DEC'], wbasetable['RA'], wbasetable['DEC'], 1./3600)
        sqltable['W1'][id1]    = wbasetable['W1_MAG'][id2]
        sqltable['W1ERR'][id1] = wbasetable['W1_MAG_ERR'][id2]
        sqltable['W2'][id1]    = wbasetable['W2_MAG'][id2]
        sqltable['W2ERR'][id1] = wbasetable['W2_MAG_ERR'][id2]



    # ADD ML PREDICTIONS USING OLD FILE
    if not noML:
        id1, id2, d = sm.spherematch(sqltable['RA'], sqltable['DEC'], ML['RA'], ML['DEC'], 1./3600)
        sqltable['PROBABILITY_CLASS1'][id1]    = ML['PROBABILITY_CLASS_1'][id2]





    # INITALIZE SDSS SPECTRAL ENTRIES
    msk = sqltable['SPEC_Z'] != -1
    sqltable['TELNAME'][msk] = 'SDSS'
    sqltable['MASKNAME'][msk] = 'SDSS'
    sqltable['SPEC_REPEAT'][msk] = 'SDSS'
    sqltable['ZQUALITY'][msk] = 4

    # SET BAD SDSSS SPECTROSCOPY TO ZQ = -1
    # THERE ARE A FEW RECOVERABLE SPECTRA WITH SPEC_Z_WARN = 4, 
    # BUT WILL NOT RECOVER THEM HERE
    msk_badsdss = sqltable['SPEC_Z_WARN'] != 0
    sqltable['ZQUALITY'][msk & msk_badsdss] = 1


    # IF THIS IS A SAGA HOST, SET SAGA NAME
    names = SAGANAMES.load()
    sname = ''
    ngc   = '' 
    if nsaid in names['NSA']: 
        msk = names['NSA'] == nsaid
        sname = names['SAGA'][msk]
        ngc = names['NGC'][msk]
    sqltable['HOST_SAGA_NAME'] = sname
    sqltable['HOST_NGC_NAME'] = ngc


    # ADD EXISTING SAGA SPECTRA TO FILE
    sagaspec = SAGACAT.load()
    if not nosaga:
        sqltable = saga_tools.add_saga_spec(sagaspec,sqltable)

    # SET REMOVE FLAGS
    rmv = REMOVELIST.load()
    sqltable = saga_tools.rm_removelist_obj(rmv, sqltable)


    # CLEAN AND DE-SHRED USING NSAID
    nsa = NSACAT.load()
    print 'Cleaning on NSA catalog'
    sqltable = saga_tools.nsa_cleanup(nsa, sqltable)


    # CLEAN AND DE_SHRED HIGHER REDSHIFT OBJECTS
    print 'Cleaning on SDSS catalog'
    sqltable = saga_tools.sdss_cleanup(sqltable)


    # REMOVE OBJECTS WITH BAD PHOTO-FLAGS, ADD OBJECTS BACK IN BY HAND
    addlst = ADDLIST.load()
    print 'Cleaning on PhotoFlags'
    sqltable = saga_tools.photoflags(addlst,sqltable)




    # FIX MAJOR PROBLEM SPECTRA BY HAND
    sqltable = fix_byhand.fix_basecats(sqltable)

    # FIX SATS ID BY HAND
    clean_sagaspec = CLEAN.load()
    sqltablle = fix_byhand.sat_hack(nsaid,sqltable,clean_sagaspec)

    print
    return sqltable
def compile_saga_spectra(flagged_obs_hosts=False):


    # RUN EITHER FULL HOST LIST OR JUST FLAG ZERO HOSTS, default to flag zero
    if flagged_obs_hosts:
        sheet = GoogleSheets('1GJYuhqfKeuJr-IyyGF_NDLb_ezL6zBiX2aeZFHHPr_s', 0)
    else:
        sheet = GoogleSheets('1b3k2eyFjHFDtmHce1xi6JKuj3ATOWYduTBFftx5oPp8', 448084634)
	hostdata     = sheet.load()

	# READ ALL NON-SDSS SPECTRA AND COMBINE INTO SINGLE TABLE
	gama_table   = read_saga_spectra.read_gama()
	mmt_table    = read_saga_spectra.read_mmt()
	aat_table    = read_saga_spectra.read_aat()
	imacs_table  = read_saga_spectra.read_imacs()
	wiyn_table   = read_saga_spectra.read_wiyn()
	deimos_table = read_saga_spectra.read_deimos()

	# LEAST TO MOST IMPORTANT
	sagaspec = table.vstack([wiyn_table,imacs_table,gama_table,deimos_table,aat_table,mmt_table],\
						     metadata_conflicts='silent')


#	zq = sagaspec['ZQUALITY'] > 2
	# need to write script to resolve repeats properly
#	sagaspec=sagaspec[zq]


 # FOR EACH FLAG ZERO HOST OR NON-FLAG ZERO OBSERVED HOST
 # READ BASE SDSS FILE
 #  KEEP ONLY SDSS SPECTRA AND MATCHES FROM OTHER SOURCES
	nhost = 0
	for host in hostdata:
	    flag  = host['flag']   # SELECT ONLY NON_FLAG0#	    
	    if flag == 0:
			nra  = host['RA']
			ndec = host['Dec']
			nsaid  = host['NSAID']# NAME OF BASE SQL FILES
			basefile  = os.path.join(SAGA_DIR, 'base_catalogs', 'base_sql_nsa{0}.fits.gz'.format(nsaid))
			basetable = Table.read(basefile)	
			print nsaid


			# CALCULATE OBJECT DISTANCE FROM HOST
			catsc = SkyCoord(u.Quantity(sagaspec['RA'], u.deg), u.Quantity(sagaspec['DEC'], u.deg))
			hostcoords = SkyCoord(nra*u.deg, ndec*u.deg)
			seps  = catsc.separation(hostcoords)
			rhost = seps.to(u.deg).value
			host_spec_objs = rhost < 1.0


		  # COMBINE SDSS SPECTRA AND SAGA SPECTRA	
			sdss_table   = read_saga_spectra.read_sdss(basetable)
			hostspec     = table.vstack([sdss_table,sagaspec[host_spec_objs]])


		   # MATCH ALL SPECTRA TO BASECATALOG, ADD SPEC DETAILS
			id2,id1,d = sm.spherematch(hostspec['RA'], hostspec['DEC'],basetable['RA'], basetable['DEC'],3./3600,nnearest=1)
			basetable['TELNAME'][id1]    = hostspec['TELNAME'][id2]
			basetable['MASKNAME'][id1]   = hostspec['MASKNAME'][id2]
			basetable['ZQUALITY'][id1]   = hostspec['ZQUALITY'][id2]
			basetable['SPEC_Z'][id1]     = hostspec['SPEC_Z'][id2]
			basetable['SPECOBJID'][id1]  = hostspec['specobjid'][id2]
			basetable['SPEC_REPEAT'][id1]  = hostspec['TELNAME'][id2]


			m = basetable['SPEC_Z'] != -1


	      # COMBINE INTO SINGLE ALLSPEC FILE
			if (nhost == 0):
				allspec = basetable[m]
			if (nhost > 0):
				allspec = table.vstack([allspec,basetable[m]])  # APPEND			
			nhost=nhost+1	



	# INITIALIZE SATS ARRAY (3=primary, 2=lowz, 1=satellite)	
	allspec = saga_tools.fill_sats_array(allspec)


	# CLEAN UP REPEAT ENTRY OF SATELLITES 
	allspec = saga_tools.repeat_sat_cleanup(allspec)



	# HACK FOR THE MOMENT
#	m1=allspec['REMOVE'] == 3 
#	m2=allspec['SATS'] == 1
#	m=m1&m2
#	allspec['REMOVE'][m] = -1    # keep remove = 3 satelites

#	m1=allspec['REMOVE'] == 2
#	m2=allspec['SATS'] == 1
#	m=m1&m2
#	allspec['SATS'][m] = -1     # remove remove=2 satellites



	# WRITE ALL SPECTRA TAKEN
	file  = os.path.join(SAGA_DIR, 'data', 'saga_spectra_dirty.fits.gz')
	write_fits(allspec, file)


	# WRITE ALL GOOD SPECTRA
	# KEEP ALL GOOD SPECTRA WHICH ARE GALAXIES
	zql     = allspec['ZQUALITY'] >= 3 
	rml     = allspec['REMOVE'] == -1 
	galonly = allspec['PHOTPTYPE'] == 3
	clean   = zql & rml & galonly
	allclean = allspec[clean]


	file  = os.path.join(SAGA_DIR, 'data', 'saga_spectra_clean.fits.gz')	
	write_fits(allclean,file)

	return allspec 
Exemple #21
0
      def __init__(self, catname,fitsname,jpegname,tracername,membername, boxname, bcgname, bcgname2, notesname, nedspec, photspec,choose_gal, readbcgs, xcs_z):

        im = imread(jpegname)
        scaled_image_data = im/255.
        self.im = scaled_image_data
        imfit = pyfits.open(fitsname)
	w = wcs.WCS(imfit[0].header)
        self.wcs = w
#        dt = [('ra',float), ('dec',float), ('mag_auto_g',float), ('magerr_auto_g',float), ('flags_g',float), ('class_star_g',float), ('spread_model_g',float), \
#                 ('mag_auto_r',float), ('magerr_auto_r',float), ('flags_r',float), ('class_star_r',float), ('spread_model_r',float),\
#                 ('mag_auto_i',float), ('magerr_auto_i',float), ('flags_i',float), ('class_star_i',float), ('spread_model_i',float),\
#                 ('mag_auto_z',float), ('magerr_auto_z',float), ('flags_z',float), ('class_star_z',float), ('spread_model_z',float)]
        dt = [('ra',float), ('dec',float), ('mag_auto_g',float), ('magerr_auto_g',float), ('mag_auto_r',float), ('magerr_auto_r',float), ('mag_auto_i',float), ('magerr_auto_i',float), \
                 ('mag_auto_z',float), ('magerr_auto_z',float), \
                 ('mag_detmodel_g',float), ('magerr_detmodel_g',float), ('mag_detmodel_r',float), ('magerr_detmodel_r',float), ('mag_detmodel_i',float), ('magerr_detmodel_i',float), \
                 ('mag_detmodel_z',float), ('magerr_detmodel_z',float)]

        cnr1 = w.wcs_pix2world(imfit[0].header['NAXIS1'],imfit[0].header['NAXIS2'],1)
        cnr2 = w.wcs_pix2world(imfit[0].header['NAXIS1'],0,1)
        cnr3 = w.wcs_pix2world(0,imfit[0].header['NAXIS2'],1)
        cnr4 =  w.wcs_pix2world(0,0,1)
        cnrra = [cnr1[0],cnr2[0],cnr3[0],cnr4[0]]
        cnrdec = [cnr1[1],cnr2[1],cnr3[1],cnr4[1]]
        maxra = max(cnrra)
        minra = min(cnrra)
        maxdec = max(cnrdec)
        mindec = min(cnrdec)
       
        # cut specz ad photoz galaxies
        try:
           if (nedspec != 0):
              wkeep = where((nedspec['ra'] >= minra) & (nedspec['ra'] <= maxra) & (nedspec['dec'] >= mindec) & (nedspec['dec'] <= maxdec))
              self.zra = nedspec[wkeep]['ra']
              self.zdec = nedspec[wkeep]['dec']
              self.zz = nedspec[wkeep]['z']
        except:
           self.zra = np.array([])
           self.zdec = np.array([])
           self.zz = np.array([])

        try:
           if (photspec != 0):
              wkeep = where((photspec.field('ra') >= minra) & (photspec.field('ra') <= maxra) & (photspec.field('dec') >= mindec) & (photspec.field('dec') <= maxdec))
              self.zra = np.append(self.zra, photspec[wkeep].field('ra'))
              self.zdec = np.append(self.zdec, photspec[wkeep].field('dec'))
              self.zz = np.append(self.zz, photspec[wkeep].field('z'))
        except:
           self.zra = np.array([])
           self.zdec = np.array([])
           self.zz = np.array([])

        # read in all galaxies
        galaxy = np.loadtxt(catname, dtype=dt, skiprows=(1), delimiter=',')
        inside = ((galaxy['ra'] < maxra) & (galaxy['ra'] > minra) & (galaxy['dec'] < maxdec) & (galaxy['dec'] > mindec))
        galaxy = galaxy[inside]
        self.gr_mag_all = galaxy['mag_auto_r']
        self.ri_mag_all = galaxy['mag_auto_r']
        self.iz_mag_all = galaxy['mag_auto_i']
        self.rz_mag_all = galaxy['mag_auto_i']
        self.gi_mag_all = galaxy['mag_auto_i']
        self.gr_color_all = galaxy['mag_detmodel_g'] - galaxy['mag_detmodel_r']
        self.gi_color_all = galaxy['mag_detmodel_g'] - galaxy['mag_detmodel_i']
        self.ri_color_all = galaxy['mag_detmodel_r'] - galaxy['mag_detmodel_i']
        self.iz_color_all = galaxy['mag_detmodel_i'] - galaxy['mag_detmodel_z']
        self.rz_color_all = galaxy['mag_detmodel_r'] - galaxy['mag_detmodel_z']
        self.ra_all =  galaxy['ra']
        self.dec_all =  galaxy['dec']

       # open tracer files. 
        dt = [('ra',float), ('dec',float), ('g_mag',float)]
        self.ra = np.array([])
        self.dec = np.array([])
        self.gr_color = np.array([])
        self.ri_color = np.array([])
        self.iz_color = np.array([])
        self.rz_color = np.array([])
        self.gi_color = np.array([])
        self.g_mag = np.array([])
        self.r_mag = np.array([])
        self.i_mag = np.array([])
        self.z_mag = np.array([])
	try:
 	  with open(membername):
                cmrgalaxy = np.loadtxt(membername, dtype=dt, skiprows=(0), delimiter=' ')
                m1 = np.zeros_like(cmrgalaxy['ra'], dtype=bool)
                m2 = np.zeros_like(cmrgalaxy['dec'], dtype=bool)
                m1[np.unique(cmrgalaxy['ra'], return_index=True)[1]] = True
                m2[np.unique(cmrgalaxy['dec'], return_index=True)[1]] = True
                keep = where((m1 == True) & (m2 == True))
                if size(keep) > 0:
                 for kk in range (0,size(keep)):
                   try:
                     result = psm.spherematch(cmrgalaxy['ra'][keep][kk],cmrgalaxy['dec'][keep][kk], self.ra_all, self.dec_all, nnearest=1)
                     if (result[0] < 0.1/3600.):
                       self.ra = np.append(self.ra,self.ra_all[result[1].astype(int)])
                       self.dec = np.append(self.dec,self.dec_all[result[1].astype(int)])
	               self.gr_color = np.append(self.gr_color,self.gr_color_all[result[1].astype(int)])
       		       self.gi_color = np.append(self.gi_color,self.gi_color_all[result[1].astype(int)])
        	       self.ri_color = np.append(self.ri_color,self.ri_color_all[result[1].astype(int)])
        	       self.iz_color = np.append(self.iz_color,self.iz_color_all[result[1].astype(int)])
        	       self.rz_color = np.append(self.rz_color,self.rz_color_all[result[1].astype(int)])
        	       self.g_mag = np.append(self.g_mag,galaxy['mag_auto_g'][result[1].astype(int)])
        	       self.r_mag = np.append(self.r_mag,galaxy['mag_auto_r'][result[1].astype(int)])
        	       self.i_mag = np.append(self.i_mag,galaxy['mag_auto_i'][result[1].astype(int)])
        	       self.z_mag = np.append(self.z_mag,galaxy['mag_auto_z'][result[1].astype(int)])
                   except:
                     pass
	except:
           pass
        dt = [('ra',float), ('dec',float), ('g_mag',float), ('r_mag',float),('i_mag',float),('z_mag',float),('gr_color',float), ('gi_color',float),('ri_color',float),('iz_color',float),('rz_color',float)]
        try:
          with open(tracername):
                cmrgalaxy = np.loadtxt(tracername, dtype=dt, skiprows=(0), delimiter=' ')
                m1 = np.zeros_like(cmrgalaxy['ra'], dtype=bool)
                m2 = np.zeros_like(cmrgalaxy['dec'], dtype=bool)
                m1[np.unique(cmrgalaxy['ra'], return_index=True)[1]] = True
                m2[np.unique(cmrgalaxy['dec'], return_index=True)[1]] = True
                keep = where((m1 == True) & (m2 == True))
                if size(keep) > 0:
                 for kk in range (0,size(keep)):
                   try:
                     result = psm.spherematch(cmrgalaxy['ra'][keep][kk],cmrgalaxy['dec'][keep][kk], self.ra_all, self.dec_all, nnearest=1)
                     if (result[0] < 0.1/3600.):
                       self.ra = np.append(self.ra,self.ra_all[result[1].astype(int)])
                       self.dec = np.append(self.dec,self.dec_all[result[1].astype(int)])
                       self.gr_color = np.append(self.gr_color,self.gr_color_all[result[1].astype(int)])
                       self.gi_color = np.append(self.gi_color,self.gi_color_all[result[1].astype(int)])
                       self.ri_color = np.append(self.ri_color,self.ri_color_all[result[1].astype(int)])
                       self.iz_color = np.append(self.iz_color,self.iz_color_all[result[1].astype(int)])
                       self.rz_color = np.append(self.rz_color,self.rz_color_all[result[1].astype(int)])
                       self.g_mag = np.append(self.g_mag,galaxy['mag_auto_g'][result[1].astype(int)])
                       self.r_mag = np.append(self.r_mag,galaxy['mag_auto_r'][result[1].astype(int)])
                       self.i_mag = np.append(self.i_mag,galaxy['mag_auto_i'][result[1].astype(int)])
                       self.z_mag = np.append(self.z_mag,galaxy['mag_auto_z'][result[1].astype(int)])
                   except:
                     pass
        except:
           pass
        if len(self.ra)>1:
           m1 = np.zeros_like(np.array(self.ra), dtype=bool)
           m2 = np.zeros_like(np.array(self.ra), dtype=bool)
           m1[np.unique(self.ra, return_index=True)[1]] = True
           m2[np.unique((np.array(self.ra))*(np.array(self.dec)), return_index=True)[1]] = True
           keep = where((m1 == True) & (m2 == True))
           self.ra=self.ra[keep]
           self.dec=self.dec[keep]
           self.gr_color=self.gr_color[keep]
           self.gi_color=self.gi_color[keep]
           self.ri_color=self.ri_color[keep]
           self.iz_color=self.iz_color[keep]
           self.rz_color=self.rz_color[keep]
           self.g_mag=self.g_mag[keep]
           self.r_mag=self.r_mag[keep]
           self.i_mag=self.i_mag[keep]
           self.z_mag=self.z_mag[keep]

        #initialize BCG list
        dt = [('ra',float), ('dec',float), ('g_mag',float), ('r_mag',float),('i_mag',float),('z_mag',float),('gr_color',float), ('gi_color',float),('ri_color',float),('iz_color',float),('rz_color',float)]
        self.bcg_xpix = np.array([])
        self.bcg_ypix = np.array([])
        self.bcg_ra  = np.array([])
        self.bcg_dec = np.array([])
        self.bcg_gr_color = np.array([])
        self.bcg_gi_color = np.array([])
        self.bcg_ri_color = np.array([])
        self.bcg_iz_color = np.array([])
        self.bcg_rz_color = np.array([])
        self.bcg_g_mag = np.array([])
        self.bcg_r_mag = np.array([])
        self.bcg_i_mag = np.array([])
        self.bcg_z_mag = np.array([])
        if readbcgs:
           try:
             with open(bcgname):
               bcggals=np.loadtxt(bcgname, dtype=dt, skiprows=(0), delimiter=' ')
               xpixtemp, ypixtemp= w.wcs_world2pix(bcggals['ra'], bcggals['dec'],1)
               self.bcg_xpix=np.append(self.bcg_xpix, xpixtemp)
               self.bcg_ypix=np.append(self.bcg_ypix, ypixtemp)
               self.bcg_ra=np.append(self.bcg_ra, bcggals['ra'])
               self.bcg_dec=np.append(self.bcg_dec, bcggals['dec'])
               self.bcg_gr_color=np.append(self.bcg_gr_color, bcggals['gr_color'])
               self.bcg_gi_color=np.append(self.bcg_gi_color, bcggals['gi_color'])
               self.bcg_ri_color=np.append(self.bcg_ri_color, bcggals['ri_color'])
               self.bcg_iz_color=np.append(self.bcg_iz_color, bcggals['iz_color'])
               self.bcg_rz_color=np.append(self.bcg_rz_color, bcggals['rz_color'])
               self.bcg_g_mag=np.append(self.bcg_g_mag, bcggals['g_mag'])
               self.bcg_r_mag=np.append(self.bcg_r_mag, bcggals['r_mag'])
               self.bcg_i_mag=np.append(self.bcg_i_mag, bcggals['i_mag'])
               self.bcg_z_mag=np.append(self.bcg_z_mag, bcggals['z_mag'])
           except:
             pass
           try:
             with open(bcgname2):
               bcggals=np.loadtxt(bcgname2, dtype=dt, skiprows=(0), delimiter=' ')
               xpixtemp, ypixtemp= w.wcs_world2pix(bcggals['ra'], bcggals['dec'],1)
               self.bcg_xpix=np.append(self.bcg_xpix, xpixtemp)
               self.bcg_ypix=np.append(self.bcg_ypix, ypixtemp)
               self.bcg_ra=np.append(self.bcg_ra, bcggals['ra'])
               self.bcg_dec=np.append(self.bcg_dec, bcggals['dec'])
               self.bcg_gr_color=np.append(self.bcg_gr_color, bcggals['gr_color'])
               self.bcg_gi_color=np.append(self.bcg_gi_color, bcggals['gi_color'])
               self.bcg_ri_color=np.append(self.bcg_ri_color, bcggals['ri_color'])
               self.bcg_iz_color=np.append(self.bcg_iz_color, bcggals['iz_color'])
               self.bcg_rz_color=np.append(self.bcg_rz_color, bcggals['rz_color'])
               self.bcg_g_mag=np.append(self.bcg_g_mag, bcggals['g_mag'])
               self.bcg_r_mag=np.append(self.bcg_r_mag, bcggals['r_mag'])
               self.bcg_i_mag=np.append(self.bcg_i_mag, bcggals['i_mag'])
               self.bcg_z_mag=np.append(self.bcg_z_mag, bcggals['z_mag'])
           except:
             pass
        if len(self.bcg_ra)>1:
           m1 = np.zeros_like(np.array(self.bcg_ra), dtype=bool)
           m2 = np.zeros_like(np.array(self.bcg_ra), dtype=bool)
           m1[np.unique(self.bcg_ra, return_index=True)[1]] = True
           m2[np.unique((np.array(self.bcg_ra))*(np.array(self.bcg_dec)), return_index=True)[1]] = True
           keep = where((m1 == True) & (m2 == True))
           self.bcg_xpix=self.bcg_xpix[keep]
           self.bcg_ypix=self.bcg_ypix[keep]
           self.bcg_ra=self.bcg_ra[keep]
           self.bcg_dec=self.bcg_dec[keep]
           self.bcg_gr_color=self.bcg_gr_color[keep]
           self.bcg_gi_color=self.bcg_gi_color[keep]
           self.bcg_ri_color=self.bcg_ri_color[keep]
           self.bcg_iz_color=self.bcg_iz_color[keep]
           self.bcg_rz_color=self.bcg_rz_color[keep]
           self.bcg_g_mag=self.bcg_g_mag[keep]
           self.bcg_r_mag=self.bcg_r_mag[keep]
           self.bcg_i_mag=self.bcg_i_mag[keep]
           self.bcg_z_mag=self.bcg_z_mag[keep]
           

        #open box files
        dt = [('xbot',float), ('xtop',float), ('ybot',float), ('ytop',float)]
        self.gr_xbot = np.array([])
        self.gr_xtop = np.array([])
        self.ri_xbot = np.array([])
        self.ri_xtop = np.array([])
        self.iz_xbot = np.array([])
        self.iz_xtop = np.array([])
        self.gi_xbot = np.array([])
        self.gi_xtop = np.array([])
        self.rz_xbot = np.array([])
        self.rz_xtop = np.array([])
        self.gr_ybot = np.array([])
        self.gr_ytop = np.array([])
        self.ri_ybot = np.array([])
        self.ri_ytop = np.array([])
        self.iz_ybot = np.array([])
        self.iz_ytop = np.array([])
        self.gi_ybot = np.array([])
        self.gi_ytop = np.array([])
        self.rz_ybot = np.array([])
        self.rz_ytop = np.array([])
        try:
          with open(boxname):
                 boxit = np.loadtxt(boxname, dtype=dt, skiprows=(0))
                 self.gr_xbot = boxit[0]['xbot']
                 self.gr_xtop = boxit[0]['xtop']
                 self.gr_ybot = boxit[0]['ybot']
                 self.gr_ytop = boxit[0]['ytop']
                 self.ri_xbot = boxit[1]['xbot']
                 self.ri_xtop = boxit[1]['xtop']
                 self.ri_ybot = boxit[1]['ybot']
                 self.ri_ytop = boxit[1]['ytop']
                 self.iz_xbot = boxit[2]['xbot']
                 self.iz_xtop = boxit[2]['xtop']
                 self.iz_ybot = boxit[2]['ybot']
                 self.iz_ytop = boxit[2]['ytop']
                 self.gi_xbot = boxit[3]['xbot']
                 self.gi_xtop = boxit[3]['xtop']
                 self.gi_ybot = boxit[3]['ybot']
                 self.gi_ytop = boxit[3]['ytop']
                 self.rz_xbot = boxit[4]['xbot']
                 self.rz_xtop = boxit[4]['xtop']
                 self.rz_ybot = boxit[4]['ybot']
                 self.rz_ytop = boxit[4]['ytop']
        except IOError:
                 self.gr_xbot = 0
                 self.gr_xtop = 0
                 self.gr_ybot = 0
                 self.gr_ytop = 0
                 self.ri_xbot = 0
                 self.ri_xtop = 0
                 self.ri_ybot = 0
                 self.ri_ytop = 0
                 self.iz_xbot = 0
                 self.iz_xtop = 0
                 self.iz_ybot = 0
                 self.iz_ytop = 0
                 self.gi_xbot = 0
                 self.gi_xtop = 0
                 self.gi_ybot = 0
                 self.gi_ytop = 0
                 self.rz_xbot = 0
                 self.rz_xtop = 0
                 self.rz_ybot = 0
                 self.rz_ytop = 0
        
        # trim member, specz and photoz galaxies
        if (len(self.ra) > 1):
            a = len(self.ra)
            xpix = np.zeros([len(self.ra)])
            ypix = np.zeros([len(self.ra)])
            for i in range(0,a):
                xpix[i],ypix[i] = w.wcs_world2pix(self.ra[i],self.dec[i],1)
        else:
            xpix = np.array([0])
            ypix = np.array([0])
        m1 = np.zeros_like(xpix, dtype=bool)
        m2 = np.zeros_like(ypix, dtype=bool)
        m1[np.unique(xpix, return_index=True)[1]] = True
        m2[np.unique(ypix, return_index=True)[1]] = True
        keep = where((m1 == True) & (m2 == True) & (xpix != 0))
        self.xpix = xpix[keep]
        self.ypix = ypix[keep]
        self.ra = self.ra[keep]
        self.dec = self.dec[keep]
        self.gr_color = self.gr_color[keep]
        self.gi_color = self.gi_color[keep]
        self.ri_color = self.ri_color[keep]
        self.iz_color = self.iz_color[keep]
        self.rz_color = self.rz_color[keep]
        self.g_mag = self.g_mag[keep]
        self.r_mag = self.r_mag[keep]
        self.i_mag = self.i_mag[keep]
        self.z_mag = self.z_mag[keep]


        self.z_gr_color = np.array([])
        self.z_gi_color = np.array([])
        self.z_ri_color = np.array([])
        self.z_iz_color = np.array([])
        self.z_rz_color = np.array([])
        self.z_gr_mag = np.array([])
        self.z_gi_mag = np.array([])
        self.z_ri_mag = np.array([])
        self.z_iz_mag = np.array([])
        self.z_rz_mag = np.array([])
        self.z_member = np.array([])
        self.z_member_z = np.array([])
        if (len(self.zra) > 1 and len(self.ra_all) > 1):
            a = len(self.zra)
            for i in range(0,a):
#                xpix,ypix = w.wcs_world2pix(self.zra[i],self.zdec[i],1)
#                world = w.wcs_pix2world(xpix,667-ypix,1)
#                result = psm.spherematch(world[0],world[1], self.ra_all, self.dec_all, nnearest=1)
                result = psm.spherematch(self.zra[i],self.zdec[i], self.ra_all, self.dec_all, nnearest=1)
                if (result[0] < 1./3600.):
                  self.z_gr_color = append(self.z_gr_color,self.gr_color_all[result[1].astype(int)])
                  self.z_gi_color = append(self.z_gi_color,self.gi_color_all[result[1].astype(int)])
                  self.z_ri_color = append(self.z_ri_color,self.ri_color_all[result[1].astype(int)])
                  self.z_iz_color = append(self.z_iz_color,self.iz_color_all[result[1].astype(int)])
                  self.z_rz_color = append(self.z_rz_color,self.rz_color_all[result[1].astype(int)])
                  self.z_gr_mag = append(self.z_gr_mag,self.gr_mag_all[result[1].astype(int)])
                  self.z_gi_mag = append(self.z_gi_mag,self.gi_mag_all[result[1].astype(int)])
                  self.z_ri_mag = append(self.z_ri_mag,self.ri_mag_all[result[1].astype(int)])
                  self.z_iz_mag = append(self.z_iz_mag,self.iz_mag_all[result[1].astype(int)])
                  self.z_rz_mag = append(self.z_rz_mag,self.rz_mag_all[result[1].astype(int)])
                  self.z_member = append(self.z_member,result[1].astype(int))
                  self.z_member_z = append(self.z_member_z, self.zz[i])


        #start making figures        
        ion()
        fig = plt.figure(num = 1, figsize=(12,12))
        fig.clf()
        ax = MultiPane_Subplot(fig, subplot_pos=(1, 1, 1), nrows_ncols = (2, 2), n_pane=4, pane_direction="row", axes_pad_inch=0.0)
        fig.add_subplot(ax)
        ax[0].imshow(scaled_image_data)
        ax[1].imshow(scaled_image_data[:,:,0], cmap=plt.cm.Reds_r)
        ax[2].imshow(scaled_image_data[:,:,1], cmap=plt.cm.Greens_r)
        ax[3].imshow(scaled_image_data[:,:,2], cmap=plt.cm.Blues_r)
        fig2 = plt.figure(num = 2, figsize=(18,9))
        fig2.clf()
        bx = MultiPane_Subplot(fig2, subplot_pos=(1, 1, 1), nrows_ncols = (2, 3), n_pane=6, pane_direction="row", axes_pad_inch=0.0)
        fig2.add_subplot(bx)
        self.temp_xpix=-100
        self.temp_ypix=-100
        self.temp_gr_color=-100
        self.temp_gi_color=-100
        self.temp_ri_color=-100
        self.temp_iz_color=-100
        self.temp_rz_color=-100
        self.temp_r_mag=-100
        self.temp_i_mag=-100
        def display_ax0():
            ax[0].cla()
            ax[0].imshow(scaled_image_data)
            for i in range(0,len(self.xpix)):
               ellipse = Ellipse(xy=(self.xpix[i], 667-self.ypix[i]), width=30, height=30, edgecolor='g', fc='None', lw=2)
               ax[0].add_patch(ellipse)
            if (len(self.zra) > 0):
               a = len(self.zra)
               for i in range(a):
                  xpix,ypix = w.wcs_world2pix(self.zra[i],self.zdec[i],1)
                  left = xpix - 10
                  bottom = 667 - (ypix + 10)
                  rectangle = Rectangle((left,bottom), 20,20,edgecolor='c', fc='None', lw=1)
                  ax[0].add_patch(rectangle)
            if (len(self.bcg_ra)>0):
               a = len(self.bcg_ra)
               for i in range(a):
                  xpix,ypix = w.wcs_world2pix(self.bcg_ra[i],self.bcg_dec[i],1)
                  left = xpix - 20
                  bottom = 667 - (ypix + 20)
                  rectangle = Rectangle((left,bottom), 40,40,edgecolor='r', fc='None', lw=1)
                  ax[0].add_patch(rectangle)
            if self.temp_gr_color !=-100:
               left = self.temp_xpix - 15
               bottom = 667 - (self.temp_ypix + 15)
               rectangle = Rectangle((left,bottom), 30, 30,edgecolor='g', fc='none', lw=1)
               ax[0].add_patch(rectangle)
            fig.suptitle('Measured Redshift = '+'{: .3f}'.format(xcs_z), fontsize=20)
            fig.canvas.draw()
        def display_bx():
            bx[0].cla()
            bx[1].cla()
            bx[3].cla()
            bx[4].cla()
            bx[5].cla()
            bx[0].text(20, 0.0, r'g-r', fontsize=15)
            bx[1].text(20, 0.0, r'g-i', fontsize=15)
            bx[3].text(20, 2, r'r-i', fontsize=15)
            bx[4].text(20, 2, r'i-z', fontsize=15)
            bx[5].text(20, 2, r'r-z', fontsize=15)
            bx[0].scatter(self.gr_mag_all, self.gr_color_all,color="black", marker='.',s=1)
            bx[1].scatter(self.gi_mag_all, self.gi_color_all,color="black", marker='.',s=1)
            bx[3].scatter(self.ri_mag_all, self.ri_color_all,color="black", marker='.',s=1)
       	    bx[4].scatter(self.iz_mag_all, self.iz_color_all,color="black", marker='.',s=1)
            bx[5].scatter(self.rz_mag_all, self.rz_color_all,color="black", marker='.',s=1)
       	    if len(self.ra) > 1:
               bx[0].scatter(self.r_mag,self.gr_color,marker='o',s=40,color="blue")
               bx[1].scatter(self.i_mag,self.gi_color,marker='o',s=40,color="green")
               bx[3].scatter(self.r_mag,self.ri_color,marker='o',s=40,color="orange")
               bx[4].scatter(self.i_mag,self.iz_color,marker='o',s=40,color="red")
               bx[5].scatter(self.i_mag,self.rz_color,marker='o',s=40,color="magenta")    
            if len(self.zra) > 1:
               bx[0].scatter(self.z_gr_mag,self.z_gr_color,color="cyan",marker='s',s=5)
               bx[1].scatter(self.z_gi_mag,self.z_gi_color,color="cyan",marker='s',s=5)
               bx[3].scatter(self.z_ri_mag,self.z_ri_color,color="cyan",marker='s',s=5)
               bx[4].scatter(self.z_iz_mag,self.z_iz_color,color="cyan",marker='s',s=5)
               bx[5].scatter(self.z_rz_mag,self.z_rz_color,color="cyan",marker='s',s=5)
            if len(self.bcg_ra>1):
               bx[0].scatter(self.bcg_r_mag,self.bcg_gr_color,color="red",marker=(5, 0),s=250, alpha=0.2)
               bx[1].scatter(self.bcg_i_mag,self.bcg_gi_color,color="red",marker=(5, 0),s=250, alpha=0.2)
               bx[3].scatter(self.bcg_r_mag,self.bcg_ri_color,color="red",marker=(5, 0),s=250, alpha=0.2)
               bx[4].scatter(self.bcg_i_mag,self.bcg_iz_color,color="red",marker=(5, 0),s=250, alpha=0.2)
               bx[5].scatter(self.bcg_i_mag,self.bcg_rz_color,color="red",marker=(5, 0),s=250, alpha=0.2)
            if self.temp_gr_color !=-100:
               bx[0].scatter(self.temp_r_mag, self.temp_gr_color, color="black", marker=(5, 2), s=250, alpha=0.4)
               bx[1].scatter(self.temp_i_mag, self.temp_gi_color, color="black", marker=(5, 2), s=250, alpha=0.4)
               bx[3].scatter(self.temp_r_mag, self.temp_ri_color, color="black", marker=(5, 2), s=250, alpha=0.4)
               bx[4].scatter(self.temp_i_mag, self.temp_iz_color, color="black", marker=(5, 2), s=250, alpha=0.4)
               bx[5].scatter(self.temp_i_mag, self.temp_rz_color, color="black", marker=(5, 2), s=250, alpha=0.4)
            if (self.gr_xbot != 0):
               rect = Rectangle( ( 0,0 ), 1, 1, alpha = 0.2, ec = "gray", fc = "CornflowerBlue", visible = True, axes=bx[0])
               rect.set_width(self.gr_xtop - self.gr_xbot)
               rect.set_height(self.gr_ytop - self.gr_ybot)
               rect.set_xy((self.gr_xbot, self.gr_ybot))
               bx[0].add_patch(rect)
            if (self.gi_xbot != 0):
               rect = Rectangle( ( 0,0 ), 1, 1, alpha = 0.2, ec = "gray", fc = "CornflowerBlue", visible = True, axes=bx[1])
               rect.set_width(self.gi_xtop - self.gi_xbot)
               rect.set_height(self.gi_ytop - self.gi_ybot)
               rect.set_xy((self.gi_xbot, self.gi_ybot))
               bx[1].add_patch(rect)
            if (self.ri_xbot != 0):
               rect = Rectangle( ( 0,0 ), 1, 1, alpha = 0.2, ec = "gray", fc = "CornflowerBlue", visible = True, axes=bx[3])
               rect.set_width(self.ri_xtop - self.ri_xbot)
               rect.set_height(self.ri_ytop - self.ri_ybot)
               rect.set_xy((self.ri_xbot, self.ri_ybot))
               bx[3].add_patch(rect)
            if (self.iz_xbot != 0):
               rect = Rectangle( ( 0,0 ), 1, 1, alpha = 0.2, ec = "gray", fc = "CornflowerBlue", visible = True, axes=bx[4])
               rect.set_width(self.iz_xtop - self.iz_xbot)
               rect.set_height(self.iz_ytop - self.iz_ybot)
               rect.set_xy((self.iz_xbot, self.iz_ybot))
               bx[4].add_patch(rect)
            if (self.rz_xbot != 0):
               rect = Rectangle( ( 0,0 ), 1, 1, alpha = 0.2, ec = "gray", fc = "CornflowerBlue", visible = True, axes=bx[5])
               rect.set_width(self.rz_xtop - self.rz_xbot)
               rect.set_height(self.rz_ytop - self.rz_ybot)
               rect.set_xy((self.rz_xbot, self.rz_ybot))
               bx[5].add_patch(rect)  
            bx.set_xlim(16, 24)
            bx.set_ylim(-0.2, 3.5)
            bx.axes_llc.set_xlabel("magnitude")
            bx.axes_llc.set_ylabel("color")
            fig2.suptitle('Measured Redshift = '+'{: .3f}'.format(xcs_z), fontsize=20)
            fig2.canvas.draw()  
        display_ax0()    
        display_bx()

        #start procedures to enable mouse actions
        def on_press(event):
            self.x0 = event.xdata
            self.y0 = event.ydata
            self.x0a = event.x
            self.y0a = event.y
        def on_release(event):
            x0=self.x0
            y0=self.y0
            x0a=self.x0a
            y0a=self.y0a
            x1 = event.xdata
            y1 = event.ydata
            x1a = event.x
            y1a = event.y
            self.x1=x1
            self.y1=y1
            self.x1a=x1a
            self.y1a=y1a
            if ( y1>y0 ):
               ytop = y1
               ybot = y0
            else:
               ybot = y1
               ytop = y0
            if ( x1>x0 ):
               xtop = x1
               xbot = x0
            else:
               xbot = x1
               xtop = x0            
            bbox = fig2.get_window_extent().transformed(fig2.dpi_scale_trans.inverted())
            fullwidth = bbox.width*fig2.dpi
            fullheight = bbox.height*fig2.dpi
            bbox = bx.get_window_extent()
            bxwidth = bbox.width
            bxheight = bbox.height
            padx = (fullwidth - bxwidth*3)/2.0
            pady = (fullheight - bxheight*2)/2.0
            panel0xmin = padx
            panel0xmax = padx + bxwidth
            panel0ymin = pady + bxheight
            panel0ymax = pady + 2*bxheight
            panel1xmin = padx + bxwidth
            panel1xmax = padx + 2*bxwidth
            panel1ymin = pady + bxheight
            panel1ymax = pady + 2*bxheight
            panel3xmin = padx 
            panel3xmax = padx + bxwidth
            panel3ymin = pady
            panel3ymax = pady + bxheight
            panel4xmin = padx + bxwidth
            panel4xmax = padx + 2*bxwidth
            panel4ymin = pady
            panel4ymax = pady + bxheight
            panel5xmin = padx + 2*bxwidth
            panel5xmax = padx + 3*bxwidth
            panel5ymin = pady
            panel5ymax = pady + bxheight
            if ((x0a < panel0xmax) & (x0a > panel0xmin) & (y0a > panel0ymin) & (y0a < panel0ymax)):
               self.gr_xbot = xbot
               self.gr_xtop = xtop
               self.gr_ybot = ybot
               self.gr_ytop = ytop
            elif ((x0a < panel3xmax) & (x0a > panel3xmin) & (y0a > panel3ymin) & (y0a < panel3ymax)):
               self.ri_xbot = xbot
               self.ri_xtop = xtop
               self.ri_ybot = ybot
               self.ri_ytop = ytop
            elif ((x0a < panel1xmax) & (x0a > panel1xmin) & (y0a > panel1ymin) & (y0a < panel1ymax)):
               self.gi_xbot = xbot
               self.gi_xtop = xtop
               self.gi_ybot = ybot
               self.gi_ytop = ytop
            elif ((x0a < panel4xmax) & (x0a > panel4xmin) & (y0a > panel4ymin) & (y0a < panel4ymax)):
               self.iz_xbot = xbot
               self.iz_xtop = xtop
               self.iz_ybot = ybot
               self.iz_ytop = ytop
            elif ((x0a < panel5xmax) & (x0a > panel5xmin) & (y0a > panel5ymin) & (y0a < panel5ymax)):
               self.rz_xbot = xbot
               self.rz_xtop = xtop
               self.rz_ybot = ybot
               self.rz_ytop = ytop
            display_bx()

        def onclick(event):
            if event.button !=3:
               coords = np.vstack((event.xdata,667-event.ydata)).T
               world = w.wcs_pix2world(coords,1)
               result = psm.spherematch(world[:,0],world[:,1], self.ra_all, self.dec_all, nnearest=1)
               if (result[0] < 1./3600.):
                  xpix,ypix = w.wcs_world2pix(self.ra_all[result[1].astype(int)], self.dec_all[result[1].astype(int)],1)
                  if (((xpix == self.xpix) & (ypix == self.ypix)).any() == False):
                     self.xpix = np.append(self.xpix,xpix)
                     self.ypix = np.append(self.ypix,ypix)
                     self.ra = np.append(self.ra, self.ra_all[result[1].astype(int)])
                     self.dec = np.append(self.dec, self.dec_all[result[1].astype(int)])
                     self.gr_color = np.append(self.gr_color, self.gr_color_all[result[1].astype(int)])
                     self.gi_color = np.append(self.gi_color, self.gi_color_all[result[1].astype(int)])
                     self.ri_color = np.append(self.ri_color, self.ri_color_all[result[1].astype(int)])
                     self.iz_color = np.append(self.iz_color, self.iz_color_all[result[1].astype(int)])
                     self.rz_color = np.append(self.rz_color, self.rz_color_all[result[1].astype(int)])
                     self.g_mag = np.append(self.g_mag, galaxy['mag_auto_g'][result[1].astype(int)])
                     self.r_mag = np.append(self.r_mag, galaxy['mag_auto_r'][result[1].astype(int)])
                     self.i_mag = np.append(self.i_mag, galaxy['mag_auto_i'][result[1].astype(int)])
                     self.z_mag = np.append(self.z_mag, galaxy['mag_auto_z'][result[1].astype(int)])
                  else:
                     keep = where(((xpix == self.xpix) & (ypix == self.ypix))==False)
                     self.xpix = self.xpix[keep]
                     self.ypix = self.ypix[keep]
                     self.ra = self.ra[keep]
                     self.dec = self.dec[keep]
              	     self.gr_color = self.gr_color[keep]
                     self.gi_color = self.gi_color[keep]
                     self.ri_color = self.ri_color[keep]
                     self.iz_color = self.iz_color[keep]
                     self.rz_color = self.rz_color[keep]
                     self.g_mag = self.g_mag[keep]
                     self.r_mag = self.r_mag[keep]
                     self.i_mag = self.i_mag[keep]
                     self.z_mag = self.z_mag[keep]
                  self.temp_xpix=xpix
                  self.temp_ypix=ypix
                  self.temp_gr_color=self.gr_color_all[result[1].astype(int)]
                  self.temp_gi_color=self.gi_color_all[result[1].astype(int)]
                  self.temp_ri_color=self.ri_color_all[result[1].astype(int)]
                  self.temp_iz_color=self.iz_color_all[result[1].astype(int)]
                  self.temp_rz_color=self.rz_color_all[result[1].astype(int)]
                  self.temp_r_mag=galaxy['mag_auto_r'][result[1].astype(int)]
                  self.temp_i_mag=galaxy['mag_auto_i'][result[1].astype(int)]
                  display_ax0()
                  display_bx()
                
        def onrightclick(event):
            if event.button==3:
               coords = np.vstack((event.xdata,667-event.ydata)).T
               world = w.wcs_pix2world(coords,1)
               result = psm.spherematch(world[:,0],world[:,1], self.ra_all, self.dec_all, nnearest=1)
               if (result[0] < 1./3600.):
                  xpix,ypix = w.wcs_world2pix(self.ra_all[result[1].astype(int)], self.dec_all[result[1].astype(int)],1)
                  if (((xpix == self.bcg_xpix) & (ypix == self.bcg_ypix)).any() == False):
                     self.bcg_xpix = np.append(self.bcg_xpix,xpix)
                     self.bcg_ypix = np.append(self.bcg_ypix,ypix)
                     self.bcg_ra =  np.append(self.bcg_ra, self.ra_all[result[1].astype(int)])
                     self.bcg_dec = np.append(self.bcg_dec, self.dec_all[result[1].astype(int)])
                     self.bcg_gr_color = np.append(self.bcg_gr_color, self.gr_color_all[result[1].astype(int)])
                     self.bcg_gi_color = np.append(self.bcg_gi_color, self.gi_color_all[result[1].astype(int)])
                     self.bcg_ri_color = np.append(self.bcg_ri_color, self.ri_color_all[result[1].astype(int)])
                     self.bcg_iz_color = np.append(self.bcg_iz_color, self.iz_color_all[result[1].astype(int)])
                     self.bcg_rz_color = np.append(self.bcg_rz_color, self.rz_color_all[result[1].astype(int)])
                     self.bcg_g_mag = np.append(self.bcg_g_mag, galaxy['mag_auto_g'][result[1].astype(int)])
                     self.bcg_r_mag = np.append(self.bcg_r_mag, galaxy['mag_auto_r'][result[1].astype(int)])
                     self.bcg_i_mag = np.append(self.bcg_i_mag, galaxy['mag_auto_i'][result[1].astype(int)])
                     self.bcg_z_mag = np.append(self.bcg_z_mag, galaxy['mag_auto_z'][result[1].astype(int)])
                  else:
                     keep = where(((xpix == self.bcg_xpix) & (ypix == self.bcg_ypix))==False)
                     self.bcg_xpix = self.bcg_xpix[keep]
                     self.bcg_ypix = self.bcg_ypix[keep]
                     self.bcg_ra = self.bcg_ra[keep]
               	     self.bcg_dec = self.bcg_dec[keep]
                     self.bcg_gr_color = self.bcg_gr_color[keep]
               	     self.bcg_gi_color = self.bcg_gi_color[keep]
               	     self.bcg_ri_color = self.bcg_ri_color[keep]
                     self.bcg_iz_color = self.bcg_iz_color[keep]
                     self.bcg_rz_color = self.bcg_rz_color[keep]
                     self.bcg_g_mag = self.bcg_g_mag[keep]
	             self.bcg_r_mag = self.bcg_r_mag[keep]
                     self.bcg_i_mag = self.bcg_i_mag[keep]
         	     self.bcg_z_mag = self.bcg_z_mag[keep]
               display_bx()
               display_ax0()

        #link mouse action procedures with figures
	cid = fig.canvas.mpl_connect('button_press_event', onclick)
        cid = fig.canvas.mpl_connect('button_press_event', onrightclick)
        cid = fig2.canvas.mpl_connect('button_press_event', on_press)
        cid = fig2.canvas.mpl_connect('button_release_event', on_release)
        
        # Print existing note if there is one.
        if os.path.isfile(notesname) and os.path.getsize(notesname) > 0:
        	notesexist = True
        	notefile = open(notesname,'a+')
        	notefile.seek(0)
        	print "\nExisting notes:\n" + notefile.read()
        else:
        	notesexist = False
        	
        #decide if figures will be shown or not
        if (choose_gal == True):
        	# Add a note
        	valid = 0
        	while (valid ==0):
        		newnote = raw_input('Add a note? (y/n) -> ')
        		if newnote == 'y' or newnote == 'Y':
        			if notesexist: #if note exists already
        				notetext = raw_input('Type your note below then press enter. Your text will be appended to the existing note: \n')
        				notefile.write(time.strftime("%x") + ': ' + notetext + '\n')
        				valid = 1
        			else:
        				notetext = raw_input('Type your note below then press enter. \n')
        				if len(notetext) > 0:
        					notefile = open(notesname, 'a+')
        					notefile.write(time.strftime("%x") + ': ' + notetext + '\n')
        				valid = 1
        		elif newnote == 'n' or newnote == 'N':
        			valid = 1
        		else:
        			print 'Invalid option.\n'
        			
        	# Flag the galaxy
        	valid = 0
        	while (valid == 0):
        		raiseflag = raw_input('Flag this cluster? (y/n) -> ')
        		if raiseflag == 'n' or raiseflag == 'N':
        			self.flagged = False
        			valid = 1
        		elif raiseflag == 'y' or raiseflag == 'Y':
        			self.flagged = True
        			valid = 1
        		else:
        			print 'Invalid option.\n'
        	raw_input('Press ENTER when you are ready to move on to the next cluster.')
        	show()

        		

           
        else:
        	plt.close(fig)
        	plt.close(fig2)
        	
        if os.path.isfile(notesname):
        	notefile.close()
Exemple #22
0
    def match_and_filter(self, exclusion=5 / 3600., tol=3 / 3600., plot=True):
        '''

        :param exclusion: if None, no prefiltering is done. If equal to a distance, ensures that the minimum distance between
        stars of the reference catalog is larger than that distance (in degrees)
        :param tol: maximum distance in degrees within a matched pair
        :return:
        '''

        if (exclusion is not None):
            # Performing pre-filtering on reference catalog star list. Make sure we keep only stars more distant than
            # exclusion value from each other
            idx1, idx2, ds = spherematch(self.ra_ref,
                                         self.dec_ref,
                                         self.ra_ref,
                                         self.dec_ref,
                                         nnearest=2)
            # Now exclude one of the stars from each pair where distance is smaller than exclusion
            ou = np.where((ds > exclusion))
            self.ra_ref = self.ra_ref[ou]
            self.dec_ref = self.dec_ref[ou]
            self.xpix_ref = self.xpix_ref[ou]
            self.ypix_ref = self.ypix_ref[ou]
            self.mag_reg = self.mag_ref[ou]
            # Do the same on the image star list
            idx1, idx2, ds = spherematch(self.ra_orig,
                                         self.dec_orig,
                                         self.ra_orig,
                                         self.dec_orig,
                                         nnearest=2)
            ou = np.where((ds > exclusion))
            self.ra_orig = self.ra_orig[ou]
            self.dec_orig = self.dec_orig[ou]
            self.xpix_orig = self.xpix_orig[ou]
            self.ypix_orig = self.ypix_orig[ou]
            self.inst_magnitudes = self.inst_magnitudes[ou]

        idx1, idx2, ds = spherematch(self.ra_orig,
                                     self.dec_orig,
                                     self.ra_ref,
                                     self.dec_ref,
                                     nnearest=1,
                                     tol=tol)
        self.ra_orig = self.ra_orig[idx1]
        self.xpix_orig = self.xpix_orig[idx1]
        self.dec_orig = self.dec_orig[idx1]
        self.ypix_orig = self.ypix_orig[idx1]
        self.inst_magnitudes = self.inst_magnitudes[idx1]

        self.ra_ref = self.ra_ref[idx2]
        self.dec_ref = self.dec_ref[idx2]
        self.xpix_ref = self.xpix_ref[idx2]
        self.ypix_ref = self.ypix_ref[idx2]
        self.mag_ref = self.mag_ref[idx2]

        self.distances = ds

        if (plot):

            plt.close()
            apertures = CircularAperture((self.xpix_orig, self.ypix_orig), r=4)
            ref_apertures = CircularAperture((self.xpix_ref, self.ypix_ref),
                                             r=4)
            norm = ImageNormalize(stretch=SqrtStretch())
            plt.imshow(self.data, cmap='Greys', origin='lower', norm=norm)
            apertures.plot(color='blue', lw=1.5, alpha=0.5)
            ref_apertures.plot(color='red', lw=1.5, alpha=0.5)
            plt.show()
Exemple #23
0
def create_base_catalog(nsaid, host):
    # READ SQL FILE
    sqlfile = 'sql_nsa{0}.fits'.format(nsaid)
    #sqlfile = os.path.join(SAGA_DIR, 'hosts', sqlfile)
    sqltable = Table.read(sqlfile)

    # GET BASIC HOST PARAMETERS FROM GOOGLE HOST LIST
    hostra   = host['RA']  #RA
    hostdec  = host['Dec']  #DEC
    hostdist = host['distance']  #DISTANCE
    hostv    = host['vhelio']  #VHELIO
    hostMK   = host['K'] #M_K
    hostflag = host['flag'] #HOST FLAG

    # SET VIRIAL RADIUS = 300 kpc, EXCLUDE 20 kpc AROUND HOST
    #rkpc = 300.
    #rvir = np.rad2deg(np.arcsin(0.3/hostdist))
    #rgal = np.rad2deg(np.arcsin(0.02/hostdist))

    cdec  = np.cos(np.deg2rad(hostdec))
    dra   = sqltable['RA'] - hostra
    ddec  = sqltable['DEC'] - hostdec
    rhost_arcm = 60.*((dra * cdec) ** 2 + ddec ** 2) ** 0.5
    rhost_kpc = 1000.*hostdist*np.sin(np.deg2rad(rhost_arcm/60.))

    # ADD EXTRA COLUMNS -
    size = len(sqltable)
    cols = [_filled_column('HOST_RA', hostra, size), #HOST
            _filled_column('HOST_DEC', hostdec, size),
            _filled_column('HOST_DIST', hostdist, size),
            _filled_column('HOST_VHOST', hostv, size),
            _filled_column('HOST_MK', hostMK, size),
            _filled_column('HOST_NSAID', nsaid, size),
            _filled_column('HOST_FLAG', hostflag, size),
            _filled_column('HOST_SAGA_NAME', ' '*48, size),
            Column(rhost_arcm, 'RHOST_ARCM'), #OBJECT
            Column(rhost_kpc, 'RHOST_KPC'),
            _filled_column('OBJ_NSAID', -1, size),
            _filled_column('SATS', -1, size),
            _filled_column('PCLASS_1', -1, size),
            _filled_column('PCLASS_1_WISE', -1, size),
            _filled_column('REMOVE', -1, size), # -1 = Good source; 1 = On remove list; 2 = Overlaps with NSA GALAXY
            _filled_column('TELNAME', ' '*4, size), #SPECTRA
            _filled_column('MASKNAME', ' '*48, size),
            _filled_column('ZQUALITY', -1, size),
            _filled_column('SPEC_REPEAT', ' '*48, size)]

    sqltable.add_columns(cols)
    del cols

    # ADD IN BEN'S PREDICTIONS
    #id1, id2, d = sm.spherematch(sqltable['RA'], sqltable['DEC'], ML['RA'], ML['DEC'], 1./3600)
    #sqltable['PCLASS_1'][id1] = ML['PROBABILITY_CLASS_1'][id2]
    #sqltable['PCLASS_1_WISE'][id1] = ML['PROBABILITY_CLASS_1_WISE'][id2]

    # REPLACE WISE NUMBERS
    wise = 0
    if wise:
        wbasefile = os.path.join(SAGA_DIR, 'hosts', 'wise', 'base_sql_nsa{0}_nw1.fits',format(nsaid))
        wbasetable = FitsTable(wbasefile).load()
        id1, id2, d = sm.spherematch(sqltable['RA'], sqltable['DEC'], wbasetable['RA'], wbasetable['DEC'], 1./3600)
        print 'Read WISE catalog: ', wbasefile
        print id1.size, sqltable['ra'].size

        #TODO: probably need to add columns to sqltable
        sqltable['w1'][id1]    = wbasetable['W1'][id2]
        sqltable['w1err'][id1] = wbasetable['W1ERR'][id2]
        sqltable['w2'][id1]    = wbasetable['W2'][id2]
        sqltable['w2err'][id1] = wbasetable['W2ERR'][id2]
        sqltable['w1'][np.isnan(sqltable['w1'])] = 9999
        sqltable['w1err'][np.isnan(sqltable['w1err'])] = 9999

    # INITALIZE SDSS SPECTRAL ENTRIE
    msk = (sqltable['SPEC_Z'] != -1) & (sqltable['SPEC_Z_WARN'] == 0)
    sqltable['TELNAME'][msk] = 'SDSS'
    sqltable['MASKNAME'][msk] = 'SDSS'
    sqltable['SPEC_REPEAT'][msk] = 'SDSS'
    sqltable['ZQUALITY'][msk] = 4

    # IF THIS IS A SAGA HOST, SET SAGA NAME
    #sqltable['HOST_SAGA_NAME'] = saga.saga_name(nsaid)

    # SET REMOVE FLAGS
    #sqltable = saga.rm_removelist_obj(remove_list, sqltable)

    # CLEAN USING NSAID
    #sqltable = saga.nsa_cleanup(nsa_catalog, sqltable)

    return sqltable