Beispiel #1
0
def inverse_flag_images(image):
    """
    It creates a RMS-like image using its Weight-map
    where it is defined as RMS = 1 / sqrt(WeightMap)
    ----
import alhambra_invflagimas as AIV
image = '/Volumes/amb22/imagenes/f04/f04p01_F814W_1.swp.weight.flag.fits'
AIV.inverse_flag_images(image)

    """

    print 'Processing image: ', image
    wim = image
    data = P.open(wim)[0].data
    nc = len(data[0, :])
    nf = len(data[:, 0])
    matrix = U.zeros((nf, nc), float)

    for ii in range(nc):
        for jj in range(nf):
            if data[jj, ii] < 1:
                matrix[jj, ii] = 1
            else:
                matrix[jj, ii] = 0

    nameout = A.decapfile(wim) + '.inv.fits'
    print 'Saving new image as...', nameout
    P.writeto(nameout, matrix)

    try:
        A.addheader2another(image, nameout)
    except:
        print 'Impossible to update its header!!!'
def alhambramosaico(listofimages, xx, yy, size, ID_number):
    """
import clash_tools
from clash_tools import *
lista = '/Volumes/CLASH/psfmodels/May2012/final/listilla.txt'
size=25
coox = 13
cooy = 13
ids=777
clashmosaico(lista,coox,cooy,size,ids)

   """
    # It creates the list of images to be used (from "listing.py").
    image_list = U.get_str(listofimages, 0)
    path = A.getpath(image_list[0])
    # Paths & outputs
    imageout = A.decapfile(
        listofimages) + '.mosaic.X%iY%iID%i.fits' % (xx, yy, ID_number)
    print 'Imgaeout: ', imageout

    # STARTING WITH THE PROCESS...
    # Creating "albumdata" where the whole information will be saved (MOSAIC!).
    # ===================================================================================
    n = 27  # Number of images to be used.
    nx = 6  # Number of objects along the x-axis.
    ny = 5  # Number of objects along the Y-axis.
    mad = size  # Size of every sub-squared-stamp. 100 is a good choice!
    mad2 = mad + 1

    albumdata = N.zeros((ny * mad2 + 1, nx * mad2 + 1), float)
    print ' --------------------------------------------------------------------------- '
    print ' A stamp size = %d x %d has been chosen ' % (mad, mad)
    print ' One galaxy will be display in a %d x %d album-like image' % (nx,
                                                                         ny)
    print ' --------------------------------------------------------------------------- '

    for i in range(len(image_list)):
        # print 'i', i
        ix = i % nx
        iy = ny - (i / nx) - 1
        image_in = image_list[i]

        # It picks the ith-submatrix from the ith image.
        # So, It creates every single sub-mosaic!
        stamp = sta.stamping(image_in, xx, yy, mad)
        ax = ix * mad2 + 1
        ay = iy * mad2 + 1

        # Saving the ith-submosaic in albumdata.
        albumdata[ay:ay + mad, ax:ax + mad] = stamp.astype(float)
        print ' Copying submosaic %i from image %i: ' % (i, i)

    # Creating the new mosaic as a fits file.
    fits.writeto(imageout, albumdata)
def flagging_dobledetections(cat1,cat2):
    """
    This serves to append an extra column (each to both inputted catalogs)
    indicating either a detection was repeated and with the lowest S/N
    of the two.
    Sources flagged as 1 are those detections to be excluded when combining
    both catalogs into a single one.
--------
import alhambra_overlap as alhov
cat1 = '/Volumes/amb22/catalogos/reduction_v4e/f02/f02p02_colorproext_1_ISO.cat'
cat2 = '/Volumes/amb22/catalogos/reduction_v4e/f02/f02p01_colorproext_1_ISO.cat'
alhov.flagging_dobledetections(cat1,cat2)    
    
    """
    
    id1,ra1,dec1,x1,y1,s2n1 = U.get_data(cat1,(0,1,2,3,4,14))
    id2,ra2,dec2,x2,y2,s2n2 = U.get_data(cat2,(0,1,2,3,4,14))
    ne1 = len(id1)
    ne2 = len(id2)
    g1 = U.greater_equal(ra1,min(ra2))
    g2 = U.less_equal(ra2,max(ra1))
    id1r,ra1r,dec1r,x1r,y1r,s2n1r = U.multicompress(g1,(id1,ra1,dec1,x1,y1,s2n1))
    id2r,ra2r,dec2r,x2r,y2r,s2n2r = U.multicompress(g2,(id2,ra2,dec2,x2,y2,s2n2))
    flag1 = U.zeros(ne1)
    flag2 = U.zeros(ne2)
    
    dim1 = len(id1r)
    dim2 = len(id2r)
    print 'dim1,dim2',dim1,dim2
    if dim1>0 and dim2>0:
       print 'Matching samples....'
       pepe = matching_vects_ddet(id1r,ra1r,dec1r,id2r,ra2r,dec2r,0.000312)   # We use now X,Y instead RA,Dec
       # Purging null elements
       matchidcol = pepe[:,0].astype(int)
       good_det1 = U.greater(matchidcol,0)  # Excluding 0's (non matched detections)
       matchidcol = U.compress(good_det1,(matchidcol))
       matchidsp = pepe[:,1].astype(int)
       good_det2 = U.greater(matchidsp,0) # Excluding 0's (non matched detections)
       matchidsp = U.compress(good_det2,(matchidsp))
       if len(matchidcol) == len(matchidsp) and len(matchidcol) >0 :
           newdim = len(matchidsp)
           print 'Dimension of matching',newdim
           idr1  = U.zeros(newdim)
           idr2  = U.zeros(newdim)
           s2nr1 = U.zeros(newdim)
           s2nr2 = U.zeros(newdim)
           for ii in range(newdim):
               idr1index = ap.id2pos(id1r,matchidcol[ii]) 
               idr2index = ap.id2pos(id2r,matchidsp[ii]) 
               idr1[ii]  = id1r[idr1index]
               s2nr1[ii] = s2n1r[idr1index]               
               idr2[ii]  = id2r[idr2index] 
               s2nr2[ii] = s2n2r[idr2index]
               
           # Select/Purge detections according to its S/N
           marcador1 = U.zeros(newdim)
           marcador2 = U.zeros(newdim)
           for ss in range(newdim):
               cociente = s2nr1[ss]/s2nr2[ss]  
               if cociente >= 1.: marcador1[ss] = 1.
               else: marcador2[ss] = 1.     
                   
           cond1 = U.less(marcador1,1)
           cond2 = U.less(marcador2,1)
           idr1b = U.compress(cond1,idr1)
           dim1rr = len(idr1b)
           idr2b = U.compress(cond2,idr2)
           dim2rr = len(idr2b)
           
           # Two new IDs (finalid1 & finalid2) are generated with 
           # the final elements to be included in the output catalog.
           for hh1 in range(ne1):
               if id1[hh1] in idr1b:
                  flag1[hh1] = 1
                  
           for hh2 in range(ne2):
               if id2[hh2] in idr2b:
                  flag2[hh2] = 1

           # A new smaller catalog will be created containing specz info as an extra column.
           outcat1 = ap.decapfile(cat1)+'.doubledetect.cat'
           outcat2 = ap.decapfile(cat2)+'.doubledetect.cat'
           print 'outcat1',outcat1
           print 'outcat2',outcat2
           ap.appendcol(cat1,flag1,'Flag2Detected',outcat1)
           ap.appendcol(cat2,flag2,'Flag2Detected',outcat2)

           # Renaming files
           ap.renamefile(cat1,cat1+'.old.cat')
           if not os.path.exists(cat1): ap.renamefile(outcat1,cat1)
           ap.renamefile(cat2,cat2+'.old.cat')
           if not os.path.exists(cat2): ap.renamefile(outcat2,cat2)           
           
    else:
       print 'No common sources in betwen the catalogs'
       # A new smaller catalog will be created containing specz info as an extra column.
       outcat1 = ap.decapfile(cat1)+'.doubledetect.cat'
       outcat2 = ap.decapfile(cat2)+'.doubledetect.cat'
       print 'outcat1',outcat1
       print 'outcat2',outcat2
       ap.appendcol(cat1,flag1*0,'Flag2Detected',outcat1)
       ap.appendcol(cat2,flag2*0,'Flag2Detected',outcat2)
       
       # Renaming files
       ap.renamefile(cat1,cat1+'.old.cat')
       if not os.path.exists(cat1): ap.renamefile(outcat1,cat1)
       ap.renamefile(cat2,cat2+'.old.cat')
       if not os.path.exists(cat2): ap.renamefile(outcat2,cat2)   
def purging_dobledetections(cat1,cat2):
    """

import alhambra_overlap
from alhambra_overlap import *
cat1 = '/Volumes/amb22/catalogos/reduction_v4e/f02/f02p02_colorproext_1_ISO.cat'
cat2 = '/Volumes/amb22/catalogos/reduction_v4e/f02/f02p01_colorproext_1_ISO.cat'
purging_dobledetections(cat1,cat2)    
    
    """
    
    id1,ra1,dec1,x1,y1,s2n1 = U.get_data(cat1,(0,1,2,3,4,14))
    id2,ra2,dec2,x2,y2,s2n2 = U.get_data(cat2,(0,1,2,3,4,14))
    ne1 = len(id1)
    ne2 = len(id2)
    g1 = U.greater_equal(ra1,min(ra2))
    g2 = U.less_equal(ra2,max(ra1))
    id1r,ra1r,dec1r,x1r,y1r,s2n1r = U.multicompress(g1,(id1,ra1,dec1,x1,y1,s2n1))
    id2r,ra2r,dec2r,x2r,y2r,s2n2r = U.multicompress(g2,(id2,ra2,dec2,x2,y2,s2n2))

    dim1 = len(id1r)
    dim2 = len(id2r)
    print 'dim1,dim2',dim1,dim2
    if dim1>0 and dim2>0:
       print 'Matching samples....'
       pepe = matching_vects_ddet(id1r,ra1r,dec1r,id2r,ra2r,dec2r,0.000312)   # We use now X,Y instead RA,Dec
       # Purging null elements
       matchidcol = pepe[:,0].astype(int)
       good_det1 = U.greater(matchidcol,0)  # Excluding 0's (non matched detections)
       matchidcol = U.compress(good_det1,(matchidcol))
       matchidsp = pepe[:,1].astype(int)
       good_det2 = U.greater(matchidsp,0) # Excluding 0's (non matched detections)
       matchidsp = U.compress(good_det2,(matchidsp))
       if len(matchidcol) == len(matchidsp) and len(matchidcol) >0 :
           newdim = len(matchidsp)
           print 'Dimension of matching',newdim
           idr1  = U.zeros(newdim)
           idr2  = U.zeros(newdim)
           s2nr1 = U.zeros(newdim)
           s2nr2 = U.zeros(newdim)
           for ii in range(newdim):
               idr1index = ap.id2pos(id1r,matchidcol[ii]) 
               idr2index = ap.id2pos(id2r,matchidsp[ii]) 
               idr1[ii]  = id1r[idr1index]
               s2nr1[ii] = s2n1r[idr1index]               
               idr2[ii]  = id2r[idr2index] 
               s2nr2[ii] = s2n2r[idr2index]
               
           # Select/Purge detections according to its S/N
           marcador1 = U.zeros(newdim)
           marcador2 = U.zeros(newdim)
           for ss in range(newdim):
               cociente = s2nr1[ss]/s2nr2[ss]  
               if cociente >= 1.: marcador1[ss] = 1.
               else: marcador2[ss] = 1.     
                   
           cond1 = U.less(marcador1,1)
           cond2 = U.less(marcador2,1)
           idr1b = U.compress(cond1,idr1)
           dim1rr = len(idr1b)
           idr2b = U.compress(cond2,idr2)
           dim2rr = len(idr2b)
           print ''
           print 'Number of detections to be removed from cat1: ', dim1rr
           print 'Number of detections to be removed from cat2: ', dim2rr
           print ''
           
           # Two new IDs (finalid1 & finalid2) are generated with 
           # the final elements to be included in the output catalog.
           finalid1 = U.zeros((ne1-dim1rr))
           finalid2 = U.zeros((ne2-dim2rr))
           kk1 = 0
           for hh1 in range(ne1):
               if id1[hh1] not in idr1b:
                  finalid1[kk1] = id1[hh1]
                  kk1 += 1
                  
           print 'kk1',kk1
           
           kk2 = 0       
           for hh2 in range(ne2):
               if id2[hh2] not in idr2b:
                  if kk2 <= (ne2-dim2rr-1): 
                     finalid2[kk2] = id2[hh2]
                     kk2+=1
                  
           print 'kk2',kk2       
                  
           # A new smaller catalog will be created containing specz info as an extra column.
           outcat1 = ap.decapfile(cat1)+'.wo2detect.cat'
           outcat2 = ap.decapfile(cat2)+'.wo2detect.cat'
           print 'outcat1',outcat1
           print 'outcat2',outcat2
           ap.select_rows_bylist(cat1,finalid1,outcat1)
           ap.select_rows_bylist(cat2,finalid2,outcat2)
           
           
    else:
       print 'No common sources in betwen the catalogs'
Beispiel #5
0
def alhambra_multiple_mosaic(listaids):
    """


----------
import os,sys
import useful as U
import alhambra_photools as alh
import alhambra_mosaics as alhmos
idlist = '/Users/albertomolino/doctorado/photo/programas/mosaico.list'
alhmos.alhambra_multiple_mosaic(idlist)

    """
    size = 20  # 100 # cell's size for mosaic image
    resol = 1
    single = 1
    wavel = 0

    ids0 = U.get_str(listaids, 0)
    ids = ids0[::resol]
    print 'Starting with the sample selection...'
    dim = len(ids)
    print 'dimension: ', dim

    file1name = alh.decapfile(listaids) + 'Bcolor.list'
    file2name = alh.decapfile(listaids) + 'Rcolor.list'
    file3name = alh.decapfile(listaids) + 'zcolor.list'
    if not os.path.exists(file1name) and not os.path.exists(
            file2name) and not os.path.exists(file3name):
        file1 = open(file1name, 'w')
        file2 = open(file2name, 'w')
        file3 = open(file3name, 'w')

        ff = 0
        pp = 0
        cc = 0
        for ii in range(dim):

            idi = ids[ii]
            field = int(idi[3:4])
            pointing = int(idi[4:5])
            ccd = int(idi[5:6])
            yo = int(idi[6:]) * 1
            # print 'Looking for ID: %s'%ids[ii]
            image1 = root_images + 'f0%i/f0%ip0%i_B_Subaru_%i.swp.fits' % (
                field, field, pointing, ccd)
            image2 = root_images + 'f0%i/f0%ip0%i_R_Subaru_%i.swp.fits' % (
                field, field, pointing, ccd)
            image3 = root_images + 'f0%i/f0%ip0%i_F814W_%i.swp.fits' % (
                field, field, pointing, ccd)
            # print field,ff
            # print pointing,pp
            # print ccd,cc

            if field != ff and pointing != pp and ccd != cc:
                # print 'reading new catalog'
                # catalog = '/Users/albertomolino/doctorado/photo/catalogos/reduction_v4f/alhambra.F0%iP0%iC0%i.ColorProBPZ.cat'%(field,field,pointing,ccd)
                catalog = root_catalogs + 'f0%i/alhambra.F0%iP0%iC0%i.ColorProBPZ.cat' % (
                    field, field, pointing, ccd)
                # print catalog
                idd = U.get_str(catalog, 0)
                ne = len(idd)
                x, y, mo = U.get_data(catalog, (6, 7, 65))
                # Sorting detections by magnitude.
                idd, x, y = U.multisort(mo, (idd, x, y))

            pos = N.where(idd == idi)[0][0]
            # for ss in range(ne):
            #     # print idd[ss]
            #     # if idd[ss]==idi:
            #     if idi[0] in idd:
            #        pos = ss

            if pos != -1:
                xx = int(round(x[pos]))
                yy = int(round(y[pos]))
                line1 = '%s  %i  %i  %i  %i  %i\n' % (image1, xx, yy, field,
                                                      pointing, ccd)
                # print line1
                file1.write(line1)
                line2 = '%s  %i  %i  %i  %i  %i\n' % (image2, xx, yy, field,
                                                      pointing, ccd)
                # print line2
                file2.write(line2)
                line3 = '%s  %i  %i  %i  %i  %i\n' % (image3, xx, yy, field,
                                                      pointing, ccd)
                # print line3
                file3.write(line3)
            else:
                print 'ID not found...'

            ff == field
            pp == pointing
            cc == ccd

        file1.close()
        file2.close()
        file3.close()

    print 'Sample reading completed'
    print 'Starting with mosaic...'

    nt = dim
    valor = U.sqrt(dim)
    nx = int(round(valor))
    ny = int(U.ceil(valor))

    imageout1 = alh.decapfile(file1name) + '.mosaic.fits'
    if not os.path.exists(imageout1):
        file1name = alh.decapfile(listaids) + 'Bcolor.list'
        ims1 = U.get_str(file1name, 0)
        x1, y1, field1, pointing1, ccd1 = U.get_data(file1name,
                                                     (1, 2, 3, 4, 5))
        if single:
            alhmos.singlemosaico(ims1, x1, y1, size, nt, nx, ny, imageout1)
        if wavel:
            alhmos.singlemosaico_pro(ims1, x1, y1, field1, pointing1, ccd1,
                                     size, nt, 24, nt, imageout1)

    imageout2 = alh.decapfile(file2name) + '.mosaic.fits'
    if not os.path.exists(imageout2):
        file2name = alh.decapfile(listaids) + 'Rcolor.list'
        ims2 = U.get_str(file2name, 0)
        x2, y2, field2, pointing2, ccd2 = U.get_data(file2name,
                                                     (1, 2, 3, 4, 5))
        if single:
            alhmos.singlemosaico(ims2, x2, y2, size, nt, nx, ny, imageout2)
        if wavel:
            alhmos.singlemosaico_pro(ims2, x2, y2, field2, pointing2, ccd2,
                                     size, nt, 24, nt, imageout2)

    imageout3 = alh.decapfile(file3name) + '.mosaic.fits'
    if not os.path.exists(imageout3):
        file3name = alh.decapfile(listaids) + 'zcolor.list'
        ims3 = U.get_str(file3name, 0)
        x3, y3, field3, pointing3, ccd3 = U.get_data(file3name,
                                                     (1, 2, 3, 4, 5))
        if single:
            alhmos.singlemosaico(ims3, x3, y3, size, nt, nx, ny, imageout3)
        if wavel:
            alhmos.singlemosaico_pro(ims3, x3, y3, field3, pointing3, ccd3,
                                     size, nt, 24, nt, imageout3)

    if os.path.exists(imageout1) and os.path.exists(
            imageout2) and os.path.exists(imageout3):
        alhmos.trilogy_alhambra_multiple_mosaic(imageout1, imageout2,
                                                imageout3)