"""
Identifier:     csst-l1/msc/csst_msc_instrument/csst_msc_instrument/image/basic.py
Name:           basic.py
Description:    Basic instrument effect correction.
Author:         Li Shao (shaoli@nao.cas.cn)
Created:        2023-11-16
Modified-History:
    2023-11-16, Li Shao, created
    2023-11-22, Li Shao, split from common_detector
    2024-02-06, Li Shao, move to csst_msc_instrument
"""

import numpy as np

__all__ = ["subtract_bias", "subtract_dark", ]


def subtract_bias(image: np.ndarray,
                  image_err: np.ndarray,
                  bias: np.ndarray,
                  bias_err: np.ndarray,
                  ) -> tuple[np.ndarray, np.ndarray]:
    """
    Subtract bias.

    Subtract bias.

    Parameters
    ----------
    image : numpy.ndarray
        The input image to be corrected.
    image_err : numpy.ndarray
        The uncertainty of input image.
    bias : numpy.ndarray
        The input bias to be subtracted.
    bias_err : numpy.ndarray
        The uncertainty of input bias.

    Returns
    -------
    output : np.ndarray
        Output corrected image.
    output_err : np.ndarray
        Output uncertainty map.
    """

    output = image - bias
    output_err = np.sqrt(image_err ** 2 + bias_err ** 2)

    return output, output_err


def subtract_dark(image: np.ndarray,
                  image_err: np.ndarray,
                  dark: np.ndarray,
                  dark_err: np.ndarray,
                  tdark_image: float | int = 1.0,
                  ) -> tuple[np.ndarray, np.ndarray]:
    """
    Subtract dark current.

    Subtract dark current.

    Parameters
    ----------
    image : numpy.ndarray
        The input image to be corrected.
    image_err : numpy.ndarray
        The uncertainty of input image.
    dark : numpy.ndarray
        The input dark current image to be subtracted.
    dark_err : numpy.ndarray
        The uncertainty of input dark current.
    tdark_image : float or int, optional
        The effective dark current cumulation time of input image. Default value is 1.0.

    Returns
    -------
    output : np.ndarray
        Output corrected image.
    output_err : np.ndarray
        Output uncertainty map.
    """

    output = image - dark * tdark_image
    output_err = np.sqrt(image_err ** 2 + (dark_err * tdark_image) ** 2)

    return output, output_err
