"""_summary_
"""
import os
from glob import glob
from astropy.io import fits
from L1_pipeline.ref_combine import combine_images


def combine_ref_func(
    ref_path: str,
    output_path: str,
    chip_id: str = "01"
) -> None:
    """_summary_

    Args:
        ref_path (str): _description_
        output_path (str): _description_
        chip_id (str, optional): _description_. Defaults to "01".
    """
    bias_path_list = glob(ref_path + '*/CSST_MSC_MS_BIAS_*_' + chip_id + '_*')
    dark_path_list = glob(ref_path + '*/CSST_MSC_MS_DARK_*_' + chip_id + '_*')
    flat_path_list = glob(ref_path + '*/CSST_MSC_MS_FLAT_*_' + chip_id + '_*')
    bias, dark, flat = combine_images(b_p_lst=bias_path_list,
                                      d_p_lst=dark_path_list,
                                      f_p_lst=flat_path_list, )
    bias_out_path = os.path.join(output_path, "bias_" + str(chip_id) + ".fits")
    dark_out_path = os.path.join(output_path, "dark_" + str(chip_id) + ".fits")
    flat_out_path = os.path.join(output_path, "flat_" + str(chip_id) + ".fits")
    hdu = fits.PrimaryHDU(bias)
    hdu.writeto(bias_out_path, overwrite=True)
    hdu = fits.PrimaryHDU(dark)
    hdu.writeto(dark_out_path, overwrite=True)
    hdu = fits.PrimaryHDU(flat)
    hdu.writeto(flat_out_path, overwrite=True)


if __name__ == "__main__":
    REFERENCE_PATH = "/public/share/yangxuliu/CSSOSDataProductsSims/outputs_cali/"
    OUTPUT_DIR = "/public/home/fangyuedong/project/calib_data"
    # CHIP_IDS = '08'
    CHIP_IDS = ['06', '07', '08', '09', '11', '12', '13', '14', '15',
                '16', '17', '18', '19', '20', '22', '23', '24', '25']
    for CHIP_ID in CHIP_IDS:
        combine_ref_func(
            ref_path=REFERENCE_PATH,
            output_path=OUTPUT_DIR,
            chip_id=CHIP_ID
        )
