Beispiel #1
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 equirectangular2heapix(data, nside, data_x=None, data_y=None, nest=True, manual_phi_correction=0):
    if data_x is None:
        delx = 2*np.pi / (data.shape[1] - 1)
        data_x = np.arange(0, 2*np.pi+delx/100., delx)
    if data_y is None:
        dely = np.pi / (data.shape[0] - 1)
        data_y = np.arange(np.pi, -dely/100., -dely)
    if data.shape != (len(data_y), len(data_x)):
        raise ValueError("Input shape mismatch between %s and (%i, %i)"%(data.shape, len(data_y), len(data_x)))
    inter_f = si.interp2d(sorted(data_x), sorted(data_y), data[np.ix_(np.argsort(data_y), np.argsort(data_x))])

    result = np.empty(12*nside**2, dtype=data.dtype)

    heal_thetas, heal_phis = hpf.pix2ang(nside, range(12*nside**2), nest=nest)
    unique_heal_thetas = np.unique(heal_thetas)

    for heal_theta in unique_heal_thetas:
        theta_mask = heal_thetas == heal_theta

        #doing some complicated juggling bc interp function automatically sort the list input and output according to that implicitly re-arranged inuput list
        qaz_phis = (heal_phis[theta_mask] + manual_phi_correction) % (np.pi*2)
        qaz = np.zeros_like(heal_phis[theta_mask])
        qaz[np.argsort(qaz_phis)] = inter_f(np.sort(qaz_phis), heal_theta).flatten()

        result[theta_mask] = qaz
    #     if np.abs(heal_theta - np.pi/2.) < 5*np.pi/180.:
    #         print np.isnan(qaz).all()
    # print np.isnan(data).all()
    # print data_x
    # print data_y
    return result
Beispiel #3
0
def get_fitting_region(order,pixel):
    """Expand tile by quarter of a pixel for fitting

    
    :param order: the HEALPix resolution level
    :param pixel: given HEALPix pixel that needs to be fit
    :return: HEALPix pixels that need to be fit
    """

    #define old and new order
    old_nside=2**order
    new_nside=2**(order+2)

    #get co-ord of main pixel
    theta,phi=pixelfunc.pix2ang(old_nside, pixel, nest=True)
    #define offsets such that main pixel is split into four sub pixels
    scale=pixelfunc.max_pixrad(old_nside)
    offset_theta=np.array([-0.125,0.0,0.125,0.0])*scale
    offset_phi=np.array([0.0,-0.125,0.0,0.125])*scale
    #convert co-ords to pixels at higher order
    pix_fit=pixelfunc.ang2pix(new_nside, theta+offset_theta, phi+offset_phi, nest=True)
    #get neighbouring pixels and remove duplicates
    moc_tile=MOC()
    pixels=np.unique(pixelfunc.get_all_neighbours(new_nside, pix_fit,nest=True))
    moc_tile.add(order+2,np.unique(pixelfunc.get_all_neighbours(new_nside, pixels,nest=True)))
    return moc_tile
Beispiel #4
0
def rotate_healpixmap(healpixmap, z1, y1, z2):#the three rotation angles are (fixed rotation axes and right hand convention): rotate around z axis by z1, around y axis by y1, and z axis again by z2. I think they form a set of Euler angles, but not exactly sure.
    nside = int((len(healpixmap)/12.)**.5)
    if len(healpixmap)%12 != 0 or 12*(nside**2) != len(healpixmap):
        raise Exception('ERROR: Input healpixmap length %i is not 12*nside**2!'%len(healpixmap))
    newmapcoords_in_oldcoords = [rotatez(rotatey(rotatez(hpf.pix2ang(nside, i), -z2), -y1), -z1) for i in range(12*nside**2)]
    newmap = [hpf.get_interp_val(healpixmap, coord[0], coord[1]) for coord in newmapcoords_in_oldcoords]
    return newmap
Beispiel #5
0
    def inv_projmap(self, img, nside=None):
        """Inverse projection of the projected map to a healpix spherical map.

        Input:
          - img: an array cantains the projected map.

        Return:
          - a 1D array contains the healpix spherical map.

        """
        ysize, xsize = img.shape

        if nside is None:
            lonra = self.arrayinfo['lonra']
            latra = self.arrayinfo['latra']
            npix = np.int(
                (360.0 * xsize / (lonra[1] - lonra[0])) *
                (180.0 * ysize / (latra[1] - latra[0])))  # the total pixel
            nside = 2**np.int(np.ceil(np.log2(np.sqrt(npix / 12.0)) - 1))

        npix = 12 * nside**2
        hpmap = np.zeros(npix, dtype=img.dtype)
        theta, phi = pixelfunc.pix2ang(
            nside,
            np.arange(npix))  # in radians, theta: [0, pi], phi: [0. 2pi]
        x = np.degrees(phi)
        x = -np.where(x > 180.0, x - 360.0, x)  # [-180.0, 180.0]
        y = -np.degrees(theta) + 90.0  # [-90.0, 90.0]
        for pix in np.arange(npix):
            i, j = self.xy2ij(x[pix], y[pix])
            if i is not None and j is not None:
                hpmap[pix] = img[i, j]

        return hpmap
def rotate_healpixmap(healpixmap, z1, y1, z2):#the three rotation angles are (fixed rotation axes and right hand convention): rotate around z axis by z1, around y axis by y1, and z axis again by z2. I think they form a set of Euler angles, but not exactly sure.
    nside = int((len(healpixmap)/12.)**.5)
    if len(healpixmap)%12 != 0 or 12*(nside**2) != len(healpixmap):
        raise Exception('ERROR: Input healpixmap length %i is not 12*nside**2!'%len(healpixmap))
    newmapcoords_in_oldcoords = [rotatez(rotatey(rotatez(hpf.pix2ang(nside, i), -z2), -y1), -z1) for i in range(12*nside**2)]
    newmap = [hpf.get_interp_val(healpixmap, coord[0], coord[1]) for coord in newmapcoords_in_oldcoords]
    return newmap
Beispiel #7
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
        )
Beispiel #8
0
    def inv_projmap(self, img, nside=None):
        """Inverse projection of the projected map to a healpix spherical map.

        Input:
          - img: an array cantains the projected map.

        Return:
          - a 1D array contains the healpix spherical map.

        """
        ysize, xsize = img.shape

        if nside is None:
            lonra = self.arrayinfo['lonra']
            latra = self.arrayinfo['latra']
            npix = np.int((360.0 * xsize / (lonra[1] - lonra[0])) * (180.0 * ysize / (latra[1] - latra[0]))) # the total pixel
            nside = 2**np.int(np.ceil(np.log2(np.sqrt(npix/12.0)) - 1))

        npix = 12 * nside**2
        hpmap = np.zeros(npix, dtype=img.dtype)
        theta, phi = pixelfunc.pix2ang(nside, np.arange(npix)) # in radians, theta: [0, pi], phi: [0. 2pi]
        x = np.degrees(phi)
        x = -np.where(x>180.0, x-360.0, x) # [-180.0, 180.0]
        y = -np.degrees(theta) + 90.0 # [-90.0, 90.0]
        for pix in np.arange(npix):
            i, j = self.xy2ij(x[pix], y[pix])
            if i is not None and j is not None:
                hpmap[pix] = img[i, j]

        return hpmap
Beispiel #9
0
def get_fitting_region(order, pixel):
    """Expand tile by quarter of a pixel for fitting

    
    :param order: the HEALPix resolution level
    :param pixel: given HEALPix pixel that needs to be fit
    :return: HEALPix pixels that need to be fit
    """

    #define old and new order
    old_nside = 2**order
    new_nside = 2**(order + 2)

    #get co-ord of main pixel
    theta, phi = pixelfunc.pix2ang(old_nside, pixel, nest=True)
    #define offsets such that main pixel is split into four sub pixels
    scale = pixelfunc.max_pixrad(old_nside)
    offset_theta = np.array([-0.125, 0.0, 0.125, 0.0]) * scale
    offset_phi = np.array([0.0, -0.125, 0.0, 0.125]) * scale
    #convert co-ords to pixels at higher order
    pix_fit = pixelfunc.ang2pix(new_nside,
                                theta + offset_theta,
                                phi + offset_phi,
                                nest=True)
    #get neighbouring pixels and remove duplicates
    moc_tile = MOC()
    pixels = np.unique(
        pixelfunc.get_all_neighbours(new_nside, pix_fit, nest=True))
    moc_tile.add(
        order + 2,
        np.unique(pixelfunc.get_all_neighbours(new_nside, pixels, nest=True)))
    return moc_tile
Beispiel #10
0
    def test_josh_gsm(self):
        self.result = {}
        colors = ['r', 'b', 'c', 'g']
        for self.nside, color in zip([32, 64, 128, 256], colors):
            nside = self.nside
            pca1 = hp.fitsfunc.read_map(self.test_dir + '/../data/gsm1.fits' +
                                        str(nside))
            pca2 = hp.fitsfunc.read_map(self.test_dir + '/../data/gsm2.fits' +
                                        str(nside))
            pca3 = hp.fitsfunc.read_map(self.test_dir + '/../data/gsm3.fits' +
                                        str(nside))
            gsm = 422.952 * (0.307706 * pca1 + -0.281772 * pca2 +
                             0.0123976 * pca3)
            equatorial_GSM = np.zeros(12 * nside**2, 'float')
            #rotate sky map
            for i in range(12 * nside**2):
                ang = hp.rotator.Rotator(coord='cg')(hpf.pix2ang(nside, i))
                equatorial_GSM[i] = hpf.get_interp_val(gsm, ang[0], ang[1])
            self.alm = sv.convert_healpy_alm(
                hp.sphtfunc.map2alm(equatorial_GSM), 3 * nside - 1)
            timer = time.time()
            self.result[nside] = self.vs.calculate_visibility(
                sv.expand_real_alm(self.alm),
                d=self.rot.dot(np.array([15.0, 21.0, 0.0])),
                freq=self.freq,
                nt=len(self.correct_result) - 1,
                L=3 * self.nside - 1,
                verbose=False)
            print(time.time() - timer) / 60., "minutes for nside = %i" % nside
            sys.stdout.flush()
            plt.plot(np.real(self.result[nside]), '%s--' % color)

        #plt.plot(np.real(self.correct_result), 'g--')
        #plt.plot(np.real(self.result32), 'r--', np.real(self.result64), 'b--', np.real(self.correct_result), 'g--')
        plt.show()
Beispiel #11
0
def healpix_to_skycoord(pix, nside=None):
    """
    given a healpix pix and the nside level of the healpix return the SkyCoord representing the centre of the field
    """
    if nside is None:
        nside = HEALPIX_NSIDE
    ra, dec = pixelfunc.pix2ang(nside, pix, lonlat=True)
    return SkyCoord(ra, dec, unit='degree')
def rotate_healpixmap(healpixmap, z1, y1, z2):#the three rotation angles are (fixed rotation axes and right hand convention): rotate around z axis by z1, around y axis by y1, and z axis again by z2. I think they form a set of Euler angles, but not exactly sure.
    nside = int((len(healpixmap)/12.)**.5)
    if len(healpixmap)%12 != 0 or 12*(nside**2) != len(healpixmap):
        raise Exception('ERROR: Input healpixmap length %i is not 12*nside**2!'%len(healpixmap))
    rot_matrix = rotatez_matrix(-z1).dot(rotatey_matrix(-y1).dot(rotatez_matrix(-z2)))
    new_coords = hpf.pix2ang(nside, range(12*nside**2))
    old_coords = hpr.rotateDirection(rot_matrix, new_coords)
    newmap = hpf.get_interp_val(healpixmap, old_coords[0], old_coords[1])
    return newmap
Beispiel #13
0
    def test_speed(self):
        self.test_dir = os.path.dirname(os.path.realpath(__file__)) + '/'
        self.blmequ = sv.read_real_alm(self.test_dir + 'bx125.195.bin')
        self.blmax = 5
        self.freq = 125.195#my correct answers are computed with c=300,so im rescaing freq here to compensate
        self.zenithequ = sv.ctos(np.fromfile(self.test_dir + 'zenith.bin', dtype = 'float32'))[1:]
        self.zenithequ[0] = np.pi/2 - self.zenithequ[0]
        self.zenithequ = np.array(self.zenithequ)[::-1]
        self.correct_result = np.loadtxt(self.test_dir + 'Revised_Location_Visibilties_for_21_m_south_21_m_east_0_m_up_xx_pol_125.195_MHz.dat')
        self.correct_result = self.correct_result[:-1, 1] + 1j * self.correct_result[:-1, 2]
        self.nside = 128

        self.vs = sv.Visibility_Simulator()
        self.vs.initial_zenith = self.zenithequ
        self.vs.Blm = sv.expand_real_alm(self.blmequ)
        self.rot = np.fromfile(self.test_dir + 'x5rot.bin', dtype = 'float32').reshape((3,3))

        nside = self.nside
        print "Reading fits...",
        sys.stdout.flush()
        pca1 = hp.fitsfunc.read_map(self.test_dir + '/../data/gsm1.fits' + str(nside))
        pca2 = hp.fitsfunc.read_map(self.test_dir + '/../data/gsm2.fits' + str(nside))
        pca3 = hp.fitsfunc.read_map(self.test_dir + '/../data/gsm3.fits' + str(nside))
        gsm = 422.952*(0.307706*pca1+-0.281772*pca2+0.0123976*pca3)
        print "Done reading"
        sys.stdout.flush()
        equatorial_GSM = np.zeros(12*nside**2,'float')

        #rotate sky map
        print "Rotating map...",
        sys.stdout.flush()
        for i in range(12*nside**2):
            ang = hp.rotator.Rotator(coord='cg')(hpf.pix2ang(nside,i))
            pixindex, weight = hpf.get_neighbours(nside,ang[0],ang[1])
            for pix in range(len(pixindex)):
                equatorial_GSM[i] += weight[pix]*gsm[pixindex[pix]]
        print "Done rotating"
        sys.stdout.flush()

        print "Creating map alm...",
        sys.stdout.flush()
        self.alm = sv.convert_healpy_alm(hp.sphtfunc.map2alm(equatorial_GSM), 3 * nside - 1)
        print "Done alm"
        sys.stdout.flush()

        print "Computing visibilities...",
        sys.stdout.flush()
        timer = time.time()
        self.result = self.vs.calculate_visibility(sv.expand_real_alm(self.alm), d=self.rot.dot(np.array([21.0,21.0,0.0])), freq=self.freq, nt=len(self.correct_result), L = 3*self.nside-1, verbose = True)
        print "done", (time.time() - timer)/60, 'min'
        sys.stdout.flush()
        #print len(self.result), np.argmax(np.real(self.result)) - np.argmax(np.real(self.correct_result)), np.argmax(np.imag(self.result)) - np.argmax(np.imag(self.correct_result))
        plt.plot(np.real(self.result), 'r--', np.real(self.correct_result), 'b--')
        plt.show()
        plt.plot(np.imag(self.result), 'r--', np.imag(self.correct_result), 'b--')
        plt.show()
Beispiel #14
0
 def _get_eq_coords(self):
     """ Compute the equatorial and horizontal coordinates
         for the HEALPix sky
     """
     ra, dec = pix2ang(nside=self.nside,
                       ipix=self.pixidx,
                       lonlat=True,
                       nest=False)
     ra *= u.deg
     dec *= u.deg
     self._eq_coords = SkyCoord(ra, dec, frame='icrs')
     return
Beispiel #15
0
def tile_in_tile(order_small,tile_small,order_large):
    """Routine to find our what larger tile to load data from when fitting smaller tiles.
     Returns larger tile no. Useful for when fitting segmented maps on HPC using a hierarchical segmentation scheme

    :param order_small: order of smaller tile
    :param tile_small:  tile no. of smaller tile
    :param order_large: order of larger tiling scheme
    :return: pixel number of larger tile
    """
    theta, phi =pixelfunc.pix2ang(2**order_small, tile_small, nest=True)
    ipix = pixelfunc.ang2pix(2**order_large, theta, phi, nest=True)
    return ipix
Beispiel #16
0
def tile_in_tile(order_small, tile_small, order_large):
    """Routine to find our what larger tile to load data from when fitting smaller tiles.
     Returns larger tile no. Useful for when fitting segmented maps on HPC using a hierarchical segmentation scheme

    :param order_small: order of smaller tile
    :param tile_small:  tile no. of smaller tile
    :param order_large: order of larger tiling scheme
    :return: pixel number of larger tile
    """
    theta, phi = pixelfunc.pix2ang(2**order_small, tile_small, nest=True)
    ipix = pixelfunc.ang2pix(2**order_large, theta, phi, nest=True)
    return ipix
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
Beispiel #18
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)
Beispiel #19
0
 def test_josh_gsm(self):
     self.nside = 32
     nside = self.nside
     pca1 = hp.fitsfunc.read_map(self.test_dir + '/../data/gsm1.fits' + str(nside))
     pca2 = hp.fitsfunc.read_map(self.test_dir + '/../data/gsm2.fits' + str(nside))
     pca3 = hp.fitsfunc.read_map(self.test_dir + '/../data/gsm3.fits' + str(nside))
     gsm = 422.952*(0.307706*pca1+-0.281772*pca2+0.0123976*pca3)
     equatorial_GSM = np.zeros(12*nside**2,'float')
     #rotate sky map
     for i in range(12*nside**2):
         ang = hp.rotator.Rotator(coord='cg')(hpf.pix2ang(nside,i))
         equatorial_GSM[i] = hpf.get_interp_val(gsm, ang[0], ang[1])
     self.alm = sv.convert_healpy_alm(hp.sphtfunc.map2alm(equatorial_GSM), 3 * nside - 1)
     self.result32 = self.vs.calculate_visibility(sv.expand_real_alm(self.alm), d=self.rot.dot(np.array([6.0,3.0,0.0])), freq=self.freq, nt=len(self.correct_result)-1, L = 3*self.nside-1, verbose = False)
     print self.result32[0], self.result32[-1]
     print self.correct_result[0], self.correct_result[-1]
     #self.nside = 64
     #nside = self.nside
     #pca1 = hp.fitsfunc.read_map(self.test_dir + '/../data/gsm1.fits' + str(nside))
     #pca2 = hp.fitsfunc.read_map(self.test_dir + '/../data/gsm2.fits' + str(nside))
     #pca3 = hp.fitsfunc.read_map(self.test_dir + '/../data/gsm3.fits' + str(nside))
     #gsm = 422.952*(0.307706*pca1+-0.281772*pca2+0.0123976*pca3)
     #equatorial_GSM = np.zeros(12*nside**2,'float')
     ##rotate sky map
     #for i in range(12*nside**2):
         #ang = hp.rotator.Rotator(coord='cg')(hpf.pix2ang(nside,i))
         #pixindex, weight = hpf.get_neighbours(nside,ang[0],ang[1])
         #for pix in range(len(pixindex)):
             #equatorial_GSM[i] += weight[pix]*gsm[pixindex[pix]]
     #self.alm = sv.convert_healpy_alm(hp.sphtfunc.map2alm(equatorial_GSM), 3 * nside - 1)
     #self.result64 = self.vs.calculate_visibility(sv.expand_real_alm(self.alm), d=self.rot.dot(np.array([6.0,3.0,0.0])), freq=self.freq, nt=len(self.correct_result)-1, L = 3*self.nside-1, verbose = False)
     #plt.plot(np.real(self.result32), 'r--', np.real(self.result64), 'b--', np.real(self.correct_result), 'g--')
     #plt.show()
     #plt.plot(np.imag(self.result32), 'r--', np.imag(self.result64), 'b--', np.imag(self.correct_result), 'g--')
     #plt.show()
     plt.plot(np.real(self.result32), 'r--', np.real(self.correct_result), 'g--')
     plt.show()
    def test_josh_gsm(self):
        self.result = {}
        colors = ['r','b','c','g']
        for self.nside, color in zip([32, 64, 128, 256], colors):
            nside = self.nside
            pca1 = hp.fitsfunc.read_map(self.test_dir + '/../data/gsm1.fits' + str(nside))
            pca2 = hp.fitsfunc.read_map(self.test_dir + '/../data/gsm2.fits' + str(nside))
            pca3 = hp.fitsfunc.read_map(self.test_dir + '/../data/gsm3.fits' + str(nside))
            gsm = 422.952*(0.307706*pca1+-0.281772*pca2+0.0123976*pca3)
            equatorial_GSM = np.zeros(12*nside**2,'float')
            #rotate sky map
            for i in range(12*nside**2):
                ang = hp.rotator.Rotator(coord='cg')(hpf.pix2ang(nside,i))
                equatorial_GSM[i] = hpf.get_interp_val(gsm, ang[0], ang[1])
            self.alm = sv.convert_healpy_alm(hp.sphtfunc.map2alm(equatorial_GSM), 3 * nside - 1)
            timer = time.time()
            self.result[nside] = self.vs.calculate_visibility(sv.expand_real_alm(self.alm), d=self.rot.dot(np.array([15.0,21.0,0.0])), freq=self.freq, nt=len(self.correct_result)-1, L = 3*self.nside-1, verbose = False)
            print (time.time() - timer)/60., "minutes for nside = %i"%nside
            sys.stdout.flush();
            plt.plot(np.real(self.result[nside]), '%s--'%color)

        #plt.plot(np.real(self.correct_result), 'g--')
        #plt.plot(np.real(self.result32), 'r--', np.real(self.result64), 'b--', np.real(self.correct_result), 'g--')
        plt.show()
else:
    raise Exception('Cant tell if the iinfo is for PAPER or MITEoR')

vs.initial_zenith = np.array([0, lat_degree*np.pi/180])#self.zenithequ
beam_heal_equ = np.array(sv.rotate_healpixmap(beam_healpix, 0, np.pi/2 - vs.initial_zenith[1], vs.initial_zenith[0]))
timer = time.time()

if all_ubl:
    ubls = infoubl
else:
    ubls = [ubl for ubl in infoubl if la.norm(ubl) < inclusion_thresh * (nside_target * 299.792458 / freq)]
print "%i UBLs to include"%len(ubls)

if len(tlist)*len(ubls) < 12*nside**2:
    for i in range(5):
        print 'Not enough degree of freedom! %i*%i<%i.'%(len(tlist), len(ubls), 12*nside**2)
A = np.empty((len(tlist)*len(ubls), 12*nside**2), dtype='complex64')
for i in range(12*nside**2):
    dec, ra = hpf.pix2ang(nside, i)#gives theta phi
    dec = np.pi/2 - dec
    print "%.1f%%"%(100.*float(i)/(12.*nside**2)),
    sys.stdout.flush()

    A[:, i] = np.array([vs.calculate_pointsource_visibility(ra, dec, d, freq, beam_heal_equ = beam_heal_equ, tlist = tlist) for d in ubls]).flatten()
print float(time.time()-timer)/60.
if all_ubl:
    A.tofile('/home/omniscope/data/GSM_data/Amatrix_allubl_nside%i_%iby%i_'%(nside, len(A), len(A[0])) + infofile.split('/')[-1])
else:
    A.tofile('/home/omniscope/data/GSM_data/Amatrix_%iubl_nside%i_%iby%i_'%(len(ubls), nside, len(A), len(A[0])) + infofile.split('/')[-1])

    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()
Beispiel #23
0
if all_ubl:
    ubls = infoubl
else:
    ubls = [
        ubl for ubl in infoubl
        if la.norm(ubl) < inclusion_thresh * (nside_target * 299.792458 / freq)
    ]
print "%i UBLs to include" % len(ubls)

if len(tlist) * len(ubls) < 12 * nside**2:
    for i in range(5):
        print 'Not enough degree of freedom! %i*%i<%i.' % (
            len(tlist), len(ubls), 12 * nside**2)
A = np.empty((len(tlist) * len(ubls), 12 * nside**2), dtype='complex64')
for i in range(12 * nside**2):
    dec, ra = hpf.pix2ang(nside, i)  #gives theta phi
    dec = np.pi / 2 - dec
    print "%.1f%%" % (100. * float(i) / (12. * nside**2)),
    sys.stdout.flush()

    A[:, i] = np.array([
        vs.calculate_pointsource_visibility(ra,
                                            dec,
                                            d,
                                            freq,
                                            beam_heal_equ=beam_heal_equ,
                                            tlist=tlist) for d in ubls
    ]).flatten()
print float(time.time() - timer) / 60.
if all_ubl:
    A.tofile('/home/omniscope/data/GSM_data/Amatrix_allubl_nside%i_%iby%i_' %
def colorcode(i):
    return abs(hpf.pix2ang(mother_nside, i, nest=True)[0] - np.pi/2)/(np.pi/2)
Beispiel #25
0
        A[p] = np.fromfile(A_filename, dtype='complex64').reshape((len(ubls), len(tlist), 12*nside_beamweight**2))[:,tmask].reshape((len(ubls)*len(tlist[tmask]), 12*nside_beamweight**2))
    else:
        #beam
        beam_healpix = local_beam[p](freq)
        #hpv.mollview(beam_healpix, title='beam %s'%p)
        #plt.show()

        vs = sv.Visibility_Simulator()
        vs.initial_zenith = np.array([0, lat_degree*np.pi/180])#self.zenithequ
        beam_heal_equ = np.array(sv.rotate_healpixmap(beam_healpix, 0, np.pi/2 - vs.initial_zenith[1], vs.initial_zenith[0]))
        print "Computing A matrix for %s pol..."%p
        sys.stdout.flush()
        timer = time.time()
        A[p] = np.empty((len(tlist)*len(ubls), 12*nside_beamweight**2), dtype='complex64')
        for i in range(12*nside_beamweight**2):
            dec, ra = hpf.pix2ang(nside_beamweight, i)#gives theta phi
            dec = np.pi/2 - dec
            print "\r%.1f%% completed, %f minutes left"%(100.*float(i)/(12.*nside_beamweight**2), (12.*nside_beamweight**2-i)/(i+1)*(float(time.time()-timer)/60.)),
            sys.stdout.flush()

            A[p][:, i] = np.array([vs.calculate_pointsource_visibility(ra, dec, d, freq, beam_heal_equ = beam_heal_equ, tlist = tlist) for d in ubls]).flatten()

        print "%f minutes used"%(float(time.time()-timer)/60.)
        sys.stdout.flush()
        A[p].tofile(A_filename)
        A[p] = A[p].reshape((len(ubls), len(tlist), 12*nside_beamweight**2))[:,tmask].reshape((len(ubls)*len(tlist[tmask]), 12*nside_beamweight**2))

####################################################
###beam weights using an equal pixel A matrix######
#################################################
print "Computing beam weight...",
Beispiel #26
0
        #plt.show()

        vs = sv.Visibility_Simulator()
        vs.initial_zenith = np.array([0, lat_degree * np.pi / 180
                                      ])  #self.zenithequ
        beam_heal_equ = np.array(
            sv.rotate_healpixmap(beam_healpix, 0,
                                 np.pi / 2 - vs.initial_zenith[1],
                                 vs.initial_zenith[0]))
        print "Computing A matrix for %s pol..." % p
        sys.stdout.flush()
        timer = time.time()
        A[p] = np.empty((len(tlist) * len(ubls), 12 * nside**2),
                        dtype='complex64')
        for i in range(12 * nside**2):
            dec, ra = hpf.pix2ang(nside, i)  #gives theta phi
            dec = np.pi / 2 - dec
            print "\r%.1f%% completed, %f minutes left" % (
                100. * float(i) / (12. * nside**2), (12. * nside**2 - i) /
                (i + 1) * (float(time.time() - timer) / 60.)),
            sys.stdout.flush()

            A[p][:, i] = np.array([
                vs.calculate_pointsource_visibility(
                    ra, dec, d, freq, beam_heal_equ=beam_heal_equ, tlist=tlist)
                for d in ubls
            ]).flatten()

        print "%f minutes used" % (float(time.time() - timer) / 60.)
        sys.stdout.flush()
        A[p].tofile(A_filename)
def K_RJ2MJysr(K_RJ, nu):#in Kelvin and Hz
    conversion_factor = 2 * (nu / c)**2 * kB
    return K_RJ * conversion_factor * 1e20#1e-26 for Jy and 1e6 for MJy

TRJ_mask = original_freqs < 20
TCMB_mask = (original_freqs >= 20) & (original_freqs < 500)
original_idata[TRJ_mask] = K_RJ2MJysr(1., original_freqs[TRJ_mask] * 1e9)[:, None] * original_idata[TRJ_mask]
original_idata[TCMB_mask] = K_CMB2MJysr(1., original_freqs[TCMB_mask] * 1e9)[:, None] * original_idata[TCMB_mask]

###############################
##start loop
estimated_data = []
latitude_masks = [np.ones(mother_npix, dtype=bool)]
n_latitude_regions = 2
gal_lat = np.abs(np.pi / 2 - hpf.pix2ang(mother_nside, range(mother_npix), nest=True)[0])
lat_width = np.pi / 2 / n_latitude_regions
for n_lat in range(n_latitude_regions):
    latitude_masks.append((gal_lat >= n_lat * lat_width) & (gal_lat < (n_lat + 1) * lat_width))
estimation_errors = [[] for i in range(1 + n_latitude_regions)]
estimation_renorm_errors = [[] for i in range(1 + n_latitude_regions)]
pseudo_estimation_errors = [[] for i in range(1 + n_latitude_regions)]
for remove_f in range(-1, len(original_idata)):
    removed_idata = np.copy(original_idata[remove_f])
    removed_freq = np.copy(original_freqs[remove_f])
    idata = original_idata[np.array([f for f in range(len(original_idata)) if f != remove_f])]
    freqs = original_freqs[np.array([f for f in range(len(original_freqs)) if f != remove_f])]
    ##############################################
    ##############################################
    ####start I data processing method 1
    ##############################################
metStart = float(par[2])
metStop = float(par[3])
if len(par)>4:
    pathCatalogue = par[4]
else:
    pathCatalogue = "/disk/gamma/cta/store/takhsm/FermiData/catalogue/gll_psch_v09.fit"
print "Time domain:", metStart, "-", metStop
# ON/OFF regions
NHPSIDE_OFF = 16
aHpxOFF = [find_galoff_healpxs(NHPSIDE_OFF, 0, pathCatalogue)]
print aHpxOFF
aCoordsPix_array = [[]]
aAreaPix_array = [[]]
aStrRegion = ["GalacticOFF"]
for npix in aHpxOFF[0]:
    aAngPix = hppf.pix2ang(NHPSIDE_OFF, npix)
    aCoordsPix_array[0].append(SkyCoord(aAngPix[1], pi/2-aAngPix[0], unit="rad"))
    aAreaPix_array[0].append(hppf.nside2pixarea(NHPSIDE_OFF, npix))
    
# Output objects
aFileToI = []
fileRoot = ROOT.TFile("Livetime_GalacticOff_{0}.root".format(nameFileSuffix), "update")
aHtgLt = []
for hRegion in range(len(aStrRegion)):
    aHtgLt.append(ROOT.TH3D("htgLt_{0}".format(aStrRegion[hRegion]), "Livetime over {0} [sec sr];Cos(Inclination angle);Zenith angle [deg];Time[sec]".format(aStrRegion[hRegion]), 40, 0.2, 1.0, 180, 0, 180, max(10, int(metStop-metStart)/54000), metStart, metStop+1))
make_livetime_histogram(aHtgLt, len(aStrRegion), pathFileScAll, metStart, metStop, aFileToI, aCoordsPix_array, aAreaPix_array)
aHtgLt_projYX = []
fileRoot.cd()
for jR in range(len(aStrRegion)):
    aHtgLt[jR].Write()
    aHtgLt_projYX.append(aHtgLt[jR].Project3D("yx"))
    '/home/omniscope/simulate_visibilities/data/gsm1.fits' +
    str(nside_standard))
pca2 = hp.fitsfunc.read_map(
    '/home/omniscope/simulate_visibilities/data/gsm2.fits' +
    str(nside_standard))
pca3 = hp.fitsfunc.read_map(
    '/home/omniscope/simulate_visibilities/data/gsm3.fits' +
    str(nside_standard))
gsm_standard = 422.952 * (0.307706 * pca1 + -0.281772 * pca2 +
                          0.0123976 * pca3)
equatorial_GSM_standard = np.zeros(12 * nside_standard**2, 'float')
#rotate sky map
print "Rotating GSM_standard...",
sys.stdout.flush()
for i in range(12 * nside_standard**2):
    ang = hp.rotator.Rotator(coord='cg')(hpf.pix2ang(nside_standard, i))
    equatorial_GSM_standard[i] = hpf.get_interp_val(gsm_standard, ang[0],
                                                    ang[1])
print "done."
sys.stdout.flush()

if nside != nside_standard:
    if nside > 4:
        pca1 = hp.fitsfunc.read_map(
            '/home/omniscope/simulate_visibilities/data/gsm1.fits' +
            str(nside))
        pca2 = hp.fitsfunc.read_map(
            '/home/omniscope/simulate_visibilities/data/gsm2.fits' +
            str(nside))
        pca3 = hp.fitsfunc.read_map(
            '/home/omniscope/simulate_visibilities/data/gsm3.fits' +
Beispiel #30
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)
        sys.stdout.flush()
        A[p] = np.fromfile(A_path, dtype='complex64').reshape((len(ubls) * nt_used, 12 * nside_beamweight ** 2))
    else:
        # beam
        if p == 'x':
            beam_heal_equ = beam_heal_equ_x
        elif p == 'y':
            beam_heal_equ = beam_heal_equ_x
        print "Computing sky weighting A matrix for %s pol..." % p
        sys.stdout.flush()

        A[p] = np.zeros((nt_used * len(ubls), 12 * nside_beamweight ** 2), dtype='complex64')

        timer = time.time()
        for i in np.arange(12 * nside_beamweight ** 2):
            dec, ra = hpf.pix2ang(nside_beamweight, i)  # gives theta phi
            dec = np.pi / 2 - dec
            print "\r%.1f%% completed" % (100. * float(i) / (12. * nside_beamweight ** 2)),
            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()
        A_filename = datadir + tag + '_%s%s_%i_%i.Agsm'%(p, p, len(tlist)*len(ubls), 12*nside**2)

        if os.path.isfile(A_filename) and not force_recompute:
            print A_filename + " already exists."

        else:
            #beam
            beam_healpix = local_beam[p](freq)
            #hpv.mollview(beam_healpix, title='beam %s'%p)
            #plt.show()

            vs = sv.Visibility_Simulator()
            vs.initial_zenith = np.array([0, lat_degree*np.pi/180])#self.zenithequ
            beam_heal_equ = np.array(sv.rotate_healpixmap(beam_healpix, 0, np.pi/2 - vs.initial_zenith[1], vs.initial_zenith[0]))
            print "Computing A matrix for %s pol..."%p
            sys.stdout.flush()
            timer = time.time()
            A[p] = np.empty((len(tlist)*len(ubls), 12*nside**2), dtype='complex64')
            rotator = hpr.Rotator(coord=['G', 'C'])
            for i in range(12*nside**2):
                dec, ra = rotator(hpf.pix2ang(nside, i, nest=True))#gives theta phi
                dec = np.pi/2 - dec
                print "\r%.1f%% completed, %f minutes left"%(100.*float(i)/(12.*nside**2), (12.*nside**2-i)/(i+1)*(float(time.time()-timer)/60.)),
                sys.stdout.flush()

                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_filename)
Beispiel #33
0
 def p2a(ipix):
     return pix2ang(nside, ipix, nest=True, lonlat=True)
def main(name, ra, dec, perf, inclin, energy, suffix, start, stop, torigin, truepoint, errrad, exstart, exstop):

    # Spacecraft data
    pathFileScAll = "/disk/gamma/cta/store/takhsm/FermiData/spacecraft/mtakahas-AstroServer-00011-ft2-30s.fits"

    print "===================="
    
    # PSF histogram
    file_perf = ROOT.TFile(perf, 'READ')
    htg2_psf = file_perf.Get('psf_cth_q68_hist')
    FIXED_PSF_ENERGY = energy
    if FIXED_PSF_ENERGY<=0:
        print 'Scaling is variable for energy'
    else:
        print 'Scaling is fixed for energy at', FIXED_PSF_ENERGY
    FIXED_PSF_INCLIN = inclin
    if FIXED_PSF_INCLIN<=0:
        print 'Scaling is variable for inclination'
    else:
        print 'Scaling is fixed for inclination at', FIXED_PSF_INCLIN


    # ON/OFF regions
    nOff = 0;
#    NHPSIDE_ON = 512
    NHPSIDE_ON = 32
    ANG_CUT = 5.
    ANG_CUT_RAD = radians(5.)
    aHpx_array = [find_pointon_healpxs(ra, dec, ANG_CUT, nhpside=NHPSIDE_ON)]
    aCoordsRegion = [SkyCoord(ra, dec, unit="deg")]
    aCoordsPix_array = []
    aAreaPix_array = []
    aAreaPix_sum = []
    aStrRegion = []
    str_lit_unit = []
    for (iRegion, coordsRegion) in enumerate(aCoordsRegion):
        if iRegion==0:
            aStrRegion.append("ON")
        else:
            aStrRegion.append("OFF{0}".format(iRegion))
        if truepoint==True:
            aCoordsPix_array.append(coordsRegion)
            aAreaPix_array.append(0)
            print aCoordsPix_array[-1]
            str_lit_unit.append('sec')
            aAreaPix_sum.append(0)
        else:
            aCoordsPix_array.append([])
            aAreaPix_array.append([])
            for npix in aHpx_array[iRegion]:
                aAngPix = hppf.pix2ang(NHPSIDE_ON, npix)
                aCoordsPix_array[-1].append(SkyCoord(aAngPix[1], pi/2.-aAngPix[0], unit="rad"))
                area_pix = hppf.nside2pixarea(NHPSIDE_ON)
                aAreaPix_array[-1].append(area_pix)
            str_lit_unit.append('sec sr')
            print aCoordsPix_array[-1]
            aAreaPix_sum.append(sum(aAreaPix_array[-1]))
            print 'Solid angle =', aAreaPix_sum[-1], 'sr'

    # Output objects
    aFileToI = []
    if suffix!="":
        suffix = "_" + suffix
    if truepoint==True:
        suffix = suffix + "_TruePoint"
    
    fileRoot = ROOT.TFile("Livetime_{0}_MET{1}-{2}{3}.root".format(name, int(start), int(stop), suffix), "update")
    aHtgLt = []
    NBIN_CTH = 50
    EDGE_CTH_LOW = 0.0
    EDGE_CTH_UP = 1.0
    NBIN_ZEN = 180
    EDGE_ZEN_LOW = 0
    EDGE_ZEN_UP = 180
    NBIN_ENE = htg2_psf.GetNbinsX()
    EDGE_ENE_LOW =  htg2_psf.GetXaxis().GetBinLowEdge(1)
    EDGE_ENE_UP =  htg2_psf.GetXaxis().GetBinUpEdge(htg2_psf.GetNbinsX())
    if torigin==0:
        torigin = start 
    tmin_htg = 239557417
    tmax_htg = 536457605
        
    for hRegion in range(nOff+1):
        aHtgLt.append(ROOT.TH3D("htgLt_{0}".format(hRegion), "Livetime for {0} [{1}];Cos(Inclination angle);Zenith angle [deg];Time - {2} [sec]".format(name, str_lit_unit[hRegion], torigin), NBIN_CTH, EDGE_CTH_LOW, EDGE_CTH_UP, NBIN_ZEN, EDGE_ZEN_LOW, EDGE_ZEN_UP, max(10, int(tmax_htg-tmin_htg)/54000), tmin_htg, tmax_htg))#tPro, tPost))
    # make_livetime_histogram(aHtgLt, nOff+1 ,pathFileScAll, start+torigin, stop+torigin, aFileToI, aCoordsPix_array, aAreaPix_array, torigin) !!!BUG!!!
    make_livetime_histogram(aHtgLt, nOff+1 ,pathFileScAll, start, stop, aFileToI, aCoordsPix_array, aAreaPix_array, torigin, exstart, exstop)
    aHtgLt_projYX = []
    aHtgLt_scaled = []
    print 'Making output products...'
    fileRoot.cd()
    for jR in range(nOff+1):
        print 'Region', jR
        aHtgLt[jR].Write()
        aHtgLt_projYX.append(aHtgLt[jR].Project3D("yx"))
        aHtgLt_projYX[jR].Write()
        if truepoint==False:
            aHtgLt_scaled.append(ROOT.TH3D('{0}_scaled'.format(aHtgLt[jR].GetName()), '{0} scaled;{1};{2};log_{{10}}Energy [MeV]'.format(aHtgLt[jR].GetTitle(), aHtgLt_projYX[jR].GetXaxis().GetTitle(), aHtgLt_projYX[jR].GetYaxis().GetTitle()), NBIN_CTH, EDGE_CTH_LOW, EDGE_CTH_UP, NBIN_ZEN, EDGE_ZEN_LOW, EDGE_ZEN_UP, NBIN_ENE, EDGE_ENE_LOW, EDGE_ENE_UP))
            for iz in range(1, 1+NBIN_ENE):
                print '  Energy {0} - {1}'.format(aHtgLt_scaled[-1].GetZaxis().GetBinLowEdge(iz), aHtgLt_scaled[-1].GetZaxis().GetBinUpEdge(iz))
                if FIXED_PSF_ENERGY<=0:
                    nbin_ene_psf = htg2_psf.GetXaxis().FindBin(aHtgLt_scaled[-1].GetZaxis().GetBinCenter(iz))
                else:
                    nbin_ene_psf = htg2_psf.GetXaxis().FindBin(FIXED_PSF_ENERGY)
                for ix in range(1, 1+NBIN_CTH):
                    if FIXED_PSF_INCLIN<=0:
                        nbin_inc_psf = htg2_psf.GetYaxis().FindBin(aHtgLt_scaled[-1].GetXaxis().GetBinCenter(ix))
                    else:
                        nbin_inc_psf = htg2_psf.GetYaxis().FindBin(FIXED_PSF_INCLIN)
                    psf_cut_rad = radians(htg2_psf.GetBinContent(nbin_ene_psf, nbin_inc_psf) + errrad)
                    area_ratio = 2.*pi*(1.0-cos(psf_cut_rad)) / aAreaPix_sum[jR]
                    print '    Inclination {0} - {1} : Scaling factor = {2}'.format(aHtgLt_scaled[-1].GetXaxis().GetBinLowEdge(ix), aHtgLt_scaled[-1].GetXaxis().GetBinUpEdge(ix), area_ratio)
                    for iy in range(1, 1+NBIN_ZEN):
                        aHtgLt_scaled[-1].SetBinContent(ix, iy, iz, aHtgLt_projYX[jR].GetBinContent(ix, iy)*area_ratio)
            aHtgLt_scaled[-1].Write()
    print 'Livetime calculation finished.'
        nside = min(nside, 512)
        L = nside*3 - 1
        print "Using nside = %i for GSM."%nside,
        sys.stdout.flush()
        if nside not in GSMs:
            print "Loading..."
            sys.stdout.flush()
            pcas = [hp.fitsfunc.read_map(datadir + '/gsm%i.fits'%(i+1) + str(nside), verbose=False) for i in range(3)]
            print "done.",
            sys.stdout.flush()
            ####rotate sky map and get alm
            print "Rotating GSM",
            sys.stdout.flush()
            GSMs[nside] = np.zeros((3,12*nside**2),'float')
            for i in range(12*nside**2):
                ang = g2e_rotator(hpf.pix2ang(nside,i))
                for j in range(3):
                    GSMs[nside][j,i] = hpf.get_interp_val(pcas[j], ang[0], ang[1])
            print "Done."
        gsm_weights = gsm_weights_f(np.log(freqs[f]))
        print "GSM weights:", gsm_weights
        sys.stdout.flush()

        gsm = gsm_weights[0]*(gsm_weights[1]*GSMs[nside][0] + gsm_weights[2]*GSMs[nside][1] + gsm_weights[3]*GSMs[nside][2])
        alm = sv.convert_healpy_alm(hp.sphtfunc.map2alm(gsm), 3 * nside - 1)
        result = vs.calculate_visibility(sv.expand_real_alm(alm), d=ubl, freq=freqs[f], nt=nt, L = 3*nside-1, verbose = True)
        result.astype('complex64').tofile(opdir+'/Visibilties_for_%i_south_%i_east_0_up_%s%s_pol_%.1f_MHz_%i_step.bin'%(unit_ubl[0],unit_ubl[1],pol,pol,freqs[f],nt))
        print "Time taken %.4f"%(float((time.time()-timer)/60.))


                           #_
      #__ _ ____ __    __ _| |_ __
     #/ _` (_-< '  \  / _` | | '  \
     #\__, /__/_|_|_| \__,_|_|_|_|_|
     #|___/
    #create sky map alm
    pca1 = hp.fitsfunc.read_map('/home/eric/Dropbox/MIT/UROP/simulate_visibilities/GSM_32/gsm1.fits32')
    pca2 = hp.fitsfunc.read_map('/home/eric/Dropbox/MIT/UROP/simulate_visibilities/GSM_32/gsm2.fits32')
    pca3 = hp.fitsfunc.read_map('/home/eric/Dropbox/MIT/UROP/simulate_visibilities/GSM_32/gsm3.fits32')
    gsm = 422.952*(0.307706*pca1+-0.281772*pca2+0.0123976*pca3)

    nside=32
    equatorial_GSM = np.zeros(12*nside**2,'float')
    #rotate sky map
    for i in range(12*nside**2):
        ang = hp.rotator.Rotator(coord='cg')(hpf.pix2ang(nside,i))
        pixindex, weight = hpf.get_neighbours(nside,ang[0],ang[1])
        for pix in range(len(pixindex)):
            equatorial_GSM[i] += weight[pix]*gsm[pixindex[pix]]

    almlist = hp.sphtfunc.map2alm(equatorial_GSM,iter=10)
    alm={}
    for l in range(96):
        for mm in range(-l,l+1):
            if mm >= 0:
                alm[(l,mm)] = (1.0j)**mm*almlist[hp.sphtfunc.Alm.getidx(nside*3-1,l,abs(mm))]
            if mm < 0:
                alm[(l,mm)] = np.conj((1.0j)**mm*almlist[hp.sphtfunc.Alm.getidx(nside*3-1,l,abs(mm))])


    #set frequency and baseline vector
            beam_healpix = abs(local_beam(freq)[0]) ** 2 + abs(local_beam(freq)[1]) ** 2
        elif p == 'y':
            beam_healpix = abs(local_beam(freq)[2]) ** 2 + abs(local_beam(freq)[3]) ** 2
        # hpv.mollview(beam_healpix, title='beam %s'%p)
        # plt.show()

        vs = sv.Visibility_Simulator()
        vs.initial_zenith = np.array([0, lat_degree * np.pi / 180])  # self.zenithequ
        beam_heal_equ = np.array(
            sv.rotate_healpixmap(beam_healpix, 0, np.pi / 2 - vs.initial_zenith[1], vs.initial_zenith[0]))
        print "Computing A matrix for %s pol..." % p
        sys.stdout.flush()
        timer = time.time()
        A[p] = np.empty((len(tlist) * len(ubls), 12 * nside_beamweight ** 2), dtype='complex64')
        for i in range(12 * nside_beamweight ** 2):
            dec, ra = hpf.pix2ang(nside_beamweight, i)  # gives theta phi
            dec = np.pi / 2 - dec
            print "\r%.1f%% completed, %f minutes left" % (100. * float(i) / (12. * nside_beamweight ** 2),
                                                           (12. * nside_beamweight ** 2 - i) / (i + 1) * (
                                                           float(time.time() - timer) / 60.)),
            sys.stdout.flush()

            A[p][:, i] = np.array(
                [vs.calculate_pointsource_visibility(ra, dec, d, freq, beam_heal_equ=beam_heal_equ, tlist=tlist) for d
                 in ubls]).flatten()

        print "%f minutes used" % (float(time.time() - timer) / 60.)
        sys.stdout.flush()
        A[p].tofile(A_path)
        A[p] = A[p].reshape((len(ubls), len(tlist), 12 * nside_beamweight ** 2))[:, tmask].reshape(
            (len(ubls) * len(tlist[tmask]), 12 * nside_beamweight ** 2))
    beam_heal_equ = np.array(sv.rotate_healpixmap(beam_healpix, 0, np.pi/2 - vs.initial_zenith[1], vs.initial_zenith[0]))



    #compute A matrix
    A_filename = datadir + tag + '_%s%s_%i_%i.A'%(p, p, len(tlist)*len(ubls), 12*nside**2)

    if os.path.isfile(A_filename) and not force_recompute:
        print "Reading A matrix from %s"%A_filename
        A[p] = np.fromfile(A_filename, dtype='complex64').reshape((len(tlist)*len(ubls), 12*nside**2))
    else:
        print "Computing A matrix for %s pol..."%p
        timer = time.time()
        A[p] = np.empty((len(tlist)*len(ubls), 12*nside**2), dtype='complex64')
        for i in range(12*nside**2):
            dec, ra = hpf.pix2ang(nside, i)#gives theta phi
            dec = np.pi/2 - dec
            print "\r%.1f%% completed, %f minutes left"%(100.*float(i)/(12.*nside**2), (12.*nside**2-i)/(i+1)*(float(time.time()-timer)/60.)),
            sys.stdout.flush()

            A[p][:, i] = np.array([vs.calculate_pointsource_visibility(ra, dec, d, freq, beam_heal_equ = beam_heal_equ, tlist = tlist) for d in ubls]).flatten()

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

    #get Ni (1/variance) and data
    var_filename = datadir + tag + '_%s%s_%i_%i.var'%(p, p, nt, nUBL)
    Ni[p] = 1./np.fromfile(var_filename, dtype='float32').reshape((nt, nUBL)).transpose().flatten()
    data_filename = datadir + tag + '_%s%s_%i_%i.dat'%(p, p, nt, nUBL)
    data[p] = (np.fromfile(data_filename, dtype='complex64').reshape((nt, nUBL)).transpose().flatten()*1.e-26*(C/freq)**2/kB/(4*np.pi/(12*nside**2))).conjugate()#there's a conjugate convention difference
vs.initial_zenith = np.array([0, lat_degree*np.pi/180])#self.zenithequ
beam_heal_equ = np.array(sv.rotate_healpixmap(beam_healpix, 0, np.pi/2 - vs.initial_zenith[1], vs.initial_zenith[0]))
timer = time.time()

if all_ubl:
    ubls = infoubl
else:
    ubls = [ubl for ubl in infoubl if la.norm(ubl) < inclusion_thresh * (nside_target * 299.792458 / freq)]
print "%i UBLs to include"%len(ubls)

if len(tlist)*len(ubls) < 12*nside**2:
    for i in range(5):
        print 'Not enough degree of freedom! %i*%i<%i.'%(len(tlist), len(ubls), 12*nside**2)


A = np.empty((len(tlist)*len(ubls), nps), dtype='complex64')
for i in range(nps):
    ra, dec = point_sources[i]
    theta = np.pi/2 - dec
    phi = ra
    theta_heal, phi_heal = hpf.pix2ang(nside, hpf.ang2pix(nside, theta, phi))

    A[:, i] = np.array([vs.calculate_pointsource_visibility(phi_heal, np.pi/2.-theta_heal, d, freq, beam_heal_equ = beam_heal_equ, tlist = tlist) for d in ubls]).flatten()
print float(time.time()-timer)/60.
if all_ubl:
    A.tofile('/home/omniscope/data/GSM_data/AmatrixPS_allubl_nside%i_%iby%i_'%(nside, len(A), len(A[0])) + infofile.split('/')[-1])
else:
    A.tofile('/home/omniscope/data/GSM_data/AmatrixPS_%iubl_nside%i_%iby%i_'%(len(ubls), nside, len(A), len(A[0])) + infofile.split('/')[-1])

        #plt.show()

        vs = sv.Visibility_Simulator()
        vs.initial_zenith = np.array([0, lat_degree * np.pi / 180
                                      ])  #self.zenithequ
        beam_heal_equ = np.array(
            sv.rotate_healpixmap(beam_healpix, 0,
                                 np.pi / 2 - vs.initial_zenith[1],
                                 vs.initial_zenith[0]))
        print "Computing A matrix for %s pol..." % p
        sys.stdout.flush()
        timer = time.time()
        A[p] = np.empty((len(tlist) * len(ubls), 12 * nside_beamweight**2),
                        dtype='complex64')
        for i in range(12 * nside_beamweight**2):
            dec, ra = hpf.pix2ang(nside_beamweight, i)  #gives theta phi
            dec = np.pi / 2 - dec
            print "\r%.1f%% completed, %f minutes left" % (
                100. * float(i) / (12. * nside_beamweight**2),
                (12. * nside_beamweight**2 - i) / (i + 1) *
                (float(time.time() - timer) / 60.)),
            sys.stdout.flush()

            A[p][:, i] = np.array([
                vs.calculate_pointsource_visibility(
                    ra, dec, d, freq, beam_heal_equ=beam_heal_equ, tlist=tlist)
                for d in ubls
            ]).flatten()

        print "%f minutes used" % (float(time.time() - timer) / 60.)
        sys.stdout.flush()
#hp.mollview(pca2, title='first PCA')
#hp.mollview(pca3, title='first PCA')
#plt.show()

pca1 = hp.fitsfunc.read_map(
    '/home/eric/Dropbox/MIT/UROP/simulate_visibilities/GSM_32/gsm1.fits64')
pca2 = hp.fitsfunc.read_map(
    '/home/eric/Dropbox/MIT/UROP/simulate_visibilities/GSM_32/gsm2.fits64')
pca3 = hp.fitsfunc.read_map(
    '/home/eric/Dropbox/MIT/UROP/simulate_visibilities/GSM_32/gsm3.fits64')
gsm = 422.952 * (0.307706 * pca1 - 0.281772 * pca2 + 0.0123976 * pca3)

nside = 64
equatorial_GSM = np.zeros(12 * nside**2, 'float')
#rotate sky map
for i in range(12 * nside**2):
    ang = hp.rotator.Rotator(coord='cg')(hpf.pix2ang(nside, i))
    pixindex, weight = hpf.get_neighbours(nside, ang[0], ang[1])
    for pix in range(len(pixindex)):
        equatorial_GSM[i] += np.log(weight[pix] * gsm[pixindex[pix]])

almlist = hp.sphtfunc.map2alm(equatorial_GSM)
alm = {}
for l in range(96):
    for mm in range(-l, l + 1):
        alm[(l, mm)] = almlist[hp.sphtfunc.Alm.getidx(nside * 3 - 1, l,
                                                      abs(mm))]

hp.visufunc.mollview(equatorial_GSM)
plt.show()

###########################
###########################
###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)

data_file = np.load(data_file_name)
freqs = data_file['freqs']
exclude_freqs = [freq for freq in freqs if freq > 40.]#[60.8, 70, 93.5]


idata = data_file['idata']
qudata = data_file['qdata'] + 1.j * data_file['udata']
Beispiel #43
0
        if nside not in GSMs:
            print "Loading..."
            sys.stdout.flush()
            pcas = [
                hp.fitsfunc.read_map(datadir + '/gsm%i.fits' % (i + 1) +
                                     str(nside),
                                     verbose=False) for i in range(3)
            ]
            print "done.",
            sys.stdout.flush()
            ####rotate sky map and get alm
            print "Rotating GSM",
            sys.stdout.flush()
            GSMs[nside] = np.zeros((3, 12 * nside**2), 'float')
            for i in range(12 * nside**2):
                ang = g2e_rotator(hpf.pix2ang(nside, i))
                for j in range(3):
                    GSMs[nside][j, i] = hpf.get_interp_val(
                        pcas[j], ang[0], ang[1])
            print "Done."
        gsm_weights = gsm_weights_f(np.log(freqs[f]))
        print "GSM weights:", gsm_weights
        sys.stdout.flush()

        gsm = gsm_weights[0] * (gsm_weights[1] * GSMs[nside][0] +
                                gsm_weights[2] * GSMs[nside][1] +
                                gsm_weights[3] * GSMs[nside][2])
        alm = sv.convert_healpy_alm(hp.sphtfunc.map2alm(gsm), 3 * nside - 1)
        result = vs.calculate_visibility(sv.expand_real_alm(alm),
                                         d=ubl,
                                         freq=freqs[f],
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])
gsm_standard = np.exp(scale_loglog(np.log(freq))) * (w1(freq) * pca1 + w2(freq) * pca2 + w3(freq) * pca3)

# rotate sky map and converts to nest
equatorial_GSM_standard = np.zeros(12 * nside_standard ** 2, 'float')
print "Rotating GSM_standard and converts to nest...",
sys.stdout.flush()
equ2013_to_gal_matrix = hp.rotator.Rotator(coord='cg').mat.dot(sv.epoch_transmatrix(2000, stdtime=2013.58))
ang0, ang1 = hp.rotator.rotateDirection(equ2013_to_gal_matrix,
                                        hpf.pix2ang(nside_standard, range(12 * nside_standard ** 2), nest=True))
equatorial_GSM_standard = hpf.get_interp_val(gsm_standard, ang0, ang1)
print "done."
sys.stdout.flush()


nside_distribution = np.zeros(12 * nside_standard ** 2)
final_index = np.zeros(12 * nside_standard ** 2, dtype=int)
thetas, phis, sizes = [], [], []
abs_thresh = np.mean(equatorial_GSM_standard) * thresh
pixelize(equatorial_GSM_standard, nside_distribution, nside_standard, nside_start, abs_thresh,
         final_index, thetas, phis, sizes)
npix = len(thetas)
valid_pix_mask = hpf.get_interp_val(equatorial_GSM_standard, thetas, phis, nest=True) > valid_pix_thresh * max(equatorial_GSM_standard)
valid_npix = np.sum(valid_pix_mask)
print '>>>>>>VALID NPIX =', valid_npix
Beispiel #45
0
        ubl for ubl in infoubl
        if la.norm(ubl) < inclusion_thresh * (nside_target * 299.792458 / freq)
    ]
print "%i UBLs to include" % len(ubls)

if len(tlist) * len(ubls) < 12 * nside**2:
    for i in range(5):
        print 'Not enough degree of freedom! %i*%i<%i.' % (
            len(tlist), len(ubls), 12 * nside**2)

A = np.empty((len(tlist) * len(ubls), nps), dtype='complex64')
for i in range(nps):
    ra, dec = point_sources[i]
    theta = np.pi / 2 - dec
    phi = ra
    theta_heal, phi_heal = hpf.pix2ang(nside, hpf.ang2pix(nside, theta, phi))

    A[:, i] = np.array([
        vs.calculate_pointsource_visibility(phi_heal,
                                            np.pi / 2. - theta_heal,
                                            d,
                                            freq,
                                            beam_heal_equ=beam_heal_equ,
                                            tlist=tlist) for d in ubls
    ]).flatten()
print float(time.time() - timer) / 60.
if all_ubl:
    A.tofile('/home/omniscope/data/GSM_data/AmatrixPS_allubl_nside%i_%iby%i_' %
             (nside, len(A), len(A[0])) + infofile.split('/')[-1])
else:
    A.tofile('/home/omniscope/data/GSM_data/AmatrixPS_%iubl_nside%i_%iby%i_' %
A = np.fromfile(A_filename, dtype='float32').reshape((len(data), npix))
print "Memory usage: %.3fMB"%(resource.getrusage(resource.RUSAGE_SELF).ru_maxrss / 1000)

#simulate data using GSM
nside_standard = nside
pca1 = hp.fitsfunc.read_map('/home/omniscope/simulate_visibilities/data/gsm1.fits' + str(nside_standard))
pca2 = hp.fitsfunc.read_map('/home/omniscope/simulate_visibilities/data/gsm2.fits' + str(nside_standard))
pca3 = hp.fitsfunc.read_map('/home/omniscope/simulate_visibilities/data/gsm3.fits' + str(nside_standard))
gsm_standard = 422.952*(0.307706*pca1+-0.281772*pca2+0.0123976*pca3)
equatorial_GSM_standard = np.zeros(12*nside_standard**2,'float')
#rotate sky map
print "Rotating GSM_standard...",
sys.stdout.flush()
#print hp.rotator.Rotator(coord='cg').mat
for i in range(12*nside_standard**2):
    ang = hp.rotator.Rotator(coord='cg')(hpf.pix2ang(nside_standard,i))
    equatorial_GSM_standard[i] = hpf.get_interp_val(gsm_standard, ang[0], ang[1])
print "done."
sys.stdout.flush()

print "Simulating GSM data...",
sys.stdout.flush()
sim_data = np.array([A[i].dot(equatorial_GSM_standard[pix_mask]) for i in range(len(A))]) + np.random.randn(len(data))/Ni**.5
print "done."
sys.stdout.flush()

#compute At.y
print "Computing At.y...",
sys.stdout.flush()
qaz = Ni * data
Atdata = np.array([A[:, i].dot(qaz) for i in range(len(A[0]))])
        A[p] = np.fromfile(A_filename, dtype='complex64').reshape((len(ubls), len(tlist), 12*nside**2))[:,tmask].reshape((len(ubls)*len(tlist[tmask]), 12*nside**2))
    else:
        #beam
        beam_healpix = local_beam[p](freq)
        #hpv.mollview(beam_healpix, title='beam %s'%p)
        #plt.show()

        vs = sv.Visibility_Simulator()
        vs.initial_zenith = np.array([0, lat_degree*np.pi/180])#self.zenithequ
        beam_heal_equ = np.array(sv.rotate_healpixmap(beam_healpix, 0, np.pi/2 - vs.initial_zenith[1], vs.initial_zenith[0]))
        print "Computing A matrix for %s pol..."%p
        sys.stdout.flush()
        timer = time.time()
        A[p] = np.empty((len(tlist)*len(ubls), 12*nside**2), dtype='complex64')
        for i in range(12*nside**2):
            dec, ra = hpf.pix2ang(nside, i)#gives theta phi
            dec = np.pi/2 - dec
            print "\r%.1f%% completed, %f minutes left"%(100.*float(i)/(12.*nside**2), (12.*nside**2-i)/(i+1)*(float(time.time()-timer)/60.)),
            sys.stdout.flush()

            A[p][:, i] = np.array([vs.calculate_pointsource_visibility(ra, dec, d, freq, beam_heal_equ = beam_heal_equ, tlist = tlist) for d in ubls]).flatten()

        print "%f minutes used"%(float(time.time()-timer)/60.)
        sys.stdout.flush()
        A[p].tofile(A_filename)
        A[p] = A[p].reshape((len(ubls), len(tlist), 12*nside**2))[:,tmask].reshape((len(ubls)*len(tlist[tmask]), 12*nside**2))

    #get Ni (1/variance) and data
    var_filename = datadir + tag + '_%s%s_%i_%i.var'%(p, p, nt, nUBL)
    Ni[p] = 1./(np.fromfile(var_filename, dtype='float32').reshape((nt, nUBL))[tmask].transpose().flatten() * (1.e-26*(C/freq)**2/kB/(4*np.pi/(12*nside**2)))**2)
    data_filename = datadir + tag + '_%s%s_%i_%i'%(p, p, nt, nUBL) + datatag
import glob, sys
import numpy as np
import healpy as hp
import healpy.pixelfunc as hpf
import healpy.visufunc as hpv
import scipy.interpolate as interp
import simulate_visibilities.simulate_visibilities as sv
import time
import matplotlib.pyplot as plt
nside_standard = 512



#out put Ephi, Ealpha in healpix ring convention

heal_thetas, heal_phis = hpf.pix2ang(nside_standard, range(12*nside_standard**2))

pos_alpha_mask = heal_thetas < np.pi/2

unique_heal_thetas = np.unique(heal_thetas[pos_alpha_mask])
#The 1st column is the azimuthal angle "phi", the second column is the elevation angle "alpha" (theta=pi/2-alpha, if you think of theta as the angle from the z-axis), and the only other relevant column is the 7th one - that's the linear beam strength (the 8th column is a 10log10 of the 7th, but it's not nearly as intuitive in representation).
#Then the 3-4th columns are the real and imaginary components of the electric field in the phi direction "E_phi" and the 5-6th columns are the real and imaginary components of the electric field in the alpha direction "E_alpha"

# print np.sum(pos_alpha_mask), len(pos_alpha_mask)
# plt.plot(pos_alpha_mask); plt.show()

##original script
for pol in ['x', 'y']:
    result = np.zeros((9, 12*nside_standard**2, 2), dtype='complex64')
    d = np.concatenate([np.loadtxt(f) for f in sorted(glob.glob('/home/omniscope/data/mwa_beam/mwa_*-*%s*'%pol))])
    d.shape = (9, 180, 45, 8)
Beispiel #49
0
def _catalog_to_cells_neighbor(catalog, radius, order):
    """
    Convert a catalog to a list of cells.

    This is the original implementation of the `catalog_to_cells`
    function which does not make use of the Healpy `query_disc` routine.

    Note: this function uses a simple flood-filling approach and is
    very slow, especially when used with a large radius for catalog objects
    or a high resolution order.
    """

    if not isinstance(radius, Quantity):
        radius = radius * arcsecond

    nside = 2**order

    # Ensure catalog is in ICRS coordinates.
    catalog = catalog.icrs

    # Determine central cell for each catalog entry.
    phi = catalog.ra.radian
    theta = (pi / 2) - catalog.dec.radian

    cells = np.unique(ang2pix(nside, theta, phi, nest=True))

    # Iteratively consider the neighbors of cells within our
    # catalog regions.
    new_cells = cells
    rejected = np.array((), dtype=np.int64)
    while True:
        # Find new valid neighboring cells which we didn't already
        # consider.
        neighbors = np.unique(
            np.ravel(get_all_neighbours(nside, new_cells, nest=True)))

        neighbors = np.extract([(x != -1) and (x not in cells) and
                                (x not in rejected)
                                for x in neighbors], neighbors)

        # Get the coordinates of each of these neighbors and compare them
        # to the catalog entries.
        (theta, phi) = pix2ang(nside, neighbors, nest=True)

        coords = SkyCoord(phi, (pi / 2) - theta, frame='icrs', unit='rad')

        (idx, sep2d, dist3d) = coords.match_to_catalog_sky(catalog)

        within_range = (sep2d < radius)

        # If we didn't find any new cells within range,
        # end the iterative process.
        if not np.any(within_range):
            break

        new_cells = neighbors[within_range]
        cells = np.concatenate((cells, new_cells))
        rejected = np.concatenate(
            (rejected, neighbors[np.logical_not(within_range)]))

    return cells
        sys.stdout.flush()
        A[p] = np.fromfile(A_filename, dtype='complex64').reshape((len(ubls), len(tlist), 12*nside_beamweight**2))[:,tmask].reshape((len(ubls)*len(tlist[tmask]), 12*nside_beamweight**2))
    else:
        beam_healpix = np.fromfile(datadir + 'mwa_curtin_beam_%s_nside%i_freq167.275_zenith_float32.dat'%(p, bnside), dtype='float32')
        #hpv.mollview(beam_healpix, title='beam %s'%p)
        #plt.show()

        vs = sv.Visibility_Simulator()
        vs.initial_zenith = np.array([0, lat_degree*np.pi/180])#self.zenithequ
        beam_heal_equ = np.array(sv.rotate_healpixmap(beam_healpix, 0, np.pi/2 - vs.initial_zenith[1], vs.initial_zenith[0]))
        print "Computing A matrix for %s pol..."%p
        sys.stdout.flush()
        timer = time.time()
        A[p] = np.empty((len(tlist)*len(ubls), 12*nside_beamweight**2), dtype='complex64')
        for i in range(12*nside_beamweight**2):
            dec, ra = hpf.pix2ang(nside_beamweight, i)#gives theta phi
            dec = np.pi/2 - dec
            print "\r%.1f%% completed, %f minutes left"%(100.*float(i)/(12.*nside_beamweight**2), (12.*nside_beamweight**2-i)/(i+1)*(float(time.time()-timer)/60.)),
            sys.stdout.flush()

            A[p][:, i] = np.array([vs.calculate_pointsource_visibility(ra, dec, d, freq, beam_heal_equ = beam_heal_equ, tlist = tlist) for d in ubls]).flatten()

        print "%f minutes used"%(float(time.time()-timer)/60.)
        sys.stdout.flush()
        A[p].tofile(A_filename)
        A[p] = A[p].reshape((len(ubls), len(tlist), 12*nside_beamweight**2))[:,tmask].reshape((len(ubls)*len(tlist[tmask]), 12*nside_beamweight**2))

####################################################
###beam weights using an equal pixel A matrix######
#################################################
Beispiel #51
0
    #compute A matrix
    A_filename = datadir + tag + '_%s%s_%i_%i.A' % (p, p, len(tlist) *
                                                    len(ubls), 12 * nside**2)

    if os.path.isfile(A_filename) and not force_recompute:
        print "Reading A matrix from %s" % A_filename
        A[p] = np.fromfile(A_filename, dtype='complex64').reshape(
            (len(tlist) * len(ubls), 12 * nside**2))
    else:
        print "Computing A matrix for %s pol..." % p
        timer = time.time()
        A[p] = np.empty((len(tlist) * len(ubls), 12 * nside**2),
                        dtype='complex64')
        for i in range(12 * nside**2):
            dec, ra = hpf.pix2ang(nside, i)  #gives theta phi
            dec = np.pi / 2 - dec
            print "\r%.1f%% completed, %f minutes left" % (
                100. * float(i) / (12. * nside**2), (12. * nside**2 - i) /
                (i + 1) * (float(time.time() - timer) / 60.)),
            sys.stdout.flush()

            A[p][:, i] = np.array([
                vs.calculate_pointsource_visibility(
                    ra, dec, d, freq, beam_heal_equ=beam_heal_equ, tlist=tlist)
                for d in ubls
            ]).flatten()

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