# Path: emirdrp/processing/info.py
# def gather_info_frames(framelist):
#     iinfo = []
#     for frame in framelist:
#         with frame.open() as hdulist:
#             iinfo.append(gather_info_hdu(hdulist))
#     return iinfo
# 
# 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


import uuid
import datetime
import numpy
import astropy.io.fits as fits
import numina.types.datatype as dt
import emirdrp.products as prods
import emirdrp.requirements as reqs
from numina.array.combine import median
from numina.array.robustfit import fit_theil_sen
from numina.core import Result
from numina.frame.utils import copy_img
from numina.processing.combine import basic_processing_with_combination_frames
from emirdrp.processing.info import gather_info_frames
from emirdrp.core.recipe import EmirRecipe
# License-Filename: LICENSE.txt
#


"""Twilight Flat Recipe for a list of frames in different filters"""






class MultiTwilightFlatRecipe(EmirRecipe):
    """Create a list of twilight flats"""
    obresult = reqs.ObservationResultRequirement()
    master_bpm = reqs.MasterBadPixelMaskRequirement()
    master_bias = reqs.MasterBiasRequirement()
    master_dark = reqs.MasterDarkRequirement()

    twflatframes = Result(dt.ListOfType(prods.MasterIntensityFlat))

    def run(self, rinput):

        results = []
        self.logger.info('starting multiflat flat reduction')

        # Uncomment this line
        # to revert to non-ramp
        # flow = self.init_filters(rinput)
        saturation = 45000.0


# Path: emirdrp/processing/info.py
# def gather_info_frames(framelist):
#     iinfo = []
#     for frame in framelist:
#         with frame.open() as hdulist:
#             iinfo.append(gather_info_hdu(hdulist))
#     return iinfo
# 
# 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/recipes/auxiliary/mtwflat.py
import uuid
import datetime
import numpy
import astropy.io.fits as fits
import numina.types.datatype as dt
import emirdrp.products as prods
import emirdrp.requirements as reqs
from numina.array.combine import median
from numina.array.robustfit import fit_theil_sen
from numina.core import Result
from numina.frame.utils import copy_img
from numina.processing.combine import basic_processing_with_combination_frames
from emirdrp.processing.info import gather_info_frames
from emirdrp.core.recipe import EmirRecipe

# License-Filename: LICENSE.txt
#


"""Twilight Flat Recipe for a list of frames in different filters"""






class MultiTwilightFlatRecipe(EmirRecipe):
    """Create a list of twilight flats"""
    obresult = reqs.ObservationResultRequirement()
    master_bpm = reqs.MasterBadPixelMaskRequirement()
    master_bias = reqs.MasterBiasRequirement()
    master_dark = reqs.MasterDarkRequirement()

    twflatframes = Result(dt.ListOfType(prods.MasterIntensityFlat))

    def run(self, rinput):

        results = []
        self.logger.info('starting multiflat flat reduction')

        # Uncomment this line
        # to revert to non-ramp
        # flow = self.init_filters(rinput)
        saturation = 45000.0


        iinfo = gather_info_frames(rinput.obresult.frames)