Ejemplo n.º 1
0
    def EstimateBeam(self, TimesBeam, RA, DEC,progressBar=True, quiet=False):
        TimesBeam = np.float64(np.array(TimesBeam))
        T0s = TimesBeam[:-1].copy()
        T1s = TimesBeam[1:].copy()
        Tm = (T0s+T1s)/2.
        # RA,DEC=self.BeamRAs,self.BeamDECs

        NDir=RA.size
        
        DicoBeam={}
        FreqDomains=self.BeamMachine.getFreqDomains()

        DicoBeam["VisToJonesChanMapping"]=self.GiveVisToJonesChanMapping(FreqDomains)
        if not quiet:
            print>>log,"VisToJonesChanMapping: %s"%DicoBeam["VisToJonesChanMapping"]


        DicoBeam["Jones"]=np.zeros((Tm.size,NDir,self.MS.na,FreqDomains.shape[0],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=self.MS.OriginalRadec
        pBAR= ProgressBar(Title="  Init E-Jones ")#, HeaderSize=10,TitleSize=13)
        if not progressBar: pBAR.disable()
        pBAR.render(0, Tm.size)
        for itime in range(Tm.size):
            DicoBeam["t0"][itime]=T0s[itime]
            DicoBeam["t1"][itime]=T1s[itime]
            DicoBeam["tm"][itime]=Tm[itime]
            ThisTime=Tm[itime]
            Beam=self.GiveInstrumentBeam(ThisTime,RA,DEC)
            #
            if self.GD["Beam"]["CenterNorm"]==1:
                Beam0=self.GiveInstrumentBeam(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
                Beam= ModLinAlg.BatchDot(Beam0inv, Beam)
                
 
            DicoBeam["Jones"][itime]=Beam
            NDone=itime+1
            pBAR.render(NDone,Tm.size)

            DicoBeam["Jones"][itime] = Beam

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

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

        # print TimesBeam-TimesBeam[0]
        # print t0-t1
        # print DicoBeam["t1"][-1]-DicoBeam["t0"][0]

        return DicoBeam
Ejemplo n.º 2
0
    def GiveCovMat(self):

        x = self.x
        y = self.y

        N = x.size

        dN = 10
        N = 2 * dN + 1
        x, y = np.mgrid[-dN:dN:N * 1j, -dN:dN:N * 1j]
        x = x.flatten()
        y = y.flatten()

        N = x.size
        GG = np.zeros((N, N), np.float32)
        for i in range(N):
            GG[i, :] = Gaussian.GaussianXY(x[i] - x,
                                           y[i] - y,
                                           self.noise**2,
                                           sig=(self.psf[0], self.psf[1]),
                                           pa=self.psf[2])

        Ginv = ModLinAlg.invSVD(GG)

        pylab.clf()
        pylab.subplot(1, 2, 1)
        pylab.imshow(GG, interpolation="nearest")
        pylab.subplot(1, 2, 2)
        pylab.imshow(np.real(Ginv), interpolation="nearest")
        pylab.draw()
        pylab.show(False)
        stop
Ejemplo n.º 3
0
    def MergeJones(self, DicoJ0, DicoJ1):
        T0 = DicoJ0["t0"][0]
        DicoOut = {}
        DicoOut["t0"] = []
        DicoOut["t1"] = []
        DicoOut["tm"] = []
        it = 0
        CurrentT0 = T0

        while True:
            DicoOut["t0"].append(CurrentT0)
            T0 = DicoOut["t0"][it]

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

            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"])

        _, nd, na, nch, _, _ = DicoJ0["Jones"].shape
        nt = DicoOut["tm"].size
        DicoOut["Jones"] = np.zeros((nt, nd, na, 1, 2, 2), np.complex64)

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

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

        for itime in xrange(nt):
            G0 = DicoJ0["Jones"][iG0[itime]]
            G1 = DicoJ1["Jones"][iG1[itime]]
            DicoOut["Jones"][itime] = ModLinAlg.BatchDot(G0, G1)

        return DicoOut
Ejemplo n.º 4
0
    def __init__(self,
                 MeanResidual,
                 MeanModelImage,
                 PSFServer,
                 DeltaChi2=4.,
                 IdSharedMem="",
                 NCPU=6):
        IdSharedMem += "SmearSM."
        NpShared.DelAll(IdSharedMem)
        self.IdSharedMem = IdSharedMem
        self.NCPU = NCPU
        self.MeanModelImage = NpShared.ToShared(
            "%sMeanModelImage" % self.IdSharedMem, MeanModelImage)
        self.MeanResidual = NpShared.ToShared(
            "%sMeanResidual" % self.IdSharedMem, MeanResidual)
        NPixStats = 10000
        RandomInd = np.int64(np.random.rand(NPixStats) * (MeanResidual.size))
        self.RMS = np.std(np.real(self.MeanResidual.ravel()[RandomInd]))
        self.FWHMMin = 3.

        self.PSFServer = PSFServer
        self.DeltaChi2 = DeltaChi2
        self.Var = self.RMS**2
        self.NImGauss = 31
        self.CubeMeanVariablePSF = NpShared.ToShared(
            "%sCubeMeanVariablePSF" % self.IdSharedMem,
            self.PSFServer.DicoVariablePSF['CubeMeanVariablePSF'])
        self.DicoConvMachine = {}

        N = self.NImGauss
        dx, dy = np.mgrid[-(N // 2):N // 2:1j * N, -(N // 2):N // 2:1j * N]

        ListPixParms = [(int(dx.ravel()[i]), int(dy.ravel()[i]))
                        for i in range(dx.size)]
        ListPixData = ListPixParms
        ConvMode = "Matrix"
        N = self.NImGauss

        #stop
        #for
        #ClassConvMachine():
        #def __init__(self,PSF,ListPixParms,ListPixData,ConvMode):

        d = np.sqrt(dx**2 + dy**2)
        self.dist = d
        self.NGauss = 10

        GSig = np.linspace(0., 2, self.NGauss)
        self.GSig = GSig
        ListGauss = []
        One = np.zeros_like(d)
        One[N // 2, N // 2] = 1.
        ListGauss.append(One)
        for sig in GSig[1::]:
            v = np.exp(-d**2 / (2. * sig**2))
            Sv = np.sum(v)
            v /= Sv
            ListGauss.append(v)

        self.ListGauss = ListGauss

        print("Declare convolution machines", file=log)
        NJobs = self.PSFServer.NFacets
        pBAR = ProgressBar(Title=" Declare      ")
        #pBAR.disable()
        pBAR.render(0, '%4i/%i' % (0, NJobs))
        for iFacet in range(self.PSFServer.NFacets):
            #print iFacet,"/",self.PSFServer.NFacets
            PSF = self.PSFServer.DicoVariablePSF['CubeMeanVariablePSF'][
                iFacet]  #[0,0]
            _, _, NPixPSF, _ = PSF.shape
            PSF = PSF[:, :, NPixPSF // 2 - N:NPixPSF // 2 + N + 1,
                      NPixPSF // 2 - N:NPixPSF // 2 + N + 1]
            #print PSF.shape
            #sig=1
            #PSF=(np.exp(-self.dist**2/(2.*sig**2))).reshape(1,1,N,N)

            self.DicoConvMachine[iFacet] = ClassConvMachine.ClassConvMachine(
                PSF, ListPixParms, ListPixData, ConvMode)
            CM = self.DicoConvMachine[iFacet].CM
            NpShared.ToShared("%sCM_Facet%4.4i" % (self.IdSharedMem, iFacet),
                              CM)
            #invCM=ModLinAlg.invSVD(np.float64(CM[0,0]))/self.Var
            #NpShared.ToShared("%sInvCov_Facet%4.4i"%(self.IdSharedMem,iFacet),invCM)

            NDone = iFacet + 1
            intPercent = int(100 * NDone / float(NJobs))
            pBAR.render(intPercent, '%4i/%i' % (NDone, NJobs))

        PSFMean = np.mean(
            self.PSFServer.DicoVariablePSF['CubeMeanVariablePSF'], axis=0)
        self.ConvMachineMeanPSF = ClassConvMachine.ClassConvMachine(
            PSFMean, ListPixParms, ListPixData, ConvMode)
        CM = self.ConvMachineMeanPSF.CM
        invCM = ModLinAlg.invSVD(np.float64(CM[0, 0]), Cut=1e-8) / self.Var
        NpShared.ToShared("%sInvCov_AllFacet" % (self.IdSharedMem), invCM)
        self.FindSupport()
Ejemplo n.º 5
0
    def MergeJones(self, DicoJ0, DicoJ1):
        import DDFacet.Other.ClassJonesDomains
        DomainMachine = DDFacet.Other.ClassJonesDomains.ClassJonesDomains()
        JonesSols = DomainMachine.MergeJones(DicoJ0, DicoJ1)
        print("There are %i channels in the merged Jones array" %
              JonesSols["FreqDomains"].shape[0],
              file=log)
        return JonesSols

        T0 = DicoJ0["t0"][0]
        DicoOut = {}
        DicoOut["t0"] = []
        DicoOut["t1"] = []
        DicoOut["tm"] = []
        it = 0
        CurrentT0 = T0

        while True:
            DicoOut["t0"].append(CurrentT0)
            T0 = DicoOut["t0"][it]

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

            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"])

        _, nd, na, nch, _, _ = DicoJ0["Jones"].shape
        _, nd1, na1, nch1, _, _ = DicoJ1["Jones"].shape
        nt = DicoOut["tm"].size
        nchout = np.max([nch, nch1])

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

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

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

        for itime in range(nt):
            G0 = DicoJ0["Jones"][iG0[itime]]
            G1 = DicoJ1["Jones"][iG1[itime]]
            DicoOut["Jones"][itime] = ModLinAlg.BatchDot(G0, G1)

        return DicoOut
Ejemplo n.º 6
0
    def GiveInvertCov(self, Var):
        if self.invCM is None:

            self.invCM = ModLinAlg.invSVD(np.float64(self.CM[0, 0]))
        return self.invCM / Var