Пример #1
0
def getxyfromradec(im, ra, dec):
    iraf.imgets(image=im, param='CRVAL1')  #get RA of image in deg
    t = iraf.imgets.value
    RAcenter = float(t)
    iraf.imgets(image=im, param='CRVAL2')  #get RA of image
    t = iraf.imgets.value
    DECcenter = float(t)
    dra = (RAcenter - ra) * N.cos(
        N.pi / 180. * dec)  #correct delta ra for cos declination
    ddec = (dec - DECcenter)
    #ra=ra*3600.#convert to arcsec
    #dec=dec*3600.

    #dra=N.array(dra,'f')
    #ddec=N.array(ddec,'f')
    iraf.imgets(image=im, param='CRPIX1')  #get x value corresponding to RA
    t = float(iraf.imgets.value)
    xcenter = t
    iraf.imgets(image=im, param='CRPIX2')  #get y value corresponding to dec
    t = float(iraf.imgets.value)
    ycenter = t
    iraf.imgets(image=im, param='CD1_1')  #get x value corresponding to RA
    xplate = abs(float(iraf.imgets.value))  #deg/pixel
    iraf.imgets(image=im, param='CD2_2')  #get x value corresponding to RA
    yplate = abs(float(iraf.imgets.value))  #deg/pixel
    #convert offsets to pixels on pisces at 90"
    x = dra / xplate + xcenter
    y = ddec / yplate + ycenter
    return x, y
Пример #2
0
    def xy2rd(self, pos):
        """
        This method would apply the WCS keywords to a position to
        generate a new sky position.

        The algorithm comes directly from 'imgtools.xy2rd'

        translate (x,y) to (ra, dec)
        """
        if self.ctype1.find('TAN') < 0 or self.ctype2.find('TAN') < 0:
            print 'XY2RD only supported for TAN projections.'
            raise TypeError

        if isinstance(pos, N.NumArray):
            # If we are working with an array of positions,
            # point to just X and Y values
            posx = pos[:, 0]
            posy = pos[:, 1]
        else:
            # Otherwise, we are working with a single X,Y tuple
            posx = pos[0]
            posy = pos[1]

        xi = self.cd11 * (posx - self.crpix1) + self.cd12 * (posy -
                                                             self.crpix2)
        eta = self.cd21 * (posx - self.crpix1) + self.cd22 * (posy -
                                                              self.crpix2)

        xi = DEGTORAD(xi)
        eta = DEGTORAD(eta)
        ra0 = DEGTORAD(self.crval1)
        dec0 = DEGTORAD(self.crval2)

        ra = N.arctan((xi / (N.cos(dec0) - eta * N.sin(dec0)))) + ra0
        dec = N.arctan(
            ((eta * N.cos(dec0) + N.sin(dec0)) /
             (N.sqrt((N.cos(dec0) - eta * N.sin(dec0))**2 + xi**2))))

        ra = RADTODEG(ra)
        dec = RADTODEG(dec)
        ra = DIVMOD(ra, 360.)

        # Otherwise, just return the RA,Dec tuple.
        return ra, dec
Пример #3
0
def xy2rd(filename, x,y,hdu = -1, hms = 0,  verbose = 0):
    from numarray import sin,cos,arctan2,sqrt
    def degtorad(num):
	return num/180.0*numarray.pi
    def radtodeg(num):
	return num/numarray.pi * 180.0
    def degtoHMS(num):
	mmm = int((num-int(num))*60.0)
	sss = ((num-int(num))*60.0-mmm)*60.0
	num = `int(num)`+":"+ `mmm`+":"+`sss`
	return num
	
    keys = ["CRPIX1","CRPIX2","CRVAL1","CRVAL2","CD1_1","CD1_2","CD2_1","CD2_2"]
    CD =  read_keys(filename,keys,hdu)
    crpix = numarray.zeros((2),numarray.Float)
    cd = numarray.zeros((2,2),numarray.Float)

    crpix[0] = CD['CRPIX1'][0]
    crpix[1] = CD['CRPIX2'][0]
    ra0 = CD['CRVAL1'][0]
    dec0 = CD['CRVAL2'][0]
    cd[0,0] = CD['CD1_1'][0]
    cd[0,1] = CD['CD1_2'][0]
    cd[1,0] = CD['CD2_1'][0]
    cd[1,1] = CD['CD2_2'][0]
    xi = cd[0, 0] * (x - crpix[0]) + cd[0, 1] * (y - crpix[1])
    eta = cd[1, 0] * (x - crpix[0]) + cd[1, 1] * (y - crpix[1])
    xi = degtorad(xi)
    eta = degtorad(eta)
    ra0 = degtorad(ra0)
    dec0 = degtorad(dec0)

    ra = arctan2(xi,cos(dec0)-eta*sin(dec0)) + ra0
    dec = arctan2(eta*cos(dec0)+sin(dec0),sqrt((cos(dec0)-eta*sin(dec0))**2 + xi**2))

    ra = radtodeg(ra)# % 360.0
  #  if ra < 0: ra = ra + 360.0
    dec = radtodeg(dec)

    if (hms):
	return degtoHMS(ra/15.0),degtoHMS(dec)
    else:
	return ra,dec
Пример #4
0
    def xy2rd(self,pos):
        """
        This method would apply the WCS keywords to a position to
        generate a new sky position.

        The algorithm comes directly from 'imgtools.xy2rd'

        translate (x,y) to (ra, dec)
        """
        if self.ctype1.find('TAN') < 0 or self.ctype2.find('TAN') < 0:
            print 'XY2RD only supported for TAN projections.'
            raise TypeError

        if isinstance(pos,N.NumArray):
            # If we are working with an array of positions,
            # point to just X and Y values
            posx = pos[:,0]
            posy = pos[:,1]
        else:
            # Otherwise, we are working with a single X,Y tuple
            posx = pos[0]
            posy = pos[1]

        xi = self.cd11 * (posx - self.crpix1) + self.cd12 * (posy - self.crpix2)
        eta = self.cd21 * (posx - self.crpix1) + self.cd22 * (posy - self.crpix2)

        xi = DEGTORAD(xi)
        eta = DEGTORAD(eta)
        ra0 = DEGTORAD(self.crval1)
        dec0 = DEGTORAD(self.crval2)

        ra = N.arctan((xi / (N.cos(dec0)-eta*N.sin(dec0)))) + ra0
        dec = N.arctan( ((eta*N.cos(dec0)+N.sin(dec0)) /
                (N.sqrt((N.cos(dec0)-eta*N.sin(dec0))**2 + xi**2))) )

        ra = RADTODEG(ra)
        dec = RADTODEG(dec)
        ra = DIVMOD(ra, 360.)

        # Otherwise, just return the RA,Dec tuple.
        return ra,dec
Пример #5
0
    def psf_select(alpha_j, delta_j):					
        distance = 9999.0
        psffile = 'test.fits'
        psflist = c.psflist
        r = 3.14159265 / 180.0
        for element in psflist:
            p=pyfits.open(element)
            header = p[0].header
            if (header.has_key('RA_TARG')):
                ra = header['RA_TARG']
            if (header.has_key('DEC_TARG')):
                dec= header['DEC_TARG']
            p.close()
#		d = sqrt((ra - alpha_j) ** 2.0 + (dec - delta_j) ** 2.0)
            d = n.arccos(n.cos((90.0 - delta_j) * r) * n.cos((90.0 - dec) *\
                r) + n.sin((90.0 - delta_j) * r) *  n.sin((90.0 - dec) * r) * \
                n.cos((alpha_j - ra) * r))
            if(d < distance):
                psffile = element
                distance = d
        return psffile, distance
Пример #6
0
    def rd2xy(self, skypos, hour=no):
        """
        This method would use the WCS keywords to compute the XY position
        from a given RA/Dec tuple (in deg).

        NOTE: Investigate how to let this function accept arrays as well
        as single positions. WJH 27Mar03

        """
        if self.ctype1.find('TAN') < 0 or self.ctype2.find('TAN') < 0:
            print 'RD2XY only supported for TAN projections.'
            raise TypeError

        det = self.cd11 * self.cd22 - self.cd12 * self.cd21

        if det == 0.0:
            raise ArithmeticError, "singular CD matrix!"

        cdinv11 = self.cd22 / det
        cdinv12 = -self.cd12 / det
        cdinv21 = -self.cd21 / det
        cdinv22 = self.cd11 / det

        # translate (ra, dec) to (x, y)

        ra0 = DEGTORAD(self.crval1)
        dec0 = DEGTORAD(self.crval2)
        if hour:
            skypos[0] = skypos[0] * 15.
        ra = DEGTORAD(skypos[0])
        dec = DEGTORAD(skypos[1])

        bottom = float(
            N.sin(dec) * N.sin(dec0) +
            N.cos(dec) * N.cos(dec0) * N.cos(ra - ra0))
        if bottom == 0.0:
            raise ArithmeticError, "Unreasonable RA/Dec range!"

        xi = RADTODEG((N.cos(dec) * N.sin(ra - ra0) / bottom))
        eta = RADTODEG((N.sin(dec) * N.cos(dec0) -
                        N.cos(dec) * N.sin(dec0) * N.cos(ra - ra0)) / bottom)

        x = cdinv11 * xi + cdinv12 * eta + self.crpix1
        y = cdinv21 * xi + cdinv22 * eta + self.crpix2

        return x, y
Пример #7
0
def autoalign(images):
    ra = []
    dec = []
    xcenter = []
    ycenter = []
    dx = []
    dy = []
    xpscale = []
    ypscale = []
    for im in images:
        iraf.imgets(image=im, param='CRVAL1')  #get RA of image in deg
        t = iraf.imgets.value
        ra.append(float(t))
        iraf.imgets(image=im, param='CRPIX1')  #get pix of RA
        t = iraf.imgets.value
        xcenter.append(float(t))
        iraf.imgets(image=im, param='CD1_1')  #get pix of RA
        t = iraf.imgets.value
        xpscale.append(float(t))
        iraf.imgets(image=im, param='CRVAL2')  #get RA of image
        t = iraf.imgets.value
        dec.append(float(t))
        iraf.imgets(image=im, param='CRPIX2')  #get RA of image
        t = iraf.imgets.value
        ycenter.append(float(t))
        iraf.imgets(image=im, param='CD2_2')  #get RA of image
        t = iraf.imgets.value
        ypscale.append(float(t))

    dra = (ra - ra[0]) * N.cos(
        N.pi / 180. * dec)  #correct delta ra for cos declination
    ddec = (dec - dec[0])
    dx = dra / xpscale + (xcenter - xcenter[0])
    dy = dra / ypscale + (ycenter - ycenter[0])
    xshift = dx
    yshift = -1. * dy
    outfile = open('shifts', 'w')
    for i in range(len(xshift)):
        s = '%8.3f %8.3f \n' % (xshift[i], yshift[i])
        outfile.write(s)
    outfile.close()
Пример #8
0
    def rd2xy(self,skypos,hour=no):
        """
        This method would use the WCS keywords to compute the XY position
        from a given RA/Dec tuple (in deg).

        NOTE: Investigate how to let this function accept arrays as well
        as single positions. WJH 27Mar03

        """
        if self.ctype1.find('TAN') < 0 or self.ctype2.find('TAN') < 0:
            print 'RD2XY only supported for TAN projections.'
            raise TypeError

        det = self.cd11*self.cd22 - self.cd12*self.cd21

        if det == 0.0:
            raise ArithmeticError,"singular CD matrix!"

        cdinv11 = self.cd22 / det
        cdinv12 = -self.cd12 / det
        cdinv21 = -self.cd21 / det
        cdinv22 = self.cd11 / det

        # translate (ra, dec) to (x, y)

        ra0 = DEGTORAD(self.crval1)
        dec0 = DEGTORAD(self.crval2)
        if hour:
            skypos[0] = skypos[0] * 15.
        ra = DEGTORAD(skypos[0])
        dec = DEGTORAD(skypos[1])

        bottom = float(N.sin(dec)*N.sin(dec0) + N.cos(dec)*N.cos(dec0)*N.cos(ra-ra0))
        if bottom == 0.0:
            raise ArithmeticError,"Unreasonable RA/Dec range!"

        xi = RADTODEG((N.cos(dec) * N.sin(ra-ra0) / bottom))
        eta = RADTODEG((N.sin(dec)*N.cos(dec0) - N.cos(dec)*N.sin(dec0)*N.cos(ra-ra0)) / bottom)

        x = cdinv11 * xi + cdinv12 * eta + self.crpix1
        y = cdinv21 * xi + cdinv22 * eta + self.crpix2

        return x,y
Пример #9
0
def rd2xy(filename,ra,dec,verbose=0,hdu=-1,hour=0):
    from numarray import sin,cos,arctan2,sqrt
    def degtorad(num):
	return num/180.0*numarray.pi
    def radtodeg(num):
	return num/numarray.pi * 180.0
    keys = ["CRPIX1","CRPIX2","CRVAL1","CRVAL2","CD1_1","CD1_2","CD2_1","CD2_2"]
    CD =  read_keys(filename,keys,hdu)
    crpix = numarray.zeros((2),numarray.Float)
    cd = numarray.zeros((2,2),numarray.Float)
    cdinv = numarray.zeros((2,2),numarray.Float)

    crpix[0] = CD['CRPIX1'][0]
    crpix[1] = CD['CRPIX2'][0]
    ra0 = CD['CRVAL1'][0]
    dec0 = CD['CRVAL2'][0]
    cd[0,0] = CD['CD1_1'][0]
    cd[0,1] = CD['CD1_2'][0]
    cd[1,0] = CD['CD2_1'][0]
    cd[1,1] = CD['CD2_2'][0]
    det = cd[0,0]*cd[1,1] - cd[0,1]*cd[1,0]
    if det == 0:
	raise SingularMatrix
    cdinv[0,0] = cd[1,1] / det
    cdinv[0,1] = -cd[0,1] / det
    cdinv[1,0] = -cd[1,0] / det
    cdinv[1,1] = cd[0,0] / det
    print det,cdinv
    ra0 = degtorad(ra0)
    dec0 = degtorad(dec0)
    if hour: ra=ra*15.0
    ra = degtorad(ra)
    dec = degtorad(dec)
    bottom = sin(dec)*sin(dec0) + cos(dec)*cos(dec0)*cos(ra-ra0)
    if bottom == 0:
	raise InvalidRaDecRange
    xi = cos(dec) * sin(ra-ra0) / bottom
    eta = (sin(dec)*cos(dec0) - cos(dec)*sin(dec0)*cos(ra-ra0)) / bottom
    xi = radtodeg(xi)
    eta = radtodeg(eta)
    x = cdinv[0, 0] * xi + cdinv[0, 1] * eta + crpix[0]
    y = cdinv[1, 0] * xi + cdinv[1, 1] * eta + crpix[1]
    return x,y
Пример #10
0
def mask(clus_id, line_s):
    imagefile = c.imagefile
    sex_cata = c.sex_cata
    clus_cata = c.out_cata
    threshold = c.threshold
    thresh_area = c.thresh_area
    size = c.size
    mask_reg = c.mask_reg
    x = n.reshape(n.arange(size*size),(size,size)) % size
    x = x.astype(n.Float32)
    y = n.reshape(n.arange(size*size),(size,size)) / size
    y = y.astype(n.Float32)
    values = line_s.split()
    mask_file = 'ell_mask_' + str(imagefile)[:6] + '_'  + str(clus_id) + '.fits'
    xcntr_o  = float(values[1]) #x center of the object
    ycntr_o  = float(values[2]) #y center of the object
    xcntr = size / 2.0 + 1.0 + xcntr_o - int(xcntr_o)
    ycntr = size / 2.0 + 1.0 + ycntr_o - int(ycntr_o)
    mag    = float(values[7]) #Magnitude
    radius = float(values[9]) #Half light radius
    mag_zero = c.mag_zero #magnitude zero point
    sky	 = float(values[10]) #sky 
    pos_ang = float(values[11]) - 90.0 #position angle
    axis_rat = 1.0 / float(values[12]) #axis ration b/a
    major_axis = float(values[14])	#major axis of the object
    z = n.zeros((size,size))		
    for line_j in open(sex_cata,'r'):
        try:
            values = line_j.split()
            xcntr_n  = float(values[1]) #x center of the neighbour
            ycntr_n  = float(values[2]) #y center of the neighbour
            mag    = float(values[7]) #Magnitude
            radius = float(values[9]) #Half light radius
            sky      = float(values[10]) #sky
            pos_ang = float(values[11]) #position angle
            axis_rat = 1.0/float(values[12]) #axis ration b/a
            si = n.sin(pos_ang * n.pi / 180.0)
            co = n.cos(pos_ang * n.pi / 180.0)
            area = float(values[13])
            maj_axis = float(values[14])#major axis of neighbour
            eg = 1.0 - axis_rat
            one_minus_eg_sq    = (1.0-eg)**2.0
            if(abs(xcntr_n - xcntr_o) < size/2.0 and \
               abs(ycntr_n - ycntr_o) < size/2.0 and \
               xcntr_n != xcntr_o and ycntr_n != ycntr_o):
                if((xcntr_o - xcntr_n) < 0):
                    xn = xcntr + abs(xcntr_n - xcntr_o)
                if((ycntr_o - ycntr_n) < 0):
                    yn = ycntr + abs(ycntr_n - ycntr_o)
                if((xcntr_o - xcntr_n) > 0):
                    xn = xcntr - (xcntr_o -xcntr_n)
                if((ycntr_o - ycntr_n) > 0):
                    yn = ycntr - (ycntr_o -ycntr_n)
                tx = (x - xn + 0.5) * co + (y - yn + 0.5) * si
                ty = (xn - 0.5 -x) * si + (y - yn + 0.5) * co
                R = n.sqrt(tx**2.0 + ty**2.0 / one_minus_eg_sq)
                z[n.where(R<=mask_reg*maj_axis)] = 1
        except:
            i=1	
    hdu = pyfits.PrimaryHDU(z.astype(n.Float32))
    hdu.writeto(mask_file)
Пример #11
0
def HookModel( elevs , c , fsin , fcos ):

    return c+ fsin  * numarray.sin(elevs * math.pi / 180 ) + fcos  * numarray.cos(elevs * math.pi / 180 )
Пример #12
0
    def recenter(self):
        """
        Reset the reference position values to correspond to the center
        of the reference frame.
        Algorithm used here developed by Colin Cox - 27-Jan-2004.
        """
        if self.ctype1.find('TAN') < 0 or self.ctype2.find('TAN') < 0:
            print 'WCS.recenter() only supported for TAN projections.'
            raise TypeError

        # Check to see if WCS is already centered...
        if self.crpix1 == self.naxis1 / 2. and self.crpix2 == self.naxis2 / 2.:
            # No recentering necessary... return without changing WCS.
            return

        # This offset aligns the WCS to the center of the pixel, in accordance
        # with the 'align=center' option used by 'drizzle'.
        #_drz_off = -0.5
        _drz_off = 0.
        _cen = (self.naxis1 / 2. + _drz_off, self.naxis2 / 2. + _drz_off)

        # Compute the RA and Dec for center pixel
        _cenrd = self.xy2rd(_cen)
        _cd = N.array([[self.cd11, self.cd12], [self.cd21, self.cd22]],
                      type=N.Float64)
        _ra0 = DEGTORAD(self.crval1)
        _dec0 = DEGTORAD(self.crval2)
        _ra = DEGTORAD(_cenrd[0])
        _dec = DEGTORAD(_cenrd[1])

        # Set up some terms for use in the final result
        _dx = self.naxis1 / 2. - self.crpix1
        _dy = self.naxis2 / 2. - self.crpix2

        _dE, _dN = DEGTORAD(N.dot(_cd, (_dx, _dy)))
        _dE_dN = 1 + N.power(_dE, 2) + N.power(_dN, 2)
        _cosdec = N.cos(_dec)
        _sindec = N.sin(_dec)
        _cosdec0 = N.cos(_dec0)
        _sindec0 = N.sin(_dec0)

        _n1 = N.power(_cosdec, 2) + _dE * _dE + _dN * _dN * N.power(_sindec, 2)
        _dra_dE = (_cosdec0 - _dN * _sindec0) / _n1
        _dra_dN = _dE * _sindec0 / _n1

        _ddec_dE = -_dE * N.tan(_dec) / _dE_dN
        _ddec_dN = (1 / _cosdec) * ((_cosdec0 / N.sqrt(_dE_dN)) -
                                    (_dN * N.sin(_dec) / _dE_dN))

        # Compute new CD matrix values now...
        _cd11n = _cosdec * (self.cd11 * _dra_dE + self.cd21 * _dra_dN)
        _cd12n = _cosdec * (self.cd12 * _dra_dE + self.cd22 * _dra_dN)
        _cd21n = self.cd11 * _ddec_dE + self.cd21 * _ddec_dN
        _cd22n = self.cd12 * _ddec_dE + self.cd22 * _ddec_dN

        _new_orient = RADTODEG(N.arctan2(_cd12n, _cd22n))

        # Update the values now...
        self.crpix1 = _cen[0]
        self.crpix2 = _cen[1]
        self.crval1 = RADTODEG(_ra)
        self.crval2 = RADTODEG(_dec)

        # Keep the same plate scale, only change the orientation
        self.rotateCD(_new_orient)

        # These would update the CD matrix with the new rotation
        # ALONG with the new plate scale which we do not want.
        self.cd11 = _cd11n
        self.cd12 = _cd12n
        self.cd21 = _cd21n
        self.cd22 = _cd22n
Пример #13
0
    def recenter(self):
        """
        Reset the reference position values to correspond to the center
        of the reference frame.
        Algorithm used here developed by Colin Cox - 27-Jan-2004.
        """
        if self.ctype1.find('TAN') < 0 or self.ctype2.find('TAN') < 0:
            print 'WCS.recenter() only supported for TAN projections.'
            raise TypeError

        # Check to see if WCS is already centered...
        if self.crpix1 == self.naxis1/2. and self.crpix2 == self.naxis2/2.:
            # No recentering necessary... return without changing WCS.
            return

        # This offset aligns the WCS to the center of the pixel, in accordance
        # with the 'align=center' option used by 'drizzle'.
        #_drz_off = -0.5
        _drz_off = 0.
        _cen = (self.naxis1/2.+ _drz_off,self.naxis2/2. + _drz_off)

        # Compute the RA and Dec for center pixel
        _cenrd = self.xy2rd(_cen)
        _cd = N.array([[self.cd11,self.cd12],[self.cd21,self.cd22]],type=N.Float64)
        _ra0 = DEGTORAD(self.crval1)
        _dec0 = DEGTORAD(self.crval2)
        _ra = DEGTORAD(_cenrd[0])
        _dec = DEGTORAD(_cenrd[1])

        # Set up some terms for use in the final result
        _dx = self.naxis1/2. - self.crpix1
        _dy = self.naxis2/2. - self.crpix2

        _dE,_dN = DEGTORAD(N.dot(_cd,(_dx,_dy)))
        _dE_dN = 1 + N.power(_dE,2) + N.power(_dN,2)
        _cosdec = N.cos(_dec)
        _sindec = N.sin(_dec)
        _cosdec0 = N.cos(_dec0)
        _sindec0 = N.sin(_dec0)

        _n1 = N.power(_cosdec,2) + _dE*_dE + _dN*_dN*N.power(_sindec,2)
        _dra_dE = (_cosdec0 - _dN*_sindec0)/_n1
        _dra_dN = _dE*_sindec0 /_n1

        _ddec_dE = -_dE*N.tan(_dec) / _dE_dN
        _ddec_dN = (1/_cosdec) * ((_cosdec0 / N.sqrt(_dE_dN)) - (_dN*N.sin(_dec) / _dE_dN))

        # Compute new CD matrix values now...
        _cd11n = _cosdec * (self.cd11*_dra_dE + self.cd21 * _dra_dN)
        _cd12n = _cosdec * (self.cd12*_dra_dE + self.cd22 * _dra_dN)
        _cd21n = self.cd11 * _ddec_dE + self.cd21 * _ddec_dN
        _cd22n = self.cd12 * _ddec_dE + self.cd22 * _ddec_dN

        _new_orient = RADTODEG(N.arctan2(_cd12n,_cd22n))

        # Update the values now...
        self.crpix1 = _cen[0]
        self.crpix2 = _cen[1]
        self.crval1 = RADTODEG(_ra)
        self.crval2 = RADTODEG(_dec)

        # Keep the same plate scale, only change the orientation
        self.rotateCD(_new_orient)

        # These would update the CD matrix with the new rotation
        # ALONG with the new plate scale which we do not want.
        self.cd11 = _cd11n
        self.cd12 = _cd12n
        self.cd21 = _cd21n
        self.cd22 = _cd22n
Пример #14
0
def gotoit():
    nbin = 10
    #c=Cluster()
    #g=Galaxy()
    clusterfile = "clusters.spec.dat"
    print "reading in cluster file to get cluster parameters"
    c.creadfiles(clusterfile)
    print "got ", len(c.z), " clusters"
    c.convarray()
    c.Kcorr()

    go2 = []  #combined arrays containing all galaxies
    gsf = []  #combined arrays containing all galaxies
    gsig5 = []
    gsig10 = []
    gsig52r200 = []  #spec catalogs extended out to 2xR200
    gsig102r200 = []  #spec catalogs extended out to 2xR200
    gsig5phot = []
    gsig10phot = []
    sgo2 = []  #combined arrays containing all galaxies
    sgha = []  #combined arrays containing all galaxies
    sgsf = []  #combined arrays containing all galaxies
    sgsig5 = []
    sgsig10 = []
    sgsig52r200 = []  #spec catalogs extended out to 2xR200
    sgsig102r200 = []  #spec catalogs extended out to 2xR200
    sgsig5phot = []
    sgsig10phot = []

    if (mode < 1):
        c.getsdssphotcats()
        c.getsdssspeccats()

    gr = []  #list of median g-r colors
    psplotinit('summary.ps')
    x1 = .1
    x2 = .45
    x3 = .6
    x4 = .95
    y1 = .15
    y2 = .45
    y3 = .55
    y4 = .85
    ppgplot.pgsch(1.2)  #font size
    ppgplot.pgslw(2)
    #for i in range(len(c.z)):
    cl = [10]
    (xl, xu, yl, yu) = ppgplot.pgqvp(0)
    print "viewport = ", xl, xu, yl, yu
    complall = []
    for i in range(len(c.z)):
        #for i in cl:
        gname = "g" + str(i)
        gname = Galaxy()
        gspecfile = "abell" + str(c.id[i]) + ".spec.dat"
        gname.greadfiles(gspecfile, i)
        print "number of members = ", len(gname.z)
        if len(gname.z) < 10:
            print "less than 10 members", len(gname.z)
            continue
        gname.convarray()
        #gname.cullmembers()
        #gname.getmemb()#get members w/in R200
        #gr.append(N.average(gname.g-gname.r))

        gspec2r200file = "abell" + str(c.id[i]) + ".spec2r200.dat"
        gname.greadspecfiles(gspec2r200file, c.dL[i], c.kcorr[i], i)
        print i, c.id[i], " getnearest, first call", len(gname.ra), len(
            gname.sra), sum(gname.smemb)
        #gname.getnearest(i)
        (gname.sig52r200, gname.sig102r200) = gname.getnearestgen(
            gname.ra, gname.dec, gname.sra, gname.sdec, i
        )  #measure distances from ra1, dec1 to members in catalog ra2, dec2
        sig52r200 = N.compress(gname.memb > 0, gname.sig52r200)
        gsig52r200[len(gsig5phot):] = sig52r200
        sig102r200 = N.compress(gname.memb > 0, gname.sig102r200)
        gsig102r200[len(gsig10phot):] = sig102r200

        gphotfile = "abell" + str(c.id[i]) + ".phot.dat"
        gname.greadphotfiles(gphotfile, c.dL[i], c.kcorr[i])
        gname.getnearest(i)
        #print "len of local density arrays = ",len(gname.sig5),len(gname.sig5phot)
        #print gspecfile, c.z[i],c.kcorr[i]
        (ds5, ds10) = gname.gwritefiles(gspecfile, i)
        o2 = N.compress(gname.memb > 0, gname.o2)
        go2[len(go2):] = o2
        sf = N.compress(gname.memb > 0, gname.sf)
        gsf[len(gsf):] = sf
        sig5 = N.compress(gname.memb > 0, gname.sig5)
        gsig5[len(gsig5):] = sig5
        sig10 = N.compress(gname.memb > 0, gname.sig10)
        gsig10[len(gsig10):] = sig10
        sig5phot = N.compress(gname.memb > 0, gname.sig5phot)
        gsig5phot[len(gsig5phot):] = sig5phot
        sig10phot = N.compress(gname.memb > 0, gname.sig10phot)
        gsig10phot[len(gsig10phot):] = sig10phot

        ds5 = N.array(ds5, 'f')
        ds10 = N.array(ds10, 'f')
        #print len(ds5),len(ds10)
        #ppgplot.pgsvp(xl,xu,yl,yu)
        ppgplot.pgsvp(0.1, .9, .08, .92)
        ppgplot.pgslw(7)
        label = 'Abell ' + str(
            c.id[i]) + ' (z=%5.2f, \gs=%3.0f km/s)' % (c.z[i], c.sigma[i])
        ppgplot.pgtext(0., 1., label)
        ppgplot.pgslw(2)
        ppgplot.pgsvp(x1, x2, y1, y2)  #sets viewport
        #ppgplot.pgbox("",0.0,0,"",0.0)
        ppgplot.pgswin(-1., 3., -1., 3.)  #axes limits
        ppgplot.pgbox('bcnst', 1, 2, 'bcvnst', 1, 2)  #tickmarks and labeling
        ppgplot.pgmtxt('b', 2.5, 0.5, 0.5,
                       "\gS\d10\u(phot) (gal/Mpc\u2\d)")  #xlabel
        ppgplot.pgmtxt('l', 2.6, 0.5, 0.5, "\gS\d10\u(spec) (gal/Mpc\u2\d)")

        x = N.arange(-5., 10., .1)
        y = x
        ppgplot.pgsls(1)  #dotted
        ppgplot.pgslw(4)  #line width
        ppgplot.pgline(x, y)
        x = N.log10(sig10phot)
        y = N.log10(sig10)
        ppgplot.pgsch(.7)
        ppgplot.pgpt(x, y, 17)
        xp = N.array([-0.5], 'f')
        yp = N.array([2.5], 'f')
        ppgplot.pgpt(xp, yp, 17)
        ppgplot.pgtext((xp + .1), yp, 'spec(1.2xR200) vs phot')
        ppgplot.pgsci(4)
        xp = N.array([-0.5], 'f')
        yp = N.array([2.2], 'f')
        ppgplot.pgpt(xp, yp, 21)
        ppgplot.pgtext((xp + .1), yp, 'spec(2xR200) vs phot')

        y = N.log10(sig102r200)

        ppgplot.pgsch(.9)
        ppgplot.pgpt(x, y, 21)
        ppgplot.pgsch(1.2)
        ppgplot.pgslw(2)  #line width
        ppgplot.pgsci(1)

        #ppgplot.pgenv(-200.,200.,-1.,20.,0,0)
        #ppgplot.pgsci(2)
        #ppgplot.pghist(len(ds5),ds5,-200.,200.,30,1)
        #ppgplot.pgsci(4)
        #ppgplot.pghist(len(ds10),ds10,-200.,200.,30,1)
        #ppgplot.pgsci(1)
        #ppgplot.pglab("\gD\gS","Ngal",gspecfile)
        #ppgplot.pgpanl(1,2)
        g = N.compress(gname.memb > 0, gname.g)
        r = N.compress(gname.memb > 0, gname.r)
        V = N.compress(gname.memb > 0, gname.V)
        dmag = N.compress(gname.memb > 0, gname.dmagnearest)
        dnearest = N.compress(gname.memb > 0, gname.nearest)
        dz = N.compress(gname.memb > 0, gname.dz)
        #ppgplot.pgsvp(x3,x4,y1,y2)  #sets viewport
        #ppgplot.pgenv(-.5,3.,-1.,5.,0,0)
        #ppgplot.pgpt((g-V),(g-r),17)
        #ppgplot.pgsci(1)
        #ppgplot.pglab("g - M\dV\u",'g-r',gspecfile)
        ppgplot.pgsvp(x1, x2, y3, y4)  #sets viewport
        #ppgplot.pgbox("",0.0,0,"",0.0)
        ppgplot.pgswin(
            (c.ra[i] + 2. * c.r200deg[i] / N.cos(c.dec[i] * N.pi / 180.)),
            (c.ra[i] - 2 * c.r200deg[i] / N.cos(c.dec[i] * N.pi / 180.)),
            (c.dec[i] - 2. * c.r200deg[i]), (c.dec[i] + 2. * c.r200deg[i]))
        ppgplot.pgbox('bcnst', 0.0, 0.0, 'bcvnst', 0.0,
                      0.0)  #tickmarks and labeling
        ppgplot.pgmtxt('b', 2.5, 0.5, 0.5, "RA")  #xlabel
        ppgplot.pgmtxt('l', 2.6, 0.5, 0.5, "Dec")

        #ppgplot.pglab("RA",'Dec',gspecfile)
        ppgplot.pgsfs(2)
        ppgplot.pgcirc(c.ra[i], c.dec[i], c.r200deg[i])
        ppgplot.pgsls(4)
        ppgplot.pgcirc(c.ra[i], c.dec[i], 1.2 * c.r200deg[i])
        ppgplot.pgsls(1)
        #ppgplot.pgcirc(c.ra[i],c.dec[i],c.r200deg[i]/N.cos(c.dec[i]*N.pi/180.))
        ppgplot.pgsci(2)
        ppgplot.pgpt(gname.ra, gname.dec, 17)
        ppgplot.pgsci(4)
        ppgplot.pgpt(gname.photra, gname.photdec, 21)
        ppgplot.pgsci(1)

        #calculate completeness w/in R200

        dspec = N.sqrt((gname.ra - c.ra[i])**2 + (gname.dec - c.dec[i])**2)
        dphot = N.sqrt((gname.photra - c.ra[i])**2 +
                       (gname.photdec - c.dec[i])**2)
        nphot = 1. * len(N.compress(dphot < c.r200deg[i], dphot))
        nspec = 1. * len(N.compress(dspec < c.r200deg[i], dspec))
        s = "Completeness for cluster Abell %s = %6.2f (nspec=%6.1f,nphot= %6.1f)" % (
            str(c.id[i]), float(nspec / nphot), nspec, nphot)
        print s
        complall.append(float(nspec / nphot))
        ppgplot.pgsvp(x3, x4, y3, y4)  #sets viewport
        #ppgplot.pgsvp(x1,x2,y3,y4)  #sets viewport
        #ppgplot.pgbox("",0.0,0,"",0.0)
        ppgplot.pgswin(-0.005, .05, -1., 1.)
        ppgplot.pgbox('bcnst', .02, 2, 'bcvnst', 1, 4)  #tickmarks and labeling
        ppgplot.pgsch(1.0)
        ppgplot.pgmtxt('b', 2.5, 0.5, 0.5,
                       "Dist to nearest phot neighbor (deg)")  #xlabel
        ppgplot.pgsch(1.2)
        ppgplot.pgmtxt('l', 2.6, 0.5, 0.5, 'M\dV\u(phot) - M\dV\u(spec)')
        ppgplot.pgsci(2)
        ppgplot.pgpt(dnearest, dmag, 17)
        ppgplot.pgsci(1)
        x = N.arange(-30., 30., 1.)
        y = 0 * x
        ppgplot.pgsci(1)
        ppgplot.pgsls(2)
        ppgplot.pgline(x, y)
        ppgplot.pgsls(1)
        ppgplot.pgsci(1)
        dm = N.compress(dnearest < 0.01, dmag)
        std = '%5.3f (%5.3f)' % (pylab.mean(dm), pylab.std(dm))
        #ppgplot.pgslw(7)
        #label='Abell '+str(c.id[i])
        #ppgplot.pgtext(0.,1.,label)
        ppgplot.pgslw(2)
        label = '\gDM\dV\u(err) = ' + std
        ppgplot.pgsch(.9)
        ppgplot.pgtext(0., .8, label)
        #label = "z = %5.2f"%(c.z[i])
        #ppgplot.pgtext(0.,.8,label)
        ppgplot.pgsch(1.2)
        #ppgplot.pgsvp(x3,x4,y3,y4)  #sets viewport
        #ppgplot.pgenv(-.15,.15,-3.,3.,0,0)
        #ppgplot.pgsci(2)
        #ppgplot.pgpt(dz,dmag,17)
        #ppgplot.pgsci(1)
        #ppgplot.pglab("z-z\dcl\u",'\gD Mag',gspecfile)
        ppgplot.pgsvp(x3, x4, y1, y2)  #sets viewport
        ppgplot.pgswin(-3., 3., -1., 1.)
        ppgplot.pgbox('bcnst', 1, 2, 'bcvnst', 1, 4)  #tickmarks and labeling
        ppgplot.pgmtxt('b', 2.5, 0.5, 0.5, "\gDv/\gs")  #xlabel
        ppgplot.pgmtxt('l', 2.6, 0.5, 0.5, 'M\dV\u(phot) - M\dV\u(spec)')

        ppgplot.pgsci(2)
        dv = dz / (1 + c.z[i]) * 3.e5 / c.sigma[i]
        ppgplot.pgpt(dv, dmag, 17)
        ppgplot.pgsci(1)
        x = N.arange(-30., 30., 1.)
        y = 0 * x
        ppgplot.pgsci(1)
        ppgplot.pgsls(2)
        ppgplot.pgline(x, y)
        ppgplot.pgsls(1)
        ppgplot.pgsci(1)
        #ppgplot.pgsvp(x1,x2,y1,y2)  #sets viewport
        #ppgplot.pgenv(0.,3.5,-3.,3.,0,0)
        #ppgplot.pgsci(4)
        #ppgplot.pgpt((g-r),dmag,17)
        #ppgplot.pgsci(1)
        #ppgplot.pglab("g-r",'\gD Mag',gspecfile)

        #ppgplot.pgsvp(x1,x2,y1,y2)  #sets viewport
        #ppgplot.pgenv(-25.,-18.,-1.,1.,0,0)
        #ppgplot.pgsci(4)
        #ppgplot.pgpt((V),dmag,17)
        #x=N.arange(-30.,30.,1.)
        #y=0*x
        #ppgplot.pgsci(1)
        #ppgplot.pgsls(2)
        #ppgplot.pgline(x,y)
        #ppgplot.pgsls(1)
        #ppgplot.pgsci(1)
        #ppgplot.pglab("M\dV\u(spec)",'M\dV\u(phot) - M\dV\u(spec)',gspecfile)
        #ppgplot.pgpage()
        #ppgplot.pgpage()
        #combine galaxy data
        ppgplot.pgpage()

        (sssig5,
         sssig10) = gname.getnearestgen(gname.sra, gname.sdec, gname.sra,
                                        gname.sdec,
                                        i)  #get spec-spec local density
        (spsig5,
         spsig10) = gname.getnearestgen(gname.sra, gname.sdec, gname.photra,
                                        gname.photdec,
                                        i)  #get spec-phot local density

        o2 = N.compress(gname.smemb > 0, gname.so2)
        sgo2[len(sgo2):] = o2
        ha = N.compress(gname.smemb > 0, gname.sha)
        sgha[len(sgha):] = ha
        sf = N.compress(gname.smemb > 0, gname.ssf)
        sgsf[len(sgsf):] = sf
        sig5 = N.compress(gname.smemb > 0, sssig5)
        sgsig5[len(sgsig5):] = sig5
        sig10 = N.compress(gname.smemb > 0, sssig10)
        sgsig10[len(sgsig10):] = sig10
        sig5phot = N.compress(gname.smemb > 0, spsig5)
        sgsig5phot[len(sgsig5phot):] = sig5phot
        sig10phot = N.compress(gname.smemb > 0, spsig10)
        sgsig10phot[len(sgsig10phot):] = sig10phot

    #gr=N.array(gr,'f')
    #c.assigncolor(gr)

    #for i in range(len(c.z)):
    #    print c.id[i],c.z[i],c.r200[i],c.r200deg[i]

    print "Average Completeness w/in R200 = ", N.average(N.array(
        complall, 'f'))
    print "sig o2", len(gsig10), len(gsig10phot), len(go2)
    print "sig o2 large", len(sgsig10), len(sgsig10phot), len(sgo2)
    plotsigo2all(gsig10, gsig10phot, go2, 'o2vsig10spec', nbin)
    #plotsigo2(gsig5phot,-1*go2,'o2vsig5phot',nbin)
    plotsigsff(gsig5, gsf, 'sffvsig5spec', nbin)  #sf frac versus sigma
    plotsigsff(gsig5phot, gsf, 'sffvsig5phot', nbin)  #sf frac versus sigma
    plotsigsffall(gsig5, gsig5phot, gsf, 'sffvsig5all',
                  nbin)  #sf frac versus sigma
    plotsig10sffall(gsig10, gsig10phot, gsf, 'sffvsig10all',
                    nbin)  #sf frac versus sigma
    #plotsighaall(gsig10,gsig10phot,gha,'havsig10spec',20)
    #plotsigo2all(sgsig10,sgsig10phot,sgo2,'o2vsig10spec.large',30)
    plotsighaall(sgsig10, sgsig10phot, sgha, 'havsig10spec.large', 10)
    #plotsigsffall(sgsig5,sgsig5phot,sgsf,'sffvsig5.large',nbin)#sf frac versus sigma
    #plotsig10sffall(sgsig10,sgsig10phot,sgsf,'sffvsig10.large',nbin)#sf frac versus sigma
    psplotinit('one2one.ps')
    ppgplot.pgenv(-1.5, 2.5, -1.5, 2.5, 0)
    ppgplot.pglab("\gS\d10\u(phot) (gal/Mpc\u2\d)",
                  "\gS\d10\u(spec) (gal/Mpc\u2\d)", "")
    x = N.arange(-5., 10., .1)
    y = x
    ppgplot.pgsls(1)  #dotted
    ppgplot.pgslw(4)  #line width
    ppgplot.pgline(x, y)
    x = N.log10(gsig10phot)
    y = N.log10(gsig10)
    ppgplot.pgsch(.7)
    ppgplot.pgpt(x, y, 17)
    ppgplot.pgsch(1.)
    ppgplot.pgsci(1)
    ppgplot.pgend()