# Path: killMS/Array/ModLinAlg.py
# def invertChol(A):
# def invertLU(A):
# def sqrtSVD(A,Rank=None):
# def BatchInverse(A,H=False):
# def BatchH(A):
# def BatchDot(A,B):
# def BatchDot2(A,B):
# def PlotMatSVD(A,s,Ainv):
# def invSVD(A):
# def SVDw(A):
# def EigClean(A):
# def Dot_ListBlockMat_Mat(ListBlocks,Mat):
# def Dot_ListBlockMat_Mat_Iregular(ListBlocks,Mat):
# def test_Dot_ListBlockMat_Mat():
# def test_Dot_ListBlockMat_Mat_Big():
#     L=np.linalg.cholesky(A)
#     A=(A+A.T)/2.
#     A=A.reshape((A.size//4,2,2))
#     A=A.reshape((A.size//4,2,2))
#     AH=np.zeros_like(A)
#     AH[:,0,0]=a0
#     AH[:,1,1]=d0
#     AH[:,0,1]=b0
#     AH[:,1,0]=c0
#     AH=AH.reshape(shapeOut)
#     A=A.reshape((A.size//4,2,2))
#     B=B.reshape((B.size//4,2,2))
#     C=np.zeros_like(A)
#     C00=C[:,0,0]
#     C01=C[:,0,1]
#     C10=C[:,1,0]
#     C11=C[:,1,1]
#     C00[:]=a0*a1+c0*b1
#     C01[:]=a0*c1+c0*d1
#     C10[:]=b0*a1+d0*b1
#     C11[:]=b0*c1+d0*d1
#     C=C.reshape(shapeOut)
#     A=A.reshape((NDir_a,nf,na,2,2))
#     B=B.reshape((NDir_b,nf,na,2,2))
#     C=np.zeros_like(A)
#     C00=C[:,:,:,0,0]
#     C01=C[:,:,:,0,1]
#     C10=C[:,:,:,1,0]
#     C11=C[:,:,:,1,1]
#     C00[:,:,:]=a0*a1+c0*b1
#     C01[:,:,:]=a0*c1+c0*d1
#     C10[:,:,:]=b0*a1+d0*b1
#     C11[:,:,:]=b0*c1+d0*d1
#     C=C.reshape(shapeOut)
#     I=np.dot(Ainv,A)
#     B=np.random.randn(nblocks*n,m)
#     T=ClassTimeIt.ClassTimeIt()
#     R0=Dot_ListBlockMat_Mat(ListBlocks,B)
#     R1=np.dot(BlocksMat,B)
#     R2=Dot_ListBlockMat_Mat_Iregular(ListBlocks,B)
#     B=np.random.randn(nblocks*n,m)
#     T=ClassTimeIt.ClassTimeIt()
#     R0=Dot_ListBlockMat_Mat(ListBlocks,B)


import numpy as np
from . import ClassMS
from pyrap.tables import table
from DDFacet.Other import logger
from killMS.Array import ModLinAlg
            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

# Path: killMS/Array/ModLinAlg.py
# def invertChol(A):
# def invertLU(A):
# def sqrtSVD(A,Rank=None):
# def BatchInverse(A,H=False):
# def BatchH(A):
# def BatchDot(A,B):
# def BatchDot2(A,B):
# def PlotMatSVD(A,s,Ainv):
# def invSVD(A):
# def SVDw(A):
# def EigClean(A):
# def Dot_ListBlockMat_Mat(ListBlocks,Mat):
# def Dot_ListBlockMat_Mat_Iregular(ListBlocks,Mat):
# def test_Dot_ListBlockMat_Mat():
# def test_Dot_ListBlockMat_Mat_Big():
#     L=np.linalg.cholesky(A)
#     A=(A+A.T)/2.
#     A=A.reshape((A.size//4,2,2))
#     A=A.reshape((A.size//4,2,2))
#     AH=np.zeros_like(A)
#     AH[:,0,0]=a0
#     AH[:,1,1]=d0
#     AH[:,0,1]=b0
#     AH[:,1,0]=c0
#     AH=AH.reshape(shapeOut)
#     A=A.reshape((A.size//4,2,2))
#     B=B.reshape((B.size//4,2,2))
#     C=np.zeros_like(A)
#     C00=C[:,0,0]
#     C01=C[:,0,1]
#     C10=C[:,1,0]
#     C11=C[:,1,1]
#     C00[:]=a0*a1+c0*b1
#     C01[:]=a0*c1+c0*d1
#     C10[:]=b0*a1+d0*b1
#     C11[:]=b0*c1+d0*d1
#     C=C.reshape(shapeOut)
#     A=A.reshape((NDir_a,nf,na,2,2))
#     B=B.reshape((NDir_b,nf,na,2,2))
#     C=np.zeros_like(A)
#     C00=C[:,:,:,0,0]
#     C01=C[:,:,:,0,1]
#     C10=C[:,:,:,1,0]
#     C11=C[:,:,:,1,1]
#     C00[:,:,:]=a0*a1+c0*b1
#     C01[:,:,:]=a0*c1+c0*d1
#     C10[:,:,:]=b0*a1+d0*b1
#     C11[:,:,:]=b0*c1+d0*d1
#     C=C.reshape(shapeOut)
#     I=np.dot(Ainv,A)
#     B=np.random.randn(nblocks*n,m)
#     T=ClassTimeIt.ClassTimeIt()
#     R0=Dot_ListBlockMat_Mat(ListBlocks,B)
#     R1=np.dot(BlocksMat,B)
#     R2=Dot_ListBlockMat_Mat_Iregular(ListBlocks,B)
#     B=np.random.randn(nblocks*n,m)
#     T=ClassTimeIt.ClassTimeIt()
#     R0=Dot_ListBlockMat_Mat(ListBlocks,B)


# Path: killMS/Data/ClassBeam.py
import numpy as np
from . import ClassMS
from pyrap.tables import table
from DDFacet.Other import logger
from killMS.Array import ModLinAlg

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