Пример #1
0
    def GiveBeamMeanAllFreq(self, times, NTimes=None):
        if self.GD["Beam"]["BeamModel"] == "LOFAR":
            useArrayFactor = ("A" in self.GD["Beam"]["LOFARBeamMode"])
            useElementBeam = ("E" in self.GD["Beam"]["LOFARBeamMode"])
            self.MS.LoadSR(useElementBeam=useElementBeam,
                           useArrayFactor=useArrayFactor)
        elif self.GD["Beam"]["BeamModel"] == "FITS" or self.GD["Beam"][
                "BeamModel"] == "ATCA":
            self.MS.LoadDDFBeam()

        tmin, tmax = times[0], times[-1]
        if NTimes is None:
            DtBeamSec = self.DtBeamMin * 60
            DtBeamMin = self.DtBeamMin
        else:
            DtBeamSec = (tmax - tmin) / (NTimes + 1)
            DtBeamMin = DtBeamSec / 60
        log.print("  Update beam [Dt = %3.1f min] ... " % DtBeamMin)

        TimesBeam = np.arange(tmin, tmax, DtBeamSec).tolist()
        if not (tmax in TimesBeam): TimesBeam.append(tmax)
        TimesBeam = np.array(TimesBeam)
        T0s = TimesBeam[:-1]
        T1s = TimesBeam[1:]
        Tm = (T0s + T1s) / 2.
        RA, DEC = self.SM.ClusterCat.ra, self.SM.ClusterCat.dec
        NDir = RA.size
        Beam = np.zeros((Tm.size, NDir, self.MS.na, self.MS.NSPWChan, 2, 2),
                        np.complex64)
        for itime in range(Tm.size):
            ThisTime = Tm[itime]
            Beam[itime] = self.MS.GiveBeam(ThisTime, RA, DEC)

        ###### Normalise
        rac, decc = self.MS.OriginalRadec
        if self.GD["Beam"]["CenterNorm"] == 1:
            for itime in range(Tm.size):
                ThisTime = Tm[itime]
                Beam0 = self.MS.GiveBeam(ThisTime, np.array([rac]),
                                         np.array([decc]))
                Beam0inv = ModLinAlg.BatchInverse(Beam0)
                nd, _, _, _, _ = Beam[itime].shape
                Ones = np.ones((nd, 1, 1, 1, 1), np.float32)
                Beam0inv = Beam0inv * Ones
                Beam[itime] = ModLinAlg.BatchDot(Beam0inv, Beam[itime])
        ######

        nt, nd, na, nch, _, _ = Beam.shape
        Beam = np.mean(Beam, axis=3).reshape((nt, nd, na, 1, 2, 2))

        DicoBeam = {}
        DicoBeam["t0"] = T0s
        DicoBeam["t1"] = T1s
        DicoBeam["tm"] = Tm
        DicoBeam["Jones"] = Beam
        return DicoBeam
Пример #2
0
    def ApplyCal(self, DicoData, ApplyTimeJones, iCluster):
        D = ApplyTimeJones
        Beam = D["Beam"]
        BeamH = D["BeamH"]
        lt0, lt1 = D["t0"], D["t1"]
        ColOutDir = DicoData["data"]
        A0 = DicoData["A0"]
        A1 = DicoData["A1"]
        times = DicoData["times"]
        na = int(DicoData["infos"][0])

        # nt,nd,nd,nchan,_,_=Beam.shape
        # med=np.median(np.abs(Beam))
        # Threshold=med*1e-2

        for it in range(lt0.size):
            t0, t1 = lt0[it], lt1[it]
            ind = np.where((times >= t0) & (times < t1))[0]
            if ind.size == 0: continue
            data = ColOutDir[ind]
            # flags=DicoData["flags"][ind]
            A0sel = A0[ind]
            A1sel = A1[ind]
            #print("CACA",ChanMap)
            if "ChanMap" in ApplyTimeJones.keys():
                ChanMap = ApplyTimeJones["ChanMap"]
            else:
                ChanMap = range(nf)

            for ichan in range(len(ChanMap)):
                JChan = ChanMap[ichan]
                if iCluster != -1:
                    J0 = Beam[it, iCluster, :, JChan, :, :].reshape((na, 4))
                    JH0 = BeamH[it, iCluster, :, JChan, :, :].reshape((na, 4))
                else:
                    J0 = np.mean(Beam[it, :, :, JChan, :, :], axis=1).reshape(
                        (na, 4))
                    JH0 = np.mean(BeamH[it, :, :, JChan, :, :],
                                  axis=1).reshape((na, 4))

                J = ModLinAlg.BatchInverse(J0)
                JH = ModLinAlg.BatchInverse(JH0)

                data[:, ichan, :] = ModLinAlg.BatchDot(J[A0sel, :],
                                                       data[:, ichan, :])
                data[:, ichan, :] = ModLinAlg.BatchDot(data[:, ichan, :],
                                                       JH[A1sel, :])

                # Abs_g0=(np.abs(J0[A0sel,0])<Threshold)
                # Abs_g1=(np.abs(JH0[A1sel,0])<Threshold)
                # flags[Abs_g0,ichan,:]=True
                # flags[Abs_g1,ichan,:]=True

            ColOutDir[ind] = data[:]
Пример #3
0
    def ApplyCal(self, DicoData, ApplyTimeJones, iCluster):
        D = ApplyTimeJones
        Jones = D["Jones"]
        JonesH = D["JonesH"]
        lt0, lt1 = D["t0"], D["t1"]
        ColOutDir = DicoData["data"]
        A0 = DicoData["A0"]
        A1 = DicoData["A1"]
        times = DicoData["times"]
        na = int(DicoData["infos"][0])

        for it in range(lt0.size):

            t0, t1 = lt0[it], lt1[it]
            ind = np.where((times >= t0) & (times < t1))[0]

            if ind.size == 0: continue
            data = ColOutDir[ind]
            # flags=DicoData["flags"][ind]
            A0sel = A0[ind]
            A1sel = A1[ind]

            if "Map_VisToJones_Freq" in ApplyTimeJones.keys():
                ChanMap = ApplyTimeJones["Map_VisToJones_Freq"]
            else:
                ChanMap = range(nf)

            for ichan in range(len(ChanMap)):
                JChan = ChanMap[ichan]
                if iCluster != -1:
                    J0 = Jones[it, iCluster, :, JChan, :, :].reshape((na, 4))
                    JH0 = JonesH[it, iCluster, :, JChan, :, :].reshape((na, 4))
                else:
                    J0 = np.mean(Jones[it, :, :, JChan, :, :], axis=1).reshape(
                        (na, 4))
                    JH0 = np.mean(JonesH[it, :, :, JChan, :, :],
                                  axis=1).reshape((na, 4))

                J = ModLinAlg.BatchInverse(J0)
                JH = ModLinAlg.BatchInverse(JH0)

                data[:, ichan, :] = ModLinAlg.BatchDot(J[A0sel, :],
                                                       data[:, ichan, :])
                data[:, ichan, :] = ModLinAlg.BatchDot(data[:, ichan, :],
                                                       JH[A1sel, :])

                # Abs_g0=(np.abs(J0[A0sel,0])<Threshold)
                # Abs_g1=(np.abs(JH0[A1sel,0])<Threshold)
                # flags[Abs_g0,ichan,:]=True
                # flags[Abs_g1,ichan,:]=True

            ColOutDir[ind] = data[:]
Пример #4
0
    def PrepareJHJ_EKF(self,Pa_in,rms):
        self.L_JHJinv=[]
        incr=1
        # pylab.figure(1)
        # pylab.clf()
        # pylab.imshow(np.abs(self.JHJ),interpolation="nearest")
        # pylab.draw()
        # pylab.show(False)
        # pylab.pause(0.1)

        for ipol in range(self.NJacobBlocks_X):
            PaPol=self.GivePaPol(Pa_in,ipol)
            Pinv=ModLinAlg.invSVD(PaPol)
            JHJ=self.L_JHJ[ipol]#*(1./rms**2)
            JHJ+=Pinv
            if self.DoReg:
                JHJ+=self.LQxInv[ipol]*(self.gamma**2)
            JHJinv=ModLinAlg.invSVD(JHJ)
            self.L_JHJinv.append(JHJinv)
Пример #5
0
    def PrepareJHJ_LM(self):

        self.L_JHJinv=[]
        if self.DataAllFlagged:
            return

        for ipol in range(self.NJacobBlocks_X):

            M=self.L_JHJ[ipol]
            if self.DoTikhonov:
                self.LambdaTkNorm=self.LambdaTk*np.mean(np.abs(np.diag(M)))
                
                # Lin.shape= (self.NDir,self.NJacobBlocks_X,self.NJacobBlocks_Y)
                Linv=np.diag(self.Linv[:,ipol,:].ravel())
                Linv*=self.LambdaTkNorm/(1.+self.LambdaLM)
                M2=M+Linv

                # pylab.clf()
                # pylab.subplot(1,2,1)
                # pylab.imshow(np.abs(M),interpolation="nearest")
                # pylab.colorbar()
                # pylab.subplot(1,2,2)
                # pylab.imshow(np.abs(Linv),interpolation="nearest")
                # pylab.colorbar()
                # pylab.draw()
                # pylab.show(False)
                # pylab.pause(0.1)
                # stop

            else:
                M2=M


            JHJinv=ModLinAlg.invSVD(M2)
            #JHJinv=ModLinAlg.invSVD(self.JHJ)
            self.L_JHJinv.append(JHJinv)
Пример #6
0
    def Stack_SingleTime(self,iTime,iFreq):
            

        ra=self.PosArray.ra[0]
        dec=self.PosArray.dec[0]

        l, m = self.radec2lm(ra, dec)
        n  = np.sqrt(1. - l**2. - m**2.)
        self.DicoDATA.reload()
        self.DicoGrids.reload()
        
        #indRow = np.where((self.DicoDATA["times"]==self.times[iTime]))[0]
        ch0,ch1=self.DicoGrids["DomainEdges_Freq"][iFreq],self.DicoGrids["DomainEdges_Freq"][iFreq+1]
        #print(ch0,ch1)
        t0,t1=self.DicoGrids["DomainEdges_Time"][iTime],self.DicoGrids["DomainEdges_Time"][iTime+1]
        try:
            indRow=np.where((self.DicoDATA["times"]>=t0)&(self.DicoDATA["times"]<t1))[0]
        except:
            print(it0,it1)
            print(it0,it1)
            print(it0,it1)
            print(it0,it1)
            print(it0,it1)
            print(it0,it1)
            print(it0,it1)
            #indRow = np.where((self.DicoDATA["times"]==self.times[it0]))[0]
        
        f   = self.DicoDATA["flag"][indRow, ch0:ch1, :]
        d   = self.DicoDATA["data"][indRow, ch0:ch1, :]
        dp   = self.DicoDATA["data_p"][indRow, ch0:ch1, :]
        nrow,nch,_=d.shape
        weights   = (self.DicoDATA["weights"][indRow, ch0:ch1]).reshape((nrow,nch,1))
        A0s = self.DicoDATA["A0"][indRow]
        A1s = self.DicoDATA["A1"][indRow]
        u0  = self.DicoDATA["u"][indRow].reshape((-1,1,1))
        v0  = self.DicoDATA["v"][indRow].reshape((-1,1,1))
        w0  = self.DicoDATA["w"][indRow].reshape((-1,1,1))
        times=self.DicoDATA["times"][indRow]
        NTimesBin=np.unique(times).size
        if NTimesBin==0:
            return
        try:
            i_TimeBin=np.int64(np.argmin(np.abs(times.reshape((-1,1))-np.sort(np.unique(times)).reshape((1,-1))),axis=1).reshape(-1,1))*np.ones((1,nch))
        except:
            print(iTime,iFreq)
            print(iTime,iFreq)
            print(iTime,iFreq)
            print(iTime,iFreq)
            
        i_ch=np.int64((np.arange(nch).reshape(1,-1))*np.ones((nrow,1)))
        i_A0=A0s.reshape((-1,1))*np.ones((1,nch))
        i_A1=A1s.reshape((-1,1))*np.ones((1,nch))

        
        i_TimeBin=np.int64(i_TimeBin).ravel()
        i_ch=np.int64(i_ch).ravel()
        i_A0=np.int64(i_A0).ravel()
        i_A1=np.int64(i_A1).ravel()
        na=self.na
        
        def Give_R(din):
            d0=(din[:,:,0]+din[:,:,-1])/2.
            R=np.zeros((NTimesBin*nch*na,na),din.dtype)
            
            ind0=i_TimeBin * nch*na**2 + i_ch * na**2 + i_A0 * na + i_A1
            ind1=i_TimeBin * nch*na**2 + i_ch * na**2 + i_A1 * na + i_A0
            
            R.flat[ind0]=d0.flat[:]
            R.flat[ind1]=d0.conj().flat[:]
            f0=np.ones((R.shape),dtype=np.float64)
            f0[R==0]=0

            # import pylab
            # R_=R.reshape((NTimesBin,nch,na,na))
            # Rf_=f0.reshape((NTimesBin,nch,na,na))
            # for iT in range(NTimesBin):
            #     for iF in range(nch):
            #         print(iT,iF)
            #         C=R_[iT,iF]
            #         Cn=Rf_[iT,iF]
            #         pylab.clf()
            #         pylab.subplot(1,2,1)
            #         pylab.imshow(np.abs(C))
            #         pylab.subplot(1,2,2)
            #         pylab.imshow(Cn)
            #         pylab.draw()
            #         pylab.show(block=False)
            #         pylab.pause(0.1)
            #         stop
            return R,f0
        
        iMS  = self.DicoDATA["iMS"]
        
        chfreq=self.DicoMSInfos[iMS]["ChanFreq"].reshape((1,-1,1))
        chfreq_mean=np.mean(chfreq)
        kk  = np.exp(-2.*np.pi*1j* chfreq/const.c.value *(u0*l + v0*m + w0*(n-1)) ) # Phasing term

        # #ind=np.where((A0s==0)&(A1s==10))[0]
        # ind=np.where((A0s!=1000))[0]
        # import pylab
        # pylab.ion()
        # pylab.clf()
        # pylab.plot(np.angle(d[ind,2,0]))
        # pylab.plot(np.angle(kk[ind,2,0].conj()))
        # pylab.draw()
        # pylab.show(False)
        # pylab.pause(0.1)

        
        f0, _ = self.Freq_minmax
        
        DicoMSInfos      = self.DicoMSInfos
        
        #_,nch,_=self.DicoDATA["data"].shape

        dcorr=d
        dcorr[f==1]=0
        
        if self.DoJonesCorr_kMS:
            self.DicoJones_kMS.reload()
            tm = self.DicoJones_kMS['tm']
            # Time slot for the solution
            iTJones=np.argmin(np.abs(tm-self.times[iTime]))
            iDJones=np.argmin(AngDist(ra,self.DicoJones_kMS['ra'],dec,self.DicoJones_kMS['dec']))
            _,nchJones,_,_,_,_=self.DicoJones_kMS['G'].shape
            for iFJones in range(nchJones):
                nu0,nu1=self.DicoJones_kMS['FreqDomains'][iFJones]
                fData=self.DicoMSInfos[iMS]["ChanFreq"].ravel()
                indCh=np.where((fData>=nu0) & (fData<nu1))[0]
                #iFJones=np.argmin(np.abs(chfreq_mean-self.DicoJones_kMS['FreqDomains_mean']))
                # construct corrected visibilities
                J0 = self.DicoJones_kMS['G'][iTJones, iFJones, A0s, iDJones, 0, 0]
                J1 = self.DicoJones_kMS['G'][iTJones, iFJones, A1s, iDJones, 0, 0]
                J0 = J0.reshape((-1, 1, 1))*np.ones((1, indCh.size, 1))
                J1 = J1.reshape((-1, 1, 1))*np.ones((1, indCh.size, 1))
                #dcorr[:,indCh,:] = J0.conj() * dcorr[:,indCh,:] * J1
                dcorr[:,indCh,:] = 1./J0 * dcorr[:,indCh,:] * 1./J1.conj()
            # iFJones=np.argmin(np.abs(chfreq_mean-self.DicoJones_kMS['FreqDomains_mean']))
            # # construct corrected visibilities
            # J0 = self.DicoJones_kMS['G'][iTJones, iFJones, A0s, iDJones, 0, 0]
            # J1 = self.DicoJones_kMS['G'][iTJones, iFJones, A1s, iDJones, 0, 0]
            # J0 = J0.reshape((-1, 1, 1))*np.ones((1, nch, 1))
            # J1 = J1.reshape((-1, 1, 1))*np.ones((1, nch, 1))
            # dcorr = J0.conj() * dcorr * J1

        if self.DoJonesCorr_Beam:
            self.DicoJones_Beam.reload()
            tm = self.DicoJones_Beam['tm']
            # Time slot for the solution
            iTJones=np.argmin(np.abs(tm-self.times[iTime]))
            iDJones=np.argmin(AngDist(ra,self.DicoJones_Beam['ra'],dec,self.DicoJones_Beam['dec']))
            _,nchJones,_,_,_,_=self.DicoJones_Beam['G'].shape
            for iFJones in range(nchJones):
                nu0,nu1=self.DicoJones_Beam['FreqDomains'][iFJones]
                fData=self.DicoMSInfos[iMS]["ChanFreq"].ravel()
                indCh=np.where((fData>=nu0) & (fData<nu1))[0]
                #iFJones=np.argmin(np.abs(chfreq_mean-self.DicoJones_Beam['FreqDomains_mean']))
                # construct corrected visibilities
                J0 = self.DicoJones_Beam['G'][iTJones, iFJones, A0s, iDJones, 0, 0]
                J1 = self.DicoJones_Beam['G'][iTJones, iFJones, A1s, iDJones, 0, 0]
                J0 = J0.reshape((-1, 1, 1))*np.ones((1, indCh.size, 1))
                J1 = J1.reshape((-1, 1, 1))*np.ones((1, indCh.size, 1))
                #dcorr[:,indCh,:] = J0.conj() * dcorr[:,indCh,:] * J1
                dcorr[:,indCh,:] = 1./J0 * dcorr[:,indCh,:] * 1./J1.conj()


        RMS=scipy.stats.median_abs_deviation((dcorr[dcorr!=0]).ravel(),scale="normal")
        df=(dcorr[dcorr!=0]).ravel()
        if df.size>100:
            RMS=np.sqrt(np.abs(np.sum(df*df.conj()))/df.size)
        
        dp[dp==0]=1.
        
        # #dcorr/=dp# *= kk
        # def Give_r(din,iAnt,pol):
        #     ind=np.where(A0s==iAnt)[0]
        #     d0=din[ind,:,pol].ravel()
        #     ind=np.where(A1s==iAnt)[0]
        #     d1=din[ind,:,pol].conj().ravel()
        #     return np.concatenate([d0,d1]).ravel()
        # def Give_R(din,pol):
        #     r0=Give_r(din,0,pol)
        #     R=np.zeros((r0.size,self.na),dtype=r0.dtype)
        #     R[:,0]=r0
        #     for iAnt in range(1,self.na):
        #         R[:,iAnt]=Give_r(din,iAnt,pol)
        #     Rf=np.ones(R.shape,np.float64)
        #     Rf[R==0]=0
        #     return R,Rf
        # R=(Give_R(dcorr,0)+Give_R(dcorr,-1))/2.
        
        #        if (Rf[Rf==1]).size<100:
        #            return
        R,Rf=Give_R(dcorr)
        
        C=np.dot(R.T.conj(),R)
        Cn=np.dot(Rf.T,Rf)
        Cn[Cn==0]=1.
        C/=Cn

        #Rp=(Give_R(dp,0)+Give_R(dp,-1))/2.
        Rp,Rpf=Give_R(dp)
        
        #Rpf=np.ones(Rp.shape,np.float64)
        #Rpf[Rp==0]=0
        
        Cp=np.dot(Rp.T.conj(),Rp)
        Cpn=np.dot(Rpf.T,Rpf)
        Cpn[Cpn==0]=1.
        Cp/=Cpn

        # print(np.unique(Cn))
        # print(np.unique(Cn))
        # print(np.unique(Cn))
        
        
        # RMS=self.DicoDATA["RMS"]**2
        
        
        # II=np.diag(RMS**2*np.ones((self.na,),C.dtype))
        # C=C-II

        #C=self.na*C

        # # ################################
        # Cr=np.load("Cr.npy")
        # Cr2=np.abs(np.dot(Cr.T.conj(),Cr))
        # Crn=np.ones_like(Cr)
        # Crn[Cr==0]=0
        # Crn2=np.abs(np.dot(Crn.T.conj(),Crn))
        # Cr2/=Crn2
        
        # R_=R.reshape((NTimesBin,nch,na,na))
        # Rf_=Rf.reshape((NTimesBin,nch,na,na))
        # import pylab
        # pylab.clf()
        # pylab.subplot(2,3,1)
        # pylab.imshow(Cr2)#,vmin=v0,vmax=v1)
        # pylab.title("Cr2 Sim")
        # pylab.colorbar()
        # pylab.subplot(2,3,2)
        # pylab.imshow(np.abs(C))#,vmin=v0,vmax=v1)
        # pylab.title("C2 Solve")
        # pylab.colorbar()
        # pylab.subplot(2,3,3)
        # pylab.imshow(np.abs(Cr2)-np.abs(C))#,vmin=v0,vmax=v1)
        # pylab.title("diff")
        
        # pylab.subplot(2,3,4)
        # pylab.imshow(np.abs(Cr))#,vmin=v0,vmax=v1)
        # pylab.title("Cr Sim")
        # pylab.colorbar()
        # c=R_[0,0]
        # c2=c#np.abs(np.dot(c.T.conj(),c))
        # pylab.subplot(2,3,5)
        # pylab.imshow(np.abs(c))#,vmin=v0,vmax=v1)
        # pylab.title("single timefreq")
        # pylab.colorbar()
        # pylab.draw()
        # pylab.show(block=False)
        # pylab.pause(0.1)
        # stop
        # # ################################
        
        diagC=np.diag(C)
        ind=np.where(diagC<=0.)[0]
        for i in ind:
            C[i,i]=0.
            
        
        # C/=Cp
            

        C=np.abs(C)
        sqrtC=ModLinAlg.sqrtSVD(C[:,:],Rank=1)
        #sqrtC=ModLinAlg.sqrtSVD(C[:,:])
        #C=np.abs(np.dot(sqrtC.T.conj(),sqrtC))

        #C=sqrtC
        self.DicoGrids["GridC2"][iTime, iFreq, :,:]=C[:,:]
        self.DicoGrids["GridC"][iTime, iFreq, :,:]=sqrtC[:,:]

        #C=ModLinAlg.invSVD(self.DicoGrids["GridSTD"][iTime, :,:])
        Want=np.sum(C,axis=0)

        _,nch,_=self.DicoDATA["data"][indRow,ch0:ch1].shape
        WOUT=self.DicoDATA["WOUT"][indRow,ch0:ch1]
        A0s = self.DicoDATA["A0"][indRow]
        A1s = self.DicoDATA["A1"][indRow]

        
        # w0=Want[A0s]
        # w1=Want[A1s]
        # w=w0*w1
        # w/=np.median(w)
        # w[w<=0]=0
        # w[w>2.]=2

        # # ###############################
        # Cr=np.load("Cr.npy")
        # Cr2=np.dot(Cr.T.conj(),Cr)
        # C=Cr2
        # sqrtC=np.abs(Cr)
        # sqrtC=np.abs(ModLinAlg.sqrtSVD(Cr2,Rank=1))
        # # ###############################

        Rp,Rpf=Give_R(dp)
        
        R_=R.reshape((NTimesBin,nch,na,na))
        Rf_=Rf.reshape((NTimesBin,nch,na,na))  
        Rp_=Rp.reshape((NTimesBin,nch,na,na))
        Rpf_=Rpf.reshape((NTimesBin,nch,na,na))
        Rp_[Rf_==0]=1.
        Rp/=np.abs(Rp)
        # R_/=Rp_
        
        V=np.sum(np.sum(R_*R_.conj(),axis=0),axis=0)
        Vn=np.sum(np.sum(Rf_*Rf_.conj(),axis=0),axis=0)
        Vn[V==0]=1.
        V/=Vn
        C=V
        
        V=C[A0s,A1s]
        #V=sqrtC[A0s,A0s]+sqrtC[A1s,A1s]
        #V=C[A0s,A0s]+C[A1s,A1s]
        #V=sqrtC[A0s,A1s]
        ind=(V==0)
        V[ind]=1e10
        
        RMS=0.
        w=(1./np.abs(np.abs(V)))#+RMS**2)
        w[ind]=0
        
        #w/=np.median(w)
        #w[w<=0]=0
        #w[w>2.]=2

        for ii,iRow in enumerate(indRow):
            for ich in np.arange(ch0,ch1):
                self.DicoDATA["WOUT"][iRow,ich]=w[ii]
Пример #7
0
    def MergeJones(self, DicoJ0, DicoJ1):

        print >> log, "Merging Jones arrays"

        FreqDomainOut = self.GetMergedFreqDomains(DicoJ0, DicoJ1)

        DicoOut = {}
        DicoOut["t0"] = []
        DicoOut["t1"] = []
        DicoOut["tm"] = []
        DicoOut["FreqDomains"] = FreqDomainOut
        T0 = np.min([DicoJ0["t0"][0], DicoJ1["t0"][0]])
        it = 0
        CurrentT0 = T0

        while True:
            T0 = CurrentT0

            dT0 = DicoJ0["t1"] - T0
            dT0 = dT0[dT0 > 0]
            dT1 = DicoJ1["t1"] - T0
            dT1 = dT1[dT1 > 0]
            if (dT0.size == 0) & (dT1.size == 0):
                break
            elif dT0.size == 0:
                dT = dT1[0]
            elif dT1.size == 0:
                dT = dT0[0]
            else:
                dT = np.min([dT0[0], dT1[0]])

            DicoOut["t0"].append(CurrentT0)
            T1 = T0 + dT
            DicoOut["t1"].append(T1)
            Tm = (T0 + T1) / 2.
            DicoOut["tm"].append(Tm)
            CurrentT0 = T1
            it += 1

        DicoOut["t0"] = np.array(DicoOut["t0"])
        DicoOut["t1"] = np.array(DicoOut["t1"])
        DicoOut["tm"] = np.array(DicoOut["tm"])

        nt0 = DicoJ0["t0"].size
        nt1 = DicoJ1["t0"].size

        fm0 = np.mean(DicoJ0["FreqDomains"], axis=1)
        fm1 = np.mean(DicoJ1["FreqDomains"], axis=1)
        fmOut = np.mean(DicoOut["FreqDomains"], axis=1)

        _, nd, na, _, _, _ = DicoJ0["Jones"].shape
        nt = DicoOut["tm"].size

        nchOut = fmOut.size

        DicoOut["Jones"] = np.zeros((nt, nd, na, nchOut, 2, 2), np.complex64)
        DicoOut["Jones"][:, :, :, :, 0, 0] = 1.
        DicoOut["Jones"][:, :, :, :, 1, 1] = 1.

        iG0_t = np.argmin(np.abs(DicoOut["tm"].reshape((nt, 1)) -
                                 DicoJ0["tm"].reshape((1, nt0))),
                          axis=1)
        iG1_t = np.argmin(np.abs(DicoOut["tm"].reshape((nt, 1)) -
                                 DicoJ1["tm"].reshape((1, nt1))),
                          axis=1)

        #        print>>log,fmOut,DicoJ0["FreqDomain"],DicoJ1["FreqDomain"]
        for ich in range(nchOut):

            #            print>>log,fmOut[ich]
            indChG0 = np.where((fmOut[ich] >= DicoJ0["FreqDomains"][:, 0])
                               & (fmOut[ich] < DicoJ0["FreqDomains"][:, 1]))[0]
            if indChG0.size == 0: continue
            indChG0 = indChG0[0]

            indChG1 = np.where((fmOut[ich] >= DicoJ1["FreqDomains"][:, 0])
                               & (fmOut[ich] < DicoJ1["FreqDomains"][:, 1]))[0]
            if indChG1.size == 0: continue
            indChG1 = indChG1[0]

            for itime in range(nt):
                G0 = DicoJ0["Jones"][iG0_t[itime], :, :, indChG0, :, :]
                G1 = DicoJ1["Jones"][iG1_t[itime], :, :, indChG1, :, :]
                DicoOut["Jones"][itime, :, :,
                                 ich, :, :] = ModLinAlg.BatchDot(G0, G1)

        return DicoOut
Пример #8
0
    def Stack_SingleTime(self, iA0, iA1):

        self.DicoDATA.reload()
        self.DicoGrids.reload()
        DicoMSInfos = self.DicoMSInfos

        #indRow = np.where((self.DicoDATA["times"]==self.times[iTime]))[0]
        #ch0,ch1=self.DicoGrids["DomainEdges_Freq"][iFreq],self.DicoGrids["DomainEdges_Freq"][iFreq+1]
        nrow, nch, _ = self.DicoDATA["data"].shape
        ch0, ch1 = 0, nch

        #print(ch0,ch1)
        C0 = (self.DicoDATA["A0"] == iA0) & (self.DicoDATA["A1"] == iA1)
        indRow = np.where(C0)[0]

        f = self.DicoDATA["flag"][indRow, ch0:ch1, :]
        d = self.DicoDATA["data"][indRow, ch0:ch1, :]
        dp = self.DicoDATA["data_p"][indRow, ch0:ch1, :]
        nrow, nch, _ = d.shape
        weights = (self.DicoDATA["weights"][indRow, ch0:ch1]).reshape(
            (nrow, nch, 1))
        A0s = self.DicoDATA["A0"][indRow]
        A1s = self.DicoDATA["A1"][indRow]
        times = self.DicoDATA["times"][indRow]
        NTimesBin = np.unique(times).size

        i_TimeBin = np.int64(
            np.argmin(np.abs(
                times.reshape((-1, 1)) -
                np.sort(np.unique(times)).reshape((1, -1))),
                      axis=1).reshape(-1, 1)) * np.ones((1, nch))
        i_ch = np.int64((np.arange(nch).reshape(1, -1)) * np.ones((nrow, 1)))
        i_A0 = A0s.reshape((-1, 1)) * np.ones((1, nch))
        i_A1 = A1s.reshape((-1, 1)) * np.ones((1, nch))

        i_TimeBin = np.int64(i_TimeBin).ravel()
        i_ch = np.int64(i_ch).ravel()
        i_A0 = np.int64(i_A0).ravel()
        i_A1 = np.int64(i_A1).ravel()
        na = self.na

        def Give_R(din):
            d0 = (din[:, :, 0] + din[:, :, -1]) / 2.

            R = np.zeros((nch, NTimesBin), din.dtype)

            ind0 = i_ch * NTimesBin + i_TimeBin

            R.flat[:] = d0.flat[ind0]
            R = d0.T

            Rf = np.ones((R.shape), dtype=np.float64)
            Rf[R == 0] = 0

            return R, Rf

        iMS = self.DicoDATA["iMS"]

        f0, _ = self.Freq_minmax

        dcorr = d
        dcorr[f == 1] = 0

        RMS = scipy.stats.median_abs_deviation((dcorr[dcorr != 0]).ravel(),
                                               scale="normal")
        df = (dcorr[dcorr != 0]).ravel()
        if df.size > 100:
            RMS = np.sqrt(np.abs(np.sum(df * df.conj())) / df.size)

        dp[dp == 0] = 1.

        R, Rf = Give_R(dcorr)
        Rp, Rpf = Give_R(dp)
        Rp[Rpf == 0] = 1.
        #Rp/=np.abs(Rp)
        R /= Rp

        if np.max(np.abs(R)) == 0: return

        C = np.dot(R.T.conj(), R)
        Cn = np.dot(Rf.T, Rf)
        Cn[Cn == 0] = 1.
        C /= Cn

        # import pylab
        # pylab.clf()
        # pylab.subplot(2,2,1)
        # pylab.imshow(np.abs(C),aspect="auto",interpolation="nearest")
        # pylab.colorbar()
        # pylab.title("%i, %i"%(iA0,iA1))
        # pylab.subplot(2,2,2)
        # pylab.imshow(np.abs(R),aspect="auto",interpolation="nearest")
        # pylab.colorbar()
        # pylab.subplot(2,2,3)
        # pylab.imshow(np.abs(Cn),aspect="auto",interpolation="nearest")
        # pylab.colorbar()
        # pylab.title("%i, %i"%(iA0,iA1))
        # pylab.subplot(2,2,4)
        # pylab.imshow(np.abs(Rf),aspect="auto",interpolation="nearest")
        # pylab.colorbar()
        # pylab.draw()
        # pylab.show(block=False)
        # pylab.pause(0.1)
        # return

        # ################################

        Cp = np.dot(Rp.T.conj(), Rp)
        Cpn = np.dot(Rpf.T, Rpf)
        Cpn[Cpn == 0] = 1.
        Cp /= Cpn
        C /= Cp

        C = np.abs(C)
        invC = ModLinAlg.invSVD(C[:, :])

        w = np.abs(np.sum(invC, axis=0))

        WOUT = self.DicoDATA["WOUT"][indRow, ch0:ch1]

        for ii, iRow in enumerate(indRow):
            for ich in np.arange(ch0, ch1):
                self.DicoDATA["WOUT"][iRow, ich] = w[ii]
Пример #9
0
    def predictKernelPolCluster(self,
                                DicoData,
                                SM,
                                iDirection=None,
                                ApplyJones=None,
                                ApplyTimeJones=None,
                                Noise=None,
                                VariableFunc=None):
        T = ClassTimeIt("predictKernelPolCluster")
        T.disable()
        self.DicoData = DicoData
        self.SourceCat = SM.SourceCat

        freq = DicoData["freqs"]
        times = DicoData["times"]
        nf = freq.size
        na = int(DicoData["infos"][0])

        nrows = DicoData["A0"].size
        DataOut = np.zeros((nrows, nf, 4), self.CType)
        if nrows == 0: return DataOut

        self.freqs = freq
        self.wave = 299792458. / self.freqs

        if iDirection != None:
            ListDirection = [iDirection]
        else:
            ListDirection = SM.Dirs  #range(SM.NDir)
        T.timeit("0")
        A0 = DicoData["A0"]
        A1 = DicoData["A1"]
        if ApplyJones != None:
            na, NDir, _ = ApplyJones.shape
            Jones = np.swapaxes(ApplyJones, 0, 1)
            Jones = Jones.reshape((NDir, na, 4))
            JonesH = ModLinAlg.BatchH(Jones)
        T.timeit("1")

        for iCluster in ListDirection:
            print("IIIIIIIIIIIIIIIIII", iCluster)
            ColOutDir = self.PredictDirSPW(iCluster)
            T.timeit("2")
            if ColOutDir is None: continue

            # print(iCluster,ListDirection)
            # print(ColOutDir.shape)
            # ColOutDir.fill(0)
            # print(ColOutDir.shape)
            # ColOutDir[:,:,0]=1
            # print(ColOutDir.shape)
            # ColOutDir[:,:,3]=1
            # print(ColOutDir.shape)

            # Apply Jones
            if ApplyJones != None:

                J = Jones[iCluster]
                JH = JonesH[iCluster]
                for ichan in range(nf):
                    ColOutDir[:, ichan, :] = ModLinAlg.BatchDot(
                        J[A0, :], ColOutDir[:, ichan, :])
                    ColOutDir[:, ichan, :] = ModLinAlg.BatchDot(
                        ColOutDir[:, ichan, :], JH[A1, :])
            T.timeit("3")

            if VariableFunc is not None:  #"DicoBeam" in DicoData.keys():
                tt = np.unique(times)
                lt0, lt1 = tt[0:-1], tt[1::]
                for it in range(lt0.size):
                    t0, t1 = lt0[it], lt1[it]
                    ind = np.where((times >= t0) & (times < t1))[0]
                    if ind.size == 0: continue
                    data = ColOutDir[ind]

                    if "ChanMap" in ApplyTimeJones.keys():
                        ChanMap = ApplyTimeJones["ChanMap"]
                    else:
                        ChanMap = range(nf)

                    for ichan in range(len(ChanMap)):
                        tc = (t0 + t1) / 2.
                        nuc = freq[ichan]
                        ColOutDir[ind, ichan, :] *= VariableFunc(tc, nuc)
                        # c0=ColOutDir[ind,ichan,:].copy()
                        # ColOutDir[ind,ichan,:]*=VariableFunc(tc,nuc)
                        # print(c0-ColOutDir[ind,ichan,:])
                        #print(it,ichan,VariableFunc(tc,nuc))

            if ApplyTimeJones is not None:  #"DicoBeam" in DicoData.keys():
                D = ApplyTimeJones  #DicoData["DicoBeam"]
                Beam = D["Beam"]
                BeamH = D["BeamH"]

                lt0, lt1 = D["t0"], D["t1"]

                for it in range(lt0.size):
                    t0, t1 = lt0[it], lt1[it]
                    ind = np.where((times >= t0) & (times < t1))[0]
                    if ind.size == 0: continue
                    data = ColOutDir[ind]
                    A0sel = A0[ind]
                    A1sel = A1[ind]

                    if "ChanMap" in ApplyTimeJones.keys():
                        ChanMap = ApplyTimeJones["ChanMap"]
                    else:
                        ChanMap = range(nf)

                    #print("ChanMap:",ChanMap)

                    for ichan in range(len(ChanMap)):
                        JChan = ChanMap[ichan]
                        J = Beam[it, iCluster, :, JChan, :, :].reshape((na, 4))
                        JH = BeamH[it, iCluster, :, JChan, :, :].reshape(
                            (na, 4))
                        data[:, ichan, :] = ModLinAlg.BatchDot(
                            J[A0sel, :], data[:, ichan, :])
                        data[:, ichan, :] = ModLinAlg.BatchDot(
                            data[:, ichan, :], JH[A1sel, :])

                    ColOutDir[ind] = data[:]
            T.timeit("4")

            DataOut += ColOutDir
            T.timeit("5")

        if Noise is not None:
            DataOut += Noise / np.sqrt(2.) * (np.random.randn(
                *ColOutDir.shape) + 1j * np.random.randn(*ColOutDir.shape))

        return DataOut
Пример #10
0
    def predictKernelPolClusterCatalog(self,
                                       DicoData,
                                       SM,
                                       iDirection=None,
                                       ApplyJones=None,
                                       ApplyTimeJones=None,
                                       Noise=None):
        self.DicoData = DicoData
        self.SourceCat = SM.SourceCat
        self.SM = SM

        freq = DicoData["freqs_full"]
        times = DicoData["times"]
        nf = freq.size
        na = DicoData["infos"][0]

        nrows = DicoData["A0"].size
        DataOut = np.zeros((nrows, nf, 4), self.CType)
        if nrows == 0: return DataOut

        self.freqs = freq
        self.wave = 299792458. / self.freqs

        if iDirection != None:
            ListDirection = [iDirection]
        else:
            ListDirection = SM.Dirs  #range(SM.NDir)

        A0 = DicoData["A0"]
        A1 = DicoData["A1"]
        if ApplyJones is not None:
            #print "!!!!!",ApplyJones.shape
            #print "!!!!!",ApplyJones.shape
            #print "!!!!!",ApplyJones.shape
            na, NDir, _ = ApplyJones.shape
            Jones = np.swapaxes(ApplyJones, 0, 1)
            Jones = Jones.reshape((NDir, na, 4))
            JonesH = ModLinAlg.BatchH(Jones)

        TSmear = 0.
        FSmear = 0.
        DT = DicoData["infos"][1]
        UVW_dt = DicoData["uvw"]
        if self.DoSmearing:
            if "T" in self.DoSmearing:
                TSmear = 1.
                UVW_dt = DicoData["UVW_dt"]
            if "F" in self.DoSmearing:
                FSmear = 1.

        # self.SourceCat.m[:]=0
        # self.SourceCat.l[:]=0.1
        # self.SourceCat.I[:]=10
        # self.SourceCat.alpha[:]=0

        # DataOut=DataOut[1:2]
        # self.DicoData["uvw"]=self.DicoData["uvw"][1:2]
        # self.DicoData["A0"]=self.DicoData["A0"][1:2]
        # self.DicoData["A1"]=self.DicoData["A1"][1:2]
        # self.DicoData["IndexTimesThisChunk"]=self.DicoData["IndexTimesThisChunk"][1:2]
        # self.SourceCat=self.SourceCat[0:1]

        ColOutDir = np.zeros(DataOut.shape, np.complex64)

        for iCluster in ListDirection:
            ColOutDir.fill(0)

            indSources = np.where(self.SourceCat.Cluster == iCluster)[0]
            T = ClassTimeIt("predict")
            T.disable()
            ### new
            SourceCat = self.SourceCat[indSources].copy()
            #l=np.ones((1,),dtype=np.float64)#,float64(SourceCat.l).copy()
            l = np.require(SourceCat.l,
                           dtype=np.float64,
                           requirements=["A", "C"])
            m = np.require(SourceCat.m,
                           dtype=np.float64,
                           requirements=["A", "C"])

            #m=SourceCat.m#np.float64(SourceCat.m).copy()
            I = np.float32(SourceCat.I)
            Gmaj = np.float32(SourceCat.Gmaj)
            Gmin = np.float32(SourceCat.Gmin)
            GPA = np.float32(SourceCat.Gangle)
            alpha = np.float32(SourceCat.alpha)
            WaveL = np.float64(299792458. / self.freqs)
            WaveL = np.require(WaveL,
                               dtype=np.float64,
                               requirements=["A", "C"])

            flux = np.float32(SourceCat.I)
            alpha = SourceCat.alpha
            dnu = np.float32(self.DicoData["dfreqs_full"])
            f0 = (self.freqs / SourceCat.RefFreq[0])
            fluxFreq = np.float32(
                flux.reshape((flux.size, 1)) * (f0.reshape(
                    (1, f0.size)))**(alpha.reshape((alpha.size, 1))))

            LSM = [l, m, fluxFreq, Gmin, Gmaj, GPA]
            LFreqs = [WaveL, np.float32(self.freqs), dnu]
            LUVWSpeed = [UVW_dt, DT]

            LSmearMode = [FSmear, TSmear]
            T.timeit("init")

            AllowEqualiseChan = IsChanEquidistant(DicoData["freqs_full"])

            if ApplyTimeJones != None:

                #predict.predictJones(ColOutDir,(DicoData["uvw"]),LFreqs,LSM,LUVWSpeed,LSmearMode,ParamJonesList)

                #ColOutDir.fill(0)
                #predict.predictJones(ColOutDir,(DicoData["uvw"]),LFreqs,LSM,LUVWSpeed,LSmearMode,ParamJonesList,0)
                #d0=ColOutDir.copy()
                #ColOutDir.fill(0)

                # predict.predictJones(ColOutDir,(DicoData["uvw"]),LFreqs,LSM,LUVWSpeed,LSmearMode,ParamJonesList,AllowEqualiseChan)
                # ColOutDir0=ColOutDir.copy()
                # ColOutDir.fill(0)

                #predict.predictJones2(ColOutDir,(DicoData["uvw"]),LFreqs,LSM,LUVWSpeed,LSmearMode,ParamJonesList,AllowEqualiseChan)
                #print LSmearMode
                predict.predictJones2_Gauss(ColOutDir, (DicoData["uvw"]),
                                            LFreqs, LSM, LUVWSpeed, LSmearMode,
                                            AllowEqualiseChan, self.LExp,
                                            self.LSinc)

                T.timeit("predict")

                ParamJonesList = self.GiveParamJonesList(
                    ApplyTimeJones, A0, A1)
                ParamJonesList = ParamJonesList + [iCluster]

                predict.ApplyJones(ColOutDir, ParamJonesList)
                T.timeit("apply")

                # print ColOutDir

                #d1=ColOutDir.copy()
                #ind=np.where(d0!=0)
                #print np.max((d0-d1)[ind]/(d0[ind]))
                #stop
            else:
                #predict.predict(ColOutDir,(DicoData["uvw"]),LFreqs,LSM,LUVWSpeed,LSmearMode)
                #AllowEqualiseChan=0
                #predict.predict(ColOutDir,(DicoData["uvw"]),LFreqs,LSM,LUVWSpeed,LSmearMode,AllowEqualiseChan)
                #d0=ColOutDir.copy()
                #ColOutDir.fill(0)

                predict.predictJones2_Gauss(ColOutDir, (DicoData["uvw"]),
                                            LFreqs, LSM, LUVWSpeed, LSmearMode,
                                            AllowEqualiseChan, self.LExp,
                                            self.LSinc)
                #print ColOutDir
                #predict.predict(ColOutDir,(DicoData["uvw"]),LFreqs,LSM,LUVWSpeed,LSmearMode,AllowEqualiseChan)
                T.timeit("predict")
                # d0=ColOutDir.copy()
                # ColOutDir.fill(0)

                # predict_np19.predict(ColOutDir,(DicoData["uvw"]),LFreqs,LSM,LUVWSpeed,LSmearMode,AllowEqualiseChan)
                # print ColOutDir,d0
                # d1=ColOutDir.copy()
                # ind=np.where(d0!=0)
                # print np.max((d0-d1)[ind]/(d0[ind]))
                # stop

            del (l, m, I, SourceCat, alpha, WaveL, flux, dnu, f0, fluxFreq,
                 LSM, LFreqs)
            T.timeit("del")

            # d1=ColOutDir
            # ind=np.where(d0!=0)
            # print np.max((d0-d1)[ind]/(d0[ind]))
            # stop

            if Noise != None:
                ColOutDir += Noise * (np.random.randn(*ColOutDir.shape) +
                                      1j * np.random.randn(*ColOutDir.shape))
                stop
            DataOut += ColOutDir
            T.timeit("add")
        #del(LFreqs,LSM,LUVWSpeed,LSmearMode)
        #del(ColOutDir)

        return DataOut
Пример #11
0
    def MergeJones(self, DicoJ0, DicoJ1):

        log.print("Merging Jones arrays")

        FreqDomainOut = self.GetMergedFreqDomains(DicoJ0, DicoJ1)

        DicoOut = {}
        DicoOut["t0"] = []
        DicoOut["t1"] = []
        DicoOut["tm"] = []
        DicoOut["FreqDomain"] = FreqDomainOut
        T0 = np.min([DicoJ0["t0"][0], DicoJ1["t0"][0]])
        it = 0
        CurrentT0 = T0

        while True:
            T0 = CurrentT0

            dT0 = DicoJ0["t1"] - T0
            dT0 = dT0[dT0 > 0]
            dT1 = DicoJ1["t1"] - T0
            dT1 = dT1[dT1 > 0]
            if (dT0.size == 0) & (dT1.size == 0):
                break
            elif dT0.size == 0:
                dT = dT1[0]
            elif dT1.size == 0:
                dT = dT0[0]
            else:
                dT = np.min([dT0[0], dT1[0]])

            DicoOut["t0"].append(CurrentT0)
            T1 = T0 + dT
            DicoOut["t1"].append(T1)
            Tm = (T0 + T1) / 2.
            DicoOut["tm"].append(Tm)
            CurrentT0 = T1
            it += 1

        DicoOut["t0"] = np.array(DicoOut["t0"])
        DicoOut["t1"] = np.array(DicoOut["t1"])
        DicoOut["tm"] = np.array(DicoOut["tm"])

        nt0 = DicoJ0["t0"].size
        nt1 = DicoJ1["t0"].size

        fm0 = np.mean(DicoJ0["FreqDomain"], axis=1)
        fm1 = np.mean(DicoJ1["FreqDomain"], axis=1)
        fmOut = np.mean(DicoOut["FreqDomain"], axis=1)

        #nt,nd,na,_,_,_=G.shape
        _, nd0, _, _, _, _ = DicoJ0["Jones"].shape
        _, nd1, _, _, _, _ = DicoJ1["Jones"].shape
        _, nd, na, _, _, _ = DicoJ0["Jones"].shape
        if nd0 == nd1:
            _, nd, na, _, _, _ = DicoJ0["Jones"].shape
            ndOut = nd
            indexDirJ0 = indexDirJ1 = range(nd)
        else:
            if not self.FacetToJonesDir:
                log.print(
                    "  need to provide a direction mapping DicoJ0<-DicoJ1")
                stop
            else:
                if nd0 > nd1: stop
                log.print("  using provided FacetToJonesDir mapping [%i->%i]" %
                          (nd1, nd0))
                ndOut = nd1
                indexDirJ0 = self.FacetToJonesDir
                indexDirJ1 = range(nd1)

        nt = DicoOut["tm"].size

        nchOut = fmOut.size

        DicoOut["Jones"] = np.zeros((nt, ndOut, na, nchOut, 2, 2),
                                    np.complex64)

        iG0_t = np.argmin(np.abs(DicoOut["tm"].reshape((nt, 1)) -
                                 DicoJ0["tm"].reshape((1, nt0))),
                          axis=1)
        iG1_t = np.argmin(np.abs(DicoOut["tm"].reshape((nt, 1)) -
                                 DicoJ1["tm"].reshape((1, nt1))),
                          axis=1)

        #        log.print(fmOut,DicoJ0["FreqDomain"],DicoJ1["FreqDomain"])
        for ich in range(nchOut):

            #            log.print(fmOut[ich])
            indChG0 = np.where((fmOut[ich] >= DicoJ0["FreqDomain"][:, 0]) &
                               (fmOut[ich] < DicoJ0["FreqDomain"][:, 1]))[0][0]
            indChG1 = np.where((fmOut[ich] >= DicoJ1["FreqDomain"][:, 0]) &
                               (fmOut[ich] < DicoJ1["FreqDomain"][:, 1]))[0][0]

            for itime in range(nt):
                for iDir in range(ndOut):
                    G0 = DicoJ0["Jones"][iG0_t[itime], indexDirJ0[iDir], :,
                                         indChG0, :, :]
                    G1 = DicoJ1["Jones"][iG1_t[itime], indexDirJ1[iDir], :,
                                         indChG1, :, :]
                    DicoOut["Jones"][itime, iDir, :,
                                     ich, :, :] = ModLinAlg.BatchDot(G0, G1)

        return DicoOut
Пример #12
0
    def GiveJones(self):
        if self.Sols is None:
            Sols = self.GiveSols()
        else:
            Sols = self.Sols

        MS = self.MS
        SM = self.SM
        VS = self.VS
        ApplyBeam = self.ApplyBeam
        na = MS.na
        nd = SM.NDir

        if self.BeamAt == "facet":
            NDir = SM.SourceCat.shape[0]
            SM.SourceCat.Cluster = np.arange(NDir)
            SM.Dirs = SM.SourceCat.Cluster
            SM.NDir = NDir
            SM.ClusterCat = np.zeros((NDir, ), SM.ClusterCat.dtype)
            SM.ClusterCat = SM.ClusterCat.view(np.recarray)
            SM.ClusterCat.ra = SM.SourceCat.ra
            SM.ClusterCat.dec = SM.SourceCat.dec

        Jones = {}
        Jones["t0"] = Sols.t0
        Jones["t1"] = Sols.t1

        nt, nch, na, nd, _, _ = Sols.G.shape
        G = np.swapaxes(Sols.G, 1, 3).reshape((nt, nd, na, nch, 2, 2))

        # G[:,:,:,:,0,0]/=np.abs(G[:,:,:,:,0,0])
        # G[:,:,:,:,1,1]=G[:,:,:,:,0,0]
        # G.fill(0)
        # G[:,:,:,:,0,0]=1
        # G[:,:,:,:,1,1]=1

        nt, nd, na, nch, _, _ = G.shape

        # G=np.random.randn(*G.shape)+1j*np.random.randn(*G.shape)

        useArrayFactor = True
        useElementBeam = False
        if ApplyBeam:
            print(ModColor.Str("Apply Beam"))
            MS.LoadSR(useElementBeam=useElementBeam,
                      useArrayFactor=useArrayFactor)
            RA = SM.ClusterCat.ra
            DEC = SM.ClusterCat.dec

            NDir = RA.size
            Tm = Sols.tm
            T0s = Sols.t0
            T1s = Sols.t1
            DicoBeam = {}
            DicoBeam["Jones"] = np.zeros(
                (Tm.size, NDir, MS.na, MS.NSPWChan, 2, 2), dtype=np.complex64)
            DicoBeam["t0"] = np.zeros((Tm.size, ), np.float64)
            DicoBeam["t1"] = np.zeros((Tm.size, ), np.float64)
            DicoBeam["tm"] = np.zeros((Tm.size, ), np.float64)

            rac, decc = MS.OriginalRadec

            def GB(time, ra, dec):
                Beam = np.zeros(
                    (ra.shape[0], self.MS.na, self.MS.NSPWChan, 2, 2),
                    dtype=np.complex)
                # Beam[...,0,0]=1
                # Beam[...,1,1]=1
                # return Beam

                for i in range(ra.shape[0]):
                    self.MS.SR.setDirection(ra[i], dec[i])
                    Beam[i] = self.MS.SR.evaluate(time)
                return Beam

            for itime in range(Tm.size):
                print(itime)
                DicoBeam["t0"][itime] = T0s[itime]
                DicoBeam["t1"][itime] = T1s[itime]
                DicoBeam["tm"][itime] = Tm[itime]
                ThisTime = Tm[itime]
                Beam = GB(ThisTime, RA, DEC)

                ###### Normalise
                Beam0 = GB(ThisTime, np.array([rac]), np.array([decc]))
                Beam0inv = ModLinAlg.BatchInverse(Beam0)
                nd, _, _, _, _ = Beam.shape
                Ones = np.ones((nd, 1, 1, 1, 1), np.float32)
                Beam0inv = Beam0inv * Ones
                nd_, na_, nf_, _, _ = Beam.shape
                # Beam_=np.ones((nd_,na_,nf_),np.float32)*(1+np.arange(nd_).reshape((-1,1,1)))
                # Beam.fill(0)
                # Beam[:,:,:,0,0]=Beam_[:,:,:]
                # Beam[:,:,:,1,1]=Beam_[:,:,:]
                Beam = ModLinAlg.BatchDot(Beam0inv, Beam)
                ######

                DicoBeam["Jones"][itime] = Beam

            nt, nd, na, nch, _, _ = DicoBeam["Jones"].shape

            #m=np.mean(np.abs(DicoBeam["Jones"][:,1,:,:,:,:]))
            # m=np.mean(np.abs(DicoBeam["Jones"][:,1,:,:,:,:]))
            # DicoBeam["Jones"][:,1,0:6,:,:,:]*=2
            # DicoBeam["Jones"][:,1,:,:,:,:]/=np.mean(np.abs(DicoBeam["Jones"][:,1,:,:,:,:]))
            # DicoBeam["Jones"][:,1,:,:,:,:]*=m

            # #################"
            # # Single Channel
            # DicoBeam["Jones"]=np.mean(DicoBeam["Jones"],axis=3).reshape((nt,nd,na,1,2,2))

            # G=ModLinAlg.BatchDot(G,DicoBeam["Jones"])

            # #################"
            # Multiple Channel
            Ones = np.ones((1, 1, 1, nch, 1, 1), np.float32)
            G = G * Ones
            G = ModLinAlg.BatchDot(G, DicoBeam["Jones"])

            # #################"

            # G[:,:,:,:,0,0]=1
            # G[:,:,:,:,0,1]=0.5
            # G[:,:,:,:,1,0]=2.
            # G[:,:,:,:,1,1]=1

            print("Done")

        # #################
        # Multiple Channel
        self.ChanMap = range(nch)
        # #################

        Jones["Beam"] = G
        Jones["BeamH"] = ModLinAlg.BatchH(G)
        if self.ChanMap is None:
            self.ChanMap = np.zeros((VS.MS.NSPWChan, ), np.int32).tolist()

        Jones["ChanMap"] = self.ChanMap

        # ###### for PM5
        # Jones["Map_VisToJones_Freq"]=self.ChanMap
        # Jones["Jones"]=Jones["Beam"]
        # nt=VS.MS.times_all.size
        # ntJones=DicoBeam["tm"].size
        # d=VS.MS.times_all.reshape((nt,1))-DicoBeam["tm"].reshape((1,ntJones))
        # Jones["Map_VisToJones_Time"]=np.argmin(np.abs(d),axis=1)

        return Jones