def save_to_pdf(data1, sources1, data2, sources2, dirname=None): """ Save the results of the matched stars to a multipage pdf Using the common sources, save 40 by 40 pixel cutouts of each matched source. There will be a total of 16 cutouts per page, that are split into two groups of 8. Within each group of 8, the matching sources will be displayed column-wise. The top row of 4 stars will all be distinct and their matches will be displayed in the row immediately below. Parameters ---------- data1 : numpy.ndarray FITS data from image 1 sources1 : astropy.table.Table Catalog of matched sources found in image 1 data2 : numpy.ndarray Label for all cosmic rays in data1 sources2 : astropy.table.Table Catalog of matches sources found in image 2 dirname : Path to the directory containing the images that were analyzed Returns ------- None This function will save a pdf to the results directory containing the matched stars used in the analysis """ ncolors = np.max(data2) + 1 prng = np.random.RandomState(1234) h = prng.uniform(low=0.0, high=1.0, size=ncolors) s = prng.uniform(low=0.2, high=0.7, size=ncolors) v = prng.uniform(low=0.5, high=1.0, size=ncolors) hsv = np.dstack((h, s, v)) rgb = np.squeeze(colors.hsv_to_rgb(hsv)) rgb[0] = (0, 0, 0) cmap = colors.ListedColormap(rgb) mk_patch = lambda xy, r, c, lw, fill: patch.Circle(xy=xy, radius=r, color=c, fill=fill, lw=lw) norm = ImageNormalize(data1, stretch=LogStretch(), vmin=0, vmax=1000) # interval=ZScaleInterval()) outname = 'centroid_comparison.pdf' print('Total number of sources {}'.format(len(sources1))) with PdfPages(outname) as pdf: num_pages = int(np.ceil(len(sources1) / 16)) # num_pages = 20 start_idx = 0 for i in range(num_pages): start_idx += 8 sources1_to_plot = sources1[start_idx: start_idx + 8] sources2_to_plot = sources2[start_idx: start_idx + 8] # Initalize the plot, each axes list will contain 8 plots # The plots should be organize by columns, i.e. two plots in # the same column correspond to the same image fig, axes00, axes10 = mk_grid() # i will run from 0 to 15 (i.e. 16 elements) for j in range(len(sources1_to_plot)): cr1 = sources1_to_plot[j] cr2 = sources2_to_plot[j] # limits for the first star # print(j) if j < 4: # print(j%4) ax1 = axes00[j] else: # print(j%4) ax1 = axes10[j % 4] cutout_size = 40 # Limits for the first plot xlimits1 = cr1['xcenter'] - cutout_size / 2, \ cr1['xcenter'] + cutout_size / 2 ylimits1 = cr1['ycenter'] - cutout_size / 2, \ cr1['ycenter'] + cutout_size / 2 if j < 4: # print(j%4 + 4) ax2 = axes00[j % 4 + 4] else: # print(j%4 + 4) ax2 = axes10[j % 4 + 4] # Limits for the second plot xlimits2 = cr2['xcenter'] - cutout_size/2, \ cr2['xcenter'] + cutout_size/2 ylimits2 = cr2['ycenter'] - cutout_size/2, \ cr2['ycenter'] + cutout_size/2 im1 = ax1.imshow(data1, norm=norm, cmap='gray', origin='lower') im2 = ax2.imshow(data2, cmap=cmap, origin='lower') # Add axes for color bar to show scale divider = make_axes_locatable(ax1) cax1 = divider.append_axes("right", size="8%", pad=0.05) cbar = fig.colorbar(im1, cax=cax1) n = len(cbar.ax.get_yticklabels()) labels_to_hide = [n-3, n - 2] loop = zip(cbar.ax.get_yticklabels(), cbar.ax.yaxis.get_major_ticks()) for i, (label, tick) in enumerate(loop): if i in labels_to_hide: label.set_visible(False) tick.set_visible(False) cbar.ax.set_yticklabels(cbar.ax.get_yticklabels(), rotation=10, horizontalalignment='left', verticalalignment='center', fontsize=5 ) cbar.update_ticks() flux_max_patch = mk_patch((cr1['xmax'], cr1['ymax']), r=0.5, c='magenta', lw=1, fill=True) ax1.add_patch(flux_max_patch) flux_center_patch = mk_patch((cr1['xcenter'], cr1['ycenter']), r=0.5, c='red', lw=1., fill=True) ax1.add_patch(flux_center_patch) geo_center_patch = mk_patch((cr2['xcenter'], cr2['ycenter']), r=0.5, c='blue', lw=1., fill=True) ax1.add_patch(geo_center_patch) current_label_patch = mk_patch((cr2['xcenter'], cr2['ycenter']), r=4, c='white', lw=1.5, fill=False) ax2.add_patch(current_label_patch) ax1.set_title('Red: flux-weight\n ' 'Blue: uniform-weight \n' 'Magneta: max value', fontsize='medium') ax2.set_title('Label', fontsize='medium') # Set the plot limits for star 1 ax1.set_xlim(xlimits1[0], xlimits1[1]) ax1.set_ylim(ylimits1[0], ylimits1[1]) # Set the plot limits for star 2 ax2.set_xlim(xlimits2[0], xlimits2[1]) ax2.set_ylim(ylimits2[0], ylimits2[1]) ax1.grid(False) ax2.grid(False) # add colorbar to outer grid pdf.savefig(fig) plt.close()
def for i in range(5): file125 = 'gal%s_UV_F125_scale_04_psfmatch.fits'%(i+1) hd = fits.open(file125) photflam = float(hd[1].header['PHOTFLAM']) data_125 = hd[1].data section_gal = 'NULIRG%s' % (int(i + 1)) params, params_gal = basic_params(config_file, 'basic', section_gal) cent_x, cent_y = UV_centers_drz(file125) print ('=============================================\n') print ('<<<<<<<<<<<<<<<<<<<<<ULIRG %s>>>>>>>>>>>>>>>>>>\n'%(i+1)) print ('=============================================\n') print ('Original shape', data.shape) print ('UV 125 centers --> (%s, %s)'% (cent_x, cent_y)) print (' ----> Making annuli masks\n') width = 4.0 aper_lim = 300 if i ==4: aper_lim = 300*0.158/0.131 nx = data_125.shape[0] ny = data_125.shape[1] rad1, rad_annulus, masks, masks_annulus = masks_circular(cent_x, cent_y, width, aper_lim, nx, ny) ################## computing center boxes ################ if i ==1: x = [150, 150, 150, 350, 350, 350, 550, 550, 550] y = [150, 350, 550, 150, 350, 550, 150, 350, 550] elif i ==2: x = [150, 150, 150, 150, 350, 350, 350, 350, 550, 550, 550, 550, 750, 750, 750, 750] y = [150, 350, 550, 750, 150, 350, 550, 750, 150, 350, 550, 750, 150, 350, 550, 750] else: x = [350, 350, 350, 550, 550, 550, 750, 750, 750] y = [350, 550, 750, 350, 550, 750, 350, 550, 750] fig,((ax1, ax2), (ax3, ax4)) = plt.subplots(2,2, figsize = (30,22)) [plot_rectangle(x[i],y[i], szx, szy, color[i], ax1) for i in range(len(x))] norm = ImageNormalize(data_125, stretch=AsinhStretch()) img = ax1.imshow(data_125, origin = 'lower', vmin =0.0005, vmax = 0.001, norm = norm, cmap = plt.cm.Greys_r, zorder =0) divider = make_axes_locatable(ax1) cax = divider.append_axes("right", size="5%", pad=0.0) cbar = plt.colorbar(img,cax=cax, orientation='vertical') cbar.minorticks_on data_lya = sum_ext(i, 'A_lmfit_x') err_lya = sum_ext(i, 'A_lmfit_err') ### positive flux cond #data_all[data_all<0] = 0.0 cond_positive = data_lya < 0.0 cond_negative = data_lya > 0.0 #### SN >1 condition SN_all = abs(data_lya/err_lya) #SN_add = data_lya + err_lya #SN_diff = data_lya - err_lya #cond_sn = np.isnan(SN_all) #SN_all[cond_sn] = 0.0 #SN_all[cond_sn_tot] = 0.0 b_all = sum_ext(i, 'b_lmfit_x') cond_b = abs(abs(b_all)-3) <1e-5 status_all = sum_ext(i, 'status_lmfit_x') cond_status = status_all == 0 data_neg = np.copy(data_all) data_test = np.copy(data_all) aper_total = [len(data_neg[masks_annulus[k]]) for k in range(len(rad1)-1) ] aper_nonzero_total = [non_zero(data_test[masks_annulus[k]]) for k in range(len(rad1)-1) ] cond_test = cond_sn_tot + cond_b + cond_status data_test[cond_test] = 0.0 aper_nonzero_total_cutoff = [non_zero(data_test[masks_annulus[k]]) for k in range(len(rad1)-1) ] cond_all = cond_sn_tot + cond_b + cond_status + cond_positive data_all[cond_all] = 0.0 SN_add[cond_all] = 0.0 SN_diff[cond_all] = 0.0 cond_neg = cond_sn_tot + cond_b + cond_status + cond_negative data_neg[cond_neg] = 0.0 #data_neg = abs(data_neg) #img = ax1.imshow(data_all, origin = 'lower', vmin =-1e-15, vmax = 1e-20, norm = norm, cmap = plt.cm.Greys_r, zorder =0) fits.writeto('./ULIRG%s_test/data%s.fits'%(i+1, i+1), data = data_test, overwrite = True) fits.writeto('./ULIRG%s_test/SN%s.fits'%(i+1, i+1), data = SN_all, overwrite = True) fits.writeto('./ULIRG%s_test/b%s.fits'%(i+1, i+1), data = b_all, overwrite = True) fits.writeto('./ULIRG%s_test/status%s.fits'%(i+1, i+1), data = status_all, overwrite = True) aper_SN_low = [np.mean(SN_add[masks_annulus[k]]) for k in range(len(rad1)-1) ] aper_SN_high = [np.mean(SN_diff[masks_annulus[k]]) for k in range(len(rad1)-1) ] sum_SN_low = [np.sum(SN_add[masks[k]]) for k in range(len(rad1)) ] sum_SN_high = [np.sum(SN_diff[masks[k]]) for k in range(len(rad1)) ] sum_125 = [np.sum(data_all[masks[k]]) for k in range(len(rad1)) ] aper_125 = [np.mean(data_all[masks_annulus[k]]) for k in range(len(rad1)-1) ] sum_125_neg = [np.sum(abs(data_neg[masks[k]])) for k in range(len(rad1)) ] aper_125_neg = [np.mean(abs(data_neg[masks_annulus[k]])) for k in range(len(rad1)-1) ] sum_UV = [np.nansum(data[masks[k]]*photflam*150) for k in range(len(rad1)) ] aper_UV = [np.nanmean(data[masks_annulus[k]]*photflam*150) for k in range(len(rad1)-1) ] aper_nonzero = [non_zero(data_all[masks_annulus[k]]) for k in range(len(rad1)-1) ] aper_nonzero_neg = [non_zero(data_neg[masks_annulus[k]]) for k in range(len(rad1)-1) ] rad_kpc = [rad*0.04*scale[i] for rad in rad1] rad_annulus_kpc = [rad*0.04*scale[i] for rad in rad_annulus] diff = [(-aper_125_neg[k] + aper_125[k]) for k in range(len (rad_annulus))] diff_sum = [(-sum_125_neg[k] + sum_125[k]) for k in range(len (rad1))] #norm_sum = [x1/sum_125[-1] for x1 in sum_125] #low = [aper_125[i] -aper_125[i] /aper_SN[i] for i in range(len (rad_annulus))] #high = [aper_125[i] +aper_125[i]/aper_SN[i] for i in range(len (rad_annulus))] ax4.plot(rad_kpc, sum_125, 'r', label = r'$Ly\alpha$ positive') ax4.fill_between(rad_kpc, sum_SN_low, sum_SN_high, alpha =0.1, color = 'b', zorder =1) ax4.plot(rad_kpc, sum_125_neg, 'g', label = r'abs($Ly\alpha$ negative)') ax4.plot(rad_kpc, diff_sum, 'b', label = r'($Ly\alpha$ total)') ax4.plot(rad_kpc, sum_UV, 'k', label = 'F125 flux') ax3.plot(rad_annulus_kpc, aper_125, 'r', label = r'$Ly\alpha$ positive') ax3.fill_between(rad_annulus_kpc, aper_SN_low, aper_SN_high, alpha =0.1, color = 'b', zorder =1) ax3.plot(rad_annulus_kpc, aper_125_neg, 'g', label = r'abs($Ly\alpha$ negative)') ax3.plot(rad_annulus_kpc, diff, 'b', label = r'($Ly\alpha$ total)') ax3.plot(rad_annulus_kpc, aper_UV, 'k', lw = 2.0, label = 'F125 flux') theoretical = [2*np.pi*k*width for k in rad_annulus] ax2.plot(rad_annulus_kpc, theoretical, color = 'k', zorder =2,lw = 2.0, label = 'Expected') ax2.fill_between(rad_annulus_kpc, aper_total, alpha =0.1, color = 'b', zorder =1, label = 'Total') ax2.fill_between(rad_annulus_kpc, aper_nonzero_total, alpha =0.2, color = 'c', zorder =1, label = 'Total non-zero') ax2.fill_between(rad_annulus_kpc, aper_nonzero_total_cutoff, alpha =0.3, color = 'm', zorder =1, label = 'Quality fit cutoff') ax2.fill_between(rad_annulus_kpc, aper_nonzero, alpha =0.3, color = 'r', zorder =2, label ='Positive') ax2.fill_between(rad_annulus_kpc, aper_nonzero_neg, alpha =0.6, color = 'g', zorder =3, label = 'Negative') ''' #testing the total counts test = [aper_nonzero[k] + aper_nonzero_neg[k]- aper_nonzero_total_cutoff[k] for k in range(len (rad_annulus))] ax1.fill_between(rad_annulus_kpc, test, alpha =0.5, color = 'g', zorder =2, label = 'negative') ''' #ax2.plot(rad_annulus_kpc, aper_UV, ) ax3.set_xlim(0, 32) ax4.set_xlim(0, 32) ax2.set_xlim(0, 32) ax2.set_yscale('log') ax2.set_ylabel('Number of points') ax2.set_xlabel('Distance from UV center [kpc]') ax3.set_ylabel(r'Aperture mean Flux[in ergs-$s^{-1}$-$cm^{-2}$-arcsec$^{-2}$]') ax3.set_xlabel('Distance from UV center [kpc]') ax3.set_yscale('log') ax3.axvline(x = petro_all[i], color = 'm', linestyle='--', label = r'$2\times R_p$') ax4.axvline(x = petro_all[i], color = 'm', linestyle = '--', label = r'$2\times R_p$') ax2.legend(loc ='lower left') ax3.legend(loc ='upper right') ax1.set_xlim(0,1000) ax1.set_ylim(0,1000) ax1.plot(cent_x, cent_y, marker ='x', color = 'b', mew=5, ms=20) circle(cent_x, cent_y, aper_lim, 'c', ax1) circle(cent_x, cent_y, petro_all[i]/(0.05*scale[i]), 'm', ax1) new_label_x = np.linspace((cent_x-sz[i]), (cent_x+sz[i]), 10)#*0.05*phys ) new_label_y = np.linspace((cent_y-sz[i]), (cent_y+sz[i]), 10)#*0.05*phys ) ar_x = np.round((new_label_x- cent_x)*0.04*scale[i] ) ar_y = np.round((new_label_y - cent_y)*0.04*scale[i] ) ax1.set_yticks(new_label_y) ax1.set_yticklabels(int(x) for x in (ar_y)) ax1.set_xticks(new_label_x) ax1.set_xticklabels((int(x) for x in (ar_x))) ax1.set_xlabel('kpc') ax1.set_ylabel('kpc') ax1.set_aspect(1.) ax4.legend(loc = 'upper left') ax4.set_ylabel(r'Cumulative Flux[in ergs-$s^{-1}$-$cm^{-2}$-arcsec$^{-2}$]') ax4.set_xlabel('Distance from UV center [kpc]') keys = ['radius', 'Lya_pos', 'UV', 'Lya_neg', 'Lya_total', ] dict_annuli = OrderedDict.fromkeys(keys) dict_annuli['radius'] = rad_annulus_kpc dict_annuli['Lya_pos'] = aper_125 dict_annuli['UV'] = aper_UV dict_annuli['Lya_neg'] = aper_125_neg dict_annuli['Lya_total'] = diff df = pd.DataFrame.from_dict(dict_annuli) df.to_csv('ULIRG%s_annuli.csv'%(i+1), columns = keys, index = True) dict_sum = OrderedDict.fromkeys(keys) dict_sum['radius'] = rad_kpc dict_sum['Lya_pos'] = sum_125 dict_sum['UV'] = sum_UV dict_sum['Lya_neg'] = sum_125_neg dict_sum['Lya_total'] = diff_sum df = pd.DataFrame.from_dict(dict_sum) df.to_csv('ULIRG%s_sum.csv'%(i+1), columns = keys, index = True) ax1.set_title('ULIRG %s F125 image'%(i+1), fontsize = 15) fig.savefig('ULIRG%s_radial.png'%(i+1), dvi = 400) plt.show()
import matplotlib.gridspec as gridspec from matplotlib.font_manager import FontProperties home = os.getenv('HOME') # does not have a trailing slash taffy_dir = home + '/Desktop/ipac/taffy/' taffy_extdir = home + '/Desktop/ipac/taffy_lzifu/' sys.path.append(taffy_dir + 'codes/') import vel_channel_map as vcm if __name__ == '__main__': sdss_i, wcs_sdss = vcm.get_sdss('i') # plot norm = ImageNormalize(sdss_i[0].data, stretch=LogStretch()) fig = plt.figure() ax = fig.add_subplot(111, projection=wcs_sdss) im = ax.imshow(sdss_i[0].data, origin='lower', cmap=mpl.cm.Greys, vmin=0.05, vmax=7, norm=norm) ax.set_autoscale_on(False) lon = ax.coords[0] lat = ax.coords[1] lon.set_ticks_visible(False)
a.jsoc.Notify(jsoc_email), a.jsoc.Segment.image, cutout, ) ##################################################### # Submit the export request and download the data. We # set the number of parallel downloads to 2 so as not # to overwhelm the JSOC export service with our number # of download requests. files = Fido.fetch(q, max_conn=2) files.sort() ##################################################### # Now that we've downloaded the files, we can create # a `~sunpy.map.MapSequence` from them. m_seq = sunpy.map.Map(files, sequence=True) ##################################################### # Finally, we can construct an animation in time from # our stack of cutouts and interactively flip through # each image in our sequence. We first adjust the plot # settings on each image to ensure the colorbar is the # same at each time step. for m in m_seq: m.plot_settings['norm'] = ImageNormalize(vmin=0, vmax=5e3, stretch=SqrtStretch()) m_seq.peek() plt.show()
def mosaicCoadd(repo, patch_list, band='g', tractIndex=0, refPatchIndex=None, sampling=100, norm=None, nImage=False, fig=None, show_colorbar=True, filename=None, flipX=True, flipY=False, instrument=None, collections=None, skymapName=None, coaddName='deepCoadd'): """Generate a mosaic image of many coadded patches. Gen3 only. Parameters ---------- repo : `str` The path to the data repository. patch_list : `list` A list of the patch indices containing images to mosaic. List elements will be `float` (sequential patch indices) band : `str`, optional The band of the coadd to retrieve from the repository. tractIndex : `int`, optional The tract of the skyMap. refPatchIndex : `str`, optional If set, use the given patch to set the image normalization for the figure. sampling : `int`, optional Stride factor to sample each input image in order to reduce the size in memory. A `sampling` of 1 will attempt to display every pixel. norm : `astropy.visualization.ImageNormalize`, optional Normalization to set the color scale of the images. If `None`, the normalization will be calculated from the first image. If you wish to use any normalization other than zscale, you must calculate it ahead of time and pass it in as `norm` here. nImage : `bool`, optional Mosaic the nImage instead of the coadd. fig : `matplotlib.pyplot.fig`, optional Figure instance to display the mosaic in. show_colorbar : `bool`, optional Display a colorbar on the figure. filename : `str`, optional If set, write the figure to a file with the given filename. flipX : `bool`, optional Set to flip the individual patch images horizontally. flipY : `bool`, optional Set to flip the individual patch images vertically. instrument : `str`, optional Default is 'DECam'. collections : `list` or `str`, optional Must be provided to load the camera properly skymapName : `str`, optional Must be provided to load the skymap. e.g., 'hsc_rings_v1' coaddName : `str`, optional Type of coadd, default `deepCoadd` (but you might want `goodSeeingCoadd`) """ # Decide what data product to plot (actual pixel data or nImages) if nImage: coaddName += '_nImage' # Create a mapping between sequential patch indices and (x,y) patch indices if not collections: raise ValueError('One or more collections is required.') if not skymapName: raise ValueError('A skymap is required.') if not instrument: raise ValueError('An instrument is required.') butler = dafButler.Butler(repo, collections=collections) skymap = butler.get('skyMap', skymap=skymapName, instrument=instrument, collections='skymaps') tract = skymap.generateTract(tractIndex) patchesToLoad = [ patch for patch in tract if tract.getSequentialPatchIndex(patch) in patch_list ] patchIndicesToLoad = [patch.getIndex() for patch in patchesToLoad] # (x,y) indices indexmap = {} for patch in tract: indexmap[str(tract.getSequentialPatchIndex(patch))] = patch.getIndex() # Use a reference patch to set the normalization for all the patches if norm is None: if refPatchIndex in patchIndicesToLoad: sequentialPatchIndex = [ key for key, value in indexmap.items() if value == refPatchIndex ][0] dataId = { 'band': band, 'tract': tractIndex, 'patch': int(sequentialPatchIndex), 'instrument': instrument, 'skymap': skymapName } if nImage: coaddArray = butler.get(coaddName, dataId=dataId).getArray() else: coaddArray = butler.get(coaddName, dataId=dataId).getImage().getArray() norm = ImageNormalize(coaddArray, interval=ZScaleInterval(), stretch=SqrtStretch()) # Set up the figure grid patch_x = [] patch_y = [] for patch in patchesToLoad: patch_x.append(patch.getIndex()[0]) patch_y.append(patch.getIndex()[1]) x0 = min(patch_x) y0 = min(patch_y) nx = max(patch_x) - x0 + 1 ny = max(patch_y) - y0 + 1 fig = plt.figure(figsize=(nx, ny), constrained_layout=False) gs1 = fig.add_gridspec(ny, nx, wspace=0, hspace=0) # Plot coadd patches with data, and print the patch index if there's no data for x in range(nx): for y in range(ny): figIdx = x + nx * y ax = fig.add_subplot(gs1[figIdx]) ax.set_aspect('equal', 'box') patchIndex = (x, ny - y - 1) if patchIndex in patchIndicesToLoad: sequentialPatchIndex = [ key for key, value in indexmap.items() if value == patchIndex ][0] dataId = { 'band': band, 'tract': tractIndex, 'patch': int(sequentialPatchIndex), 'instrument': instrument, 'skymap': skymapName } try: coadd = butler.get(coaddName, dataId=dataId) except LookupError: print(f'Failed to retrieve data for patch {patchIndex}') ax.text(.3, .3, patchIndex) continue if nImage: coaddArray = coadd.getArray() else: coaddArray = coadd.getImage().getArray() coaddArray = coaddArray[::sampling, ::sampling] if flipX: coaddArray = np.flip(coaddArray, axis=0) if flipY: coaddArray = np.flip(coaddArray, axis=1) if norm is None: norm = ImageNormalize(coaddArray, interval=ZScaleInterval(), stretch=SqrtStretch()) im = ax.imshow(coaddArray, cmap='gray', norm=norm) else: ax.text(.3, .3, patchIndex) im = None # print('No data in patch', patchIndex) plt.setp(ax, xticks=[], yticks=[]) # Adjust and annotate plot if show_colorbar and (im is not None): cbar_width = 0.01 cbar_height = 0.5 cbar_ax = fig.add_axes( [0.9 - cbar_width, 0.5 - cbar_height / 2, cbar_width, cbar_height]) fig.colorbar(im, cax=cbar_ax) # Save figure, if desired if filename: try: plt.savefig(filename, transparent=True) except Exception as e: print(f"Could not write file '{filename}': {e}")
def thickness_plot(fname=None, fname_comp=None, fout=None, instr=None): """ Parameters ---------- fname fname_comp Returns ------- """ rc('text', usetex=True) if 'fits' in fname_comp: comp_data = fits.getdata(fname_comp) astrofits=True else: comp_data = Image.open(fname_comp) astrofits=False with fits.open(fname) as hdu: data = hdu[0].data # mean, median, std = sigma_clipped_stats(data, sigma=3.0) # smoothed = median_filter(data, size=3) smoothed = gaussian_filter(data, sigma=2) # norm = ImageNormalize(data, # stretch=LinearStretch(), # vmin=np.min(data), vmax=np.max(data)) uvis = (140, 240) wfc = (50, 110) hrc = (130,200) norm = ImageNormalize(data, stretch=LinearStretch(), vmin=wfc[0], vmax=wfc[1]) fig1, ax1 = plt.subplots(nrows=1, ncols=1, figsize=(5,4)) fig2, ax2 = plt.subplots(nrows=1, ncols=1, figsize=(5,4)) im1 = ax1.imshow(smoothed, norm=norm, cmap='plasma', origin='lower') # Add a colorbar to show the image scaling divider1 = make_axes_locatable(ax1) cax1 = divider1.append_axes('bottom', size='5%', pad=0.1) cbar1 = fig1.colorbar(im1, cax=cax1, orientation='horizontal') cbar1.ax.set_xticklabels(cbar1.ax.get_xticklabels(), rotation=45) cbar1.set_label('Cosmic Ray Strikes') if not astrofits: norm1 = ImageNormalize(comp_data, stretch=LinearStretch(), interval=ZScaleInterval()) im2 = ax2.imshow(comp_data, norm=norm1, cmap='plasma') else: norm1 = ImageNormalize(comp_data, stretch=LinearStretch(), vmin=12.5, vmax=16) im2 = ax2.imshow(comp_data, cmap='plasma', norm=norm1)#, origin='lower') # Add a colorbar to show the image scaling divider2 = make_axes_locatable(ax2) cax2 = divider2.append_axes('bottom', size='5%', pad=0.1) cbar2 = fig2.colorbar(im2, cax=cax2, orientation='horizontal') cbar2.ax.set_xticklabels(cbar2.ax.get_xticklabels(), rotation=45) cbar2.set_label(r'Thickness $[\mu m]$') ax1.grid(False) ax2.grid(False) ax1.set_title('WFC Cosmic Ray Incidence Heat Map') ax2.set_title('WFC Fringing Thickness Map') ax1.get_xaxis().set_visible(False) ax1.get_yaxis().set_visible(False) ax2.get_xaxis().set_visible(False) ax2.get_yaxis().set_visible(False) # fig.suptitle(instr, # x=0.5, y=0.9, # horizontalalignment='center', # size=16, weight='bold') fig1.savefig('cr_heat_map_WFC.png', transparent=True, format='png', dpi=350, bbox_inches='tight') fig2.savefig('thickness_heat_map_WFC.png', transparent=True,format='png', dpi=350, bbox_inches='tight') plt.show()
imdss = sax.imshow(dssdata, transform=sax.get_transform(dssw), cmap='bone', vmin=vmin+dmad) textent = None talpha = .66 else: sax = plt.subplot() textent = [column, column+args.size, row, row+args.size] talpha = 1 lcmap = cmap_map(lambda x: 0.6*x, cm.get_cmap(args.cmap)) #stamp = sax.imshow(np.log10(np.nanmedian(flux[::10], axis=0)), # cmap=lcmap, origin='lower', aspect='equal', alpha=talpha, # extent=textent) mstamp = np.nanmedian(flux[::10], axis=0) norm = ImageNormalize(mstamp, interval=AsymmetricPercentileInterval(0,100), stretch=LogStretch(a=250)) stamp = sax.imshow(mstamp, cmap=lcmap, origin='lower', aspect='equal', alpha=talpha, extent=textent, norm=norm) xm, ym = pixel_border(dap[bidx]) for xi,yi in zip(xm, ym): if args.pngdss: sax.plot(xi, yi, color='#FF0043', lw=1.5, transform=sax.get_transform('pixel')) else: sax.plot(column+xi, row+yi, color='#FF0043', lw=1.5) #sax.grid(which='minor', zorder=99) #sax.grid(color='white')
#source_x = sources['xcentroid'][0] #source_y = sources['ycentroid'][0] #positions.append([sources['xcentroid'][0], sources['ycentroid'][0]]) print 'No star has been found' continue else: source_x = sources['xcentroid'][0] source_y = sources['ycentroid'][0] positions.append([sources['xcentroid'][0], sources['ycentroid'][0]]) centers_diago.append(positions[0]) if plot == True: fig, ax = plt.subplots() norm = ImageNormalize(image_cutted, interval=ZScaleInterval(),stretch=LinearStretch()) plt.imshow(image_cutted, cmap='Greys', origin='lower', norm=norm) plt.plot(source_x,source_y,'+',color = 'red') cid = fig.canvas.mpl_connect('button_press_event', lambda event: onclick(filename)) plt.show() fig.canvas.mpl_disconnect(cid) plt.close(fig) index += 1 print "We are in the sourcy file: " + str(index) r_in = 15 r_out = 20 aper = aper_phot(image_cutted,positions,r = r, r_in = r_in,r_out = r_out,bkg_sub = bkg_sub,plot=plot_aper) appertures.append(aper['residual_aperture_sum'][0])
def plot_image_fit_residuals(fig, image, fit, residuals): """ Make a figure with three subplots showing the image, the fit and the residuals. The image and the fit are shown with logarithmic scaling and a common colorbar. The residuals are shown with linear scaling and a separate colorbar. Parameters: fig (fig object): Figure object in which to make the subplots. image (2D array): Image numpy array. fit (2D array): Fitted image numpy array. residuals (2D array): Fitted image subtracted from image numpy array. positions (list of arrays): List with the catalog and PSF fitted centroid positions. Format is (row,col). Default is ``None`` which does not plot the positions. Returns: axes (list): List with Matplotlib subplot axes objects for each subplot. """ # Calculate common normalization for the first two subplots: vmin_image, vmax_image = PercentileInterval(95.).get_limits(image) vmin_fit, vmax_fit = PercentileInterval(95.).get_limits(fit) vmin = np.nanmin([vmin_image, vmin_fit]) vmax = np.nanmax([vmax_image, vmax_fit]) norm = ImageNormalize(vmin=vmin, vmax=vmax, stretch=LogStretch()) # Add subplot with the image: ax1 = fig.add_subplot(131) im1 = plot_image(image, scale=norm, make_cbar=False) # Add subplot with the fit: ax2 = fig.add_subplot(132) plot_image(fit, scale=norm, make_cbar=False) # Make the common colorbar for image and fit subplots: cbar_ax12 = fig.add_axes([0.125, 0.2, 0.494, 0.03]) fig.colorbar(im1, norm=norm, cax=cbar_ax12, orientation='horizontal') cbar_ax12.set_xticklabels(cbar_ax12.get_xticklabels(), rotation='vertical') # Calculate the normalization for the third subplot: vmin, vmax = PercentileInterval(95.).get_limits(residuals) norm = ImageNormalize(vmin=vmin, vmax=vmax, stretch=LinearStretch()) # Add subplot with the residuals: ax3 = fig.add_subplot(133) im3 = plot_image(residuals, scale='linear', make_cbar=False) # Make the colorbar for the residuals subplot: cbar_ax3 = fig.add_axes([0.7, 0.2, 0.205, 0.03]) fig.colorbar(im3, norm=norm, cax=cbar_ax3, orientation='horizontal') cbar_ax3.set_xticklabels(cbar_ax3.get_xticklabels(), rotation='vertical') # Add more space between subplots: plt.subplots_adjust(wspace=0.4, hspace=0.4) # Set titles: ax_list = [ax1, ax2, ax3] title_list = ['Image', 'PSF fit', 'Residuals'] for ax, title in zip(ax_list, title_list): ax.set_title(title) return ax_list
def run(self): import os import logging import numpy as np import matplotlib.pyplot as plt from matplotlib.colors import SymLogNorm from astropy.visualization import ZScaleInterval, ImageNormalize from astropy.wcs.utils import skycoord_to_pixel logger = logging.getLogger('tgk.science') logger.debug(' Plot comet location.') try: comet = CometPhotometry().get_frame(self.obs.frame_name) except IndexError as e: raise PlotCometLocFailure(e) yc, xc = int(comet['y']), int(comet['x']) fig = plt.figure(figsize=(8, 8)) fig.clear() axes = [ fig.add_subplot(gs) for gs in plt.GridSpec( 2, 2, wspace=0, hspace=0, bottom=0, top=1, left=0, right=1) ] opts = dict(origin='lower', cmap='viridis', norm=ImageNormalize(self.im.data[self.obs.trimsec], interval=ZScaleInterval())) axes[0].imshow(self.im.data, **opts) axes[2].imshow(self.im.data, **opts) im = self.im.data - comet['comet bg'] opts['norm'] = SymLogNorm( comet['comet bgsig'], vmin=-comet['comet bgsig'], vmax=im[yc, xc] if im[yc, xc] > -comet['comet bgsig'] else comet['comet bgsig']) axes[1].imshow(im, **opts) axes[3].imshow(im, **opts) xjpl, yjpl = skycoord_to_pixel(self.geom.radec_predict, self.obs.wcs) opts = dict(color='k', linewidths=1) for i in range(4): axes[i].scatter(self.im.cat['X'], self.im.cat['Y'], s=24, marker='o', color='w', linewidth=0.5, label=None, facecolor='none') axes[i].scatter(self.im.cat['X'], self.im.cat['Y'], s=20, marker='o', color='k', linewidth=0.5, label=None, facecolor='none') axes[i].scatter([xjpl], [yjpl], marker='+', label='JPL/HORIZONS', **opts) axes[i].scatter([xc], [yc], marker='x', label='Centroid', **opts) axes[0].legend(numpoints=1, prop=dict(size='medium'), loc='upper left') plt.setp(axes[2:], xlim=[xc - 100, xc + 100], ylim=[yc - 100, yc + 100]) plt.setp(axes[:2], xlim=[0, self.im.data.shape[1] - 1], ylim=[0, self.im.data.shape[0] - 1]) plt.setp(axes, frame_on=False, xticks=[], yticks=[]) fig.canvas.draw() date = self.obs.time.iso[:10].replace('-', '') fn = self.minion_file('{}/{}.png'.format(date, self.obs.frame_name)) d = os.path.split(fn)[0] if not os.path.exists(d): os.mkdir(d) self.logger.debug('Created directory {}.'.format(d)) try: fig.savefig(fn, dpi=75) except ValueError as e: raise PlotCometLocFailure("Failed to save image {}: {}".format( fn, e)) plt.close(fig)
def fits_finder_chart( fitsfile, outfile, wcsfrom=None, scale=ZScaleInterval(), stretch=LinearStretch(), colormap=plt.cm.gray_r, findersize=None, finder_coordlimits=None, overlay_ra=None, overlay_decl=None, overlay_pltopts={'marker':'o', 'markersize':10.0, 'markerfacecolor':'none', 'markeredgewidth':2.0, 'markeredgecolor':'red'}, overlay_zoomcontain=False, grid=False, gridcolor='k', ): '''This makes a finder chart from fitsfile with an optional object position overlay. Args ---- `fitsfile` is the FITS file to use to make the finder chart `outfile` is the name of the output file. This can be a png or pdf or whatever else matplotlib can write given a filename and extension. If `wcsfrom` is None, the WCS to transform the RA/Dec to pixel x/y will be taken from the FITS header of `fitsfile`. If this is not None, it must be a FITS or similar file that contains a WCS header in its first extension. `scale` sets the normalization for the FITS pixel values. This is an astropy.visualization Interval object. `stretch` sets the stretch function for mapping FITS pixel values to output pixel values. This is an astropy.visualization Stretch object. See http://docs.astropy.org/en/stable/visualization/normalization.html for details on `scale` and `stretch` objects. `colormap` is a matplotlib color map object to use for the output image. If `findersize` is None, the output image size will be set by the NAXIS1 and NAXIS2 keywords in the input `fitsfile` FITS header. Otherwise, `findersize` must be a tuple with the intended x and y size of the image in inches (all output images will use a DPI = 100). `finder_coordlimits` sets x and y limits for the plot, effectively zooming it in if these are smaller than the dimensions of the FITS image. This should be a list of the form: [minra, maxra, mindecl, maxdecl] all in decimal degrees. `overlay_ra` and `overlay_decl` are ndarrays containing the RA and Dec values to overplot on the image as an overlay. `overlay_pltopts` controls how the overlay points will be plotted. This a dict with standard matplotlib marker, etc. kwargs. `overlay_zoomcontain` controls if the finder chart will be zoomed to just contain the overlayed points. Everything outside the footprint of these points will be discarded. `grid` sets if a grid will be made on the output image. `gridcolor` sets the color of the grid lines. This is a usual matplotib color spec string. Returns ------- The filename of the generated output image if successful. None otherwise. ''' # read in the FITS file if wcsfrom is None: hdulist = pyfits.open(fitsfile) img, hdr = hdulist[0].data, hdulist[0].header hdulist.close() frameshape = (hdr['NAXIS1'], hdr['NAXIS2']) w = WCS(hdr) elif os.path.exists(wcsfrom): hdulist = pyfits.open(fitsfile) img, hdr = hdulist[0].data, hdulist[0].header hdulist.close() frameshape = (hdr['NAXIS1'], hdr['NAXIS2']) w = WCS(wcsfrom) else: LOGERROR('could not determine WCS info for input FITS: %s' % fitsfile) return None # use the frame shape to set the output PNG's dimensions if findersize is None: fig = plt.figure(figsize=(frameshape[0]/100.0, frameshape[1]/100.0)) else: fig = plt.figure(figsize=findersize) # set the coord limits if zoomcontain is True # we'll leave 30 arcseconds of padding on each side if (overlay_zoomcontain and overlay_ra is not None and overlay_decl is not None): finder_coordlimits = [overlay_ra.min()-30.0/3600.0, overlay_ra.max()+30.0/3600.0, overlay_decl.min()-30.0/3600.0, overlay_decl.max()+30.0/3600.0] # set the coordinate limits if provided if finder_coordlimits and isinstance(finder_coordlimits, list): minra, maxra, mindecl, maxdecl = finder_coordlimits cntra, cntdecl = (minra + maxra)/2.0, (mindecl + maxdecl)/2.0 pixelcoords = w.all_world2pix([[minra, mindecl], [maxra, maxdecl], [cntra, cntdecl]],1) x1, y1, x2, y2 = (int(pixelcoords[0,0]), int(pixelcoords[0,1]), int(pixelcoords[1,0]), int(pixelcoords[1,1])) xmin = x1 if x1 < x2 else x2 xmax = x2 if x2 > x1 else x1 ymin = y1 if y1 < y2 else y2 ymax = y2 if y2 > y1 else y1 # create a new WCS with the same transform but new center coordinates whdr = w.to_header() whdr['CRPIX1'] = (xmax - xmin)/2 whdr['CRPIX2'] = (ymax - ymin)/2 whdr['CRVAL1'] = cntra whdr['CRVAL2'] = cntdecl whdr['NAXIS1'] = xmax - xmin whdr['NAXIS2'] = ymax - ymin w = WCS(whdr) else: xmin, xmax, ymin, ymax = 0, hdr['NAXIS2'], 0, hdr['NAXIS1'] # add the axes with the WCS projection # this should automatically handle subimages because we fix the WCS # appropriately above for these fig.add_subplot(111,projection=w) if scale is not None and stretch is not None: norm = ImageNormalize(img, interval=scale, stretch=stretch) plt.imshow(img[ymin:ymax,xmin:xmax], origin='lower', cmap=colormap, norm=norm) else: plt.imshow(img[ymin:ymax,xmin:xmax], origin='lower', cmap=colormap) # handle additional options if grid: plt.grid(color=gridcolor,ls='solid',lw=1.0) # handle the object overlay if overlay_ra is not None and overlay_decl is not None: our_pltopts = dict( transform=plt.gca().get_transform('fk5'), marker='o', markersize=10.0, markerfacecolor='none', markeredgewidth=2.0, markeredgecolor='red', rasterized=True, linestyle='none' ) if overlay_pltopts is not None and isinstance(overlay_pltopts, dict): our_pltopts.update(overlay_pltopts) plt.gca().set_autoscale_on(False) plt.gca().plot(overlay_ra, overlay_decl, **our_pltopts) plt.xlabel('Right Ascension [deg]') plt.ylabel('Declination [deg]') # get the x and y axes objects to fix the ticks xax = plt.gca().coords[0] yax = plt.gca().coords[1] yax.set_major_formatter('d.ddd') xax.set_major_formatter('d.ddd') # save the figure plt.savefig(outfile, dpi=100.0) plt.close('all') return outfile
def pathtest(step_input_filename, reffile, comparison_filename, writefile=True, show_figs=True, save_figs=False, threshold_diff=1.0e-7, debug=False): """ This function calculates the difference between the pipeline and calculated pathloss values. Args: step_input_filename: str, full path name of sourcetype output fits file reffile: str, path to the pathloss IFU reference fits file comparison_filename: str, path to comparison pipeline pathloss file writefile: boolean, if True writes calculated flat and difference image fits files show_figs: boolean, whether to show plots or not save_figs: boolean, save the plots threshold_diff: float, threshold diff between pipeline and ESA file debug: boolean, if true print statements will show on-screen Returns: - 1 plot, if told to save and/or show them. - median_diff: Boolean, True if smaller or equal to threshold. - log_msgs: list, all print statements are captured in this variable """ log_msgs = [] # start the timer pathtest_start_time = time.time() # get info from the rate file header msg = 'step_input_filename='+step_input_filename print(msg) log_msgs.append(msg) exptype = fits.getval(step_input_filename, "EXP_TYPE", 0) grat = fits.getval(step_input_filename, "GRATING", 0) filt = fits.getval(step_input_filename, "FILTER", 0) msg = "pathloss file: Grating:"+grat+" Filter:"+filt+" EXP_TYPE:"+exptype print(msg) log_msgs.append(msg) msg = "Using reference file: "+reffile print(msg) log_msgs.append(msg) is_point_source = True if writefile: # create the fits list to hold the calculated flat values for each slit hdu0 = fits.PrimaryHDU() outfile = fits.HDUList() outfile.append(hdu0) # create fits list to hold pipeline-calculated difference values hdu0 = fits.PrimaryHDU() compfile = fits.HDUList() compfile.append(hdu0) # list to determine if pytest is passed or not total_test_result = [] # get all the science extensions if is_point_source: ext = 1 # for all PS IFU # get files print('Checking files exist & obtaining datamodels. Takes a few mins...') if os.path.isfile(comparison_filename): if debug: print('Comparison file does exist.') else: result_msg = 'Comparison file does NOT exist. Skipping pathloss test.' print(result_msg) log_msgs.append(result_msg) result = 'skip' return result, result_msg, log_msgs # get the comparison data model ifu_pipe_model = datamodels.open(comparison_filename) if debug: print('got comparison datamodel!') if os.path.isfile(step_input_filename): if debug: print('Input file does exist.') else: result_msg = 'Input file does NOT exist. Skipping pathloss test.' log_msgs.append(result_msg) result = 'skip' return result, result_msg, log_msgs # get the input data model ifu_input_model = datamodels.open(step_input_filename) if debug: print('got input datamodel!') # get slices (instead of using .slit) pl_ifu_slits = nirspec.nrs_ifu_wcs(ifu_input_model) print("got input slices") plcor_ref_ext = fits.getdata(reffile, ext) hdul = fits.open(reffile) plcor_ref = hdul[1].data w = wcs.WCS(hdul[1].header) w1, y1, x1 = np.mgrid[:plcor_ref.shape[0], :plcor_ref.shape[1], :plcor_ref.shape[2]] slitx_ref, slity_ref, wave_ref = w.all_pix2world(x1, y1, w1, 0) # these are full 2048 * 2048 files: previous_sci = fits.getdata(step_input_filename, "SCI") comp_sci = fits.getdata(comparison_filename, "SCI") pathloss_divided = comp_sci/previous_sci # Can manually test correction at nonzero point # slit_x = -0.3 # slit_y = 0.3 # if debug: # print("""WARNING: Using manually set slit_x and slit_y! # The pipeline correction will not use manually set values # and thus the residuals will change""") # set up generals for all plots font = {'weight': 'normal', 'size': 10} matplotlib.rc('font', **font) # loop through the slices msg = " Looping through the slices... " print(msg) log_msgs.append(msg) slit_list = np.ndarray.tolist(np.arange(0, 30)) for slit, slit_num in zip(pl_ifu_slits, slit_list): print("working with slice {}".format(slit_num)) x, y = wcstools.grid_from_bounding_box(slit.bounding_box, step=(1, 1)) ra, dec, wave = slit(x, y) slit_x = 0 # This assumption is made for IFU sources only slit_y = 0 if debug: print("slit_x, slit_y", slit_x, slit_y) correction_array = np.array([]) lambda_array = np.array([]) wave_sci = wave * 10**(-6) # microns --> meters wave_sci_flat = wave_sci.reshape(wave_sci.size) wave_ref_flat = wave_ref.reshape(wave_ref.size) ref_xy = np.column_stack((slitx_ref.reshape(slitx_ref.size), slity_ref.reshape(slitx_ref.size))) # loop through slices in lambda from reference file shape = 0 for lambda_val in wave_ref_flat: # loop through every lambda value # flattened so that looping works smoothly shape = shape + 1 index = np.where(wave_ref[:, 0, 0] == lambda_val) # index of closest lambda value in reffile to given sci lambda # took index of only the first slice of wave_ref because # the others were repetitive & we got extra indices # take slice where lambda=index: plcor_slice = plcor_ref_ext[index[0][0]].reshape(plcor_ref_ext[index[0][0]].size) # do 2d interpolation to get a single correction factor for each slice corr_val = scipy.interpolate.griddata(ref_xy[:plcor_slice.size], plcor_slice, np.asarray([slit_x, slit_y]), method='linear') # append values from loop to create a vector of correction factors correction_array = np.append(correction_array, corr_val[0]) # map to array with corresponding lambda lambda_array = np.append(lambda_array, lambda_val) # get correction value for each pixel corr_vals = np.interp(wave_sci_flat, lambda_array, correction_array) corr_vals = corr_vals.reshape(wave_sci.shape) box = slit.bounding_box small_y = box[0][0] big_y = box[0][1] small_x = box[1][0] big_x = box[1][1] left = int(math.trunc(small_x)) right = int(math.ceil(big_x)) bottom = int(math.trunc(small_y)) top = int(math.ceil(big_y)) full_cut2slice = previous_sci[left:right, bottom:top] print("shapes:", full_cut2slice.shape, corr_vals.shape) if full_cut2slice.shape != corr_vals.shape: value = 0 while (((full_cut2slice.shape[0] - corr_vals.shape[0]) != 0) or ((full_cut2slice.shape[1] - corr_vals.shape[1]) != 0)) and \ value < 7: # can delete second criteria once all pass if value == 6: print("WARNING: may be in infinite loop!") x_amount_off = full_cut2slice.shape[0]-corr_vals.shape[0] if x_amount_off >= 1: if x_amount_off % 2 == 0: # need to add two values, so do one on each side right = right-1 left = left+1 print("ALTERED SHAPE OF SLICE: V1") value = value + 1 print("left {}, right {}, top {}, bottom {}".format(left, right, top, bottom)) else: # just add one value left = left+1 print("ALTERED SHAPE OF SLICE: V2") value = value+1 print("left {}, right {}, top {}, bottom {}".format(left, right, top, bottom)) elif x_amount_off <= -1: if x_amount_off % 2 == 0: right = right+1 left = left-1 print("ALTERED SHAPE OF SLICE: V3") value = value+1 print("left {}, right {}, top {}, bottom {}".format(left, right, top, bottom)) else: left = left-1 print("ALTERED SHAPE OF SLICE: V4") value = value+1 print("left {}, right {}, top {}, bottom {}".format(left, right, top, bottom)) y_amount_off = full_cut2slice.shape[1]-corr_vals.shape[1] if y_amount_off >= 1: if y_amount_off % 2 == 0: bottom = bottom-1 top = top+1 print("ALTERED SHAPE OF SLICE: V5") value = value+1 print("left {}, right {}, top {}, bottom {}".format(left, right, top, bottom)) else: bottom = bottom+1 print("ALTERED SHAPE OF SLICE: V6") value = value+1 print("left {}, right {}, top {}, bottom {}".format(left, right, top, bottom)) elif y_amount_off <= -1: if y_amount_off % 2 == 0: top = top+1 bottom = bottom-1 print("ALTERED SHAPE OF SLICE: V7") value = value+1 print("left {}, right {}, top {}, bottom {}".format(left, right, top, bottom)) else: bottom = bottom-1 print("ALTERED SHAPE OF SLICE: V8") value = value+1 print("left {}, right {}, top {}, bottom {}".format(left, right, top, bottom)) full_cut2slice = previous_sci[left:right, bottom:top] print("final left {}, right {}, top {}, bottom {}".format(left, right, top, bottom)) print("NEW SHAPE OF SLICE: {} and corr_vals.shape: {}".format(full_cut2slice.shape, corr_vals.shape)) if full_cut2slice.shape != corr_vals.shape: print("shapes did not match! full_cut2slice: {}, corr_vals {}".format(full_cut2slice.shape, corr_vals.shape)) continue corrected_array = full_cut2slice/corr_vals pipe_correction = pathloss_divided[left:right, bottom:top] if pipe_correction.shape != corr_vals.shape: print("shapes did not match! pipe_correction: {}, corr_vals {}".format(pipe_correction.shape, corr_vals.shape)) continue prev_sci_slit = previous_sci[left:right, bottom:top] if prev_sci_slit.shape != corr_vals.shape: print("shapes did not match! prev_sci_slit: {}, corr_vals {}".format(prev_sci_slit.shape, corr_vals.shape)) continue comp_sci_slit = comp_sci[left:right, bottom:top] if comp_sci_slit.shape != corr_vals.shape: print("shapes did not match! comp_sci_slit: {}, corr_vals {}".format(comp_sci_slit.shape, corr_vals.shape)) continue # Plots: step_input_filepath = step_input_filename.replace(".fits", "") # my correction values fig = plt.figure(figsize=(15, 15)) plt.subplot(221) norm = ImageNormalize(corr_vals) plt.imshow(corr_vals, vmin=0.999995, vmax=1.000005, aspect=10.0, origin='lower', cmap='viridis') plt.xlabel('dispersion in pixels') plt.ylabel('y in pixels') plt.title('Calculated Correction') plt.colorbar() # pipe correction plt.subplot(222) norm = ImageNormalize(pipe_correction) plt.imshow(pipe_correction, vmin=0.999995, vmax=1.000005, aspect=10.0, origin='lower', cmap='viridis') plt.xlabel('dispersion in pixels') plt.ylabel('y in pixels') plt.title('Pipe Correction') plt.colorbar() # residuals (pipe correction - my correction) if pipe_correction.shape == corr_vals.shape: corr_residuals = pipe_correction - corr_vals plt.subplot(223) norm = ImageNormalize(corr_residuals) plt.imshow(corr_residuals, vmin=-0.000000005, vmax=0.000000005, aspect=10.0, origin='lower', cmap='viridis') plt.xlabel('dispersion in pixels') plt.ylabel('y in pixels') plt.title('Correction residuals') plt.colorbar() # my science data after pathloss plt.subplot(224) norm = ImageNormalize(corrected_array) plt.imshow(corrected_array, vmin=0, vmax=300, aspect=10.0, origin='lower', cmap='viridis') plt.title('My slit science data after pathloss') plt.xlabel('dispersion in pixels') plt.ylabel('y in pixels') plt.colorbar() fig.suptitle("IFU PS Pathloss Correction Testing" + str(corrected_array.shape)) if show_figs: plt.show() if save_figs: plt_name = step_input_filepath + "_Pathloss_test_slitlet_IFU_PS_" + str(slit_num) + ".png" plt.savefig(plt_name) print('Figure saved as: ', plt_name) elif not save_figs and not show_figs: msg = "Not making plots because both show_figs and save_figs were set to False." if debug: print(msg) log_msgs.append(msg) elif not save_figs: msg = "Not saving plots because save_figs was set to False." if debug: print(msg) log_msgs.append(msg) plt.clf() # create fits file to hold the calculated pathloss for each slit if writefile: msg = "Saving the fits files with the calculated pathloss for each slit..." print(msg) log_msgs.append(msg) # this is the file to hold the image of pipeline-calculated difference values outfile_ext = fits.ImageHDU(corr_vals, name=str(slit_num)) outfile.append(outfile_ext) # this is the file to hold the image of pipeline-calculated difference values compfile_ext = fits.ImageHDU(corr_residuals, name=str(slit_num)) compfile.append(compfile_ext) if corr_residuals[~np.isnan(corr_residuals)].size == 0: msg1 = " * Unable to calculate statistics because difference array has all values as NaN. " \ "Test will be set to FAILED." print(msg1) log_msgs.append(msg1) test_result = "FAILED" else: msg = "Calculating statistics... " print(msg) log_msgs.append(msg) corr_residuals = corr_residuals[np.where((corr_residuals != 999.0) & (corr_residuals < 0.1) & (corr_residuals > -0.1))] # ignore outliers if corr_residuals.size == 0: msg1 = """ * Unable to calculate statistics because difference array has all outlier values. Test will be set to FAILED.""" print(msg1) log_msgs.append(msg1) test_result = "FAILED" else: stats_and_strings = auxfunc.print_stats(corr_residuals, "Difference", float(threshold_diff), abs=True) stats, stats_print_strings = stats_and_strings corr_residuals_mean, corr_residuals_median, corr_residuals_std = stats for msg in stats_print_strings: log_msgs.append(msg) # This is the key argument for the assert pytest function median_diff = False if abs(corr_residuals_median) <= float(threshold_diff): median_diff = True if median_diff: test_result = "PASSED" else: test_result = "FAILED" msg = " *** Result of the test: "+test_result+"\n" print(msg) log_msgs.append(msg) total_test_result.append(test_result) if writefile: outfile_name = step_input_filename.replace("srctype", "_calcuated_FS_UNI_pathloss") compfile_name = step_input_filename.replace("srctype", "_comparison_FS_UNI_pathloss") # create the fits list to hold the calculated flat values for each slit outfile.writeto(outfile_name, overwrite=True) # this is the file to hold the image of pipeline-calculated difference values compfile.writeto(compfile_name, overwrite=True) msg = "\nFits file with calculated pathloss values of each slit saved as: " print(msg) log_msgs.append(msg) print(outfile_name) log_msgs.append(outfile_name) msg = "Fits file with comparison (pipeline pathloss - calculated pathloss) saved as: " print(msg) log_msgs.append(msg) print(compfile_name) log_msgs.append(compfile_name) # If all tests passed then pytest will be marked as PASSED, else FAILED FINAL_TEST_RESULT = False for t in total_test_result: if t == "FAILED": FINAL_TEST_RESULT = False break else: FINAL_TEST_RESULT = True if FINAL_TEST_RESULT: msg = "\n *** Final pathloss test result reported as PASSED *** \n" print(msg) log_msgs.append(msg) result_msg = "All slits PASSED path_loss test." else: msg = "\n *** Final pathloss test result reported as FAILED *** \n" print(msg) log_msgs.append(msg) result_msg = "One or more slits FAILED path_loss test." # end the timer pathloss_end_time = time.time() - pathtest_start_time if pathloss_end_time > 60.0: pathloss_end_time = pathloss_end_time/60.0 # in minutes pathloss_tot_time = "* Script IFU_PS.py took ", repr(pathloss_end_time)+" minutes to finish." if pathloss_end_time > 60.0: pathloss_end_time = pathloss_end_time/60. # in hours pathloss_tot_time = "* Script IFU_PS.py took ", repr(pathloss_end_time)+" hours to finish." else: pathloss_tot_time = "* Script IFU_PS.py took ", repr(pathloss_end_time)+" seconds to finish." print(pathloss_tot_time) log_msgs.append(pathloss_tot_time) return FINAL_TEST_RESULT, result_msg, log_msgs
sci_med = ccdproc.combine(reprojected,method='median',sigma_clip=True,sigma_clip_func=np.ma.median) sci_med.write(red_path+sci+'.fits',overwrite=True) with fits.open(red_path+sci+'.fits',mode='update') as hdr: hdr[0].header['RDNOISE'] = np.median(rdnoise)/len(sci_list[sci]) hdr[0].header['NFILES'] = len(sci_list[sci]) for k, n in enumerate(sci_list[sci]): hdr[0].header['FILE'+str(k+1)] = (os.path.basename(n), 'Name of file used in median.') if args.wcs == 'True' or args.wcs == 'yes': fig, ax = plt.subplots(figsize=(7,7)) ax = plt.subplot(projection=wcs_object) gaia = Irsa.query_region(SkyCoord(hdr[0].header['CRVAL1']*u.deg, hdr[0].header['CRVAL2']*u.deg,frame='fk5'), catalog="gaia_dr2_source", spatial="Cone",radius=3*u.arcmin) if len(gaia) == 0: log.info('No GAIA stars found within 3 arcmin for starlist.') plt.close() else: ax.imshow(sci_med, cmap='gray', norm=ImageNormalize(sci_med, interval=ZScaleInterval())) _, median, std = sigma_clipped_stats(sci_med, sigma=3.0) daofind = DAOStarFinder(fwhm=7.0, threshold=3.*std) sources = daofind(np.asarray(sci_med)) for l,m in enumerate(gaia['source_id']): x, y = (wcs.WCS(hdr[0].header)).all_world2pix(gaia['ra'][l],gaia['dec'][l],1) ax.add_patch(patches.Circle((x,y),radius=3,edgecolor='g',alpha=0.5,facecolor='none',linewidth=2, label='Gaia star: RA = %f, Dec = %f'%(gaia['ra'][l],gaia['dec'][l]), picker=True)) for i in range(len(sources)): ax.add_patch(patches.Circle((sources['xcentroid'][i],sources['ycentroid'][i]),radius=3,edgecolor='b',alpha=0.5,facecolor='none',linewidth=2,label='Source star x = %f, y = %f'%(sources['xcentroid'][i],sources['ycentroid'][i]), picker=True)) ax.set_title('Target: '+sci) source_star = [] gaia_star = [] fig.canvas.mpl_connect('pick_event', lambda event: onpick(event,source_star,gaia_star,[])) print('Displaying interactive plot to select star to center WCS solution.') print('Select 1 star near the center/near your target.') print('Default WCS is generally accurate enough but may be off-set.')
indata1, hdr1 = fits.getdata(inputfits1, header=True) indata2, hdr2 = fits.getdata(inputfits2, header=True) indata3, hdr3 = fits.getdata(inputfits3, header=True) indata4, hdr4 = fits.getdata(inputfits4, header=True) xlim1, xlim2 = 1300, 1600 ylim1, ylim2 = 1300, 1900 wcs = WCS(hdr1) plt.subplots_adjust(hspace=0.01, wspace=0.01) plt.title("Emergency of SN 2017ein", loc='center', fontsize=14) ax1 = plt.subplot(141) #ax1=plt.subplot(141,projection=wcs) norm2 = ImageNormalize(indata1, interval=ZScaleInterval(), stretch=LinearStretch()) ax1.imshow(indata1, cmap='gist_heat', norm=norm2, origin='lower') #plt.axis('off') # ax1.set_xlim(xlim1, xlim2) ax1.set_ylim(ylim1, ylim2) circ = Circle((1472, 1593), 15, color='white', fill=None, linewidth='1') ax1.add_patch(circ) ax1.set_axis_off() ax1.text(1350, 1850, '2017-05-20.76', fontsize=10, weight='bold', color='white')
async def get(self, objectId: str = None): """ --- summary: Serve ZTF alert cutout as fits or png tags: - lab parameters: - in: query name: candid description: "ZTF alert candid" required: true schema: type: integer - in: query name: cutout description: "retrieve science, template, or difference cutout image?" required: true schema: type: string enum: [science, template, difference] - in: query name: file_format description: "response file format: original loss-less FITS or rendered png" required: true schema: type: string enum: [fits, png] - in: query name: interval description: "Interval to use when rendering png" required: false schema: type: string enum: [min_max, zscale] - in: query name: stretch description: "Stretch to use when rendering png" required: false schema: type: string enum: [linear, log, asinh, sqrt] - in: query name: cmap description: "Color map to use when rendering png" required: false schema: type: string enum: [bone, gray, cividis, viridis, magma] responses: '200': description: retrieved cutout content: image/fits: schema: type: string format: binary image/png: schema: type: string format: binary '400': description: retrieval failed content: application/json: schema: Error """ streams = self.get_user_streams() # allow access to public data only by default selector = {1} for stream in streams: if "ztf" in stream.name.lower(): selector.update(set(stream.altdata.get("selector", []))) selector = list(selector) try: candid = int(self.get_argument("candid")) cutout = self.get_argument("cutout").capitalize() file_format = self.get_argument("file_format").lower() interval = self.get_argument("interval", default=None) stretch = self.get_argument("stretch", default=None) cmap = self.get_argument("cmap", default=None) known_cutouts = ["Science", "Template", "Difference"] if cutout not in known_cutouts: return self.error( f"cutout {cutout} of {objectId}/{candid} not in {str(known_cutouts)}" ) known_file_formats = ["fits", "png"] if file_format not in known_file_formats: return self.error( f"file format {file_format} of {objectId}/{candid}/{cutout} not in {str(known_file_formats)}" ) normalization_methods = { "asymmetric_percentile": AsymmetricPercentileInterval(lower_percentile=1, upper_percentile=100), "min_max": MinMaxInterval(), "zscale": ZScaleInterval(nsamples=600, contrast=0.045, krej=2.5), } if interval is None: interval = "asymmetric_percentile" normalizer = normalization_methods.get( interval.lower(), AsymmetricPercentileInterval(lower_percentile=1, upper_percentile=100), ) stretching_methods = { "linear": LinearStretch, "log": LogStretch, "asinh": AsinhStretch, "sqrt": SqrtStretch, } if stretch is None: stretch = "log" if cutout != "Difference" else "linear" stretcher = stretching_methods.get(stretch.lower(), LogStretch)() if (cmap is None) or (cmap.lower() not in [ "bone", "gray", "cividis", "viridis", "magma" ]): cmap = "bone" else: cmap = cmap.lower() query = { "query_type": "find", "query": { "catalog": "ZTF_alerts", "filter": { "candid": candid, "candidate.programid": { "$in": selector }, }, "projection": { "_id": 0, f"cutout{cutout}": 1 }, }, "kwargs": { "limit": 1, "max_time_ms": 5000 }, } resp = self.query_kowalski(query=query) if resp.status_code == requests.codes.ok: alert = bj.loads(resp.text).get("data", list(dict()))[0] else: alert = dict() cutout_data = bj.loads( bj.dumps([alert[f"cutout{cutout}"]["stampData"]]))[0] # unzipped fits name fits_name = pathlib.Path( alert[f"cutout{cutout}"]["fileName"]).with_suffix("") # unzip and flip about y axis on the server side with gzip.open(io.BytesIO(cutout_data), "rb") as f: with fits.open(io.BytesIO(f.read())) as hdu: header = hdu[0].header data_flipped_y = np.flipud(hdu[0].data) if file_format == "fits": hdu = fits.PrimaryHDU(data_flipped_y, header=header) hdul = fits.HDUList([hdu]) stamp_fits = io.BytesIO() hdul.writeto(fileobj=stamp_fits) self.set_header("Content-Type", "image/fits") self.set_header("Content-Disposition", f"Attachment;filename={fits_name}") self.write(stamp_fits.getvalue()) if file_format == "png": buff = io.BytesIO() plt.close("all") fig, ax = plt.subplots(figsize=(4, 4)) fig.subplots_adjust(0, 0, 1, 1) ax.set_axis_off() # replace nans with median: img = np.array(data_flipped_y) # replace dubiously large values xl = np.greater(np.abs(img), 1e20, where=~np.isnan(img)) if img[xl].any(): img[xl] = np.nan if np.isnan(img).any(): median = float(np.nanmean(img.flatten())) img = np.nan_to_num(img, nan=median) norm = ImageNormalize(img, stretch=stretcher) img_norm = norm(img) vmin, vmax = normalizer.get_limits(img_norm) ax.imshow(img_norm, cmap=cmap, origin="lower", vmin=vmin, vmax=vmax) plt.savefig(buff, dpi=42) buff.seek(0) plt.close("all") self.set_header("Content-Type", "image/png") self.write(buff.getvalue()) except Exception: _err = traceback.format_exc() return self.error(f"failure: {_err}")
def plot_image(image, scale='log', origin='lower', xlabel='Pixel Column Number', ylabel='Pixel Row Number', make_cbar=False, clabel='Flux ($e^{-}s^{-1}$)', cbar_ticks=None, cbar_ticklabels=None, title=None, percentile=95.0, vmin=None, vmax=None, ax=None, cmap=plt.cm.Blues, offset_axes=None, **kwargs): """ Utility function to plot a 2D image. Parameters: image (2d array): Image data. scale (str or astropy.visualization.ImageNormalize object, optional): Normalization used to stretch the colormap. Options: ``'linear'``, ``'sqrt'``, or ``'log'``. Can also be a `astropy.visualization.ImageNormalize` object. Default is ``'log'``. origin (str, optional): The origin of the coordinate system. xlabel (str, optional): Label for the x-axis. ylabel (str, optional): Label for the y-axis. make_cbar (boolean, optional): Create colorbar? Default is ``False``. clabel (str, optional): Label for the color bar. title (str or None, optional): Title for the plot. percentile (float, optional): The fraction of pixels to keep in color-trim. The same fraction of pixels is eliminated from both ends. Default=95. ax (matplotlib.pyplot.axes, optional): Axes in which to plot. Default (None) is to use current active axes. cmap (matplotlib colormap, optional): Colormap to use. Default is the ``Blues`` colormap. kwargs (dict, optional): Keyword arguments to be passed to `matplotlib.pyplot.imshow`. """ if allnan(image): logger = logging.getLogger(__name__) logger.error("Image is all NaN") return None # Calcualte limits of color scaling: if vmin is None or vmax is None: vmin1, vmax1 = PercentileInterval(percentile).get_limits(image) if vmin is None: vmin = vmin1 if vmax is None: vmax = vmax1 # Create ImageNormalize object with extracted limits: if scale == 'log': norm = ImageNormalize(vmin=vmin, vmax=vmax, stretch=LogStretch()) elif scale == 'linear': norm = ImageNormalize(vmin=vmin, vmax=vmax, stretch=LinearStretch()) elif scale == 'sqrt': norm = ImageNormalize(vmin=vmin, vmax=vmax, stretch=SqrtStretch()) elif isinstance(scale, matplotlib.colors.Normalize) or isinstance(scale, ImageNormalize): norm = scale else: raise ValueError("scale {} is not available.".format(scale)) if offset_axes: extent = (offset_axes[0]-0.5, offset_axes[0] + image.shape[1]-0.5, offset_axes[1]-0.5, offset_axes[1] + image.shape[0]-0.5) else: extent = (-0.5, image.shape[1]-0.5, -0.5, image.shape[0]-0.5) if ax is None: ax = plt.gca() if isinstance(cmap, str): cmap = plt.get_cmap(cmap) im = ax.imshow(image, origin=origin, norm=norm, extent=extent, cmap=cmap, interpolation='nearest', **kwargs) if xlabel is not None: ax.set_xlabel(xlabel) if ylabel is not None: ax.set_ylabel(ylabel) if title is not None: ax.set_title(title) ax.set_xlim([extent[0], extent[1]]) ax.set_ylim([extent[2], extent[3]]) if make_cbar: cbar = plt.colorbar(im, norm=norm, ax=ax, orientation='horizontal', pad=0.02) cbar.set_label(clabel) if cbar_ticks is not None: cbar.set_ticks(cbar_ticks) if cbar_ticklabels is not None: cbar.set_ticklabels(cbar_ticklabels) # Settings for ticks (to make Mikkel happy): ax.xaxis.set_major_locator(MaxNLocator(integer=True)) ax.xaxis.set_minor_locator(MaxNLocator(integer=True)) ax.yaxis.set_major_locator(MaxNLocator(integer=True)) ax.yaxis.set_minor_locator(MaxNLocator(integer=True)) ax.tick_params(direction='out', which='both', pad=5) ax.xaxis.tick_bottom() #ax.set_aspect(aspect) return im
ImageNormalize,LinearStretch,ZScaleInterval) DIR = #Directory with the frames to stack if len(sys.argv) < 2: source = 'list.txt' #list.txt should contain the name of the frames to stack else: source = sys.argv[1] with open(DIR+source, 'r') as fp: files = fp.read().splitlines() final_image = [] for filename in files[0:50]: hdulist = fits.open(DIR+filename,ignore_missing_end=True) image = hdulist['SCI'].data final_image.append(image) hdulist.close() final_image = np.median(final_image,0) data = final_image norm = ImageNormalize(data, interval=ZScaleInterval(),stretch=LinearStretch()) plt.imshow(data, cmap='Greys', origin='lower', norm=norm) hdr = fits.open(DIR+files[33],ignore_missing_end=True)['SCI'].header fits.writeto('stacked_0_50.fits',final_image,hdr, overwrite=True) plt.savefig('stacked_0_50.pdf',overwrite=True) plt.show()
#import astropy #astropy.test() # from astropy.visualization import SqrtStretch # stretch = SqrtStretch() # print(stretch([0., 0.25, 0.5, 0.75, 1.])) import numpy as np import matplotlib.pyplot as plt from astropy.visualization import (MinMaxInterval, SqrtStretch, ImageNormalize) # Generate a test image image = np.arange(65536).reshape((256, 256)) # Create an ImageNormalize object norm = ImageNormalize(image, interval=MinMaxInterval(), stretch=SqrtStretch()) # or equivalently using positional arguments # norm = ImageNormalize(image, MinMaxInterval(), SqrtStretch()) # Display the image fig = plt.figure() ax = fig.add_subplot(1, 1, 1) im = ax.imshow(image, origin='lower', norm=norm) fig.colorbar(im)
def plot_multiepoch_cutout(ra, dec, measurements, images, radius=300, imagepath_col='path', imageid_col='image_id', time_col='time', samescale=True): ''' Plot multi-epoch cutout in one image in order to plot stokesV plot, you can add a row in images dataframe and pass the corresponding stokesV path in imagepath_col Params: ---------- ra, dec: float position of the source measurements: pandas.DataFrame dataframe contains all measurements, image_id, time of observation etc. images: pandas.DataFrame or NoneType #todo dataframe contains all images information radius: int or float, 300 by default radius of the cutout, in arcsec imagepath_col: str column that saves image path in images dataframe imageid_col: str column that saves imageid in measurements dataframe time_col: str column that saves time of the observation in measurements daraframe samescale: bool, True by default If the image use the same scale and limit for all subplots or not Returns: ---------- fig ''' ### sort the measurements based on time sorted_measurements = measurements.sort_values('time') nepochs = len(sorted_measurements) subplot_layout, figsize = _get_figure_layout(nepochs, 4) ### start plotting fig = plt.figure(figsize=figsize, facecolor='w') norms = None; plotcount = 1 for i, row in sorted_measurements.iterrows(): imageid = row[imageid_col] imagepath = images.loc[imageid][imagepath_col] try: # handle fits file doesnot exist fitsimage = FITSIMAGE(imagepath) cutout = fitsimage.cutout(ra, dec, radius) except: plotcount += 1 continue if (norms == None) or (samescale == False): norms = ImageNormalize(cutout.data,interval=ZScaleInterval()) ### add subplot ax = fig.add_subplot(*subplot_layout, plotcount, projection=cutout.wcs) ax, im = plot_fits(cutout.data, ax, norms) ax = source_crosshair((ra, dec), ax, color='red', sep=radius/12., length=radius/12.) ax.set_title(row['time']) # set axislable invisible ax.coords[0].set_axislabel(' ') ax.coords[1].set_axislabel(' ') plotcount += 1 return fig
def _update_visual_attributes(self, changed, force=False): if not self.enabled: return if self.state.markers_visible: if self.state.density_map: if self.state.cmap_mode == 'Fixed': if force or 'color' in changed or 'cmap_mode' in changed: self.density_artist.set_color(self.state.color) self.density_artist.set_c(None) self.density_artist.set_clim( self.density_auto_limits.min, self.density_auto_limits.max) elif force or any(prop in changed for prop in CMAP_PROPERTIES): c = self.layer[self.state.cmap_att].ravel() set_mpl_artist_cmap(self.density_artist, c, self.state) if force or 'stretch' in changed: self.density_artist.set_norm( ImageNormalize( stretch=STRETCHES[self.state.stretch]())) if force or 'dpi' in changed: self.density_artist.set_dpi(self._viewer_state.dpi) if force or 'density_contrast' in changed: self.density_auto_limits.contrast = self.state.density_contrast self.density_artist.stale = True else: if self.state.cmap_mode == 'Fixed' and self.state.size_mode == 'Fixed': if force or 'color' in changed: self.plot_artist.set_color(self.state.color) if force or 'size' in changed or 'size_scaling' in changed: self.plot_artist.set_markersize( self.state.size * self.state.size_scaling) else: # TEMPORARY: Matplotlib has a bug that causes set_alpha to # change the colors back: https://github.com/matplotlib/matplotlib/issues/8953 if 'alpha' in changed: force = True if self.state.cmap_mode == 'Fixed': if force or 'color' in changed or 'cmap_mode' in changed: self.scatter_artist.set_facecolors( self.state.color) self.scatter_artist.set_edgecolor('none') elif force or any(prop in changed for prop in CMAP_PROPERTIES): c = self.layer[self.state.cmap_att].ravel() set_mpl_artist_cmap(self.scatter_artist, c, self.state) self.scatter_artist.set_edgecolor('none') if force or any(prop in changed for prop in MARKER_PROPERTIES): if self.state.size_mode == 'Fixed': s = self.state.size * self.state.size_scaling s = broadcast_to( s, self.scatter_artist.get_sizes().shape) else: s = self.layer[self.state.size_att].ravel() s = ((s - self.state.size_vmin) / (self.state.size_vmax - self.state.size_vmin)) * 30 s *= self.state.size_scaling # Note, we need to square here because for scatter, s is actually # proportional to the marker area, not radius. self.scatter_artist.set_sizes(s**2) if self.state.line_visible: if self.state.cmap_mode == 'Fixed': if force or 'color' in changed or 'cmap_mode' in changed: self.line_collection.set_array(None) self.line_collection.set_color(self.state.color) elif force or any(prop in changed for prop in CMAP_PROPERTIES): # Higher up we oversampled the points in the line so that # half a segment on either side of each point has the right # color, so we need to also oversample the color here. c = self.layer[self.state.cmap_att].ravel() cnew = np.zeros((len(c) - 1) * 2) cnew[::2] = c[:-1] cnew[1::2] = c[1:] set_mpl_artist_cmap(self.line_collection, cnew, self.state) if force or 'linewidth' in changed: self.line_collection.set_linewidth(self.state.linewidth) if force or 'linestyle' in changed: self.line_collection.set_linestyle(self.state.linestyle) if self.state.vector_visible and self.vector_artist is not None: if self.state.cmap_mode == 'Fixed': if force or 'color' in changed or 'cmap_mode' in changed: self.vector_artist.set_array(None) self.vector_artist.set_color(self.state.color) elif force or any(prop in changed for prop in CMAP_PROPERTIES): c = self.layer[self.state.cmap_att].ravel() set_mpl_artist_cmap(self.vector_artist, c, self.state) if self.state.xerr_visible or self.state.yerr_visible: for eartist in list(self.errorbar_artist[2]): if eartist is None: continue if self.state.cmap_mode == 'Fixed': if force or 'color' in changed or 'cmap_mode' in changed: eartist.set_color(self.state.color) elif force or any(prop in changed for prop in CMAP_PROPERTIES): c = self.layer[self.state.cmap_att].ravel() set_mpl_artist_cmap(eartist, c, self.state) if force or 'alpha' in changed: eartist.set_alpha(self.state.alpha) if force or 'visible' in changed: eartist.set_visible(self.state.visible) if force or 'zorder' in changed: eartist.set_zorder(self.state.zorder) for artist in [ self.scatter_artist, self.plot_artist, self.vector_artist, self.line_collection, self.density_artist ]: if artist is None: continue if force or 'alpha' in changed: artist.set_alpha(self.state.alpha) if force or 'zorder' in changed: artist.set_zorder(self.state.zorder) if force or 'visible' in changed: artist.set_visible(self.state.visible) self.redraw()
def display_multiband(data, geometry=None, mgefit=None, ellipsefit=None, indx=None, magrange=10, inchperband=3, contours=False, png=None, verbose=True): """Display the multi-band images and, optionally, the isophotal fits based on either MGE and/or Ellipse. """ from astropy.visualization import ZScaleInterval as Interval from astropy.visualization import AsinhStretch as Stretch from astropy.visualization import ImageNormalize band = data['band'] nband = len(band) fig, ax = plt.subplots(1, 3, figsize=(inchperband*nband, nband)) for filt, ax1 in zip(band, ax): img = data['{}_masked'.format(filt)] #img = data[filt] norm = ImageNormalize(img, interval=Interval(contrast=0.95), stretch=Stretch(a=0.95)) im = ax1.imshow(img, origin='lower', norm=norm, cmap='viridis', interpolation='nearest') plt.text(0.1, 0.9, filt, transform=ax1.transAxes, #fontweight='bold', ha='center', va='center', color='k', fontsize=14) if mgefit: from mge.mge_print_contours import _multi_gauss, _gauss2d_mge sigmapsf = np.atleast_1d(0) normpsf = np.atleast_1d(1) _magrange = 10**(-0.4*np.arange(0, magrange, 1)[::-1]) # 0.5 mag/arcsec^2 steps #_magrange = 10**(-0.4*np.arange(0, magrange, 0.5)[::-1]) # 0.5 mag/arcsec^2 steps model = _multi_gauss(mgefit[filt].sol, img, sigmapsf, normpsf, mgefit['xpeak'], mgefit['ypeak'], mgefit['pa']) peak = data[filt][mgefit['xpeak'], mgefit['ypeak']] levels = peak * _magrange s = img.shape extent = [0, s[1], 0, s[0]] ax1.contour(model, levels, colors='k', linestyles='solid', extent=extent, alpha=0.75, lw=1) if geometry: from photutils import EllipticalAperture ellaper = EllipticalAperture((geometry.x0, geometry.y0), geometry.sma, geometry.sma*(1 - geometry.eps), geometry.pa) ellaper.plot(color='k', lw=1, ax=ax1) if ellipsefit: if ellipsefit['success']: if len(ellipsefit[filt]) > 0: if indx is None: indx = np.ones(len(ellipsefit[filt]), dtype=bool) nfit = len(indx) # len(ellipsefit[filt]) nplot = np.rint(0.5*nfit).astype('int') smas = np.linspace(0, ellipsefit[filt].sma[indx].max(), nplot) for sma in smas: efit = ellipsefit[filt].get_closest(sma) x, y, = efit.sampled_coordinates() ax1.plot(x, y, color='k', alpha=0.75) else: from photutils import EllipticalAperture geometry = ellipsefit['geometry'] ellaper = EllipticalAperture((geometry.x0, geometry.y0), geometry.sma, geometry.sma*(1 - geometry.eps), geometry.pa) ellaper.plot(color='k', lw=1, ax=ax1) ax1.get_xaxis().set_visible(False) ax1.get_yaxis().set_visible(False) ax1.axis('off') #ax1.set_adjustable('box-forced') ax1.autoscale(False) fig.subplots_adjust(wspace=0.02, top=0.98, bottom=0.02, left=0.02, right=0.98) if png: if verbose: print('Writing {}'.format(png)) fig.savefig(png, bbox_inches='tight', pad_inches=0) plt.close(fig) else: plt.show()
def main(data_title: str, show: bool = False): properties = p.object_params_imacs(data_title) path = properties['data_dir'] raw_path = path + '/0-raw_data_with_calibs/' master_path = path + '/1-master_calibs/' reduced_path = path + '/2-reduced/' u.mkdir_check(raw_path) u.mkdir_check(master_path) u.mkdir_check(reduced_path) for file in os.listdir(path): if os.path.isfile(path + file): shutil.move(path + file, raw_path + file) files = os.listdir(raw_path) biases = [] flats = {} science = {} airmasses = {} print('Creating lists of files.') param_dict = {} for file in files: if file[-5:] == '.fits': hdu = fits.open(raw_path + file) header = hdu[0].header obj = header['OBJECT'] f = header['FILTER'] if 'bias' in obj: biases.append(file) elif 'flat' in obj: if f not in flats: flats[f] = [] flats[f].append(file) else: if f not in science: science[f] = [] airmasses[f] = [] science[f].append(file) airmasses[f].append(header['AIRMASS']) # Loop through chip numbers. for chip in range(1, 9): print(f'Processing biases for chip {chip}.') # Get only those bias frames relevant to the current chip. biases_chip = list(filter(lambda name: name[-6] == str(chip), biases)) bias_hdus = [] for bias in biases_chip: # Correct NAXIS1 & 2 in headers; they are too large in the unreduced files, leading to padding by astropy. bias = ff.correct_naxis(file=raw_path + bias, x=2048, y=4096, write=False) bias_hdus.append(bias) # Stack biases. print(biases_chip) ff.stack(bias_hdus, output=master_path + f'master_bias_c{chip}.fits', stack_type='median', directory=raw_path) master_bias = CCDData.read(master_path + f'master_bias_c{chip}.fits', unit='du') # Loop through filters. for f in science: flats_filter = flats[f] master_path_filter = master_path + f + '/' u.mkdir_check(master_path_filter) print(f'Processing flats for chip {chip}, filter {f}.') # Get only those flats relevant to the current chip and filter. flats_chip = list(filter(lambda name: name[-6] == str(chip), flats_filter)) flats_chip_ccds = [] for flat in flats_chip: flat_ccd = CCDData.read(raw_path + flat, unit='du') # Correct NAXIS1 & 2 in headers; they are too large in the unreduced files, leading to padding by astropy. flat_ccd = ff.correct_naxis(file=flat_ccd, x=2048, y=4096, write=False) # Subtract master bias from each flat. flat_ccd = ccdproc.subtract_bias(ccd=flat_ccd, master=master_bias) flats_chip_ccds.append(flat_ccd) # Stack debiased flats. master_flat = ff.stack(flats_chip_ccds, output=None, stack_type='median') master_flat.writeto(master_path_filter + f'master_flat_c{chip}.fits', overwrite=True) master_flat = CCDData.read(master_path_filter + f'master_flat_c{chip}.fits', unit='du') science_filter = science[f] param_dict[f + '_airmass_mean'] = float(np.nanmean(airmasses[f])) param_dict[f + '_airmass_err'] = float(2 * np.nanstd(airmasses[f])) param_dict[f + '_n_frames'] = len(science_filter) param_dict[f + '_n_exposures'] = len(science_filter) / 8 reduced_path_filter = reduced_path + f + '/' u.mkdir_check(reduced_path_filter) # Loop through the science images taken on the current chip. for image in filter(lambda name: name[-6] == str(chip), science_filter): print(f'Reducing {image}.') image_ccd = CCDData.read(raw_path + image, unit='du') if show: norm = ImageNormalize(image_ccd.data, interval=ZScaleInterval(), stretch=SqrtStretch()) plt.imshow(image_ccd.data, origin='lower', norm=norm) plt.title('Unreduced image') plt.show() # Correct NAXIS1 & 2 in header; they are too large in the unreduced files, leading to padding by astropy. image_ccd = ff.correct_naxis(file=image_ccd, x=2048, y=4096, write=False) if show: norm = ImageNormalize(image_ccd.data, interval=ZScaleInterval(), stretch=SqrtStretch()) plt.imshow(image_ccd.data, origin='lower', norm=norm) plt.title('After NAXIS correction') plt.show() # Subtract master bias from science image. image_ccd = ccdproc.subtract_bias(image_ccd, master_bias) if show: norm = ImageNormalize(image_ccd.data, interval=ZScaleInterval(), stretch=SqrtStretch()) plt.imshow(image_ccd.data, origin='lower', norm=norm) plt.title('After debiasing') plt.show() # Divide by master flat. image_ccd = ccdproc.flat_correct(image_ccd, master_flat) if show: norm = ImageNormalize(image_ccd.data, interval=ZScaleInterval(), stretch=SqrtStretch()) plt.imshow(image_ccd.data, origin='lower', norm=norm) plt.title('After flatfielding') plt.show() # Convert back to HDU object for saving. image_ccd = image_ccd.to_hdu() image_ccd.writeto(reduced_path_filter + image, overwrite=True) u.write_log(path=reduced_path + data_title + ".log", action=f'Reduced using 1-reduce.py') p.add_params(path + '/output_values', param_dict)
if args.wcs == 'True' or args.wcs == 'yes': fig, ax = plt.subplots(figsize=(7, 7)) ax = plt.subplot(projection=wcs_object) gaia = Irsa.query_region(SkyCoord(hdr[0].header['CRVAL1'] * u.deg, hdr[0].header['CRVAL2'] * u.deg, frame='fk5'), catalog="gaia_dr2_source", spatial="Cone", radius=4 * u.arcmin) if len(gaia) == 0: log.info('No GAIA stars found within 4 arcmin for starlist.') plt.close() else: ax.imshow(sci_med, cmap='gray', norm=ImageNormalize(sci_med, interval=ZScaleInterval())) _, median, std = sigma_clipped_stats(sci_med, sigma=3.0) daofind = DAOStarFinder(fwhm=7.0, threshold=5. * std) sources = daofind(np.asarray(sci_med)) for l, m in enumerate(gaia['source_id']): x, y = (wcs.WCS(hdr[0].header)).all_world2pix( gaia['ra'][l], gaia['dec'][l], 1) ax.add_patch( patches.Circle((x, y), radius=4, edgecolor='g', alpha=0.5, facecolor='none', linewidth=2, label='Gaia star: RA = %f, Dec = %f' % (gaia['ra'][l], gaia['dec'][l]),
def __init__(self, parent, controller): tk.Frame.__init__(self, parent) label = tk.Label(self, text="u", font=LARGE_FONT) label.pack(pady=10, padx=10) button = ttk.Button(self, text="optical g", command=lambda: controller.show_frame(GPage)) button.pack() button2 = ttk.Button(self, text="optical r", command=lambda: controller.show_frame(RPage)) button2.pack() button3 = ttk.Button(self, text="optical i", command=lambda: controller.show_frame(IPage)) button3.pack() f = Figure(figsize=(8, 6)) a = f.add_subplot(111) f.subplots_adjust(left=0, bottom=0.005, right=1, top=1) a.get_xaxis().set_visible(False) a.get_yaxis().set_visible(False) # add axes for sliders ax_norma = f.add_axes([0.81, 0.1, 0.15, 0.025]) ax_contr = f.add_axes([0.81, 0.05, 0.15, 0.025]) hdu_list = fits.open(sky_image) hdu_list.info() img = hdu_list[0].data contrast = image_contrast interval = ZScaleInterval(contrast=contrast) vmin, vmax = interval.get_limits(img) stretch_options = { 'LogStretch': LogStretch(power_normalise), 'PowerStretch': PowerStretch(power_normalise), } norm = ImageNormalize(vmin=vmin, vmax=vmax, stretch=stretch_options[image_scaling]) a.imshow(img, origin='lower', norm=norm, cmap='cividis') # Embedding In Tk canvas = FigureCanvasTkAgg(f, self) canvas.draw() canvas.get_tk_widget().pack(side=tk.TOP, fill=tk.BOTH, expand=True) # Show ToolBar toolbar = NavigationToolbar(canvas, self) toolbar.update() # Activate Zoom toolbar.zoom(self) canvas._tkcanvas.pack(side=tk.TOP, fill=tk.BOTH, expand=True) delta_f = 0.1 # add sliders s_norma = Slider(ax_norma, 'Normalise', 0.1, 5.0, valinit=power_normalise, valstep=delta_f) s_contr = Slider(ax_contr, 'Contrast', 0.1, 1.0, valinit=contrast) def update(val): n_norma = s_norma.val n_contr = s_contr.val # assign new values to contrast and normalise interval = ZScaleInterval(contrast=n_contr) vmin, vmax = interval.get_limits(img) norm = ImageNormalize(vmin=vmin, vmax=vmax, stretch=PowerStretch(n_norma)) a.imshow(img, origin='lower', norm=norm, cmap='cividis') # saving data to json file SaveJson(n_norma, n_contr) canvas.draw_idle() s_norma.on_changed(update) s_contr.on_changed(update) hdu_list.close()
def test_map_sequence_plot_custom_cmap_norm(aia171_test_map, hmi_test_map): seq = sunpy.map.Map([aia171_test_map, hmi_test_map], sequence=True) animation = seq.plot(cmap='Greys', norm=ImageNormalize(vmin=0, vmax=100)) animation._step()
def display_multiple_trajectory(arrow_coord, map, map_2, scale="logZscale"): image_file = get_pkg_data_filename(map) image_data = fits.getdata(image_file, ext=0) image_file_2 = get_pkg_data_filename(map_2) image_data_2 = fits.getdata(image_file_2, ext=0) fig, ax = plt.subplots(1, 2, figsize=(15, 15)) #print real_arrow if scale == "linZscale": norm = ImageNormalize(image_data, interval=ZScaleInterval(contrast=0.1), stretch=SqrtStretch()) im = ax[0].imshow(image_data, cmap='gray', norm=norm) im_2 = ax[1].imshow(image_data_2, cmap='gray', norm=norm) plt.colorbar(im, ax=ax) if scale == "logZscale": new_image_data = 2.5 * np.log10(abs(image_data)) new_image_data_2 = 2.5 * np.log10(abs(image_data_2)) norm = ImageNormalize(new_image_data - np.min(new_image_data), interval=ZScaleInterval(contrast=0.25), stretch=SqrtStretch()) im = ax[0].imshow(new_image_data - np.min(new_image_data), aspect='auto', cmap='gray') im_2 = ax[1].imshow(new_image_data_2 - np.min(new_image_data_2), aspect='auto', cmap='gray') plt.colorbar(im) #for i,elem in enumerate(arrow_coord): # real_arrow = [elem[0], elem[1], # elem[2]- elem[0], # elem[3] - (elem[1])] # plt.arrow(real_arrow[0], real_arrow[1], real_arrow[2], real_arrow[3], head_width=50, # head_length=50, fc='g', ec='g') real_arrow = [ arrow_coord[0][0], arrow_coord[0][1], arrow_coord[0][2] - arrow_coord[0][0], arrow_coord[0][3] - (arrow_coord[0][1]) ] real_arrow_2 = [ arrow_coord[1][0], arrow_coord[1][1], arrow_coord[1][2] - arrow_coord[1][0], arrow_coord[1][3] - (arrow_coord[1][1]) ] # ax.arrow(real_arrow[0], len(image_data)- real_arrow[1], real_arrow[2],-real_arrow[3], head_width=50, head_length=50, fc='g', ec='g') # print real_arrow ax[0].arrow(real_arrow[0], real_arrow[1], real_arrow[2], real_arrow[3], head_width=50, head_length=50, fc='g', ec='g') ax[1].arrow(real_arrow_2[0], real_arrow_2[1], real_arrow_2[2], real_arrow_2[3], head_width=50, head_length=50, fc='g', ec='g') plt.show()
#pl.subplots_adjust(left=panel_rect[0], bottom=panel_rect[1], right=panel_rect[0]+panel_rect[2], top=panel_rect[1]+panel_rect[3]) pl.imshow(FitsImage, cmap='gray', origin='lower', aspect='equal', norm=LogNorm()) #cb = pl.colorbar() # # Save eps # fig.savefig('%s.scale.log.jpg'%(FitsFile), format='jpg') print('Output to %s'%('%s.scale.log.jpg'%(FitsFile))) # # Plot the fits image in 99.5 percentile # if [[ 1 == 0 ]]; then pl.cla() #fig.delaxes(fig.axes[-1]) # remove previous colorbar #pl.subplots_adjust(left=panel_rect[0], bottom=panel_rect[1], right=panel_rect[0]+panel_rect[2], top=panel_rect[1]+panel_rect[3]) normfun = ImageNormalize(FitsImage, interval=AsymmetricPercentileInterval(19.5,99.5)) pl.imshow(FitsImage, cmap='gray', origin='lower', aspect='equal', norm=normfun) #cb = pl.colorbar() # # Save eps # fig.savefig('%s.scale.995.jpg'%(FitsFile), format='jpg') print('Output to %s'%('%s.scale.995.jpg'%(FitsFile))) print('Done!')
def _update_visual_attributes(self, changed, force=False): if not self.enabled: return if self.state.markers_visible: if self.state.density_map: if self.state.cmap_mode == 'Fixed': if force or 'color' in changed or 'cmap_mode' in changed: self.density_artist.set_color(self.state.color) self.density_artist.set_clim(self.density_auto_limits.min, self.density_auto_limits.max) elif force or any(prop in changed for prop in CMAP_PROPERTIES): c = ensure_numerical(self.layer[self.state.cmap_att].ravel()) set_mpl_artist_cmap(self.density_artist, c, self.state) if force or 'stretch' in changed: self.density_artist.set_norm(ImageNormalize(stretch=STRETCHES[self.state.stretch]())) if force or 'dpi' in changed: self.density_artist.set_dpi(self._viewer_state.dpi) if force or 'density_contrast' in changed: self.density_auto_limits.contrast = self.state.density_contrast self.density_artist.stale = True else: if self.state.cmap_mode == 'Fixed' and self.state.size_mode == 'Fixed': if force or 'color' in changed or 'fill' in changed: if self.state.fill: self.plot_artist.set_markeredgecolor('none') self.plot_artist.set_markerfacecolor(self.state.color) else: self.plot_artist.set_markeredgecolor(self.state.color) self.plot_artist.set_markerfacecolor('none') if force or 'size' in changed or 'size_scaling' in changed: self.plot_artist.set_markersize(self.state.size * self.state.size_scaling) else: # TEMPORARY: Matplotlib has a bug that causes set_alpha to # change the colors back: https://github.com/matplotlib/matplotlib/issues/8953 if 'alpha' in changed: force = True if self.state.cmap_mode == 'Fixed': if force or 'color' in changed or 'cmap_mode' in changed or 'fill' in changed: self.scatter_artist.set_array(None) if self.state.fill: self.scatter_artist.set_facecolors(self.state.color) self.scatter_artist.set_edgecolors('none') else: self.scatter_artist.set_facecolors('none') self.scatter_artist.set_edgecolors(self.state.color) elif force or any(prop in changed for prop in CMAP_PROPERTIES) or 'fill' in changed: self.scatter_artist.set_edgecolors(None) self.scatter_artist.set_facecolors(None) c = ensure_numerical(self.layer[self.state.cmap_att].ravel()) set_mpl_artist_cmap(self.scatter_artist, c, self.state) if self.state.fill: self.scatter_artist.set_edgecolors('none') else: colors = self.scatter_artist.get_facecolors() self.scatter_artist.set_facecolors('none') self.scatter_artist.set_edgecolors(colors) if force or any(prop in changed for prop in MARKER_PROPERTIES): if self.state.size_mode == 'Fixed': s = self.state.size * self.state.size_scaling s = broadcast_to(s, self.scatter_artist.get_sizes().shape) else: s = ensure_numerical(self.layer[self.state.size_att].ravel()) s = ((s - self.state.size_vmin) / (self.state.size_vmax - self.state.size_vmin)) # The following ensures that the sizes are in the # range 3 to 30 before the final size_scaling. np.clip(s, 0, 1, out=s) s *= 0.95 s += 0.05 s *= (30 * self.state.size_scaling) # Note, we need to square here because for scatter, s is actually # proportional to the marker area, not radius. self.scatter_artist.set_sizes(s ** 2) if self.state.line_visible: if self.state.cmap_mode == 'Fixed': if force or 'color' in changed or 'cmap_mode' in changed: self.line_collection.set_linearcolor(color=self.state.color) elif force or any(prop in changed for prop in CMAP_PROPERTIES): # Higher up we oversampled the points in the line so that # half a segment on either side of each point has the right # color, so we need to also oversample the color here. c = ensure_numerical(self.layer[self.state.cmap_att].ravel()) self.line_collection.set_linearcolor(data=c, state=self.state) if force or 'linewidth' in changed: self.line_collection.set_linewidth(self.state.linewidth) if force or 'linestyle' in changed: self.line_collection.set_linestyle(self.state.linestyle) if self.state.vector_visible and self.vector_artist is not None: if self.state.cmap_mode == 'Fixed': if force or 'color' in changed or 'cmap_mode' in changed: self.vector_artist.set_array(None) self.vector_artist.set_color(self.state.color) elif force or any(prop in changed for prop in CMAP_PROPERTIES): c = ensure_numerical(self.layer[self.state.cmap_att].ravel()) set_mpl_artist_cmap(self.vector_artist, c, self.state) if self.state.xerr_visible or self.state.yerr_visible: for eartist in ravel_artists(self.errorbar_artist): if self.state.cmap_mode == 'Fixed': if force or 'color' in changed or 'cmap_mode' in changed: eartist.set_color(self.state.color) elif force or any(prop in changed for prop in CMAP_PROPERTIES): c = ensure_numerical(self.layer[self.state.cmap_att].ravel()).copy() c = c[self._errorbar_keep] set_mpl_artist_cmap(eartist, c, self.state) if force or 'alpha' in changed: eartist.set_alpha(self.state.alpha) if force or 'visible' in changed: eartist.set_visible(self.state.visible) if force or 'zorder' in changed: eartist.set_zorder(self.state.zorder) for artist in [self.scatter_artist, self.plot_artist, self.vector_artist, self.line_collection, self.density_artist]: if artist is None: continue if force or 'alpha' in changed: artist.set_alpha(self.state.alpha) if force or 'zorder' in changed: artist.set_zorder(self.state.zorder) if force or 'visible' in changed: # We need to hide the density artist if it is not needed because # otherwise it might still show even if there is no data as the # neutral/zero color might not be white. if artist is self.density_artist: artist.set_visible(self.state.visible and self.state.density_map and self.state.markers_visible) else: artist.set_visible(self.state.visible) self.redraw()
def plot_image(image, ax=None, scale='linear', origin='lower', xlabel='Pixel Column Number', ylabel='Pixel Row Number', clabel='Flux ($e^{-}s^{-1}$)', title=None, show_colorbar=True, **kwargs): """Utility function to plot a 2D image Parameters ---------- image : 2d array Image data. ax : `~matplotlib.axes.Axes` A matplotlib axes object to plot into. If no axes is provided, a new one will be generated. scale : str Scale used to stretch the colormap. Options: 'linear', 'sqrt', or 'log'. origin : str The origin of the coordinate system. xlabel : str Label for the x-axis. ylabel : str Label for the y-axis. clabel : str Label for the color bar. title : str or None Title for the plot. show_colorbar : bool Whether or not to show the colorbar kwargs : dict Keyword arguments to be passed to `matplotlib.pyplot.imshow`. Returns ------- ax : `~matplotlib.axes.Axes` The matplotlib axes object. """ if ax is None: _, ax = plt.subplots() with warnings.catch_warnings(): warnings.simplefilter("ignore", RuntimeWarning) # ignore image NaN values mask = np.nan_to_num(image) > 0 if mask.any() > 0: vmin, vmax = PercentileInterval(95.).get_limits(image[mask]) else: vmin, vmax = 0, 0 norm = None if scale is not None: if scale == 'linear': norm = ImageNormalize(vmin=vmin, vmax=vmax, stretch=LinearStretch(), clip=False) elif scale == 'sqrt': norm = ImageNormalize(vmin=vmin, vmax=vmax, stretch=SqrtStretch(), clip=False) elif scale == 'log': # To use log scale we need to guarantee that vmin > 0, so that # we avoid division by zero and/or negative values. norm = LogNorm(vmin=max(vmin, sys.float_info.epsilon), vmax=vmax, clip=True) else: raise ValueError("scale {} is not available.".format(scale)) cax = ax.imshow(image, origin=origin, norm=norm, **kwargs) ax.set_xlabel(xlabel) ax.set_ylabel(ylabel) ax.set_title(title) if show_colorbar: cbar = plt.colorbar(cax, ax=ax, norm=norm, label=clabel) cbar.ax.yaxis.set_tick_params(tick1On=False, tick2On=False) cbar.ax.minorticks_off() return ax
pl.subplot(2, 2, 4).contour(proj_image1[pky - npix:pky + npix, pkx - npix:pkx + npix], linewidths=[0.1] * 10, colors=['k'] * 10) pl.savefig( f'{diagnostic_figure_path}/{regname}_{survey}_xcorr_diagnostics.png', bbox_inches='tight') pl.figure(2).clf() diffim = (proj_image1 / np.nanpercentile(proj_image1, 99) - proj_image2 / np.nanpercentile(proj_image2, 99)) slices = ndimage.find_objects(np.isfinite(diffim))[0] diffim = (proj_image1 / np.nanpercentile(proj_image1[slices], 99) - proj_image2 / np.nanpercentile(proj_image2[slices], 99)) asinhnorm = ImageNormalize(diffim[slices], interval=PercentileInterval(99.5), stretch=AsinhStretch()) pl.subplot(1, 1, 1).imshow(diffim[slices], origin='lower', norm=asinhnorm) #try: # HiGal.TIMEOUT = 300 # higal_ims = HiGal.get_images(coordinate, radius=radius) # for hgim in higal_ims: # xcorr = image_registration.FITS_tools.register_fits(fh, hgim) # print(f"HiGal{hgim[0].header['WAVELEN']} = {xcorr}") # xcorr = image_registration.FITS_tools.register_fits(fh, hgim, zeromean=True) # print(f"zero-mean HiGal{hgim[0].header['WAVELEN']} = {xcorr}") #except requests.exceptions.ReadTimeout: