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[:]
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[:]
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
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
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
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
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