class GPSFermiPlot(GalacticPlaneSurveyPanelPlot): def main(self, figure, subplot): filename = FermiGalacticCenter.filenames()['counts'] self.fits_figure = FITSFigure(filename, figure=figure, subplot=subplot) self.fits_figure.show_colorscale(vmin=1, vmax=10) self.fits_figure.ticks.set_xspacing(2)
def test_scalebar_length(): data = np.zeros((16, 16)) f = FITSFigure(data) f.add_scalebar(0.1) f.scalebar.set_length(0.01) f.scalebar.set_length(0.1) f.close()
def test_frame_linewidth(): data = np.zeros((16, 16)) f = FITSFigure(data) f.frame.set_linewidth(0) f.frame.set_linewidth(1) f.frame.set_linewidth(10) f.close()
def test_scalebar_showhide(): data = np.zeros((16, 16)) f = FITSFigure(data) f.add_scalebar(0.1) f.scalebar.hide() f.scalebar.show(0.1) f.close()
def test_scalebar_font(): data = np.zeros((16, 16)) f = FITSFigure(data) f.add_scalebar(0.1) f.scalebar.set_font(size='small', weight='bold', stretch='normal', family='serif', style='normal', variant='normal') f.close()
def test_scalebar_frame(): data = np.zeros((16, 16)) f = FITSFigure(data) f.add_scalebar(0.1) f.scalebar.set_frame(True) f.scalebar.set_frame(False) f.close()
def test_ticks_minor_frequency(): data = np.zeros((16, 16)) f = FITSFigure(data) f.ticks.set_minor_frequency(1) f.ticks.set_minor_frequency(5) f.ticks.set_minor_frequency(10) f.close()
def test_ticks_linewidth(): data = np.zeros((16, 16)) f = FITSFigure(data) f.ticks.set_linewidth(1) f.ticks.set_linewidth(3) f.ticks.set_linewidth(10) f.close()
def test_ticks_color(): data = np.zeros((16, 16)) f = FITSFigure(data) f.ticks.set_color('black') f.ticks.set_color('#003344') f.ticks.set_color((1.0, 0.4, 0.3)) f.close()
def test_grid_addremove(): data = np.zeros((16, 16)) f = FITSFigure(data) f.add_grid() f.remove_grid() f.add_grid() f.close()
def test_scalebar_addremove(): data = np.zeros((16, 16)) f = FITSFigure(data) f.add_scalebar(0.1) f.remove_scalebar() f.add_scalebar(0.1) f.close()
def part_8(): object_list = ['ROXs12','ROXs42B','ROXs42B'] filename_list = ['ROXs12','ROXs42Bb','ROXs42Bc'] for i in range(len(filename_list)): outfile = open('position_'+filename_list[i]+'.dat','w') outfile.write('# '+'%25s'%'file name\t'\ +'%12s'%'x planet\t'\ +'%12s'%'y planet\t'\ +'%12s'%'position angle\t'\ +'%12s'%'projected separation (pixels)\n') if filename_list[i] == 'ROXs12': int_x,int_y = 562,685 elif filename_list[i] == 'ROXs42Bb': int_x,int_y = 629,495 elif filename_list[i] == 'ROXs42Bc': int_x,int_y = 546,465 parts = ['','_CMsub','_ADIsub','_bPSFsub'] for j in parts: filename = object_list[i]+j+'_aligned_median.fits' fit_radius = 16 f = fits.open(filename) scidata = f[0].data x_data = scidata[int_y,int_x-fit_radius:int_x+fit_radius+1] x_data -= min(x_data) fit_x = np.linspace(-fit_radius,fit_radius,1+2*fit_radius) xfitParam, xfitCovar = curve_fit(gaussian_pdf,fit_x,x_data) y_data = scidata[int_y-fit_radius:int_y+fit_radius+1,int_x] y_data -= min(y_data) fit_y = np.linspace(-fit_radius,fit_radius,1+2*fit_radius) yfitParam, yfitCovar = curve_fit(gaussian_pdf,fit_y,y_data) thisPARANG = f[0].header['PARANG'] thisROTPPOSN = f[0].header['ROTPPOSN'] thisEL = f[0].header['EL'] thisINSTANGL = f[0].header['INSTANGL'] PA_yaxis = thisPARANG +thisROTPPOSN -thisEL -thisINSTANGL PA_planet = PA_yaxis -rad_to_deg(arctan2(float(xfitParam[0]+int_x-512),float(yfitParam[0]+int_y-512))) if PA_planet < 0: PA_planet += 360 outfile.write('%25s'%str(object_list[i]+j+'_aligned_median.fits')+'\t'\ +'%8s'%'%.3f'%(float(xfitParam[0]+int_x))+'\t'\ +'%8s'%'%.3f'%(float(yfitParam[0]+int_y))+'\t'\ +'%8s'%'%.3f'%(float(PA_planet))+'\t'\ +'%8s'%'%.3f'%(float(sqrt((xfitParam[0]+int_x-512)**2+(yfitParam[0]+int_y-512)**2)))+'\n') tf = FITSFigure(filename) tf.show_colorscale(cmap='gray') tf.add_colorbar() tf.colorbar.show(location='top',box_orientation='horizontal') plt.savefig(object_list[i]+j+'_aligned_median.pdf',dpi=200) plt.close() outfile.close()
def test_scalebar_corner(): data = np.zeros((16, 16)) f = FITSFigure(data) f.add_scalebar(0.1) for corner in ['top', 'bottom', 'left', 'right', 'top left', 'top right', 'bottom left', 'bottom right']: f.scalebar.set_corner(corner) f.close()
def test_tick_labels_position(): data = np.zeros((16, 16)) f = FITSFigure(data) f.tick_labels.set_xposition('top') f.tick_labels.set_xposition('bottom') f.tick_labels.set_yposition('right') f.tick_labels.set_yposition('left') f.close()
def test_beam_linestyle(): data = np.zeros((16, 16)) f = FITSFigure(data) f.add_beam(major=0.1, minor=0.04, angle=10.) f.beam.set_linestyle('solid') f.beam.set_linestyle('dotted') f.beam.set_linestyle('dashed') f.close()
def test_axis_labels_position(): data = np.zeros((16, 16)) f = FITSFigure(data) f.axis_labels.set_xposition('top') f.axis_labels.set_xposition('bottom') f.axis_labels.set_yposition('right') f.axis_labels.set_yposition('left') f.close()
def test_beam_edgecolor(): data = np.zeros((16, 16)) f = FITSFigure(data) f.add_beam(major=0.1, minor=0.04, angle=10.) f.beam.set_edgecolor('black') f.beam.set_edgecolor('#003344') f.beam.set_edgecolor((1.0, 0.4, 0.3)) f.close()
def test_scalebar_alpha(): data = np.zeros((16, 16)) f = FITSFigure(data) f.add_scalebar(0.1) f.scalebar.set_alpha(0.1) f.scalebar.set_alpha(0.2) f.scalebar.set_alpha(0.5) f.close()
def test_grid_color(): data = np.zeros((16, 16)) f = FITSFigure(data) f.add_grid() f.grid.set_color('black') f.grid.set_color('#003344') f.grid.set_color((1.0, 0.4, 0.3)) f.close()
def test_grid_alpha(): data = np.zeros((16, 16)) f = FITSFigure(data) f.add_grid() f.grid.set_alpha(0.0) f.grid.set_alpha(0.3) f.grid.set_alpha(1.0) f.close()
def test_scalebar_color(): data = np.zeros((16, 16)) f = FITSFigure(data) f.add_scalebar(0.1) f.scalebar.set_color('black') f.scalebar.set_color('#003344') f.scalebar.set_color((1.0, 0.4, 0.3)) f.close()
def test_beam_linewidth(): data = np.zeros((16, 16)) f = FITSFigure(data) f.add_beam(major=0.1, minor=0.04, angle=10.) f.beam.set_linewidth(0) f.beam.set_linewidth(1) f.beam.set_linewidth(5) f.close()
def test_scalebar_label(): data = np.zeros((16, 16)) f = FITSFigure(data) f.add_scalebar(0.1) f.scalebar.set_label('1 pc') f.scalebar.set_label('5 AU') f.scalebar.set_label('2"') f.close()
def main(self, figure, subplot): filename = FermiGalacticCenter.filenames()['counts'] self.fits_figure = FITSFigure(filename, hdu=1, figure=figure, subplot=subplot) self.fits_figure.show_colorscale(vmin=1, vmax=10, cmap='afmhot') self.fits_figure.ticks.set_xspacing(2)
def test_grid_linestyle(): data = np.zeros((16, 16)) f = FITSFigure(data) f.add_grid() f.grid.set_linestyle('solid') f.grid.set_linestyle('dashed') f.grid.set_linestyle('dotted') f.close()
def test_grid_linewidth(): data = np.zeros((16, 16)) f = FITSFigure(data) f.add_grid() f.grid.set_linewidth(0) f.grid.set_linewidth(2) f.grid.set_linewidth(5) f.close()
def test_ticks_spacing(): data = np.zeros((16, 16)) f = FITSFigure(data) f.ticks.set_xspacing(0.5) f.ticks.set_xspacing(1.) f.ticks.set_yspacing(0.5) f.ticks.set_yspacing(1.) f.close()
def test_beam_corner(): data = np.zeros((16, 16)) f = FITSFigure(data) f.add_beam(major=0.1, minor=0.04, angle=10.) for corner in ['top', 'bottom', 'left', 'right', 'top left', 'top right', 'bottom left', 'bottom right']: f.beam.set_corner(corner) f.close()
def test_init_extensive_wcs(header, dimensions): hdu = generate_hdu(header) if 'CAR' in header: f = FITSFigure(hdu, dimensions=dimensions, convention='calabretta') else: f = FITSFigure(hdu, dimensions=dimensions) f.show_grayscale() f.add_grid() f.close()
def fits_to_png(infile, outfile, draw, dpi=100): """Plot FITS image in PNG format. For the default ``dpi=100`` a 1:1 copy of the pixels in the FITS image and the PNG image is achieved, i.e. they have exactly the same size. Parameters ---------- infile : str Input FITS file name outfile : str Output PNG file name draw : callable Callback function ``draw(figure)`` where ``figure`` is an `~aplpy.FITSFigure`. dpi : int Resolution Examples -------- >>> def draw(figure): ... x, y, width, height = 42, 0, 3, 2 ... figure.recenter(x, y, width, height) ... figure.show_grayscale() >>> from gammapy.image import fits_to_png >>> fits_to_png('image.fits', 'image.png', draw) """ import matplotlib matplotlib.use("Agg") # Prevents image popup import matplotlib.pyplot as plt from astropy.io import fits from aplpy import FITSFigure # Peak ahead just to get the figure size NAXIS1 = float(fits.getval(infile, "NAXIS1")) NAXIS2 = float(fits.getval(infile, "NAXIS2")) # Note: For dpi=100 I get exactly the same FITS and PNG image size in pix. figsize = np.array((NAXIS1, NAXIS2)) figure = plt.figure(figsize=figsize / dpi) # Also try this: # matplotlib.rcParams['figure.figsize'] = NAXIS1, NAXIS2 # figsize(x,y) subplot = [0, 0, 1, 1] figure = FITSFigure(infile, figure=figure, subplot=subplot) draw(figure) figure.axis_labels.hide() figure.tick_labels.hide() figure.ticks.set_linewidth(0) figure.frame.set_linewidth(0) figure.save(outfile, max_dpi=dpi, adjust_bbox=False)
def test_axis_labels_show_hide(): data = np.zeros((16, 16)) f = FITSFigure(data) f.axis_labels.hide() f.axis_labels.show() f.axis_labels.hide_x() f.axis_labels.show_x() f.axis_labels.hide_y() f.axis_labels.show_y() f.close()
def test_ticks_show_hide(): data = np.zeros((16, 16)) f = FITSFigure(data) f.ticks.hide() f.ticks.show() f.ticks.hide_x() f.ticks.show_x() f.ticks.hide_y() f.ticks.show_y() f.close()
def test_axis_labels_font(): data = np.zeros((16, 16)) f = FITSFigure(data) f.axis_labels.set_font(size='small', weight='bold', stretch='normal', family='serif', style='normal', variant='normal') f.close()
def fits_to_png(infile, outfile, draw, dpi=100): """Plot FITS image in PNG format. For the default ``dpi=100`` a 1:1 copy of the pixels in the FITS image and the PNG image is achieved, i.e. they have exactly the same size. Parameters ---------- infile : str Input FITS file name outfile : str Output PNG file name draw : callable Callback function ``draw(figure)`` where ``figure`` is an `~aplpy.FITSFigure`. dpi : int Resolution Examples -------- >>> def draw(figure): ... x, y, width, height = 42, 0, 3, 2 ... figure.recenter(x, y, width, height) ... figure.show_grayscale() >>> from gammapy.image import fits_to_png >>> fits_to_png('image.fits', 'image.png', draw) """ import matplotlib matplotlib.use('Agg') # Prevents image popup import matplotlib.pyplot as plt from astropy.io import fits from aplpy import FITSFigure # Peak ahead just to get the figure size NAXIS1 = float(fits.getval(infile, 'NAXIS1')) NAXIS2 = float(fits.getval(infile, 'NAXIS2')) # Note: For dpi=100 I get exactly the same FITS and PNG image size in pix. figsize = np.array((NAXIS1, NAXIS2)) figure = plt.figure(figsize=figsize / dpi) # Also try this: # matplotlib.rcParams['figure.figsize'] = NAXIS1, NAXIS2 # figsize(x,y) subplot = [0, 0, 1, 1] figure = FITSFigure(infile, figure=figure, subplot=subplot) draw(figure) figure.axis_labels.hide() figure.tick_labels.hide() figure.ticks.set_linewidth(0) figure.frame.set_linewidth(0) figure.save(outfile, max_dpi=dpi, adjust_bbox=False)
def test_axis_labels_position_invalid(): data = np.zeros((16, 16)) f = FITSFigure(data) with pytest.raises(ValueError): f.axis_labels.set_xposition('right') with pytest.raises(ValueError): f.axis_labels.set_xposition('left') with pytest.raises(ValueError): f.axis_labels.set_yposition('top') with pytest.raises(ValueError): f.axis_labels.set_yposition('bottom') f.close()
def test_beam_angle(): data = np.zeros((16, 16)) f = FITSFigure(data) f.show_grayscale() f.add_beam(major=0.1, minor=0.04, angle=10.) f.beam.set_angle(0.) f.beam.set_angle(55.) f.close()
def test_colorbar_showhide(): data = np.zeros((16, 16)) f = FITSFigure(data) f.show_grayscale() f.add_colorbar() f.colorbar.hide() f.colorbar.show() f.close()
def test_colorbar_pad(): data = np.zeros((16, 16)) f = FITSFigure(data) f.show_grayscale() f.add_colorbar() f.colorbar.set_pad(0.1) f.colorbar.set_pad(0.2) f.colorbar.set_pad(0.5) f.close()
def lookAtHDU(hdulist, outputFileName, v1=0, v2=10, channelMode=False, coAddSpectra=False, lowVelCut = 0, highVelCut = 0, plotInKmPerS = True): hdu = hdulist[0] workingHeader = hdu.header img = momentcube(hdu, v1, v2, moment=0, chan=channelMode) workingImageHeader = img.header imglist = pyfits.PrimaryHDU(data=img.data, header=img.header) # write an output file hdulist2 = pyfits.hdu hdulist2 = pyfits.HDUList([imglist]) if os.path.exists(outputFileName): os.remove(outputFileName) hdulist2.writeto(outputFileName) if coAddSpectra: (axis3Len, axis2Len, specLen) = numpy.shape(hdu.data) specLen -= lowVelCut specLen -= highVelCut meanSpectra = numpy.zeros((specLen)) for axis3Index in range(axis3Len): for axis2Index in range(axis2Len): meanSpectra += hdu.data[axis3Index, axis2Index, highVelCut:specLen+highVelCut] meanSpectra = meanSpectra / float(axis3Len * axis2Len) vAxisStart = hdu.header['CRVAL1'] - ((hdu.header['CRPIX1'] - 1.0) * hdu.header['CDELT1']) vAxisEnd = hdu.header['CRVAL1'] + (((specLen + 1) - hdu.header['CRPIX1'] ) * hdu.header['CDELT1']) vAxisStep = hdu.header['CDELT1'] velocityAxis = numpy.arange(vAxisStart, vAxisEnd, vAxisStep) if plotInKmPerS: velocityAxis = velocityAxis / 1000.0 plt.xlabel("Relative Velocity (km/s)") else: plt.xlabel("Relative Velocity (m/s)") velocityAxis = list(velocityAxis) while specLen < len(velocityAxis): velocityAxis.pop() plt.plot(meanSpectra) # plt.plot(velocityAxis, meanSpectra) plt.title("max value:" + str(max(meanSpectra)) + " Min value:" + str(min(meanSpectra))) plt.show() fig = FITSFigure(img) fig.show_colorscale( ) # time.sleep(20) # raw_input('press enter to continue') return
def test_colorbar_location(): data = np.zeros((16, 16)) f = FITSFigure(data) f.show_grayscale() f.add_colorbar() f.colorbar.set_location('top') f.colorbar.set_location('bottom') f.colorbar.set_location('left') f.colorbar.set_location('right') f.close()
def test_write_eps(tmpdir, format): filename = os.path.join(str(tmpdir), 'test_output.eps') data = np.zeros((16, 16)) f = FITSFigure(data) f.show_grayscale() f.save(filename, format=format) f.close() if format is None: assert is_format(filename, 'eps') else: assert is_format(filename, format)
def test_write_stringio(tmpdir, format): s = StringIO() data = np.zeros((16, 16)) f = FITSFigure(data) f.show_grayscale() f.save(s, format=format) f.close() s.seek(0) if format is None: assert is_format(s, 'png') else: assert is_format(s, format)
def test_colorbar_font(): data = np.zeros((16, 16)) f = FITSFigure(data) f.show_grayscale() f.add_colorbar() f.colorbar.set_font(size='small', weight='bold', stretch='normal', family='serif', style='normal', variant='normal') f.close()
def test_beam_hatch(): data = np.zeros((16, 16)) f = FITSFigure(data) f.add_beam(major=0.1, minor=0.04, angle=10.) for hatch in ['/', '\\', '|', '-', '+', 'x', 'o', 'O', '.', '*']: f.beam.set_hatch(hatch) f.close()
def test_grid_showhide(): data = np.zeros((16, 16)) f = FITSFigure(data) f.add_grid() f.grid.hide() f.grid.show() f.close()
def test_beam_frame(): data = np.zeros((16, 16)) f = FITSFigure(data) f.add_beam(major=0.1, minor=0.04, angle=10.) f.beam.set_frame(True) f.beam.set_frame(False) f.close()
def test_beam_pad(): data = np.zeros((16, 16)) f = FITSFigure(data) f.add_beam(major=0.1, minor=0.04, angle=10.) f.beam.set_pad(0.1) f.beam.set_pad(0.3) f.close()