"""
Script to build a file with a CXB model
"""
import logging
from astropy.io import fits
from ecpi.common import add_path_caldb_eclairs
from ecpi.simu.lib.sources import ModelCxbShapeBasedEnergy
[docs]def create_cxb_shape_based_file(cxb_filename="ECL-CXB-BKG_model_per_channel_1s.fits"):
"""Creation of the cxb background file
:param cxb_filename: name of the cxb file
:type cxb_filename: string
:return: cosmic x-ray background file
:type: fits file
"""
logging.info("creation of CXB background file from Moretti formula")
cxb = ModelCxbShapeBasedEnergy()
nb_channel = cxb.context._mdl_effect.chan_nb()
i_chan = cxb.context._mdl_effect._channel[0]
f_chan = cxb.context._mdl_effect._channel[-1]
print(f'First channel number {i_chan}, and last channel number {f_chan}')
prihdr = fits.Header()
prihdu = fits.PrimaryHDU(header=prihdr)
hdu_list = [prihdu]
for idx_chan in range(i_chan, f_chan):
logging.info(f"cxb for channel {idx_chan}")
# cxb.set_idx_channel(idx_chan)
cxb.context.set_idx_chan(idx_chan)
shadowgram = cxb.get_model(1)
hdu = fits.ImageHDU(shadowgram)
hdu.name = "CXB_chan_{}".format(idx_chan)
hdu_list.append(hdu)
thdulist = fits.HDUList(hdu_list)
cxb_path = add_path_caldb_eclairs(cxb_filename)
thdulist.writeto(cxb_path)
logging.info(f"cxb file in {cxb_path}")
if __name__ == '__main__':
create_cxb_shape_based_file()