def find_obj(path, coordinates_RA, coordinates_DEC, obj_id): file_list = [] #list to recive the fits files names that are in path for file2 in glob.glob( os.path.join(path, '*.fits') ): file_list.append(file2) del file2 file_list.sort() for i in range(0,len(file_list)): print "Working in file:",file_list[i][len(path)+1:] im_hdulist = pyfits.open(file_list[i]) im_header = im_hdulist[0].header n_axis1 = im_header['NAXIS1'] n_axis2 = im_header['NAXIS2'] #print n_axis1, n_axis1 for j in range(0,len(coordinates_RA)): coord_XY = radec2xy(file_list[i],coordinates_RA[j],coordinates_DEC[j]) if coord_XY[0]>0 and coord_XY[0]<n_axis1 and coord_XY[1]>0 and coord_XY[1]<n_axis2: print j, coord_XY comand = 'imcp.py ' + str(file_list[i]) + ' ' + str(coord_XY[0]) + ' ' + str(coord_XY[1]) + ' -s 900,900 -o ' + '_' + str(obj_id[n]) + "_" + str(file_list[i][len(path)+1:]) + '_' #comando para fazer o corte nas imagens
def find_cs82_image(): """ Function to find in which cs82 image the redmapper clusters are and create the command to make a cut_out using imcp.py from sltools Input - Output - #FIXME - finish documentation """ print "find_cs82_im" red_data, red_names = rc.read_catalog("cs82_redmapper") limits, imgs = cor.cs82_limits() cs82_path = os.environ["CS82_IMAGE_DIR"] for i in range(len(limits)): #print imgs[i] """ if limits[i][0][1] > 300 and limits[i][0][0] < 300: print imgs[i] print limits[i] """ str_out = "" for j in range(len(red_data["RA"])): if red_data["RA"][j] < limits[i][0][1] and \ red_data["RA"][j] > limits[i][0][0] and \ red_data["DEC"][j] < limits[i][1][1] and \ red_data["DEC"][j] > limits[i][1][0]: hdu = pyfits.open(imgs[i]) hdr = hdu[0].header pix_xy = sltools_wcs_conv.radec2xy(hdr, red_data["RA"][j], \ red_data["DEC"][j]) str_out = "imcp.py " + imgs[i] + " " str_out += str(pix_xy[0]) + " " + str(pix_xy[1]) + " " str_out += "-s 600,600 -J -o " str_out += str(red_data["MEM_MATCH_ID"][j]) + "_" str_out += imgs[i][len(cs82_path) + 1:len(cs82_path) + 10] if pix_xy[0] > 0 and pix_xy[0] < 21000 and \ pix_xy[1] > 0 and pix_xy[1] < 21000: print str_out
def find_sdss_image(): """ Function to make create the sdss image for the redmapper clusters Input - Output - #FIXME - finish documentation """ red_data, red_names = rc.read_catalog("cs82_redmapper") str_swarp = "" str_imcp = "" # i_tmp = 897 for i in range(31, len(red_data["RA"])): time.sleep(1) print i names = si.get_sdss_tiles_within(red_data["RA"][i], \ red_data["DEC"][i], "i", win = 5, stripe82 = True) sdss_out_im = str(red_data["MEM_MATCH_ID"][i]) + "_sdss_i.fits " str_swarp = "swarp -IMAGEOUT_NAME " + sdss_out_im for j in range(len(names)): str_swarp += names[j] + " " os.system(str_swarp) hdu = pyfits.open(sdss_out_im) hdr = hdu[0].header pix_xy = sltools_wcs_conv.radec2xy(hdr, red_data["DEC"][i], \ red_data["RA"][i]) str_imcp = "imcp.py " + sdss_out_im + " " str_imcp += str(pix_xy[0]) + " " + str(pix_xy[1]) str_imcp += " -s 600,600 -J -o " + sdss_out_im[:-6] os.system(str_imcp)
def catalog2cutout_pipe(im_path, catalog_path, dim): """ make cuts in images given a coordinate catalogue Input: - im_path : str Path for a fits image - catalog_path : str Path for a fits catalog - dim : str Output stamps shape --- """ im_lim = get_ra_dec_limits(im_path) im_lim_xy = (radec2xy(im_path, im_lim[0][0], im_lim[1][0]), radec2xy(im_path, im_lim[0][1], im_lim[1][1])) im_x_min = min(im_lim_xy[0][0], im_lim_xy[1][0]) im_x_max = max(im_lim_xy[0][0], im_lim_xy[1][0]) im_y_min = min(im_lim_xy[0][1], im_lim_xy[1][1]) im_y_max = max(im_lim_xy[0][1], im_lim_xy[1][1]) hdulist_cat = pyfits.open(catalog_path) hdulist_im = pyfits.open(im_path, memmap=True) im_header = hdulist_im[0].header im_data = hdulist_im[0].data #print hdulist_im.filename() cat_coord = zip(hdulist_cat[1].data.field("RA"), hdulist_cat[1].data.field("DEC")) cat_coord_xy = [] coord_ok = [] for i in range(len(cat_coord)): cat_coord_xy.append(radec2xy( im_path, \ cat_coord[i][0], cat_coord[i][1]) ) test_lim_x = cat_coord_xy[i][0] > im_x_min \ and cat_coord_xy[i][0] < im_x_max test_lim_y = cat_coord_xy[i][1] > im_y_min \ and cat_coord_xy[i][1] < im_y_max if test_lim_x and test_lim_y: coord_ok.append(cat_coord_xy[i]) dx, dy = string.split(dim, sep=',') imgcut_out, hdr_out = cutout( im_data, im_header, coord_unit='pixel', \ xo=coord_ok[0][0], yo=coord_ok[0][1], \ size_unit='pixel', x_size=dx, y_size=dy, mask=None) for i in range(len(coord_ok)): outfits = "out_catalog2cutout_" + str(i) + ".fits" pyfits.writeto(outfits, imgcut_out, hdr_out) return 0
def cutout( img, hdr=None, coord_unit='pixel', xo=0, yo=0, size_unit='pixel', x_size=0, y_size=0, mask=None ): """ Do a snapshot from given fits image. cutout( ndarray, ...) -> (ndarray, header) The function can deal with 'pixel' and 'degrees' cordinate units. As input, besides the image FITS filename (input_file), it receives a central position (xo,yo) and side lengths (x_size,y_size) for output image position and dimensioning. Function returns two values: a numpy.array with resultant image pixel intensities and respective header object. In addition, an image mask information can be given to use as interest region selection. 'mask' is expected to be a numpy.where like structure (i.e, mask=numpy.where()). If given, returned image will have all pixels null except the ones listed in 'mask' parameter. If 'xo=0' and 'yo=0', given image (input_file) central pixel will be chosen as (xo,yo). If 'x_size=0' and 'y_size=0', half length of each side will be used for output dimensions. Input: - img numpy.ndarray : Image array (ndim=2,dtype=float) - hdr pyfits.header : Image (FITS) header - coord_unit str : Position (xo,yo) units ('pixel','degrees') - xo int : Centroid (horizontal) position for cutting window - yo int : Centroid (vertical) position for cutting window - size_unit str : Window sizes (x_size,y_size) units ('pixel','degrees') - x_size int : Horizontal cutting window size - y_size int : Vertical cutting window size - mask (ndarray,ndarray) : Tuple with index arrays (Output like numpy.where()) Output: - (ndarray, header) : Resultant image array and (updated) header instance --- """ image = img; logging.debug("Input parameters (type(image),header,coord_unit,xo,yo,size_unit,x_size,y_size,mask): %s",(type(image),hdr,coord_unit,xo,yo,size_unit,x_size,y_size,mask)); xo=float(xo); yo=float(yo); x_size=float(x_size); y_size=float(y_size); imagem = image; # Initialize some variables.. # x_diff = 0; x_edge = 0; y_diff = 0; y_edge = 0; x_fin = 0; x_ini = 0; y_fin = 0; y_ini = 0; if ( hdr ): dimpix = hf.get_pixelscale( hdr ); logging.info("Pixel_scale: %s",dimpix); y_img_size, x_img_size = imagem.shape; logging.debug("Input image shape: %s",(x_img_size,y_img_size)); # Get the side sides (at least, 1!) and transform for the size_unit if necessary.. # x_cut_size = float(x_size); y_cut_size = float(y_size); if ( size_unit == 'degrees' ): x_cut_size = int(float(x_cut_size)/dimpix); y_cut_size = int(float(y_cut_size)/dimpix); # And if no side size was given, define a default value correspondig to half of original image.. # if not ( x_size ): x_cut_size = int(x_img_size/2); logging.warning("'x_size' not given. Using half of image x side(%d)", x_cut_size); if not ( y_size ): y_cut_size = int(y_img_size/2); logging.warning("'y_size' not given. Using half of image y side(%d)", y_cut_size); logging.debug("Output image shape: %s",(x_cut_size,y_cut_size)); # Check requested output.vs.input image sizes.. # if ( x_cut_size == x_img_size and y_cut_size == y_img_size ): logging.warning("Requested output sizes are the same as input image. Returning image and header unchanged."); return (imagem,hdr); # Verify central coordinates values.. # if ( coord_unit == 'pixel' and (xo != 0 and yo != 0) ): x_halo = int(float(xo)); y_halo = int(float(yo)); elif ( coord_unit == 'degrees' and (xo != 0 and yo != 0) ): x_halo = int(wcsc.radec2xy(hdr,xo,yo)[0]); y_halo = int(wcsc.radec2xy(hdr,xo,yo)[1]); elif ( xo == 0 and yo == 0 ): x_halo = int(x_img_size/2); y_halo = int(y_img_size/2); logging.warning("No central coordinates were given for snapshot. Using image central pixel as the cut center."); else: logging.error("Central positioning is out of valid values."); return (False,False); logging.debug("Central point for output image: %s",(x_halo,y_halo)); # Define the images (in/out) slices to be copied.. # x_ini = x_halo - int(x_cut_size/2) #-1; x_fin = x_ini + x_cut_size; y_ini = y_halo - int(y_cut_size/2) #-1; y_fin = y_ini + y_cut_size; x_ini_old = max( 0, x_ini ); x_fin_old = min( x_img_size, x_fin ); y_ini_old = max( 0, y_ini ); y_fin_old = min( y_img_size, y_fin ); x_ini_new = abs( min( 0, x_ini )); x_fin_new = x_cut_size - (x_fin - x_fin_old); y_ini_new = abs( min( 0, y_ini )); y_fin_new = y_cut_size - (y_fin - y_fin_old); # If header, update pixel<->sky information.. # if ( hdr ): hdr = hf.update_coordinates(hdr.copy(), x_ini, y_ini); hdr.update('NAXIS1',x_cut_size); hdr.update('NAXIS2',y_cut_size); # Initialize new image, and take all index list.. # imagemnova = np.zeros( (y_cut_size,x_cut_size), dtype=imagem.dtype ); ind_z = np.where(imagemnova == 0); # Copy requested image slice.. # imagemnova[ y_ini_new:y_fin_new, x_ini_new:x_fin_new ] = imagem[ y_ini_old:y_fin_old, x_ini_old:x_fin_old ]; # If 'mask', maintain just "central" object on it.. # if ( mask ): msk = ( mask[0]-y_ini, mask[1]-x_ini ) zip_m = zip( msk[0], msk[1] ); zip_z = zip( ind_z[0], ind_z[1] ); L = list(set(zip_z) - set(zip_m)); try: ind_0, ind_1 = zip(*L); indx = ( np.array(ind_0), np.array(ind_1) ); imagemnova[ indx ] = 0; except: pass; return (imagemnova, hdr);