# Licensed under a 3-clause BSD style license - see LICENSE.rst
import logging
import numpy as np
import astropy.units as u
from astropy.io import fits
from astropy.nddata.utils import NoOverlapError
from astropy.table import Table
from astropy.utils import lazyproperty
from regions import CircleSkyRegion, RectangleSkyRegion
from gammapy.data import GTI
from gammapy.irf import EDispKernel, EffectiveAreaTable
from gammapy.irf.edisp_map import EDispMap, EDispKernelMap
from gammapy.irf.psf_kernel import PSFKernel
from gammapy.irf.psf_map import PSFMap
from gammapy.maps import Map, MapAxis
from gammapy.modeling.models import BackgroundModel, Models, ProperModels
from gammapy.stats import cash, cash_sum_cython, wstat
from gammapy.utils.random import get_random_state
from gammapy.utils.scripts import make_name, make_path
from .core import Dataset

__all__ = ["MapDataset", "MapDatasetOnOff"]

log = logging.getLogger(__name__)

CUTOUT_MARGIN = 0.1 * u.deg
RAD_MAX = 0.66
RAD_AXIS_DEFAULT = MapAxis.from_bounds(
    0, RAD_MAX, nbin=66, node_type="edges", name="theta", unit="deg"
)
MIGRA_AXIS_DEFAULT = MapAxis.from_bounds(
    0.2, 5, nbin=48, node_type="edges", name="migra"
)

BINSZ_IRF_DEFAULT = 0.2

class MapDataset(Dataset):
    """Perform sky model likelihood fit on maps.

    Parameters
    ----------
    models : `~gammapy.modeling.models.Models`
        Source sky models.
    counts : `~gammapy.maps.WcsNDMap`
        Counts cube
    exposure : `~gammapy.maps.WcsNDMap`
        Exposure cube
    mask_fit : `~gammapy.maps.WcsNDMap`
        Mask to apply to the likelihood for fitting.
    psf : `~gammapy.cube.PSFKernel` or `~gammapy.cube.PSFMap`
        PSF kernel
    edisp : `~gammapy.irf.EDispKernel` or `~gammapy.cube.EDispMap`
        Energy dispersion kernel
    evaluation_mode : {"local", "global"}
        Model evaluation mode.
        The "local" mode evaluates the model components on smaller grids to save computation time.
        This mode is recommended for local optimization algorithms.
        The "global" evaluation mode evaluates the model components on the full map.
        This mode is recommended for global optimization algorithms.
    use_cache : bool
        Use cached values of frozen models or recompute them
    mask_safe : `~gammapy.maps.WcsNDMap`
        Mask defining the safe data range.
    gti : `~gammapy.data.GTI`
        GTI of the observation or union of GTI if it is a stacked observation
    meta_table : `~astropy.table.Table`
        Table listing informations on observations used to create the dataset.
        One line per observation for stacked datasets.
    """

    stat_type = "cash"
    tag = "MapDataset"

    def __init__(
        self,
        models=None,
        counts=None,
        exposure=None,
        mask_fit=None,
        psf=None,
        edisp=None,
        name=None,
        evaluation_mode="local",
        use_cache=True,
        mask_safe=None,
        gti=None,
        meta_table=None,
    ):
        if mask_fit is not None and mask_fit.data.dtype != np.dtype("bool"):
            raise ValueError("mask data must have dtype bool")

        if mask_safe is not None and mask_safe.data.dtype != np.dtype("bool"):
            raise ValueError("mask data must have dtype bool")

        self._name = make_name(name)
        self._background_model = None
        self.evaluation_mode = evaluation_mode
        self.counts = counts
        self.exposure = exposure
        self.mask_fit = mask_fit
        self.psf = psf
        self.edisp = edisp
        self.mask_safe = mask_safe
        self.models = models
        self.gti = gti
        self.use_cache = use_cache
        self.meta_table = meta_table

        # check whether a reference geom is defined
        _ = self._geom

    @property
    def name(self):
        return self._name

    def __str__(self):
        str_ = f"{self.__class__.__name__}\n"
        str_ += "-" * len(self.__class__.__name__) + "\n"
        str_ += "\n"

        str_ += "\t{:32}: {} \n\n".format("Name", self.name)

        counts = np.nan
        if self.counts is not None:
            counts = np.sum(self.counts.data)
        str_ += "\t{:32}: {:.0f} \n".format("Total counts", counts)

        npred = np.nan
        if self.models is not None:
            npred = np.sum(self.npred().data)
        str_ += "\t{:32}: {:.2f}\n".format("Total predicted counts", npred)

        background = np.nan
        if self.background_model is not None:
            background = np.sum(self.background_model.evaluate().data)
        str_ += "\t{:32}: {:.2f}\n\n".format("Total background counts", background)

        exposure_min, exposure_max, exposure_unit = np.nan, np.nan, ""
        if self.exposure is not None:
            if self.mask_safe is not None:
                mask = self.mask_safe.reduce_over_axes(np.logical_or).data
                if not mask.any():
                    mask = None
            else:
                mask = None
            exposure_min = np.min(self.exposure.data[..., mask])
            exposure_max = np.max(self.exposure.data[..., mask])
            exposure_unit = self.exposure.unit

        str_ += "\t{:32}: {:.2e} {}\n".format(
            "Exposure min", exposure_min, exposure_unit
        )
        str_ += "\t{:32}: {:.2e} {}\n\n".format(
            "Exposure max", exposure_max, exposure_unit
        )

        # data section
        n_bins = 0
        if self.counts is not None:
            n_bins = self.counts.data.size
        str_ += "\t{:32}: {} \n".format("Number of total bins", n_bins)

        n_fit_bins = 0
        if self.mask is not None:
            n_fit_bins = np.sum(self.mask.data)
        str_ += "\t{:32}: {} \n\n".format("Number of fit bins", n_fit_bins)

        # likelihood section
        str_ += "\t{:32}: {}\n".format("Fit statistic type", self.stat_type)

        stat = np.nan
        if self.counts is not None and self.models is not None:
            stat = self.stat_sum()
        str_ += "\t{:32}: {:.2f}\n\n".format("Fit statistic value (-2 log(L))", stat)

        # model section
        n_models, n_pars, n_free_pars = 0, 0, 0
        if self.models is not None:
            n_models = len(self.models)
            n_pars = len(self.models.parameters)
            n_free_pars = len(self.models.parameters.free_parameters)

        str_ += "\t{:32}: {} \n".format("Number of models", n_models)
        str_ += "\t{:32}: {}\n".format("Number of parameters", n_pars)
        str_ += "\t{:32}: {}\n\n".format("Number of free parameters", n_free_pars)

        if self.models is not None:
            str_ += "\t" + "\n\t".join(str(self.models).split("\n")[2:])

        return str_.expandtabs(tabsize=2)

    @property
    def models(self):
        """Models (`~gammapy.modeling.models.Models`)."""
        return ProperModels(self)

    @property
    def background_model(self):
        return self._background_model

    @models.setter
    def models(self, models):
        if models is None:
            self._models = None
        else:
            self._models = Models(models)

        # TODO: clean this up (probably by removing)
        if self.models is not None:
            for model in self.models:
                if isinstance(model, BackgroundModel):
                    if model.datasets_names is not None:
                        if self.name in model.datasets_names:
                            self._background_model = model
                            break
            else:
                log.warning(f"No background model defined for dataset {self.name}")
        self._evaluators = {}

    @property
    def evaluators(self):
        """Model evaluators"""

        models = self.models
        if models:
            keys = list(self._evaluators.keys())
            for key in keys:
                if key not in models:
                    del self._evaluators[key]

            for model in models:
                evaluator = self._evaluators.get(model)

                if evaluator is None:
                    evaluator = MapEvaluator(
                        model=model, evaluation_mode=self.evaluation_mode, gti=self.gti
                    )
                    self._evaluators[model] = evaluator

                # if the model component drifts out of its support the evaluator has
                # has to be updated
                if evaluator.needs_update:
                    evaluator.update(self.exposure, self.psf, self.edisp, self._geom)

        return self._evaluators

    @property
    def _geom(self):
        """Main analysis geometry"""
        if self.counts is not None:
            return self.counts.geom
        elif self.background_model is not None:
            return self.background_model.map.geom
        elif self.mask_safe is not None:
            return self.mask_safe.geom
        elif self.mask_fit is not None:
            return self.mask_fit.geom
        else:
            raise ValueError(
                "Either 'counts', 'background_model', 'mask_fit'"
                " or 'mask_safe' must be defined."
            )

    @property
    def data_shape(self):
        """Shape of the counts or background data (tuple)"""
        return self._geom.data_shape

    def npred(self):
        """Predicted source and background counts (`~gammapy.maps.Map`)."""
        npred_total = Map.from_geom(self._geom, dtype=float)

        for evaluator in self.evaluators.values():
            if evaluator.contributes:
                if self.use_cache is False:
                    evaluator._pars_cached = None
                npred = evaluator.compute_npred()
                npred_total.stack(npred)
        return npred_total

    @classmethod
    def from_geoms(
        cls,
        geom,
        geom_exposure,
        geom_psf,
        geom_edisp,
        reference_time="2000-01-01",
        name=None,
        **kwargs,
    ):
        """
        Create a MapDataset object with zero filled maps according to the specified geometries

        Parameters
        ----------
        geom : `Geom`
            geometry for the counts and background maps
        geom_exposure : `Geom`
            geometry for the exposure map
        geom_psf : `Geom`
            geometry for the psf map
        geom_edisp : `Geom`
            geometry for the energy dispersion kernel map.
            If geom_edisp has a migra axis, this wil create an EDispMap instead.
        reference_time : `~astropy.time.Time`
            the reference time to use in GTI definition
        name : str
            Name of the returned dataset.

        Returns
        -------
        empty_maps : `MapDataset`
            A MapDataset containing zero filled maps
        """
        name = make_name(name)
        kwargs = kwargs.copy()
        kwargs["name"] = name
        kwargs["counts"] = Map.from_geom(geom, unit="")

        background = Map.from_geom(geom, unit="")
        kwargs["models"] = Models(
            [BackgroundModel(background, name=name + "-bkg", datasets_names=[name])]
        )
        kwargs["exposure"] = Map.from_geom(geom_exposure, unit="m2 s")

        if geom_edisp.axes[0].name.lower() == "energy":
            kwargs["edisp"] = EDispKernelMap.from_geom(geom_edisp)
        else:
            kwargs["edisp"] = EDispMap.from_geom(geom_edisp)

        kwargs["psf"] = PSFMap.from_geom(geom_psf)

        kwargs.setdefault(
            "gti", GTI.create([] * u.s, [] * u.s, reference_time=reference_time)
        )
        kwargs["mask_safe"] = Map.from_geom(geom, unit="", dtype=bool)

        return cls(**kwargs)

    @classmethod
    def create(
        cls,
        geom,
        energy_axis_true=None,
        migra_axis=None,
        rad_axis=None,
        binsz_irf=None,
        reference_time="2000-01-01",
        name=None,
        meta_table=None,
        **kwargs,
    ):
        """Create a MapDataset object with zero filled maps.

        Parameters
        ----------
        geom : `~gammapy.maps.WcsGeom`
            Reference target geometry in reco energy, used for counts and background maps
        energy_axis_true : `~gammapy.maps.MapAxis`
            True energy axis used for IRF maps
        migra_axis : `~gammapy.maps.MapAxis`
            If set, this provides the migration axis for the energy dispersion map.
            If not set, an EDispKernelMap is produced instead. Default is None
        rad_axis : `~gammapy.maps.MapAxis`
            Rad axis for the psf map
        binsz_irf : float
            IRF Map pixel size in degrees.
        reference_time : `~astropy.time.Time`
            the reference time to use in GTI definition
        name : str
            Name of the returned dataset.
        meta_table : `~astropy.table.Table`
            Table listing informations on observations used to create the dataset.
            One line per observation for stacked datasets.

        Returns
        -------
        empty_maps : `MapDataset`
            A MapDataset containing zero filled maps
        """
        rad_axis = rad_axis or RAD_AXIS_DEFAULT

        if energy_axis_true is not None:
            if energy_axis_true.name != "energy_true":
                raise ValueError("True enery axis name must be 'energy_true'")
        else:
            energy_axis_true = geom.get_axis_by_name("energy").copy(name="energy_true")

        binsz_irf = binsz_irf or BINSZ_IRF_DEFAULT
        geom_image = geom.to_image()
        geom_exposure = geom_image.to_cube([energy_axis_true])
        geom_irf = geom_image.to_binsz(binsz=binsz_irf)
        geom_psf = geom_irf.to_cube([rad_axis, energy_axis_true])
        if migra_axis:
            geom_edisp = geom_irf.to_cube([migra_axis, energy_axis_true])
        else:
            geom_edisp = geom_irf.to_cube(
                [geom.get_axis_by_name("energy"), energy_axis_true]
            )

        return cls.from_geoms(
            geom,
            geom_exposure,
            geom_psf,
            geom_edisp,
            reference_time=reference_time,
            name=name,
            **kwargs,
        )

    def stack(self, other):
        """Stack another dataset in place.

        Parameters
        ----------
        other: `~gammapy.cube.MapDataset` or `~gammapy.cube.MapDatasetOnOff`
            Map dataset to be stacked with this one. If other is an on-off
            dataset alpha * counts_off is used as a background model.
        """
        if self.mask_safe is None:
            self.mask_safe = Map.from_geom(
                self._geom, data=np.ones_like(self.data_shape)
            )

        if other.mask_safe is None:
            other_mask_safe = Map.from_geom(
                other._geom, data=np.ones_like(other.data_shape)
            )
        else:
            other_mask_safe = other.mask_safe

        if self.counts and other.counts:
            self.counts *= self.mask_safe
            self.counts.stack(other.counts, weights=other_mask_safe)

        if self.exposure and other.exposure:
            mask_image = self.mask_safe.reduce_over_axes(func=np.logical_or)
            self.exposure *= mask_image.data
            # TODO: apply energy dependent mask to exposure. Does this require
            #  a mask_safe in true energy?
            mask_image_other = other_mask_safe.reduce_over_axes(func=np.logical_or)
            self.exposure.stack(other.exposure, weights=mask_image_other)

        # TODO: unify background model handling
        if other.stat_type == "wstat":
            background_model = BackgroundModel(other.background)
        else:
            background_model = other.background_model

        if self.background_model and background_model:
            self._background_model.map *= self.mask_safe
            self._background_model.stack(background_model, other_mask_safe)
            self.models = Models([self.background_model])
        else:
            self.models = None

        if self.psf and other.psf:
            if isinstance(self.psf, PSFMap) and isinstance(other.psf, PSFMap):
                mask_irf = self._mask_safe_irf(self.psf.psf_map, mask_image)
                self.psf.psf_map *= mask_irf.data
                self.psf.exposure_map *= mask_irf.data

                mask_image_other = other_mask_safe.reduce_over_axes(func=np.logical_or)
                mask_irf_other = self._mask_safe_irf(
                    other.psf.psf_map, mask_image_other
                )
                self.psf.stack(other.psf, weights=mask_irf_other)
            else:
                raise ValueError("Stacking of PSF kernels not supported")

        if self.edisp and other.edisp:
            if isinstance(self.edisp, EDispMap) and isinstance(other.edisp, EDispMap):
                mask_irf = self._mask_safe_irf(self.edisp.edisp_map, mask_image)
                self.edisp.edisp_map *= mask_irf.data
                self.edisp.exposure_map *= mask_irf.data

                mask_image_other = other_mask_safe.reduce_over_axes(func=np.logical_or)
                mask_irf_other = self._mask_safe_irf(
                    other.edisp.edisp_map, mask_image_other
                )
                self.edisp.stack(other.edisp, weights=mask_irf_other)
            elif isinstance(self.edisp, EDispKernelMap) and isinstance(
                other.edisp, EDispKernelMap
            ):
                mask_irf = self._mask_safe_irf(self.edisp.edisp_map, mask_image)
                self.edisp.edisp_map *= mask_irf.data
                self.edisp.exposure_map *= mask_irf.data

                mask_image_other = other_mask_safe.reduce_over_axes(func=np.logical_or)
                mask_irf_other = self._mask_safe_irf(
                    other.edisp.edisp_map, mask_image_other
                )
                self.edisp.stack(other.edisp, weights=mask_irf_other)
            else:
                raise ValueError("Stacking of edisp kernels not supported")

        self.mask_safe.stack(other_mask_safe)

        if self.gti and other.gti:
            self.gti = self.gti.stack(other.gti).union()

    @staticmethod
    def _mask_safe_irf(irf_map, mask):
        geom = irf_map.geom.to_image()
        coords = geom.get_coord()
        data = mask.get_by_coord(coords).astype(bool)
        return Map.from_geom(geom=geom, data=data)

    def stat_array(self):
        """Likelihood per bin given the current model parameters"""
        return cash(n_on=self.counts.data, mu_on=self.npred().data)

    def residuals(self, method="diff"):
        """Compute residuals map.

        Parameters
        ----------
        method: {"diff", "diff/model", "diff/sqrt(model)"}
            Method used to compute the residuals. Available options are:
                - "diff" (default): data - model
                - "diff/model": (data - model) / model
                - "diff/sqrt(model)": (data - model) / sqrt(model)

        Returns
        -------
        residuals : `gammapy.maps.WcsNDMap`
            Residual map.
        """
        return self._compute_residuals(self.counts, self.npred(), method=method)

    def plot_residuals(
        self,
        method="diff",
        smooth_kernel="gauss",
        smooth_radius="0.1 deg",
        region=None,
        figsize=(12, 4),
        **kwargs,
    ):
        """
        Plot spatial and spectral residuals.

        The spectral residuals are extracted from the provided region, and the
        normalization used for the residuals computation can be controlled using
        the method parameter. If no region is passed, only the spatial
        residuals are shown.

        Parameters
        ----------
        method : {"diff", "diff/model", "diff/sqrt(model)"}
            Method used to compute the residuals, see `MapDataset.residuals()`
        smooth_kernel : {'gauss', 'box'}
            Kernel shape.
        smooth_radius: `~astropy.units.Quantity`, str or float
            Smoothing width given as quantity or float. If a float is given it
            is interpreted as smoothing width in pixels.
        region: `~regions.Region`
            Region (pixel or sky regions accepted)
        figsize : tuple
            Figure size used for the plotting.
        **kwargs : dict
            Keyword arguments passed to `~matplotlib.pyplot.imshow`.

        Returns
        -------
        ax_image, ax_spec : `~matplotlib.pyplot.Axes`,
            Image and spectrum axes.
        """
        import matplotlib.pyplot as plt

        fig = plt.figure(figsize=figsize)

        counts, npred = self.counts, self.npred()

        if self.mask is not None:
            counts = counts * self.mask
            npred = npred * self.mask

        counts_spatial = counts.sum_over_axes().smooth(
            width=smooth_radius, kernel=smooth_kernel
        )
        npred_spatial = npred.sum_over_axes().smooth(
            width=smooth_radius, kernel=smooth_kernel
        )
        spatial_residuals = self._compute_residuals(
            counts_spatial, npred_spatial, method
        )

        if self.mask_safe is not None:
            mask = self.mask_safe.reduce_over_axes(func=np.logical_or)
            spatial_residuals.data[~mask.data] = np.nan

        # If no region is provided, skip spectral residuals
        ncols = 2 if region is not None else 1
        ax_image = fig.add_subplot(1, ncols, 1, projection=spatial_residuals.geom.wcs)
        ax_spec = None

        kwargs.setdefault("cmap", "coolwarm")
        kwargs.setdefault("stretch", "linear")
        kwargs.setdefault("vmin", -5)
        kwargs.setdefault("vmax", 5)
        spatial_residuals.plot(ax=ax_image, add_cbar=True, **kwargs)

        # Spectral residuals
        if region:
            ax_spec = fig.add_subplot(1, 2, 2)
            counts_spec = counts.get_spectrum(region=region)
            npred_spec = npred.get_spectrum(region=region)
            residuals = self._compute_residuals(counts_spec, npred_spec, method)
            ax = residuals.plot()
            ax.set_yscale("linear")
            ax.axhline(0, color="black", lw=0.5)

            y_max = 2 * np.nanmax(residuals.data)
            plt.ylim(-y_max, y_max)
            label = self._residuals_labels[method]
            plt.ylabel(f"Residuals ({label})")

            # Overlay spectral extraction region on the spatial residuals
            pix_region = region.to_pixel(wcs=spatial_residuals.geom.wcs)
            pix_region.plot(ax=ax_image)

        return ax_image, ax_spec

    @lazyproperty
    def _counts_data(self):
        return self.counts.data.astype(float)

    def stat_sum(self):
        """Total likelihood given the current model parameters."""
        counts, npred = self._counts_data, self.npred().data

        if self.mask is not None:
            return cash_sum_cython(counts[self.mask.data], npred[self.mask.data])
        else:
            return cash_sum_cython(counts.ravel(), npred.ravel())

    def fake(self, random_state="random-seed"):
        """Simulate fake counts for the current model and reduced IRFs.

        This method overwrites the counts defined on the dataset object.

        Parameters
        ----------
        random_state : {int, 'random-seed', 'global-rng', `~numpy.random.RandomState`}
                Defines random number generator initialisation.
                Passed to `~gammapy.utils.random.get_random_state`.
        """
        random_state = get_random_state(random_state)
        npred = self.npred()
        npred.data = random_state.poisson(npred.data)
        self.counts = npred

    def to_hdulist(self):
        """Convert map dataset to list of HDUs.

        Returns
        -------
        hdulist : `~astropy.io.fits.HDUList`
            Map dataset list of HDUs.
        """
        # TODO: what todo about the model and background model parameters?
        exclude_primary = slice(1, None)

        hdu_primary = fits.PrimaryHDU()
        hdulist = fits.HDUList([hdu_primary])
        if self.counts is not None:
            hdulist += self.counts.to_hdulist(hdu="counts")[exclude_primary]

        if self.exposure is not None:
            hdulist += self.exposure.to_hdulist(hdu="exposure")[exclude_primary]

        if self.background_model is not None:
            hdulist += self.background_model.map.to_hdulist(hdu="background")[
                exclude_primary
            ]

        if self.edisp is not None:
            if isinstance(self.edisp, EDispKernel):
                hdus = self.edisp.to_hdulist()
                hdus["MATRIX"].name = "edisp_matrix"
                hdus["EBOUNDS"].name = "edisp_matrix_ebounds"
                hdulist.append(hdus["EDISP_MATRIX"])
                hdulist.append(hdus["EDISP_MATRIX_EBOUNDS"])
            else:
                hdulist += self.edisp.edisp_map.to_hdulist(hdu="EDISP")[exclude_primary]
                hdulist += self.edisp.exposure_map.to_hdulist(hdu="edisp_exposure")[
                    exclude_primary
                ]

        if self.psf is not None:
            if isinstance(self.psf, PSFKernel):
                hdulist += self.psf.psf_kernel_map.to_hdulist(hdu="psf_kernel")[
                    exclude_primary
                ]
            else:
                hdulist += self.psf.psf_map.to_hdulist(hdu="psf")[exclude_primary]
                hdulist += self.psf.exposure_map.to_hdulist(hdu="psf_exposure")[
                    exclude_primary
                ]

        if self.mask_safe is not None:
            mask_safe_int = self.mask_safe.copy()
            mask_safe_int.data = mask_safe_int.data.astype(int)
            hdulist += mask_safe_int.to_hdulist(hdu="mask_safe")[exclude_primary]

        if self.mask_fit is not None:
            mask_fit_int = self.mask_fit.copy()
            mask_fit_int.data = mask_fit_int.data.astype(int)
            hdulist += mask_fit_int.to_hdulist(hdu="mask_fit")[exclude_primary]

        if self.gti is not None:
            hdulist.append(fits.BinTableHDU(self.gti.table, name="GTI"))

        return hdulist

    @classmethod
    def from_hdulist(cls, hdulist, name=None):
        """Create map dataset from list of HDUs.

        Parameters
        ----------
        hdulist : `~astropy.io.fits.HDUList`
            List of HDUs.
        name : str
            Name of the new dataset.

        Returns
        -------
        dataset : `MapDataset`
            Map dataset.
        """
        name = make_name(name)
        kwargs = {"name": name}

        if "COUNTS" in hdulist:
            kwargs["counts"] = Map.from_hdulist(hdulist, hdu="counts")

        if "EXPOSURE" in hdulist:
            exposure = Map.from_hdulist(hdulist, hdu="exposure")
            if exposure.geom.axes[0].name == "energy":
                exposure.geom.axes[0].name = "energy_true"
            kwargs["exposure"] = exposure

        if "BACKGROUND" in hdulist:
            background_map = Map.from_hdulist(hdulist, hdu="background")
            kwargs["models"] = Models(
                [
                    BackgroundModel(
                        background_map, datasets_names=[name], name=name + "-bkg"
                    )
                ]
            )

        if "EDISP_MATRIX" in hdulist:
            kwargs["edisp"] = EDispKernel.from_hdulist(
                hdulist, hdu1="EDISP_MATRIX", hdu2="EDISP_MATRIX_EBOUNDS"
            )
        if "EDISP" in hdulist:
            edisp_map = Map.from_hdulist(hdulist, hdu="edisp")
            exposure_map = Map.from_hdulist(hdulist, hdu="edisp_exposure")
            if edisp_map.geom.axes[0].name == "energy":
                kwargs["edisp"] = EDispKernelMap(edisp_map, exposure_map)
            else:
                kwargs["edisp"] = EDispMap(edisp_map, exposure_map)

        if "PSF_KERNEL" in hdulist:
            psf_map = Map.from_hdulist(hdulist, hdu="psf_kernel")
            kwargs["psf"] = PSFKernel(psf_map)
        if "PSF" in hdulist:
            psf_map = Map.from_hdulist(hdulist, hdu="psf")
            exposure_map = Map.from_hdulist(hdulist, hdu="psf_exposure")
            kwargs["psf"] = PSFMap(psf_map, exposure_map)

        if "MASK_SAFE" in hdulist:
            mask_safe = Map.from_hdulist(hdulist, hdu="mask_safe")
            mask_safe.data = mask_safe.data.astype(bool)
            kwargs["mask_safe"] = mask_safe

        if "MASK_FIT" in hdulist:
            mask_fit = Map.from_hdulist(hdulist, hdu="mask_fit")
            mask_fit.data = mask_fit.data.astype(bool)
            kwargs["mask_fit"] = mask_fit

        if "GTI" in hdulist:
            gti = GTI(Table.read(hdulist, hdu="GTI"))
            kwargs["gti"] = gti

        return cls(**kwargs)

    def write(self, filename, overwrite=False):
        """Write map dataset to file.

        Parameters
        ----------
        filename : str
            Filename to write to.
        overwrite : bool
            Overwrite file if it exists.
        """
        self.to_hdulist().writeto(make_path(filename), overwrite=overwrite)

    @classmethod
    def read(cls, filename, name=None):
        """Read map dataset from file.

        Parameters
        ----------
        filename : str
            Filename to read from.
        name : str
            Name of the new dataset.

        Returns
        -------
        dataset : `MapDataset`
            Map dataset.
        """
        with fits.open(make_path(filename), memmap=False) as hdulist:
            return cls.from_hdulist(hdulist, name=name)

    @classmethod
    def from_dict(cls, data, models):
        """Create from dicts and models list generated from YAML serialization."""

        filename = make_path(data["filename"])
        dataset = cls.read(filename, name=data["name"])

        for model in models:
            if (
                isinstance(model, BackgroundModel)
                and model.filename is None
                and dataset.name == model.datasets_names[0]
            ):
                model.map = dataset.background_model.map

        dataset.models = models
        return dataset

    def to_dict(self, filename=""):
        """Convert to dict for YAML serialization."""
        return {
            "name": self.name,
            "type": self.tag,
            "filename": str(filename),
        }

    def info_dict(self, region=None):
        """Basic info dict with summary statistics

        If a region is passed, then a spectrum dataset is
        extracted, and the corresponding info returned.

        Parameters
        ----------
        region : `~regions.SkyRegion`, optional
            the input ON region on which to extract the spectrum

        Returns
        -------
        info_dict : dict
            Dictionary with summary info.
        """
        if self.gti is not None:
            if region is None:
                region = RectangleSkyRegion(
                    center=self._geom.center_skydir,
                    width=self._geom.width[0][0],
                    height=self._geom.width[1][0],
                )
            info = self.to_spectrum_dataset(on_region=region).info_dict()
        else:
            info = dict()
            if self.counts:
                info["counts"] = np.sum(self.counts.data)
            if self.background_model:
                info["background"] = np.sum(self.background_model.evaluate().data)
                info["excess"] = info["counts"] - info["background"]

            info["npred"] = np.sum(self.npred())
            if self.mask_safe is not None:
                mask = self.mask_safe.reduce_over_axes(np.logical_or).data
                if not mask.any():
                    mask = None
            else:
                mask = None
            if self.exposure:
                exposure_min = np.min(self.exposure.data[..., mask])
                exposure_max = np.max(self.exposure.data[..., mask])
                info["aeff_min"] = exposure_min * self.exposure.unit
                info["aeff_max"] = exposure_max * self.exposure.unit

        info["name"] = self.name

        return info

    def to_spectrum_dataset(self, on_region, containment_correction=False, name=None):
        """Return a ~gammapy.spectrum.SpectrumDataset from on_region.

        Counts and background are summed in the on_region.

        Effective area is taken from the average exposure divided by the livetime.
        Here we assume it is the sum of the GTIs.

        The energy dispersion kernel is obtained at the on_region center.
        Only regions with centers are supported.

        The model is not exported to the ~gammapy.spectrum.SpectrumDataset.
        It must be set after the dataset extraction.

        Parameters
        ----------
        on_region : `~regions.SkyRegion`
            the input ON region on which to extract the spectrum
        containment_correction : bool
            Apply containment correction for point sources and circular on regions
        name : str
            Name of the new dataset.

        Returns
        -------
        dataset : `~gammapy.spectrum.SpectrumDataset`
            the resulting reduced dataset
        """
        from .spectrum import SpectrumDataset

        kwargs = {"gti": self.gti, "name": name}

        if self.gti is not None:
            kwargs["livetime"] = self.gti.time_sum
        else:
            raise ValueError("No GTI in `MapDataset`, cannot compute livetime")

        if self.counts is not None:
            kwargs["counts"] = self.counts.get_spectrum(on_region, np.sum)

        if self.background_model is not None:
            kwargs["background"] = self.background_model.evaluate().get_spectrum(
                on_region, np.sum
            )

        if self.exposure is not None:
            exposure = self.exposure.get_spectrum(on_region, np.mean)
            energy = exposure.geom.axes[0].edges
            kwargs["aeff"] = EffectiveAreaTable(
                energy_lo=energy[:-1],
                energy_hi=energy[1:],
                data=exposure.quantity[:, 0, 0] / kwargs["livetime"],
            )

        if containment_correction:
            if not isinstance(on_region, CircleSkyRegion):
                raise TypeError(
                    "Containement correction is only supported for"
                    " `CircleSkyRegion`."
                )
            elif self.psf is None or isinstance(self.psf, PSFKernel):
                raise ValueError("No PSFMap set. Containement correction impossible")
            else:
                psf = self.psf.get_energy_dependent_table_psf(on_region.center)
                containment = psf.containment(
                    kwargs["aeff"].energy.center, on_region.radius
                )
                kwargs["aeff"].data.data *= containment.squeeze()

        if self.edisp is not None:
            if isinstance(self.edisp, EDispKernel):
                edisp = self.edisp
            elif isinstance(self.edisp, EDispKernelMap):
                edisp = self.edisp.get_edisp_kernel(on_region.center)
            else:
                axis = self._geom.get_axis_by_name("energy")
                edisp = self.edisp.get_edisp_kernel(on_region.center, e_reco=axis.edges)
            kwargs["edisp"] = edisp

        return SpectrumDataset(**kwargs)

    def to_image(self, spectrum=None, name=None):
        """Create images by summing over the energy axis.

        Exposure is weighted with an assumed spectrum,
        resulting in a weighted mean exposure image.

        Currently the PSFMap and EdispMap are dropped from the
        resulting image dataset.

        Parameters
        ----------
        spectrum : `~gammapy.modeling.models.SpectralModel`
            Spectral model to compute the weights.
            Default is power-law with spectral index of 2.
        name : str
            Name of the new dataset.

        Returns
        -------
        dataset : `MapDataset`
            Map dataset containing images.
        """
        from gammapy.makers.utils import _map_spectrum_weight

        name = make_name(name)
        kwargs = {}
        kwargs["name"] = name
        kwargs["gti"] = self.gti

        if self.mask_safe is not None:
            mask_safe = self.mask_safe
            kwargs["mask_safe"] = mask_safe.reduce_over_axes(
                func=np.logical_or, keepdims=True
            )
        else:
            mask_safe = 1

        if self.counts is not None:
            counts = self.counts * mask_safe
            kwargs["counts"] = counts.sum_over_axes(keepdims=True)

        if self.exposure is not None:
            exposure = _map_spectrum_weight(self.exposure, spectrum)
            kwargs["exposure"] = exposure.sum_over_axes(keepdims=True)

        if self.background_model is not None:
            background = self.background_model.evaluate() * mask_safe
            background = background.sum_over_axes(keepdims=True)
            kwargs["models"] = Models(
                [BackgroundModel(background, datasets_names=[name])]
            )

        if self.psf is not None:
            # TODO: implement PSFKernel.to_image()
            if not isinstance(self.psf, PSFKernel):
                kwargs["psf"] = self.psf.to_image(spectrum=spectrum, keepdims=True)
            else:
                # assume exposure at center position
                kwargs["psf"] = None

        return self.__class__(**kwargs)

    def cutout(self, position, width, mode="trim", name=None):
        """Cutout map dataset.

        Parameters
        ----------
        position : `~astropy.coordinates.SkyCoord`
            Center position of the cutout region.
        width : tuple of `~astropy.coordinates.Angle`
            Angular sizes of the region in (lon, lat) in that specific order.
            If only one value is passed, a square region is extracted.
        mode : {'trim', 'partial', 'strict'}
            Mode option for Cutout2D, for details see `~astropy.nddata.utils.Cutout2D`.
        name : str
            Name of the new dataset.

        Returns
        -------
        cutout : `MapDataset`
            Cutout map dataset.
        """
        name = make_name(name)
        kwargs = {"gti": self.gti, "name": name}
        cutout_kwargs = {"position": position, "width": width, "mode": mode}

        if self.counts is not None:
            kwargs["counts"] = self.counts.cutout(**cutout_kwargs)

        if self.exposure is not None:
            kwargs["exposure"] = self.exposure.cutout(**cutout_kwargs)

        if self.background_model is not None:
            model = self.background_model.cutout(**cutout_kwargs, name=name + "-bkg")
            model.datasets_names = [name]
            kwargs["models"] = model

        if self.edisp is not None:
            kwargs["edisp"] = self.edisp.cutout(**cutout_kwargs)

        if self.psf is not None:
            kwargs["psf"] = self.psf.cutout(**cutout_kwargs)

        if self.mask_safe is not None:
            kwargs["mask_safe"] = self.mask_safe.cutout(**cutout_kwargs)

        if self.mask_fit is not None:
            kwargs["mask_fit"] = self.mask_fit.cutout(**cutout_kwargs)

        return self.__class__(**kwargs)

class MapDatasetOnOff(MapDataset):
    """Map dataset for on-off likelihood fitting.

    Parameters
    ----------
    models : `~gammapy.modeling.models.Models`
        Source sky models.
    counts : `~gammapy.maps.WcsNDMap`
        Counts cube
    counts_off : `~gammapy.maps.WcsNDMap`
        Ring-convolved counts cube
    acceptance : `~gammapy.maps.WcsNDMap`
        Acceptance from the IRFs
    acceptance_off : `~gammapy.maps.WcsNDMap`
        Acceptance off
    exposure : `~gammapy.maps.WcsNDMap`
        Exposure cube
    mask_fit : `~numpy.ndarray`
        Mask to apply to the likelihood for fitting.
    psf : `~gammapy.cube.PSFKernel`
        PSF kernel
    edisp : `~gammapy.irf.EDispKernel`
        Energy dispersion
    evaluation_mode : {"local", "global"}
        Model evaluation mode.
        The "local" mode evaluates the model components on smaller grids to save computation time.
        This mode is recommended for local optimization algorithms.
        The "global" evaluation mode evaluates the model components on the full map.
        This mode is recommended for global optimization algorithms.
    mask_safe : `~numpy.ndarray`
        Mask defining the safe data range.
    gti : `~gammapy.data.GTI`
        GTI of the observation or union of GTI if it is a stacked observation
    meta_table : `~astropy.table.Table`
        Table listing informations on observations used to create the dataset.
        One line per observation for stacked datasets.
    name : str
        Name of the dataset.

    """

    stat_type = "wstat"
    tag = "MapDatasetOnOff"

    def __init__(
        self,
        models=None,
        counts=None,
        counts_off=None,
        acceptance=None,
        acceptance_off=None,
        exposure=None,
        mask_fit=None,
        psf=None,
        edisp=None,
        name=None,
        evaluation_mode="local",
        mask_safe=None,
        gti=None,
        meta_table=None,
    ):
        if mask_fit is not None and mask_fit.dtype != np.dtype("bool"):
            raise ValueError("mask data must have dtype bool")

        self.evaluation_mode = evaluation_mode
        self.counts = counts
        self.counts_off = counts_off
        self.exposure = exposure

        if np.isscalar(acceptance):
            acceptance = Map.from_geom(
                self._geom, data=np.ones(self.data_shape) * acceptance
            )

        if np.isscalar(acceptance_off):
            acceptance_off = Map.from_geom(
                self._geom, data=np.ones(self.data_shape) * acceptance_off
            )

        self.acceptance = acceptance
        self.acceptance_off = acceptance_off
        self._background_model = None
        self.mask_fit = mask_fit
        self.psf = psf
        self.edisp = edisp
        self._name = make_name(name)
        self.models = models
        self.mask_safe = mask_safe
        self.gti = gti
        self.meta_table = meta_table

    def __str__(self):
        str_ = super().__str__()

        counts_off = np.nan
        if self.counts_off is not None:
            counts_off = np.sum(self.counts_off.data)
        str_ += "\t{:32}: {:.0f} \n".format("Total counts_off", counts_off)

        acceptance = np.nan
        if self.acceptance is not None:
            acceptance = np.sum(self.acceptance.data)
        str_ += "\t{:32}: {:.0f} \n".format("Acceptance", acceptance)

        acceptance_off = np.nan
        if self.acceptance_off is not None:
            acceptance_off = np.sum(self.acceptance_off.data)
        str_ += "\t{:32}: {:.0f} \n".format("Acceptance off", acceptance_off)

        return str_.expandtabs(tabsize=4)

    @property
    def alpha(self):
        """Exposure ratio between signal and background regions"""
        alpha = self.acceptance / self.acceptance_off
        alpha.data = np.nan_to_num(alpha.data)
        return alpha

    @property
    def background(self):
        """Predicted background in the on region.

        Notice that this definition is valid under the assumption of cash statistic.
        """
        return self.alpha * self.counts_off

    @property
    def excess(self):
        """Excess (counts - alpha * counts_off)"""
        return self.counts.data - self.background.data

    def stat_array(self):
        """Likelihood per bin given the current model parameters"""
        mu_sig = self.npred().data
        on_stat_ = wstat(
            n_on=self.counts.data,
            n_off=self.counts_off.data,
            alpha=list(self.alpha.data),
            mu_sig=mu_sig,
        )
        return np.nan_to_num(on_stat_)

    @classmethod
    def from_geoms(
        cls,
        geom,
        geom_exposure,
        geom_psf,
        geom_edisp,
        reference_time="2000-01-01",
        name=None,
        **kwargs,
    ):
        """
        Create a MapDatasetOnOff object with zero filled maps according to the specified geometries

        Parameters
        ----------
        geom : `gammapy.maps.WcsGeom`
            geometry for the counts, counts_off, acceptance and acceptance_off maps
        geom_exposure : `gammapy.maps.WcsGeom`
            geometry for the exposure map
        geom_psf : `gammapy.maps.WcsGeom`
            geometry for the psf map
        geom_edisp : `gammapy.maps.WcsGeom`
            geometry for the energy dispersion kernel map.
            If geom_edisp has a migra axis, this wil create an EDispMap instead.
        reference_time : `~astropy.time.Time`
            the reference time to use in GTI definition
        name : str
            Name of the returned dataset.

        Returns
        -------
        empty_maps : `MapDatasetOnOff`
            A MapDatasetOnOff containing zero filled maps
        """
        kwargs = kwargs.copy()
        kwargs["name"] = name

        for key in ["counts", "counts_off", "acceptance", "acceptance_off"]:
            kwargs[key] = Map.from_geom(geom, unit="")

        kwargs["exposure"] = Map.from_geom(geom_exposure, unit="m2 s")
        if geom_edisp.axes[0].name.lower() == "energy":
            kwargs["edisp"] = EDispKernelMap.from_geom(geom_edisp)
        else:
            kwargs["edisp"] = EDispMap.from_geom(geom_edisp)

        kwargs["psf"] = PSFMap.from_geom(geom_psf)
        kwargs["gti"] = GTI.create([] * u.s, [] * u.s, reference_time=reference_time)
        kwargs["mask_safe"] = Map.from_geom(geom, dtype=bool)

        return cls(**kwargs)

    @classmethod
    def from_map_dataset(
        cls, dataset, acceptance, acceptance_off, counts_off=None, name=None
    ):
        """Create spectrum dataseton off from another dataset.

        Parameters
        ----------
        dataset : `MapDataset`
            Spectrum dataset defining counts, edisp, aeff, livetime etc.
        acceptance : `Map`
            Relative background efficiency in the on region.
        acceptance_off : `Map`
            Relative background efficiency in the off region.
        counts_off : `Map`
            Off counts map . If the dataset provides a background model,
            and no off counts are defined. The off counts are deferred from
            counts_off / alpha.
        name : str
            Name of the returned dataset.

        Returns
        -------
        dataset : `MapDatasetOnOff`
            Map dataset on off.

        """
        kwargs = {"name": name}

        if counts_off is None and dataset.background_model is not None:
            alpha = acceptance / acceptance_off
            kwargs["counts_off"] = dataset.background_model.evaluate() / alpha

        return cls(
            counts=dataset.counts,
            exposure=dataset.exposure,
            counts_off=counts_off,
            edisp=dataset.edisp,
            gti=dataset.gti,
            mask_safe=dataset.mask_safe,
            mask_fit=dataset.mask_fit,
            acceptance=acceptance,
            acceptance_off=acceptance_off,
            name=dataset.name,
            evaluation_mode=dataset.evaluation_mode,
        )

    @property
    def _is_stackable(self):
        """Check if the Dataset contains enough information to be stacked"""
        if (
            self.acceptance_off is None
            or self.acceptance is None
            or self.counts_off is None
        ):
            return False
        else:
            return True

    def stack(self, other):
        r"""Stack another dataset in place.

        The ``acceptance`` of the stacked dataset is normalized to 1,
        and the stacked ``acceptance_off`` is scaled so that:

        .. math::
            \alpha_\text{stacked} =
            \frac{1}{a_\text{off}} =
            \frac{\alpha_1\text{OFF}_1 + \alpha_2\text{OFF}_2}{\text{OFF}_1 + OFF_2}

        Parameters
        ----------
        other : `MapDatasetOnOff`
            Other dataset
        """
        if not isinstance(other, MapDatasetOnOff):
            raise TypeError("Incompatible types for MapDatasetOnOff stacking")

        if not self._is_stackable or not other._is_stackable:
            raise ValueError("Cannot stack incomplete MapDatsetOnOff.")

        # Factor containing: self.alpha * self.counts_off + other.alpha * other.counts_off
        tmp_factor = (self.alpha * self.counts_off).copy()
        tmp_factor.data[~self.mask_safe.data] = 0
        tmp_factor.stack(other.alpha * other.counts_off, weights=other.mask_safe)

        # Stack the off counts (in place)
        self.counts_off.data[~self.mask_safe.data] = 0
        self.counts_off.stack(other.counts_off, weights=other.mask_safe)

        self.acceptance_off = self.counts_off / tmp_factor
        self.acceptance.data = np.ones(self.data_shape)

        super().stack(other)

    def stat_sum(self):
        """Total likelihood given the current model parameters."""
        return Dataset.stat_sum(self)

    def fake(self, background_model, random_state="random-seed"):
        """Simulate fake counts (on and off) for the current model and reduced IRFs.

        This method overwrites the counts defined on the dataset object.

        Parameters
        ----------
        random_state : {int, 'random-seed', 'global-rng', `~numpy.random.RandomState`}
                Defines random number generator initialisation.
                Passed to `~gammapy.utils.random.get_random_state`.
        """
        random_state = get_random_state(random_state)
        npred = self.npred()
        npred.data = random_state.poisson(npred.data)

        npred_bkg = background_model.copy()
        npred_bkg.data = random_state.poisson(npred_bkg.data)

        self.counts = npred + npred_bkg

        npred_off = background_model / self.alpha
        npred_off.data = random_state.poisson(npred_off.data)
        self.counts_off = npred_off

    def to_hdulist(self):
        """Convert map dataset to list of HDUs.

        Returns
        -------
        hdulist : `~astropy.io.fits.HDUList`
            Map dataset list of HDUs.
        """
        hdulist = super().to_hdulist()
        exclude_primary = slice(1, None)

        if self.counts_off is not None:
            hdulist += self.counts_off.to_hdulist(hdu="counts_off")[exclude_primary]

        if self.acceptance is not None:
            hdulist += self.acceptance.to_hdulist(hdu="acceptance")[exclude_primary]

        if self.acceptance_off is not None:
            hdulist += self.acceptance_off.to_hdulist(hdu="acceptance_off")[
                exclude_primary
            ]

        return hdulist

    @classmethod
    def from_hdulist(cls, hdulist, name=None):
        """Create map dataset from list of HDUs.

        Parameters
        ----------
        hdulist : `~astropy.io.fits.HDUList`
            List of HDUs.
        name : str
            Name of the new dataset.

        Returns
        -------
        dataset : `MapDataset`
            Map dataset.
        """
        kwargs = {}
        kwargs["name"] = name
        if "COUNTS" in hdulist:
            kwargs["counts"] = Map.from_hdulist(hdulist, hdu="counts")

        if "COUNTS_OFF" in hdulist:
            kwargs["counts_off"] = Map.from_hdulist(hdulist, hdu="counts_off")

        if "ACCEPTANCE" in hdulist:
            kwargs["acceptance"] = Map.from_hdulist(hdulist, hdu="acceptance")

        if "ACCEPTANCE_OFF" in hdulist:
            kwargs["acceptance_off"] = Map.from_hdulist(hdulist, hdu="acceptance_off")

        if "EXPOSURE" in hdulist:
            kwargs["exposure"] = Map.from_hdulist(hdulist, hdu="exposure")

        if "EDISP_MATRIX" in hdulist:
            kwargs["edisp"] = EDispKernel.from_hdulist(
                hdulist, hdu1="EDISP_MATRIX", hdu2="EDISP_MATRIX_EBOUNDS"
            )

        if "PSF_KERNEL" in hdulist:
            psf_map = Map.from_hdulist(hdulist, hdu="psf_kernel")
            kwargs["psf"] = PSFKernel(psf_map)

        if "MASK_SAFE" in hdulist:
            mask_safe = Map.from_hdulist(hdulist, hdu="mask_safe")
            mask_safe.data = mask_safe.data.astype(bool)
            kwargs["mask_safe"] = mask_safe

        if "MASK_FIT" in hdulist:
            mask_fit = Map.from_hdulist(hdulist, hdu="mask_fit")
            mask_fit.data = mask_fit.data.astype(bool)
            kwargs["mask_fit"] = mask_fit

        if "GTI" in hdulist:
            gti = GTI(Table.read(hdulist, hdu="GTI"))
            kwargs["gti"] = gti
        return cls(**kwargs)

    def info_dict(self, region=None):
        """Basic info dict with summary statistics

        If a region is passed, then a spectrum dataset is
        extracted, and the corresponding info returned.

        Parameters
        ----------
        region : `~regions.SkyRegion`, optional
            the input ON region on which to extract the spectrum

        Returns
        -------
        info_dict : dict
            Dictionary with summary info.
        """
        info = super().info_dict(region)
        info["name"] = self.name
        if self.gti is None:
            if self.counts_off is not None:
                info["counts_off"] = np.sum(self.counts_off.data)

            if self.acceptance is not None:
                info["acceptance"] = np.sum(self.acceptance.data)

            if self.acceptance_off is not None:
                info["acceptance_off"] = np.sum(self.acceptance_off.data)

            info["excess"] = np.sum(self.excess.data)
        return info

    def to_spectrum_dataset(self, on_region, containment_correction=False, name=None):
        """Return a ~gammapy.spectrum.SpectrumDatasetOnOff from on_region.

        Counts and OFF counts are summed in the on_region.

        Acceptance is the average of all acceptances while acceptance OFF
        is taken such that number of excess is preserved in the on_region.

        Effective area is taken from the average exposure divided by the livetime.
        Here we assume it is the sum of the GTIs.

        The energy dispersion kernel is obtained at the on_region center.
        Only regions with centers are supported.

        The model is not exported to the ~gammapy.spectrum.SpectrumDataset.
        It must be set after the dataset extraction.

        Parameters
        ----------
        on_region : `~regions.SkyRegion`
            the input ON region on which to extract the spectrum
        containment_correction : bool
            Apply containment correction for point sources and circular on regions
        name : str
            Name of the new dataset.

        Returns
        -------
        dataset : `~gammapy.spectrum.SpectrumDatasetOnOff`
            the resulting reduced dataset
        """
        from .spectrum import SpectrumDatasetOnOff

        dataset = super().to_spectrum_dataset(on_region, containment_correction, name)

        kwargs = {}
        if self.counts_off is not None:
            kwargs["counts_off"] = self.counts_off.get_spectrum(on_region, np.sum)

        if self.acceptance is not None:
            kwargs["acceptance"] = self.acceptance.get_spectrum(on_region, np.mean)
            background = self.background.get_spectrum(on_region, np.sum)
            kwargs["acceptance_off"] = (
                kwargs["acceptance"] * kwargs["counts_off"] / background
            )

        return SpectrumDatasetOnOff.from_spectrum_dataset(dataset=dataset, **kwargs)

    def cutout(self, position, width, mode="trim", name=None):
        """Cutout map dataset.

        Parameters
        ----------
        position : `~astropy.coordinates.SkyCoord`
            Center position of the cutout region.
        width : tuple of `~astropy.coordinates.Angle`
            Angular sizes of the region in (lon, lat) in that specific order.
            If only one value is passed, a square region is extracted.
        mode : {'trim', 'partial', 'strict'}
            Mode option for Cutout2D, for details see `~astropy.nddata.utils.Cutout2D`.
        name : str
            Name of the new dataset.

        Returns
        -------
        cutout : `MapDatasetOnOff`
            Cutout map dataset.
        """
        cutout_kwargs = {
            "position": position,
            "width": width,
            "mode": mode,
            "name": name,
        }

        cutout_dataset = super().cutout(**cutout_kwargs)

        del cutout_kwargs["name"]

        if self.counts_off is not None:
            cutout_dataset.counts_off = self.counts_off.cutout(**cutout_kwargs)

        if self.acceptance is not None:
            cutout_dataset.acceptance = self.acceptance.cutout(**cutout_kwargs)

        if self.acceptance_off is not None:
            cutout_dataset.acceptance_off = self.acceptance_off.cutout(**cutout_kwargs)

        return cutout_dataset

    def to_image(self, spectrum=None, name=None):
        """Create images by summing over the energy axis.

        Exposure is weighted with an assumed spectrum,
        resulting in a weighted mean exposure image.

        Currently the PSFMap and EdispMap are dropped from the
        resulting image dataset.

        Parameters
        ----------
        spectrum : `~gammapy.modeling.models.SpectralModel`
            Spectral model to compute the weights.
            Default is power-law with spectral index of 2.
        name : str
            Name of the new dataset.

        Returns
        -------
        dataset : `MapDatasetOnOff`
            Map dataset containing images.
        """
        kwargs = {"name": name}
        dataset = super().to_image(spectrum, name)

        if self.mask_safe is not None:
            mask_safe = self.mask_safe
        else:
            mask_safe = 1

        if self.counts_off is not None:
            counts_off = self.counts_off * mask_safe
            kwargs["counts_off"] = counts_off.sum_over_axes(keepdims=True)

        if self.acceptance is not None:
            acceptance = self.acceptance * mask_safe
            kwargs["acceptance"] = acceptance.sum_over_axes(keepdims=True)

            background = self.background * mask_safe
            background = background.sum_over_axes(keepdims=True)
            kwargs["acceptance_off"] = (
                kwargs["acceptance"] * kwargs["counts_off"] / background
            )

        return self.from_map_dataset(dataset, **kwargs)

class MapEvaluator:
    """Sky model evaluation on maps.

    This evaluates a sky model on a 3D map and convolves with the IRFs,
    and returns a map of the predicted counts.
    Note that background counts are not added.

    For now, we only make it work for 3D WCS maps with an energy axis.
    No HPX, no other axes, those can be added later here or via new
    separate model evaluator classes.

    Parameters
    ----------
    model : `~gammapy.modeling.models.SkyModel`
        Sky model
    exposure : `~gammapy.maps.Map`
        Exposure map
    psf : `~gammapy.cube.PSFKernel`
        PSF kernel
    edisp : `~gammapy.irf.EDispKernel`
        Energy dispersion
    gti : `~gammapy.data.GTI`
        GTI of the observation or union of GTI if it is a stacked observation
    evaluation_mode : {"local", "global"}
        Model evaluation mode.
    """

    def __init__(
        self,
        model=None,
        exposure=None,
        psf=None,
        edisp=None,
        gti=None,
        evaluation_mode="local",
    ):

        self.model = model
        self.exposure = exposure
        self.psf = psf
        self.edisp = edisp
        self.gti = gti
        self.contributes = True
        self._npred_cached = None
        self._pars_cached = None

        if evaluation_mode not in {"local", "global"}:
            raise ValueError(f"Invalid evaluation_mode: {evaluation_mode!r}")
        self.evaluation_mode = evaluation_mode

        # TODO: this is preliminary solution until we have further unified the model handling
        if isinstance(self.model, BackgroundModel):
            self.evaluation_mode = "global"

    @property
    def geom(self):
        """True energy map geometry (`~gammapy.maps.Geom`)"""
        return self.exposure.geom

    @property
    def needs_update(self):
        """Check whether the model component has drifted away from its support."""
        # TODO: simplify and clean up
        if isinstance(self.model, BackgroundModel):
            return False
        elif self.exposure is None:
            return True
        elif self.evaluation_mode == "global" or self.model.evaluation_radius is None:
            return False
        else:
            position = self.model.position
            separation = self._init_position.separation(position)
            update = separation > (self.model.evaluation_radius + CUTOUT_MARGIN)
        return update

    def update(self, exposure, psf, edisp, geom):
        """Update MapEvaluator, based on the current position of the model component.

        Parameters
        ----------
        exposure : `~gammapy.maps.Map`
            Exposure map.
        psf : `gammapy.cube.PSFMap`
            PSF map.
        edisp : `gammapy.cube.EDispMap`
            Edisp map.
        geom : `WcsGeom`
            Counts geom
        """
        # TODO: simplify and clean up
        log.debug("Updating model evaluator")
        # cache current position of the model component

        if isinstance(edisp, EDispKernelMap):
            self.edisp = edisp.get_edisp_kernel(self.model.position)
        elif isinstance(edisp, EDispMap):
            e_reco = geom.get_axis_by_name("energy").edges
            self.edisp = edisp.get_edisp_kernel(self.model.position, e_reco=e_reco)
        else:
            self.edisp = edisp

        if isinstance(psf, PSFMap):
            self.psf = psf.get_psf_kernel(self.model.position, geom=exposure.geom)
        else:
            self.psf = psf

        if self.evaluation_mode == "local" and self.model.evaluation_radius is not None:
            self._init_position = self.model.position
            if self.psf is not None:
                psf_width = np.max(self.psf.psf_kernel_map.geom.width)
            else:
                psf_width = 0 * u.deg

            width = psf_width + 2 * (self.model.evaluation_radius + CUTOUT_MARGIN)
            try:
                self.exposure = exposure.cutout(
                    position=self.model.position, width=width
                )
                self.contributes = True
            except (NoOverlapError, ValueError):
                self.contributes = False
        else:
            self.exposure = exposure

        self._pars_cached = None

    def compute_dnde(self):
        """Compute model differential flux at map pixel centers.

        Returns
        -------
        model_map : `~gammapy.maps.Map`
            Sky cube with data filled with evaluated model values.
            Units: ``cm-2 s-1 TeV-1 deg-2``
        """
        return self.model.evaluate_geom(self.geom, self.gti)

    def compute_flux(self):
        """Compute model integral flux over map pixel volumes.

        For now, we simply multiply dnde with bin volume.
        """
        return self.model.integrate_geom(self.geom, self.gti)

    def apply_exposure(self, flux):
        """Compute npred cube

        For now just divide flux cube by exposure
        """
        npred = (flux.quantity * self.exposure.quantity).to_value("")
        return Map.from_geom(self.geom, data=npred, unit="")

    def apply_psf(self, npred):
        """Convolve npred cube with PSF"""
        tmp = npred.convolve(self.psf)
        tmp.data[tmp.data < 0.0] = 0
        return tmp

    def apply_edisp(self, npred):
        """Convolve map data with energy dispersion.

        Parameters
        ----------
        npred : `~gammapy.maps.Map`
            Predicted counts in true energy bins

        Returns
        -------
        npred_reco : `~gammapy.maps.Map`
            Predicted counts in reco energy bins
        """
        return npred.apply_edisp(self.edisp)

    def compute_npred(self):
        """
        Evaluate model predicted counts.

        Returns
        -------
        npred : `~gammapy.maps.Map`
            Predicted counts on the map (in reco energy bins)
        """

        pars = list(self.model.parameters.values)
        npred = self._npred_cached
        if self._pars_cached != pars:
            self._pars_cached = pars
            if isinstance(self.model, BackgroundModel):
                npred = self.model.evaluate()
            else:
                flux = self.compute_flux()

                if self.model.apply_irf["exposure"]:
                    npred = self.apply_exposure(flux)

                if self.psf and self.model.apply_irf["psf"]:
                    npred = self.apply_psf(npred)

                if self.model.apply_irf["edisp"]:
                    npred = self.apply_edisp(npred)

            self._npred_cached = npred
        return npred

import numpy,import astropy,from astropy


#! /usr/bin/python
# differentail corrrection for
#
# mlpd /home/astro115/carmenes/data/svn/zero/CARM_VIS/J16167+672S.avc.dat  /home/astro115/carmenes/data/svn/other-rvs/J16167+672S/J16167+672S_hires.dat --fbeg 0.00022

# -*- coding: utf-8 -*-
# Copyright (c) 2012 Sebastian Schrter, Stefan Czesla, and Mathias Zechmeister

# The MIT License (MIT)

# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the "Software"),
# to deal in the Software without restriction, including without limitation the
# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
# sell copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:

# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Software.

# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.

# Note : The software is also available as part of the PyAstronomy package.
#        See: http://www.hs.uni-hamburg.de/DE/Ins/Per/Czesla/index.html

from __future__ import print_function, division
import numpy as np
from numpy import sum, pi, cos, sin, arctan2, exp, log, sqrt,\
                  dot, argmax, arange
#from pause import *
#from gplot import *
from scipy import optimize as op
import time
try:
   from pathos.pools import ProcessPool as Pool
except:
   pass

# for python3: emulate the nice python2 behaviour of map and zip
xmap = map
map = lambda *x: list(xmap(*x))
xzip = zip
zip = lambda *x: list(xzip(*x))

__version__ = '2021-08-18'
__author__ = 'Mathias Zechmeister'

def mod_abc(x, a):
   ''' sine model with multiple data set and offsets
   x - list of tuples with cosx and sinx terms
   '''
   y = [a[0]*cosx + a[1]*sinx + a[2+i] for i,(cosx,sinx) in enumerate(x)]
   return y

def mod_c(x, a):
   ''' sine model with multiple data set and offsets
   x - list of tuples with cosx and sinx terms
   '''
   y = [0*cosx + a[i] for i,cosx in enumerate(x)]
   return y

class Gls:
    """
    Compute the Generalized Lomb-Scargle (GLS) periodogram.

    The *Gls* class computes the error-weighted Lomb-Scargle periodogram as
    developed by [ZK09]_ using various possible normalizations.

    The constructor of *Gls* takes a *TimeSeries* instance (i.e., a light curve)
    as first argument. The constructor allows to pass keywords to adjust the
    `freq` array, which will be used to calculate the periodogram.

    The main result of the calculation, i.e., the power, are stored in the
    class property `power`.

    Parameters
    ----------
    lc : TimeSeries object or tuple or list
        The light curve data either in the form of a TimeSeries object (or any
        object providing the attributes time, flux, and error) or a tuple or list
        providing time as first element, flux as second element, and optionally,
        the error as third element.
    fbeg, fend : float, optional
        The beginning and end frequencies for the periodogram
        (inverse units of time axis).
    Pbeg, Pend : float, optional
        The beginning and end periods for the periodogram
        (same units as for time axis).
    ofac : int
        Oversampling factor of frequency grid (default=10).
    hifac : float
        Maximum frequency `freq` = `hifac` * (average Nyquist frequency)
        (default=1).
    freq : array, optional
        Contains the frequencies at which to calculate the periodogram.
        If given, fast and verbose option are not available.
        If not given, a frequency array will be automatically generated.
    norm : string, optional
        The normalization; either of "lnL", "Scargle", "HorneBaliunas", "Cumming", "wrms", "chisq".
        The default is unity ("lnL").
    ls : boolean, optional
        If True, the conventional Lomb-Scargle periodogram will be computed
        (default is False).
    fast : boolean, optional
        If True, recursive relations for trigonometric functions will be used
        leading to faster evaluation (default is False).
    verbose : boolean, optional
        Set True to obtain some statistical output (default is False).

    Attributes
    ----------
    power : array
        The normalized power of the GLS.
    freq : array
        The frequency array.
    ofac : int
        The oversampling factor of frequency grid.
    hifac : float
        The maximum frequency.
    t : array
        The abscissa data values.
    y : array
        The ordinate data values.
    e_y : array
        The errors of the data values.
    norm : string, {'lnL', 'Scargle', 'HorneBaliunas', 'Cumming', 'wrms', 'chisq'}
        The used normalization.

    Examples
    --------
    Create 1000 unevenly sampled data points with frequency=0.1,
    measurement error and Gaussian noise
    >>> time = np.random.uniform(54000., 56000., 1000)
    >>> flux = 0.15 * np.sin(2. * np.pi * time / 10.)

    Add some noise
    >>> error = 0.3 * np.ones(time.size)
    >>> flux += np.random.normal(0, error+0.2)

    Compute the full error-weighted Lomb-Periodogram
    in 'lnL' normalization and calculate the significance
    of the maximum peak.
    >>> gls = Gls((time, flux, error), verbose=True)

    >>> maxPower = gls.pmax
    >>> print("GLS maximum power: ", maxPower)
    >>> print("GLS statistics of maximum power peak: ", gls.stats(maxPower))
    >>> gls.plot(block=True)

    """
    # Available normalizations
    norms = ['dlnL', 'lnL', 'Scargle', 'HorneBaliunas', 'Cumming', 'wrms', 'chisq']

    def __init__(self, lc, fbeg=None, fend=None, Pbeg=None, Pend=None, ofac=10, hifac=1, freq=None, norm="dlnL", ls=False, fast=False, ncpus=None, verbose=False, **kwargs):

        self.freq = freq
        self.fbeg = fbeg
        self.fend = fend
        self.Pbeg = Pbeg
        self.Pend = Pend
        self.ofac = ofac
        self.hifac = hifac
        self.ls = ls
        self.norm = norm
        self.fast = fast
        self.ncpus = ncpus
        self.label = {'title': 'Maximum Likelihood Periodogram',
                      'xlabel': 'Frequency'}
        if "stats" in kwargs:
          print("Warning: 'stats' option is outdated. Please use 'verbose' instead.")
          verbose = kwargs["stats"]

        self._normcheck(norm)

        self._assignTimeSeries(lc)
        self._buildFreq()
        self._calcPeriodogram()
        self.pnorm(norm)
        self._peakPeriodogram()

        # Output statistics
        if verbose:
            self.info()

    def _assignTimeSeries(self, lcs):
      """
      A container class that holds the observed light curve.

      Parameters
      ----------
      time : array
          The time array.
      flux : array
          The observed flux/data.
      error : array, optional
          The error of the data values.

      """
      self.t = []
      self.y = []
      self.e_y = []
      self.N = 0
      self.Nj = len(lcs)
      for lc in lcs:
         if isinstance(lc, (tuple, list)):
             # t, y[, e_y] were given as list or tuple.
             if len(lc) in (2, 3):
                 t = np.ravel(lc[0])
                 y = np.ravel(lc[1])
                 e_y = None
                 if len(lc) == 3 and lc[2] is not None:
                     # Error has been specified.
                     e_y = np.ravel(lc[2])
             else:
                 raise(ValueError("lc is a list or tuple with " + str(len(lc)) + " elements. Needs to have 2 or 3 elements." + \
                                    " solution=Use 2 or 3 elements (t, y[, e_y]) or an instance of TimeSeries"))
         else:
             # Assume lc is an instance of TimeSeries.
             t, y, e_y = lc.time, lc.flux, lc.error
         self.t += [t]
         self.y += [y]
         self.e_y += [e_y]

         N = len(y)
         self.N += N

         # Re-check array length compatibility
         if (len(t) != N) or ((e_y is not None) and (len(e_y) != N)):
             raise(ValueError("Incompatible dimensions of input data arrays (time and flux [and error]). Current shapes are: " + \
                              ', '.join(str(np.shape(x)) for x in (t, y, e_y))))

      self.data = zip(self.t, self.y, self.e_y)
      self.tmin = min(map(min,self.t))
      self.th = [t - self.tmin for t in self.t]
      self.tbase = max(map(max,self.th))

    def _buildFreq(self):
        """
        Build frequency array (`freq` attribute).

        Attributes
        ----------
        fnyq : float
            Half of the average sampling frequency of the time series.

        """
        self.fstep = 1 / self.tbase / self.ofac   # frequency sampling depends on the time span, default for start frequency
        self.fnyq = 0.5 / self.tbase * self.N     # Nyquist frequency
        self.f = self.freq

        if self.freq is None:
            # Build frequency array if not present.
            if self.fbeg is None:
                self.fbeg = self.fstep if self.Pend is None else 1 / self.Pend
            if self.fend is None:
                self.fend = self.fnyq * self.hifac if self.Pbeg is None else 1 / self.Pbeg

            if self.fend <= self.fbeg:
                raise(ValueError("fend is smaller than (or equal to) fbeg but it must be larger." + \
                               "Choose fbeg and fend so that fend > fbeg."))

            self.freq = arange(self.fbeg, self.fend, self.fstep)
        elif self.fast:
            raise(ValueError("freq and fast cannot be used together."))

        self.nf = len(self.freq)

        # An ad-hoc estimate of the number of independent frequencies (Eq. (24) in ZK_09).
        self.M = (self.fend-self.fbeg) * self.tbase

    def lnL(self, theta, X, Y, e_Y, func):
       # the log-likelihood
       global L, chisqr, wtrms # a blob
       L = []   # log-likelihood for each instrument
       Ymod = func(X, theta)
       chisqr = []
       wtrms = []   # weighted rms for each instrument
       N = len(X)
       for y,e_y,ymod,lnf in zip(Y,e_Y,Ymod,theta[-N:]):
          # loop over data sets
          sigma2 = e_y**2 + lnf**2
          #weight = 1/sigma2
          chisqr += [np.sum((y-ymod)**2/sigma2)]
          L += [-0.5 * np.sum((y-ymod)**2/sigma2 + np.log(2*pi*sigma2))]
          wtrms += [np.sqrt(np.sum((y-ymod)**2/sigma2)/np.sum(1/sigma2))]
       return sum(L)

    def single_freq_fit(self, omega):

      # Circular frequencies
      X = [omega*th for th in self.th]
      cosX = map(cos, X)
      sinX = map(sin, X)
      a = op.fmin_powell(self.nll, self.a, args=(zip(cosX,sinX), self.y, self.e_y, mod_abc), disp=False)

      _a= a[0]
      _b= a[1]
      _off= a[2:2+self.Nj]
      _lnMLj = L
      _chisqr = chisqr
      lnML = sum(L)
      _wtrms = wtrms
      W = [np.sum(1/(e_y**2+jit**2)) for e_y,jit in zip(self.e_y, a[-self.Nj:])]
      _wrms = np.sqrt(np.sum(chisqr)/np.sum(W))   # a bit handwavy defined

      return (a, _a, _b, _off, _lnMLj, _chisqr, lnML, _wtrms, _wrms)

    def _calcPeriodogram(self):

        self._a, self._b, self.p, self.lnML = np.zeros((4, self.nf))
        self.par = []
        self._off = np.zeros((self.nf, self.Nj))
        self._lnMLj = np.zeros((self.nf, self.Nj))
        self._chij = np.zeros((self.nf, self.Nj))
        self._chisqr = np.zeros((self.nf, self.Nj))
        self._chi = np.zeros(self.nf)
        self._wrmsj = np.zeros((self.nf, self.Nj))
        self._wrms = np.zeros(self.nf)
        self._wtrms = np.zeros((self.nf, self.Nj))

        global L, chisqr, wtrms
        nll = lambda *args: -self.lnL(*args)
        #a0 = [0.]*self.Nj + [3.]*self.Nj # start guess for first frequency
        a0 = map(np.mean, self.y) + map(np.std, self.y)
        #print(a0)
        self.nll = nll
        # The model with only offset c
        a0 = op.fmin_powell(nll, a0, args=(self.th, self.y, self.e_y, mod_c), disp=False)
        self.lnML0 = sum(L)
        self.lnML0j = L
        self.a0 = a0
        W0 = [np.sum(e_y**2+jit**2) for e_y,jit in zip(self.e_y, a0[-self.Nj:])]
        self.wrms0 = np.sqrt(np.sum(chisqr)/np.sum(W0)*self.N)   # hand-wavy definition

        self.chisqr = chisqr
        self.wtrms = wtrms
        self.a = [np.median(a0[len(a0)//2:])]*2 + list(a0)   # start guess for first frequency

        # frequency grid
        omegas = [omega for omega in 2.*pi*self.freq]

        start_time = time.time()
        if self.ncpus is not None:
           with Pool(ncpus=self.ncpus) as thread:
              results = thread.map(self.single_freq_fit, omegas)

        for k, omega in enumerate(omegas):
           if self.ncpus is None:
              res_k = self.single_freq_fit(omega)
              print(end="%6.2f %%\r" % (k/(self.nf-1)*100))    # progress indicator
           else:
              res_k = results[k]

           self.par.append(res_k[0])
           self._a[k] = res_k[1]
           self._b[k] = res_k[2]
           self._off[k] = res_k[3]
           self._lnMLj[k] = res_k[4]
           self._chisqr[k] = res_k[5]
           self.lnML[k] = res_k[6]
           self._wtrms[k] = res_k[7]
           self._wrms[k] = res_k[8]

        self.p = self.lnML

        print("--- %s CPUs done in %s seconds --- " % (self.ncpus, time.time() - start_time))

    def _normcheck(self, norm):
        """
        Check normalization

        Parameters
        ----------
        norm : string
            Normalization string

        """
        if norm not in self.norms:
            raise(ValueError("Unknown norm: " + str(norm) + ". " + \
                "Use either of " + ', '.join(self.norms)))

    def pnorm(self, norm="dlnL"):
        """
        Assign or modify normalization (can be done afterwards).

        Parameters
        ----------
        norm : string, optional
            The normalization to be used (default is 'lnL').

        Examples
        --------
        >>> gls.pnorm('wrms')

        """
        self._normcheck(norm)
        self.norm = norm
        p = self.p
        power = p   # default lnL
        self.label["ylabel"] = norm

        if norm == "Scargle":
            popvar = input('pyTiming::gls - Input a priori known population variance:')
            power = p / float(popvar)
        elif norm == "HorneBaliunas":
            power = (self.N-1)/2. * p
        elif norm == "Cumming":
            power = (self.N-3)/2. * p / (1.-self.p.max())
        elif norm == "chisq":
            power = self._YY *self.wsum * (1.-p)
            self.label["ylabel"] = "chisq"
        elif norm == "wrms":
            power = sqrt(self._YY*(1.-p))
            self.label["ylabel"] = "wrms"
        elif norm == "dlnL":
            self.powerj = self._lnMLj - self.lnML0j
            power = self.lnML - self.lnML0
            self.label["ylabel"] = "$\Delta$lnL"
        else:
            self.powerj = self._lnMLj.T
            power = self.lnML

        self.power = power

    def _peakPeriodogram(self):
        """
        Analyze the highest periodogram peak.
        """
        # Index with maximum power
        k = argmax(self.p)
        # Maximum power
        self.pmax = pmax = self.p[k]
        self.rms = rms = self._wrms[k]
        # Statistics of highest peak
        self.hpstat = p = {}

        # Best parameters
        p["fbest"] = fbest = self.freq[k]
        p["P"] = 1/fbest
        p["amp"] = amp = sqrt(self._a[k]**2 + self._b[k]**2)
        p["ph"] = ph = arctan2(self._a[k], self._b[k]) / (2.*pi)
        p["T0"]  = self.tmin - ph/fbest
        p["offset"] = self._off[k]
        #print(len(self.par),self.Nj)
        p["jitter"] = self.par[k][-self.Nj:]

        # Error estimates
        p["amp_err"] = sqrt(2./self.N) * rms
        p["ph_err"] = ph_err = sqrt(2./self.N) * rms/amp/(2.*pi)
        p["T0_err"] = ph_err / fbest
        p["offset_err"] = sqrt(1./self.N) * rms

        # Get the curvature in the power peak by fitting a parabola y=aa*x^2
        if 1 < k < self.nf-2:
            # Shift the parabola origin to power peak
            xh = (self.freq[k-1:k+2] - self.freq[k])**2
            yh = self.p[k-1:k+2] - pmax
            # Calculate the curvature (final equation from least square)
            aa = dot(yh, xh) / dot(xh, xh)
            p["f_err"] = e_f = sqrt(-2./self.N / aa * (1.-self.pmax))
            p["Psin_err"] = e_f / fbest**2
        else:
            self.hpstat["f_err"] = np.nan
            self.hpstat["Psin_err"] = np.nan
            print("WARNING: Highest peak is at the edge of the frequency range.\nNo output of frequency error.\nIncrease frequency range to sample the peak maximum.")

    def sinmod(self, t):
        """
        Calcuate best-fit sine curve.

        Parameters
        ----------
        t : array
            Time array at which to calculate the sine.

        Returns
        -------
        Sine curve : array
            The best-fit sine curve (i.e., that for which the
            power is maximal).
        """
        try:
            p = self.hpstat
            if isinstance(t, (list, tuple)):
               return [p["amp"] * sin(2*np.pi*p["fbest"]*(tj-p["T0"])) + offj for tj,offj in zip(t,p["offset"])]
            else:
               return p["amp"] * sin(2*np.pi*p["fbest"]*(t-p["T0"]))
        except Exception as e:
            print("Failed to calcuate best-fit sine curve.")
            raise(e)

    def info(self, stdout=True):
        """
        Prints some basic statistical output screen.
        """
        lines = ("MLP - statistical output",
           "-----------------------------------",
           "Number of input points:     %6d" % self.N,
           "Weighted rms of dataset:    %f"  % self.wrms0,
           "Time base:                  %f"  % self.tbase,
           "Number of frequency points: %6d" % self.nf,
           "Weighted rms of residuals:  %f" % self.rms,
           "")

        k = argmax(self.p)
        Yfit = self.sinmod([t for t,y,e in self.data])
        W = [1/(e_y**2+jit**2) for e_y,jit in zip(self.e_y, self.hpstat["jitter"])]
        wrmsj = [np.sqrt(np.sum(w*(yfit-y)**2)/np.sum(w)) for y,w,yfit in zip(self.y, W, Yfit)]

        header = "%s:   %10s %10s  %10s %10s" % ("j", "lnL0", "dlnL", "wrms","jit")
        fmt = "%d:   %10.3f %10.3f  %10.3f %10.3f"
        if self.e_y is not None:
           header += " %10s" % "internal error"
           fmt += " %10.3f"
        lines += (header,)

        col = list(self.lnML0j), list(self._lnMLj[k]-self.lnML0j)
        col += wrmsj, self.hpstat["jitter"]
        if self.e_y is not None:
           col += [np.mean(1./e_y**2)**-0.5 for e_y in self.e_y],

        for j,line in enumerate(zip(*col)):
           lines += fmt % ((j,)+line),

        wrmsall = np.sqrt(np.sum([np.sum(w*(yfit-y)**2) for y,w,yfit in zip(self.y, W, Yfit)])/sum(map(sum,W)))
        lines += "-"*60,
        lines += "all: %10.3f %10.3f  %10.3f\n" % (self.lnML0, self.lnML.max()-self.lnML0, wrmsall),

        self.best = self.hpstat
        lines += ("Best sine frequency:  {fbest:f} +/- {f_err:f}",
             "Best sine period:     {P:f} +/- {Psin_err:f}",
             "Amplitude:            {amp:f} +/- {amp_err:f}")
             #"Phase (ph):           %f +/- %f" % (self.best["ph"], self.best["ph_err"]),
             #"Phase (T0):           %f +/- %f" % (self.best["T0"], self.best["T0_err"]))
        for j,off in enumerate(self.best["offset"]):
           lines += "Offset %d:             %f +/- %f" % (j, off, self.best["offset_err"]),
        #for j,jit in enumerate(self.best["jitter"]:
        #   print("Jitter %d:             %f +/- %f" % (j, jit, self.best["offset_err"]))
        lines += "-----------------------------------",
        text = "\n".join(lines).format(**self.best)
        if stdout:
           print(text)
        else:
           return text

    def plot(self, block=False, period=False):
        """
        Create a plot.
        """
        try:
            import matplotlib
            import matplotlib.pyplot as plt
            from matplotlib.ticker import FormatStrFormatter
        except ImportError:
            raise(ImportError("Could not import matplotlib.pylab."))

        fig = plt.figure()
        fig.subplots_adjust(hspace=0.15, wspace=0.08, right=0.97, top=0.95)
        ax = fig.add_subplot(3, 1, 1)
        ax.set_title("Maximum likelihood periodogram")
        if period:
           ax.set_xscale("log")
           ax.set_xlabel("Period")
        else:
           ax.set_xlabel("Frequency")
        ax.set_ylabel(self.label["ylabel"])
        #pause()
        for pj in self.powerj.T:
           ax.plot(1/self.freq if period else self.freq, pj, '-')
        ax.plot(1/self.freq if period else self.freq, self.power, 'k-')

        fbest, T0 = self.hpstat["fbest"], self.hpstat["T0"]
        # Data and model
        datstyle = {'fmt':'.', 'capsize':0}
        tt = arange(self.tmin, self.tmin+self.tbase, 0.01/fbest)
        ymod = self.sinmod(tt)
        yfit = self.sinmod([t for t,y,e in self.data])
        ax1 = fig.add_subplot(3, 2, 3)
        # ax1.set_xlabel("Time")
        ax1.set_ylabel("Data")
        plt.setp(ax1.get_xticklabels(), visible=False)
        #ax1.errorbar(self.t, self.y, **datstyle)
        for (tj,yj,e_yj),off in zip(self.data, self.hpstat['offset']):
           ax1.errorbar(tj, yj-off, yerr=e_yj, **datstyle)
        ax1.plot(tt, ymod, 'k-')

        tt = arange(T0, T0+1/fbest, 0.01/fbest)
        yy = self.sinmod(tt)
        ax2 = fig.add_subplot(3, 2, 4, sharey=ax1)
        plt.setp(ax2.get_xticklabels(), visible=False)
        plt.setp(ax2.get_yticklabels(), visible=False)
        # ax2.set_xlabel("Time")
        # ax2.set_ylabel("Data")
        for (tj,yj,e_yj),off in zip(self.data, self.hpstat['offset']):
           ax2.errorbar(tj*fbest % 1, yj-off, yerr=e_yj, **datstyle)
        xx = tt*fbest % 1
        ii = np.argsort(xx)
        ax2.plot(xx[ii], yy[ii], 'k-')

        # Residuals
        #yres = self.y - yfit
        ax3 = fig.add_subplot(3, 2, 5, sharex=ax1)
        ax3.set_xlabel("Time")
        ax3.set_ylabel("Residuals")
        for (tj,yj,e_yj),yjfit in zip(self.data, yfit):
           ax3.errorbar(tj, yj-yjfit, yerr=e_yj, **datstyle)
        ax3.plot([self.tmin, self.tmin+self.tbase], [0,0], 'k-')

        ax4 = fig.add_subplot(3, 2, 6, sharex=ax2, sharey=ax3)
        # ax4.set_title("Data")
        ax4.set_xlabel("Phase")
        # ax4.set_ylabel("Data")
        plt.setp(ax4.get_yticklabels(), visible=False)
        for (tj,yj,e_yj),yjfit in zip(self.data, yfit):
           ax4.errorbar(tj*fbest % 1,  yj-yjfit, yerr=e_yj, **datstyle)
        ax4.plot([0,1], [0,0], 'k-')

        if hasattr(plt.get_current_fig_manager(), 'toolbar'):
            # check seems not needed when "TkAgg" is set
            plt.get_current_fig_manager().toolbar.pan()
        #t = fig.canvas.toolbar
        #plt.ToggleTool(plt.wx_ids['Pan'], False)
        if block:
           print("Close the plot to continue.")
        else:
           plt.ion()
        plt.show()
        # plt.show(block=block) # unexpected keyword argument 'block' in older matplotlib
        return plt

    def prob(self, Pn):
        """
        Probability of obtaining the given power.

        Calculate the probability to obtain a power higher than
        `Pn` from the noise, which is assumed to be Gaussian.

        .. note:: Normalization
          (see [ZK09]_ for further details).

          - `Scargle`:
          .. math::
            exp(-Pn)

          - `HorneBaliunas`:
          .. math::
            \\left(1 - 2 \\times \\frac{Pn}{N-1} \\right)^{(N-3)/2}

          - `Cumming`:
          .. math::
            \\left(1+2\\times \\frac{Pn}{N-3}\\right)^{-(N-3)/2}

        Parameters
        ----------
        Pn : float
            Power threshold.

        Returns
        -------
        Probability : float
            The probability to obtain a power equal or
            higher than the threshold from the noise.

        """
        self._normcheck(self.norm)
        if self.norm == "lnL": return (1.-Pn)**((self.N-3.)/2.)
        if self.norm == "Scargle": return exp(-Pn)
        if self.norm == "HorneBaliunas": return (1.-2.*Pn/(self.N-1.))**((self.N-3.)/2.)
        if self.norm == "Cumming": return (1.+2.*Pn/(self.N-3.))**(-(self.N-3.)/2.)
        if self.norm == "wrms": return (Pn**2/self._YY)**((self.N-3.)/2.)
        if self.norm == "chisq": return (Pn/self._YY/self.wsum)**((self.N-3.)/2.)

    def probInv(self, Prob):
        """
        Calculate minimum power for given probability.

        This function is the inverse of `Prob(Pn)`.
        Returns the minimum power for a given probability threshold `Prob`.

        Parameters
        ----------
        Prob : float
            Probability threshold.

        Returns
        -------
        Power threshold : float
            The minimum power for the given false-alarm probability threshold.

        """
        self._normcheck(self.norm)
        if self.norm == "lnL": return 1.-Prob**(2./(self.N-3.))
        if self.norm == "Scargle": return -log(Prob)
        if self.norm == "HorneBaliunas": return (self.N-1) / 2. * (1.-Prob**(2./(self.N-3)))
        if self.norm == "Cumming": return (self.N-3) / 2. * (Prob**(-2./(self.N-3.))-1.)
        if self.norm == "wrms": return sqrt(self._YY * Prob**(2./(self.N-3.)))
        if self.norm == "chisq": return self._YY * self.wsum * Prob**(2./(self.N-3.))

    def FAP(self, Pn):
        """
        Obtain the false-alarm probability (FAP).

        The FAP denotes the probability that at least one out of M independent
        power values in a prescribed search band of a power spectrum computed
        from a white-noise time series is as large as or larger than the
        threshold, `Pn`. It is assessed through

        .. math:: FAP(Pn) = 1 - (1-Prob(P>Pn))^M \\; ,

        where "Prob(P>Pn)" depends on the type of periodogram and normalization
        and is calculated by using the *prob* method; *M* is the number of
        independent power values and is computed internally.

        Parameters
        ----------
        Pn : float
            Power threshold.

        Returns
        -------
        FAP : float
            False alarm probability.

        """
        prob = self.M * self.prob(Pn)
        if prob > 0.01:
           return 1. - (1.-self.prob(Pn))**self.M
        return prob

    def powerLevel(self, FAPlevel):
        """
        Power threshold for FAP level.

        Parameters
        ----------
        FAPlevel : float or array_like
              "False Alarm Probability" threshold

        Returns
        -------
        Threshold : float or array
            The power threshold pertaining to a specified false-alarm
            probability (FAP). Powers exceeding this threshold have FAPs
            smaller than FAPlevel.

        """
        Prob = 1. - (1.-FAPlevel)**(1./self.M)
        return self.probInv(Prob)

    def stats(self, Pn):
        """
        Obtain basic statistics for power threshold.

        Parameters
        ----------
        Pn : float
            Power threshold.

        Returns
        -------
        Statistics : dictionary
            A dictionary containing {'Pn': *Pn*, 'Prob': *Prob(Pn)* ,
            'FAP': *FAP(Pn)*} for the specified power threshold, *Pn*.

        """
        return {'Pn': Pn, 'Prob': self.prob(Pn), 'FAP': self.FAP(Pn)}

    def toFile(self, ofile, header=True):
        """
        Write periodogram to file.

        Parameters
        ----------
        ofile : string
            Name of the output file.

        """
        with open(ofile, 'w') as f:
            if header:
               f.write("# Generalized Lomb-Scargle periodogram\n")
               f.write("# Parameters:\n")
               if hasattr(self, 'df'):
                  f.write("#    Data file: %s\n" % self.df)
               f.write("#    ofac     : %s\n" % self.ofac)
               f.write("#    norm     : %s\n" % self.norm)
               f.write("# 1) Frequency, 2) Normalized power\n")
            for line in zip(self.freq, self.power, *self.powerj.T.tolist()):
               f.write(("%f "*len(line)) % line + "\n")

        print("Results have been written to file: ", ofile)

def example():
    # Run the example in the Gls class.
    print("--- EXAMPLE CALCULATION ---")
    import doctest
    exec(doctest.script_from_examples(Gls.__doc__))
    print("----------------------------------------------------")

if __name__ == "__main__":

  import argparse

  parser = argparse.ArgumentParser(description='Generalized Lomb-Scargle periodogram.', add_help=False)
  argadd = parser.add_argument   # function short cut
  argadd('df', nargs='*',
                 help='Data file (three columns: time, data, error). If not specified example will be shown.')
  argadd('-fbeg', '--fbeg', type=float, help="Starting frequency for periodogram.")
  argadd('-fend', '--fend', type=float, help="Stopping frequency for periodogram.")
  argadd('-Pbeg', '--Pbeg', type=float, help="Starting period for periodogram.")
  argadd('-Pend', '--Pend', type=float, help="Stopping period for periodogram.")
  argadd('-ofac', '--ofac', type=float, help="Oversampling factor (default=10).", default=10)
  argadd('-hifac', '--hifac', type=float, help="Maximum frequency (default=1).", default=1)
  argadd('-fast', '--fast', help="Use trigonometric recurrences.", action='store_true')
  argadd('-nojit', '--nojit', help="Optimise jitter.", dest='jit', action='store_false')
  argadd('-norm', '--norm', help="The normalization (default=dlnL).", choices=Gls.norms, default='dlnL')
  argadd('-ofile', '--ofile', type=str, help="Output file for results.")
  argadd('-noplot', '--noplot', help="Suppress plots.", dest='plot', action='store_false')
  argadd('-nostat', '--nostat', help="Switch off statistical output on screen.", dest='verbose',
                 action='store_false')
  argadd('-?', '-h', '-help', '--help', help='Show this help message and exit.', action='help')

  args = vars(parser.parse_args())
  df = args.pop('df')
  ofile = args.pop('ofile')
  plot = args.pop('plot')

  if df is None:
    # No data file given. Show example:
    example()
    print("Available options:")
    parser.print_help()
    exit(0)

  # A data file has been given.
  try:
   DAT = []
   for dfi in df:
     dat = np.loadtxt(dfi, unpack=True)
     tye = None
     if len(dat) > 1:
        tye = dat[0], dat[1]
     if len(dat) > 2:
        tye += dat[2],
     DAT += [tye]
   tye = DAT
  except Exception as e:
     print("An error occurred while trying to read data file: ")
     print("  " + str(e))
     exit(9)

  gls = Gls(tye, **args)

  if ofile:
     gls.df = df
     gls.toFile(ofile)

  if plot:
     gls.plot(block=True)

#import numpy as np
#x = np.genfromtxt('/home/astro115/carmenes/data/svn/serval/CARM_VIS/J11421+267/J11421+267.rvc.dat', usecols=(0,1,2)).T
#print(x)

##g = Gls(tuple(x),fbeg=0.37,fend=0.384, plot=True)
#ml = Gls(tuple(x),fend=1., plot=True)
##g.plot()
##g=Gls(tuple(x), plot=True)

##import numpy as np; import mlp; g=mlp.Gls(tuple(x),fbeg=0.37,fend=0.384); g.plot()
##import numpy as np; import gls; g=gls.Gls(tuple(x),fbeg=0.37,fend=0.384);
#import numpy as np; import gls; g=gls.Gls(tuple(x),fend=1.);
#from gplot import*
#gplot(ml.freq,ml.p,',',g.freq,g.p)
#g.plot()