# Path: redmapper/utilities.py
# def gaussFunction(x, *p):
#     """
#     Compute a normalizes Gaussian G(x) for a given set of parameters
# 
#     Parameters
#     ----------
#     x: `np.array`
#        Float array of x values
#     A: `float`
#        Normalization of the Gaussian
#     mu: `float`
#        Mean value (mu) for the Gaussian
#     sigma: `float`
#        Gaussian sigma
# 
#     Returns
#     -------
#     pdf: `np.array`
#        Float array of Gaussian pdf values
#     """
#     A, mu, sigma = p
#     return A*np.exp(-(x-mu)**2./(2.*sigma**2))
# 
# Path: redmapper/utilities.py
# def interpol(v, x, xout):
#     """
#     Port of IDL interpol.py.  Does fast and simple linear interpolation.
# 
#     Parameters
#     ----------
#     v: `np.array`
#        Float array of y (dependent) values to interpolate between
#     x: `np.array`
#        Float array of x (independent) values to interpolate between
#     xout: `np.array`
#        Float array of x values to compute interpolated values
# 
#     Returns
#     -------
#     yout: `np.array`
#        Float array of y output values associated with xout
#     """
# 
#     m = v.size
#     nOut = m
# 
#     s = np.clip(np.searchsorted(x, xout) - 1, 0, m - 2)
# 
#     diff = v[s + 1] - v[s]
# 
#     return (xout - x[s]) * diff / (x[s + 1] - x[s]) + v[s]


import fitsio
import esutil
import numpy as np
from .utilities import gaussFunction
from .utilities import interpol
    """

    def find_center(self):
        """
        Find the center using the CenteringWcenZred algorithm.

        This algorithm computes the primary centering likelihood algorithm by
        computing the connectivity of the members, as well as ensuring
        consistency between zred of the candidates and the cluster redshift.

        Will set self.maxind (index of best center); self.ra, self.dec
        (position of best center); self.ngood (number of good candidates);
        self.index[:] (indices of all the candidates); self.p_cen[:] (pcen
        centering probabilities); self.q_cen[:] (qcen unused miss
        probabilities); self.p_sat[:] (p_sat satellite probabilities).

        Returns
        -------
        success: `bool`
           True when a center is successfully found. (Always True).
        """
        # These are the galaxies considered as candidate centers
        use, = np.where((self.cluster.neighbors.r < self.cluster.r_lambda) &
                        (self.cluster.neighbors.pfree >= self.config.percolation_pbcg_cut) &
                        (self.cluster.neighbors.zred_chisq < self.config.wcen_zred_chisq_max) &
                        ((self.cluster.neighbors.pmem > 0.0) |
                         (np.abs(self.cluster.redshift - self.cluster.neighbors.zred) < 5.0 * self.cluster.neighbors.zred_e)))

        # Do the phi_cen filter
        mbar = self.cluster.mstar + self.config.wcen_Delta0 + self.config.wcen_Delta1 * np.log(self.cluster.Lambda / self.config.wcen_pivot)

# Path: redmapper/utilities.py
# def gaussFunction(x, *p):
#     """
#     Compute a normalizes Gaussian G(x) for a given set of parameters
# 
#     Parameters
#     ----------
#     x: `np.array`
#        Float array of x values
#     A: `float`
#        Normalization of the Gaussian
#     mu: `float`
#        Mean value (mu) for the Gaussian
#     sigma: `float`
#        Gaussian sigma
# 
#     Returns
#     -------
#     pdf: `np.array`
#        Float array of Gaussian pdf values
#     """
#     A, mu, sigma = p
#     return A*np.exp(-(x-mu)**2./(2.*sigma**2))
# 
# Path: redmapper/utilities.py
# def interpol(v, x, xout):
#     """
#     Port of IDL interpol.py.  Does fast and simple linear interpolation.
# 
#     Parameters
#     ----------
#     v: `np.array`
#        Float array of y (dependent) values to interpolate between
#     x: `np.array`
#        Float array of x (independent) values to interpolate between
#     xout: `np.array`
#        Float array of x values to compute interpolated values
# 
#     Returns
#     -------
#     yout: `np.array`
#        Float array of y output values associated with xout
#     """
# 
#     m = v.size
#     nOut = m
# 
#     s = np.clip(np.searchsorted(x, xout) - 1, 0, m - 2)
# 
#     diff = v[s + 1] - v[s]
# 
#     return (xout - x[s]) * diff / (x[s + 1] - x[s]) + v[s]


# Path: redmapper/centering.py
import fitsio
import esutil
import numpy as np
from .utilities import gaussFunction
from .utilities import interpol

    """

    def find_center(self):
        """
        Find the center using the CenteringWcenZred algorithm.

        This algorithm computes the primary centering likelihood algorithm by
        computing the connectivity of the members, as well as ensuring
        consistency between zred of the candidates and the cluster redshift.

        Will set self.maxind (index of best center); self.ra, self.dec
        (position of best center); self.ngood (number of good candidates);
        self.index[:] (indices of all the candidates); self.p_cen[:] (pcen
        centering probabilities); self.q_cen[:] (qcen unused miss
        probabilities); self.p_sat[:] (p_sat satellite probabilities).

        Returns
        -------
        success: `bool`
           True when a center is successfully found. (Always True).
        """
        # These are the galaxies considered as candidate centers
        use, = np.where((self.cluster.neighbors.r < self.cluster.r_lambda) &
                        (self.cluster.neighbors.pfree >= self.config.percolation_pbcg_cut) &
                        (self.cluster.neighbors.zred_chisq < self.config.wcen_zred_chisq_max) &
                        ((self.cluster.neighbors.pmem > 0.0) |
                         (np.abs(self.cluster.redshift - self.cluster.neighbors.zred) < 5.0 * self.cluster.neighbors.zred_e)))

        # Do the phi_cen filter
        mbar = self.cluster.mstar + self.config.wcen_Delta0 + self.config.wcen_Delta1 * np.log(self.cluster.Lambda / self.config.wcen_pivot)

        phi_cen = gaussFunction(self.cluster.neighbors.refmag[use],