# Path: yatsm/accel.py
# @_doublewrap
# def try_jit(f, *args, **kwargs):
#     """ Apply numba.jit to function ``f`` if Numba is available
# 
#     Accepts arguments to Numba jit function (signature, nopython, etc.).
# 
#     Examples:
# 
#     .. code-block:: python
# 
#         @try_jit
#         @try_jit()
#         @try_jit(nopython=True)
#         @try_jit("float32[:](float32[:], float32[:])", nopython=True)
# 
#     """
#     if has_numba:
#         @wraps(f)
#         def wrap(*args, **kwargs):
#             return nb.jit(*args, **kwargs)
#         return wrap(f, *args, **kwargs)
#     else:
#         return f
# 
# Path: yatsm/regression/robust_fit.py
# EPS = np.finfo('float').eps
# def bisquare(resid, c=4.685):
# def mad(resid, c=0.6745):
# def _check_converge(x0, x, tol=1e-8):
# def _weight_fit(X, y, w):
#     def __init__(self, M=bisquare, tune=4.685,
#                  scale_est=mad, scale_constant=0.6745,
#                  update_scale=True, maxiter=50, tol=1e-8):
#     def fit(self, X, y):
#     def predict(self, X):
#     def __str__(self):
# class RLM(sklearn.base.BaseEstimator):


import numpy as np
from .accel import try_jit
from .regression import robust_fit as rlm
    from statsmodels.nonparametric import smoothers_lowess
    procedure was ported from CCDC v9.3.

    Args:
        x (ndarray): array of ordinal dates
        Y (ndarray): matrix of observed spectra
        n_year (float): "number of years to mask"
        crit (float): critical value for masking clouds/shadows
        green (int): 0 indexed value for green band in Y
            (default: 1)
        swir1 (int): 0 indexed value for SWIR (~1.55-1.75um) band
            in Y (default: 4)
        maxiter (int): maximum iterations for RLM fit

    Returns:
        mask (np.ndarray): mask where False indicates values to be masked

    """
    green = Y[green, :]
    swir1 = Y[swir1, :]

    n_year = np.ceil(n_year)
    w = 2.0 * np.pi / ndays

    X = np.array([np.ones_like(x),
                  np.cos(w * x),
                  np.sin(w * x),
                  np.cos(w / n_year * x),
                  np.sin(w / n_year * x)]).T

    green_RLM = rlm.RLM(M=rlm.bisquare, maxiter=maxiter).fit(X, green)

# Path: yatsm/accel.py
# @_doublewrap
# def try_jit(f, *args, **kwargs):
#     """ Apply numba.jit to function ``f`` if Numba is available
# 
#     Accepts arguments to Numba jit function (signature, nopython, etc.).
# 
#     Examples:
# 
#     .. code-block:: python
# 
#         @try_jit
#         @try_jit()
#         @try_jit(nopython=True)
#         @try_jit("float32[:](float32[:], float32[:])", nopython=True)
# 
#     """
#     if has_numba:
#         @wraps(f)
#         def wrap(*args, **kwargs):
#             return nb.jit(*args, **kwargs)
#         return wrap(f, *args, **kwargs)
#     else:
#         return f
# 
# Path: yatsm/regression/robust_fit.py
# EPS = np.finfo('float').eps
# def bisquare(resid, c=4.685):
# def mad(resid, c=0.6745):
# def _check_converge(x0, x, tol=1e-8):
# def _weight_fit(X, y, w):
#     def __init__(self, M=bisquare, tune=4.685,
#                  scale_est=mad, scale_constant=0.6745,
#                  update_scale=True, maxiter=50, tol=1e-8):
#     def fit(self, X, y):
#     def predict(self, X):
#     def __str__(self):
# class RLM(sklearn.base.BaseEstimator):


# Path: yatsm/masking.py
import numpy as np
from .accel import try_jit
from .regression import robust_fit as rlm
    from statsmodels.nonparametric import smoothers_lowess

    procedure was ported from CCDC v9.3.

    Args:
        x (ndarray): array of ordinal dates
        Y (ndarray): matrix of observed spectra
        n_year (float): "number of years to mask"
        crit (float): critical value for masking clouds/shadows
        green (int): 0 indexed value for green band in Y
            (default: 1)
        swir1 (int): 0 indexed value for SWIR (~1.55-1.75um) band
            in Y (default: 4)
        maxiter (int): maximum iterations for RLM fit

    Returns:
        mask (np.ndarray): mask where False indicates values to be masked

    """
    green = Y[green, :]
    swir1 = Y[swir1, :]

    n_year = np.ceil(n_year)
    w = 2.0 * np.pi / ndays

    X = np.array([np.ones_like(x),
                  np.cos(w * x),
                  np.sin(w * x),
                  np.cos(w / n_year * x),
                  np.sin(w / n_year * x)]).T

    green_RLM = rlm.RLM(M=rlm.bisquare, maxiter=maxiter).fit(X, green)

    swir1_RLM = rlm.RLM(M=rlm.bisquare, maxiter=maxiter).fit(X, swir1)