# Path: emirdrp/core/recipe.py
# class EmirRecipe(recipes.BaseRecipe):
#     """Base clase for all EMIR Recipes
# 
# 
#     Attributes
#     ----------
#     logger :
#          recipe logger
# 
#     datamodel : EmirDataModel
# 
#     """
#     logger = logging.getLogger(__name__)
#     datamodel = emirdrp.datamodel.EmirDataModel()
# 
#     def types_getter(self):
#         imgtypes = [prods.MasterBadPixelMask,
#                     prods.MasterBias,
#                     prods.MasterDark,
#                     prods.MasterIntensityFlat,
#                     prods.MasterSpectralFlat,
#                     prods.MasterSky
#                     ]
#         getters = [cor.get_corrector_p, cor.get_corrector_b, cor.get_corrector_d,
#                    [cor.get_corrector_f, cor.get_checker], cor.get_corrector_sf, cor.get_corrector_s]
# 
#         return imgtypes, getters
# 
#     def get_filters(self):
#         import collections
#         imgtypes, getters = self.types_getter()
#         used_getters = []
#         for rtype, getter in zip(imgtypes, getters):
#             self.logger.debug('get_filters, %s  %s', rtype, getter)
#             if rtype is None:
#                 # Unconditional
#                 if isinstance(getter, collections.Iterable):
#                     used_getters.extend(getter)
#                 else:
#                     used_getters.append(getter)
#             else:
#                 # Search
#                 for key, val in self.RecipeInput.stored().items():
#                     if isinstance(val.type, rtype):
#                         if isinstance(getter, collections.Iterable):
#                             used_getters.extend(getter)
#                         else:
#                             used_getters.append(getter)
#                         break
#                 else:
#                     pass
#         return used_getters
# 
#     def init_filters_generic(self, rinput, getters, ins):
#         # with BPM, bias, dark, flat and sky
#         if numina.ext.gtc.check_gtc():
#             self.logger.debug('running in GTC environment')
#         else:
#             self.logger.debug('running outside of GTC environment')
# 
#         meta = emirdrp.processing.info.gather_info(rinput)
#         self.logger.debug('obresult info')
#         for entry in meta['obresult']:
#             self.logger.debug('frame info is %s', entry)
#         correctors = [getter(rinput, meta, ins, self.datamodel) for getter in getters]
#         flow = flowmod.SerialFlow(correctors)
#         return flow
# 
#     def init_filters(self, rinput, ins='EMIR'):
#         getters = self.get_filters()
#         return self.init_filters_generic(rinput, getters, ins)
# 
#     def aggregate_result(self, result, rinput):
#         return result
# 
# Path: emirdrp/instrument/channels.py
# QUADRANTS = [(_P2, _P1), (_P1, _P1), (_P1, _P2), (_P2, _P2)]
# 
# Path: emirdrp/instrument/channels.py
# CHANNELS = [chan for chans in [_CH1, _CH2, _CH3, _CH4] for chan in chans]
# 
# Path: emirdrp/products.py
# class MasterGainMap(DataProductType):
#     def __init__(self, mean, var, frame):
#         self.mean = mean
#         self.var = var
#         self.frame = frame
# 
#     def __getstate__(self):
#         gmean = list(map(float, self.mean.flat))
#         gvar = list(map(float, self.var.flat))
#         return {'frame': self.frame, 'mean': gmean, 'var': gvar}
# 
# class MasterRONMap(DataProductType):
#     def __init__(self, mean, var):
#         self.mean = mean
#         self.var = var
# 
#     def __getstate__(self):
#         gmean = map(float, self.mean.flat)
#         gvar = map(float, self.var.flat)
#         return {'mean': gmean, 'var': gvar}


import logging
import math
import numpy
import scipy.stats
from astropy.io import fits
from numina.core.requirements import ObservationResultRequirement
from numina.core import Parameter, DataFrame
from numina.exceptions import RecipeError
from numina.core import Result
from emirdrp.core.recipe import EmirRecipe
from emirdrp.instrument.channels import QUADRANTS
from emirdrp.instrument.channels import CHANNELS
from emirdrp.products import MasterGainMap, MasterRONMap
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with PyEmir.  If not, see <http://www.gnu.org/licenses/>.
#

"""Recipe for the reduction of gain calibration frames."""





_logger = logging.getLogger('numina.recipes.emir')


class GainRecipe1(EmirRecipe):

    """Detector Gain Recipe.

    Recipe to calibrate the detector gain.
    """
    
    obresult = ObservationResultRequirement()
    region = Parameter('channel', 'Region used to compute: '
                       '(full|quadrant|channel)',
                       choices=['full', 'quadrant', 'channel']
                       )

    gain = Result(MasterGainMap(None, None, None))

# Path: emirdrp/core/recipe.py
# class EmirRecipe(recipes.BaseRecipe):
#     """Base clase for all EMIR Recipes
# 
# 
#     Attributes
#     ----------
#     logger :
#          recipe logger
# 
#     datamodel : EmirDataModel
# 
#     """
#     logger = logging.getLogger(__name__)
#     datamodel = emirdrp.datamodel.EmirDataModel()
# 
#     def types_getter(self):
#         imgtypes = [prods.MasterBadPixelMask,
#                     prods.MasterBias,
#                     prods.MasterDark,
#                     prods.MasterIntensityFlat,
#                     prods.MasterSpectralFlat,
#                     prods.MasterSky
#                     ]
#         getters = [cor.get_corrector_p, cor.get_corrector_b, cor.get_corrector_d,
#                    [cor.get_corrector_f, cor.get_checker], cor.get_corrector_sf, cor.get_corrector_s]
# 
#         return imgtypes, getters
# 
#     def get_filters(self):
#         import collections
#         imgtypes, getters = self.types_getter()
#         used_getters = []
#         for rtype, getter in zip(imgtypes, getters):
#             self.logger.debug('get_filters, %s  %s', rtype, getter)
#             if rtype is None:
#                 # Unconditional
#                 if isinstance(getter, collections.Iterable):
#                     used_getters.extend(getter)
#                 else:
#                     used_getters.append(getter)
#             else:
#                 # Search
#                 for key, val in self.RecipeInput.stored().items():
#                     if isinstance(val.type, rtype):
#                         if isinstance(getter, collections.Iterable):
#                             used_getters.extend(getter)
#                         else:
#                             used_getters.append(getter)
#                         break
#                 else:
#                     pass
#         return used_getters
# 
#     def init_filters_generic(self, rinput, getters, ins):
#         # with BPM, bias, dark, flat and sky
#         if numina.ext.gtc.check_gtc():
#             self.logger.debug('running in GTC environment')
#         else:
#             self.logger.debug('running outside of GTC environment')
# 
#         meta = emirdrp.processing.info.gather_info(rinput)
#         self.logger.debug('obresult info')
#         for entry in meta['obresult']:
#             self.logger.debug('frame info is %s', entry)
#         correctors = [getter(rinput, meta, ins, self.datamodel) for getter in getters]
#         flow = flowmod.SerialFlow(correctors)
#         return flow
# 
#     def init_filters(self, rinput, ins='EMIR'):
#         getters = self.get_filters()
#         return self.init_filters_generic(rinput, getters, ins)
# 
#     def aggregate_result(self, result, rinput):
#         return result
# 
# Path: emirdrp/instrument/channels.py
# QUADRANTS = [(_P2, _P1), (_P1, _P1), (_P1, _P2), (_P2, _P2)]
# 
# Path: emirdrp/instrument/channels.py
# CHANNELS = [chan for chans in [_CH1, _CH2, _CH3, _CH4] for chan in chans]
# 
# Path: emirdrp/products.py
# class MasterGainMap(DataProductType):
#     def __init__(self, mean, var, frame):
#         self.mean = mean
#         self.var = var
#         self.frame = frame
# 
#     def __getstate__(self):
#         gmean = list(map(float, self.mean.flat))
#         gvar = list(map(float, self.var.flat))
#         return {'frame': self.frame, 'mean': gmean, 'var': gvar}
# 
# class MasterRONMap(DataProductType):
#     def __init__(self, mean, var):
#         self.mean = mean
#         self.var = var
# 
#     def __getstate__(self):
#         gmean = map(float, self.mean.flat)
#         gvar = map(float, self.var.flat)
#         return {'mean': gmean, 'var': gvar}


# Path: emirdrp/recipes/engineering/detectorgain.py
import logging
import math
import numpy
import scipy.stats
from astropy.io import fits
from numina.core.requirements import ObservationResultRequirement
from numina.core import Parameter, DataFrame
from numina.exceptions import RecipeError
from numina.core import Result
from emirdrp.core.recipe import EmirRecipe
from emirdrp.instrument.channels import QUADRANTS
from emirdrp.instrument.channels import CHANNELS
from emirdrp.products import MasterGainMap, MasterRONMap

# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with PyEmir.  If not, see <http://www.gnu.org/licenses/>.
#

"""Recipe for the reduction of gain calibration frames."""





_logger = logging.getLogger('numina.recipes.emir')


class GainRecipe1(EmirRecipe):

    """Detector Gain Recipe.

    Recipe to calibrate the detector gain.
    """
    
    obresult = ObservationResultRequirement()
    region = Parameter('channel', 'Region used to compute: '
                       '(full|quadrant|channel)',
                       choices=['full', 'quadrant', 'channel']
                       )

    gain = Result(MasterGainMap(None, None, None))

    ron = Result(MasterRONMap(None, None))