AlanLoh / nenupy

NenuFAR python package
MIT License
10 stars 6 forks source link

Improving function for exporting in fits #63

Open EmilieMauduit opened 1 year ago

EmilieMauduit commented 1 year ago

Proposition to modify the SourceInLobes.export() function, so that the .fits files take less space.

def export(self, filename: str , in_bytes : bool = False): """ Exports the time-frequency contamination array in FITS. The mask being either in floats or in bytes. :param filename: FITS file name. :type filename: str :param in_bytes: Set to True ton convert the mask array to bytes. Default is False. :type in_bytes: bool

    """
    t_size, f_size = self.time.size, self.frequency.size

    if in_bytes :
        mask.value = np.array(np.floor(mask.value*255)).astype(int)
        dtype = [('contamination', 'B', (t_size,))]
    else :
        dtype = [('contamination', 'i8' if mask.value.dtype==np.int32 else 'f8', (t_size,))]

    primary_hdu = fits.PrimaryHDU()

    time_jd= self.time.jd
    time_data = fits.BinTableHDU.from_columns([fits.Column(name='time_jd', format='f8', array=time_jd)])
    freq_mhz= self.frequency.to(u.MHz).value
    freq_data = fits.BinTableHDU.from_columns([fits.Column(name='freq_mhz', format='f8', array=freq_mhz)])

    data = np.zeros(f_size, dtype=dtype)
    data['contamination'] = self.value
    cont_data = fits.BinTableHDU(data)

    hdu_list = fits.HDUList([primary_hdu,time_data,freq_data,cont_data])
    hdu_list.writeto(filename, overwrite=True)
AlanLoh commented 1 year ago

No longer time_lst HDU?

EmilieMauduit commented 1 year ago

Oups, we should keep it yes !