示例#1
0
def bolocam_mask():
    bolocam = fits.open(config.OUTPUT + 'bolocam_master_template.fits')
    header = bolocam[0].header
    temphead = fits.PrimaryHDU()
    bolohead = temphead.header
    # Setting the tempheader for the bolocam data
    bolohead.set('CRVAL1' , header['CRVAL1']) # DEC in deg of ref pixel
    bolohead.set('CRVAL2' , header['CRVAL2']) # RA in deg of ref pixel
    bolohead.set('CRPIX1' , header['CRPIX1']) # ref pixel x
    bolohead.set('CRPIX2' , header['CRPIX2']) # ref pixel y
    bolohead.set('CD1_1' , header['CD1_1']) # Deg/pixel
    bolohead.set('CD1_2' , header['CD1_2']) # Deg/pixel
    bolohead.set('CD2_1' , header['CD2_1']) # Deg/pixel
    bolohead.set('CD2_2' , header['CD2_2']) # Deg/pixel
    bolohead.set('EPOCH' , 2000)
    bolohead.set('EQUINOX' , 2000)
    bolohead.set('CTYPE1' , header['CTYPE1']) # coord type
    bolohead.set('CTYPE2' , header['CTYPE2']) # coord type

    bolo_shape = bolocam[0].data.shape
    bolo = np.ones((bolo_shape[0],bolo_shape[1]))
    hdu = fits.PrimaryHDU(bolo,bolohead)
    bolo_psw = hcongrid(hdu.data,hdu.header,maps[0]['shead'])
    bolo_pmw = hcongrid(hdu.data,hdu.header,maps[1]['shead'])
    bolo_plw = hcongrid(hdu.data,hdu.header,maps[2]['shead'])
# We will use a gaussian kernel for simplicity, but in reality a proper
# PSF kernel should be used (See, e.g., Aniano+ 2011).
##
# First of all, all maps must be in surface brightness units. 
# PACS maps need hence to be converted. We choose the My/sr conversion
# to keep consistency with SPIRE maps.
if wind < 2:
	print "Now converting the flux into surface brightness units..."
	flx_sb = flx_zero/(1e6*kk**2*pixsz**2)	# converted in MJy/sr from Jy/pix 
else:
	flx_sb = flx_zero
print "Building the gaussian simulating the PSF"
print ''
# the following is the sigma of the gaussian, in pixels
sigma = np.sqrt(psf[4]**2-psf[wind]**2)/pixsz*fw2sig
kernel = Gaussian2DKernel(stddev=sigma,x_size=20,y_size=20) 
print ''
print "Convolving..."
convolved = convolve(flx_sb, kernel, mode='same')  #, method='direct')
print "Done!"
# now regrids to the new header
print ''
print 'Regridding...'
flxmap = hcongrid(convolved,header,header500)
# Now converting from MJy/sr to Jy/px
flx_reg = flxmap*1e6*kk**2*pixsz500**2
print ''
print 'Writing the file with the convolved inmage...'
hdu_s = fits.PrimaryHDU(flx_reg,header=header500)
hdu_s.writeto(outfile)
示例#3
0
print 'Done...!'

nl = header1["NAXIS1"]
map = cube[0, :, :]
# Note the a convolution must be performed in surface brightness units
# We first define the convolution kernel.
# We will use a gaussian kernel for simplicity, but in reality a proper
# PSF kernel should be used (See, e.g., Aniano+ 2011)
# the sigma of the gaussian (PSF) should be defined in pixels
pixsz1 = np.abs(header1["CDELT1"]) * 3600.
pixsz2 = np.abs(header2["CDELT1"]) * 3600.
# first of all, all maps must be in surface brightness units.
print "I am converting the flux into surface brightness"
flx_sb = map / (kk**2 * pixsz1**2)  # converted in K/sr from K
print "Building the gaussian simulating the PSF"
# the following is the gaussian FWHM in pixels
sigma = np.sqrt(psf2**2 - psf1**2) / pixsz1
#kernel = Gaussian2DKernel(stddev=sigma,x_size=xs,y_size=ys)
kernel = Gaussian2DKernel(stddev=sigma, x_size=40, y_size=40)
print "Convolving..."
convolved = scipy_convolve(flx_sb, kernel, mode='same')  #, method='direct')
print "Done!"
# now regrids to the new header
flxmap = hcongrid(convolved, header1, header2)
# Now converting from K/sr to K
flx_reg = flxmap * kk**2 * pixsz2**2
outfile = 'convolved.fits'

hdu_s = fits.PrimaryHDU(flx_reg, header=header2)
hdu_s.writeto(outfile)
示例#4
0
                reshld = rbf(XI, YI)

                #now add the values to the residual image
                res[ee:ee + bxs, oo:oo + bxs] = reshld
                tts = tts + 1

    #get the median background
        mbck = numpy.median(bck)
        sbck = numpy.median(sbk)

        #subtract the sky gradient and add back the median background
        sub = bigimg - res
        sub = sub + mbck

        #align the image
        algn = hcongrid(sub, header, rhead)

        #update the header
        header['CTYPE1'] = rhead['CTYPE1']
        header['CTYPE2'] = rhead['CTYPE2']
        header['CRVAL1'] = rhead['CRVAL1']
        header['CRVAL2'] = rhead['CRVAL2']
        header['CRPIX1'] = rhead['CRPIX1']
        header['CRPIX2'] = rhead['CRPIX2']
        header['CD1_1'] = rhead['CD1_1']
        header['CD1_2'] = rhead['CD1_2']
        header['CD2_1'] = rhead['CD2_1']
        header['CD2_2'] = rhead['CD2_2']

        #update the header
        header['medback'] = mbck
示例#5
0
文件: clean.py 项目: ryanoelkers/DIA
				reshld = rbf(XI, YI)
			
				#now add the values to the residual image
				res[ee:ee+bxs, oo:oo+bxs] = reshld
				tts = tts+1

		#get the median background
		mbck = numpy.median(bck)
		sbck = numpy.median(sbk)
	
		#subtract the sky gradient and add back the median background
		sub = bigimg-res
		sub = sub + mbck

		#align the image
		algn = hcongrid(sub, header, rhead)

		#update the header
		header['CTYPE1'] = rhead['CTYPE1']
		header['CTYPE2'] = rhead['CTYPE2']
		header['CRVAL1'] = rhead['CRVAL1']
		header['CRVAL2'] = rhead['CRVAL2']
		header['CRPIX1'] = rhead['CRPIX1']
		header['CRPIX2'] = rhead['CRPIX2']
		header['CD1_1'] = rhead['CD1_1']
		header['CD1_2'] = rhead['CD1_2']
		header['CD2_1'] = rhead['CD2_1']
		header['CD2_2'] = rhead['CD2_2']

		#update the header
		header['medback'] = mbck
示例#6
0
def clus_add_sziso(maps,
                   isim,
                   yin,
                   tin,
                   params,
                   verbose=0,
                   testflag=0,
                   saveplot=0,
                   nsim=0,
                   clusname=None):

    errmsg = False

    # Now the bolocam data is in the SPIRE format
    # we can do a loop over the map adding in the false sz signal

    mapsize = len(maps)

    if verbose:
        print('Fetching BOLOCAM maps')
    ''' This is just until Jack gives us more new Bolocam templates... '''
    if clusname == 'rxj1347':
        data_file = CLUSDATA + 'bolocam/new/' + maps[0]['name'] + '.fits'
        # cluster_struct = fits.getdata(data_file)
        hdul = fits.open(data_file)
        header = hdul[0].header
        naxis = hdul[0].data.shape
        # BCAMNORM in units of MJy/sr
        bolocam = clus_convert_bolocam(hdul[0].data,
                                       norm=header['BCAMNORM'],
                                       verbose=verbose,
                                       clusname=clusname)
    else:
        #fetch bolocam data from .sav files in bolocam directory.
        data_file = str('bolocam/data/' + maps[0]['name'] + '.sav')
        data_dict = scipy.io.readsav(CLUSDATA + data_file, python_dict=True)
        cluster_struct = list(data_dict.values())

        bolocam, err = clus_convert_bolocam(cluster_struct,
                                            verbose=verbose,
                                            clusname=clusname)
        if err:
            errmsg = str('clus_convert_bolocam exited with error: ' + err)
            if verbose:
                print(errmsg)
            return None, errmsg

        # Set the size of the image for later use
        naxis = bolocam[0]['deconvolved_image'][0].shape

    naxis = np.array(naxis)
    sziso = fits.PrimaryHDU()
    temphead = sziso.header

    if clusname == 'rxj1347':
        # Setting the tempheader for the bolocam data
        temphead.set('CRVAL1', header['CRVAL1'])  # DEC in deg of ref pixel
        temphead.set('CRVAL2', header['CRVAL2'])  # RA in deg of ref pixel
        temphead.set('CRPIX1', header['CRPIX1'])  # ref pixel x
        temphead.set('CRPIX2', header['CRPIX2'])  # ref pixel y
        temphead.set('CD1_1', header['CD1_1'])  # Deg/pixel
        temphead.set('CD1_2', header['CD1_2'])  # Deg/pixel
        temphead.set('CD2_1', header['CD2_1'])  # Deg/pixel
        temphead.set('CD2_2', header['CD2_2'])  # Deg/pixel
        temphead.set('EPOCH', 2000)
        temphead.set('EQUINOX', 2000)
        temphead.set('CTYPE1', header['CTYPE1'])  # coord type
        temphead.set('CTYPE2', header['CTYPE2'])  # coord type

    else:
        # Set reference pizel position
        crpix1 = int(naxis[0] / 2)
        crpix2 = int(naxis[1] / 2)

        # Setting the tempheader for the bolocam data
        temphead.set(
            'CRVAL1', bolocam[0]['deconvolved_image_ra_j2000_deg'][0][crpix1,
                                                                      crpix2])
        temphead.set(
            'CRVAL2', bolocam[0]['deconvolved_image_dec_j2000_deg'][0][crpix1,
                                                                       crpix2])
        temphead.set('CRPIX1', crpix1)
        temphead.set('CRPIX2', crpix2)
        temphead.set(
            'CD1_1',
            -bolocam[0]['deconvolved_image_resolution_arcmin'][0] / 60.0)
        temphead.set('CD1_2', 0)
        temphead.set('CD2_1', 0)
        temphead.set(
            'CD2_2',
            bolocam[0]['deconvolved_image_resolution_arcmin'][0] / 60.0)
        temphead.set('EPOCH', 2000)
        temphead.set('EQUINOX', 2000)
        temphead.set('CTYPE1', 'RA---TAN')
        temphead.set('CTYPE2', 'DEC--TAN')

    # 14.5  = full size of the bolocam image in pixels ?
    x = np.arange(naxis[0]) - 14.5
    y = np.arange(naxis[1]) - 14.5

    rad = np.zeros((naxis[0], naxis[1]))

    for ix in range(naxis[1]):
        rad[:, ix] = (np.tile(x[ix], naxis[0])**2 + y**2)**(1 / 2)

    # Outer marks the indices of the outside of the circle used to add in the sz effect
    n = 0
    outer = []
    for i in range(len(rad)):
        for j in range(len(rad)):
            if rad[i, j] > 10:
                outer.append(n)
            n += 1

    # Set up the spectral shape of the sz effect to be appled to the 500um map
    if testflag == 0:
        if clusname == 'rxj1347':
            szmap1 = hdul[0].data
        else:
            szmap1 = -1 * bolocam[0]['deconvolved_image'][
                0]  # -1 is needed because SZ amp flips below 217 GHz
        szmap1 = (np.array(szmap1)).flatten()
        sz_mean = np.mean(szmap1[outer])
        szmap2 = [x - sz_mean for x in szmap1]
        sz_max = max(szmap2)
        szmap = [x / sz_max for x in szmap2]

    final_dI = []
    for imap in range(mapsize):
        # Applying the effect to the 500 um and 350 um bands.
        if imap == 2 or imap == 1:
            if testflag == 1:
                szmap, err = IB_model(maps[imap], params, verbose)
                szmap = np.array(szmap)
                plt.imshow(szmap)
                plt.colorbar().set_label(['Jy'])
                plt.title('Clus Add Sziso : IB Model for %s' %
                          (maps[imap]['name']))
                plt.savefig(config.OUTPUT + 'add_sz/%s_ibmodel_%s_%s.png' %
                            (maps[imap]['name'], maps[imap]['band'], isim))
                plt.clf()
                naxis = szmap.shape
                szmap = szmap.flatten()

            nu = 3e5 / clus_get_lambdas((maps[imap]['band']))

            if int(isim) == 0:  # dI only needs to be calculated once...
                dI, errmsg = clus_get_relsz(isim,
                                            nu,
                                            imap,
                                            y=yin,
                                            te=tin,
                                            vpec=0.0)  # dI = [MJy/sr]
                np.save(
                    config.OUTPUT + 'add_sz/sim_dI_%s.npy' %
                    (maps[imap]['band']), dI)
            else:
                dI = np.load(config.OUTPUT + 'add_sz/sim_dI_%s.npy' %
                             (maps[imap]['band']))

            if errmsg:
                if verbose:
                    new_errmsg = 'Clus_get_relSZ exited with error' + errmsg
                return None, new_errmsg

            # Combine the spectral shape of the SZ effect, and combine with the peak intensity
            # converted to Jy/beam  ***Confirm these Units***
            if int(isim) == 0:
                szin = [x * dI / maps[imap]['calfac'] for x in szmap]
                final_dI.append(dI / maps[imap]['calfac'])
            else:
                szin = [x * dI / maps[imap]['calfac'] for x in szmap]
                final_dI.append(dI / maps[imap]['calfac'])

            szin = np.reshape(szin, (naxis[0], naxis[1]))

            # Have to interpolate to SPIRE map size
            hdu = fits.PrimaryHDU(szin, temphead)
            hdx = fits.PrimaryHDU(maps[imap]['signal'], maps[imap]['shead'])

            if testflag == 0:
                szinp = hcongrid(hdu.data, hdu.header, hdx.header)
                # need to smooth output with SPIRE PSF
                fwhm = maps[imap]['widtha']
                pixscale = maps[imap]['pixsize']
                retext = round(fwhm * 5.0 / pixscale)
                if retext % 2 == 0:
                    retext += 1
                bmsigma = fwhm / math.sqrt(8 * math.log(2))
                beam = Gaussian2DKernel(bmsigma / pixscale,
                                        x_size=retext,
                                        y_size=retext,
                                        mode='oversample',
                                        factor=1)
                beam *= 1.0 / beam.array.max()
                out_map = convolve(szinp, beam, boundary='wrap')

                # Combine the original signal with the sz effect
                maps[imap]['signal'] = maps[imap]['signal'] + out_map
            else:
                # maps[imap]['signal'] = maps[imap]['signal'] + szin
                '''THIS IS JUST FOR TESTING WITH SZ SIGNAL'''
                maps[imap]['signal'] = maps[imap]['error'] + szin

            # Used to check the alligned sz effect image
            if saveplot:
                filename = config.OUTPUT + 'sim_sz/' + maps[imap][
                    'name'] + '_sze_' + maps[imap]['band'] + '_' + str(
                        isim) + '.png'
                plt.imshow(maps[imap]['signal'])
                plt.title('Clus Add Sziso : SZE + Signal Map %s' %
                          (maps[imap]['band']))
                plt.colorbar().set_label('[Jy]')
                plt.savefig(filename)
                plt.clf()

    return maps, None, final_dI
示例#7
0
def clus_subtract_xcomps(maps,
                         sgen=None,
                         verbose=1,
                         superplot=1,
                         nsim=0,
                         saveplot=1):

    err = None
    ncols = len(maps)
    #check to see if the maps are in the correct order.
    if maps[0]['band'] != 'PSW':
        err = 'first element of map structure is not PSW, aborting.'
        if verbose:
            print(err)
        return None, err

    #make a noise mask and populate source removed map with nans where mask is 1 so they don't effect the fit.
    for i in range(len(maps)):
        # mask = clus_make_noise_mask(maps, i)
        #maps[i]['mask'] = maps[i]['mask'] + mask
        if sgen == None:
            maps[i]['mask'] = fits.getdata(config.HOME +
                                           'bolocam_mask_%s.fits' %
                                           (maps[i]['band']))
        for j in range(maps[i]['signal'].shape[0]):
            for k in range(maps[i]['signal'].shape[1]):
                if maps[i]['mask'][j, k] == 0:
                    maps[i]['srcrm'][j, k] = np.nan

        if saveplot:
            if sgen != None:
                filename = config.OUTPUT + 'pcat_residuals/' + maps[i][
                    'name'] + '_mask_resid_' + maps[i]['band'] + '_' + str(
                        nsim) + '.png'
            else:
                filename = config.OUTPUT + 'pcat_residuals/' + maps[i][
                    'name'] + '_mask_resid_' + maps[i][
                        'band'] + '_real' + '.png'
            plt.imshow(maps[i]['srcrm'])
            plt.clim([-0.01, 0.01])
            plt.colorbar().set_label('[Jy]')
            plt.title('Clus Subtract Xcomps : Masked PCAT Residual %s' %
                      (maps[i]['band']))
            plt.savefig(filename)
            plt.clf()
            ''' For making hists for Jack '''
            hda = fits.PrimaryHDU(maps[i]['srcrm'], maps[i]['shead'])
            hda.writeto(config.OUTPUT + 'pcat_residuals/' + maps[i]['name'] +
                        '_mask_resid_' + maps[i]['band'] + '_' + str(nsim) +
                        '.fits',
                        overwrite=True)

    for i in range(1, ncols):
        if verbose:
            print('On band %s' % (maps[i]['band']))

        #create a new image for the PSW band that is the same shape and beamsize as the reference images
        width = sqrt(maps[i]['widtha']**2 -
                     maps[0]['widtha']**2) / maps[0]['pixsize']
        kern = makeGaussian(15, 15, fwhm=width, center=None)
        kern = kern / np.sum(kern)
        kern = padGaussian(maps[0]['srcrm'], kern)
        inmap = convolve_fft(maps[0]['srcrm'], kern)

        inmap = maps[0]['srcrm']
        maps[0]['xclean'] = maps[0]['srcrm']
        xmap = inmap
        xmap_align = hcongrid(xmap, maps[0]['shead'], maps[i]['shead'])

        # plt.title('HCONGRID')
        # plt.imshow(xmap_align)
        # plt.show()

        plt.imshow(xmap_align)
        plt.clim(0, 0.08)
        plt.colorbar()
        plt.savefig('/home/vaughan/dumb_folder/hcongrid.png')
        plt.clf()
        hda = fits.PrimaryHDU(xmap_align, maps[i]['shead'])
        hda.writeto('/home/vaughan/hcongrid.fits', overwrite=True)

        interp_map = interp_band_to_band(maps[1], maps[0])
        plt.imshow(interp_map)
        plt.clim(0, 0.08)
        plt.colorbar()
        plt.savefig('/home/vaughan/dumb_folder/interp.png')
        plt.clf()
        hda = fits.PrimaryHDU(interp_map, maps[i]['shead'])
        hda.writeto('/home/vaughan/interp.fits', overwrite=True)

        exit()

        # plt.title('CUSTOM INTERP')
        # plt.imshow(interp_map)
        # plt.show()

        # plt.imshow(xmap_align)
        # plt.colorbar()
        # plt.clim(-0.03,0.04)
        # plt.savefig('xmap_align_%s.png' %(maps[i]['band']))
        # plt.clf()

        for j in range(xmap_align.shape[0]):
            for k in range(xmap_align.shape[1]):
                if np.isnan(xmap_align[j, k]) or np.isnan(maps[i]['srcrm'][j,
                                                                           k]):
                    xmap_align[j, k] = 0
                    maps[i]['srcrm'][j, k] = 0

        #now that we have our new PSW image flatten both images
        PSW_array = xmap_align.flatten()
        ref_array = maps[i]['srcrm'].flatten()

        #find the linear fit for PSW vs ref
        slope, intercept, r_value, p_value, std_err = linregress(
            PSW_array, ref_array)
        y = slope * PSW_array + intercept

        if superplot or saveplot:
            plt.plot(PSW_array, ref_array, 'x', label='Raw Intensities')
            plt.plot(PSW_array, y, c='red', label='Linear Regression Fit')
            plt.legend()
            plt.title('Clus Subtract Xcomps : PSW vs. %s' % (maps[i]['band']))
            plt.xlabel('PSW [Jy]')
            plt.ylabel('%s [Jy]' % (maps[i]['band']))

            if superplot:
                plt.show()
            elif saveplot:
                if sgen != None:
                    filename = config.OUTPUT + 'corr_comps/' + maps[i][
                        'name'] + '_xcomps_' + maps[i]['band'] + '_' + str(
                            nsim) + '.png'
                else:
                    filename = config.OUTPUT + 'corr_comps/' + maps[i][
                        'name'] + '_xcomps_' + maps[i]['band'] + '_real.png'

                plt.savefig(filename)
                plt.clf()

        #subtract the correlated components from the image
        maps[i]['xclean'] = np.empty(maps[i]['srcrm'].shape)
        for j in range(maps[i]['xclean'].shape[0]):
            for k in range(maps[i]['xclean'].shape[1]):
                if maps[i]['srcrm'][j, k] == 0:
                    maps[i]['xclean'][j, k] = np.nan
                else:
                    maps[i]['xclean'][j, k] = maps[i]['srcrm'][
                        j, k] - slope * xmap_align[j, k] + intercept

        plt.imshow(maps[i]['xclean'])
        plt.clim([-0.01, 0.01])
        plt.colorbar().set_label('[Jy]')
        plt.title('Clus Subtract Xcomp : Correlated Components Removed')

        if superplot:
            plt.show()
        elif saveplot:
            if sgen != None:
                filename = config.OUTPUT + 'corr_comps/' + maps[i][
                    'name'] + '_xclean_' + maps[i]['band'] + '_' + str(
                        nsim) + '.png'
            else:
                filename = config.OUTPUT + 'corr_comps/' + maps[i][
                    'name'] + '_xclean_' + maps[i]['band'] + '_real.png'

            plt.savefig(filename)
            plt.clf()

    #subtract the mean of the new map from itself. Why do we do this? we need to figure out what the purpose of this step is.
    # for i in range(maps[0]['xclean'].shape[0]):
    #     for j in range(maps[0]['xclean'].shape[1]):
    #         maps[0]['xclean'][i,j] = maps[0]['xclean'][i,j] - np.mean(maps[0]['xclean'])

    return maps, err
示例#8
0

####input parameters####
infile  = sys.argv[1]       # input
tfile   = sys.argv[2]       # target header
outname = sys.argv[3]       # output
reso    = float(sys.argv[4])# in arcsec

#############################
#### Main code ##############
#############################
hd      = fits.getheader(infile) 
dat     = fits.getdata(infile)
projhd  = fits.getheader(tfile)


cornel = np.sqrt(reso**2.-(hd['BMAJ']*3600.)**2.)
sigma = cornel/(hd['CDELT2']*3600.)/(2.*np.sqrt(2.*np.log(2.)))

### Gaussian smoothing the image ######
gimage=ndimage.filters.gaussian_filter(dat, sigma, order=0, output=None, mode='reflect', cval=0.0)

### project image  ####
new_image = hcongrid(gimage, hd, projhd)

fits.writeto(outname, new_image, header=projhd,overwrite=True)




示例#9
0
def clus_add_sziso_new(maps,
                       isim,
                       yin=0,
                       tin=0,
                       params=None,
                       verbose=0,
                       testflag=0,
                       saveplot=0,
                       clusname=None):

    #------------------------------------- This part is just for testing
    yin = 9.65 * 1e-4
    tin = 10.88

    errmsg = False

    # Now the bolocam data is in the SPIRE format
    # we can do a loop over the map adding in the false sz signal

    mapsize = len(maps)

    if verbose:
        print('Fetching BOLOCAM maps')
    ''' This is just until Jack gives us more new Bolocam templates... '''
    if clusname == 'rxj1347':
        data_file = CLUSDATA + 'bolocam/new/' + maps[0]['name'] + '.fits'
        cluster_struct = fits.getdata(data_file)
        data = fits.open(data_file)
        header = data[0].header
        naxis = cluster_struct.shape
        # BCAMNORM in units of MJy/sr
        print(header['BCAMNORM'], 'bolo cam norm')
        bolocam, err = clus_convert_bolocam(cluster_struct,
                                            norm=header['BCAMNORM'],
                                            verbose=verbose,
                                            clusname=clusname)

    else:
        #fetch bolocam data from .sav files in bolocam directory.
        data_file = str('bolocam/data/' + maps[0]['name'] + '.sav')
        data_dict = scipy.io.readsav(CLUSDATA + data_file, python_dict=True)
        cluster_struct = list(data_dict.values())

        bolocam, err = clus_convert_bolocam(cluster_struct,
                                            verbose=verbose,
                                            clusname=clusname)
        if err:
            errmsg = str('clus_convert_bolocam exited with error: ' + err)
            if verbose:
                print(errmsg)
            return None, errmsg

        # Set the size of the image for later use
        naxis = bolocam[0]['deconvolved_image'][0].shape

    naxis = np.array(naxis)
    sziso = fits.PrimaryHDU()
    temphead = sziso.header

    if clusname == 'rxj1347':
        # Setting the tempheader for the bolocam data
        temphead = header

    else:
        # Set reference pizel position
        crpix1 = int(naxis[0] / 2)
        crpix2 = int(naxis[1] / 2)

        # Setting the tempheader for the bolocam data
        temphead.set(
            'CRVAL1', bolocam[0]['deconvolved_image_ra_j2000_deg'][0][crpix1,
                                                                      crpix2])
        temphead.set(
            'CRVAL2', bolocam[0]['deconvolved_image_dec_j2000_deg'][0][crpix1,
                                                                       crpix2])
        temphead.set('CRPIX1', crpix1)
        temphead.set('CRPIX2', crpix2)
        temphead.set(
            'CD1_1',
            -bolocam[0]['deconvolved_image_resolution_arcmin'][0] / 60.0)
        temphead.set('CD1_2', 0)
        temphead.set('CD2_1', 0)
        temphead.set(
            'CD2_2',
            bolocam[0]['deconvolved_image_resolution_arcmin'][0] / 60.0)
        temphead.set('EPOCH', 2000)
        temphead.set('EQUINOX', 2000)
        temphead.set('CTYPE1', 'RA---TAN')
        temphead.set('CTYPE2', 'DEC--TAN')

    # 14.5  = full size of the bolocam image in pixels ?
    x = np.arange(naxis[0]) - 14.5
    y = np.arange(naxis[1]) - 14.5

    rad = np.zeros((naxis[0], naxis[1]))

    for ix in range(naxis[1]):
        rad[:, ix] = (np.tile(x[ix], naxis[0])**2 + y**2)**(1 / 2)

    # Outer marks the indices of the outside of the circle used to add in the sz effect
    n = 0
    outer = []
    for i in range(len(rad)):
        for j in range(len(rad)):
            if rad[i, j] > 10:
                outer.append(n)
            n += 1

    # Set up the spectral shape of the sz effect to be appled to the 500um map
    if testflag == 0:
        if clusname == 'rxj1347':
            szmap1 = bolocam
        else:
            szmap1 = -1 * bolocam[0]['deconvolved_image'][
                0]  # -1 is needed because SZ amp flips below 217 GHz
            szmap1 = (np.array(szmap1)).flatten()
            sz_mean = np.mean(szmap1[outer])
            szmap2 = [x - sz_mean for x in szmap1]
            sz_max = max(szmap2)
            szmap = [x / sz_max for x in szmap2]

    final_dI = []
    if clusname == 'rxj1347':  # need to calculate the dI for Bolocam using new maps
        # if os.path.isfile(config.HOME + 'lookup/rxj1347_bol_lookup.npy') : # use lookup file instead of running szpack every time
        #     dI_bolo = np.load(config.HOME + 'lookup/rxj1347_bol_lookup.npy')
        # else :
        szmap1 = bolocam
        dI_bolo, thisx, JofXout, xout, errmsg = clus_get_relsz(
            isim,
            3e5 / clus_get_lambdas('BOLOCAM'),
            'BOLOCAM',
            y=yin,
            te=tin,
            vpec=0.0)  # dI = [MJy/sr]
        # np.save(config.HOME + 'lookup/rxj1347_bol_lookup.npy',dI_bolo)
        szmap1 = np.array(szmap1).flatten()
        szmap1 = [x / dI_bolo for x in szmap1]

    for imap in range(mapsize):
        # Applying the effect to the 500 um and 350 um bands.
        # if imap == 2 or imap == 1:
        if testflag == 1:
            szmap, err = IB_model(maps[imap], params, verbose)
            szmap = np.array(szmap)
            plt.imshow(szmap, origin=0)
            plt.colorbar().set_label(['Jy'])
            plt.title('Clus Add Sziso : IB Model for %s' %
                      (maps[imap]['name']))
            plt.savefig(config.OUTPUT + 'add_sz/%s_ibmodel_%s_%s.png' %
                        (maps[imap]['name'], maps[imap]['band'], isim))
            plt.clf()
            naxis = szmap.shape
            szmap = szmap.flatten()

        nu = 3e5 / clus_get_lambdas((maps[imap]['band']))

        if int(isim) == 0:  # dI only needs to be calculated once...
            dI, thisx, JofXout, xout, errmsg = clus_get_relsz(
                isim, nu, imap, y=yin, te=tin, vpec=0.0)  # dI = [MJy/sr]
            np.save(config.OUTPUT + 'add_sz/sim_dI_%s.npy' %
                    (maps[imap]['band']),
                    dI)  #just the expected amplitude at spire wavelength
            np.save(config.OUTPUT + 'add_sz/input_dI_%s.npy' %
                    (maps[imap]['band']), dI * -.94 /
                    dI_bolo)  #input amplitude saved for comparison in fit_sz
        else:
            dI = np.load(config.OUTPUT + 'add_sz/sim_dI_%s.npy' %
                         (maps[imap]['band']))

        if errmsg:
            if verbose:
                new_errmsg = 'Clus_get_relSZ exited with error' + errmsg
            return None, new_errmsg

        # Combine the spectral shape of the SZ effect, and combine with the peak intensity
        # converted to Jy/beam

        hdtemp = fits.PrimaryHDU(szmap1, temphead)
        hdtemp.writeto(config.OUTPUT +
                       'sz_fit/bolo_fit_template%s.fits' % isim,
                       overwrite=True)

        szin = [x * dI / maps[imap]['calfac'] for x in szmap1]  #
        final_dI.append(dI / maps[imap]['calfac'])
        szin = np.reshape(szin, (naxis[0], naxis[1]))

        # Have to interpolate to SPIRE map size
        hdu = fits.PrimaryHDU(szin, temphead)
        hdx = fits.PrimaryHDU(maps[imap]['signal'], maps[imap]['shead'])

        if testflag == 0:
            szinp = hcongrid(hdu.data, hdu.header, hdx.header)
            # need to smooth output with SPIRE PSF
            fwhm = maps[imap]['widtha']
            pixscale = maps[imap]['pixsize']
            retext = round(fwhm * 5.0 / pixscale)
            if retext % 2 == 0:
                retext += 1
            bmsigma = fwhm / math.sqrt(8 * math.log(2))
            beam = Gaussian2DKernel(bmsigma / pixscale,
                                    x_size=retext,
                                    y_size=retext,
                                    mode='oversample',
                                    factor=1)
            beam *= 1.0 / beam.array.max()
            out_map = convolve(szinp, beam, boundary='wrap')

            # Combine the original signal with the sz effect
            maps[imap]['signal'] = maps[imap]['signal'] + out_map
            '''THIS IS JUST FOR TESTING WITH SZ SIGNAL'''
            # maps[imap]['signal'] = out_map
        elif testflag == 2:

            szinp = hcongrid(hdu.data, hdu.header, hdx.header)
            # need to smooth output with SPIRE PSF
            fwhm = maps[imap]['widtha']
            pixscale = maps[imap]['pixsize']
            retext = round(fwhm * 5.0 / pixscale)
            if retext % 2 == 0:
                retext += 1
            bmsigma = fwhm / math.sqrt(8 * math.log(2))
            beam = np.asarray(
                Gaussian2DKernel(bmsigma / pixscale,
                                 x_size=retext,
                                 y_size=retext,
                                 mode='oversample',
                                 factor=1))
            n_beam = beam / np.sum(
                beam
            )  #the input bolocam map is a surface brightness so we need to normalize over area
            out_map = convolve(szinp, n_beam, boundary='wrap')

            maps[imap]['signal'] = maps[imap]['signal'] + out_map
            # maps[imap]['srcrm'] = out_map #for testing
            # maps[imap]['signal'] = maps[imap]['error'] + out_map

        # Used to check the alligned sz effect image
        if saveplot:
            filename = config.OUTPUT + 'sim_sz/' + maps[imap][
                'name'] + '_sze+signal_' + maps[imap]['band'] + '_' + str(
                    isim) + '.png'
            plt.imshow(maps[imap]['signal'], origin=0)
            plt.title('Clus Add Sziso : SZE + Signal Map %s' %
                      (maps[imap]['band']))
            plt.colorbar().set_label('[Jy]')
            plt.clim(-0.1, 0.1)
            plt.savefig(filename)
            plt.clf()

            filename = config.OUTPUT + 'sim_sz/' + maps[imap][
                'name'] + '_sze_' + maps[imap]['band'] + '_' + str(
                    isim) + '.png'
            plt.imshow(out_map, origin=0)
            plt.title('Clus Add Sziso : SZE + Signal Map %s' %
                      (maps[imap]['band']))
            plt.clim(-0.1, 0.1)
            plt.colorbar().set_label('[Jy]')
            plt.savefig(filename)
            plt.clf()

            savefile = config.OUTPUT + 'sim_sz/' + maps[imap][
                'name'] + '_sze_' + maps[imap]['band'] + '_' + str(
                    isim) + '.fits'
            hda = fits.PrimaryHDU(maps[imap]['signal'], maps[imap]['shead'])
            hda.writeto(savefile, overwrite=True)

    return maps, None, final_dI
示例#10
0
def smooth_images_toresolution(target_resolution,
                               globs=["destripe*P[LMS]W*fits",
                                      "destripe*blue*fits",
                                      "destripe*red*fits"],
                               reject_regex='smooth|smregrid', verbose=True,
                               skip_existing=True, regrid=True,
                               target_header=None, regrid_order=1,
                               clobber=False, **kwargs):
    """
    Smooth a series of images to the same resolution.  The output files will be
    of the form ``{inputfilename}_smooth.fits`` and
    ``{inputfilename}_smregrid.fits``

    Parameters
    ----------
    target_resolution : `~astropy.units.quantity.Quantity`
        A degree-equivalent value that specifies the beam size in the output
        image
    globs : list
        A list of strings to pass into `~glob.glob`.  All files found will be
        smoothed and possibly regridded.
    reject_regex : str
        A regular expression to apply to each discovered file to choose whether
        to reject it.  For example, if you've run this function once, you'll
        have files named ``file.fits`` and ``file_smooth.fits`` that you don't
        want to re-smooth and re-regrid.
    verbose : bool
        Print messages at each step?
    skip_existing : bool
        If the output smooth file is found and this is True, skip and move on
        to the next
    regrid : bool
        Regrid the file?  If True, ``target_header`` is also required
    regrid_order : int
        The order of the regridding operation.  Regridding is performed with
        interpolation, so 0'th order means nearest-neighbor and 1st order means
        bilinear.  Regridding is done with
        `FITS_tools.hcongrid.hcongrid`
    clobber : bool
        Overwrite files if they exist?
    kwargs : dict
        Passed to `smooth_image_toresolution`

    Raises
    ------
    ValueError
        If ``target_header`` is not specified but ``regrid`` is

    Returns
    -------
    Nothing.  All output is to disk
    """

    for fn in [x for g in globs for x in glob.glob(g)
               if not re.search(reject_regex, x)]:
        if verbose:
            print("Reading file {0}".format(fn),)

        outnum = int(target_resolution.to(u.arcsec).value)
        smoutfn = fn.replace(".fits", "_smooth{0:d}.fits".format(outnum))
        if os.path.exists(smoutfn):
            if skip_existing:
                if verbose:
                    print("Skipping {0}".format(fn))
                continue
        
        smhduL = smooth_image_toresolution(fn, smoutfn, target_resolution,
                                           verbose=verbose, clobber=clobber,
                                           **kwargs)
        smhdu = smhduL[0]

        if regrid:
            if target_header is None:
                raise ValueError("Must specify a target header if regridding.")
            newimage = hcongrid(smhdu.data, smhdu.header, target_header,
                                order=regrid_order)

            rgoutfn = fn.replace(".fits", "_smregrid{0:d}.fits".format(outnum))
            print("Regridding {0} to {1}".format(smoutfn, rgoutfn))
            newhdu = fits.PrimaryHDU(data=newimage, header=target_header)
            newhdu.writeto(rgoutfn, clobber=clobber)
示例#11
0
def clus_new_fitsz(maps, saveplot=0, nsim=None):

    band = ['PSW', 'PMW', 'PLW']
    fit = [0] * 3

    data_file = config.CLUSDATA + 'bolocam/new/' + maps[0]['name'] + '.fits'
    base_model = fits.getdata(data_file)

    data = fits.open(data_file)
    header = data[0].header

    ylims = [[-0.01, 0.01], [0, 0.1], [0.1, 0.4]]
    for i in range(3):
        #Error Analysis -------
        error_map = maps[i]['error']
        error_test = np.multiply(error_map, maps[i]['calfac'])
        error = error_map.flatten()

        noise_map = maps[i]['noise']

        # plt.imshow(noise_map, origin='lower')
        # plt.title('Bolocam Template_%s' % maps[i]['band'])
        # plt.colorbar()
        # plt.savefig(config.OUTPUT + 'sz_fit/fitsz_in_temp%s%s.png' % (maps[i]['band'], nsim))
        # plt.clf()
        # noise_map = np.random.normal(loc=0, scale=1.0,size=(maps[i]['signal'].shape[0],maps[i]['signal'].shape[1]))
        noise = noise_map.flatten()

        for j in range(len(noise)):
            if not math.isnan(error[j]):  #Jy/beam
                noise[j] = maps[i]['calfac'] * noise[j]

        noise_map = np.reshape(noise, maps[i]['signal'].shape)

        bolo_temp = hcongrid(base_model, header, maps[i]['shead'])

        fwhm = maps[i]['widtha']
        pixscale = maps[i]['pixsize']
        retext = round(fwhm * 5.0 / pixscale)
        if retext % 2 == 0:
            retext += 1
        bmsigma = fwhm / math.sqrt(8 * math.log(2))
        beam = Gaussian2DKernel(bmsigma / pixscale,
                                x_size=retext,
                                y_size=retext,
                                mode='oversample',
                                factor=1)
        beam *= 1 / np.sum(beam.array)
        model = convolve(bolo_temp, beam, boundary='wrap')

        for j in range(maps[i]['signal'].shape[0]):
            for k in range(maps[i]['signal'].shape[1]):
                if maps[i]['mask'][j, k] == 0:
                    model[j, k] = np.nan
                    maps[i]['srcrm'][j, k] = np.nan
                    noise_map[j, k] = np.nan
                else:
                    maps[i]['srcrm'][j, k] *= maps[i]['calfac']

        image = maps[i]['srcrm']
        #
        # plt.imshow(maps[i]['srcrm'], origin='lower')
        # plt.title('SZ without noise_ %s' % maps[i]['band'])
        # plt.colorbar()
        # plt.savefig(config.OUTPUT + 'sz_fit/sz_no_noise%s%s.png' % (maps[i]['band'], nsim))
        # plt.clf()
        #
        # plt.imshow(noise_map, origin='lower')
        # plt.title('Noise Map_%s' % maps[i]['band'])K
        # plt.colorbar()
        # plt.savefig(config.OUTPUT + 'sz_fit/noise_map%s%s.png' % (maps[i]['band'], nsim))
        # plt.clf()

        plt.imshow(image, origin='lower', clim=(-0.1, 0.1))
        plt.title('Input SZ effect %s %s' % (maps[i]['band'], nsim))
        plt.colorbar()
        plt.savefig(config.OUTPUT + 'sz_fit/fitsz_in_map%s%s.png' %
                    (maps[i]['band'], nsim))
        plt.clf()

        plt.imshow(model, origin='lower')
        plt.title('Bolocam Template %s' % maps[i]['band'])
        plt.colorbar()
        plt.savefig(config.OUTPUT + 'sz_fit/fitsz_in_temp%s%s.png' %
                    (maps[i]['band'], nsim))
        plt.clf()

        flat_im = image.flatten()
        flat_mod = model.flatten()

        x_data = [
            flat_mod[i] for i in range(len(flat_mod)) if not np.isnan(noise[i])
        ]
        y_data = [
            flat_im[i] for i in range(len(flat_im)) if not np.isnan(noise[i])
        ]
        # noise_data = [1 / n for n in noise_map.flatten() if not np.isnan(n)] #polyfit wants weights as 1 / sigma
        sigma_data = [n for n in noise if not np.isnan(n)]

        print(len(x_data), len(y_data), len(sigma_data))
        # z, cov = np.polyfit(x_data,y_data,1, cov=True),# w = noise_data)

        params, cov = curve_fit(fitting_func, x_data, y_data, sigma=sigma_data)
        e = np.sqrt(np.diag(cov))
        # p = np.poly1d(z)
        intercept = params[1]
        intercept_error = e[1]
        slope = params[0]
        slope_error = e[0]
        y_fit = [slope * x + intercept for x in x_data]
        fit[i] = params
        chi_square = chi_square_test(y_data, y_fit, sigma_data)
        red_chi_square = chi_square / len(y_data)

        if saveplot:
            input_DI = np.load(config.OUTPUT + 'add_sz/input_dI_%s.npy' %
                               (band[i]))
            # plt.ylim(ylims[i])
            plt.plot(
                x_data,
                y_fit,
                c='red',
                label=
                'dI = %.4E +/- %.3E \n offset = %.1E +/- %.1E \n chi square: %.3E \n red chi square %.3E'
                % (slope, slope_error, intercept, intercept_error, chi_square,
                   red_chi_square))
            plt.scatter(x_data,
                        y_data,
                        alpha=0.5,
                        label='Image Data: Input DI : %.4E' % input_DI)
            in_y = [input_DI * x for x in x_data]
            # plt.plot(x_data, in_y, label='input dI = %.4E' % input_DI, c='green')

            sigma = (slope - input_DI) / slope_error
            print('uncertainty in fits', slope_error, intercept_error)
            print('Slope is %.2E sigma from the expected value' % sigma)

            #these next few lines is for testing only

            plt.legend()
            plt.xlabel('Model Flux [Unitless]')
            plt.ylabel('Image Flux [MJy/Sr]')
            plt.title('SZ Fit for %s %s' % (maps[i]['band'], nsim))
            # plt.xlim(-0.005,0.005)
            plt.savefig(
                config.OUTPUT + 'sz_fit/rxj1347_%s_%s.png' %
                (maps[i]['band'], nsim))  #rxj1347 placeholder for clusname.
            plt.clf()

    return fit