diff --git a/shapepipe/modules/uncompress_fits_image_package/__init__.py b/shapepipe/modules/uncompress_fits_image_package/__init__.py new file mode 100644 index 000000000..0ca86a56d --- /dev/null +++ b/shapepipe/modules/uncompress_fits_image_package/__init__.py @@ -0,0 +1,15 @@ +# -*- coding: utf-8 -*- + +"""UNCOMRESS FITS + +This module uncompress fits images and save them on a single hdu fits. + +:Author: Axel Guinot, Martin Kilbinger + +:Date: 2020 + +:Package: ShapePipe + +""" + +__all__ = ['uncompress'] diff --git a/shapepipe/modules/uncompress_fits_image_package/uncompress.py b/shapepipe/modules/uncompress_fits_image_package/uncompress.py new file mode 100644 index 000000000..6ca3e806e --- /dev/null +++ b/shapepipe/modules/uncompress_fits_image_package/uncompress.py @@ -0,0 +1,72 @@ +# -*- coding: utf-8 -*- + +"""UNCOMRESS FITS + +This module uncompress fits images and save them on a single hdu fits. + +:Author: Axel Guinot, Martin Kilbinger + +:Date: 2020 + +:Package: ShapePipe + +""" + +from astropy.io import fits + + +class Uncompress(object): + """Uncompress + + This class handles the uncompress process of compressed FITS files. + + Parameters + ---------- + input_file_list : array of string + input files + output_pattern_list : array of string + output file pattern + output_dir : string + output directory + file_number_string : string + image numbering (pipeline ID string) + data_hdu : int + input HDU number + """ + + def __init__( + self, + input_file_list, + output_pattern_list, + output_dir, + file_number_string, + data_hdu + ): + + self._input_file_list = input_file_list + self._output_pattern_list = output_pattern_list + self._output_dir = output_dir + self._file_number_string = file_number_string + self._data_hdu = data_hdu + + def process(self): + """Process + + Main function to process uncompress. + """ + + # Go through input list + for i in range(len(self._input_file_list)): + + # Get data and header + data = fits.getdata(self._input_file_list[i], self._data_hdu) + header = fits.getheader(self._input_file_list[i], self._data_hdu) + + # Create and write new FITS file with that HDU only + hdu = fits.PrimaryHDU(data, header) + hdul = fits.HDUList([hdu]) + hdul.writeto( + f'{self._output_dir}/' + + f'{self._output_pattern_list[i]}' + + f'{self._file_number_string}.fits' + ) diff --git a/shapepipe/modules/uncompress_fits_image_runner.py b/shapepipe/modules/uncompress_fits_image_runner.py index fbbd5a509..22cb91597 100644 --- a/shapepipe/modules/uncompress_fits_image_runner.py +++ b/shapepipe/modules/uncompress_fits_image_runner.py @@ -1,46 +1,61 @@ # -*- coding: utf-8 -*- -"""UNCOMPRESS_FITS_IMAGE_RUNNER +"""UNCOMPRESS FITS IMAGE RUNNER +Module runner for ``uncompress_fits_image_runner`` This module uncompress fits images and save them on a single hdu fits. -:Author: Axel Guinot +:Author: Axel Guinot, Martin Kilbinger -""" - -from shapepipe.modules.module_decorator import module_runner +:Date: 2020 -from astropy.io import fits +:Package: ShapePipe +""" -@module_runner(version='1.0', - file_pattern=['image'], - file_ext=['.fits'], - numbering_scheme='_0') -def uncompress_fits_image_runner(input_file_list, run_dirs, file_number_string, - config, w_log): - +from shapepipe.modules.module_decorator import module_runner +import shapepipe.modules.uncompress_fits_image_package.uncompress as uz + + +@module_runner( + version='1.1', + file_pattern=['image'], + file_ext=['.fits'], + numbering_scheme='_0' +) +def uncompress_fits_image_runner( + input_file_list, + run_dirs, + file_number_string, + config, + w_log +): + + # Get output patterns output_pattern_list = config.getlist('UNCOMPRESS_FITS_IMAGE_RUNNER', 'OUTPUT_PATTERN') + # Get HDU number if config.has_option('UNCOMPRESS_FITS_IMAGE_RUNNER', 'HDU_DATA'): data_hdu = config.getint("UNCOMPRESS_FITS_IMAGE_RUNNER", "HDU_DATA") else: data_hdu = 0 + # Check consistency of input and output list lengths if len(input_file_list) != len(output_pattern_list): - raise ValueError('Lists INPUT_PATH ({}) and OUTPUT_PATTERN ({}) ' - 'need to be of equal length.' - ''.format(len(input_file_list), - len(output_pattern_list))) - - # Read data from input list files - for i in range(len(input_file_list)): - data = fits.getdata(input_file_list[i], data_hdu) - header = fits.getheader(input_file_list[i], data_hdu) - - # Create and write new FITS file with that HDU only - hdu = fits.PrimaryHDU(data, header) - hdul = fits.HDUList([hdu]) - hdul.writeto('{}/{}{}.fits'.format(run_dirs['output'], output_pattern_list[i], file_number_string)) + raise ValueError( + f'Lists INPUT_PATH ({len(input_file_list)})' + + f' and OUTPUT_PATTERN ({len(output_pattern_list)})' + + 'need to be of equal length.' + ) + + # Create instance of uncompress + uz_inst = uz.Uncompress( + input_file_list, + output_pattern_list, + run_dirs['output'], + file_number_string, + data_hdu) + + uz_inst.process() return None, None