'''fits outputs for bube
Created on 21 february 2019
@author: Catalano Camille, APC
'''
import numpy as np
from astropy.io import fits
import astropy.table as astt
import astropy.units as u
from ecpi.common.io import fits_tools
from ecpi.process.bube.core.main_bube import DetectorImages
from ecpi.common.mission.observation import EclairsObservation
from ecpi.common.instru.array_convention import yzegp_to_ijdet_array
[docs]def save_detector_images(det_image : DetectorImages, attitude, tstart, tstop, file_path, creator='BUBE?', proc_id="01"):
"""save detector images in two fits files : ECL-DET-IMA and ECL-DET-UBC
This function call save_detector_raw_image and save_detector_corrected_image
common keywords are used
filenames are ('ECL-DET-IMA-' + proc_id + '.fits') and ('ECL-DET-UBC-' + proc_id + '.fits')
:param det_image: detector image to save
:type det_image: DetectorImages
:param attitude: [ra, dec, ori] in degrees
:type attitude: [float, float, float]
:param tstart: start time of the observation in s from mjdref
:type tstart: float
:param tstop: end time of the observation in s from mjdref
:type tstop: float
:param file_path: PATH to the directory where to write the file
:type file_path: string
:param creator: program that has generated the file. Default='BUBE?'.
:type creator: string
:param proc_id: id of the process. Default="01".
:type proc_id: string
"""
save_detector_raw_image(det_image, tstart, tstop, file_path, attitude, creator, proc_id)
save_detector_corrected_image(det_image, tstart, tstop, file_path, attitude, creator, proc_id)
[docs]def save_detector_raw_image(det_image : DetectorImages, tstart, tstop, file_path, attitude, creator='BUBE?', proc_id="01"):
"""save detector raw images in a fits file : ECL-DET-IMA
common keywords are used
filename is ('ECL-DET-IMA-' + proc_id + '.fits')
:param det_image: detector image to save
:type det_image: DetectorImages
:param tstart: start time of the observation in s from mjdref
:type tstart: float
:param tstop: end time of the observation in s from mjdref
:type tstop: float
:param file_path: PATH to the directory where to write the file
:type file_path: string
:param attitude: [ra, dec, ori] in degrees
:type attitude: [float, float, float]
:param creator: program that has generated the file. Default='BUBE?'.
:type creator: string
:param proc_id: id of the process. Default="01".
:type proc_id: string
"""
prihdr = fits.Header()
prihdu = fits.PrimaryHDU(header=prihdr)
obstime = tstop - tstart
fits_tools.pointing_keywords(prihdu, attitude, obstime)
fits_tools.common_keyword(prihdu, tstart, tstop, creator, 1.0, proc_id)
prihdu.header['CARD'] = ("ECL-DET-IMA", "Product type")
hdu_list = [prihdu]
hdu_images = []
grp_table = astt.Table([[]]*11,
names=('member_xtension', 'member_name', 'member_version',
'member_position', 'member_location', 'member_uri_type',
'imatype', 'chanmin', 'chanmax', 'e_min', 'e_max'),
dtype=('S8', 'S32', 'i4', 'i4', 'S256', 'S3', 'S20', 'f4', 'f4', 'f4', 'f4'))
grp_table['e_min'].unit = u.keV
grp_table['e_max'].unit = u.keV
for shadow_idx in range(len(det_image.shadowgrams)):
chanmin = det_image.energy_ranges[shadow_idx][0]
chanmax = det_image.energy_ranges[shadow_idx][1]
e_min = chanmin
e_max = chanmax
grp_table.add_row(['IMAGE ', 'Shadowgram', '1', shadow_idx*2+2, '', '', 'intensity', chanmin, chanmax, e_min, e_max])
grp_table.add_row(['IMAGE ', 'Variance', '1', shadow_idx*2+3, '', '', 'variance', chanmin, chanmax, e_min, e_max])
hdu_shadowgram = fits.ImageHDU(np.array(yzegp_to_ijdet_array(det_image.shadowgrams[shadow_idx]),
dtype='float32'))
hdu_var = fits.ImageHDU(np.array(yzegp_to_ijdet_array(det_image.shadowgrams_var[shadow_idx]),
dtype='float32'))
fits_tools.common_keyword(hdu_shadowgram, tstart, tstop, creator, 1.0, proc_id)
fits_tools.common_keyword(hdu_var, tstart, tstop, creator, 1.0, proc_id)
fits_tools.energy_keywords(hdu_shadowgram, chanmin, chanmax, e_min, e_max)
fits_tools.energy_keywords(hdu_var, chanmin, chanmax, e_min, e_max)
hdu_shadowgram.name = 'Shadowgram'
hdu_shadowgram.header['comment'] = "Shadowgram Image"
hdu_shadowgram.header['BUNIT'] = 'cts.s^-1.cm^-2'
hdu_shadowgram.header['IMATYPE'] = ("Intensity", "Type of image")
hdu_var.name = 'Variance'
hdu_var.header['comment'] = "Shadowgram Variance Image"
hdu_var.header['BUNIT'] = 'cts^2.s^-2.cm^-4'
hdu_var.header['IMATYPE'] = ("Variance", "Type of image")
hdu_images.extend([hdu_shadowgram, hdu_var])
hdu_grp = fits.table_to_hdu(grp_table)
hdu_grp.header['comment'] = 'ECLAIRs detector images'
fits_tools.grouphdu_keyword(hdu_grp, 'ECL-DET-IMA-IDX', attitude)
hdu_list.append(hdu_grp)
hdu_list.extend(hdu_images)
thdulist = fits.HDUList(hdu_list)
thdulist.writeto(file_path + '/ECL-DET-IMA-' + proc_id + '.fits', overwrite=True, checksum=True)
[docs]def save_detector_corrected_image(det_image : DetectorImages, tstart, tstop, file_path, attitude, creator='BUBE?', proc_id="01"):
"""save detector corrected images in a fits file : ECL-DET-UBC
common keywords are used
filename is ('ECL-DET-UBC-' + proc_id + '.fits')
:param det_image: detector image to save
:type det_image: DetectorImages
:param tstart: start time of the observation in s from mjdref
:type tstart: float
:param tstop: end time of the observation in s from mjdref
:type tstop: float
:param file_path: PATH to the directory where to write the file
:type file_path: string
:param attitude: [ra, dec, ori] in degrees
:type attitude: [float, float, float]
:param creator: program that has generated the file. Default='BUBE?'.
:type creator: string
:param proc_id: id of the process. Default="01".
:type proc_id: string
"""
prihdr = fits.Header()
prihdu = fits.PrimaryHDU(header=prihdr)
obstime = tstop - tstart
fits_tools.pointing_keywords(prihdu, attitude, obstime)
fits_tools.common_keyword(prihdu, tstart, tstop, creator, 1.0, proc_id)
prihdu.header['CARD'] = ("ECL-DET-UBC", "Product type")
hdu_list = [prihdu]
hdu_images = []
grp_table = astt.Table([[]]*11,
names=('member_xtension', 'member_name', 'member_version',
'member_position', 'member_location', 'member_uri_type',
'imatype', 'chanmin', 'chanmax', 'e_min', 'e_max'),
dtype=('S8', 'S32', 'i4', 'i4', 'S256', 'S3', 'S20', 'f4', 'f4', 'f4', 'f4'))
grp_table['e_min'].unit = u.keV
grp_table['e_max'].unit = u.keV
for shadow_idx in range(len(det_image.shadowgrams)):
chanmin = det_image.energy_ranges[shadow_idx][0]
chanmax = det_image.energy_ranges[shadow_idx][1]
e_min = chanmin
e_max = chanmax
grp_table.add_row(['IMAGE ', 'ShadowgramCorr', '1', shadow_idx+2, '', '', 'intensity', chanmin, chanmax, e_min, e_max])
shdg_bkgc = yzegp_to_ijdet_array(det_image.shadowgrams_bgcorrected[shadow_idx])
hdu_shadowgram_corrected = fits.ImageHDU(np.array(shdg_bkgc, dtype='float32'))
fits_tools.common_keyword(hdu_shadowgram_corrected, tstart, tstop, creator, 1.0, proc_id)
fits_tools.energy_keywords(hdu_shadowgram_corrected, chanmin, chanmax, e_min, e_max)
hdu_shadowgram_corrected.name = 'ShadowgramCorr'
hdu_shadowgram_corrected.header['comment'] = "Shadowgram Corrected Image"
hdu_shadowgram_corrected.header['BUNIT'] = 'cts.s^-1.cm^-2'
hdu_shadowgram_corrected.header['IMATYPE'] = ("Intensity", "Type of image")
hdu_images.append(hdu_shadowgram_corrected)
hdu_grp = fits.table_to_hdu(grp_table)
hdu_grp.header['comment'] = 'ECLAIRs detector images corrected from uniformity, background and earth occultation/albedo'
fits_tools.grouphdu_keyword(hdu_grp, 'ECL-DET-UBC-GRP', attitude)
fits_tools.common_keyword(hdu_grp, tstart, tstop, creator, 1.0, proc_id)
hdu_list.append(hdu_grp)
hdu_list.extend(hdu_images)
thdulist = fits.HDUList(hdu_list)
thdulist.writeto(file_path + '/ECL-DET-UBC-' + proc_id + '.fits', overwrite=True, checksum=True)
[docs]def save_gti(obs : EclairsObservation, file_path, attitude, creator='BUBE?', proc_id="01"):
"""save GTI table into a fits file
common keywords are used
filename is 'ECL-GTI-3-' + proc_id + '.fits'
:param obs: observation parameters
:type obs: EclairsObservation
:param file_path: PATH to the directory where to write the file
:type file_path: string
:param attitude: [ra, dec, ori] in degrees
:type attitude: [float, float, float]
:param creator: program that has generated the file. Default='BUBE?'.
:type creator: string
:param proc_id: id of the process. Default="01".
:type proc_id: string
"""
if isinstance(obs.gti_table, int):
obs.gti_compute()
tbhdu = fits.table_to_hdu(obs.gti_table)
prihdr = fits.Header()
prihdu = fits.PrimaryHDU(header=prihdr)
fits_tools.pointing_keywords(prihdu, attitude, obs.obs_time)
fits_tools.common_keyword(prihdu, obs.start_time, obs.end_time, creator, 1.0, proc_id)
prihdu.header['CARD'] = ("ECL-GTI-3", "Product type")
fits_tools.common_keyword(tbhdu, obs.start_time, obs.end_time, creator, 1.0, proc_id)
thdulist = fits.HDUList([prihdu, tbhdu])
thdulist[1].header['comment'] = "Good Time Intervals for ECLAIRs data analysis"
thdulist[1].name = 'Eclairs Good Time Intervals'
thdulist.writeto(file_path + '/ECL-GTI-3-' + proc_id + '.fits', overwrite=True, checksum=True)