# Path: killMS/Other/rad2hmsdms.py
# def rad2hmsdms(rad,Type="dec",deg=False):
#     
#     if deg==False:
#         deg=rad*180/np.pi
#     else:
#         deg=rad
# 
#     strsgn="+"
#     if Type=="ra":
#         deg/=15.
#         strsgn=""
# 
#     if np.sign(deg)==-1.: strsgn="-"
#     deg=np.abs(deg)
#     degd=np.int(deg)
#     degms=(deg-degd)*60.
#     degm=np.int(degms)
#     degs=((degms-degm)*60)
#     return "%s%2.2i %2.2i %06.3f"%(strsgn,degd,degm,degs)
# 
# Path: killMS/Other/ModColor.py
# HEADER = '\033[95m'
# OKBLUE = '\033[94m'
# OKGREEN = '\033[92m'
# WARNING = '\033[93m'
# FAIL = '\033[91m'
# ENDC = '\033[0m'
#     HEADER = ''
#     OKBLUE = ''
#     OKGREEN = ''
#     WARNING = ''
#     FAIL = ''
#     ENDC = ''
# def Str(strin0,col="red",Bold=True):
# def Sep(strin=None,D=1):
# def Title(strin,Big=False):
# def disable():
# 
# Path: killMS/Other/reformat.py
# def reformat(ssin,slash=True,LastSlash=True):
#     ss=ssin.split("/")
#     ss=filter (lambda a: a != "", ss)
#     ss="/".join(ss)+"/"
#     if ssin[0]=="/": ss="/"+ss
#     if not(slash):
#         ss=ss[1:-1]
#     if not(LastSlash):
#         if ss[-1]=="/": ss=ss[0:-1]
#     return ss
# 
# Path: killMS/Other/ClassTimeIt.py
# class ClassTimeIt():
#     def __init__(self,name="",f=1.):
#         self.t0=timemod.time()
#         self.IsEnable=True
#         if name=="":
#             self.name=name
#         else:
#             self.name=name+": "
#         self.IsEnableIncr=False
#         self.Counter=""
#         self.f=f
# 
#     def reinit(self):
#         self.t0=timemod.time()
# 
#     def timeit(self,stri=" Time",hms=False):
#         if self.IsEnable==False: return
#         t1=timemod.time()
#         dt=self.f*(t1-self.t0)
#         if not(hms):
#             Sout= "  * %s%s %s : %7.5fs"%(self.name,stri,str(self.Counter),dt)
#             if self.IsEnableIncr: self.Counter+=1
#         else:
#             ss=(dt)/60.
#             m=int(ss)
#             s=(ss-m)*60.
#             Sout= "  * %s computation time: %i min. %4.1f sec."%(stri,m,s)
#         self.t0=t1
#         if DoLog:
#             log.print( Sout)
#         else:
#             print(Sout)
# 
#         return dt
# 
#     def timeitHMS(self,stri=" Time"):
#         t1=timemod.time()
#         self.t0=t1
# 
#     def disable(self):
#         self.IsEnable=False
# 
#     def enableIncr(self,incr=1):
#         self.IsEnableIncr=True
#         self.Counter=0
# 
#     def AddDt(self,Var):
#         t1=timemod.time()
#         dt=t1-self.t0
#         Var=Var+dt
#         self.t0=t1
#         return Var


import numpy as np
import os
import pyrap.quanta as qa
import pyrap.measures as pm
import ephem
import DDFacet.ToolsDir.ModRotate
        import copy
        import sidereal
        import datetime
            import pylab
        import pylab
        import ModRotate
        import pyrap.tables
from pyrap.tables import table
from killMS.Other.rad2hmsdms import rad2hmsdms
from killMS.Other import ModColor
from killMS.Other import reformat
from DDFacet.Other import logger
from killMS.Other import ClassTimeIt
from DDFacet.Other.progressbar import ProgressBar
        from lofar.stationresponse import stationresponse
            from DDFacet.Data.ClassFITSBeam import ClassFITSBeam as ClassDDFBeam
            from DDFacet.Data.ClassATCABeam import ClassATCABeam as ClassDDFBeam

    def ReadMSInfo(self,MSname,DoPrint=True):
        T=ClassTimeIt.ClassTimeIt()
        T.enableIncr()
        T.disable()
        #print(MSname+'/ANTENNA')

        # open main table
        table_all=table(MSname,ack=False)

        #print(MSname+'/ANTENNA')
        ta=table(table_all.getkeyword('ANTENNA'),ack=False)
        #ta=table(MSname+'::ANTENNA',ack=False)

        StationNames=ta.getcol('NAME')

        na=ta.getcol('POSITION').shape[0]
        self.StationPos=ta.getcol('POSITION')
        #nbl=(na*(na-1))/2+na

        A0,A1=table_all.getcol("ANTENNA1"),table_all.getcol("ANTENNA2")
        ind=np.where(A0==A1)[0]
        self.HasAutoCorr=(ind.size>0)
        A=np.concatenate([A0,A1])

        nas=np.unique(A).size
        self.nbl=(nas**2-nas)//2
        if self.HasAutoCorr:
            self.nbl+=nas
        if A0.size%self.nbl!=0:

# Path: killMS/Other/rad2hmsdms.py
# def rad2hmsdms(rad,Type="dec",deg=False):
#     
#     if deg==False:
#         deg=rad*180/np.pi
#     else:
#         deg=rad
# 
#     strsgn="+"
#     if Type=="ra":
#         deg/=15.
#         strsgn=""
# 
#     if np.sign(deg)==-1.: strsgn="-"
#     deg=np.abs(deg)
#     degd=np.int(deg)
#     degms=(deg-degd)*60.
#     degm=np.int(degms)
#     degs=((degms-degm)*60)
#     return "%s%2.2i %2.2i %06.3f"%(strsgn,degd,degm,degs)
# 
# Path: killMS/Other/ModColor.py
# HEADER = '\033[95m'
# OKBLUE = '\033[94m'
# OKGREEN = '\033[92m'
# WARNING = '\033[93m'
# FAIL = '\033[91m'
# ENDC = '\033[0m'
#     HEADER = ''
#     OKBLUE = ''
#     OKGREEN = ''
#     WARNING = ''
#     FAIL = ''
#     ENDC = ''
# def Str(strin0,col="red",Bold=True):
# def Sep(strin=None,D=1):
# def Title(strin,Big=False):
# def disable():
# 
# Path: killMS/Other/reformat.py
# def reformat(ssin,slash=True,LastSlash=True):
#     ss=ssin.split("/")
#     ss=filter (lambda a: a != "", ss)
#     ss="/".join(ss)+"/"
#     if ssin[0]=="/": ss="/"+ss
#     if not(slash):
#         ss=ss[1:-1]
#     if not(LastSlash):
#         if ss[-1]=="/": ss=ss[0:-1]
#     return ss
# 
# Path: killMS/Other/ClassTimeIt.py
# class ClassTimeIt():
#     def __init__(self,name="",f=1.):
#         self.t0=timemod.time()
#         self.IsEnable=True
#         if name=="":
#             self.name=name
#         else:
#             self.name=name+": "
#         self.IsEnableIncr=False
#         self.Counter=""
#         self.f=f
# 
#     def reinit(self):
#         self.t0=timemod.time()
# 
#     def timeit(self,stri=" Time",hms=False):
#         if self.IsEnable==False: return
#         t1=timemod.time()
#         dt=self.f*(t1-self.t0)
#         if not(hms):
#             Sout= "  * %s%s %s : %7.5fs"%(self.name,stri,str(self.Counter),dt)
#             if self.IsEnableIncr: self.Counter+=1
#         else:
#             ss=(dt)/60.
#             m=int(ss)
#             s=(ss-m)*60.
#             Sout= "  * %s computation time: %i min. %4.1f sec."%(stri,m,s)
#         self.t0=t1
#         if DoLog:
#             log.print( Sout)
#         else:
#             print(Sout)
# 
#         return dt
# 
#     def timeitHMS(self,stri=" Time"):
#         t1=timemod.time()
#         self.t0=t1
# 
#     def disable(self):
#         self.IsEnable=False
# 
#     def enableIncr(self,incr=1):
#         self.IsEnableIncr=True
#         self.Counter=0
# 
#     def AddDt(self,Var):
#         t1=timemod.time()
#         dt=t1-self.t0
#         Var=Var+dt
#         self.t0=t1
#         return Var


# Path: killMS/Data/ClassMS.py
import numpy as np
import os
import pyrap.quanta as qa
import pyrap.measures as pm
import ephem
import DDFacet.ToolsDir.ModRotate
        import copy
        import sidereal
        import datetime
            import pylab
        import pylab
        import ModRotate
        import pyrap.tables
from pyrap.tables import table
from killMS.Other.rad2hmsdms import rad2hmsdms
from killMS.Other import ModColor
from killMS.Other import reformat
from DDFacet.Other import logger
from killMS.Other import ClassTimeIt
from DDFacet.Other.progressbar import ProgressBar
        from lofar.stationresponse import stationresponse
            from DDFacet.Data.ClassFITSBeam import ClassFITSBeam as ClassDDFBeam
            from DDFacet.Data.ClassATCABeam import ClassATCABeam as ClassDDFBeam


    def ReadMSInfo(self,MSname,DoPrint=True):
        T=ClassTimeIt.ClassTimeIt()
        T.enableIncr()
        T.disable()
        #print(MSname+'/ANTENNA')

        # open main table
        table_all=table(MSname,ack=False)

        #print(MSname+'/ANTENNA')
        ta=table(table_all.getkeyword('ANTENNA'),ack=False)
        #ta=table(MSname+'::ANTENNA',ack=False)

        StationNames=ta.getcol('NAME')

        na=ta.getcol('POSITION').shape[0]
        self.StationPos=ta.getcol('POSITION')
        #nbl=(na*(na-1))/2+na

        A0,A1=table_all.getcol("ANTENNA1"),table_all.getcol("ANTENNA2")
        ind=np.where(A0==A1)[0]
        self.HasAutoCorr=(ind.size>0)
        A=np.concatenate([A0,A1])

        nas=np.unique(A).size
        self.nbl=(nas**2-nas)//2
        if self.HasAutoCorr:
            self.nbl+=nas
        if A0.size%self.nbl!=0:

            log.print(ModColor.Str("MS is non conformant!"))