Exemplo n.º 1
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
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
Exemplo n.º 3
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()
Exemplo n.º 4
0
    def ant_gain(self, coords, time):
        """ Returns NenuFAR antenna gain values interpolated at
            coordinates ``coords`` at time ``time``.

            :param coords:
            :type coords: :class:`~astropy.coordinates.ICRS` or 
                :class:`~astropy.coordinates.SkyCoord`
            :param time:
            :type time: :class:`~astropy.time.Time`

            :returns: NenuFAR normalized antenna gain
            :rtype: `~numpy.ndarray`

            :Example:
                To get back the HEALPix ant gain:
                
                >>> from nenupy.beam import Beam
                >>> import numpy as np
                >>> import astropy.units as u
                >>> from astropy.coordinates import ICRS
                >>> from astropy.time import Time
                >>> from healpy.pixelfunc import nside2npix, pix2ang
                >>> from healpy.visufunc import mollview
                >>> b = Beam(
                        freq=50,
                        polar='NE',
                    )
                >>> npix = nside2npix(nside=32)
                >>> ra, dec = pix2ang(
                        nside=32,
                        ipix=np.arange(npix),
                        lonlat=True
                    )
                >>> gain = b.ant_gain(
                        coords=ICRS(
                            ra=ra*u.deg,
                            dec=dec*u.deg
                        ),
                        time=Time.now()
                    )
                >>> mollview(gain)

        """
        if not isinstance(coords, (ICRS, SkyCoord)):
            raise TypeError('coords should be ICRS or SkyCoord object')
        if not isinstance(time, Time):
            raise TypeError('time should be Time object')
        hpxgain = nenufar_ant_gain(freq=self.freq,
                                   polar=self.polar,
                                   nside=32,
                                   time=time)
        log.info('NenuFAR HEALPix antenna gain loaded.')
        vals = get_interp_val(m=hpxgain,
                              theta=coords.ra.deg,
                              phi=coords.dec.deg,
                              lonlat=True)
        log.info('NenuFAR antenna gain values interpolated.')
        return vals
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
Exemplo n.º 6
0
    def calculate_pointsource_visibility(
            self,
            ra,
            dec,
            d,
            freq,
            beam_healpix_hor=None,
            beam_heal_equ=None,
            nt=None,
            tlist=None,
            verbose=False):  #d in horizontal coord, tlist in lst hours
        if self.initial_zenith.tolist() == [1000, 1000]:
            raise Exception(
                'ERROR: need to set self.initial_zenith first, which is at t=0, the position of zenith in equatorial coordinate in ra dec radians.'
            )
        if tlist is None and nt is None:
            raise Exception(
                "ERROR: neither nt nor tlist was specified. Must input what lst you want in sidereal hours"
            )
        d_equ = stoc(
            np.append(
                la.norm(d),
                rotatez(
                    rotatey(
                        ctos(d)[1:3], (np.pi / 2 - self.initial_zenith[1])),
                    self.initial_zenith[0])))
        if beam_healpix_hor is None and beam_heal_equ is None:
            raise Exception(
                "ERROR: conversion from alm for beam to beam_healpix not yet supported, so please specify beam_healpix as a keyword directly, in horizontal coord."
            )
        elif beam_heal_equ is None:
            beam_heal_equ = np.array(
                rotate_healpixmap(beam_healpix_hor, 0,
                                  np.pi / 2 - self.initial_zenith[1],
                                  self.initial_zenith[0]))
        if tlist is None:
            tlist = np.arange(0., 24., 24. / nt)
        else:
            tlist = np.array(tlist)

        angle_list = tlist / 12. * np.pi
        result = np.empty(len(angle_list), dtype='complex64')
        ps_vec = -np.array(
            [np.cos(dec) * np.cos(ra),
             np.cos(dec) * np.sin(ra),
             np.sin(dec)])
        ik = 2.j * np.pi * freq / 299.792458
        ###for i, phi in zip(range(len(angle_list)), angle_list):
        ####print beam_heal_equ.shape, np.pi/2 - dec, ra - phi
        ###result[i] = hpf.get_interp_val(beam_heal_equ, np.pi/2 - dec, ra - phi) * np.exp(ik*rotatez_matrix(phi).dot(d_equ).dot(ps_vec))
        ###return result
        return hpf.get_interp_val(beam_heal_equ, np.pi / 2 - dec,
                                  ra - np.array(angle_list)) * np.exp(
                                      ik *
                                      (rotatez_matrix(angle_list).transpose(
                                          2, 0, 1).dot(d_equ).dot(ps_vec)))
    def calculate_pointsource_visibility(self, ra, dec, d, freq, beam_healpix_hor = None, beam_heal_equ = None, nt = None, tlist = None, verbose = False):#d in horizontal coord, tlist in lst hours, beam in unites of power
        if self.initial_zenith.tolist() == [1000, 1000]:
            raise Exception('ERROR: need to set self.initial_zenith first, which is at t=0, the position of zenith in equatorial coordinate in ra dec radians.')
        if tlist is None and nt is None:
                raise Exception("ERROR: neither nt nor tlist was specified. Must input what lst you want in sidereal hours")
        if np.array(d).ndim == 1:
            input_ndim = 1
            d = np.array([d])
        elif np.array(d).ndim == 2:
            input_ndim = 2
        else:
            raise TypeError("Input d has incorrect dimension number of %i."%np.array(d).ndim)
        d_equ = d.dot(np.transpose(rotatez_matrix(self.initial_zenith[0]).dot(rotatey_matrix(np.pi/2 - self.initial_zenith[1]))))
        if beam_healpix_hor is None and beam_heal_equ is None:
            raise Exception("ERROR: conversion from alm for beam to beam_healpix not yet supported, so please specify beam_healpix as a keyword directly, in horizontal coord.")
        elif beam_heal_equ is None:
            beam_heal_equ = np.array(rotate_healpixmap(beam_healpix_hor, 0, np.pi/2 - self.initial_zenith[1], self.initial_zenith[0]))
        if tlist is None:
            tlist = np.arange(0.,24.,24./nt)
        else:
            tlist = np.array(tlist)

        angle_list = tlist/12.*np.pi
        ps_vec = -np.array([np.cos(dec)*np.cos(ra), np.cos(dec)*np.sin(ra), np.sin(dec)])
        ik = 2.j*np.pi*freq/299.792458
        ###for i, phi in zip(range(len(angle_list)), angle_list):
            ####print beam_heal_equ.shape, np.pi/2 - dec, ra - phi
            ###result[i] = hpf.get_interp_val(beam_heal_equ, np.pi/2 - dec, ra - phi) * np.exp(ik*rotatez_matrix(phi).dot(d_equ).dot(ps_vec))
        ###return result
        try:
            result = hpf.get_interp_val(beam_heal_equ, np.pi/2 - dec, ra - np.array(angle_list)) * np.exp(ik * np.einsum('ijt,uj->uti', rotatez_matrix(angle_list), d_equ).dot(ps_vec))
        except:
            print hpf.get_interp_val(beam_heal_equ, np.pi/2 - dec, ra - np.array(angle_list)).shape
            print rotatez_matrix(angle_list).shape, d_equ.shape
            print np.exp(ik * np.einsum('ijt,uj->uti', rotatez_matrix(angle_list), d_equ).dot(ps_vec)).shape
            sys.stdout.flush()
        if input_ndim == 1:
            return result[0]
        else:
            return result
Exemplo n.º 8
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()
Exemplo n.º 9
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()
Exemplo n.º 10
0
    def sky(self, sbar):
        healpix = self.healpix_brightness(sbar)

        lm_map = np.zeros((len(self.f0), self.ncells * self.ncells)) * np.nan
        for i,hp in enumerate(healpix):
            # Convert lgrid to co-lat and longitude in radians.
            L, M = np.meshgrid(self.lgrid[i].value, self.lgrid[i].value)
            lm = np.sqrt(L**2+M**2).flatten()
            mask = lm < 1

            theta = np.arcsin(lm[mask])
            phimod = np.arccos(L.flatten()[mask] / lm[mask])
            phi = np.where(M.flatten()[mask] < 0, phimod, -phimod)
            phi[np.isnan(phi)] = 0.0


            # Generate map from interpolation
            lm_map[i][mask] = pixelfunc.get_interp_val(hp, theta, phi)
            #lm_map[i] = lm_map[i].reshape((self.ncells, self.ncells))

            #print i, lm.max(), L.max(), M.max(), lm_map[i][500]

        lm_map = lm_map.reshape((len(self.f0), self.ncells, self.ncells))
        return lm_map
    def calculate_pointsource_visibility(self, ra, dec, d, freq, beam_healpix_hor = None, beam_heal_equ = None, nt = None, tlist = None, verbose = False):#d in horizontal coord, tlist in lst hours
        if self.initial_zenith.tolist() == [1000, 1000]:
            raise Exception('ERROR: need to set self.initial_zenith first, which is at t=0, the position of zenith in equatorial coordinate in ra dec radians.')
        if tlist is None and nt is None:
                raise Exception("ERROR: neither nt nor tlist was specified. Must input what lst you want in sidereal hours")
        d_equ = stoc(np.append(la.norm(d),rotatez(rotatey(ctos(d)[1:3], (np.pi/2 - self.initial_zenith[1])), self.initial_zenith[0])))
        if beam_healpix_hor is None and beam_heal_equ is None:
            raise Exception("ERROR: conversion from alm for beam to beam_healpix not yet supported, so please specify beam_healpix as a keyword directly, in horizontal coord.")
        elif beam_heal_equ is None:
            beam_heal_equ = np.array(rotate_healpixmap(beam_healpix_hor, 0, np.pi/2 - self.initial_zenith[1], self.initial_zenith[0]))
        if tlist is None:
            tlist = np.arange(0.,24.,24./nt)
        else:
            tlist = np.array(tlist)

        angle_list = tlist/12.*np.pi
        result = np.empty(len(angle_list), dtype='complex64')
        ps_vec = -np.array([np.cos(dec)*np.cos(ra), np.cos(dec)*np.sin(ra), np.sin(dec)])
        ik = 2.j*np.pi*freq/299.792458
        ###for i, phi in zip(range(len(angle_list)), angle_list):
            ####print beam_heal_equ.shape, np.pi/2 - dec, ra - phi
            ###result[i] = hpf.get_interp_val(beam_heal_equ, np.pi/2 - dec, ra - phi) * np.exp(ik*rotatez_matrix(phi).dot(d_equ).dot(ps_vec))
        ###return result
        return hpf.get_interp_val(beam_heal_equ, np.pi/2 - dec, ra - np.array(angle_list)) * np.exp(ik * (rotatez_matrix(angle_list).transpose(2,0,1).dot(d_equ).dot(ps_vec)))
GSMs = {}#will store equatorial coord maps for 3 components for the key word of nside

nside = MIN_GSM_NSIDE
print "Loading GSMs:",
while nside < 2 * 5 * np.max(la.norm(ubls, axis = 1))/(299.792458/np.max(freqs)) or nside == MIN_GSM_NSIDE:
    print nside,
    pca1 = hp.fitsfunc.read_map(datadir + 'gsm1.fits' + str(nside), verbose=False)
    pca2 = hp.fitsfunc.read_map(datadir + 'gsm2.fits' + str(nside), verbose=False)
    pca3 = hp.fitsfunc.read_map(datadir + 'gsm3.fits' + str(nside), verbose=False)

    equ2013_to_gal_matrix = hp.rotator.Rotator(coord='cg').mat.dot(sv.epoch_transmatrix(2000,stdtime=year))
    ang0, ang1 =hp.rotator.rotateDirection(equ2013_to_gal_matrix, hpf.pix2ang(nside, range(12*nside**2)))

    GSMs[nside] = np.zeros((3, 12*nside**2))
    GSMs[nside][0] = hpf.get_interp_val(pca1, ang0, ang1)
    GSMs[nside][1] = hpf.get_interp_val(pca2, ang0, ang1)
    GSMs[nside][2] = hpf.get_interp_val(pca3, ang0, ang1)
    nside = nside * 2
print "Done."
######start calculation
for ubl,unit_ubl in zip(ubls,unit_ubls):
    result = np.zeros((nt, nf), dtype='complex64')
    for f in range(len(freqs)):
        print "Starting UBL: %s at frequency %.3f MHz, %.2f wavelengths."%(ubl, freqs[f], la.norm(ubl)/(299.792458/freqs[f])),
        timer = time.time()

        vs.import_beam(beam_healpix[f])

        ######decide nside for GSM
        nside = MIN_GSM_NSIDE
######load beam
bnside = 64
beam_freqs = np.arange(110., 195., 5.)
print "Reading calfile %s..."%calfile,
sys.stdout.flush()

######cal file loaded
aa = ap.cal.get_aa(calfile, beam_freqs/1000.)
print "Done. Antenna layout:"
print aa.ant_layout
sys.stdout.flush()


beam_healpix = np.zeros((len(beam_freqs), 2, 12*bnside**2), dtype='float32')

healpixvecs = np.array(hpf.pix2vec(bnside, range(12*bnside**2)))
paper_healpixvecs = (healpixvecs[:, healpixvecs[2]>=0]).transpose().dot(sv.rotatez_matrix(-np.pi/2).transpose())#in paper's bm_response convention, (x,y) = (0,1) points north.
for p, pol in enumerate(['x', 'y']):
    for i, paper_angle in enumerate(paper_healpixvecs):
        beam_healpix[:, p, i] = (aa[0].bm_response(paper_angle, pol)**2.).flatten()

local_beam_unpol = si.interp1d(beam_freqs, beam_healpix, axis=0)

freq = 110.
for p in range(2):
    plt.subplot(2, 1, p+1)
    plt.plot(hpf.get_interp_val(local_beam_unpol(freq)[p], np.arange(0, PI/2, .01), 0))
    plt.plot(hpf.get_interp_val(local_beam_unpol(freq)[p], np.arange(0, PI/2, .01), PI/2))
plt.show()

#quit()

################################################
#####################GSM###########################
#############################################
#rotate sky map and converts to nest
equatorial_GSM_standard_2000 = np.fromfile(datadir +
                                           'skymap_mwacs_nside256_float32.dat',
                                           dtype='float32')
print "Rotating GSM_standard and converts to nest...",
sys.stdout.flush()
equ2013_to_equ2000_matrix = sv.epoch_transmatrix(2000, stdtime=2013.64)
ang0, ang1 = hp.rotator.rotateDirection(
    equ2013_to_equ2000_matrix,
    hpf.pix2ang(nside_standard, range(12 * nside_standard**2), nest=True))
equatorial_GSM_standard = hpf.get_interp_val(equatorial_GSM_standard_2000,
                                             ang0, ang1)
print "done."
sys.stdout.flush()
#hpv.mollview(np.log10(equatorial_GSM_standard), min=-3.5,max=1.5, coord=plotcoord, title='GSM', nest=True)
#plt.show()
#quit()

########################################################################
########################processing dynamic pixelization######################
########################################################################

nside_distribution = np.zeros(12 * nside_standard**2)
final_index = np.zeros(12 * nside_standard**2)
thetas, phis, sizes = [], [], []
abs_thresh = np.mean(equatorial_GSM_standard * beam_weight) * thresh
pixelize(equatorial_GSM_standard * beam_weight, nside_distribution,
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
    def DBG_calculate_pol_pointsource_visibility(self, ra, dec, d_in, freq, beam_healpix_hor = None, beam_heal_equ = None, nt = None, tlist = None, verbose = False):#d_in in horizontal coord, beam is 4 by npix (xx,xy,yx,yy) in unites of Jones matrix, square root of power, tlist in lst hours, return 4 by 4 by nt numbers, where first dim of 4 is received xx xy yx yy, and second is xx xy yx yy on sky
        if self.initial_zenith.tolist() == [1000, 1000]:
            raise Exception('ERROR: need to set self.initial_zenith first, which is at t=0, the position of zenith in equatorial coordinate in ra dec radians.')
        if tlist is None and nt is None:
                raise Exception("ERROR: neither nt nor tlist was specified. Must input what lst you want in sidereal hours")
        if np.array(d_in).ndim == 1:
            d_in = [d_in]
        d_equ = np.array([stoc(np.append(la.norm(d),rotatez(rotatey(ctos(d)[1:3], (np.pi/2 - self.initial_zenith[1])), self.initial_zenith[0]))) for d in d_in])
        if beam_healpix_hor is None and beam_heal_equ is None:
            raise Exception("ERROR: conversion from alm for beam to beam_healpix not yet supported, so please specify beam_healpix_hor as a keyword directly, in horizontal coord.")
        elif beam_heal_equ is None:
            beam_heal_equ = np.array([rotate_healpixmap(ibeam_healpix_hor, 0, np.pi/2 - self.initial_zenith[1], self.initial_zenith[0]) for ibeam_healpix_hor in beam_healpix_hor])
        if tlist is None:
            tlist = np.arange(0.,24.,24./nt)
        else:
            tlist = np.array(tlist)

        angle_list = tlist/12.*np.pi

        ps_vec = -np.array([np.cos(dec)*np.cos(ra), np.cos(dec)*np.sin(ra), np.sin(dec)])#pointing towards observer
        ik = 2.j*np.pi*freq/299.792458


        beamt = np.array([hpf.get_interp_val(ibeam_heal_equ, np.pi/2 - dec, ra - np.array(angle_list)) for ibeam_heal_equ in beam_heal_equ])
        beamt.shape = (2, 2, len(tlist))
        if verbose:
            plot_jones(beam_heal_equ)

            print "J"
            print beamt

        Rut = rotatez_matrix(angle_list).transpose(2,0,1)#rotation matrix for ubl over t
        fringe = np.exp(ik * (Rut.dot(d_equ.transpose()).transpose(2,0,1).dot(ps_vec)))#u by t

        local_zenith_vect = Rut.dot([np.cos(self.initial_zenith[1]) * np.cos(self.initial_zenith[0]), np.cos(self.initial_zenith[1]) * np.sin(self.initial_zenith[0]), np.sin(self.initial_zenith[1])])#over t the zenith in local coord expressed in equitorial coord

        #BUGGED CODE
        # if np.abs(ps_vec[-1]) == 1:
        #     phi0 = np.array([0, 1, 0])
        #     alpha0 = np.array([-1, 0, 0])
        # else:
        #     phi0 = np.cross([0,0,1], -ps_vec)
        #     phi0 = phi0/la.norm(phi0)
        #     alpha0 = np.cross([0,0,1], phi0)
        #     alpha0 = alpha0/la.norm(alpha0)
        # phi1t = np.cross(local_zenith_vect, -ps_vec)
        # if np.min(la.norm(local_zenith_vect-(-ps_vec), axis = -1)) == 0.:
        #     if la.norm(np.cross([0,0,1], -ps_vec)) != 0:
        #         phi1t[np.argmin(la.norm(local_zenith_vect-(-ps_vec), axis = -1))] = np.cross([0,0,1], -ps_vec)
        #     else:
        #         phi1t[np.argmin(la.norm(local_zenith_vect-(-ps_vec), axis = -1))] = np.array([0, 1, 0])
        # phi1t = phi1t / (la.norm(phi1t, axis=-1)[:,None])
        #
        # Ranglet = np.arctan2(phi1t.dot(alpha0), phi1t.dot(phi0))#rotation angle for polarization coord over t, from equatotial(phi0,alpha0) to local(phi1,alpha1), around vector -ps_vec
        if np.abs(ps_vec[-1]) == 1:
            ps_equ_north_plane = np.array([0, 1, 0])
        else:

            ps_equ_north_plane = np.cross([0, 0, 1], ps_vec)
            ps_equ_north_plane /= la.norm(ps_equ_north_plane)#normal vector of the plane defined by point source vec and north vec in equatorial

        ps_local_north_plane_t = np.cross(local_zenith_vect, ps_vec)
        ps_local_north_plane_t /= la.norm(ps_local_north_plane_t, axis=-1)[:, None]#normal vector of the plane defined by point source vec and north vec in local coord
        Ranglet = -np.sign(np.cross(ps_local_north_plane_t, ps_equ_north_plane).dot(ps_vec)) * np.arccos(ps_local_north_plane_t.dot(ps_equ_north_plane))



        Ranglet = rotatez_matrix(Ranglet)[:2,:2]#rotation matrix around -ps_vec for polarization coord over t, 3 by 3 by t

        BRt = np.array([beamt[..., i].dot(Ranglet[...,i]) for i in range(len(tlist))])

        if verbose:
            print "R"
            print Ranglet
            print "B"
            print BRt

        result = np.empty((len(d_in), 4 * len(tlist), 4), dtype='complex64')#time is fastest changing in 4 by t
        for truen, (truei, truej) in enumerate([[0,0],[0,1],[1,0],[1,1]]):
            for measuren, (measurei, measurej) in enumerate([[0,0],[0,1],[1,0],[1,1]]):
                result[:, measuren*len(tlist):(measuren+1)*len(tlist), truen] = (BRt[:, measurei,truei] * np.conjugate(BRt[:, measurej,truej]))[None,:]*fringe
        return result
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()


###UBL####

ubls = {}
for p in ['x', 'y']:
    ubl_filename = datadir + tag + '_%s%s_%i_%i.ubl' % (p, p, nUBL, 3)
    ubls[p] = np.fromfile(ubl_filename, dtype='float32').reshape((nUBL, 3))
common_ubls = np.array([u for u in ubls['x'] if (u in ubls['y'] or -u in ubls['y'])])
#manually filter UBLs
used_common_ubls = common_ubls#[np.argsort(la.norm(common_ubls, axis=-1))[10:]]     #remove shorted 10

ubl_index = {}  # stored index in each pol's ubl for the common ubls
Exemplo n.º 18
0
        new_phi_arr[i] = -phi_arr[i]
    else:
        new_phi_arr[i] = 360 - phi_arr[i]

pix = np.zeros(len(new_theta_arr))
for i in range(len(new_theta_arr)):
    pix[i] = hp.ang2pix(NSIDE, new_phi_arr[i], new_theta_arr[i], lonlat=True)
    sky_map[int(pix[i])] = eff_area_arr[i]

NSIDE_NEW = 64

all_pix_ids = np.arange(0, hp.nside2npix(NSIDE_NEW))

all_theta, all_phi = hp.pix2ang(NSIDE_NEW, all_pix_ids, lonlat=True)

new_map = get_interp_val(sky_map, all_theta, all_phi, lonlat=True)

print len(new_map)
hp.mollview(new_map, rot=(0, 90), cmap="gist_earth_r", cbar=False)
hp.graticule()
hp.projtext(0, 0, "X", fontsize=15, lonlat=True)
hp.projtext(-90, 0, "Y", fontsize=15, lonlat=True)
plt.title("All sky sensitivity at 120keV")
fig = plt.gcf()
ax = plt.gca()
image = ax.get_images()[0]
cbar = fig.colorbar(image,
                    ax=ax,
                    orientation="horizontal",
                    fraction=0.046,
                    pad=0.04)
Exemplo n.º 19
0
    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()
equ2013_to_gal_matrix = hp.rotator.Rotator(coord='cg').mat.dot(
    sv.epoch_transmatrix(2000, stdtime=2013.8))
ang0, ang1 = hp.rotator.rotateDirection(
    equ2013_to_gal_matrix,
    hpf.pix2ang(nside_standard, range(12 * nside_standard**2)))
equatorial_GSM_standard = hpf.get_interp_val(gsm_standard, ang0, ang1)
print "done."
sys.stdout.flush()

sim_data_clean = A.dot(equatorial_GSM_standard[pix_mask])
noise_data = np.random.randn(len(data)) / Ni**.5
sim_data = sim_data_clean + noise_data
sim_sol = np.zeros(12 * nside**2, dtype='float32')
sim_sol[pix_mask] = AtNiAi.dot(AtNi.dot(sim_data))
noise_sol = np.zeros(12 * nside**2, dtype='float32')
noise_sol[pix_mask] = AtNiAi.dot(AtNi.dot(noise_data))
sim_sol_clean = np.zeros(12 * nside**2, dtype='float32')
sim_sol_clean[pix_mask] = AtNiAi.dot(AtNi.dot(sim_data_clean))

chisq = np.sum(Ni * np.abs(
    (A.dot(x[pix_mask]) - data))**2) / float(len(data) - npix)
    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' +
            str(nside))
        gsm = 422.952 * (0.307706 * pca1 + -0.281772 * pca2 + 0.0123976 * pca3)
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
        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.))


Exemplo n.º 23
0
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]))])
qaz = Ni * sim_data
Exemplo n.º 24
0
            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)
Exemplo n.º 25
0
x[pix_mask] = AtNiAi.dot(AtNi.dot(data))


#simulate
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()
equ2013_to_gal_matrix = hp.rotator.Rotator(coord='cg').mat.dot(sv.epoch_transmatrix(2000,stdtime=2013.8))
ang0, ang1 =hp.rotator.rotateDirection(equ2013_to_gal_matrix, hpf.pix2ang(nside_standard, range(12*nside_standard**2)))
equatorial_GSM_standard = hpf.get_interp_val(gsm_standard, ang0, ang1)
print "done."
sys.stdout.flush()

sim_data_clean = A.dot(equatorial_GSM_standard[pix_mask])
noise_data = np.random.randn(len(data))/Ni**.5
sim_data = sim_data_clean + noise_data
sim_sol = np.zeros(12*nside**2, dtype='float32')
sim_sol[pix_mask] = AtNiAi.dot(AtNi.dot(sim_data))
noise_sol = np.zeros(12*nside**2, dtype='float32')
noise_sol[pix_mask] = AtNiAi.dot(AtNi.dot(noise_data))
sim_sol_clean = np.zeros(12*nside**2, dtype='float32')
sim_sol_clean[pix_mask] = AtNiAi.dot(AtNi.dot(sim_data_clean))

chisq = np.sum(Ni * np.abs((A.dot(x[pix_mask]) - data))**2) / float(len(data) - npix)
chisq_sim = np.sum(Ni * np.abs((A.dot(sim_sol[pix_mask]) - sim_data))**2) / float(len(sim_data) - npix)
            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])
gsm_standard = np.exp(scale_loglog(np.log(freq))) * (w1(freq) * pca1 + w2(freq) * pca2 + w3(freq) * pca3)
print "done."
sys.stdout.flush()
#hpv.mollview(beam_weight, nest=True)
#plt.show()
#quit()

################################################
#####################GSM###########################
#############################################
#rotate sky map and converts to nest
equatorial_GSM_standard_2000 = np.fromfile(datadir + 'skymap_mwacs_nside256_float32.dat', dtype='float32')
print "Rotating GSM_standard and converts to nest...",
sys.stdout.flush()
equ2013_to_equ2000_matrix = sv.epoch_transmatrix(2000,stdtime=2013.64)
ang0, ang1 =hp.rotator.rotateDirection(equ2013_to_equ2000_matrix, hpf.pix2ang(nside_standard, range(12*nside_standard**2), nest=True))
equatorial_GSM_standard = hpf.get_interp_val(equatorial_GSM_standard_2000, ang0, ang1)
print "done."
sys.stdout.flush()
#hpv.mollview(np.log10(equatorial_GSM_standard), min=-3.5,max=1.5, coord=plotcoord, title='GSM', nest=True)
#plt.show()
#quit()

########################################################################
########################processing dynamic pixelization######################
########################################################################

nside_distribution = np.zeros(12*nside_standard**2)
final_index = np.zeros(12*nside_standard**2)
thetas, phis, sizes = [], [], []
abs_thresh = np.mean(equatorial_GSM_standard * beam_weight) * thresh
pixelize(equatorial_GSM_standard * beam_weight, nside_distribution, nside_standard, nside_start, abs_thresh, final_index, thetas, phis, sizes)
        beam_healpix={}
        beam_healpix['x'] = abs(local_beam(freqs[pick_f])[0])**2 + abs(local_beam(freqs[pick_f])[1])**2
        beam_healpix['y'] = abs(local_beam(freqs[pick_f])[2])**2 + abs(local_beam(freqs[pick_f])[3])**2

        ###################################
        ###########simulate##
        cal_sources = ['cyg', 'cas']
        ps_visibilities = {}
        for source in cal_sources:
            phis = PI - southern_points[source]['pos'][:, 1]
            thetas = PI/2 - southern_points[source]['pos'][:, 0]
            ks = np.array([np.cos(phis)*np.sin(thetas), np.sin(phis)*np.sin(thetas), np.cos(thetas)])
            fringes = np.exp(1.j * TPI/lambdas[pick_f] * (info['ubl'].dot(correction_mat)).dot(ks)).transpose()
            ps_visibilities[source] = {}
            for pol in ['xx', 'yy']:
                b_values = hpf.get_interp_val(beam_healpix[pol[0]], thetas, phis)
                ps_visibilities[source][pol] = b_values[:, None]*fringes*flux_func[source](freqs[pick_f])/2


        ###################################
        ##########calibrate
        #################################


        ##This part decides which lst_range and min ubl length to use
        rawdata = {}
        simdata = {}
        rawdata['xx'] = np.copy(compressed_data[0,~compr_flag[:, pick_f],pick_f])
        rawdata['yy'] = np.copy(compressed_data[3,~compr_flag[:, pick_f],pick_f])
        for pol in ['xx', 'yy']:
            simdata[pol] = np.sum([ps_visibilities[source][pol] for source in cal_sources], axis = 0)