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