Example #1
0
def plot_imag(fig,imag,mask,wcs,ftsize='xx-large',coor=None,title=None,beam=None,cmap='hot',colorbar=False,size=10):
    ax = fig.add_subplot(1,1,1,projection=wcs)
    # ------------------------
    # Display the image
    im = ax.imshow(imag,origin='lower',interpolation='nearest',cmap=colormap(cmap),\
            aspect='equal',vmin=0.)
    ax.contour(mask,linewidths=4,levels=[0.001],colors='blue')
    # ------------------------
    # coordinates
    ra = ax.coords['ra']
    de = ax.coords['dec']
    ra.set_axislabel('R.A. (J2000)',minpad=0.5,size=ftsize)
    de.set_axislabel('Dec. (J2000)',minpad=0.5,size=ftsize)
    ra.set_separator(('$\mathrm{^h}$','$\mathrm{^m}$'))
    ra.set_ticklabel(size=ftsize)
    de.set_ticklabel(size=ftsize)
    # ------------------------
    if title is not None:
        ax.set_title(title,fontsize=ftsize)
    if coor is not None:
        ax.set_xlim(coor[0]-size,coor[0]+size)
        ax.set_ylim(coor[1]-size,coor[1]+size)
    if colorbar:
        cbar = ax.figure.colorbar(im)
        cbar.ax.set_ylabel('mJy$\,$beam$^{-1}$ km$\,$s$^{-1}$',fontsize=ftsize)
    if beam is not None:
        bm = patches.Circle((5,5),radius=args.beam/2,edgecolor='k',facecolor='w',alpha=0.5) # pixel coordinates
        ax.add_patch(bm)

    return ax
Example #2
0
def plot_imag(fig,
              imag,
              mask,
              wcs,
              ftsize='x-large',
              coor=None,
              title=None,
              cmap='hot'):
    ax = fig.add_subplot(1, 2, 2, projection=wcs)

    # ------------------------
    # Display the image
    im = ax.imshow(imag,origin='lower',interpolation='nearest',cmap=colormap(cmap),\
            aspect='equal',vmin=0.)#,norm=LogNorm()) #,vmin=0.0005,vmax=0.005
    ax.contour(mask, linewidths=2, levels=[0.001], alpha=0.8, colors='grey')
    # ------------------------
    # coordinates
    ra = ax.coords['ra']
    de = ax.coords['dec']
    ra.set_axislabel('R.A. (J2000)', minpad=0.5, size=ftsize)
    de.set_axislabel('Dec. (J2000)', minpad=0.5, size=ftsize)
    ra.set_separator(('$\mathrm{^h}$', '$\mathrm{^m}$'))
    ra.set_ticklabel(size=ftsize)
    de.set_ticklabel(size=ftsize)
    # ------------------------
    if title is not None:
        ax.set_title(title, fontsize=ftsize)
    if coor is not None:
        ax.plot(coor[0], coor[1], marker='*')
    cbar = ax.figure.colorbar(im)
    cbar.ax.set_ylabel('mJy$\,$beam$^{-1}$ km$\,$s$^{-1}$', fontsize=ftsize)

    return ax
Example #3
0
def plot_image(file_in,
               file_out=None,
               file_contour=None,
               file_reg=None,
               resize=None,
               contour=False,
               levels=None,
               contour_color='#d0d0d0',
               plot=False,
               oformat='png',
               skycoor='equ',
               vmin=None,
               vmax=None,
               pmin=0.25,
               pmax=99.75,
               stretch='linear',
               vmid=None,
               exponent=2,
               cmap='hot',
               beam=None,
               colorbar=None,
               dendro=None,
               file_catalog=None,
               addgal=True,
               save=True,
               smooth=None,
               beamcolor='blue'):
    if smooth == 0:
        smooth = None
    fig = aplpy.FITSFigure(file_in)
    fig.show_colorscale(vmin=vmin,
                        vmax=vmax,
                        pmin=pmin,
                        pmax=pmax,
                        cmap=colormap(cmap),
                        aspect='equal',
                        smooth=smooth,
                        stretch=stretch,
                        vmid=vmid,
                        exponent=exponent)

    #    fig.hide_colorscale()
    if resize is not None:
        fig.recenter(resize[0], resize[1], width=resize[2], height=resize[3])
#-------------------------------------------------------------------
#   ADD beam
    if beam is not None:
        fig.add_beam(major=beam / 60,
                     minor=beam / 60,
                     angle=0,
                     corner='bottom left')
        fig.beam.set_alpha(0.5)
        fig.beam.set_edgecolor(beamcolor)
        fig.beam.set_facecolor('white')
        fig.beam.set_linewidth(3)
#-------------------------------------------------------------------
#    fig.show_circles([50.05,], [-0.85,], 0.028,color='white',linewidth=1)
#    fig.add_label(50.05, -0.95,"FWHM 3.4'",color='white')
#-------------------------------------------------------------------
    if addgal:
        l = np.arange(30, 70, 0.05)
        b0 = np.zeros(int((70 - 30) / 0.05))
        bp = np.zeros(int((70 - 30) / 0.05)) + 0.5
        bm = np.zeros(int((70 - 30) / 0.05)) - 0.5

        gal_c0 = SkyCoord(l, b0, frame=Galactic, unit="deg")
        gal_cp = SkyCoord(l, bp, frame=Galactic, unit="deg")
        gal_cm = SkyCoord(l, bm, frame=Galactic, unit="deg")

        ra0 = gal_c0.icrs.ra.value
        rap = gal_cp.icrs.ra.value
        ram = gal_cm.icrs.ra.value
        dec0 = gal_c0.icrs.dec.value
        decp = gal_cp.icrs.dec.value
        decm = gal_cm.icrs.dec.value

        fig.show_lines([np.array([ra0,dec0]),np.array([rap,decp]),np.array([ram,decm])], \
                color='darkgrey', linewidths=2, linestyle=['-','--','--'])
        fig.add_label(283.95,
                      2.18,
                      r"b=$0^{\circ}$",
                      color='black',
                      fontsize=14)
        fig.add_label(283.38,
                      2.18,
                      r"b=$0.5^{\circ}$",
                      color='black',
                      fontsize=14)
        fig.add_label(284.42,
                      1.8,
                      r"b=$-0.5^{\circ}$",
                      color='black',
                      fontsize=14)

    if skycoor == 'gal':
        fig.axis_labels.set_xtext('Galactic Longitude')
        fig.axis_labels.set_ytext('Galactic Latitude')
        fig.tick_labels.set_xformat('dd.d')
        fig.tick_labels.set_yformat('dd.d')
        fig.ticks.set_xspacing(0.5)
        fig.ticks.set_yspacing(0.5)
        #fig.ticks.set_minor_frequency(5)
    else:
        z = 0

    fig.axis_labels.set_font(size='xx-large')
    fig.tick_labels.set_font(size='xx-large')

    fig.ticks.show_x()
    fig.ticks.show_y()

    fig.ticks.set_linewidth(1)
    fig.ticks.set_length(2)
    fig.ticks.set_color('black')
    fig.ticks.show()
    fig.set_nan_color('white')
    if colorbar is not None:
        fig.add_colorbar()
        fig.colorbar.show()
        fig.colorbar.set_axis_label_text(colorbar)
        fig.colorbar.set_axis_label_font(size='xx-large')
        fig.colorbar.set_font(size='xx-large')
#-------------------------------------------
# DS9 regions
    if file_reg is not None:
        fig.show_regions(file_reg)

#-------------------------------------------
# add contour
#-------------------------------------------
    if contour:
        if levels is not None:
            if file_contour is None:
                file_contour = file_in
            fig.show_contour(file_contour,
                             levels=levels,
                             smooth=1,
                             colors=contour_color,
                             linewidths=1)
        else:
            print("contour levels is not set")


#------------------------
# Plot all leaves and branches on full frame of Moment0 map.
#------------------------
# add sources in catalog
#    add_source(ax0,catalog,wcs)
# ------------------------
# leaf label
    list_idx = []  # raw index
    list_idv = []  # sorted index
    list_peak = []  # raw peaks
    for i, struc in enumerate(dendro.leaves):
        peak = struc.get_peak()[1]
        list_peak.append(peak)
        list_idx.append(struc.idx)
    peak_ind = np.argsort(np.array(list_peak))[::-1]
    leaves_idx_arr = np.array(list_idx)[peak_ind]
    # ------------------------

    colors = ['blue', 'red', 'green']
    c = 1
    for i, struc in enumerate(dendro.trunk):
        if struc.is_leaf:
            leaf_label = add_leaf_label(fig, leaves_idx_arr, struc)
            color = colors[0]
            subtree = []
            line = 'solid'
        elif struc.is_branch:  # branch
            color = colors[c]
            c = c + 1
            subtree = struc.descendants
            line = 'dashed'

        mask = struc.get_mask().mean(axis=0)

        fig.show_contour(mask,
                         linewidths=1.5,
                         levels=[0.001],
                         alpha=0.8,
                         colors=[color],
                         linestyles=line)  #,smooth=1)
        fig.add_label(283.95,
                      2.18,
                      r"b=$0^{\circ}$",
                      color='black',
                      fontsize=14)

        print(color)

        for j, sub_struc in enumerate(subtree):
            if sub_struc.is_leaf:
                leaf_label = add_leaf_label(fig, leaves_idx_arr, sub_struc)
                mask = sub_struc.get_mask().mean(axis=0)
                fig.show_contour(mask,
                                 linewidths=1.5,
                                 levels=[0.001],
                                 alpha=0.8,
                                 colors=[color])  #,smooth=1)

    if file_catalog is not None:
        add_source(fig, file_catalog)

    if save:
        if file_out is None:
            file_out = 'm0-clumps_{}.png'.format(cmap)
        fig.save(file_out, dpi=300, format=oformat, adjust_bbox=True)

    if plot:
        plt.show()
    fig.close()
    return 0
Example #4
0
def main(args):

    #    #------------------------
    #    #    Load DataCube
    #    #------------------------
    #    print('Load DataCube')
    #    hdu = fits.open(args.file_cube)[0]
    #    hdr = hdu.header
    #    wcs = WCS(header=hdr).celestial
    #    data = hdu.data
    #    nchan = data.shape[0]
    #    velo = (np.arange(nchan) - hdr['CRPIX3'] + 1) * hdr['CDELT3'] + hdr['CRVAL3']
    #    ny = data.shape[1]
    #    nx = data.shape[2]
    #
    #    # unit convert: Jy/beam -> mJy/pix
    #    beam = 4.7 # arcmin
    #    pix = 1.0 # arcmin
    #    pix_over_beam = pix**2/((beam/2)**2*np.pi)
    #    data = data * 1000 * pix_over_beam # x Jy/beam = (x * pix/beam) Jy/pix
    #    imag0 = data[args.chan_0:args.chan_1,:,:].mean(axis=0)
    #    print(imag0.shape)

    #------------------------
    #    Load Moment 0 map
    #------------------------
    hdu = fits.open(args.file_map)[0]
    hdr = hdu.header
    wcs = WCS(header=hdr).celestial
    imag0 = hdu.data
    print(imag0.shape)

    #------------------------
    #    Load dendrogram
    #------------------------
    print('Load Dendrogram')
    d = Dendrogram.load_from(args.file_d + '.hdf5')

    #------------------------
    # Plot all leaves and branches on full frame of Moment0 map.
    #------------------------
    norm = simple_norm(imag0,
                       stretch='asinh',
                       asinh_a=0.18,
                       min_percent=5,
                       max_percent=100)

    fig0 = plt.figure(figsize=(8, 8))
    ax0 = fig0.add_subplot(1, 1, 1, projection=wcs)
    im0 = ax0.imshow(imag0,origin='lower',interpolation='nearest',cmap=colormap(args.cmap),\
            aspect='equal')#,norm=norm)
    # coordinates
    if args.proj == 'equ':
        ra0 = ax0.coords['ra']
        de0 = ax0.coords['dec']
        ra0.set_axislabel('R.A. (J2000)', minpad=0.5, size="xx-large")
        de0.set_axislabel('Dec. (J2000)', minpad=0.5, size="xx-large")
        ra0.set_separator(('$\mathrm{^h}$', '$\mathrm{^m}$'))
        ra0.set_ticklabel(size="xx-large")
        de0.set_ticklabel(size="xx-large")
    elif args.proj == 'gal':
        l0 = ax0.coords['glon']
        b0 = ax0.coords['glat']
        l0.set_axislabel('l deg', minpad=0.5, size="xx-large")
        b0.set_axislabel('b deg', minpad=0.5, size="xx-large")
        l0.set_ticklabel(size="xx-large")
        b0.set_ticklabel(size="xx-large")

    # beam
    if args.beam is not None:
        beam = patches.Circle((5, 5),
                              radius=args.beam / 2,
                              edgecolor='k',
                              facecolor='w',
                              alpha=0.5)  # pixel coordinates
        ax0.add_patch(beam)
    # ------------------------
    # add sources in catalog
    if args.file_c is not None:
        add_source(ax0, args.file_c, wcs)
    # ------------------------
    # leaf label
    list_idx = []  # raw index
    list_idv = []  # sorted index
    list_peak = []  # raw peaks
    for i, struc in enumerate(d.leaves):
        peak = struc.get_peak()[1]
        list_peak.append(peak)
        list_idx.append(struc.idx)
    peak_ind = np.argsort(np.array(list_peak))[::-1]
    leaves_idx_arr = np.array(list_idx)[peak_ind]
    # ------------------------

    colors = ['blue', 'red', 'green']
    c = 1
    for i, struc in enumerate(d.trunk):
        if struc.is_leaf:
            leaf_label = add_leaf_label(ax0, leaves_idx_arr, struc)
            file_out = 'leaf_{:d}_{}.png'.format(leaf_label, args.cmap)
            color = colors[0]
            subtree = []
            line = 'solid'
        elif struc.is_branch:  # branch
            file_out = 'branch_{:d}_{}.png'.format(struc.idx, args.cmap)
            color = colors[c]
            c = c + 1
            subtree = struc.descendants
            line = 'dashed'

        mask = struc.get_mask().mean(axis=0)
        ax0.contour(mask,
                    linewidths=1.5,
                    levels=[0.001],
                    alpha=0.8,
                    colors=[color],
                    linestyles=line)

        for j, sub_struc in enumerate(subtree):
            if sub_struc.is_leaf:
                leaf_label = add_leaf_label(ax0, leaves_idx_arr, sub_struc)
                file_out = 'leaf_{:d}_{}.png'.format(leaf_label, args.cmap)
                mask = sub_struc.get_mask().mean(axis=0)
                print(mask.shape)
                ax0.contour(mask,
                            linewidths=1.5,
                            levels=[0.001],
                            alpha=0.8,
                            colors=[color])

    fig0.savefig('m0-clumps_{}.png'.format(args.cmap),
                 dpi=300,
                 format='png',
                 bbox_inches='tight')

    return 0