HLS_mask.py 5.8 KB
Newer Older
LUSTIG Peter's avatar
LUSTIG Peter committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
import numpy as np
import matplotlib.pyplot as plt
from astropy.io import fits
from pathlib import Path
from matplotlib.figure import Figure


class myfigure(Figure):
    def save(self, filename, outdir='/home/peter/theoryplots'):

        outpath = Path(outdir)
        formats = ['.png', '.svg', '.pdf']

        for format in formats:
            _filename = outpath / (filename + format)
            self.savefig(str(_filename))

# %matplotlib tk



shape = (501, 501)
fwhm_pix = 6.25
circle_radius = 3 * fwhm_pix
idy, idx = np.meshgrid(np.arange(shape[0]), np.arange(shape[1]))
mean = (250, 251)
pixdist2 = np.square(idy-mean[0]) + np.square(idx-mean[1])
mask = pixdist2 < (circle_radius ** 2)

phdu = fits.PrimaryHDU()
ihdu = fits.ImageHDU(data=mask.astype(int), name='HLSmask')
ihdu.header['radius'] = circle_radius
ihdu.header['xcenter'] = mean[0]
ihdu.header['ycenter'] = mean[1]


# Load data for plot
maskdir = Path('/home/peter/Dokumente/Uni/Paris/Stage/FirstSteps/verify')
with fits.open(maskdir / 'snrmasks.fits') as hdul:
    areamask = (hdul['SNRMASK0'].data).astype(bool)
    wcs = WCS(hdul['SNRMASK0'].header)

newfile = '/home/peter/Dokumente/Uni/Paris/Stage/NicePlots/newimage.fits'
SNRimg = fits.getdata(newfile, 'SNR')

# %%
if 1:
    bandadd = ' 1mm'
    plt.close('all')
    import matplotlib.pylab as pl
    from matplotlib.colors import ListedColormap

    cmap = pl.cm.get_cmap('binary')
    my_cmap = cmap(np.arange(cmap.N))
    my_cmap[:,-1] = np.linspace(0, 1, cmap.N)
    my_cmap = ListedColormap(my_cmap)
    globmask = np.array(areamask | mask, dtype=bool)

    labelsize = 23
    titlesize = 28
    ticklabelsize = 18

    plotwidth = .35
    plotheight = .8
    # %matplotlib tk
    # Hitmap, Variancemap with colorbar try
    fig = plt.figure(figsize=(14, 6))
    # ax1 = fig.add_subplot(121, projection=nm.wcs)

    ax1 = fig.add_axes([.07, .1, plotwidth, plotheight], projection=nm.wcs)
    imhits = ax1.imshow(SNRimg, origin='lower')
    ra = ax1.coords[0]
    dec = ax1.coords[1]
    # ax1.set_title('Nhits{}'.format(bandadd), fontsize=25, y=1.02)
    ax1.set_title('HLS091828{}'.format(bandadd), fontsize=titlesize, y=1.02)
    ra.set_axislabel('Ra', minpad=0.4, fontsize=labelsize)
    ra.set_ticklabel(fontsize=ticklabelsize)
    dec.set_axislabel('Dec', minpad=-0.5, fontsize=labelsize)
    dec.set_ticklabel(fontsize=ticklabelsize)
    # %matplotlib tk
    # fig.colorbar(imhits, shrink=0.7)
    # (ax1x0, ax1y0), (ax1x0, ax1y0) =

    # ax2 = fig.add_subplot(122, projection=nm_mf.wcs)
    ax2 = fig.add_axes([0.93-plotwidth, 0.1, plotwidth, plotheight], projection=nm.wcs)
    snrvmin, snrvmax = -3, 5
    imstddev = ax2.imshow(SNRimg, vmin=snrvmin, vmax=snrvmax, origin='lower')
    ax2.imshow(globmask, origin='lower', alpha=0.6, cmap=my_cmap)

    ra = ax2.coords[0]
    dec = ax2.coords[1]
    # ax2.set_title('1 / Var(pix)', fontsize=25, y=1.02)
    ax2.set_title('SNR', fontsize=titlesize, y=1.02)
    ra.set_axislabel('Ra', minpad=0.4, fontsize=labelsize)
    ra.set_ticklabel(fontsize=ticklabelsize)
    dec.set_ticklabel_position('r')
    dec.set_ticklabel(fontsize=ticklabelsize)
    dec.set_ticklabel_visible(True)

    pos1 = ax1.get_position()
    pos2 = ax2.get_position()
    caxwidth = 0.02
    caxmean = (pos1.x1 + pos2.x0) / 2
    cax = fig.add_axes([caxmean-.5*caxwidth, pos1.y0, caxwidth,
                       (pos1.y1-pos1.y0)])
    pos1.height

    cbar = fig.colorbar(imhits, cax=cax)
    # cbar.set_label('', fontsize=20)
    cax.yaxis.set_label_position('left')
    cax.yaxis.set_tick_params(labelsize=ticklabelsize)
    cax.yaxis.set_ticks_position('left')
    cax.set_ylabel('Flux [mJy]', fontsize=labelsize, rotation=270,
                   va='center', labelpad=14.7)
    cax2 = cax.twinx()
    cax2labelpos = cax2.get_yticks()
    nlabels = 9
    cax2labelpos = np.linspace(0, 1, nlabels)

    cax2labels = np.linspace(snrvmin, snrvmax, nlabels)
    cax2.set_yticks(cax2labelpos)
    cax2formattedlabels = ['{:.0f}'.format(label) for label in cax2labels]
    cax2.set_yticklabels(cax2formattedlabels)
    cax2.yaxis.set_tick_params(labelsize=ticklabelsize)
    cax2.set_ylabel('SNR', fontsize=labelsize, labelpad=15)
    # %%
    #%matplotlib tk
    # SaveFigure(fig, )
'''
fig = plt.figure(figsize=(12, 6))
ax.set_title('Detection Mask', fontsize=30, y=1.02)
ax.imshow(globmask, origin='lower', alpha=0.5, cmap=my_cmap)
ra = ax.coords[0]
dec = ax.coords[1]
print(type(ra))
ra.set_axislabel('Ra', minpad=0.4, fontsize=25)
ra.set_ticklabel(fontsize=20)
dec.set_axislabel('Dec', minpad=0.17, fontsize=25)
dec.set_ticklabel(fontsize=20)
plt.subplots_adjust(left=0.16)
plt.show()
'''

if 0:
    import matplotlib.pylab as pl
    from matplotlib.colors import ListedColormap

    cmap = pl.cm.get_cmap('binary')
    my_cmap = cmap(np.arange(cmap.N))
    my_cmap[:,-1] = np.linspace(0, 1, cmap.N)
    my_cmap = ListedColormap(my_cmap)

    #sys.exit()
    maskfile = 'shit...'
    fname = 'jgfsjdg'
    hdul = fits.open(fname)
    areamask = hdul['MATCHMASK'].data != 0

    globmask = np.array(areamask | mask, dtype=bool)
    realfile = '/home/peter/Dokumente/Uni/Paris/Stage/data/map.fits'
    from nikamap import NikaMap
    nm = NikaMap.read(realfile)
    mf_nm = nm.match_filter(nm.beam)
    std = mf_nm.check_SNR()
    mf_nm.uncertainty.array *= std
    # mf_nm.mask = np.array(mf_nm.mask | mask, dtype=bool)
    # plt.figure()
    im = mf_nm.plot_SNR()
    ax = plt.gca()
    ax.set_title('Detection Mask', fontsize=30, y=1.02)
    ax.imshow(globmask, origin='lower', alpha=0.5, cmap=my_cmap)
    ra = ax.coords[0]
    dec = ax.coords[1]
    print(type(ra))
    ra.set_axislabel('Ra', minpad=0.4, fontsize=25)
    ra.set_ticklabel(fontsize=20)
    dec.set_axislabel('Dec', minpad=0.17, fontsize=25)
    dec.set_ticklabel(fontsize=20)
    plt.subplots_adjust(left=0.16)
    plt.show()


# fits.HDUList([phdu, ihdu]).writeto('hlsmask3.fits', overwrite=False)