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
def run(D_in, objimg, segimg=None, shape=(100, 100), objsfile='pstamp_', hdr=None): """ Function to take snapshots from objects listed in a DS9's regions file Sextractor is run over given 'imagename' and take the snapshots from objects listed in DS9's 'regionfile'. Input: - regionfile : str DS9' regions file - imagename : str FITS image filename - outname_stamps : str String to add to output name of the poststamp images. - outname_cat : str Output: -> <bool> """ from numpy import asarray imagename_ds9 = D_in['filename'] # Detect objects corresponding to given centroids.. # XY = zip(asarray(D_in['x']), asarray(D_in['y'])) logging.debug("Input ['x'],['y'] points: %s" % (XY)) if (segimg != None): logging.info( "Segmentation image given, looking for objects for each (x,y) on it." ) objIDs = [segobjs.centroid2ID(segimg, _xy) for _xy in XY] else: logging.info("No segmentation image given.") objIDs = [0] * len(XY) logging.debug("ObjIDs: %s " % (objIDs)) D_out = D_in D_out['id'] = objIDs D_out['segmented'] = [_id != 0 for _id in objIDs] D_out['objfile'] = [] del D_out['size'] # Now lets take the detected objects snapshots.. # for i in range(len(objIDs)): outname = objsfile + str(i) + "_id_" + str(objIDs[i]) + ".fits" D_out['objfile'].append(outname) logging.info("Output objects/files (#,ID,x,y,filename): %s", (i, objIDs[i], XY[i], outname)) _otmp, _htmp = imcp.snapshot(objimg, hdr, centroid=(XY[i][0], XY[i][1]), shape=shape) try: pyfits.writeto(outname, _otmp, _htmp) except IOError: os.remove(outname) pyfits.writeto(outname, _otmp, _htmp) return D_out
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;