diff --git a/shapepipe/modules/split_exp_package/split_exp_script.py b/shapepipe/modules/split_exp_package/split_exp_script.py new file mode 100644 index 000000000..3f66e5a4f --- /dev/null +++ b/shapepipe/modules/split_exp_package/split_exp_script.py @@ -0,0 +1,139 @@ +# -*- coding: utf-8 -*- +"""SPLIT EXP SCRIPT + +Class to split single-exposure multi-CCD mosaic images into single-exposure single-CCD +files, one HDU per CCD. + +:Author: Axel Guinot + +:Date: 2019, 2020 + +:Package: ShapePipe + +""" + +import numpy as np +import sip_tpv as stp +from astropy.wcs import WCS +from astropy.io import fits + +from shapepipe.pipeline import file_io as io + + +class SplitExposures(object): + """Split Exposures initialisation + + Parameters + ---------- + input_file_list : list of strings + input file paths, typically image, weight, and flag + output_dir : string + output directory + file_number_string : string + input file identified + output_suffix : string + output file suffix + n_hdu : int + number of HDUs (= CCDs) of input files + """ + + def __init__( + self, + input_file_list, + output_dir, + file_number_string, + output_suffix, + n_hdu + ): + + self._input_file_list = input_file_list + self._output_dir = output_dir + self._file_number_string = file_number_string + self._output_suffix = output_suffix + self._n_hdu = n_hdu + + def process(self): + """Process + + Process the splitting of single-exposure images + """ + + for exp_path, output_suffix in zip( + self._input_file_list, + self._output_suffix + ): + + transf_int = 'flag' in output_suffix + transf_coord = 'image' in output_suffix + save_header = 'image' in output_suffix + + self.create_hdus( + exp_path, + output_suffix, + transf_coord, + transf_int, + save_header + ) + + def create_hdus( + self, + exp_path, + output_suffix, + transf_coord, + transf_int, + save_header + ): + """ Create HDUs + + Split a single exposures CCDs into separate files. + + Parameters + ---------- + exp_path : str + Path to the single exposure + output_suffix : str + Suffix for the output file + transf_coord : bool + Transform the WCS (pv to sip) if True + transf_int : bool + Set data types to int if True + save_header : bool + Save WCS information if True + """ + + header_file = np.zeros(self._n_hdu, dtype='O') + + for i in range(1, self._n_hdu+1): + + h = fits.getheader(exp_path, i) + if transf_coord: + stp.pv_to_sip(h) + + d = fits.getdata(exp_path, i) + if transf_int: + d = d.astype(np.int16) + + file_name = ( + f'{self._output_dir}/{output_suffix}' + + '{self._file_number_string}-{str(i-1)}.fits' + ) + + new_file = io.FITSCatalog( + file_name, + open_mode=io.BaseCatalog.OpenMode.ReadWrite + ) + new_file.save_as_fits(data=d, image=True, image_header=h) + + if save_header: + try: + w = WCS(h) + except: + print(f'WCS error for file {exp_path}') + raise + header_file[i-1] = {'WCS': w, 'header': h.tostring()} + + if save_header: + file_name = ( + f'{self._output_dir}/headers{self._file_number_string}.npy' + ) + np.save(file_name, header_file) diff --git a/shapepipe/modules/split_exp_runner.py b/shapepipe/modules/split_exp_runner.py index 389a121fd..5c1a2df5e 100644 --- a/shapepipe/modules/split_exp_runner.py +++ b/shapepipe/modules/split_exp_runner.py @@ -2,93 +2,49 @@ """SPLIT EXP RUNNER -This module split the different CCD's hdu of a single exposure into separate -files. +This module splits the different CCDs (= hdus in FITS files) of a +single exposure into separate files. :Author: Axel Guinot -""" - - -import numpy as np -import sip_tpv as stp -from astropy.wcs import WCS -from astropy.io import fits - -from shapepipe.modules.module_decorator import module_runner -from shapepipe.pipeline import file_io as io - - -def create_hdus(exp_path, output_dir, output_name, output_sufix, n_hdu=40, - transf_coord=True, transf_int=False, save_header=False): - """ Create HDUs - - Split a single exposures CCDs into separate files. - - exp_path : str - Path to the single exp. - output_dir : str - Path to the output directory. - output_sufix : str - Suffix for the output file. - n_hdu : int - Number of HDUs (i.e. : number of CCDs). - transf_coord : bool - If True will transform the WCS (pv to sip). - transf_int : bool - If True will set datas to int. - save_header : bool - If True will save WCS information - - """ +:Date: 2019, 2020 - header_file = np.zeros(n_hdu, dtype='O') - - for i in range(1, n_hdu+1): - - h = fits.getheader(exp_path, i) - if transf_coord: - stp.pv_to_sip(h) - - d = fits.getdata(exp_path, i) - if transf_int: - d = d.astype(np.int16) - - file_name = (output_dir + '/' + output_sufix + output_name + - '-' + str(i-1) + '.fits') - new_file = io.FITSCatalog(file_name, - open_mode=io.BaseCatalog.OpenMode.ReadWrite) - new_file.save_as_fits(data=d, image=True, image_header=h) - - if save_header: - try: - w = WCS(h) - except: - print('WCS error for file {}'.format(exp_path)) - raise - header_file[i-1] = {'WCS': w, 'header': h.tostring()} - - if save_header: - file_name = output_dir + '/' + 'headers' + output_name + '.npy' - np.save(file_name, header_file) +:Package: ShapePipe +""" -@module_runner(version='1.0', file_pattern=['image', 'weight', 'flag'], - file_ext=['.fz', '.fz', '.fz'], - depends=['numpy', 'astropy', 'sip_tpv']) -def split_exp_runner(input_file_list, run_dirs, file_number_string, - config, w_log): - file_suffix = config.getlist("SPLIT_EXP_RUNNER", "OUTPUT_SUFFIX") +from shapepipe.modules.module_decorator import module_runner +from shapepipe.modules.split_exp_package import split_exp_script as split + + +@module_runner( + version='1.0', + input_module='get_images_runner', + file_pattern=['image', 'weight', 'flag'], + file_ext=['.fz', '.fz', '.fz'], + depends=['numpy', 'astropy', 'sip_tpv'], + run_method='parallel' +) +def split_exp_runner( + input_file_list, + run_dirs, + file_number_string, + config, + w_log +): + + output_suffix = config.getlist("SPLIT_EXP_RUNNER", "OUTPUT_SUFFIX") n_hdu = config.getint("SPLIT_EXP_RUNNER", "N_HDU") - for exp_path, exp_suffix in zip(input_file_list, file_suffix): - - transf_int = 'flag' in exp_suffix - transf_coord = 'image' in exp_suffix - save_header = 'image' in exp_suffix + inst = split.SplitExposures( + input_file_list, + run_dirs['output'], + file_number_string, + output_suffix, + n_hdu + ) - create_hdus(exp_path, run_dirs['output'], file_number_string, - exp_suffix, n_hdu, transf_coord, transf_int, save_header) + inst.process() return None, None