# 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/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 SharedArray
import numpy as np
from killMS.Other import ModColor
from DDFacet.Other import logger
from killMS.Other import ClassTimeIt
    try:
        return SharedArray.attach(Name)
    except:
        return None


def DicoToShared(Prefix,Dico,DelInput=False):
    DicoOut={}
    log.print( ModColor.Str("DicoToShared: start [prefix = %s]"%Prefix))
    for key in Dico.keys():
        if type(Dico[key])!=np.ndarray: continue
        #print "%s.%s"%(Prefix,key)
        ThisKeyPrefix="%s.%s"%(Prefix,key)
        log.print( ModColor.Str("  %s -> %s"%(key,ThisKeyPrefix)))
        ar=Dico[key]
        Shared=ToShared(ThisKeyPrefix,ar)
        #T.timeit("getarray %s"%ThisKeyPrefix)
        DicoOut[key]=Shared
        if DelInput:
            del(Dico[key],ar)
            
    if DelInput:
        del(Dico)
    log.print( ModColor.Str("DicoToShared: done"))

    return DicoOut


def SharedToDico(Prefix):
    log.print( ModColor.Str("SharedToDico: start [prefix = %s]"%Prefix))

# 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/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/Array/NpShared.py
import SharedArray
import numpy as np
from killMS.Other import ModColor
from DDFacet.Other import logger
from killMS.Other import ClassTimeIt

    try:
        return SharedArray.attach(Name)
    except:
        return None


def DicoToShared(Prefix,Dico,DelInput=False):
    DicoOut={}
    log.print( ModColor.Str("DicoToShared: start [prefix = %s]"%Prefix))
    for key in Dico.keys():
        if type(Dico[key])!=np.ndarray: continue
        #print "%s.%s"%(Prefix,key)
        ThisKeyPrefix="%s.%s"%(Prefix,key)
        log.print( ModColor.Str("  %s -> %s"%(key,ThisKeyPrefix)))
        ar=Dico[key]
        Shared=ToShared(ThisKeyPrefix,ar)
        #T.timeit("getarray %s"%ThisKeyPrefix)
        DicoOut[key]=Shared
        if DelInput:
            del(Dico[key],ar)
            
    if DelInput:
        del(Dico)
    log.print( ModColor.Str("DicoToShared: done"))

    return DicoOut


def SharedToDico(Prefix):
    log.print( ModColor.Str("SharedToDico: start [prefix = %s]"%Prefix))

    T=ClassTimeIt.ClassTimeIt("   SharedToDico")