# Path: cubical/param_db.py
# def create(filename, metadata={}, backup=True):
# def load(filename):
#     G = db['G']
#     B = db['B']
# 
# Path: cubical/tools/logger.py
# def logToFile(filename, append=False):
# def getLogFilename():
#     def __init__(self, logger, level, color=None, bold=None):
#     def write(self, message, level_override=None, print_once=None):
#     def print(self, *args):
#     def __init__(self, logger, verbose=None, log_verbose=None):
#     def verbosity(self, set_verb=None):
#     def log_verbosity(self, set_verb=None):
#     def __call__(self, level, color=None):
#     def warn(self, msg, color=None, print_once=None):
#     def error(self, msg, color="red", print_once=None):
#     def info(self, msg, color=None, print_once=None):
#     def critical(self, msg, color=None, print_once=None):
#     def debug(self, msg, color=None, print_once=None):
#     def exception(self, msg, color=None, print_once=None):
#     def print(self, *args):
#     def write(self, message, level=logging.INFO, verbosity=0, print_once=None, color=None):
# def enableMemoryLogging(level=1):
# def set_subprocess_label(label):
# def get_subprocess_label():
# def _sigusr1_handler(signum, frame):
# def _sigusr2_handler(signum, frame):
#     def setMemoryLogging(level):
#     def filter(self, event):
#     def __init__(self, fmt, datefmt, strip=True):
#     def label(self, record):
#     def format(self, record):
# def init(app_name):
# def getLogger(name, verbose=None, log_verbose=None):
# def setBoring(boring=True):
# def setGlobalVerbosity(verbosity):
# def setGlobalLogVerbosity(verbosity):
# def setSilent(Lname):
# def setLoud(Lname):
# class _DefaultWriter(object):
# class LoggerWrapper(object):
# class LogFilter(logging.Filter):
# class ColorStrippingFormatter(logging.Formatter):
# GB = float(1024 ** 3)
#                 KEYS = _log_memory_types[memlevel]
# 
# Path: cubical/tools/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 disableColors():
# def Str(strin0,col="red",Bold=True):
# def Sep(strin=None,D=1):
# def Title(strin,Big=False):
# def disable():


import numpy as np
from numpy.ma import masked_array
from cubical import param_db
from cubical.tools import logger, ModColor
        from cubical.main import expand_templated_name
    def __init__(self, gmfactory, ifrgain_opts, compute=True):
        """
        Initializes the IFR-based gains machinery.
        
        Args:
            gmfactory:      a GainMachine Factory is used to manage the solution databases
            ifrgain_opts:   dict of options
            compute:        if False, gains are not computed even if options ask them to
        """
        self.gmfactory = gmfactory
        load_from = expand_templated_name(ifrgain_opts['load-from'])
        save_to = expand_templated_name(ifrgain_opts['save-to'])
        self._ifrgains_per_chan = ifrgain_opts['per-chan']
        self._ifrgain = None
        self._nfreq = gmfactory.grid["freq"]
        nfreq, nant, ncorr = [len(gmfactory.grid[axis]) for axis in ("freq", "ant", "corr")]
        if load_from:
            filename = load_from
            print(ModColor.Str("applying baseline-based corrections (BBCs) from {}".format(filename),
                                         col="green"), file=log(0))
            if "//" in filename:
                filename, prefix = filename.rsplit("//", 1)
            else:
                filename, prefix = filename, "BBC"
            parm = param_db.load(filename).get(prefix)
            if parm is None:
                print(ModColor.Str("  no solutions for '{}' in {}".format(prefix, filename)), file=log(0))
            else:
                self._ifrgain = parm.reinterpolate(freq=gmfactory.grid["freq"]).filled()
                if tuple(self._ifrgain.shape) != (nfreq, nant, nant, ncorr, ncorr):

# Path: cubical/param_db.py
# def create(filename, metadata={}, backup=True):
# def load(filename):
#     G = db['G']
#     B = db['B']
# 
# Path: cubical/tools/logger.py
# def logToFile(filename, append=False):
# def getLogFilename():
#     def __init__(self, logger, level, color=None, bold=None):
#     def write(self, message, level_override=None, print_once=None):
#     def print(self, *args):
#     def __init__(self, logger, verbose=None, log_verbose=None):
#     def verbosity(self, set_verb=None):
#     def log_verbosity(self, set_verb=None):
#     def __call__(self, level, color=None):
#     def warn(self, msg, color=None, print_once=None):
#     def error(self, msg, color="red", print_once=None):
#     def info(self, msg, color=None, print_once=None):
#     def critical(self, msg, color=None, print_once=None):
#     def debug(self, msg, color=None, print_once=None):
#     def exception(self, msg, color=None, print_once=None):
#     def print(self, *args):
#     def write(self, message, level=logging.INFO, verbosity=0, print_once=None, color=None):
# def enableMemoryLogging(level=1):
# def set_subprocess_label(label):
# def get_subprocess_label():
# def _sigusr1_handler(signum, frame):
# def _sigusr2_handler(signum, frame):
#     def setMemoryLogging(level):
#     def filter(self, event):
#     def __init__(self, fmt, datefmt, strip=True):
#     def label(self, record):
#     def format(self, record):
# def init(app_name):
# def getLogger(name, verbose=None, log_verbose=None):
# def setBoring(boring=True):
# def setGlobalVerbosity(verbosity):
# def setGlobalLogVerbosity(verbosity):
# def setSilent(Lname):
# def setLoud(Lname):
# class _DefaultWriter(object):
# class LoggerWrapper(object):
# class LogFilter(logging.Filter):
# class ColorStrippingFormatter(logging.Formatter):
# GB = float(1024 ** 3)
#                 KEYS = _log_memory_types[memlevel]
# 
# Path: cubical/tools/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 disableColors():
# def Str(strin0,col="red",Bold=True):
# def Sep(strin=None,D=1):
# def Title(strin,Big=False):
# def disable():


# Path: cubical/machines/ifr_gain_machine.py
import numpy as np
from numpy.ma import masked_array
from cubical import param_db
from cubical.tools import logger, ModColor
        from cubical.main import expand_templated_name

    def __init__(self, gmfactory, ifrgain_opts, compute=True):
        """
        Initializes the IFR-based gains machinery.
        
        Args:
            gmfactory:      a GainMachine Factory is used to manage the solution databases
            ifrgain_opts:   dict of options
            compute:        if False, gains are not computed even if options ask them to
        """
        self.gmfactory = gmfactory
        load_from = expand_templated_name(ifrgain_opts['load-from'])
        save_to = expand_templated_name(ifrgain_opts['save-to'])
        self._ifrgains_per_chan = ifrgain_opts['per-chan']
        self._ifrgain = None
        self._nfreq = gmfactory.grid["freq"]
        nfreq, nant, ncorr = [len(gmfactory.grid[axis]) for axis in ("freq", "ant", "corr")]
        if load_from:
            filename = load_from
            print(ModColor.Str("applying baseline-based corrections (BBCs) from {}".format(filename),
                                         col="green"), file=log(0))
            if "//" in filename:
                filename, prefix = filename.rsplit("//", 1)
            else:
                filename, prefix = filename, "BBC"
            parm = param_db.load(filename).get(prefix)
            if parm is None:
                print(ModColor.Str("  no solutions for '{}' in {}".format(prefix, filename)), file=log(0))
            else:
                self._ifrgain = parm.reinterpolate(freq=gmfactory.grid["freq"]).filled()
                if tuple(self._ifrgain.shape) != (nfreq, nant, nant, ncorr, ncorr):

                    print(ModColor.Str("  invalid BBC shape {}, will ignore".format(self._ifrgain.shape)), file=log(0))