Example #1
0
 def __init__(self, resolution=1):
     self.visible_sky = True
     self.nside = None
     self.pixidx = None
     self.resolution = resolution
     self._is_visible = np.ones(nside2npix(self.nside), dtype=bool)
     self.skymap = np.zeros(nside2npix(self.nside))
Example #2
0
def healpix_bins(nside, rs, phis, thetas):
    npix = pf.nside2npix(nside)
    ipix = pf.ang2pix(nside, thetas, phis)
    r_bins = [[] for _ in xrange(npix)]
    for (r, i) in zip(rs, ipix):
        r_bins[i].append(r)
    return r_bins
Example #3
0
    def __init__(self,
            resolution: u.Quantity = 1*u.deg,
            time: Time = Time.now(),
            frequency: u.Quantity = 50*u.MHz,
            polarization: np.ndarray = np.array([0]),
            value: Union[float, np.ndarray] = 0.,
            observer: EarthLocation = nenufar_position
        ):

        if hpx is None:
            log.error(
                f"Unable to create an instance of {self.__qualname__} since 'healpy' does not work."
            )

        self.nside, self.resolution = self._resol2nside(resolution=resolution)

        # Construct the Healpix coordinates map
        ra, dec = hpx.pix2ang(
            nside=self.nside,
            ipix=np.arange(
                hpx.nside2npix(self.nside),
                dtype=np.int64
            ),
            lonlat=True,
            nest=False
        )

        super().__init__(
            coordinates=SkyCoord(ra, dec, unit="deg"),
            time=time,
            frequency=frequency,
            polarization=polarization,
            value=value,
            observer=observer
        )
Example #4
0
def healpix_bins(nside, rs, phis, thetas):
    npix = pf.nside2npix(nside)
    ipix = pf.ang2pix(nside, thetas, phis)
    r_bins = [[] for _ in xrange(npix)]
    for (r, i) in zip(rs, ipix):
        r_bins[i].append(r)
    return r_bins
def smoothing(m, fwhm, nest=True):
    if fwhm <= 0:
        return m
    nside = hpf.npix2nside(len(m))
    if nest:
        return hps.smoothing(m[hpf.ring2nest(nside, np.arange(hpf.nside2npix(nside)))], fwhm=fwhm)[hpf.nest2ring(nside, np.arange(hpf.nside2npix(nside)))]
    else:
        return hps.smoothing(m, fwhm=fwhm)
Example #6
0
    def horizon_mask(self, zenith, radius=45):
        """ Mask the healpix map pixels that are below a given
            radius from the local zenith.

            :param zenith:
                Array of local zenith converted in Galactic
                coordinates. This might typically be
                :attr:`SphUVW.zen_gal`
            :type zenith: `np.ndarray`
            :param radius:
                Radius from zenith, below which mask pixels
            :type radius: float
        """
        if radius == 0:
            return np.ones(nside2npix(self._hpsize), dtype='bool')
        # Theta coordinates of the healpix map
        theta, phi = pix2ang(self._hpsize,
                             np.arange(nside2npix(self._hpsize)),
                             lonlat=True)
        # Convert theta to longitudes
        t_mask = theta > np.pi
        theta[t_mask] = (2. * np.pi - theta[t_mask]) * -1.
        pix_coord = np.deg2rad(np.vstack([theta, phi])[..., np.newaxis])
        # Galactiv coordinates
        gal_l = np.array([z.l.deg for z in zenith])
        gal_b = np.array([z.b.deg for z in zenith])
        gal_coord = np.deg2rad(np.vstack([gal_l, gal_b])[:, np.newaxis])
        # Difference between two sets of angles
        lon1, lon2 = gal_coord[0], pix_coord[0]
        lat1, lat2 = gal_coord[1], pix_coord[1]
        c1 = np.cos(lat1)
        c2 = np.cos(lat2)
        s1 = np.sin(lat1)
        s2 = np.sin(lat2)
        cd = np.cos(lon2 - lon1)
        sd = np.sin(lon2 - lon1)
        u = np.square(c2 * sd) + np.square(c1 * s2 - s1 * c2 * cd)
        d = s1 * s2 + c1 + c2 * cd
        dist = np.arctan2(u.flatten(), d.flatten()).reshape(u.shape)

        # Mask evrything below horizon, i.e. keep if raidus < 45deg
        return ~np.any(np.rad2deg(dist) < radius, axis=1)
Example #7
0
 def _get_nside(self):
     """ Compute the nearest HEALPix nside given an angular
         resolution
     """
     orders = np.arange(30)
     nsides = order2nside(orders)
     resols = Angle(angle=nside2resol(nsides, arcmin=True), unit=u.arcmin)
     order_idx = np.argmin(np.abs(resols - self._resolution))
     self.nside = nsides[order_idx]
     self.pixidx = np.arange(nside2npix(self.nside))
     return
Example #8
0
def Setdistance(nside):
    """Returns a table of distance between each HEALPix pixel in radians.
"""
    NPIX = hppf.nside2npix(nside)
    print 'Array', NPIX, 'x', NPIX
    nparr_dist = np.zeros((NPIX, NPIX))
    for (ipix, npar_pix) in enumerate(nparr_dist):
        for (jpix, rad_pix) in enumerate(npar_pix):
            rad_pix = hp.rotator.angdist(hppf.pix2ang(nside, ipix), hppf.pix2ang(nside, jpix))
            nparr_dist[ipix][jpix] = rad_pix
    np.save('PixelsDistance_{0}.npy'.format(nside), nparr_dist)
    return nparr_dist # In radians
def find_pointon_healpxs(ra_poi, dec_poi, ang_cut, nhpside=512):
    NPIX = hppf.nside2npix(nhpside)
#    MAX_PIX_DEG = degrees(hppf.max_pixrad(nhpside))
    RADEC_POI = [radians(ra_poi), radians(dec_poi)]
    VEC_POI = hppf.ang2vec(pi/2.-RADEC_POI[1], RADEC_POI[0])
    ar_pix_incl = []
    ang_cut_rad = radians(ang_cut)
    for ipix in range(NPIX):
        vec = hppf.pix2vec(nhpside, ipix)
        ang_dist = get_ang_dist_vectors(VEC_POI, vec)
        if ang_dist<ang_cut_rad:
            ar_pix_incl.append(ipix)
    return ar_pix_incl
def merge_map(maps, nside=None, nest=True, verbose=False, renormalize=False):
    if nside is None:
        nside = 4096
        for m in maps:
            nside = min(nside, hpf.npix2nside(len(m)))



    filled_mask = np.zeros(hpf.nside2npix(nside), dtype=bool)
    result = np.zeros(hpf.nside2npix(nside), dtype=maps[0].dtype)

    for m in maps:
        m = ud_grade(m, nside, nest=nest)
        #valid in m
        valid_mask = ~(np.isnan(m)|np.isinf(m))

        #pixels to be taken from m, earlier m takes priority and will not be over-written
        fill_mask = valid_mask&(~filled_mask)
        if verbose:
            print "%.1f%% valid"%(100. * np.sum(valid_mask) / len(valid_mask)),
            print "%.1f%% to be filled"%(100. * np.sum(fill_mask) / len(fill_mask))

        if renormalize:
            overlap_mask = valid_mask&filled_mask
            if overlap_mask.any():
                factor = m[overlap_mask].dot(result[overlap_mask]) / m[overlap_mask].dot(m[overlap_mask])
                if verbose:
                    print "renormalizing by ", factor
                m *= factor

        #fill pixel and mask
        result[fill_mask] = m[fill_mask]
        filled_mask[fill_mask] = True
    result[~filled_mask] = np.nan

    return result
def ud_grade(m, nside, nest=True):
    if nside != hpf.npix2nside(len(m)):
        if nest:
            order_in = 'NESTED'
        else:
            order_in = 'RING'

        bad_mask = (np.isnan(m) | np.isinf(m) | (m == 0))
        if bad_mask.all():
            return np.zeros(hpf.nside2npix(nside)) * np.nan
        bad_mask = hpf.ud_grade(bad_mask.astype('float'), nside, order_in=order_in, pess=True) > 0

        result = hpf.ud_grade(m, nside, order_in=order_in, pess=True)
        result[bad_mask] = np.nan
        return result
    return m
Example #12
0
def find_galoff_healpxs(nhpside=16, threshold_flux50=0, path_catalogue_excl='/nfs/farm/g/glast/u/mtakahas/data/catalogue/gll_psch_v09.fit'):
    """Returns a list of HEALPiX pixles which satisfy your conditions as OFF region of Galactic ridge. The coordinate system is RADEC.
"""
    NPIX = hppf.nside2npix(nhpside)
    MAX_PIX_DEG = degrees(hppf.max_pixrad(nhpside))
    ar_pix_incl = []
    for ipix in range(NPIX):
        theta, phi = hppf.pix2ang(nhpside, ipix)
        radec_rad = [phi, pi/2.-theta] #[pi/2.-theta, phi]
        radec_deg = [degrees(radec_rad[0]), degrees(radec_rad[1])]
        gal_deg = convert_radec_to_galactic(radec_deg)
        if abs(gal_deg[1])>=50 and 90<=gal_deg[0]<270:
            ar_ang_dist = calcAngDist_Catalogue(radec_deg, path_catalogue_excl, threshold_flux50)
            for di in ar_ang_dist:
                if di['ANG_DIST']<=MAX_PIX_DEG:
                    break
            else:
                ar_pix_incl.append(ipix)
    return ar_pix_incl
Example #13
0
def load_hp(fpath='cmb/dat/mask/mask_40pc.fits', view=False, printit=False):
    extension = fpath.split('.')[-1]

    if extension == 'fits':
        from astropy.io import fits

        ##   Survey files of the form:  https://lambda.gsfc.nasa.gov/toolbox/footprint/configfile.cfm
        T = Table.read(fpath)

        ORDER = T.meta['ORDERING']
        NROWS = len(T)

        NSIDE = T.meta['NSIDE']
        NPIX = nside2npix(NSIDE)

        count = 0.0

        mask = []

        for i in np.arange(NROWS):
            mask += [element for element in T[i][0]]

        mask = np.array(mask)

    else:
        mask = np.loadtxt(fpath)

        NPIX = len(mask)
        NSIDE = npix2nside(NPIX)

    print('Loaded healpy mask: %s as %s (NPIX:  %d, \t NSIDE:  %d)' %
          (fpath.split('/')[-1], extension, NPIX, NSIDE))

    if printit:
        print('Min:  %s;  Max:  %s' % (mask.min(), mask.max()))

    ##  Element check.
    mask[mask > 1.0] = 1.0
    mask[mask < 0.0] = 0.0

    return mask
sys.exit(0)
#clean
bright_points = {'cyg':{'ra': '19:59:28.3', 'dec': '40:44:02'}, 'cas':{'ra': '23:23:26', 'dec': '58:48:00'}}
pt_source_range = PI / 40
smooth_scale = PI / 30
pt_source_neighborhood_range = [smooth_scale, PI / 9]
bright_pt_mask = np.zeros(Ashape1, dtype=bool)
bright_pt_neighborhood_mask = np.zeros(Ashape1, dtype=bool)
for source in bright_points.keys():
    bright_points[source]['body'] = ephem.FixedBody()
    bright_points[source]['body']._ra = bright_points[source]['ra']
    bright_points[source]['body']._dec = bright_points[source]['dec']
    theta = PI / 2 - bright_points[source]['body']._dec
    phi = bright_points[source]['body']._ra
    pt_coord = np.array([np.sin(theta) * np.cos(phi), np.sin(theta) * np.sin(phi), np.cos(theta)])
    sky_vecs = np.array(hpf.pix2vec(nside_start, np.arange(hpf.nside2npix(nside_start)), nest=True))[:, valid_pix_mask]
    bright_pt_mask = bright_pt_mask | (la.norm(sky_vecs - pt_coord[:, None], axis=0) < pt_source_range)
    bright_pt_neighborhood_mask = bright_pt_neighborhood_mask | (la.norm(sky_vecs - pt_coord[:, None], axis=0) >= pt_source_neighborhood_range[0])
    bright_pt_neighborhood_mask = bright_pt_neighborhood_mask | (la.norm(sky_vecs - pt_coord[:, None], axis=0) <= pt_source_neighborhood_range[1])

raw_psf = psf[:, bright_pt_mask]
cleaned_result2 = np.copy(result[bright_pt_mask])
cleaned_accumulate = np.zeros_like(cleaned_result2)
# clean_stop = 10**3.5 * np.median(sizes)#2 * np.min(np.abs(cleaned_result2))
step_size = 0.02
while np.max(np.abs(cleaned_result2)) > np.median(np.abs(cleaned_result2)) * 1.1:
    clean_pix = np.argmax(np.abs(cleaned_result2))
    cleaned_accumulate[clean_pix] += step_size * cleaned_result2[clean_pix]
    cleaned_result2 -= step_size * cleaned_result2[clean_pix] * raw_psf[bright_pt_mask, clean_pix]
cleaned_result2 = result - raw_psf.dot(cleaned_accumulate)
plot_IQU(result, 'Simulated Dirty Map (Log(K))', 1, shape=(2, 1), coord='cg')
    data[data <= 0] = np.nan
    return unit_scale * data

plot_individual = False

DEGREE = PI / 180.
ARCMIN = DEGREE / 60.
###########################
###########################
###OVER ALL PARAMETERS
###########################
###########################
version = 3.0
I_only = True
mother_nside = 1024#64
mother_npix = hpf.nside2npix(mother_nside)
target_fwhm = 24 * ARCMIN#14 * ARCMIN#5 * DEGREE#3.6 * DEGREE#
min_edge_width = 3 * DEGREE
remove_cmb = True

#it's a pretty bad crime to not diffretiate file name based on what data set are included...
data_file_name = '/mnt/data0/omniscope/polarized foregrounds/data_nside_%i_smooth_%.2E_edge_%.2E_rmvcmb_%i_UV%i_v%.1f.npz'%(mother_nside, target_fwhm, min_edge_width, remove_cmb, not I_only, version)


if os.path.isfile(data_file_name):
    raise Exception(data_file_name + " already exist.")
else:
    ns = mother_nside * 2
    while ns <= 4096:
        larger_file_names = ['/mnt/data0/omniscope/polarized foregrounds/data_nside_%i_smooth_%.2E_edge_%.2E_rmvcmb_%i_UV%i_v%.1f.npz'%(ns, target_fwhm, min_edge_width, remove_cmb, not I_only, version)]
        if I_only:
Example #16
0
def pixel_assign(fnames,
                 nside,
                 border_check=False,
                 simple_ebv=True,
                 multithread=False):
    import time
    import aux_func
    import numpy as np
    import healpy as hp
    import pandas as pd
    import multiprocessing as mp
    import healpy.pixelfunc as pf

    ####################
    # Reading data in
    ####################
    t0 = time.time()
    print('Reading Schlegel map')
    if simple_ebv:
        ebv_map = [np.linspace(0, 0.2, pf.nside2npix(n)) for n in nside]
    else:
        ebv_map = [aux_func.sfd_map(percent=False, resample=n) for n in nside]

    print('Reading galaxy data for pixelisation')
    if multithread:
        pool = mp.Pool(processes=min(multithread, len(fnames)))
        temp = [
            pool.apply_async(aux_func.pix_id, (
                fnames[j],
                nside,
                ebv_map,
                j,
            )) for j in range(len(fnames))
        ]
        temp = [t.get() for t in temp]
    else:
        temp = [
            aux_func.pix_id(fnames[j], nside, ebv_map, j)
            for j in range(len(fnames))
        ]

    res = temp[0][0]
    pix_ids = [t[1] for t in temp]
    pix_ids = [
        np.concatenate([p[i] for p in pix_ids]) for i in range(len(nside))
    ]

    ####################
    # Border check
    ####################
    if border_check:
        print('Checking borders')
        if multithread:
            pool = mp.Pool(processes=min(multithread, len(fnames)))
            temp = [
                pool.apply_async(aux_func.find_border, (
                    f,
                    pix_ids,
                    nside,
                    res,
                )) for f in fnames
            ]
            temp = [t.get() for t in temp]
        else:
            temp = [
                aux_func.find_border(f, pix_ids, nside, res) for f in fnames
            ]

    t1 = time.time()
    print(f'Pixelisation finished in {t1-t0} s')
plt.subplot(1, 2, 2)
spectral_index_list = np.array(spectral_index_list)
plt.errorbar((spectral_index_list[:, 0] - 5)%24 + 5, spectral_index_list[:, 1], fmt='g+', yerr=spectral_index_list[:, 2])
plt.xlabel('LST (hour)')
plt.ylabel('Spectral index')
plt.ylim(-4, -1)
plt.show()
print np.sum(spectral_index_list[:, 1] / spectral_index_list[:,2]**2) / np.sum(1 / spectral_index_list[:,2]**2), np.sum(1 / spectral_index_list[:,2]**2)**-.5

#gal latitudes
equ2013_to_gal_matrix = hp.rotator.Rotator(coord='cg').mat.dot(sv.epoch_transmatrix(2000, stdtime=2013.58))
zen_phis = np.arange(16., 25., .1) / 24. * 2 * np.pi
zen_thetas = np.ones_like(zen_phis) * 45.297728 / 180 * PI
z_ang0, z_ang1 = hp.rotator.rotateDirection(equ2013_to_gal_matrix, zen_thetas, zen_phis)
zen_gal_lats = hpf.get_interp_val(PI/2 - hpf.pix2ang(nside, range(hpf.nside2npix(nside)))[0], z_ang0, z_ang1)

plt.errorbar((spectral_index_list[:, 0] - 5)%24 + 5, spectral_index_list[:, 1], fmt='g+', yerr=spectral_index_list[:, 2])
plt.xlabel('LST (hour)')
plt.ylabel('Spectral index')
plt.ylim(-4, -1)
plt.plot(zen_phis / (2 * np.pi) * 24, zen_gal_lats * 180. / np.pi / 40. - 3)
plt.show()


##############################################
###make pretty maps with heavy regularization
AtNiA_sum = np.fromfile(AtNiA_filename, dtype='float64')
AtNiA_sum.shape = (npix_nonzero, npix_nonzero)
for reg in 10.**np.arange(-4.5, -2, .5):
    AtNiAi_filename = datadirs['miteor'] + 'mega_v%.1f_AtNiAfi_n%i_iter%i_reg%.3e'%(version, npix_nonzero, max_iter - 1, reg) + file_tag
    cool_cmap = cm.hsv
    cool_cmap.set_under("w")  # sets background to white
    hpv.mollview(pangle, min=-90, max=90, coord=plotcoordtmp, title=title, nest=True, sub=(4, ncol, 2 * ncol + col),
                 cmap=cool_cmap)

    hpv.mollview(np.arcsinh(V) / np.log(10), min=-np.arcsinh(10. ** 4) / np.log(10),
                 max=np.arcsinh(10. ** 4) / np.log(10), coord=plotcoordtmp, title=title, nest=True,
                 sub=(4, ncol, 3 * ncol + col))
    if col == ncol:
        plt.show()

for coord in ['C', 'CG']:
    plot_IQU(fake_solution, 'GSM gridded', 1, coord=coord)
    plot_IQU(w_GSM, 'wienered GSM', 2, coord=coord)
    plot_IQU(w_sim_sol, 'wienered simulated solution', 3, coord=coord)
    plot_IQU(w_solution, 'wienered solution', 4, coord=coord)
    sol_iquv = sol2map(w_solution).reshape((4, hpf.nside2npix(nside_standard)))
    hpv.mollview(np.arcsinh(sol_iquv[1]/2.) * np.log10(np.e), nest=True, sub=(4, 6, 12), min=-4, max=4, coord=coord)
    hpv.mollview(np.arcsinh(sol_iquv[2]/2.) * np.log10(np.e), nest=True, sub=(4, 6, 18), min=-4, max=4, coord=coord)
    hpv.mollview(np.arcsinh(sol_iquv[3]/2.) * np.log10(np.e), nest=True, sub=(4, 6, 24), min=-4, max=4, coord=coord)
    plt.show()


# hpv.mollview(np.log10(fake_solution[np.array(final_index).tolist()]), min= 0, max =4, coord=plotcoord, title='GSM gridded', nest=True)
# hpv.mollview(np.log10((x/sizes)[np.array(final_index).tolist()]), min=0, max=4, coord=plotcoord, title='raw solution, chi^2=%.2f'%chisq, nest=True)
# hpv.mollview(np.log10((sim_x/sizes)[np.array(final_index).tolist()]), min=0, max=4, coord=plotcoord, title='raw simulated solution, chi^2=%.2f'%chisq_sim, nest=True)
# hpv.mollview(np.log10((w_GSM/sizes)[np.array(final_index).tolist()]), min=0, max=4, coord=plotcoord, title='wienered GSM', nest=True)
# hpv.mollview(np.log10((w_solution/sizes)[np.array(final_index).tolist()]), min=0, max=4, coord=plotcoord, title='wienered solution', nest=True)
# hpv.mollview(np.log10((w_sim_sol/sizes)[np.array(final_index).tolist()]), min=0, max=4, coord=plotcoord, title='wienered simulated solution', nest=True)
# plt.show()
    result[~filled_mask] = np.nan

    return result


plot_individual = False


###########################
###########################
###OVER ALL PARAMETERS
###########################
###########################
extract_nside = 512
mother_nside = 32
mother_npix = hpf.nside2npix(mother_nside)
smoothing_fwhm = 10. * np.pi / 180.
edge_width = 10. * np.pi / 180.
remove_cmb = True

dataset = {}
###1.4GHz###
stockert = fit.read("/home/omniscope/data/polarized foregrounds/stocker_villa_elisa.bin")[0]
stockert[:, :-1] = np.roll(stockert[:, :-1], 720, axis=1)[:, ::-1]
stockert[:, -1] = stockert[:, 0]
stockert = equirectangular2heapix(stockert, extract_nside)

reich_q = equirectangular2heapix(fit.read("/home/omniscope/data/polarized foregrounds/allsky.q.lb.fits")[0], extract_nside, data_x=(np.pi+np.arange(np.pi * 2 + 1e-9, 0, -np.pi/720))%(np.pi*2+1e-9))
reich_u = -equirectangular2heapix(fit.read("/home/omniscope/data/polarized foregrounds/allsky.u.lb.fits")[0], extract_nside, data_x=(np.pi+np.arange(np.pi * 2 + 1e-9, 0, -np.pi/720))%(np.pi*2+1e-9))#reich is in IAU convention whose U is minus sign the CMB healpix convention

dataset[1.42] = np.array([
Example #20
0
erplot = er #EnergyLogRegion(4, 4.75, 0.25)
cthr = EnergyLogRegion(10, 0.0, 0.1)
aaStrSelect = cfg.aaStrSelect
aStrSelect = cfg.aStrSelect

htgPerf = CutPerformanceHtg(listPathFilePerf)
fileOut.cd()

rOffMax=[15., 15.]#[1.7, 12.]
rAppa=20#rOffMax[1]
rOnMax=[0.45, psfCalOnly]
rOffMin=[2.*rOnMax[0], 2.*rOnMax[1]]

# For HEALPix plots
NHPSIDE = int(par[2]) #8
NHPPIX = hppf.nside2npix(NHPSIDE)
print 'HEALPix plots: ', NHPPIX, 'pixels'
print 'HEALPix resolution:', degrees(hppf.nside2resol(NHPSIDE)), 'deg'

# Target objects
aTgt = []
aTrs = []
print "Target list: "
print "*", "Galactic ridge" 
ridge = GalacticRidge("GalacticRidge", config=cfg, perf=htgPerf, eRegion=er, ePlotRegion=erplot, nhpside=NHPSIDE)#, tStart=484477130.219, tEnd=484487130.219)
limb = EarthLimb("EarthLimb", config=cfg, perf=htgPerf, eRegion=er, ePlotRegion=erplot)
#aInnerGal = [InnerGalaxy("InnerGalaxyR16", 16, config=cfg, perf=htgPerf, eRegion=er, ePlotRegion=erplot), InnerGalaxy("InnerGalaxyR41", 41, config=cfg, perf=htgPerf, eRegion=er, ePlotRegion=erplot)]
aInnerGal = [ InnerGalaxy("InnerGalaxyR41", 41, config=cfg, perf=htgPerf, eRegion=er, ePlotRegion=erplot)]
for obj in pCandleCatalogue.aObjectDict:
    aTgt.append(PointSource(obj["Name"], obj["RA"], obj["DEC"], obj["L"], obj["B"], obj["Z"], rAppa, rOffMax, rOffMin, rOnMax, cfg, er, erplot, htgPerf, nhpside=NHPSIDE))
for obj in pTransientCatalogue.aObjectDict:
            sys.stdout.flush()
            if abs(dec - lat_degree * np.pi / 180) <= np.pi / 2:
                A[p][:, i] = vs.calculate_pointsource_visibility(ra, dec, ubls, freq, beam_heal_equ=beam_heal_equ, tlist=tlist).flatten()

        print "%f minutes used" % (float(time.time() - timer) / 60.)
        sys.stdout.flush()
        A[p].tofile(A_path)

####################################################
###beam weights using an equal pixel A matrix######
#################################################
print "Computing beam weight...",
sys.stdout.flush()
beam_weight = ((la.norm(A['x'], axis=0) ** 2 + la.norm(A['y'], axis=0) ** 2) ** .5)[hpf.nest2ring(nside_beamweight, range(12 * nside_beamweight ** 2))]
beam_weight = beam_weight / np.mean(beam_weight)
thetas_standard, phis_standard = hpf.pix2ang(nside_standard, range(hpf.nside2npix(nside_standard)), nest=True)
beam_weight = hpf.get_interp_val(beam_weight, thetas_standard, phis_standard, nest=True) #np.array([beam_weight for i in range(nside_standard ** 2 / nside_beamweight ** 2)]).transpose().flatten()
print "done."
sys.stdout.flush()

################################################
#####################GSM###########################
#############################################
pca1 = hp.fitsfunc.read_map(script_dir + '/../data/gsm1.fits' + str(nside_standard))
pca2 = hp.fitsfunc.read_map(script_dir + '/../data/gsm2.fits' + str(nside_standard))
pca3 = hp.fitsfunc.read_map(script_dir + '/../data/gsm3.fits' + str(nside_standard))
components = np.loadtxt(script_dir + '/../data/components.dat')
scale_loglog = si.interp1d(np.log(components[:, 0]), np.log(components[:, 1]))
w1 = si.interp1d(components[:, 0], components[:, 2])
w2 = si.interp1d(components[:, 0], components[:, 3])
w3 = si.interp1d(components[:, 0], components[:, 4])
        print "\r%.1f%% completed" % (100. * float(i) / (12. * nside_beamweight ** 2)),
        sys.stdout.flush()
        if abs(dec - lat_degree * PI / 180) <= PI / 2:
            A[p][:, i] = vs.calculate_pointsource_visibility(ra, dec, ubls, freq, beam_heal_equ=beam_heal_equ, tlist=tlist).flatten()

    print "%f minutes used" % (float(time.time() - timer) / 60.)
    sys.stdout.flush()

####################################################
###beam weights using an equal pixel A matrix######
#################################################
print "Computing beam weight...",
sys.stdout.flush()
beam_weight = ((la.norm(A['x'], axis=0) ** 2 + la.norm(A['y'], axis=0) ** 2) ** .5)[hpf.nest2ring(nside_beamweight, range(12 * nside_beamweight ** 2))]
beam_weight = beam_weight / np.mean(beam_weight)
thetas_standard, phis_standard = hpf.pix2ang(nside_standard, range(hpf.nside2npix(nside_standard)), nest=True)
beam_weight = hpf.get_interp_val(beam_weight, thetas_standard, phis_standard, nest=True) #np.array([beam_weight for i in range(nside_standard ** 2 / nside_beamweight ** 2)]).transpose().flatten()
print "done."
sys.stdout.flush()

################################################
#####################GSM###########################
#############################################
pca1 = hp.fitsfunc.read_map(script_dir + '/../data/gsm1.fits' + str(nside_standard))
pca2 = hp.fitsfunc.read_map(script_dir + '/../data/gsm2.fits' + str(nside_standard))
pca3 = hp.fitsfunc.read_map(script_dir + '/../data/gsm3.fits' + str(nside_standard))
components = np.loadtxt(script_dir + '/../data/components.dat')
scale_loglog = si.interp1d(np.log(components[:, 0]), np.log(components[:, 1]))
w1 = si.interp1d(components[:, 0], components[:, 2])
w2 = si.interp1d(components[:, 0], components[:, 3])
w3 = si.interp1d(components[:, 0], components[:, 4])
Example #23
0
if __name__ == "__main__":
    file_dir = sys.argv[1]
    halo_id = int(sys.argv[2])

    rs_fname = path.join(file_dir, "%d_caustic.txt" % halo_id)
    x_plane_fname = path.join(file_dir, "%dh_x_caustic.txt" % halo_id)
    y_plane_fname = path.join(file_dir, "%dh_y_caustic.txt" % halo_id)
    z_plane_fname = path.join(file_dir, "%dh_z_caustic.txt" % halo_id)

    xs, ys, zs = read_pts(rs_fname)
    rs = np.sqrt(xs**2 + ys**2 + zs**2)
    vecs = np.array(zip(xs, ys, zs))
    thetas, phis = pf.vec2ang(vecs)

    nside = 2
    npix = pf.nside2npix(nside)

    r_bins = healpix_bins(nside, rs, phis, thetas)
    h = mode_radius(rs) / 5
    max_r = np.max(rs)

    cs = ["Red", "Blue",
          "Green", "Black",
          "DarkOrange", "Magenta",
          "Cyan", "BlueViolet",
          "DarkGoldenRod", "DarkRed"]

    plot_bins = random.sample(zip(np.arange(len(r_bins)), r_bins), 8)

    pretty_fig(3)
    for i, (ipix, bin) in enumerate(plot_bins):
    def model(self, TP_HTG_KING, HTG2_LIVETIME, NHPSIDE=256, THRESHOLD_ANGDIST=15, tpl_path_perf=('/nfs/farm/g/glast/u/mtakahas/v20r09p09_G1haB1/S18/S18V200909_020RAWE20ZDIR020ZCS000wwoTRKwoMCZDIR00woRWcatTwo_15/S18ZDIR020catTwoZDIR060_E28binx_Cth40bins_CalOnly_R100_perf.root', '/nfs/farm/g/glast/u/mtakahas/v20r09p09_G1haB1/S18/S18V200909_020RAWE20ZDIR020ZCS000wwoTRKwoMCZDIR00woRWcatTwo_15/S18ZDIR020catTwoZDIR060_E28binx_Cth40bins_CalOnly_R30_perf.root', '/nfs/farm/g/glast/u/mtakahas/v20r09p09_G1haB1/S18/S18V200909_020RAWE20ZDIR020ZCS000wwoTRKwoMCZDIR00woRWcatTwo_15/S18ZDIR020catTwoZDIR060_E28binx_Cth40bins_CalOnly_R10_perf.root'), aeff_regular=None):
        """Model the point source with the PSF of King function. Energy dispersion is ignored currently.
        """
        PIX_TRUE = hppf.ang2pix(NHPSIDE, pi/2.-self.DEC_RAD, self.RA_RAD) # #Pixel the true position of the source locates in
        TP_DIR_TRUE = (pi/2.-self.DEC_RAD, self.RA_RAD)
        print 'NSIDE:', NHPSIDE
        NPIX = hppf.nside2npix(NHPSIDE)
        print NPIX, 'pixels.'
        print 'Pixel resolution:', degrees(hppf.nside2resol(NHPSIDE)), 'deg'
        sa_pix = hppf.nside2pixarea(NHPSIDE) # Solid angle of a pixel [sr]
        print 'Solid angle of each pixel:', sa_pix, 'sr'
        coo_src = SkyCoord(self.RA, self.DEC, unit="deg") # True coordinate of the source
        THRESHOLD_ANGDIST_RADIANS = radians(THRESHOLD_ANGDIST)

        self.dctt_htg_sed_model_enr[NHPSIDE] = {}

        htg_aeff_regular = None
        if aeff_regular!=None:
            file_aeff_regular = ROOT.TFile(aeff_regular, 'READ')
            htg_aeff_regular = file_aeff_regular.Get('htgEaRegular')
            print htg_aeff_regular.GetName()

        # ROI
        set_pix_roi = set([])
        dict_angdist = {}
        for ipix in range(NPIX):
            tp_pix = hppf.pix2ang(NHPSIDE, ipix)
            dict_angdist[ipix] = hp.rotator.angdist(tp_pix, TP_DIR_TRUE) # Angular distance between the source and each pixel in radians
            if dict_angdist[ipix] < THRESHOLD_ANGDIST_RADIANS:
                set_pix_roi.add(ipix)

        print 'ROI pixels:', set_pix_roi

        HTG1_LT = HTG2_LIVETIME.ProjectionX("{0}_projTheta".format(HTG2_LIVETIME.GetName()), 1, HTG2_LIVETIME.GetYaxis().FindBin(self.ZENITH_CUT)-1)

        # Preparation of ON region setup
        dct_path_perf = {}
        dct_file_perf = {}
        dct_htg_psf95 = {}
        dct_htg_acc = {}
        # PSF
        fc_King_annulus = ROOT.TF1("fc_King_annulus", "TMath::Sin(x)*[0]*(1.-1./[2])*pow(1.+(x/[1])**2/2./[2],-[2])/[1]**2", 0, pi)
        fc_King = ROOT.TF1("fc_King", "[0]*(1.-1./[2])*pow(1.+(x/[1])**2/2./[2],-[2])/2./TMath::Pi()/[1]**2", 0, pi)

        for (icla,cla) in enumerate(self.TPL_STR_CLASS):
            print '======================='
            print cla
            print '======================='

            dct_path_perf[cla] = tpl_path_perf[icla]
            dct_file_perf[cla] = ROOT.TFile(dct_path_perf[cla], 'READ')
            dct_htg_psf95[cla] = dct_file_perf[cla].Get('psf_q95_hist')
            dct_htg_acc[cla] = dct_file_perf[cla].Get('acc_cth_hist')

            htg2_model = ROOT.TH2D("htg2_model_{0}_NSIDE{1}".format(cla, NHPSIDE), "Model of {0} {1} (NSIDE={2})".format(cla, self.NAME, NHPSIDE), NPIX, 0, NPIX, self.NBIN_ENERGY, self.EDGE_ENERGY_LOW, self.EDGE_ENERGY_UP)
            htg2_model_on = htg2_model.Clone('{0}_ON'.format(htg2_model.GetName()))
            htg_sed_model = ROOT.TH2D('htg_sed_model_{0}'.format(cla), 'SED model of {0} {1}'.format(cla, self.NAME), self.HTG_SED.GetNbinsX(), self.HTG_SED.GetXaxis().GetXmin(), self.HTG_SED.GetXaxis().GetXmax(), dct_htg_acc[cla].GetNbinsY(), dct_htg_acc[cla].GetYaxis().GetXmin(), dct_htg_acc[cla].GetYaxis().GetXmax())

            for iEne in range(1, self.HTG_SED.GetNbinsX()+1):
                print 'Energy:', self.HTG_SED.GetXaxis().GetBinLowEdge(iEne), '-', self.HTG_SED.GetXaxis().GetBinUpEdge(iEne)
                flux_true_itgl = self.HTG_SED.GetBinContent(iEne)
                fluxerr_true_itgl = self.HTG_SED.GetBinError(iEne)
                print '  Flux:', flux_true_itgl, '+/-', fluxerr_true_itgl, '[photons cm^-2 s^-1]'
                for iTh in range(1, HTG1_LT.GetNbinsX()+1):
                    scale_aeff = dct_htg_acc[cla].GetBinContent(dct_htg_acc[cla].GetXaxis().FindBin(self.HTG_SED.GetXaxis().GetBinCenter(iEne)), dct_htg_acc[cla].GetYaxis().FindBin(HTG1_LT.GetXaxis().GetBinCenter(iTh))) / (2*math.pi*dct_htg_acc[cla].GetYaxis().GetBinWidth(dct_htg_acc[cla].GetYaxis().FindBin(HTG1_LT.GetXaxis().GetBinCenter(iTh)))) # Effective area [m^2]
                    if htg_aeff_regular!=None:
                        #print '=-=-=-=-='
                        #print scale_aeff
                        scale_aeff = scale_aeff + htg_aeff_regular.GetBinContent(htg_aeff_regular.GetXaxis().FindBin(10**self.HTG_SED.GetXaxis().GetBinCenter(iEne)), htg_aeff_regular.GetYaxis().FindBin(HTG1_LT.GetXaxis().GetBinCenter(iTh)))
                        #print scale_aeff
                    scale_exp = scale_aeff * 100.**2 * HTG1_LT.GetBinContent(iTh) # Integrated exposure value for a certain energy and inclination angle [cm^2 s]
                    nphoton = flux_true_itgl*scale_exp
                    nphotonerr = fluxerr_true_itgl*scale_exp
                    htg_sed_model.Fill(htg_sed_model.GetXaxis().GetBinCenter(iEne), htg_sed_model.GetYaxis().GetBinCenter(iTh), nphoton)
                    kxbin = TP_HTG_KING[0].GetXaxis().FindBin(self.HTG_SED.GetXaxis().GetBinCenter(iEne))
                    kybin = TP_HTG_KING[0].GetYaxis().FindBin(HTG1_LT.GetBinCenter(iTh))
                    if nphoton>0 and kxbin>0 and kybin>0:
                        print '  cos(Inclination angle):', HTG1_LT.GetXaxis().GetBinLowEdge(iTh), '-', HTG1_LT.GetXaxis().GetBinUpEdge(iTh)
                        print '    Effective area:', scale_aeff, 'm^2'
                        print '    Exposure:', scale_exp, 'cm^2 s'
                        print '    Number of photons:', nphoton, '+/-', nphotonerr
                        print ''
                        for ipar in range(3): # Setting the parameters of King function
                        # PSF
                            par_value = TP_HTG_KING[ipar].GetBinContent(kxbin, kybin)
                            #print '    Parameter No.{0}:'.format(ipar), par_value
                            fc_King_annulus.FixParameter(ipar, par_value)
                            fc_King.FixParameter(ipar, par_value)
                        factor_norm = 1.0/fc_King_annulus.Integral(0, pi)
                        #print '    Normalization factor:', factor_norm
                        for ipix in set_pix_roi:
                            scale_psf = fc_King.Eval(dict_angdist[ipix])
                            htg2_model.Fill(ipix+0.5, htg2_model.GetYaxis().GetBinCenter(iEne), nphoton*scale_psf*factor_norm*sa_pix)
#                        for cla in self.TPL_STR_CLASS:
                            deg_psf95 = dct_htg_psf95[cla].GetBinContent(dct_htg_psf95[cla].FindBin(self.HTG_SED.GetBinCenter(iEne)))
                            if radians(min(15, deg_psf95))>dict_angdist[ipix]:
                                htg2_model_on.SetBinContent(ipix, iEne, 1)
                            else:
                                htg2_model_on.SetBinContent(ipix, iEne, 0)
                    if self.HTG_SED.GetBinContent(iEne)!=0:
                        htg_sed_model.SetBinError(iEne, iTh, self.HTG_SED.GetBinError(iEne)/self.HTG_SED.GetBinContent(iEne)*htg_sed_model.GetBinContent(iEne, iTh))
                    else:
                        htg_sed_model.SetBinError(iEne, iTh, 0)

            print htg2_model_on.Integral()
            htg2_model_on.Multiply(htg2_model)
            for ienr in range(1, htg2_model_on.GetYaxis().GetNbins()+1):
                for ipix in range(1, htg2_model_on.GetXaxis().GetNbins()+1):
                    content = htg2_model.GetBinContent(ipix, ienr)
                    content_err = htg2_model.GetBinError(ipix, ienr)
                    content_on = htg2_model_on.GetBinContent(ipix, ienr)
                    if content>0:
                        htg2_model_on.SetBinError(ipix, ienr, content_err*content_on/content)
                    else:
                        htg2_model_on.SetBinError(ipix, ienr, 0)
            print htg2_model_on.Integral()
            htg_sed_model_on = htg2_model_on.ProjectionY('{0}_ON'.format(htg_sed_model.GetName()))
            print 'ON photon number:', htg_sed_model_on.Integral()

            htg_sed_model_enr = htg_sed_model.ProjectionX('{0}_projEnergy'.format(htg_sed_model.GetName()))
            for ienr in range(1, htg_sed_model_on.GetXaxis().GetNbins()+1):
                content = htg_sed_model_enr.GetBinContent(ienr)
                content_err = htg_sed_model_enr.GetBinError(ienr)
                content_on = htg_sed_model_on.GetBinContent(ienr)
                if content>0:
                    htg_sed_model_on.SetBinError(ienr, content_err*content_on/content)
                else:
                    htg_sed_model_on.SetBinError(ienr, 0)

            print 'Making map...'
            arr_map = []
            arr_map_energy = []
            for iEne in range(0, self.HTG_SED.GetNbinsX()+1):
                arr_map.append([])
                if iEne>0:
                    arr_map_energy.append((self.HTG_SED.GetXaxis().GetBinLowEdge(iEne), self.HTG_SED.GetXaxis().GetBinUpEdge(iEne)))
                    for ipix in range(htg2_model.GetXaxis().GetNbins()):
                        if ipix in set_pix_roi:
                            arr_map[-1].append(htg2_model.GetBinContent(ipix+1, iEne))
                        else:
                            arr_map[-1].append(hppf.UNSEEN)
                else:
                    arr_map_energy.append((self.HTG_SED.GetXaxis().GetBinLowEdge(1), self.HTG_SED.GetXaxis().GetBinUpEdge(self.HTG_SED.GetNbinsX())))
                    for ipix in range(htg2_model.GetXaxis().GetNbins()):
                        if ipix in set_pix_roi:
                            arr_map[-1].append(htg2_model.Integral(ipix+1, ipix+1, 1, htg2_model.GetNbinsY()))
                        else:
                            arr_map[-1].append(hppf.UNSEEN)

                    print '  Energy:', arr_map_energy[-1][0], '-', arr_map_energy[-1][1]
                    nparr_map = np.array(arr_map[-1]) # Take this energy bin
                    hp.visufunc.cartview(nparr_map, iEne, self.tp_rotate, unit='cm^-2 s^-1 / {0:.1E} sr'.format(sa_pix), lonra=[-THRESHOLD_ANGDIST, THRESHOLD_ANGDIST], latra=[-THRESHOLD_ANGDIST, THRESHOLD_ANGDIST], title='{0} ({1:.3f} - {2:.3f} GeV)'.format(self.NAME, pow(10, arr_map_energy[-1][0]-3), pow(10, arr_map_energy[-1][1]-3)), min=0, flip='astro')
                    plt.savefig("png/Model-{0}_{1}_NSIDE{2}_{3}-{4}.png".format(self.NAME, cla, NHPSIDE, int(100*arr_map_energy[-1][0]+0.5), int(100*arr_map_energy[-1][1]+0.5)))
                    plt.close()
            
            self.FILE_OUT.cd()
            htg_sed_model.Write()
            htg_sed_model_enr.Write()
            htg2_model.Write()
            htg_sed_model_on.Write()
            self.dctt_htg_sed_model_enr[NHPSIDE][cla] = copy.copy(htg_sed_model_enr)
        self.HTG_SED.Write()
Example #25
0
def test(temp, file_path):
    import time
    import numpy as np
    import pandas as pd
    import healpy.pixelfunc as pf

    ####################
    # DF definition
    ####################
    filter_set = ['u', 'g', 'r', 'i', 'z']
    filter_set = [f'{fs}_ap'
                  for fs in filter_set] + [f'{fs}_ab' for fs in filter_set]
    zrange = [[0.0, 0.3], [0.3, 0.6], [0.6, 0.9], [0.9, 1.2], [1.2, 2.5]]

    ####################
    # Create galaxies
    ####################
    temp = np.linspace(22.8, 24.8, 1000)
    temp = {f: temp for f in filter_set}
    temp['id_galaxy'] = np.arange(1000)

    ####################
    # Position galaxies
    ####################
    '''A ring order is used for this set.'''
    pix_region = pf.get_all_neighbours(32,
                                       theta=90,
                                       phi=0,
                                       nest=False,
                                       lonlat=True)
    pix_region = np.concatenate([
        pix_region,
        np.array([pf.ang2pix(32, 90, 0, nest=False, lonlat=True)])
    ])
    pix_map = np.zeros(pf.nside2npix(32))
    pix_map[pix_region] = 1
    pix_map = pf.ud_grade(pix_map, 64, order_in='RING', order_out='RING')
    pix_region = np.arange(pf.nside2npix(64))[pix_map > 0]
    ra_region, dec_region = pf.pix2ang(64, pix_region, nest=False, lonlat=True)
    data = []
    for i in range(len(pix_region)):
        for j in range(len(zrange)):
            temp['ra'] = ra_region[i]
            temp['dec'] = dec_region[i]
            temp['zobs'] = (zrange[j][0] + zrange[j][1]) / 2
            data.append(pd.DataFrame(temp))
    data = pd.concat(data)
    big_pixels = pf.ang2pix(32,
                            data['ra'],
                            data['dec'],
                            nest=False,
                            lonlat=True)
    big_pixel_ID = np.unique(big_pixels)

    ####################
    # File save
    ####################
    fnames = []
    for pix in big_pixel_ID:
        fname = f'{file_path}galaxies_test_{pix}.csv'
        fnames.append(fname)
        data_subset = data.loc[big_pixels == pix]
        data_subset.to_csv(fname, index=False)
    print('Test data created')
    return (fnames)
Example #26
0
if __name__ == "__main__":
    file_dir = sys.argv[1]
    halo_id = int(sys.argv[2])

    rs_fname = path.join(file_dir, "%d_caustic.txt" % halo_id)
    x_plane_fname = path.join(file_dir, "%dh_x_caustic.txt" % halo_id)
    y_plane_fname = path.join(file_dir, "%dh_y_caustic.txt" % halo_id)
    z_plane_fname = path.join(file_dir, "%dh_z_caustic.txt" % halo_id)

    xs, ys, zs = read_pts(rs_fname)
    rs = np.sqrt(xs**2 + ys**2 + zs**2)
    vecs = np.array(zip(xs, ys, zs))
    thetas, phis = pf.vec2ang(vecs)

    nside = 2
    npix = pf.nside2npix(nside)

    r_bins = healpix_bins(nside, rs, phis, thetas)
    h = mode_radius(rs) / 5
    max_r = np.max(rs)

    cs = [
        "Red", "Blue", "Green", "Black", "DarkOrange", "Magenta", "Cyan",
        "BlueViolet", "DarkGoldenRod", "DarkRed"
    ]

    plot_bins = random.sample(zip(np.arange(len(r_bins)), r_bins), 8)

    pretty_fig(3)
    for i, (ipix, bin) in enumerate(plot_bins):
        plot_kde(ipix, bin, h, (0, max_r), c=cs[i])
Example #27
0
        fill_mask = np.array([not bool(int(c)) for c in bin(n_reg)[2:].zfill(n_incomplete)])[::-1]
        matching_mask = (region_sum == n_reg)
        if matching_mask.any():
            region_mask_list.append(matching_mask)
            region_indices_list.append(sorted(np.concatenate((incomplete_fs[fill_mask], complete_fs))))
    return region_indices_list, region_mask_list



###########################
###########################
###OVER ALL PARAMETERS
###########################
###########################
mother_nside = 32
mother_npix = hpf.nside2npix(mother_nside)
smoothing_fwhm = 10. * np.pi / 180.
edge_width = 10. * np.pi / 180.
# mask_name = 'herastrip'
# pixel_mask = (hpf.pix2ang(mother_nside, range(mother_npix), nest=True)[0] > np.pi/2 + np.pi/9) & (hpf.pix2ang(mother_nside, range(mother_npix), nest=True)[0] < np.pi/2 + np.pi/4.5)

mask_name = 'plane20deg'
pixel_mask = np.abs(hpf.pix2ang(mother_nside, range(mother_npix), nest=True)[0] - np.pi / 2) > np.pi / 9


step = .2
remove_cmb = True
show_plots = False

data_file_name = '/mnt/data0/omniscope/polarized foregrounds/data_nside_%i_smooth_%.2E_edge_%.2E_rmvcmb_%i.npz'%(mother_nside, smoothing_fwhm, edge_width, remove_cmb)
 def fwhm2(psf, verbose=False):
     spreaded = np.abs(psf) / np.max(np.abs(psf))
     fwhm_mask = spreaded >= .5
     return (np.sum(fwhm_mask) * 4 * PI / hpf.nside2npix(nside) / PI)**.5