Beispiel #1
0
def run_ds9(filename_A, filename_B, radius=1):
    """
    Input:
     - filename_A : DS9 region file
     - filename_B : DS9 region file
     
    Output:
     - tbhdu
    
    ---
    """
    
    # Open two tables, for now, ds9 region files:
    #
    DA_in = ascii_data.read_ds9cat(filename_A);
    DB_in = ascii_data.read_ds9cat(filename_B);
    
    # Translate them to table HDU (FITS tables Header D Unit)
    tbhdu_check = fits_data.dict_to_tbHDU(DA_in,tbname=filename_A);
    tbhdu_truth = fits_data.dict_to_tbHDU(DB_in,tbname=filename_B);
    
    return run(tbhdu_check,tbhdu_truth,radius);
Beispiel #2
0
def run_ds9(filename_A, filename_B, radius=1):
    """
    Input:
     - filename_A : DS9 region file
     - filename_B : DS9 region file
     
    Output:
     - tbhdu
    
    ---
    """
    
    # Open two tables, for now, ds9 region files:
    #
    DA_in = ascii_data.read_ds9cat(filename_A);
    DB_in = ascii_data.read_ds9cat(filename_B);
    
    # Translate them to table HDU (FITS tables Header D Unit)
    tbhdu_check = fits_data.dict_to_tbHDU(DA_in,tbname=filename_A);
    tbhdu_truth = fits_data.dict_to_tbHDU(DB_in,tbname=filename_B);
    
    return run(tbhdu_check,tbhdu_truth,radius);
Beispiel #3
0
def process_output(imgfile, catfile, clean=False):

    from scipy import ndimage
    import numpy as np

    # ----------
    # Catalog
    catalog_file = catfile
    os.system("sed -e 's/^[ ]*//' %s | tr -s ' ' > %s" %
              ('data/' + catfile, catalog_file))
    columns = [
        'X', 'Y', 'ecc', 'lambda1', 'lambda2', 'A', 'B', 'theta', 'size'
    ]
    Dcatin = ascii_data.dict_from_csv(catalog_file,
                                      columns,
                                      header_lines=10,
                                      delimiter=' ')

    # Lenzen outputs the 'Y' positions inverted. Lets fix that:
    hdr = pyfits.getheader('data/' + imgfile)
    y_size = hdr['naxis1']
    x_size = hdr['naxis2']
    Y_inv = Dcatin['Y']

    finalimg = re.sub("_out_img.fits", "_out_result.fits", imgfile)
    if len(Y_inv):
        Dcatin['Y'] = [str(y_size - int(i)) for i in Y_inv]
        Catin = fits_data.dict_to_tbHDU(Dcatin)

        # Select entries to output arc data
        Catout = fits_data.sample_entries(Catin, ecc=0.7)
        Catout.name = "Lenzen_arcsfound"
        finalcat = catalog_file[:-4] + '.fits'
        Catout.writeto(finalcat, clobber=True)

        # Image
        img = ndimage.imread('data/result.ppm')
        comb = np.maximum(img[..., 0], img[..., 1])
        diff = img[..., 2] - comb
        pyfits.writeto(finalimg, diff[::-1], clobber=True)

    else:
        blank_array = np.zeros((y_size, x_size))
        pyfits.writeto(finalimg, blank_array, clobber=True)

    if clean:
        os.system('rm -rf temp/ %s %s' % (imgfile, catfile))

    return
Beispiel #4
0
def process_output(imgfile,catfile,clean=False):

    from scipy import ndimage
    import numpy as np
    
    # ----------
    # Catalog
    catalog_file = catfile
    os.system("sed -e 's/^[ ]*//' %s | tr -s ' ' > %s" % ('data/'+catfile,catalog_file))
    columns = ['X','Y','ecc','lambda1','lambda2','A','B','theta','size']
    Dcatin = ascii_data.dict_from_csv(catalog_file,columns,header_lines=10,delimiter=' ')

    # Lenzen outputs the 'Y' positions inverted. Lets fix that:
    hdr = pyfits.getheader('data/'+imgfile)
    y_size = hdr['naxis1']
    x_size = hdr['naxis2']
    Y_inv = Dcatin['Y']
    
    finalimg = re.sub("_out_img.fits","_out_result.fits",imgfile)
    if len(Y_inv):
        Dcatin['Y'] = [ str(y_size-int(i)) for i in Y_inv ]
        Catin = fits_data.dict_to_tbHDU(Dcatin)

        # Select entries to output arc data
        Catout = fits_data.sample_entries(Catin,ecc=0.7)
        Catout.name = "Lenzen_arcsfound"
        finalcat = catalog_file[:-4]+'.fits'
        Catout.writeto(finalcat,clobber=True)
        
        # Image
        img = ndimage.imread('data/result.ppm')
        comb = np.maximum(img[...,0],img[...,1])
        diff = img[...,2] - comb
        pyfits.writeto(finalimg,diff[::-1],clobber=True)
        
    else:
        blank_array = np.zeros((y_size,x_size))
        pyfits.writeto(finalimg,blank_array,clobber=True)
    
    if clean:
        os.system('rm -rf temp/ %s %s' % (imgfile,catfile));

    return
Beispiel #5
0
    imgfile = args[0]

    # Run Horesh's arcfinder
    finalcat = run(imgfile, inputfile)

    FITSout = True

    # To finish the process, convert the ASCII table into FITS if asked for.
    if FITSout:
        #        finalfit = finalcat[:-4]+".fit";
        finalfit = 'Catalog_arcs_horesh.out.fit'
        cat = numpy.loadtxt(finalcat).T
        if cat.ndim == 1:
            cat.shape = (len(cat), 1)
        dcat = {
            'ID': cat[0],
            'X': cat[1],
            'Y': cat[2],
            'STmag': cat[3],
            'Length': cat[4],
            'Width': cat[5],
            'L/W': cat[6],
            'Area': cat[7]
        }
        tbhdu = fits_data.dict_to_tbHDU(dcat, tbname="Horesh_arcsfound")
        tbhdu.writeto(finalfit, clobber=True)
#        os.system('rm -f %s' % (finalcat));

    sys.exit(0)
Beispiel #6
0
    # Read DS9 regionfile..
    #
    D_in = asc.read_ds9cat(regionfile)
    objimg = pyfits.getdata(imagename, header=False)
    if segimg:
        segimg = pyfits.getdata(segimg)

    D_in['filename'] = imagename

    D_out = run(D_in, objimg, segimg, shape=shape, objsfile=objsfile, hdr=None)

    if cattype.upper() == 'FITS':
        outname = catfile + re.sub(".reg", ".fit", regionfile)
        tbhdu = fts.dict_to_tbHDU(D_out, D_out['filename'] + "_stamps",
                                  'filename', 'x', 'y', 'color', 'segmented',
                                  'id', 'objfile')
        try:
            tbhdu.writeto(outname)
        except IOError:
            os.remove(outname)
            tbhdu.writeto(outname)
    else:
        outname = catfile + re.sub(".reg", ".csv", regionfile)
        asc.dict_to_csv(D_out,
                        filename=outname,
                        fieldnames=[
                            'filename', 'x', 'y', 'color', 'segmented', 'id',
                            'objfile'
                        ])
Beispiel #7
0
def run(regionfile,
        imagefile,
        args={},
        preset='',
        shape=(100, 100),
        tablefile="objectsTb",
        stampsfile="pstamp_"):
    """
    Runs the pipeline for arc postage stamps creation and segmentation (in this order)
    
    For each point inside (DS9) 'regionfile' take a window of size 'shape' around it.

    Sextractor config parameters can be passed using 'args'. So far, catalog output 
    parameters are declared through the global variable 'PARAMS'.
    Use 'preset' for optimized settings. See sltools.image.sextractor for more info.
    
    Input:
     - regionfile      str : DS9 region file
     - imagefile       str : FITS filename
     - args  {'key',value} : Sextractor config params
     - preset          str : See sltools.image.sextractor
     - shape     (int,int) : Object cutouts shape (dx,dy)
     - tablefile       str : FITS table output filename
     - stampsfile      str : Output file rootnames
    
    Output:
     Write to 'tablefile' FITS table the segmented objects properties ('PARAMS')
    
    ---
    """

    rootname = imagefile[:-5]

    # Object's centroid will be placed over postamp's central point
    x_i = shape[0] / 2
    y_i = shape[1] / 2
    x_size, y_size = shape

    tbList = []
    flList = []

    # Read DS9 regionfile and input imagefile..
    #
    D_in = asc.read_ds9cat(regionfile)

    X = asarray(D_in['x'])
    Y = asarray(D_in['y'])
    centroids = XY = zip(X, Y)
    R = asarray(D_in['size'])
    logging.debug("Centroids: %s", XY)
    if len(XY) == 0:
        logging.warning("No objects in given Centroids list. Finishing.")
        return

    img, hdr = pyfits.getdata(imagefile, header=True)

    # For each "centroid":
    #
    for i in xrange(len(XY)):

        logging.info("Process %s, point: %s", i, XY[i])

        # Take a snapshot
        #
        obj, hdr = imcp.cutout(img,
                               hdr,
                               xo=X[i],
                               yo=Y[i],
                               x_size=x_size,
                               y_size=y_size)
        file_i = rootname + "_pstamp_" + str(X[i]) + "_" + str(Y[i]) + ".fits"
        pyfits.writeto(file_i, obj, hdr, clobber=True)

        logging.info("Snapshot %s created", file_i)
        flList.append(file_i)

        # Run Sextractor over newly created sanpshot..
        #
        Dsex = SE.run_segobj(file_i, PARAMS, preset)  ### SEXTRACTOR CALL
        segimg = pyfits.getdata(Dsex['SEGMENTATION'])
        cathdu = pyfits.open(Dsex['CATALOG'])[1]

        logging.info("Segmentation done.")
        logging.info("Files %s,%s,%s created", Dsex['SEGMENTATION'],
                     Dsex['OBJECTS'], Dsex['CATALOG'])

        objID = segobjs.centroid2id(segimg, (x_i, y_i))

        # The following block searchs for the nearest neighbour and check if the IDs are
        # in accordance. If the given centroid is not part of an object (i.e, ID==0),
        # the nearest object is taken in place; and if the IDs do not match and non-zero
        # valued just an warning is printed to "debug" level.
        #
        centroids_sex = zip(cathdu.data.field('X_IMAGE'),
                            cathdu.data.field('Y_IMAGE'))
        [logging.debug("SE Centroids: %s", _c) for _c in centroids_sex]

        nrst_indx_dist = tbmtch.nearest_neighbour([centroids[i]],
                                                  centroids_sex)
        logging.debug("Nearest point index|distance: %s (length:%s)",
                      nrst_indx_dist, len(nrst_indx_dist))

        _indx, _dist = nrst_indx_dist[0]
        nrst_obj = cathdu.data[_indx]

        _id = nrst_obj.field('NUMBER')
        if objID == 0:
            objID = _id
            logging.warning(
                "Object ID is 0 for Centroid:%s. Using object (ID) %s, instead.",
                centroids[i], _id)
        if objID != _id:
            logging.debug(
                "Oops, two different objects were found matching point %s, objIDs %s and %s",
                centroids[i], objID, _id)

        logging.info("ObjectID: %s being readout", objID)
        tbList.append(fts.select_entries(cathdu, 'NUMBER', objID))

        arc_i = rootname + "_nrstArc_" + str(X[i]) + "_" + str(Y[i]) + ".fits"
        arc = imcp.select(segimg, obj, objID)
        pyfits.writeto(arc_i, arc, hdr, clobber=True)

    # Create output table
    new_tbhdu = tbList[0]
    for i in xrange(1, len(tbList)):
        new_tbhdu = fts.extend_tbHDU(new_tbhdu, tbList[i])

    tb_flList = fts.dict_to_tbHDU({'filename': flList})
    new_tbhdu = fts.merge_tbHDU(new_tbhdu, tb_flList)

    tablefile = rootname + "_arcscat.fits"
    new_tbhdu.writeto(tablefile, clobber=True)

    return
Beispiel #8
0
def run(tbhdu_check, tbhdu_truth, radius=1):
    """
    Check if positions in tables match within a given radius
    
    Required fieldnames inside tbhdu's:
     'X' and 'Y' or 'X_IMAGE' and 'Y_IMAGE'
    
    Input:
     - tbhdu_check  tbHDU : Being-checked Table (FITS)
     - tbhdu_truth  tbHDU : Truth Table (FITS)
     - radius       float : Distance parameters for objects position matching
    
    Output:
     - new_tbhdu tbHDU : New table with matching information (fields):
            'X'        = [int] x position of input table
            'Y'        = [int] y position of input table
            'XY_MATCH' = [bool] was X,Y object matched within 'radius'?
            'X_NEAR'   = [int] x position of nearest object
            'Y_NEAR'   = [int] y position of nearest object


    * obs: tbHDU = pyfits.BinTableHDU
    
    ---
    """
    
    Ntotal_truth = tbhdu_truth.header['naxis2']
    Ntotal_check = tbhdu_check.header['naxis2']
    logging.debug("Input params (tbC,tbT,radius): %s,%s,%s" % (tbhdu_check.name,tbhdu_truth.name,radius))
    logging.debug("Check table length: %d",Ntotal_check)
    logging.debug("Truth table length: %d",Ntotal_truth)

    tbname_check = tbhdu_check.name
    tbname_truth = tbhdu_truth.name
    tbname_out = tbname_check+"_matched_objs"

    try:
        x_A = tbhdu_check.data.field('X')
        y_A = tbhdu_check.data.field('Y')
    except:
        x_A = tbhdu_check.data.field('X_IMAGE')
        y_A = tbhdu_check.data.field('Y_IMAGE')
    try:
        x_B = tbhdu_truth.data.field('X')
        y_B = tbhdu_truth.data.field('Y')
    except:
        x_B = tbhdu_truth.data.field('X_IMAGE')
        y_B = tbhdu_truth.data.field('Y_IMAGE')
    
    xy_check = zip(x_A,y_A)
    xy_truth = zip(x_B,y_B)
    
    dict_Matchout = match_positions(xy_check,xy_truth,radius)
    
#    Ntrue = int(tbhdu_check_wneib.header['ntrue']);
#    Nfalse = int(tbhdu_check_wneib.header['nfalse']);

    new_tbhdu = fits_data.dict_to_tbHDU(dict_Matchout,tbname_out)
    new_tbhdu.header.update('hierarch truthtbname',tbname_truth,"Name of Truth table used")
    new_tbhdu.header.update('hierarch ntottruth',Ntotal_truth,"Total number of arcs in Truth table")
    new_tbhdu.header.update('ntrue',Ntrue,"Number of found objs in Check table")
    new_tbhdu.header.update('nfalse',Nfalse,"Number of not-found objs in Check table")
    Ntrue = np.where(new_tbhdu.data.field('XY_MATCH') == True).size
    Nfalse = np.where(new_tbhdu.data.field('XY_MATCH') == False).size
    Ntotal = Ntrue + Nfalse
    logging.debug("Matched table columns/data: %s",new_tbhdu.data)
    
    # Completeza:
    Comp = float(Ntrue)/Ntotal
    logging.info("Completeness (N_true/N_total): %s" % (Comp))
    
    # Contaminacao:
    Cont = float(Nfalse)/Ntotal
    logging.info("Contamination (N_false/N_total): %s" % (Cont))

    return new_tbhdu
Beispiel #9
0
def run(tbhdu_check, tbhdu_truth, radius=1):
    """
    Check if positions in tables match within a given radius
    
    Required fieldnames inside tbhdu's:
     'X' and 'Y' or 'X_IMAGE' and 'Y_IMAGE'
    
    Input:
     - tbhdu_check  tbHDU : Being-checked Table (FITS)
     - tbhdu_truth  tbHDU : Truth Table (FITS)
     - radius       float : Distance parameters for objects position matching
    
    Output:
     - new_tbhdu tbHDU : New table with matching information (fields):
            'X'        = [int] x position of input table
            'Y'        = [int] y position of input table
            'XY_MATCH' = [bool] was X,Y object matched within 'radius'?
            'X_NEAR'   = [int] x position of nearest object
            'Y_NEAR'   = [int] y position of nearest object


    * obs: tbHDU = pyfits.BinTableHDU
    
    ---
    """
    
    Ntotal_truth = tbhdu_truth.header['naxis2']
    Ntotal_check = tbhdu_check.header['naxis2']
    logging.debug("Input params (tbC,tbT,radius): %s,%s,%s" % (tbhdu_check.name,tbhdu_truth.name,radius))
    logging.debug("Check table length: %d",Ntotal_check)
    logging.debug("Truth table length: %d",Ntotal_truth)

    tbname_check = tbhdu_check.name
    tbname_truth = tbhdu_truth.name
    tbname_out = tbname_check+"_matched_objs"

    try:
        x_A = tbhdu_check.data.field('X')
        y_A = tbhdu_check.data.field('Y')
    except:
        x_A = tbhdu_check.data.field('X_IMAGE')
        y_A = tbhdu_check.data.field('Y_IMAGE')
    try:
        x_B = tbhdu_truth.data.field('X')
        y_B = tbhdu_truth.data.field('Y')
    except:
        x_B = tbhdu_truth.data.field('X_IMAGE')
        y_B = tbhdu_truth.data.field('Y_IMAGE')
    
    xy_check = zip(x_A,y_A)
    xy_truth = zip(x_B,y_B)
    
    dict_Matchout = match_positions(xy_check,xy_truth,radius)
    
#    Ntrue = int(tbhdu_check_wneib.header['ntrue']);
#    Nfalse = int(tbhdu_check_wneib.header['nfalse']);

    new_tbhdu = fits_data.dict_to_tbHDU(dict_Matchout,tbname_out)
    new_tbhdu.header.update('hierarch truthtbname',tbname_truth,"Name of Truth table used")
    new_tbhdu.header.update('hierarch ntottruth',Ntotal_truth,"Total number of arcs in Truth table")
    new_tbhdu.header.update('ntrue',Ntrue,"Number of found objs in Check table")
    new_tbhdu.header.update('nfalse',Nfalse,"Number of not-found objs in Check table")
    Ntrue = np.where(new_tbhdu.data.field('XY_MATCH') == True).size
    Nfalse = np.where(new_tbhdu.data.field('XY_MATCH') == False).size
    Ntotal = Ntrue + Nfalse
    logging.debug("Matched table columns/data: %s",new_tbhdu.data)
    
    # Completeza:
    Comp = float(Ntrue)/Ntotal
    logging.info("Completeness (N_true/N_total): %s" % (Comp))
    
    # Contaminacao:
    Cont = float(Nfalse)/Ntotal
    logging.info("Contamination (N_false/N_total): %s" % (Cont))

    return new_tbhdu
Beispiel #10
0
                        dest='input', default=None,
                        help="Horesh' arcfinder input parameters file");
                        
    (opts,args) = parser.parse_args();

    if len(args) == 0:
        parser.error("No input images were given.");

    inputfile = opts.input;

    imgfile = args[0];
    
    # Run Horesh's arcfinder
    finalcat = run(imgfile,inputfile)

    FITSout = True;
    
    # To finish the process, convert the ASCII table into FITS if asked for.
    if FITSout:
#        finalfit = finalcat[:-4]+".fit";
        finalfit = 'Catalog_arcs_horesh.out.fit';
        cat = numpy.loadtxt(finalcat).T;
        if cat.ndim == 1:
            cat.shape = (len(cat),1);
        dcat = {'ID':cat[0], 'X':cat[1], 'Y':cat[2], 'STmag':cat[3], 'Length':cat[4], 'Width':cat[5], 'L/W':cat[6], 'Area':cat[7]};
        tbhdu = fits_data.dict_to_tbHDU(dcat,tbname="Horesh_arcsfound");
        tbhdu.writeto(finalfit,clobber=True);
#        os.system('rm -f %s' % (finalcat));

    sys.exit(0);
Beispiel #11
0
def run(regionfile, imagefile, args={}, preset='', shape=(100,100), tablefile="objectsTb",stampsfile="pstamp_"):
    """
    Runs the pipeline for arc postage stamps creation and segmentation (in this order)
    
    For each point inside (DS9) 'regionfile' take a window of size 'shape' around it.

    Sextractor config parameters can be passed using 'args'. So far, catalog output 
    parameters are declared through the global variable 'PARAMS'.
    Use 'preset' for optimized settings. See sltools.image.sextractor for more info.
    
    Input:
     - regionfile      str : DS9 region file
     - imagefile       str : FITS filename
     - args  {'key',value} : Sextractor config params
     - preset          str : See sltools.image.sextractor
     - shape     (int,int) : Object cutouts shape (dx,dy)
     - tablefile       str : FITS table output filename
     - stampsfile      str : Output file rootnames
    
    Output:
     Write to 'tablefile' FITS table the segmented objects properties ('PARAMS')
    
    ---
    """
    
    rootname = imagefile[:-5]
    
    # Object's centroid will be placed over postamp's central point
    x_i = shape[0]/2
    y_i = shape[1]/2
    x_size,y_size = shape

    tbList = []
    flList = []
    
    # Read DS9 regionfile and input imagefile..
    #
    D_in = asc.read_ds9cat(regionfile)

    X = asarray(D_in['x'])
    Y = asarray(D_in['y'])
    centroids = XY = zip(X,Y)
    R = asarray(D_in['size'])
    logging.debug("Centroids: %s",XY)
    if len(XY) == 0:
        logging.warning("No objects in given Centroids list. Finishing.")
        return

    img,hdr = pyfits.getdata(imagefile,header=True)

    # For each "centroid":
    #
    for i in xrange(len(XY)):

        logging.info("Process %s, point: %s",i,XY[i])
        
        # Take a snapshot
        #
        obj,hdr = imcp.cutout(img,hdr,xo=X[i],yo=Y[i],x_size=x_size,y_size=y_size)
        file_i = rootname+"_pstamp_"+str(X[i])+"_"+str(Y[i])+".fits"
        pyfits.writeto(file_i,obj,hdr,clobber=True)

        logging.info("Snapshot %s created",file_i)
        flList.append(file_i)
        
        # Run Sextractor over newly created sanpshot..
        #
        Dsex = SE.run_segobj(file_i, PARAMS,preset)   ### SEXTRACTOR CALL
        segimg = pyfits.getdata(Dsex['SEGMENTATION'])
        cathdu = pyfits.open(Dsex['CATALOG'])[1]
        
        logging.info("Segmentation done.")
        logging.info("Files %s,%s,%s created",Dsex['SEGMENTATION'],Dsex['OBJECTS'],Dsex['CATALOG'])
        
        objID = segobjs.centroid2id(segimg,(x_i,y_i))

        # The following block searchs for the nearest neighbour and check if the IDs are
        # in accordance. If the given centroid is not part of an object (i.e, ID==0), 
        # the nearest object is taken in place; and if the IDs do not match and non-zero
        # valued just an warning is printed to "debug" level.
        #
        centroids_sex = zip(cathdu.data.field('X_IMAGE'),cathdu.data.field('Y_IMAGE'))
        [ logging.debug("SE Centroids: %s",_c) for _c in centroids_sex ]

        nrst_indx_dist = tbmtch.nearest_neighbour([centroids[i]],centroids_sex)
        logging.debug("Nearest point index|distance: %s (length:%s)",nrst_indx_dist,len(nrst_indx_dist))

        _indx,_dist = nrst_indx_dist[0]
        nrst_obj = cathdu.data[_indx]

        _id = nrst_obj.field('NUMBER')
        if objID == 0:
            objID = _id
            logging.warning("Object ID is 0 for Centroid:%s. Using object (ID) %s, instead.",centroids[i],_id)
        if objID != _id:
            logging.debug("Oops, two different objects were found matching point %s, objIDs %s and %s",centroids[i],objID,_id)
        
        logging.info("ObjectID: %s being readout",objID)
        tbList.append(fts.select_entries(cathdu,'NUMBER',objID))
        
        arc_i = rootname+"_nrstArc_"+str(X[i])+"_"+str(Y[i])+".fits"
        arc = imcp.select(segimg,obj,objID)
        pyfits.writeto(arc_i,arc,hdr,clobber=True)


    # Create output table
    new_tbhdu = tbList[0]
    for i in xrange(1,len(tbList)):
        new_tbhdu = fts.extend_tbHDU(new_tbhdu,tbList[i])

    tb_flList = fts.dict_to_tbHDU({'filename':flList})
    new_tbhdu = fts.merge_tbHDU(new_tbhdu,tb_flList)

    tablefile = rootname+"_arcscat.fits"
    new_tbhdu.writeto(tablefile,clobber=True)

    return
def run(regionfile,
        imagefile,
        args={},
        preset='HST_Arcs',
        shape=(100,
               100)):  # , tablefile="objectsTb"):# ,stampsfile="pstamp_"):
    """
    Runs the pipeline for postamps creation and segmented
    
    run( regionfile, imagefile [,...] )
    
    For each point listed inside DS9 'regionfile', considered object 
    centroids in 'imagefile', a snapshot and object segmentation are 
    done; shape and rootname (suffixed by "objID", fits extension) 
    are passed through 'shape' and 'stampsfile', resp.
    
    Sextractor config parameters can be passed using 'args'. So far, 
    catalog output parameters are hardcoded.
    Use 'preset' for optimized settings. See sltools.image.sextractor
    for more info.
    
    Input:
     - regionfile : str
        DS9 region file
     - imagefile : str
        FITS filename
     - args : {'option':'value',}
        Sextractor config params
     - preset : str
        See sltools.image.sextractor
     - shape : (int,int)
        Object cutouts shape (pixels,pixels)
     - tablefile : str
        FITS table output filename
     - stampsfile : str
        Output file rootnames
    
    Output:
     - tbhdu : pyfits.BinTableHDU
        Output (fits) table with objects' computed parameters
    
    ---
    """

    # Sextractor Params:
    PARAMS = [
        'NUMBER', 'ELONGATION', 'ELLIPTICITY', 'ISOAREA_IMAGE', 'A_IMAGE',
        'B_IMAGE', 'THETA_IMAGE', 'X_IMAGE', 'Y_IMAGE', 'X2_IMAGE', 'Y2_IMAGE',
        'XY_IMAGE'
    ]

    # Object's centroid will be placed over postamp's central point
    x_i = shape[0] / 2
    y_i = shape[1] / 2

    _tbList = []
    _flList = []
    _imList = []

    # Read DS9 regionfile and input imagefile..
    #
    D_in = asc.read_ds9cat(regionfile)
    X = asarray(D_in['x'])
    Y = asarray(D_in['y'])

    centroids = zip(X, Y)

    ####################################################################
    # Segment the whole image at once
    # Needs:
    # X , Y , image(FITS) filename and Sextractor arguments/inputs
    selobjimg_W, selobjhdu_W = whole_image(X, Y, imagefile, PARAMS, preset)
    selobjhdu_W.writeto('Whole_image.fit', clobber=True)
    pyfits.writeto('Whole_image.fits', selobjimg_W, clobber=True)
    ####################################################################

    img, hdr = pyfits.getdata(imagefile, header=True)
    logging.debug("Centroids: %s", centroids)

    rootname = re.sub(".fits", "", imagefile)

    # For each centroid, do:
    #
    i = -1
    xy = centroids[:]
    for o_o in centroids:
        i += 1
        logging.info("Process %s, point: %s", i, o_o)

        # Take a snapshot
        #
        _obj, _hdr = imcp.snapshot(img, hdr, centroid=o_o, shape=shape)
        file_i = rootname + "_ps" + str(i) + ".fits"
        pyfits.writeto(file_i, _obj, _hdr, clobber=True)
        del _obj, _hdr

        logging.info("Poststamp %s created", file_i)

        # Run Sextractor over newly created sanpshot..
        #
        _Dsex = sextractor.run_segobj(file_i, PARAMS, preset=preset)
        ### SEXTRACTOR CALL
        segimg = pyfits.getdata(_Dsex['SEGMENTATION'])
        objimg = pyfits.getdata(_Dsex['OBJECTS'])
        cathdu = pyfits.open(_Dsex['CATALOG'])[1]

        objID = segimg[y_i, x_i]

        if not objID:
            lixo = xy.remove(o_o)
            continue

        logging.info("ObjectID: %s being readout", objID)

        _tbList.append(fts.select_entries(cathdu, 'NUMBER', objID))
        _flList.append(file_i)
        _imList.append(imcp.copy_objects(objimg, segimg, [objID]))

    # Initialize output table and image..
    #
    selobjhdu_S = _tbList[0]
    selobjimg_S = np.zeros(img.shape, dtype=img.dtype)
    selobjimg_S = combine.add_images(selobjimg_S,
                                     _imList[0],
                                     x=xy[0][0],
                                     y=xy[0][1])
    #
    # and do the same for each object
    #
    for i in xrange(1, len(_tbList)):
        selobjhdu_S = fts.extend_tbHDU(selobjhdu_S, _tbList[i])
        selobjimg_S = combine.add_images(selobjimg_S,
                                         _imList[i],
                                         x=xy[i][0],
                                         y=xy[i][1])

    # Write down the FITS catalog..
    #
    tb_flList = fts.dict_to_tbHDU({'filename': _flList})
    selobjhdu_S = fts.merge_tbHDU(selobjhdu_S, tb_flList)

    # And the FITS image, composed by the well segmented objects..
    #
    selobjhdu_S.writeto('Stamps_compose.fit', clobber=True)
    pyfits.writeto('Stamps_compose.fits', selobjimg_S, clobber=True)

    return
Beispiel #13
0
def run(regionfile, imagefile, args={}, preset='HST_Arcs', shape=(100,100)):# , tablefile="objectsTb"):# ,stampsfile="pstamp_"):
    """
    Runs the pipeline for postamps creation and segmented
    
    run( regionfile, imagefile [,...] )
    
    For each point listed inside DS9 'regionfile', considered object 
    centroids in 'imagefile', a snapshot and object segmentation are 
    done; shape and rootname (suffixed by "objID", fits extension) 
    are passed through 'shape' and 'stampsfile', resp.
    
    Sextractor config parameters can be passed using 'args'. So far, 
    catalog output parameters are hardcoded.
    Use 'preset' for optimized settings. See sltools.image.sextractor
    for more info.
    
    Input:
     - regionfile : str
        DS9 region file
     - imagefile : str
        FITS filename
     - args : {'option':'value',}
        Sextractor config params
     - preset : str
        See sltools.image.sextractor
     - shape : (int,int)
        Object cutouts shape (pixels,pixels)
     - tablefile : str
        FITS table output filename
     - stampsfile : str
        Output file rootnames
    
    Output:
     - tbhdu : pyfits.BinTableHDU
        Output (fits) table with objects' computed parameters
    
    ---
    """
    
    # Sextractor Params:
    PARAMS=['NUMBER','ELONGATION','ELLIPTICITY','ISOAREA_IMAGE','A_IMAGE','B_IMAGE','THETA_IMAGE','X_IMAGE','Y_IMAGE','X2_IMAGE','Y2_IMAGE','XY_IMAGE']

    # Object's centroid will be placed over postamp's central point
    x_i = shape[0]/2;
    y_i = shape[1]/2;

    _tbList = [];
    _flList = [];
    _imList = [];
    
    # Read DS9 regionfile and input imagefile..
    #
    D_in = asc.read_ds9cat(regionfile);
    X = asarray(D_in['x']);
    Y = asarray(D_in['y']);

    centroids = zip(X,Y);

    ####################################################################
    # Segment the whole image at once
    # Needs:
    # X , Y , image(FITS) filename and Sextractor arguments/inputs
    selobjimg_W,selobjhdu_W = whole_image(X,Y,imagefile,PARAMS,preset);
    selobjhdu_W.writeto('Whole_image.fit',clobber=True);
    pyfits.writeto('Whole_image.fits',selobjimg_W,clobber=True);
    ####################################################################
        
    img,hdr = pyfits.getdata(imagefile,header=True);
    logging.debug("Centroids: %s",centroids);

    rootname = re.sub(".fits","",imagefile);

    # For each centroid, do:
    #
    i=-1;
    xy = centroids[:];
    for o_o in centroids:
        i+=1;
        logging.info("Process %s, point: %s",i,o_o);
        
        # Take a snapshot
        #
        _obj,_hdr = imcp.snapshot( img, hdr, centroid=o_o, shape=shape );
        file_i = rootname+"_ps"+str(i)+".fits";
        pyfits.writeto(file_i,_obj,_hdr,clobber=True);
        del _obj,_hdr;
        
        logging.info("Poststamp %s created",file_i);
        
        # Run Sextractor over newly created sanpshot..
        #
        _Dsex = sextractor.run_segobj(file_i, PARAMS,preset=preset);   ### SEXTRACTOR CALL
        segimg = pyfits.getdata(_Dsex['SEGMENTATION']);
        objimg = pyfits.getdata(_Dsex['OBJECTS']);
        cathdu = pyfits.open(_Dsex['CATALOG'])[1];
        
        objID = segimg[y_i,x_i];
        
        if not objID:
            lixo = xy.remove(o_o);
            continue;
            
        logging.info("ObjectID: %s being readout",objID);

        _tbList.append(fts.select_entries(cathdu,'NUMBER',objID));
        _flList.append(file_i);
        _imList.append(imcp.copy_objects(objimg,segimg,[objID]));

    # Initialize output table and image..
    #
    selobjhdu_S = _tbList[0];
    selobjimg_S = np.zeros(img.shape,dtype=img.dtype);    
    selobjimg_S = combine.add_images(selobjimg_S,_imList[0],x=xy[0][0],y=xy[0][1]);
    #
    # and do the same for each object
    #
    for i in xrange(1,len(_tbList)):
        selobjhdu_S = fts.extend_tbHDU(selobjhdu_S,_tbList[i]);
        selobjimg_S = combine.add_images(selobjimg_S,_imList[i],x=xy[i][0],y=xy[i][1]);

    # Write down the FITS catalog..
    #
    tb_flList = fts.dict_to_tbHDU({'filename':_flList});
    selobjhdu_S = fts.merge_tbHDU(selobjhdu_S,tb_flList);

    # And the FITS image, composed by the well segmented objects..
    #
    selobjhdu_S.writeto('Stamps_compose.fit',clobber=True);
    pyfits.writeto('Stamps_compose.fits',selobjimg_S,clobber=True)

    return;