def zsview(im, cmap=pl.cm.gray, figsize=(8,5), contours=False, ccolor='r'): z1, z2 = zscale(im) pl.figure(figsize=figsize) pl.imshow(im, vmin=z1, vmax=z2, origin='lower', cmap=cmap, interpolation='none') if contours: pl.contour(im, levels=[z2], origin='lower', colors=ccolor) pl.tight_layout()
def display1D(self): self.spec1DView.canvas.ax.clear() xx=np.arange(self.spec1d.size) # if(self.lastWLCFile): # xx=self.wavelen if (self.wlc): xx=self.wavelen #self.spec1DView.canvas.ax.plot(xx,self.spec1d,'b-') self.spec1DView.canvas.ax.plot(xx,self.spec1d,'b-') #medskyUnderAp = self.medsky1d*(self.extractWin_x1-self.extractWin_x0) if (self.imagetype=='science'): medskyUnderAp = ( self.medsky1d - self.medskylev ) *(self.extractWin_x1-self.extractWin_x0) self.spec1DView.canvas.ax.plot(xx,medskyUnderAp,'r-') #self.spec1DView.canvas.ax.plot(xx,medskyUnderAp,'r-') # print np.shape(xx) # print np.shape(medskyUnderAp) self.spec1DView.canvas.ax.set_title(self.latestFrame) y0,y1=zscale(self.spec1d) # self.spec1DView.canvas.ax.set_ylim([y0,y1]) if(self.imagetype=='arc'): y1=np.max(self.spec1d) if(y0<100.0): y0=0.0 if(y1>100.0): self.spec1DView.canvas.ax.set_ylim([y0,y1]) if (self.wlc): self.spec1DView.canvas.ax.set_xlabel(r'$\lambda (\AA)$') else: self.spec1DView.canvas.ax.set_xlabel('x (pix)') self.spec1DView.canvas.draw()
def plot_image(self, result, title='ScatterPlot', grid=False): if ZSCALE == True: z_min, z_max = zscale.zscale(result.data_array, nsamples=2000, contrast=0.25) else: z_min = result.zmin z_max = result.zmax fig, graph = plt.subplots() # define the colormap and norm #cmap = plt.get_cmap('RdYlBu_r') #cmap = plt.get_cmap('jet') cmap = plt.get_cmap('Greys_r') cmap.set_over('#FFFFFF') cmap.set_under('#000000') cmap.set_bad('#00FF00') image = graph.imshow(result.get_img_array(self.master), cmap=cmap, vmin=z_min, vmax=z_max, origin='lower', interpolation='nearest', extent=([result.xmin, result.xmax, result.ymin, result.ymax]) ) graph.set_xlabel(result.x_name, fontsize=20) graph.set_ylabel(result.y_name, fontsize=20) graph.set_title(title) graph.grid(grid) colorbar = fig.colorbar(image) colorbar.set_label(result.z_name, fontsize=20) return fig
def do_image(self,undone_image,contour_file=None,contour_levels=5,contour_color=None): """Make an image""" version = 4 success = False print("Making an image for "+undone_image) print("Using files... "+str(self.images[undone_image])) cube_data = pyfits.getdata(self.filenames[self.images[undone_image][0]]) z1_k,z2_k = zscale.zscale(np.nan_to_num(cube_data[0,...])) z1_h,z2_h = zscale.zscale(np.nan_to_num(cube_data[1,...])) z1_j,z2_j = zscale.zscale(np.nan_to_num(cube_data[2,...])) aplpy.make_rgb_image(self.filenames[self.images[undone_image][0]], self.filenames[self.images[undone_image][0]].replace('.fits','_2d.png'), vmin_r = z1_k,vmax_r = z2_k,vmin_g = z1_h, vmax_g = z2_h, vmin_b = z1_j, vmax_b = z2_j) gc = aplpy.FITSFigure(self.filenames[self.images[undone_image][0]].replace('.fits','_2d.fits')) gc.show_rgb(self.filenames[self.images[undone_image][0]].replace('.fits','_2d.png')) gc.tick_labels.set_xformat("dd.dd") gc.tick_labels.set_yformat("dd.dd") #out_filename = self.filenames[undone_image].replace('.fits','.pdf') if contour_file: #gc.show_contour(contour_file,levels=contour_levels,colors="black",linewidths = 3.,smooth=3.,alpha=0.5) gc.show_contour(contour_file,levels=contour_levels,colors=contour_color,linewidths = 1.) #chunks = contour_file.split('_') #print(chunks) #linename = chunks[3] #moment_name = chunks[4][0:-5] #tag = "_"+linename+"_"+moment_name #print(tag) #out_filename = out_filename.replace('.pdf',tag+'.pdf') #self.add_labels(gc,undone_image,linename,moment_name = moment_name,color=contour_color) else: pass #self.add_labels(gc,undone_image) #gc.show_rectangles(self.apos,self.bpos,0.0625,0.0625,ec='w',lw=2,facecolor='none',zorder=1) #gc.show_ellipses(self.apos,self.bpos,self.awin*2,self.bwin*2,angle=self.twin,facecolor='none',ec='green',lw=2) #print(self.awin,self.bwin) success = True if success: self.report_success(undone_image,version) return(gc)
def get_offset_center(f, plot=False, interactive=False): ''' Given a fits image, returns the offset in Ra, DEC, that needs to be applied for the telescope tp go from the current pointing position, to the coodinates of the object specified in the fits file. ''' if(not os.path.isfile(f)): print "File %s does not exist! Returning Zero offsets..."%f return -1, 0,0 else: image = pf.open(f) wcs = pywcs.WCS(image[0].header) rra, rdec = cc.hour2deg(image[0].header['OBJRA'],image[0].header['OBJDEC'] ) x, y = np.round(wcs.wcs_sky2pix(rra, rdec, 0), 0) pra, pdec = wcs.wcs_pix2sky(np.array([[1293., 1280.]] , np.float_), 0)[0] dra, ddec = cc.get_offset(pra, pdec, rra, rdec) xl, yu = np.round(wcs.wcs_sky2pix(rra+90./3600, rdec-90./3600, 0), 0) xu, yl = np.round(wcs.wcs_sky2pix(rra-90./3600, rdec+90./3600, 0), 0) imageloc = image[0].data.T[xl:xu,yl:yu] if imageloc.shape[0]==0 or imageloc.shape[1]==0: logger.warn( "Astrometry has FAILED on this! The object is outside the frame! Resending to the numb astrometric solution") logger.error("Astrometry has FAILED on this! The object is outside the frame! Resending to the numb astrometric solution") print "Pixels are", xl, xu, yl, yu try: code, dra, ddec = get_offset_center_failed_astro(f, plot=plot, interactive=interactive) return 2, dra, ddec except: return -1,0,0 if(plot): plt.figure(figsize=(8,8)) zmin, zmax = zscale.zscale(imageloc) #print zmin, zmax, imageloc, (xl,xu,yl,yu) obj = fitsutils.get_par(f, "OBJECT") plt.suptitle(obj, fontsize=20) plt.imshow(imageloc.T, extent=(xl[0],xu[0],yl[0],yu[0]), aspect="equal", interpolation="none", origin="lower", vmin=zmin, vmax=zmax) plt.plot(1293., 1280., "ws", ms=7, label="Current pointing") plt.plot(x, y, "b*", ms=10, label="Target pointing") plt.gca().invert_xaxis() plt.legend() if (interactive): plt.show() else: plt.savefig(os.path.join(os.path.dirname(f).replace("raw", "phot"), os.path.basename(f).replace(".fits", "_a.png"))) plt.clf() return 0, dra, ddec
def updatezScaling(self): if (str(self.ui.comboBoxScaling.currentText()) == 'ZScale'): self.z0, self.z1 = zscale(self.data2D) if (str(self.ui.comboBoxScaling.currentText()) == 'Min/Max'): self.z0 = np.min(self.data2D) self.z1 = np.max(self.data2D) if (str(self.ui.comboBoxScaling.currentText()) == 'Manual'): self.z0 = float(self.ui.lineEditz0.text()) self.z1 = float(self.ui.lineEditz1.text()) self.logger.debug("updating scaling") self.ui.lineEditz0.setText("%.3f" % (self.z0)) self.ui.lineEditz1.setText("%.3f" % (self.z1)) # self.displayRaw2D() try: self.display2D() self.plotProfile() except: pass
def updatezScaling(self): if(str(self.ScalingCombo.currentText())=='ZScale'): self.z0,self.z1=zscale(self.data2D) if(str(self.ScalingCombo.currentText())=='Min/Max'): self.z0=np.min(self.data2D) self.z1=np.max(self.data2D) if(str(self.ScalingCombo.currentText())=='Manual'): self.z0=float(self.z0Fill.text()) self.z1=float(self.z1Fill.text()) if (idebugGUI): print "updating scaling" self.z0Fill.setText("%.3f"%(self.z0)) self.z1Fill.setText("%.3f"%(self.z1)) self.displayRaw2D() try: self.displaySS2D() self.plotProfile() except: pass
def plot_image(infile, outfile, ext): print(infile, outfile, ext) fig = plt.figure(figsize=(10, 10)) ax = fig.gca() fig.suptitle('%s' % (outfile), fontsize=15) try: hdu = fits.open(infile) except: print("cannot open file!!! ") #print hdu[0].header image_data = hdu[ext].data #ax.axes.get_xaxis().set_visible(False) #ax.axes.get_yaxis().set_visible(False) image_data[image_data < 0] = 1e-20 clow, chigh = zs.zscale(image_data, nsamples=2000) # percentage ''' clow = np.percentile(image_data,50) chigh = np.percentile(image_data,60) ''' #plt.imshow(image_data, cmap='afmhot',interpolation='none', norm=LogNorm(),clim=[clow,chigh]) plt.imshow(image_data, cmap='binary', interpolation='none', clim=[clow, chigh], origin='lower') #plt.imshow(image_data, cmap='gray',interpolation='nearest', clim=[clow,chigh]) #fig.set_tight_layout(True) plt.savefig(outfile, pad_inches=0.)
from astropy.io import fits from zscale import zscale ''' Codigo para HDU1 ''' master_bias = fits.getdata("../master/master_bias.fits", 1) master_flats = fits.getdata("../master/master_flats.fits", 1) sci1 = fits.getdata("../data/dat.025.fits", 1) # Get HDU1 sci11 = sp.divide(sci1 - master_bias, master_flats) mn, mx = zscale(sci11) plt.clf() plt.imshow(sci11, vmin=mn, vmax=mx) plt.show() x = 340#411 y = 543#1838 SR = 45 # 30 """ # second : x = 311 y = 1451 SR = 30
def PDF(cutout): """ Create a pdf file with the plot of your objects in different filters Parameters ---------- cutout: list array of images N filt x N sources """ # create the pdf pdfOut = PdfPages(dirpath + 'stampPDF.pdf') # it's a sum to derive the good number of stamp p = 0 # Placement of the Id sizeId = cutout[0][0].shape # while p < np.size(cutout[0]) - 1: print(p) if np.size(cutout[0]) - p > 10: fig, axs = plt.subplots(10, len(cutout), figsize=(8.3, 11.7)) else: fig, axs = plt.subplots(np.size(cutout[0]) - p, len(cutout), figsize=(8.3, 11.7)) # Loop over the 10 sources to be include in one PDF page for k in range(10): # id of object axs[k, 0].text(-sizeId[0], sizeId[0] / 2, tbl['id'][p]) # Loop over the filters for j in range(len(cutout)): # Display the image mappa = axs[k, j].imshow(cutout[j][p].data, cmap='afmhot', origin='lower', interpolation='nearest') axs[k, j].set_title(filters[j], fontsize=5, pad=2.5) axs[k, j].get_xaxis().set_visible(False) axs[k, j].get_yaxis().set_visible(False) # Plot circular aperture at the coordinate of your object apertures = SkyCircularAperture(tbl['skycoord'][p], r=1.5 * u.arcsec) aperturesPix = apertures.to_pixel(cutout[j][p].wcs) aperturesPix.plot(color='cyan', ax=axs[k, j], lw=1, alpha=0.5) # DS9 zscale zrange = zscale.zscale(cutout[j][p].data) mappa.set_clim(zrange) # it's a sum to derive the good number of stamp if p < np.size(cutout[0]) - 1: p = p + 1 else: break # save the page plt.savefig(pdfOut, format='pdf') pdfOut.close() return
def stack(cutout, radiusStack, gain, zeroPoint, wavelenght): """ Print out pdf with stamp stacking and plot graph magnitude in function of wavelenght Parameters ---------_ cutout: list array of images N filt x N sources radiusStack: float radius in pixel zeroPoint: float Zero point of your image gain: float The gain of your image wavelenght: float wavelenght of each filters """ # define the resulting stacked image as a cutout with the same dimension of the input cutouts o = 0 # Create pdf where are storing stack stamp and graph pdfOut = PdfPages(dirpath + 'stacking.pdf') fig, axs = plt.subplots(1, len(filters), figsize=(5, 5)) # Saving magnitude and error mag = [[0.0 for i in range(len(filters))] for j in range(2)] for i in cutout: # i is a list of galaxies print('Photometry ' + filters[o]) # Assuming that the shape is the same for all galaxies #this is now a tuple, e.g. (25,25), so the code will work also for rectangular stamps sizeStack = i[0].shape #this is now a tuple, e.g. (25,25), so the code will work also for rectangular stamps print(sizeStack) #just for testing stackImg = np.zeros(sizeStack) #LOOP over pixels: (I changed a lot here) # fait tous les pixel x for x in range(sizeStack[0]): # fait tous les pixel y for y in range(sizeStack[1]): # for the given pixels, collect the flux from all stamps into a list pxl = [] #use an empty list, but it can be also np.zeros() for stamp in i: pxl.append(stamp.data[x, y]) # caluclate the median: stackImg[x, y] = np.median(pxl) axs[o].set_title(filters[o], fontsize=5, pad=2.5) axs[o].get_xaxis().set_visible(False) axs[o].get_yaxis().set_visible(False) mappa = axs[o].imshow(stackImg, cmap='afmhot', origin='lower', interpolation='nearest') zrange = zscale.zscale(stackImg) mappa.set_clim(zrange) # Mask sources mask = make_source_mask(stackImg, snr=1, npixels=3, dilate_size=3) # Derive the background and the rms image bkg = Background2D( stackImg, int(sizeStack[0] / 10), filter_size=1, sigma_clip=None, bkg_estimator=SExtractorBackground(SigmaClip(sigma=2.5)), bkgrms_estimator=StdBackgroundRMS(SigmaClip(sigma=2.5)), exclude_percentile=90, mask=mask) if gain[o] == 0.: gain[o] = 1000000000000000000000000000. # Calculate the total error error = calc_total_error(stackImg, bkg.background_rms, gain[o]) # Define a cicularAperture in the wcs position of your objects apertures = CircularAperture( (int(sizeStack[0] / 2), int(sizeStack[1] / 2)), r=radiusStack[o]) # Derive the flux and error flux photometry = aperture_photometry(stackImg - bkg.background, apertures, error=error) # Saving magnitude and error mag[0][o] = -2.5 * np.log10( photometry['aperture_sum'][0]) + zeroPoint[o] mag[1][o] = 1.0857 * (photometry['aperture_sum_err'][0] / photometry['aperture_sum'][0]) o = o + 1 plt.savefig(pdfOut, format='pdf') # Plot plt.clf() y = mag[0] x = wavelenght plt.plot(x, y, 'ro') xlimL = wavelenght[0] - 1000 xlimR = wavelenght[len(wavelenght) - 1] + 1000 plt.xlim(xlimL, xlimR) plt.ylim(32, 22) plt.errorbar(wavelenght, mag[0], yerr=mag[1], fmt='none', capsize=10, ecolor='blue', zorder=1) plt.xlabel('wavelenght(Å)') plt.ylabel('magnitude') plt.title('') plt.savefig(pdfOut, format='pdf') pdfOut.close() np.savetxt(dirpath + 'fluxStack.txt', mag, header='Ligne1: mag, Ligne2: mag_err') return
def get_offsets_A_B(f, plot=False, interactive=False): ''' Returns the offsets for A and B, so that when offseting, the images do not overlap. Example fits:/scr2/nblago/Projects/SEDM/data/finders/f_b_a_rPTF15fks_r.fits ''' from scipy import stats image = pf.open(f) data = image[0].data.T wcs = pywcs.WCS(image[0].header) ra, dec = cc.hour2deg(image[0].header['OBJRA'], image[0].header['OBJDEC']) obj = fitsutils.get_par(f, "OBJECT") pra, pdec = wcs.wcs_sky2pix(ra, dec, 0) #Get a local image xl, yl = np.round(wcs.wcs_sky2pix(ra + 30. / 3600, dec - 30. / 3600, 0), 0) xu, yu = np.round(wcs.wcs_sky2pix(ra - 30. / 3600, dec + 30. / 3600, 0), 0) imageloc = image[0].data[xl:xu, yu:yl] bkg = np.median(imageloc) perc10 = np.percentile(imageloc, 15) perc90 = np.percentile(imageloc, 85) mask = (image[0].data > perc10) * (image[0].data < perc90) bkg_std = np.std(image[0].data[mask]) # mad(image[0].data) linestyles = ['solid', 'dashed', 'dashdot', 'dotted', 'solid'] offsets = np.array([[+3, -4], [+3, +4], [+2, +4], [+2, +2]]) Noff = len(offsets) pvalues = np.zeros((Noff, 2)) if (plot): fig, axarr = plt.subplots(2, Noff, sharey='row', figsize=(6 * len(offsets), 12)) axarr = np.array(axarr, ndmin=1) for i, off in enumerate(offsets): ra_off = ra - off[0] / 3600. dec_off = dec - off[1] / 3600. ra_off_op = ra + off[0] / 3600. dec_off_op = dec + off[1] / 3600. prao, pdeco = wcs.wcs_sky2pix(ra + off[0] / 3600., dec + off[1] / 3600., 0) praosym, pdecosym = wcs.wcs_sky2pix(ra - 2 * off[0] / 3600., dec - 2 * off[1] / 3600., 0) #Extract sample window to check wether the background matches well. #number on samples on each side ns = 7 sample = data[prao - ns:prao + ns, pdeco - ns:pdeco + ns] sm = np.median(sample) sstd = np.std(sample[(sample < 6000) * (sample > 0)]) bkg_prob = np.minimum(1 - stats.norm.cdf(sm, bkg, bkg_std), stats.norm.cdf(sm, bkg, bkg_std)) #Extract sample window to check wether the background matches well. sample = data[praosym - 7:praosym + 7, pdecosym - 7:pdecosym + 7] smo = np.median(sample) sstdo = np.std(sample) bkg_probo = np.minimum(1 - stats.norm.cdf(smo, bkg, bkg_std), stats.norm.cdf(smo, bkg, bkg_std)) pvalues[i] = np.array([bkg_prob, bkg_probo]) if (plot): #Retrieve the image of the object xl, yl = wcs.wcs_sky2pix(ra + 20. / 3600, dec - 20. / 3600, 0) xu, yu = wcs.wcs_sky2pix(ra - 20. / 3600, dec + 20. / 3600, 0) ifuwin = data[xl:xu, yu:yl] #Retrieve the offset A image of the object x0, y0 = wcs.wcs_sky2pix(ra_off + 20. / 3600, dec_off - 20. / 3600, 0) x1, y1 = wcs.wcs_sky2pix(ra_off - 20. / 3600, dec_off + 20. / 3600, 0) ifuwin1 = data[x0:x1, y1:y0] nx, ny = ifuwin1.shape #print nx,ny, ifuwin1.shape #Retrieve the offset A image of the object x0, y0 = wcs.wcs_sky2pix(ra_off_op + 20. / 3600, dec_off_op - 20. / 3600, 0) x1, y1 = wcs.wcs_sky2pix(ra_off_op - 20. / 3600, dec_off_op + 20. / 3600, 0) ifuwin2 = data[x0:x0 + nx, y1:y1 + ny] #Plot the A and B zmin, zmax = zscale.zscale(ifuwin) zmin1, zmax1 = zscale.zscale(ifuwin1 - ifuwin2) axarr[0, i].imshow(ifuwin.T, aspect="auto", vmin=zmin, vmax=zmax, extent=(20, -20, -20, 20), alpha=0.5) axarr[1, i].imshow(ifuwin1.T - ifuwin2.T, aspect="auto", vmin=zmin1, vmax=zmax1, extent=(20, -20, -20, 20), alpha=0.5) #, cmap=matplotlib.cm.RdBu_r) #axarr[1, i].imshow(-1 * ifuwin2.T, aspect="auto", vmin=zmin2, vmax=zmax2, extent=(20,-20,-20,20), alpha=0.5)#, cmap=matplotlib.cm.RdBu_r) axarr[0, i].scatter(0, 0, marker="x", s=20) axarr[1, i].scatter(0, 0, marker="x", s=20) axarr[0, i].set_xlabel("OBJRA") axarr[0, i].set_ylabel("OBJDEC") axarr[1, i].set_xlabel("OBJRA") axarr[1, i].set_ylabel("OBJDEC") axarr[0, i].text( 19, 17, "Red stats: $\mu=$%.2f, $\sigma=$%.2f, p-value=%.5f" % (sm, sstd, bkg_prob), bbox=dict(facecolor='white', alpha=0.5)) axarr[0, i].text( 19, 14, "Blue stats: $\mu=$%.2f, $\sigma=$%.2f, p-value=%.5f" % (smo, sstdo, bkg_probo), bbox=dict(facecolor='white', alpha=0.5)) axarr[0, i].text(19, 11, "Background stats: $\mu=$%.2f, $\sigma=$%.2f" % (bkg, bkg_std), bbox=dict(facecolor='white', alpha=0.5)) #r = plt.Circle((prao-pra, pdeco-pdec), 5, facecolor="none", edgecolor="red", lw=2) #b = plt.Circle((praosym-pra, pdecosym-pdec), 5, facecolor="none", edgecolor="blue", lw=2) r = plt.Circle((2 * off[0], +2 * off[1]), 2, facecolor="none", edgecolor="red", lw=3, ls=linestyles[i]) b = plt.Circle((-2 * off[0], -2 * off[1]), 2, facecolor="none", edgecolor="blue", lw=3, ls=linestyles[i]) #Plot the true location of the IFU patches = [] Path = mpath.Path path_data = [(Path.MOVETO, [20, -12]), (Path.LINETO, [20, 20]), (Path.LINETO, [-13, 13]), (Path.LINETO, [-20, -18]), (Path.CLOSEPOLY, [20, 10])] codes, verts = zip(*path_data) path = mpath.Path(verts, codes) patch = mpatches.PathPatch(path) patches.append(patch) collection = PatchCollection(patches, cmap=plt.cm.YlGn, alpha=0.2) colors = np.linspace(0, 1, len(patches)) collection.set_array(np.array(colors)) axarr[0, i].add_artist(r) axarr[0, i].add_artist(b) axarr[1, i].add_collection(collection) plt.suptitle(obj, fontsize=20) prod = np.prod(pvalues, axis=1) if (plot and interactive): axarr[0, np.argmax(prod)].text(0, 0, "WINNER") plt.show() elif (plot): axarr[0, np.argmax(prod)].text(0, 0, "WINNER") plt.savefig( os.path.join( os.path.dirname(f).replace("raw", "phot"), os.path.basename(f).replace(".fits", "_ab.png"))) plt.clf() return 0, offsets[np.argmax(prod)], -2 * offsets[np.argmax(prod)]
def get_offset_center(f, plot=False, interactive=False): ''' Given a fits image, returns the offset in Ra, DEC, that needs to be applied for the telescope tp go from the current pointing position, to the coodinates of the object specified in the fits file. ''' if (not os.path.isfile(f)): print "File %s does not exist! Returning Zero offsets..." % f return -1, 0, 0 else: image = pf.open(f) wcs = pywcs.WCS(image[0].header) rra, rdec = cc.hour2deg(image[0].header['OBJRA'], image[0].header['OBJDEC']) x, y = np.round(wcs.wcs_sky2pix(rra, rdec, 0), 0) pra, pdec = wcs.wcs_pix2sky(np.array([[1293., 1280.]], np.float_), 0)[0] dra, ddec = cc.get_offset(pra, pdec, rra, rdec) xl, yu = np.round( wcs.wcs_sky2pix(rra + 90. / 3600, rdec - 90. / 3600, 0), 0) xu, yl = np.round( wcs.wcs_sky2pix(rra - 90. / 3600, rdec + 90. / 3600, 0), 0) imageloc = image[0].data.T[xl:xu, yl:yu] if imageloc.shape[0] == 0 or imageloc.shape[1] == 0: logger.warn( "Astrometry has FAILED on this! The object is outside the frame! Resending to the numb astrometric solution" ) logger.error( "Astrometry has FAILED on this! The object is outside the frame! Resending to the numb astrometric solution" ) print "Pixels are", xl, xu, yl, yu try: code, dra, ddec = get_offset_center_failed_astro( f, plot=plot, interactive=interactive) return 2, dra, ddec except: return -1, 0, 0 if (plot): plt.figure(figsize=(8, 8)) zmin, zmax = zscale.zscale(imageloc) #print zmin, zmax, imageloc, (xl,xu,yl,yu) obj = fitsutils.get_par(f, "OBJECT") plt.suptitle(obj, fontsize=20) plt.imshow(imageloc.T, extent=(xl[0], xu[0], yl[0], yu[0]), aspect="equal", interpolation="none", origin="lower", vmin=zmin, vmax=zmax) plt.plot(1293., 1280., "ws", ms=7, label="Current pointing") plt.plot(x, y, "b*", ms=10, label="Target pointing") plt.gca().invert_xaxis() plt.legend() if (interactive): plt.show() else: plt.savefig( os.path.join( os.path.dirname(f).replace("raw", "phot"), os.path.basename(f).replace(".fits", "_a.png"))) plt.clf() return 0, dra, ddec
def get_offset_center_failed_astro(f, plot=False, interactive=True): ''' For fields where astrometry is challenging, there is a simple solution. Find the brightest peak within the pointing error of the telescope. As this fields will usually be centered in Standard stars and very short exposure time, the fit intends to ''' image = pf.open(f) data = image[0].data wcs = pywcs.WCS(image[0].header) ra, dec = cc.hour2deg(image[0].header['OBJRA'], image[0].header['OBJDEC']) pra, pdec = wcs.wcs_sky2pix(ra, dec, 0) #Get a local image #xl, yl = np.array(wcs.wcs_sky2pix(ra+(60./3600)*np.cos(np.deg2rad(dec)), dec-60./3600, 0), dtype=np.int) #xu, yu = np.array(wcs.wcs_sky2pix(ra-(60./3600)*np.cos(np.deg2rad(dec)), dec+60./3600, 0), dtype=np.int) imageloc = image[0].data.T[1293 - 150:1293 + 150, 1280 - 150:1280 + 150] nx = 300 ny = 300 def_x = np.argmax(np.sum(imageloc, axis=0)) def_y = np.argmax(np.sum(imageloc, axis=1)) newx = pra - nx / 2. + def_x newy = pdec - ny / 2. + def_y pra, pdec = wcs.wcs_pix2sky(np.array([[newx[0], newy[0]]], np.float_), 0)[0] dra, ddec = cc.get_offset(ra, dec, pra, pdec) print "Offset", dra, ddec, "Position RA,DEC", pra, pdec x, y, fwhmx, fwhmy, bkg, amp = fit_utils.fit_gauss(imageloc) if (plot): plt.figure(figsize=(8, 8)) obj = fitsutils.get_par(f, "OBJECT") plt.suptitle(obj, fontsize=20) zmin, zmax = zscale.zscale(imageloc) plt.imshow(imageloc, aspect="auto", interpolation="none", origin="lower", vmin=zmin, vmax=zmax, extent=(0, +300, 0, +300)) plt.plot(x, y, "go", ms=20, label="Centroid using gaussiuan fit.") plt.plot(def_x, def_y, "b*", ms=20, label="Centroid using max/min.") plt.plot(150, 150, "wo", ms=20, label="Initial pointing") plt.legend() '''zmin, zmax = zscale.zscale(data) plt.imshow(data, aspect="auto", interpolation="none", origin="lower", vmin=zmin, vmax=zmax) plt.plot(newx, newy, "go") plt.show()''' if (interactive): plt.show() else: plt.savefig( os.path.join( os.path.dirname(f).replace("raw", "phot"), os.path.basename(f).replace(".fits", "_std.png"))) plt.clf() return 1, ddec, dra
def display(self, pix, name=None, bufname=None, z1=None, z2=None, transform=None, zscale=False, contrast=0.25, scale=None, offset=None, frame=None,quiet=False): """ Displays byte-scaled (UInt8) n to XIMTOOL device. This method uses the IIS protocol for displaying the data to the image display device, which requires the data to be byte-scaled. If input is not byte-scaled, it will perform scaling using set values/defaults. """ #Ensure that the input array 'pix' is a numpy array pix = n.array(pix) self.z1 = z1 self.z2 = z2 # If any of the display parameters are specified here, apply them #if z1 or z2 or transform or scale or offset or frame: # If zscale=True (like IRAF's display) selected, calculate z1 and z2 from # the data, and clear any transform specified in the call # Offset and scale arew applied to the data and z1,z2, so they have no effect # on the display if zscale: if transform != None: if not quiet: print "transform disallowed when zscale=True" transform = None z1, z2 = _zscale.zscale(pix, contrast=contrast) self.set(frame=frame, z1=z1, z2=z2, transform=transform, scale=scale, offset=offset) # Initialize the display device if not self.view._display or self.view.checkDisplay() is False: self.open() _d = self.view._display self.handle = _d.getHandle() # If no user specified values are provided, interrogate the array itself # for the full range of pixel values if self.z1 == None: self.z1 = n.minimum.reduce(n.ravel(pix)) if self.z2 == None: self.z2 = n.maximum.reduce(n.ravel(pix)) # If the user has not selected a specific buffer for the display, # select and set the frame buffer size based on input image size. if bufname == 'iraf': useiraf = True bufname = None else: useiraf = False if bufname != None: _d.setFBconfig(None,bufname=bufname) else: _ny,_nx = pix.shape _d.selectFB(_nx,_ny,reset=1,useiraf=useiraf) # Initialize the specified frame buffer _d.setFrame(self.frame) _d.eraseFrame() # Apply user specified scaling to image, returns original # if none are specified. bpix = self._transformImage(pix) # Recompute the pixel range of (possibly) transformed array _z1 = self._transformImage(self.z1) _z2 = self._transformImage(self.z2) # If there was a problem in the transformation, then restore the original # array as the one to be displayed, even though it may not be ideal. if _z1 == _z2: if not quiet: print 'Error encountered during transformation. No transformation applied...' bpix = pix self.z1 = n.minimum.reduce(n.ravel(bpix)) self.z2 = n.maximum.reduce(n.ravel(bpix)) # Failsafe in case input image is flat: if self.z1 == self.z2: self.z1 -= 1. self.z2 += 1. else: # Reset z1/z2 values now so that image gets displayed with # correct range. Also, when displaying transformed images # this allows the input pixel value to be displayed, rather # than the transformed pixel value. self.z1 = _z1 self.z2 = _z2 _wcsinfo = displaydev.ImageWCS(bpix,z1=self.z1,z2=self.z2,name=name) if not quiet: print 'Image displayed with Z1: ',self.z1,' Z2:',self.z2 bpix = self._fbclipImage(bpix,_d.fbwidth,_d.fbheight) # Update the WCS to match the frame buffer being used. _d.syncWCS(_wcsinfo) # write out WCS to frame buffer, then erase buffer _d.writeWCS(_wcsinfo) # Now, send the trimmed image (section) to the display device _d.writeImage(bpix,_wcsinfo)
def get_offsets_A_B(f, plot=False, interactive=False): ''' Returns the offsets for A and B, so that when offseting, the images do not overlap. Example fits:/scr2/nblago/Projects/SEDM/data/finders/f_b_a_rPTF15fks_r.fits ''' from scipy import stats image = pf.open(f) data = image[0].data.T wcs = pywcs.WCS(image[0].header) ra, dec = cc.hour2deg(image[0].header['OBJRA'], image[0].header['OBJDEC'] ) obj = fitsutils.get_par(f, "OBJECT") pra, pdec = wcs.wcs_sky2pix(ra, dec, 0) #Get a local image xl, yl = np.round(wcs.wcs_sky2pix(ra+30./3600, dec-30./3600, 0), 0) xu, yu = np.round(wcs.wcs_sky2pix(ra-30./3600, dec+30./3600, 0), 0) imageloc = image[0].data[xl:xu,yu:yl] bkg = np.median(imageloc) perc10 = np.percentile(imageloc, 15) perc90 = np.percentile(imageloc, 85) mask = (image[0].data > perc10) * (image[0].data < perc90) bkg_std = np.std(image[0].data[mask])# mad(image[0].data) linestyles = ['solid' , 'dashed' , 'dashdot' , 'dotted', 'solid'] offsets = np.array([[+3, -4], [+3, +4], [+2, +4], [+2, +2]]) Noff = len(offsets) pvalues = np.zeros((Noff, 2)) if (plot): fig, axarr = plt.subplots(2, Noff, sharey='row', figsize=(6*len(offsets), 12)) axarr = np.array(axarr, ndmin=1) for i, off in enumerate(offsets): ra_off = ra - off[0]/3600. dec_off = dec - off[1]/3600. ra_off_op = ra + off[0]/3600. dec_off_op = dec + off[1]/3600. prao, pdeco = wcs.wcs_sky2pix(ra+ off[0]/3600., dec + off[1]/3600., 0) praosym, pdecosym = wcs.wcs_sky2pix(ra- 2*off[0]/3600., dec - 2*off[1]/3600., 0) #Extract sample window to check wether the background matches well. #number on samples on each side ns = 7 sample = data[prao-ns:prao+ns, pdeco-ns:pdeco+ns] sm = np.median(sample) sstd = np.std(sample[(sample<6000)*(sample>0)]) bkg_prob = np.minimum(1 - stats.norm.cdf(sm, bkg, bkg_std), stats.norm.cdf(sm, bkg, bkg_std)) #Extract sample window to check wether the background matches well. sample = data[praosym-7:praosym+7, pdecosym-7:pdecosym+7] smo = np.median(sample) sstdo = np.std(sample) bkg_probo = np.minimum(1 - stats.norm.cdf(smo, bkg, bkg_std), stats.norm.cdf(smo, bkg, bkg_std)) pvalues[i] = np.array([bkg_prob, bkg_probo]) if(plot): #Retrieve the image of the object xl, yl = wcs.wcs_sky2pix(ra+20./3600, dec-20./3600, 0) xu, yu = wcs.wcs_sky2pix(ra-20./3600, dec+20./3600, 0) ifuwin = data[xl:xu,yu:yl] #Retrieve the offset A image of the object x0, y0 = wcs.wcs_sky2pix(ra_off+20./3600, dec_off-20./3600, 0) x1, y1 = wcs.wcs_sky2pix(ra_off-20./3600, dec_off+20./3600, 0) ifuwin1 = data[x0:x1,y1:y0] nx, ny = ifuwin1.shape #print nx,ny, ifuwin1.shape #Retrieve the offset A image of the object x0, y0 = wcs.wcs_sky2pix(ra_off_op+20./3600, dec_off_op-20./3600, 0) x1, y1 = wcs.wcs_sky2pix(ra_off_op-20./3600, dec_off_op+20./3600, 0) ifuwin2 = data[x0:x0+nx,y1:y1+ny] #Plot the A and B zmin, zmax = zscale.zscale(ifuwin) zmin1, zmax1 = zscale.zscale(ifuwin1-ifuwin2) axarr[0, i].imshow(ifuwin.T, aspect="auto", vmin=zmin, vmax=zmax, extent=(20,-20,-20,20), alpha=0.5) axarr[1, i].imshow(ifuwin1.T-ifuwin2.T, aspect="auto", vmin=zmin1, vmax=zmax1, extent=(20,-20,-20,20), alpha=0.5)#, cmap=matplotlib.cm.RdBu_r) #axarr[1, i].imshow(-1 * ifuwin2.T, aspect="auto", vmin=zmin2, vmax=zmax2, extent=(20,-20,-20,20), alpha=0.5)#, cmap=matplotlib.cm.RdBu_r) axarr[0, i].scatter(0, 0, marker="x", s=20) axarr[1, i].scatter(0, 0, marker="x", s=20) axarr[0, i].set_xlabel("OBJRA") axarr[0, i].set_ylabel("OBJDEC") axarr[1, i].set_xlabel("OBJRA") axarr[1, i].set_ylabel("OBJDEC") axarr[0, i].text(19, 17, "Red stats: $\mu=$%.2f, $\sigma=$%.2f, p-value=%.5f"%(sm, sstd, bkg_prob), bbox=dict(facecolor='white', alpha=0.5)) axarr[0, i].text(19, 14, "Blue stats: $\mu=$%.2f, $\sigma=$%.2f, p-value=%.5f"%(smo, sstdo, bkg_probo), bbox=dict(facecolor='white', alpha=0.5)) axarr[0, i].text(19, 11, "Background stats: $\mu=$%.2f, $\sigma=$%.2f"%( bkg, bkg_std), bbox=dict(facecolor='white', alpha=0.5)) #r = plt.Circle((prao-pra, pdeco-pdec), 5, facecolor="none", edgecolor="red", lw=2) #b = plt.Circle((praosym-pra, pdecosym-pdec), 5, facecolor="none", edgecolor="blue", lw=2) r = plt.Circle((2*off[0], + 2*off[1]), 2, facecolor="none", edgecolor="red", lw=3, ls=linestyles[i]) b = plt.Circle((-2*off[0], -2*off[1]), 2, facecolor="none", edgecolor="blue", lw=3, ls=linestyles[i]) #Plot the true location of the IFU patches = [] Path = mpath.Path path_data = [ (Path.MOVETO, [20, -12]), (Path.LINETO, [20, 20]), (Path.LINETO, [-13, 13]), (Path.LINETO, [-20 , -18]), (Path.CLOSEPOLY, [20, 10]) ] codes, verts = zip(*path_data) path = mpath.Path(verts, codes) patch = mpatches.PathPatch(path) patches.append(patch) collection = PatchCollection(patches, cmap=plt.cm.YlGn, alpha=0.2) colors = np.linspace(0, 1, len(patches)) collection.set_array(np.array(colors)) axarr[0, i].add_artist(r) axarr[0, i].add_artist(b) axarr[1, i].add_collection(collection) plt.suptitle(obj, fontsize=20) prod = np.prod(pvalues, axis=1) if (plot and interactive): axarr[0, np.argmax(prod)].text(0,0,"WINNER") plt.show() elif(plot): axarr[0, np.argmax(prod)].text(0,0,"WINNER") plt.savefig(os.path.join(os.path.dirname(f).replace("raw", "phot"), os.path.basename(f).replace(".fits", "_ab.png"))) plt.clf() return 0, offsets[np.argmax(prod)], -2*offsets[np.argmax(prod)]
def show_list(fits_fns, nx=5, ny=3, size_mult=3.2, zoom_lvl=None, fontsize=8): """ Written by John Capone ([email protected]). Purpose: displays images in specified list file. Input: fits_fns: list of file names nx: number of images to display simultaneously in x ny: number of images to display simultaneously in y size_mult: multiple to determine image sizes zoom_lvl: amount to decrease image resolution by (to save memory) fontsize: fontsize for plots Usage: 1) enter python or ipython environment 2) load function -> 'from rat_preproc import show_list' 3) run function -> 'show_list(fits_fns)' - decreasing zoom_lvl (i.e. from 0.5 to 0.1) decreases the size of the displayed image, thus decreasing the amount of memory required 4) function will display arrays of images in list file for inspection by user Notes: - added escape character - user can now change font size """ nx = int(nx); ny = int(ny) # force parameter types to int if type(fits_fns) not in [list, dict]: print_err("Error: fits_fns must be a list or dictionary of fits file names. Exiting...") return if type(fits_fns) is dict: # convert dictionary to list fits_fns = list(itertools.chain.from_iterable(fits_fns.values())) nfits = len(fits_fns) # number of fits files listed pl.ion() # pylab in interactive mode # create figures of subplots for review nfigs = int(np.ceil(nfits / float(nx * ny))) fig = pl.figure(figsize=(nx*size_mult,ny*size_mult), tight_layout=True) for i in range(nfigs): start_fits = i*nx*ny if (i + 1)*nx*ny <= nfits: stop_fits = (i + 1)*nx*ny - 1 nsubplts = nx*ny else: stop_fits = nfits nsubplts = nfits - start_fits print "Displaying frames {} - {} ({} total).".format(start_fits, stop_fits, nfits) # display image in each subplot for j in range(nsubplts): ax = fig.add_subplot(ny, nx, j+1) # new subplot fits_fn = fits_fns[start_fits + j] dpath, fits_fn = os.path.split(fits_fn) if len(dpath) != 0: dpath += '/' temp = fits_fn.split('.') if len(temp) == 1: fits_fn += '.fits' elif len(temp) == 2: if temp[1].lower() != 'fits': print_err("Error: invalid \"{}\" file type detected. Should be \"fits\" file type. Exiting...".format(temp[1])) pl.close('all') # close image to free memory return else: print_err("Error: file names should not include \".\" except for file type extention. Exiting...") pl.close('all') # close image to free memory return fits_id = temp[0] # fits file name with extention removed # open data hdulist = pf.open(dpath + fits_fn) im = hdulist[0].data h = hdulist[0].header hdulist.close() # close FITs file # display if zoom_lvl is not None: imdisp = zoom(im, zoom_lvl) else: imdisp = im z1, z2 = zscale(im) ax.imshow(imdisp, vmin=z1, vmax=z2, origin='lower', cmap=pl.cm.gray, interpolation='none') ax.set_xticks([]) ax.set_yticks([]) try: ax.set_title("{} - {} filter".format(fits_id, h['FILTER']), fontsize=fontsize) # title with identifier except: ax.set_title("{}".format(fits_id), fontsize=fontsize) # title with identifier fig.canvas.draw() usr_select = raw_input("Press any key to continue or \"q\" to quit: ") # prompt user to continue fig.clear() # clear image if usr_select.lower() == 'q': print_bold("Exiting...") pl.close('all') # close image to free memory return pl.close('all') # close image to free memory
def get_app_phot(coords, image, plot_only=False, store=True, wcsin="world", fwhm=2.5, plotdir=None, box=15, arcsecpix=0.394): ''' coords: files: wcsin: can be "world", "logic" ''' # Load packages; splot is in the onedspec package, which is in noao. # The special keyword _doprint=0 turns off displaying the tasks # when loading a package. from pyraf import iraf if (not plot_only): iraf.noao(_doprint=0) iraf.digiphot(_doprint=0) iraf.apphot(_doprint=0) iraf.unlearn("apphot") imdir = os.path.dirname(image) imname = os.path.basename(image) if (plotdir is None): plotdir = os.path.join(imdir, "photometry") if not os.path.isdir(plotdir): os.makedirs(plotdir) out_name = os.path.join(plotdir, imname + ".seq.mag") clean_name = os.path.join(plotdir, imname + ".app.mag") print "Will create output files", out_name, clean_name # Read values from .ec file fwhm_value = fwhm/arcsecpix if (fitsutils.has_par(image, 'FWHM')): fwhm_value = fitsutils.get_par(image, 'FWHM')/arcsecpix elif (fwhm is None): fwhm_value=3.5/arcsecpix if (fitsutils.has_par(image, 'AIRMASS')): airmass_value = fitsutils.get_par(image, 'AIRMASS') else: airmass_value = 1.3 exptime = fitsutils.get_par(image, 'EXPTIME') gain = fitsutils.get_par(image, 'GAIN') noise = fitsutils.get_par(image, 'RDNOISE') print "FWHM: %.1f pixels, %.1f arcsec"%(fwhm_value, fwhm_value*arcsecpix) aperture_rad = math.ceil(float(fwhm_value)*1.5) # Set aperture radius to three times the PSF radius sky_rad= math.ceil(aperture_rad)*4 if (not plot_only): iraf.noao.digiphot.apphot.qphot(image = image,\ cbox = box ,\ annulus = sky_rad ,\ dannulus = 20. ,\ aperture = str(aperture_rad),\ coords = coords ,\ output = out_name ,\ plotfile = "" ,\ zmag = 0. ,\ exposure = "exptime",\ airmass = "airmass" ,\ filter = "filter" ,\ obstime = "DATE" ,\ #fwhm = fwhm_value,\ epadu = gain ,\ interactive = "no" ,\ radplots = "yes" ,\ verbose = "no" ,\ graphics = "stdgraph" ,\ display = "stdimage" ,\ icommands = "" ,\ wcsin = wcsin, wcsout = "logical", gcommands = "") #iraf.noao.digiphot.apphot.phot(image=image, cbox=5., annulus=12.4, dannulus=10., salgori = "centroid", aperture=9.3,wcsin="world",wcsout="tv", interac = "no", coords=coords, output=out_name) iraf.txdump(out_name, "id,image,xcenter,ycenter,xshift,yshift,fwhm,msky,stdev,cier,rapert,sum,area,nsky,flux,itime,mag,merr", "yes", Stdout=clean_name) ma = np.genfromtxt(clean_name, comments="#", dtype=[("id","<f4"), ("image","|S20"), ("X","<f4"), ("Y","<f4"), ("Xshift","<f4"), ("Yshift","<f4"),("fwhm","<f4"), ("msky","<f4"), ("stdev","<f4"),\ ("flags", np.int), ("rapert", "<f4"), ("sum", "<f4"), ("area", "<f4"), ("nsky","<f4") , ("flux", "<f4"), ("itime", "<f4"), ("fit_mag","<f4"), ("fiterr","<f4")]) if (ma.size > 0): m = ma[~np.isnan(ma["fit_mag"])] else: print "Only one object found!" m = np.array([ma]) hdulist = pf.open(image) prihdr = hdulist[0].header img = hdulist[0].data * 1. nx, ny = img.shape dimX = int(np.floor(np.sqrt(len(m)))) dimY = int(np.ceil(len(m)*1./dimX)) outerrad = sky_rad+10 cutrad = outerrad + 15 print "Cutrad %.1f"%cutrad plt.suptitle("FWHM=%.2f arcsec. %d stars"%(fwhm_value*arcsecpix, len(m))) for i in np.arange(dimX): for j in np.arange(dimY): if ( i*dimY + j < len(m)): k = i*dimY + j #print dimX, dimY, i, j, k ax = plt.subplot2grid((dimX,dimY),(i, j)) y1, y2, x1, x2 = m[k]["X"]-cutrad, m[k]["X"]+cutrad, m[k]["Y"]-cutrad, m[k]["Y"]+cutrad y1, y2, x1, x2 = int(y1), int(y2), int(x1), int(x2) y1 = np.maximum(y1, 0); y2=np.maximum(y2, 0); x1=np.maximum(x1, 0); x2 = np.maximum(x2, 0) try: zmin, zmax = zscale.zscale(img[x1:x2,y1:y2], nsamples=1000, contrast=0.25) except ValueError: print y1, y2, x1, x2 print img[x1:x2,y1:y2] sh= img[x1:x2,y1:y2].shape if sh[0]>0 and sh[1]>0: zmin = np.nanmin(img[x1:x2,y1:y2]) zmax = np.nanmax(img[x1:x2,y1:y2]) ax.imshow(img[x1:x2,y1:y2], aspect="equal", extent=(-cutrad, cutrad, -cutrad, cutrad), origin="lower", cmap=matplotlib.cm.gray_r, interpolation="none", vmin=zmin, vmax=zmax) c1 = plt.Circle( (0, 0), edgecolor="r", facecolor="none", radius=aperture_rad) c2 = plt.Circle( (0, 0), edgecolor="orange", facecolor="none", radius=sky_rad) c3 = plt.Circle( (0, 0), edgecolor="yellow", facecolor="none", radius=sky_rad+20) plt.gca().add_artist(c1) plt.gca().add_artist(c2) plt.gca().add_artist(c3) ax.set_xticks([]) ax.set_yticks([]) plt.text(+5, +5, "%d"%m[k]["id"]) plt.text(-cutrad, -cutrad, "%.2f$\pm$%.2f"%(m[k]["fit_mag"], m[k]["fiterr"]), color="b") plt.tight_layout() plt.savefig(os.path.join(plotdir, imname + "plot.png")) plt.clf()
def get_app_phot_target(image, ra=None, dec=None, plot=True, store=True, wcsin="logical", fwhm=None, box=15, arcsecpix=0.394, app=2): ''' coords: files: wcsin: can be "world", "logic" fwhm: in arcsec ''' # Load packages; splot is in the onedspec package, which is in noao. # The special keyword _doprint=0 turns off displaying the tasks # when loading a package. with warnings.catch_warnings(): warnings.simplefilter("ignore") fxn() iraf.noao(_doprint=0) iraf.digiphot(_doprint=0) iraf.apphot(_doprint=0) iraf.unlearn("apphot") impf = pf.open(image) wcs = WCS(impf[0].header) #Check that actually the object is within this frame. if (ra is None or dec is None): if (fitsutils.has_par(image, "OBJRA") and fitsutils.has_par(image, "OBJRA")): ra, dec = cc.hour2deg(fitsutils.get_par(image, 'OBJRA'), fitsutils.get_par(image, 'OBJDEC')) else: ra, dec = cc.hour2deg(fitsutils.get_par(image, 'RA'), fitsutils.get_par(image, 'DEC')) print "Assuming ra=%.5f, dec=%.5f"%(ra, dec) pra, pdec = get_xy_coords(image, ra, dec) else: if("logic" in wcsin): pra, pdec = ra, dec else: #Using new method to derive the X, Y pixel coordinates, as wcs module does not seem to be working well. try: #pra, pdec = wcs.wcs_sky2pix(ra, dec, 1) #print "Retrieved the pixel number" pra, pdec = get_xy_coords(image, ra, dec) except IndexError: print "Error with astrometry.net. trying the rudimentary method." pra, pdec = wcs.wcs_sky2pix(ra, dec, 1) #pra, pdec = wcs.wcs_sky2pix(np.array([ra, dec], ndmin=2), 1)[0] shape = impf[0].data.shape if (pra > 0) and (pra < shape[0]) and (pdec > 0) and (pdec < shape[1]): pass else: print image, "ERROR! Object coordinates are outside this frame. Skipping any aperture photometry!!" print pra, pdec, shape return imdir = os.path.dirname(image) imname = os.path.basename(image) plotdir = os.path.join(imdir, "photometry") if not os.path.isdir(plotdir): os.makedirs(plotdir) out_name = os.path.join(plotdir, imname + ".seq.mag") clean_name = os.path.join(plotdir, imname + ".objapp.mag") if (not fwhm is None): fwhm_value = fwhm elif (fitsutils.has_par(image, 'FWHM')): fwhm_value = fitsutils.get_par(image, 'FWHM') else: #Put some default value for Palomar fwhm_value=1.5 if (wcsin == 'logical'): fwhm_value = fwhm_value / arcsecpix if (fitsutils.has_par(image, 'AIRMASS')): airmass_value = fitsutils.get_par(image, 'AIRMASS') else: airmass_value = 1.3 if (not fitsutils.has_par(image, "EXPTIME")): if (fitsutils.has_par(image, "ITIME") and fitsutils.has_par(image, "COADDS")): exptime = fitsutils.get_par(image, "ITIME")*fitsutils.get_par(image, "COADDS") fitsutils.update_par(image, "EXPTIME", exptime) exptime = fitsutils.get_par(image, 'EXPTIME') gain = fitsutils.get_par(image, 'GAIN') #print "FWHM", fwhm_value aperture_rad = math.ceil(float(fwhm_value)*app) # Set aperture radius to two times the PSF radius sky_rad= math.ceil(aperture_rad*app*2) #print aperture_rad, sky_rad print "Saving coodinates for the object in pixels",pra,pdec coords = "/tmp/coords.dat" np.savetxt("/tmp/coords.dat", np.array([[pra, pdec]]), fmt="%.4f %.4f") if os.path.isfile(out_name): os.remove(out_name) if os.path.isfile(clean_name): os.remove(clean_name) iraf.noao.digiphot.apphot.qphot(image = image,\ cbox = box ,\ annulus = sky_rad ,\ dannulus = 20. ,\ aperture = str(aperture_rad),\ coords = coords ,\ output = out_name ,\ plotfile = "" ,\ zmag = 0. ,\ exposure = "exptime" ,\ airmass = "airmass" ,\ filter = "filter" ,\ obstime = "DATE" ,\ epadu = gain ,\ interactive = "no" ,\ radplots = "yes" ,\ verbose = "no" ,\ graphics = "stdgraph" ,\ display = "stdimage" ,\ icommands = "" ,\ wcsin = "logical", wcsout = "logical", gcommands = "") #iraf.noao.digiphot.apphot.phot(image=image, cbox=5., annulus=12.4, dannulus=10., salgori = "centroid", aperture=9.3,wcsin="world",wcsout="tv", interac = "no", coords=coords, output=out_name) iraf.txdump(out_name, "id,image,xcenter,ycenter,xshift,yshift,fwhm,msky,stdev,cier,rapert,sum,area,nsky,flux,itime,mag,merr", "yes", Stdout=clean_name) ma = np.genfromtxt(clean_name, comments="#", dtype=[("id","<f4"), ("image","|S20"), ("X","<f4"), ("Y","<f4"), ("Xshift","<f4"), ("Yshift","<f4"),("fwhm","<f4"), ("msky","<f4"), \ ("stdev","<f4"), ("flags", np.int), ("rapert", "<f4"), ("sum", "<f4"), ("area", "<f4"), ("nsky","<f4") , ("flux", "<f4"), ("itime", "<f4"), ("fit_mag","<f4"), ("fiterr","<f4")]) if (ma.size > 0): ma = np.array([ma]) m = ma[~np.isnan(ma["fit_mag"])] else: print "Only one object found!" m = np.array([ma]) insmag = np.round(ma['fit_mag'][0] , 3) insmagerr = np.round(ma['fiterr'][0], 3) if (fitsutils.has_par(image, "ZEROPT") and fitsutils.has_par(image, "ZEROPTU")): mag = insmag + float(fitsutils.get_par(image, "ZEROPT")) magerr = np.sqrt(insmagerr**2+ float(fitsutils.get_par(image, "ZEROPTU"))**2) else: mag = 0 magerr = 0 if np.isnan(mag): mag, magerr = 0, 0 insmag, insmagerr = 0,0 fitsutils.update_par(image, "INSMAG", "%.3f"%insmag ) fitsutils.update_par(image, "INSMAGER", "%.3f"%insmagerr) fitsutils.update_par(image, "APPMAG", np.round(mag, 3) ) fitsutils.update_par(image, "APPMAGER", np.round(magerr, 3)) if (plot): #zmin, zmax = zscale.zscale(impf[0].data.T[pra-50:pra+50,pdec-50:pdec+50]) #zmin, zmax = zscale.zscale(impf[0].data) #im = plt.imshow(impf[0].data, vmin=zmin, vmax=zmax, origin="bottom") print np.percentile(impf[0].data, 5), np.percentile(impf[0].data, 95) impf[0].data[np.isnan(impf[0].data)] = np.nanmedian(impf[0].data) print np.percentile(impf[0].data, 5), np.percentile(impf[0].data, 95) im = plt.imshow(impf[0].data, vmin=np.percentile(impf[0].data, 5), vmax=np.percentile(impf[0].data, 95), origin="bottom") X = int(ma["X"][0]) Y = int(ma["Y"][0]) pra = int(pra) pdec = int(pdec) plt.scatter(X, Y, marker="o", s=100, facecolor="none", edgecolor="red") plt.colorbar(im) plt.savefig(os.path.join(plotdir, imname+".png"), dpi=200) plt.clf() zmin, zmax = zscale.zscale(impf[0].data.T[X-50:X+50,Y-50:Y+50].T) im = plt.imshow(impf[0].data.T[pra-50:pra+50,pdec-50:pdec+50].T, vmin=zmin, vmax=zmax, interpolation="none", origin="bottom", extent=(-50,50,-50,50)) c1 = plt.Circle( (pra-X, pdec-Y), edgecolor="k", facecolor="none", radius=aperture_rad, label="Initial position") c11 = plt.Circle( (pra-X, pdec-Y), edgecolor="k", facecolor="none", radius=sky_rad) c2 = plt.Circle( (0, 0), edgecolor="orange", facecolor="none", radius=aperture_rad, label="Adjusted centroid") c22 = plt.Circle( (0, 0), edgecolor="orange", facecolor="none", radius=sky_rad) plt.gca().add_artist(c1) plt.gca().add_artist(c11) plt.gca().add_artist(c2) plt.gca().add_artist(c22) plt.colorbar(im) myhandles = [] markers = ["o", "o"] labels = ["Initial position", "Adjusted centroid"] cols = ["k", "orange"] for i in np.arange(len(markers)): myhandles.append(mlines.Line2D([], [], mec=cols[i], mfc="none", marker=markers[i], ls="None", markersize=10, label=labels[i])) plt.legend(handles=myhandles, loc="lower left", labelspacing=0.3, fontsize=11, numpoints=1, frameon=False, ncol=5, bbox_to_anchor=(0.0, 0.00), fancybox=False, shadow=True) plt.title("MIN: %.0f MAX: %.0f"%(np.nanmin(impf[0].data.T[X-50:X+50,Y-50:Y+50]), np.nanmax(impf[0].data.T[X-50:X+50,Y-50:Y+50]))) plt.savefig(os.path.join(plotdir, imname+"_zoom.png")) plt.clf()
def extract_star_sequence(imfile, band, plot=True, survey="sdss", debug=False, refstars=None, plotdir="."): """ Given a fits image: imfile and a the name of the band which we want to extract the sources from, it saves the extracted sources into '/tmp/sdss_cat_det.txt' file. If the band does not match the bands in the survey, a change is performed to adapt to the new band. If plotting activated, plots the USNOB1 field of stars on top of the star field image. Red circles are stars identified from the catalogue in the correct magnitude range. Yellow circles are stars that are isolated. """ survey = str.lower(survey) minmag = 15 maxmag = 21.0 f = pf.open(imfile) wcs = pywcs.WCS(f[0].header) img = f[0].data img[img < 0] = 0 ra, dec = wcs.wcs_pix2sky(np.array([img.shape[0] / 2, img.shape[1] / 2], ndmin=2), 1)[0] ra0, dec0 = wcs.wcs_pix2sky(np.array([img.shape[0], img.shape[1]], ndmin=2), 1)[0] sr = 2 * np.abs(dec - dec0) logger.info("%.4f %.4f %.4f" % (ra, dec, sr)) if not refstars is None: shutil.copy(refstars, "/tmp/tmp_sdss_%s.cat" % creationdate) catalog = np.genfromtxt("/tmp/tmp_sdss_%s.cat" % creationdate, names=True, dtype=None, delimiter=",") cat_ra = catalog["ra"] cat_dec = catalog["dec"] try: mag = catalog["R"] except: mag = catalog["r"] elif str.lower(survey) == "usnob1": # ra, dec = coordinates_conversor.hour2deg(f[0].header['RA'], f[0].header['DEC']) # SEDM FoV is 6.5 arcmin, due to uncertainties in the position, 4 arcmin radius assumed. # Download USNO-B1 catalog for the position catalog_url = "http://www.nofs.navy.mil/cgi-bin/vo_cone.cgi?CAT=USNO-B1&RA=%.5f&DEC=%.5f&SR=%.4f&VERB=1" % ( ra, dec, sr, ) logger.info("Downloading USNO-B1 catalog...") urllib.urlretrieve(catalog_url, "/tmp/tmp_%s.cat" % creationdate) # Read RA, Dec and magnitude from XML format USNO catalog catalog = parse_single_table("/tmp/tmp_%s.cat" % creationdate) cat_ra = catalog.array["RA"].data cat_dec = catalog.array["DEC"].data cat_R1mag = catalog.array["R1"].data cat_R2mag = catalog.array["R2"].data cat_B1mag = catalog.array["B1"].data cat_B2mag = catalog.array["B2"].data cat_I2mag = catalog.array["I2"].data cat_R1mag[cat_R1mag == 0] = np.nan cat_R2mag[cat_R2mag == 0] = np.nan cat_B1mag[cat_B1mag == 0] = np.nan cat_B2mag[cat_B2mag == 0] = np.nan cat_I2mag[cat_I2mag == 0] = np.nan Bmag = np.nanmean(np.array([cat_B1mag, cat_B2mag]), axis=0) Rmag = np.nanmean(np.array([cat_R1mag, cat_R2mag]), axis=0) Imag = cat_I2mag mag = Rmag elif survey == "apass": # Download USNO-B1 catalog for the position catalog_url = "https://www.aavso.org/cgi-bin/apass_download.pl?ra=%.5f&dec=%.5f&radius=%.4f8&outtype=1" % ( ra, dec, sr, ) print "Downloading APASS catalog..." urllib.urlretrieve(catalog_url, "/tmp/tmp_apass_%s.cat" % creationdate) catalog = np.genfromtxt("/tmp/tmp_apass_%s.cat" % creationdate, delimiter=",", names=True) if np.ndim(catalog) == 0: return False cat_ra = catalog["radeg"] cat_dec = catalog["decdeg"] mag = catalog["Sloan_r"] elif survey == "sdss": minmag = 15 maxmag = 21.5 catalog_url = ( "http://skyserver.sdss.org/dr9/en/tools/search/x_radial.asp?ra=%.5f&dec=%.5f&check_type=type&type=6&radius=%.4f&check_u=u&min_u=%.2f&max_u=%.2f&check_g=g&min_g=%.2f&max_g=%.2f&check_r=r&min_r=%.2f&max_r=%.2f&check_i=i&min_i=%.2f&max_i=%.2f&check_z=z&min_z=%.2f&max_z=%.2f&entries=top&topnum=500&format=csv" % (ra, dec, sr * 60, minmag, maxmag, minmag, maxmag, minmag, maxmag, minmag, maxmag, minmag, maxmag) ) logger.info("Downloading SDSS catalog...") logger.info("%s" % catalog_url) urllib.urlretrieve(catalog_url, "/tmp/tmp_sdss_%s.cat" % creationdate) catalog = np.genfromtxt("/tmp/tmp_sdss_%s.cat" % creationdate, delimiter=",", names=True) if np.ndim(catalog) == 0: return False try: cat_ra = np.array(catalog["ra"], ndmin=1) cat_dec = np.array(catalog["dec"], ndmin=1) if band in catalog.dtype.names: print "SDSS filter detected" mag = np.array(catalog[band], ndmin=1) elif band in ["U", "B", "V", "R", "I", "Z"]: print "Johnson filter detected." john = transformations.sdss2johnson( "/tmp/tmp_sdss_%s.cat" % creationdate, savefile="/tmp/tmp_sdss_%s.cat" % creationdate ) mag = john[band] catalog = np.genfromtxt("/tmp/tmp_sdss_%s.cat" % creationdate, dtype=None, names=True, delimiter=",") else: print "Unknown band!!", band except IOError: logger.error("Problems with SDSS image %s" % band) return False except ValueError: logger.error("Problems with the catalogue for the image") return False # Convert ra, dec position of all stars to pixels. star_pix = np.array([0, 0]) for i in range(len(cat_ra)): # Get pixel coordinates of USNO stars s = wcs.wcs_sky2pix(np.array([cat_ra[i], cat_dec[i]], ndmin=2), 1)[0] star_pix = np.row_stack((star_pix, s)) star_pix = star_pix[1:] pix2ang = 0.394 rad = math.ceil(25.0 / pix2ang) # Select only the stars within the image. mask = ( (star_pix[:, 0] > -rad) * (star_pix[:, 0] < img.shape[1] + rad) * (star_pix[:, 1] > -rad) * (star_pix[:, 1] < img.shape[0] + rad) ) if band == "u": mask = mask * (mag < 19) # Select only stars isolated in a radius of ~12 arcsec. mask2 = np.array(are_isolated(cat_ra[mask], cat_dec[mask], 15.0)) if len(mask2) == 0: logger.error("No good stars left") return False # Select only stars that are within the proper magnitude range mask3 = (mag[mask][mask2] < maxmag) * (mag[mask][mask2] > minmag) mask3 = ( mask3 * (star_pix[:, 0][mask][mask2] > rad) * (star_pix[:, 0][mask][mask2] < img.shape[1] - rad) * (star_pix[:, 1][mask][mask2] > rad) * (star_pix[:, 1][mask][mask2] < img.shape[0] - rad) ) if survey == "usnob1": output = np.column_stack( ( star_pix[:, 0][mask][mask2][mask3], star_pix[:, 0][mask][mask2][mask3], cat_ra[mask][mask2][mask3], cat_dec[mask][mask2][mask3], Bmag[mask][mask2][mask3], Rmag[mask][mask2][mask3], Imag[mask][mask2][mask3], ) ) np.savetxt("/tmp/usnob1_cat.txt", output, fmt="%.5f %.5f %.5f %.5f %.5f %.5f %.5f", header="#X Y ra dec B R I") print "Saved to", "/tmp/usnob1_cat.txt" elif survey == "apass": if not np.any(mask2) and not np.any(mask3): print star_pix print "No stars left...", mask, mask2, mask3 return else: print catalog[mask][mask2][mask3] catalog = catalog[mask][mask2][mask3] s = star_pix[mask][mask2][mask3] z = np.zeros(len(s), dtype=[("x", "f8"), ("y", "f8")]) z["x"] = s[:, 0] z["y"] = s[:, 1] for n in catalog.dtype.names: z = rfn.append_fields(z, names=n, data=catalog[n], usemask=False) fmt = "%.5f" for i in range(len(z[0]) - 1): fmt += " %.5f" np.savetxt( "/tmp/apass_cat.txt", z, fmt=fmt, header="x y radeg raerr decdeg decerr number_of_Obs V dV B dB g dg r dr i di", ) print "Saved to", "/tmp/apass_cat.txt" elif survey == "sdss": if (not np.any(mask) and not np.any(mask2) and not np.any(mask3)) or len(catalog[mask][mask2][mask3]) == 0: print star_pix print "No stars left...", mask, mask2, mask3 return False else: catalog = catalog[mask][mask2][mask3] s = star_pix[mask][mask2][mask3] print "left %d stars" % (len(catalog)), catalog.dtype.names z = np.zeros(len(s), dtype=[("x", "f8"), ("y", "f8")]) z["x"] = s[:, 0] z["y"] = s[:, 1] header = "x y " for n in catalog.dtype.names: if n in [ "objid", "ra", "dec", "u", "g", "r", "i", "z", "Err_u", "Err_g", "Err_r", "Err_i", "Err_z", ] or n in ["id", "ra", "dec", "U", "B", "V", "R", "I", "dU", "dB", "dV", "dR", "dI"]: z = rfn.append_fields(z, names=n, data=catalog[n], usemask=False) header += n.replace("Err_", "d") + " " fmt = "%.5f" for i in range(len(z[0]) - 1): fmt += " %.5f" np.savetxt("/tmp/sdss_cat_%s.txt" % creationdate, z, fmt=fmt, header=header) logger.info("Saved catalogue stars to %s" % ("/tmp/sdss_cat_%s.txt" % creationdate)) # Find FWHM for this image out = find_fwhm(imfile, star_pix[:, 1][mask][mask2][mask3], star_pix[:, 0][mask][mask2][mask3], plot=debug) mask_valid_fwhm = (out["detected"]) * (out["e"] > 0.6) * ~np.isnan(out["fwhm"] * (out["fwhm"] < 30)) if ((np.count_nonzero(mask_valid_fwhm) < 3) and (fitsutils.get_par(imfile, "FILTER") != "u")) or ( (np.count_nonzero(mask_valid_fwhm) < 2) and (fitsutils.get_par(imfile, "FILTER") == "u") ): logger.error( "ERROR with FWHM!! Too few points for a valid estimation. %d" % np.count_nonzero(mask_valid_fwhm) + ") points" ) logger.error("%s %s" % (out["detected"], out["fwhm"])) return False outd = out[mask_valid_fwhm] logger.info( "Average FWHM %.3f arcsec, %.3f pixels" % (np.median(outd["fwhm"]), np.median(outd["fwhm"]) * pix2ang) ) fwhm = np.percentile(outd["fwhm"], 40) fitsutils.update_par(imfile, "FWHM", np.round(fwhm, 3)) if band in "ugriz": header = "x y objid ra dec u g r i z du dg dr di dz" elif band in "UBVRI": header = "x y objid ra dec U B V R I dU dB dV dR dI" np.savetxt("/tmp/sdss_cat_det_%s.txt" % creationdate, z[mask_valid_fwhm], fmt=fmt, header=header) print "Saved to", "/tmp/sdss_cat_det_%s.txt" % creationdate # Plot results img = img - np.nanmin(img) zmin, zmax = zscale.zscale(img) logger.info( "Found %d stars in %s. " % (len(cat_dec), survey) + "%d of them within the FoV. " % len(cat_ra[mask]) + "%d of them are isolated." % len(cat_ra[mask][mask2]) + "%d of them with suitable magnitudes. " % len(cat_ra[mask][mask2][mask3]) + "%d of them with detected stars." % np.count_nonzero(mask_valid_fwhm) ) if plot: im = plt.imshow( img, aspect="equal", origin="lower", cmap=matplotlib.cm.gray_r, interpolation="none", vmin=zmin, vmax=zmax ) if len(star_pix[:, 0][mask]) > 0: plt.scatter( star_pix[:, 0][mask], star_pix[:, 1][mask], marker="o", s=np.minimum(150, 10000 * (10.0 / mag[mask][mask2]) ** 9), edgecolor="red", facecolor="none", label="catalogue", ) if len(star_pix[:, 0][mask][mask2]) > 0: plt.scatter( star_pix[:, 0][mask][mask2], star_pix[:, 1][mask][mask2], marker="o", s=20, edgecolor="yellow", facecolor="none", label="isolated", ) if len(star_pix[:, 0][mask][mask2][mask3]) > 0: plt.scatter( star_pix[:, 0][mask][mask2][mask3], star_pix[:, 1][mask][mask2][mask3], marker="o", s=200, edgecolor="green", facecolor="none", label="wihtin farme and mag", ) selected = star_pix[:, :][mask][mask2][mask3][mask_valid_fwhm] if len(selected) > 0: plt.scatter(selected[:, 0], selected[:, 1], marker="o", s=400, edgecolor="blue", facecolor="none") for i in np.arange(len(selected)): plt.text(selected[i, 0] + 10, selected[i, 1] + 10, i + 1) plt.legend(loc="best", frameon=False, framealpha=0.9) plt.savefig( os.path.join( plotdir, os.path.basename(imfile).replace(".fits", ".seqstars.png").replace(".new", ".seqstars.png") ) ) logger.info("Saved stars to %s" % imfile.replace(".fits", ".seqstars.png")) plt.clf() return True
def get_app_phot_target(image, plot=False, store=True, wcsin="logical", fwhm=2, box=4, ra=None, dec=None): ''' coords: files: wcsin: can be "world", "logic" ''' # Load packages; splot is in the onedspec package, which is in noao. # The special keyword _doprint=0 turns off displaying the tasks # when loading a package. with warnings.catch_warnings(): warnings.simplefilter("ignore") fxn() iraf.noao(_doprint=0) iraf.digiphot(_doprint=0) iraf.apphot(_doprint=0) iraf.unlearn("apphot") impf = pf.open(image) wcs = pywcs.WCS(impf[0].header) #Check that actually the object is within this frame. if (ra is None or dec is None): if (fitsutils.has_par(image, "OBJRA") and fitsutils.has_par(image, "OBJRA")): ra, dec = cc.hour2deg(fitsutils.get_par(image, 'OBJRA'), fitsutils.get_par(image, 'OBJDEC')) else: ra, dec = cc.hour2deg(fitsutils.get_par(image, 'RA'), fitsutils.get_par(image, 'DEC')) pra, pdec = get_xy_coords(image, ra, dec) else: if(wcsin == "logical"): pra, pdec = ra, dec else: #Using new method to derive the X, Y pixel coordinates, as pywcs does not seem to be working well. pra, pdec = get_xy_coords(image, ra, dec) #pra, pdec = wcs.wcs_sky2pix(ra, dec, 1) #pra, pdec = wcs.wcs_sky2pix(np.array([ra, dec], ndmin=2), 1)[0] shape = impf[0].data.shape if (pra > 0) and (pra < shape[0]) and (pdec > 0) and (pdec < shape[1]): pass else: print image, "ERROR! Object coordinates are outside this frame. Skipping any aperture photometry!!" print pra, pdec, shape return imdir = os.path.dirname(image) imname = os.path.basename(image) plotdir = os.path.join(imdir, "photometry") if not os.path.isdir(plotdir): os.makedirs(plotdir) out_name = os.path.join(plotdir, imname + ".seq.mag") clean_name = os.path.join(plotdir, imname + ".objapp.mag") fwhm_value = fwhm nsrc, fwhm_value, ellip = sextractor.get_image_pars(image) if np.isnan(fwhm_value): fwhm_value=99 fitsutils.update_par(image, 'FWHM', fwhm_value) if (fitsutils.has_par(image, 'AIRMASS')): airmass_value = fitsutils.get_par(image, 'AIRMASS') else: airmass_value = 1.3 exptime = fitsutils.get_par(image, 'EXPTIME') gain = fitsutils.get_par(image, 'GAIN') #print "FWHM", fwhm_value aperture_rad = math.ceil(float(fwhm_value)*3) # Set aperture radius to three times the PSF radius sky_rad= math.ceil(aperture_rad*4) #print aperture_rad, sky_rad print "Saving coodinates for the object in pixels",pra,pdec coords = "/tmp/coords.dat" np.savetxt("/tmp/coords.dat", np.array([[pra, pdec]]), fmt="%.4f %.4f") if (plot): zmin, zmax = zscale.zscale(impf[0].data) im = plt.imshow(impf[0].data, vmin=zmin, vmax=zmax, origin="bottom") plt.scatter(pra, pdec, marker="o", s=100, facecolor="none") plt.savefig(os.path.join(plotdir, imname+".png")) plt.clf() if os.path.isfile(out_name): os.remove(out_name) if os.path.isfile(clean_name): os.remove(clean_name) iraf.noao.digiphot.apphot.qphot(image = image,\ cbox = box ,\ annulus = sky_rad ,\ dannulus = 15. ,\ aperture = str(aperture_rad),\ coords = coords ,\ output = out_name ,\ plotfile = "" ,\ zmag = 0. ,\ exposure = "exptime" ,\ airmass = "airmass" ,\ filter = "filter" ,\ obstime = "DATE" ,\ epadu = gain ,\ interactive = "no" ,\ radplots = "yes" ,\ verbose = "no" ,\ graphics = "stdgraph" ,\ display = "stdimage" ,\ icommands = "" ,\ wcsin = "logical", wcsout = "logical", gcommands = "") #iraf.noao.digiphot.apphot.phot(image=image, cbox=5., annulus=12.4, dannulus=10., salgori = "centroid", aperture=9.3,wcsin="world",wcsout="tv", interac = "no", coords=coords, output=out_name) iraf.txdump(out_name, "id,image,xcenter,ycenter,xshift,yshift,fwhm,msky,stdev,mag,merr", "yes", Stdout=clean_name) ma = np.genfromtxt(clean_name, comments="#", dtype=[("id","<f4"), ("image","|S20"), ("X","<f4"), ("Y","<f4"), ("Xshift","<f4"), ("Yshift","<f4"),("fwhm","<f4"), ("ph_mag","<f4"), ("stdev","<f4"), ("fit_mag","<f4"), ("fiterr","<f4")]) if (ma.size > 0): if (ma.size==1): ma = np.array([ma]) m = ma[~np.isnan(ma["fit_mag"])] else: print "Only one object found!" m = np.array([ma]) insmag = np.round(ma['fit_mag'][0] , 3) insmagerr = np.round(ma['fiterr'][0], 3) if (fitsutils.has_par(image, "ZEROPT")): mag = insmag + float(fitsutils.get_par(image, "ZEROPT")) magerr = np.sqrt(insmagerr**2+ float(fitsutils.get_par(image, "ZEROPTU"))**2) if np.isnan(mag): mag, magerr = 0, 0 insmag, insmagerr = 0,0 fitsutils.update_par(image, "INSMAG", "%.3f"%insmag ) fitsutils.update_par(image, "INSMAGER", "%.3f"%insmagerr) fitsutils.update_par(image, "APPMAG", np.round(mag, 3) ) fitsutils.update_par(image, "APPMAGER", np.round(magerr, 3)) if (plot): X = int(ma["X"][0]) Y = int(ma["Y"][0]) pra = int(pra) pdec = int(pdec) plt.scatter(X, Y, marker="o", s=100, facecolor="none", edgecolor="red") plt.colorbar(im) plt.savefig(os.path.join(plotdir, imname+".png")) plt.clf() zmin, zmax = zscale.zscale(impf[0].data.T[X-50:X+50,Y-50:Y+50].T) im = plt.imshow(impf[0].data.T[pra-50:pra+50,pdec-50:pdec+50].T, vmin=zmin, vmax=zmax, interpolation="none", origin="bottom", extent=(-50,50,-50,50)) c1 = plt.Circle( (pra-X, pdec-Y), edgecolor="k", facecolor="none", radius=aperture_rad, label="Initial position") c11 = plt.Circle( (pra-X, pdec-Y), edgecolor="k", facecolor="none", radius=sky_rad) c2 = plt.Circle( (0, 0), edgecolor="orange", facecolor="none", radius=aperture_rad, label="Adjusted centroid") c22 = plt.Circle( (0, 0), edgecolor="orange", facecolor="none", radius=sky_rad) plt.gca().add_artist(c1) plt.gca().add_artist(c11) plt.gca().add_artist(c2) plt.gca().add_artist(c22) plt.colorbar(im) myhandles = [] markers = ["o", "o"] labels = ["Initial position", "Adjusted centroid"] cols = ["k", "orange"] for i in np.arange(len(markers)): myhandles.append(mlines.Line2D([], [], mec=cols[i], mfc="none", marker=markers[i], ls="None", markersize=10, label=labels[i])) plt.legend(handles=myhandles, loc="lower left", labelspacing=0.3, fontsize=11, numpoints=1, frameon=False, ncol=5, bbox_to_anchor=(0.0, 0.00), fancybox=False, shadow=True) plt.title("MIN: %.0f MAX: %.0f"%(np.nanmin(impf[0].data.T[X-50:X+50,Y-50:Y+50]), np.nanmax(impf[0].data.T[X-50:X+50,Y-50:Y+50]))) plt.savefig(os.path.join(plotdir, imname+"_zoom.png")) plt.clf()
# Get pixel coordinates of SN wcs = pywcs.WCS(prihdr) try: target_pix = wcs.wcs_sky2pix([(np.array([ra,dec], np.float_))], 1)[0] except: print "ERROR when converting sky to wcs. Is astrometry in place? Default coordinates assigned." target_pix = [+nx/2., ny/2.] print target_pix else: target_pix = [+nx/2., ny/2.] img = img - np.nanmin(img) av = np.median(img.flatten()) mi, ma = zscale.zscale(img) im = plt.imshow(plt.log10(img), aspect="equal", extent=(0, ny, 0, nx), \ origin="lower", cmap=matplotlib.cm.gray_r, interpolation="none", vmin=np.log10(av), vmax=np.log10(3*av)) #, interpolation="lanczos") plt.scatter(target_pix[0], target_pix[1], marker="x", s=10, c="red") plt.colorbar(im) filename = os.path.basename(f) plt.savefig(os.path.join(plot_dir, filename.replace("."+filename.split(".")[-1], "_{:}.png".format(i))), dpi=200) plt.clf() def move_to_discarded(mydir, myfilter, ra, dec): import shutil for f in glob.glob(os.path.join(mydir, myfilter)): frames_with_target = get_frames_with_target(f, ra, dec) if len(frames_with_target) == 0: discarddir = os.path.join(mydir, "discarded")
def get_app_phot(coords, image, plot_only=False, store=True, wcsin="world", fwhm=2, plotdir=".", box=15): ''' coords: files: wcsin: can be "world", "logic" ''' # Load packages; splot is in the onedspec package, which is in noao. # The special keyword _doprint=0 turns off displaying the tasks # when loading a package. if (not plot_only): iraf.noao(_doprint=0) iraf.digiphot(_doprint=0) iraf.apphot(_doprint=0) iraf.unlearn("apphot") imdir = os.path.dirname(image) imname = os.path.basename(image) plotdir = os.path.join(imdir, "photometry") if not os.path.isdir(plotdir): os.makedirs(plotdir) out_name = os.path.join(plotdir, imname + ".seq.mag") clean_name = os.path.join(plotdir, imname + ".app.mag") # Read values from .ec file ecfile= image+".ec" filter_value=''.join(ecfile).split('.',1)[0] fwhm_value = fwhm if (fitsutils.has_par(image, 'FWHM')): fwhm_value = fitsutils.get_par(image, 'FWHM') if (fitsutils.has_par(image, 'AIRMASS')): airmass_value = fitsutils.get_par(image, 'AIRMASS') else: airmass_value = 1.3 exptime = fitsutils.get_par(image, 'EXPTIME') gain = fitsutils.get_par(image, 'GAIN') try: with open(''.join(ecfile),'r') as f: for line in f: if "airmass" in line: airmass_value = line.split('=',1)[1] else: airmass_value = 1 if "FWHM" in line: print line fwhm_value = line.split('FWHM=',1)[1] fwhm_value = fwhm_value.rsplit("aperture")[0] except: pass print "FWHM", fwhm_value aperture_rad = math.ceil(float(fwhm_value)*2) # Set aperture radius to three times the PSF radius sky_rad= math.ceil(aperture_rad)*5 print aperture_rad, sky_rad if (not plot_only): if os.path.isfile(out_name): os.remove(out_name) if os.path.isfile(clean_name): os.remove(clean_name) # Check if files in list, otherwise exit if not ecfile: print "No .ec files in directory, exiting" sys.exit() iraf.noao.digiphot.apphot.qphot(image = image,\ cbox = box ,\ annulus = sky_rad ,\ dannulus = 15. ,\ aperture = str(aperture_rad),\ coords = coords ,\ output = out_name ,\ plotfile = "" ,\ zmag = 0. ,\ exposure = "exptime" ,\ airmass = "airmass" ,\ filter = "filters" ,\ obstime = "DATE" ,\ epadu = gain ,\ interactive = "no" ,\ radplots = "yes" ,\ verbose = "no" ,\ graphics = "stdgraph" ,\ display = "stdimage" ,\ icommands = "" ,\ wcsin = wcsin, wcsout = "logical", gcommands = "") #iraf.noao.digiphot.apphot.phot(image=image, cbox=5., annulus=12.4, dannulus=10., salgori = "centroid", aperture=9.3,wcsin="world",wcsout="tv", interac = "no", coords=coords, output=out_name) iraf.txdump(out_name, "id,image,xcenter,ycenter,xshift,yshift,fwhm,msky,stdev,mag,merr", "yes", Stdout=clean_name) ma = np.genfromtxt(clean_name, comments="#", dtype=[("id","<f4"), ("image","|S20"), ("X","<f4"), ("Y","<f4"), ("Xshift","<f4"), ("Yshift","<f4"),("fwhm","<f4"), ("ph_mag","<f4"), ("stdev","<f4"), ("fit_mag","<f4"), ("fiterr","<f4")]) if (ma.size > 0): m = ma[~np.isnan(ma["fit_mag"])] else: print "Only one object found!" m = np.array([ma]) hdulist = pf.open(image) prihdr = hdulist[0].header img = hdulist[0].data * 1. nx, ny = img.shape dimX = int(4) dimY = int(np.ceil(len(m)*1./4)) outerrad = sky_rad+10 cutrad = outerrad + 15 plt.suptitle("FWHM="+str(fwhm_value)) k = 0 for i in np.arange(dimX): for j in np.arange(dimY): if ( k < len(m)): ax = plt.subplot2grid((dimX,dimY),(i, j)) y1, y2, x1, x2 = m[k]["X"]-cutrad, m[k]["X"]+cutrad, m[k]["Y"]-cutrad, m[k]["Y"]+cutrad y1, y2, x1, x2 = int(y1), int(y2), int(x1), int(x2) try: zmin, zmax = zscale.zscale(img[x1:x2,y1:y2], nsamples=1000, contrast=0.25) except: sh= img[x1:x2,y1:y2].shape if sh[0]>0 and sh[1]>0: zmin = np.nanmin(img[x1:x2,y1:y2]) zmax = np.nanmax(img[x1:x2,y1:y2]) continue else: continue ax.imshow(img[x1:x2,y1:y2], aspect="equal", extent=(-cutrad, cutrad, -cutrad, cutrad), origin="lower", cmap=matplotlib.cm.gray_r, interpolation="none", vmin=zmin, vmax=zmax) c1 = plt.Circle( (0, 0), edgecolor="r", facecolor="none", radius=5.) c2 = plt.Circle( (0, 0), edgecolor="orange", facecolor="none", radius=sky_rad) c3 = plt.Circle( (0, 0), edgecolor="yellow", facecolor="none", radius=sky_rad+10) plt.gca().add_artist(c1) plt.gca().add_artist(c2) plt.gca().add_artist(c3) ax.set_xticks([]) ax.set_yticks([]) plt.text(+5, +5, "%d"%m[k]["id"]) plt.text(-cutrad, -cutrad, "%.2f$\pm$%.2f"%(m[k]["fit_mag"], m[k]["fiterr"]), color="b") k = k+1 plt.savefig(os.path.join(plotdir, imname + "plot.png")) plt.clf()
def process_query_image(): if request.args['iterative']: query_text = request.args['query'].replace("@","@"+str(request.args['iteration'])) title = "Iteration " + str(request.args['iteration']) else: query_text = str(request.args['query']) title = query_text iterative = request.args['iterative'] z_scale = request.args['z_scale'] language = str(request.args['language']) callback = request.args['callback'] query_lines = query_text.split("\n") db_interface.connect() # process all queries except for the last one for query_line in query_lines[:-1]: # process all lines except the last one db_interface.executeQuery(str(query_line), str(language)) # process last query query_result = db_interface.executeQuery(str(query_lines[-1]), str(language)) if query_result is None: # error processing query return make_response(json.dumps({ 'request': request.json, 'status': 'ERROR', 'message': "error processing query", })) data_array, array_dim = db_interface.getFirstAttrArrFromQueryInNumPY(query_result) db_interface.disconnect() #print >> sys.stdout, response_json result = SciDB.SciDB_Result(data_array) # Get the image array if not iterative: img_array = result.get_data_array() elif request.args['iteration'] == 1: img_array = result.get_data_array() else: master = memcache.get(session.pop('previous_data_key', None), None) img_array = result.get_img_array(master) # Get z min and max if z_scale == True: z_min, z_max = zscale.zscale(result.data_array, nsamples=2000, contrast=0.25) else: z_min, z_max = (array_dim[2][0], array_dim[2][1]) #TODO fix this, currently zmin and zmax are unknown # Make figure output = scalrr_vis.plot_image(img_array, z_min, z_max) encoded_image = base64.b64encode(output.getvalue()) # Save session data memcache[query_text] = result.get_data_array() session['previous_data_key'] = query_text # Make response json_message = { 'request': request.args, 'status': 'OK', 'image': encoded_image, #"plot": {"title": title, "x_axis": result.x_name, "y_axis": result.y_name, "z_axis": result.z_name}, "dimensions": { "x":{"min": array_dim[0][0], "max": array_dim[0][1]}, "y":{"min": array_dim[1][0], "max": array_dim[1][1]}, "z":{"min": array_dim[2][0], "max": array_dim[2][1]} } } message = callback + "(" + json.dumps(json_message) + ")" response = make_response(message) #print >> sys.stdout, json_message return response
def extract_star_sequence(imfile, band, plot=True, survey='apass', debug=False, refstars=None): ''' Plots the USNOB1 field of stars on top of the star field image. Red circles are stars identified from the catalogue in the correct magnitude range. Yellow circles are stars that are isolated. ''' survey = str.lower(survey) f = pf.open(imfile) wcs = pywcs.WCS(f[0].header) img = f[0].data img[img<0] = 0 ra, dec = wcs.wcs_pix2sky(np.array([img.shape[0]/2, img.shape[1]/2], ndmin=2), 1)[0] sr = 5.5/60 print ra,dec if not refstars is None: shutil.copy(refstars, "/tmp/tmp_sdss.cat") catalog = np.genfromtxt("/tmp/tmp_sdss.cat", names=True, dtype=None, delimiter=",") cat_ra = catalog["ra"] cat_dec = catalog["dec"] mag = catalog["R"] elif str.lower(survey) =='usnob1': #ra, dec = coordinates_conversor.hour2deg(f[0].header['RA'], f[0].header['DEC']) #SEDM FoV is 6.5 arcmin, due to uncertainties in the position, 4 arcmin radius assumed. # Download USNO-B1 catalog for the position catalog_url = 'http://www.nofs.navy.mil/cgi-bin/vo_cone.cgi?CAT=USNO-B1&RA=%.5f&DEC=%.5f&SR=%.4f&VERB=1' % (ra, dec, sr) print "Downloading USNO-B1 catalog..." urllib.urlretrieve(catalog_url, '/tmp/tmp.cat') # Read RA, Dec and magnitude from XML format USNO catalog catalog = parse_single_table("/tmp/tmp.cat") cat_ra = catalog.array['RA'].data cat_dec = catalog.array['DEC'].data cat_R1mag = catalog.array['R1'].data cat_R2mag = catalog.array['R2'].data cat_B1mag = catalog.array['B1'].data cat_B2mag = catalog.array['B2'].data cat_I2mag = catalog.array['I2'].data cat_R1mag[cat_R1mag==0] = np.nan cat_R2mag[cat_R2mag==0] = np.nan cat_B1mag[cat_B1mag==0] = np.nan cat_B2mag[cat_B2mag==0] = np.nan cat_I2mag[cat_I2mag==0] = np.nan Bmag = np.nanmean(np.array([cat_B1mag, cat_B2mag]), axis=0) Rmag = np.nanmean(np.array([cat_R1mag, cat_R2mag]), axis=0) Imag = cat_I2mag mag = Rmag elif survey == "apass": # Download USNO-B1 catalog for the position catalog_url = 'https://www.aavso.org/cgi-bin/apass_download.pl?ra=%.5f&dec=%.5f&radius=%.4f8&outtype=1' % (ra, dec, sr) print "Downloading APASS catalog..." urllib.urlretrieve(catalog_url, '/tmp/tmp_apass.cat') catalog = np.genfromtxt("/tmp/tmp_apass.cat", delimiter=",", names=True) cat_ra = catalog['radeg'] cat_dec = catalog['decdeg'] mag = catalog['Sloan_r'] elif (survey=='sdss'): minmag = 0 maxmag = 21.0 catalog_url='http://skyserver.sdss.org/dr7/en/tools/search/x_radial.asp?ra=%.5f&dec=%.5f&check_type=type&type=6\ &radius=%.4f&check_u=u&min_u=%.2f&max_u=%.2f&check_g=g&min_g=%.2f&max_g=%.2f&check_r=r&min_r=%.2f&max_r=%.2f&check_i=i&min_i=%.2f&max_i=%.2f&check_z=z&min_z=%.2f&max_z=%.2f&entries=top&topnum=500&format=csv'%(ra, dec, sr*60,minmag,maxmag,minmag,maxmag,minmag,maxmag,minmag,maxmag,minmag,maxmag) print "Downloading SDSS catalog..." print catalog_url urllib.urlretrieve(catalog_url, '/tmp/tmp_sdss.cat') catalog = np.genfromtxt("/tmp/tmp_sdss.cat", delimiter=",", names=True) try: cat_ra = catalog['ra'] cat_dec = catalog['dec'] mag = catalog[band] except: print "Problems with SDSS image", band return False #Convert ra, dec position of all stars to pixels. star_pix = np.array([0,0]) for i in range(len(cat_ra)): # Get pixel coordinates of USNO stars s = wcs.wcs_sky2pix(np.array([cat_ra[i], cat_dec[i]], ndmin=2), 1)[0] star_pix = np.row_stack((star_pix, s)) star_pix = star_pix[1:] pix2ang = 0.394 rad = math.ceil(25./pix2ang) #Select only the stars within the image. mask = (star_pix[:,0]>-rad) * (star_pix[:,0]<img.shape[1]+rad)*(star_pix[:,1]>-rad) * (star_pix[:,1]<img.shape[0]+rad) if (band == 'u'): mask = mask * (mag < 19) #Select only stars isolated in a radius of ~12 arcsec. mask2 = np.array(are_isolated(cat_ra[mask], cat_dec[mask], 30.)) #Select only stars that are within the proper magnitude range mask3 = (mag[mask][mask2] < 20.) * (mag[mask][mask2] > 12) mask3 = mask3 * (star_pix[:,0][mask][mask2]>rad) * (star_pix[:,0][mask][mask2]<img.shape[1]-rad)*(star_pix[:,1][mask][mask2]>rad) * (star_pix[:,1][mask][mask2]<img.shape[0]-rad) if (survey=='usnob1'): output = np.column_stack((star_pix[:,0][mask][mask2][mask3], star_pix[:,0][mask][mask2][mask3], \ cat_ra[mask][mask2][mask3], cat_dec[mask][mask2][mask3], Bmag[mask][mask2][mask3], Rmag[mask][mask2][mask3], Imag[mask][mask2][mask3])) np.savetxt('/tmp/usnob1_cat.txt', output, fmt="%.5f %.5f %.5f %.5f %.5f %.5f %.5f", header='#X Y ra dec B R I') print "Saved to", '/tmp/usnob1_cat.txt' elif (survey=='apass'): if not np.any(mask2) and not np.any(mask3): print star_pix print "No stars left...", mask, mask2, mask3 return else: print catalog[mask][mask2][mask3] catalog = catalog[mask][mask2][mask3] s = star_pix[mask][mask2][mask3] z = np.zeros(len(s), dtype=[('x','f8'), ('y', 'f8')]) z['x'] = s[:,0] z['y'] = s[:,1] for n in catalog.dtype.names: z = rfn.append_fields(z, names=n, data=catalog[n], usemask=False) fmt = "%.5f" for i in range(len(z[0])-1): fmt += " %.5f" np.savetxt('/tmp/apass_cat.txt', z, fmt=fmt, \ header='x y radeg raerr decdeg decerr number_of_Obs V dV B dB g dg r dr i di') print "Saved to", '/tmp/apass_cat.txt' elif survey=='sdss': if not np.any(mask) and not np.any(mask2) and not np.any(mask3): print star_pix print "No stars left...", mask, mask2, mask3 else: catalog = catalog[mask][mask2][mask3] s = star_pix[mask][mask2][mask3] z = np.zeros(len(s), dtype=[('x','f8'), ('y', 'f8')]) z['x'] = s[:,0] z['y'] = s[:,1] for n in catalog.dtype.names: z = rfn.append_fields(z, names=n, data=catalog[n], usemask=False) fmt = "%.5f" for i in range(len(z[0])-1): fmt += " %.5f" np.savetxt('/tmp/sdss_cat.txt', z, fmt=fmt, \ header='x y objid run rerun camcol field obj type ra dec u g r i z du dg dr di dz') print "Saved to", '/tmp/sdss_cat.txt' #Find FWHM for this image out = find_fwhm(imfile, star_pix[:,1][mask][mask2][mask3], star_pix[:,0][mask][mask2][mask3], plot=debug) mask_valid_fwhm = (out['detected']) * (out['e']>0.7) * ~np.isnan(out['fwhm']* (out['fwhm'] < 30)) if (np.count_nonzero(mask_valid_fwhm) < 3): print "ERROR with FWHM!! Too few points for a valid estimation." return False outd = out[mask_valid_fwhm] print 'Average FWHM',np.median(outd['fwhm']), 'arcsec', np.median(outd['fwhm'])*pix2ang, 'pixels' fwhm = np.median(outd['fwhm']) fitsutils.update_par(imfile,'FWHM',fwhm) np.savetxt('/tmp/sdss_cat_det.txt', z[mask_valid_fwhm], fmt=fmt, \ header='x y objid run rerun camcol field obj type ra dec u g r i z du dg dr di dz') print "Saved to", '/tmp/sdss_cat_det.txt' #Plot results img = img - np.nanmin(img) zmin, zmax = zscale.zscale(img) print "Found %d stars in %s. "%(len(cat_dec), survey), \ "%d of them within the FoV. "%len(cat_ra[mask]),\ "%d of them are isolated."%len(cat_ra[mask][mask2]),\ "%d of them with suitable magnitudes. "%len(cat_ra[mask][mask2][mask3]),\ "%d of them with detected stars."%np.count_nonzero(mask_valid_fwhm) if (plot): im = plt.imshow(img, aspect="equal", origin="lower", cmap=matplotlib.cm.gray_r, interpolation="none", vmin=zmin, vmax=zmax) if (len(star_pix[:,0][mask]) >0): plt.scatter(star_pix[:,0][mask], star_pix[:,1][mask], marker="o", s=np.minimum(150, 10000*(10./mag[mask][mask2])**9), edgecolor="red", facecolor="none") if (len(star_pix[:,0][mask][mask2]) >0): plt.scatter(star_pix[:,0][mask][mask2], star_pix[:,1][mask][mask2], marker="o", s=20, edgecolor="yellow", facecolor="none") if (len(star_pix[:,0][mask][mask2][mask3]) >0): plt.scatter(star_pix[:,0][mask][mask2][mask3], star_pix[:,1][mask][mask2][mask3], marker="o", s=200, edgecolor="green", facecolor="none") selected = star_pix[:,:][mask][mask2][mask3][mask_valid_fwhm] if (len(selected) >0): plt.scatter(selected[:,0], selected[:,1], marker="o", \ s=400, edgecolor="blue", facecolor="none") for i in np.arange(len(selected)): plt.text(selected[i,0]+10, selected[i,1]+10, i+1) plt.savefig(imfile.replace('.fits', '.seqstars.png')) print "Saved stars to ",imfile.replace('.fits', '.seqstars.png') plt.clf() return True
def get_offset_center_failed_astro(f, plot=False, interactive=True): ''' For fields where astrometry is challenging, there is a simple solution. Find the brightest peak within the pointing error of the telescope. As this fields will usually be centered in Standard stars and very short exposure time, the fit intends to ''' image = pf.open(f) data = image[0].data wcs = pywcs.WCS(image[0].header) ra, dec = cc.hour2deg(image[0].header['OBJRA'], image[0].header['OBJDEC'] ) pra, pdec = wcs.wcs_sky2pix(ra, dec, 0) #Get a local image #xl, yl = np.array(wcs.wcs_sky2pix(ra+(60./3600)*np.cos(np.deg2rad(dec)), dec-60./3600, 0), dtype=np.int) #xu, yu = np.array(wcs.wcs_sky2pix(ra-(60./3600)*np.cos(np.deg2rad(dec)), dec+60./3600, 0), dtype=np.int) imageloc = image[0].data.T[1293-150:1293+150,1280-150:1280+150] nx = 300 ny=300 def_x = np.argmax(np.sum(imageloc, axis=0)) def_y = np.argmax(np.sum(imageloc, axis=1)) newx = pra-nx/2.+def_x newy = pdec-ny/2.+def_y pra, pdec = wcs.wcs_pix2sky(np.array([[newx[0], newy[0]]] , np.float_), 0)[0] dra, ddec = cc.get_offset(ra, dec, pra, pdec) print "Offset", dra, ddec, "Position RA,DEC", pra, pdec x,y, fwhmx, fwhmy, bkg, amp = fit_utils.fit_gauss(imageloc) if (plot): plt.figure(figsize=(8,8)) obj = fitsutils.get_par(f, "OBJECT") plt.suptitle(obj, fontsize=20) zmin, zmax = zscale.zscale(imageloc) plt.imshow(imageloc, aspect="auto", interpolation="none", origin="lower", vmin=zmin, vmax=zmax, extent=(0,+300,0,+300)) plt.plot(x, y, "go", ms=20, label="Centroid using gaussiuan fit.") plt.plot(def_x, def_y, "b*", ms=20, label="Centroid using max/min.") plt.plot(150,150,"wo", ms=20, label="Initial pointing") plt.legend() '''zmin, zmax = zscale.zscale(data) plt.imshow(data, aspect="auto", interpolation="none", origin="lower", vmin=zmin, vmax=zmax) plt.plot(newx, newy, "go") plt.show()''' if (interactive): plt.show() else: plt.savefig(os.path.join(os.path.dirname(f).replace("raw", "phot"), os.path.basename(f).replace(".fits", "_std.png"))) plt.clf() return 1, ddec, dra
def do_image(self, undone_image, contour_file=None, contour_levels=5, contour_color=None): """Make an image""" version = 4 success = False print("Making an image for " + undone_image) print("Using files... " + str(self.images[undone_image])) cube_data = pyfits.getdata( self.filenames[self.images[undone_image][0]]) z1_k, z2_k = zscale.zscale(np.nan_to_num(cube_data[0, ...])) z1_h, z2_h = zscale.zscale(np.nan_to_num(cube_data[1, ...])) z1_j, z2_j = zscale.zscale(np.nan_to_num(cube_data[2, ...])) aplpy.make_rgb_image( self.filenames[self.images[undone_image][0]], self.filenames[self.images[undone_image][0]].replace( '.fits', '_2d.png'), vmin_r=z1_k, vmax_r=z2_k, vmin_g=z1_h, vmax_g=z2_h, vmin_b=z1_j, vmax_b=z2_j) gc = aplpy.FITSFigure( self.filenames[self.images[undone_image][0]].replace( '.fits', '_2d.fits')) gc.show_rgb(self.filenames[self.images[undone_image][0]].replace( '.fits', '_2d.png')) gc.tick_labels.set_xformat("dd.dd") gc.tick_labels.set_yformat("dd.dd") #out_filename = self.filenames[undone_image].replace('.fits','.pdf') if contour_file: #gc.show_contour(contour_file,levels=contour_levels,colors="black",linewidths = 3.,smooth=3.,alpha=0.5) gc.show_contour(contour_file, levels=contour_levels, colors=contour_color, linewidths=1.) #chunks = contour_file.split('_') #print(chunks) #linename = chunks[3] #moment_name = chunks[4][0:-5] #tag = "_"+linename+"_"+moment_name #print(tag) #out_filename = out_filename.replace('.pdf',tag+'.pdf') #self.add_labels(gc,undone_image,linename,moment_name = moment_name,color=contour_color) else: pass #self.add_labels(gc,undone_image) #gc.show_rectangles(self.apos,self.bpos,0.0625,0.0625,ec='w',lw=2,facecolor='none',zorder=1) #gc.show_ellipses(self.apos,self.bpos,self.awin*2,self.bwin*2,angle=self.twin,facecolor='none',ec='green',lw=2) #print(self.awin,self.bwin) success = True if success: self.report_success(undone_image, version) return (gc)
# Get pixel coordinates of SN wcs = pywcs.WCS(prihdr) try: target_pix = wcs.wcs_sky2pix( [(np.array([ra, dec], np.float_))], 1)[0] except: print "ERROR when converting sky to wcs. Is astrometry in place? Default coordinates assigned." target_pix = [+nx / 2., ny / 2.] print target_pix else: target_pix = [+nx / 2., ny / 2.] img = img - np.nanmin(img) av = np.median(img.flatten()) mi, ma = zscale.zscale(img) im = plt.imshow(plt.log10(img), aspect="equal", extent=(0, ny, 0, nx), \ origin="lower", cmap=matplotlib.cm.gray_r, interpolation="none", vmin=np.log10(av), vmax=np.log10(3*av)) #, interpolation="lanczos") plt.scatter(target_pix[0], target_pix[1], marker="x", s=10, c="red") plt.colorbar(im) filename = os.path.basename(f) plt.savefig(os.path.join( plot_dir, filename.replace("." + filename.split(".")[-1], "_{:}.png".format(i))), dpi=200) plt.clf()