30#include "cr2res_utils.h"
31#include "cr2res_bpm.h"
32#include "cr2res_dfs.h"
39#define RECIPE_STRING "cr2res_util_bpm_split"
45int cpl_plugin_get_info(cpl_pluginlist * list);
51static int cr2res_util_bpm_split_create(cpl_plugin *);
52static int cr2res_util_bpm_split_exec(cpl_plugin *);
53static int cr2res_util_bpm_split_destroy(cpl_plugin *);
54static int cr2res_util_bpm_split(cpl_frameset *,
const cpl_parameterlist *);
60static char cr2res_util_bpm_split_description[] =
"\
62 Each input BPM is split into several BPMs \n\
65 raw.fits " CR2RES_CAL_DARK_BPM_PROCATG
" [1 to n] \n\
66 or " CR2RES_CAL_FLAT_BPM_PROCATG
" \n\
67 or " CR2RES_CAL_DETLIN_BPM_PROCATG
" \n\
68 or " CR2RES_UTIL_BPM_MERGE_PROCATG
" \n\
69 or " CR2RES_UTIL_BPM_SPLIT_PROCATG
" \n\
70 or " CR2RES_UTIL_NORM_BPM_PROCATG
" \n\
73 <input_name>_split_<bpm_code>.fits "
74 CR2RES_UTIL_BPM_SPLIT_PROCATG
"\n\
77 loop on input raw frames f: \n\
78 loop on detectors d: \n\
79 loop on bpm types t: \n\
80 call cr2res_bpm_from_mask() \n\
81 -> bpm_single_type(t, d, f) \n\
82 loop on bpm types t: \n\
83 Save bpm_single_type(f, t) (UTIL_BPM_SPLIT) \n\
85 Library functions used: \n\
86 cr2res_io_load_BPM() \n\
87 cr2res_bpm_from_mask() \n\
88 cr2res_io_save_BPM() \n\
106int cpl_plugin_get_info(cpl_pluginlist * list)
108 cpl_recipe * recipe = cpl_calloc(1,
sizeof *recipe );
109 cpl_plugin * plugin = &recipe->interface;
111 if (cpl_plugin_init(plugin,
113 CR2RES_BINARY_VERSION,
114 CPL_PLUGIN_TYPE_RECIPE,
116 "BPM splitting utility",
117 cr2res_util_bpm_split_description,
118 CR2RES_PIPELINE_AUTHORS,
121 cr2res_util_bpm_split_create,
122 cr2res_util_bpm_split_exec,
123 cr2res_util_bpm_split_destroy)) {
124 cpl_msg_error(cpl_func,
"Plugin initialization failed");
125 (void)cpl_error_set_where(cpl_func);
129 if (cpl_pluginlist_append(list, plugin)) {
130 cpl_msg_error(cpl_func,
"Error adding plugin to list");
131 (void)cpl_error_set_where(cpl_func);
147static int cr2res_util_bpm_split_create(cpl_plugin * plugin)
149 cpl_recipe * recipe ;
153 if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
154 recipe = (cpl_recipe *)plugin;
159 recipe->parameters = cpl_parameterlist_new();
162 p = cpl_parameter_new_value(
"cr2res.cr2res_util_bpm_split.detector",
163 CPL_TYPE_INT,
"Only reduce the specified detector",
164 "cr2res.cr2res_util_bpm_split", 0);
165 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI,
"detector");
166 cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV);
167 cpl_parameterlist_append(recipe->parameters, p);
179static int cr2res_util_bpm_split_exec(cpl_plugin * plugin)
184 if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
185 recipe = (cpl_recipe *)plugin;
188 return cr2res_util_bpm_split(recipe->frames, recipe->parameters);
198static int cr2res_util_bpm_split_destroy(cpl_plugin * plugin)
203 if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
204 recipe = (cpl_recipe *)plugin;
207 cpl_parameterlist_delete(recipe->parameters);
219static int cr2res_util_bpm_split(
220 cpl_frameset * frameset,
221 const cpl_parameterlist * parlist)
223 const cpl_parameter * param ;
225 cpl_frameset * rawframes ;
226 cpl_frameset * cur_fset ;
228 cpl_image * split_bpms[CR2RES_NB_BPM_TYPES][CR2RES_NB_DETECTORS] ;
230 cpl_propertylist * ext_plist[CR2RES_NB_DETECTORS] ;
232 int i, j, det_nr, wished_ext_nb;
237 param = cpl_parameterlist_find_const(parlist,
238 "cr2res.cr2res_util_bpm_split.detector");
239 reduce_det = cpl_parameter_get_int(param);
243 cpl_msg_error(__func__,
"Cannot identify RAW and CALIB frames") ;
244 cpl_error_set(__func__, CPL_ERROR_ILLEGAL_INPUT) ;
252 if (rawframes==NULL || cpl_frameset_get_size(rawframes) <= 0) {
253 cpl_msg_error(__func__,
"Cannot find any RAW file") ;
254 cpl_error_set(__func__, CPL_ERROR_DATA_NOT_FOUND) ;
259 for (i = 0; i < cpl_frameset_get_size(rawframes); i++) {
260 const cpl_frame *cur_frame;
261 const char *cur_fname;
263 cur_frame = cpl_frameset_get_position(rawframes, i) ;
264 cur_fname = cpl_frame_get_filename(cur_frame) ;
265 cpl_msg_info(__func__,
"Reduce Frame %s", cur_fname) ;
266 cpl_msg_indent_more() ;
269 for (det_nr=1 ; det_nr<=CR2RES_NB_DETECTORS ; det_nr++) {
271 for (j=0 ; j<CR2RES_NB_BPM_TYPES ; j++)
272 split_bpms[j][det_nr-1] = NULL ;
274 ext_plist[det_nr-1]=cpl_propertylist_load(cur_fname,wished_ext_nb);
277 if (reduce_det != 0 && det_nr != reduce_det) continue ;
279 cpl_msg_info(__func__,
"Process Detector %d", det_nr) ;
280 cpl_msg_indent_more() ;
286 for (j=0 ; j<CR2RES_NB_BPM_TYPES ; j++) {
288 split_bpms[j][det_nr-1] =
290 cpl_mask_delete(my_mask) ;
292 cpl_image_delete(ima);
293 cpl_msg_indent_less() ;
299 for (j=0 ; j<CR2RES_NB_BPM_TYPES ; j++) {
300 out_file=cpl_sprintf(
"%s_split_%d.fits",
302 cur_fset = cpl_frameset_new() ;
303 cpl_frameset_insert(cur_fset, cpl_frame_duplicate(cur_frame)) ;
305 split_bpms[j], NULL, ext_plist,
306 CR2RES_UTIL_BPM_SPLIT_PROCATG, RECIPE_STRING) ;
307 cpl_frameset_delete(cur_fset) ;
311 for (det_nr=1 ; det_nr<=CR2RES_NB_DETECTORS ; det_nr++) {
312 for (j=0 ; j<CR2RES_NB_BPM_TYPES ; j++) {
313 if (split_bpms[j][det_nr-1] != NULL)
314 cpl_image_delete(split_bpms[j][det_nr-1]) ;
316 if (ext_plist[det_nr-1] != NULL)
317 cpl_propertylist_delete(ext_plist[det_nr-1]) ;
319 cpl_msg_indent_less() ;
321 cpl_frameset_delete(rawframes) ;
322 return (
int)cpl_error_get_code();
cpl_image * cr2res_bpm_from_mask(cpl_mask *mask, cr2res_bpm_type type)
Create a BPM from a mask.
cpl_mask * cr2res_bpm_extract_mask(const cpl_image *bpm_ima, cr2res_bpm_type bpm_type)
Extract a mask from a BPM image.
cpl_error_code cr2res_dfs_set_groups(cpl_frameset *set)
Set the group as RAW or CALIB in a frameset.
cpl_frameset * cr2res_io_find_BPM_all(const cpl_frameset *in)
Get the CR2RES_BPM_DRSTYPE frames from a frameset.
int cr2res_io_get_ext_idx(const char *filename, int detector, int data)
Get the wished extension number for a detector.
int cr2res_io_save_BPM(const char *filename, cpl_frameset *allframes, cpl_frameset *inframes, const cpl_parameterlist *parlist, cpl_image **bpms, const cpl_propertylist *qc_list, cpl_propertylist **ext_plist, const char *procatg, const char *recipe)
Save a BPM.
cpl_image * cr2res_io_load_BPM(const char *filename, int detector, int data)
Load an image from a BPM.
char * cr2res_get_root_name(const char *filename)
Find out the root part of a basename (name without extension).
const char * cr2res_get_license(void)
Get the pipeline copyright and license.