30#include "cr2res_utils.h"
31#include "cr2res_bpm.h"
32#include "cr2res_dfs.h"
39#define RECIPE_STRING "cr2res_util_bpm_merge"
45int cpl_plugin_get_info(cpl_pluginlist * list);
51static int cr2res_util_bpm_merge_create(cpl_plugin *);
52static int cr2res_util_bpm_merge_exec(cpl_plugin *);
53static int cr2res_util_bpm_merge_destroy(cpl_plugin *);
54static int cr2res_util_bpm_merge(cpl_frameset *,
const cpl_parameterlist *);
60static char cr2res_util_bpm_merge_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_SPLIT_PROCATG
" \n\
69 or " CR2RES_UTIL_BPM_MERGE_PROCATG
" \n\
70 or " CR2RES_UTIL_NORM_BPM_PROCATG
" \n\
74 CR2RES_UTIL_BPM_MERGE_PROCATG
"\n\
77 read input BPMs and merge their types by adding the \n\
78 integers, in powers of 2. \n\
80 Library functions used: \n\
81 cr2res_io_load_BPM() \n\
82 cr2res_io_save_BPM() \n\
100int cpl_plugin_get_info(cpl_pluginlist * list)
102 cpl_recipe * recipe = cpl_calloc(1,
sizeof *recipe );
103 cpl_plugin * plugin = &recipe->interface;
105 if (cpl_plugin_init(plugin,
107 CR2RES_BINARY_VERSION,
108 CPL_PLUGIN_TYPE_RECIPE,
110 "BPM merging utility",
111 cr2res_util_bpm_merge_description,
112 CR2RES_PIPELINE_AUTHORS,
115 cr2res_util_bpm_merge_create,
116 cr2res_util_bpm_merge_exec,
117 cr2res_util_bpm_merge_destroy)) {
118 cpl_msg_error(cpl_func,
"Plugin initialization failed");
119 (void)cpl_error_set_where(cpl_func);
123 if (cpl_pluginlist_append(list, plugin)) {
124 cpl_msg_error(cpl_func,
"Error adding plugin to list");
125 (void)cpl_error_set_where(cpl_func);
141static int cr2res_util_bpm_merge_create(cpl_plugin * plugin)
143 cpl_recipe * recipe ;
147 if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
148 recipe = (cpl_recipe *)plugin;
153 recipe->parameters = cpl_parameterlist_new();
156 p = cpl_parameter_new_value(
"cr2res.cr2res_util_bpm_merge.detector",
157 CPL_TYPE_INT,
"Only reduce the specified detector",
158 "cr2res.cr2res_util_bpm_merge", 0);
159 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI,
"detector");
160 cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV);
161 cpl_parameterlist_append(recipe->parameters, p);
173static int cr2res_util_bpm_merge_exec(cpl_plugin * plugin)
178 if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
179 recipe = (cpl_recipe *)plugin;
182 return cr2res_util_bpm_merge(recipe->frames, recipe->parameters);
192static int cr2res_util_bpm_merge_destroy(cpl_plugin * plugin)
197 if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
198 recipe = (cpl_recipe *)plugin;
201 cpl_parameterlist_delete(recipe->parameters);
213static int cr2res_util_bpm_merge(
214 cpl_frameset * frameset,
215 const cpl_parameterlist * parlist)
217 const cpl_parameter * param ;
219 cpl_frameset * rawframes ;
220 const cpl_frame * cur_frame ;
221 const char * cur_fname ;
222 cpl_image * tmp_ima ;
223 cpl_image * merged_bpms[CR2RES_NB_DETECTORS] ;
224 cpl_propertylist * ext_plist[CR2RES_NB_DETECTORS] ;
226 int i, det_nr, wished_ext_nb;
231 param = cpl_parameterlist_find_const(parlist,
232 "cr2res.cr2res_util_bpm_merge.detector");
233 reduce_det = cpl_parameter_get_int(param);
237 cpl_msg_error(__func__,
"Cannot identify RAW and CALIB frames") ;
238 cpl_error_set(__func__, CPL_ERROR_ILLEGAL_INPUT) ;
246 if (rawframes==NULL || cpl_frameset_get_size(rawframes) <= 0) {
247 cpl_msg_error(__func__,
"Cannot find any RAW file") ;
248 cpl_error_set(__func__, CPL_ERROR_DATA_NOT_FOUND) ;
253 for (det_nr=1 ; det_nr<=CR2RES_NB_DETECTORS ; det_nr++) {
255 merged_bpms[det_nr-1] = NULL ;
256 ext_plist[det_nr-1] = NULL ;
259 if (reduce_det != 0 && det_nr != reduce_det) continue ;
261 cpl_msg_info(__func__,
"Process Detector %d", det_nr) ;
262 cpl_msg_indent_more() ;
265 for (i=0 ; i<cpl_frameset_get_size(rawframes) ; i++) {
267 cur_frame = cpl_frameset_get_position(rawframes, i) ;
268 cur_fname = cpl_frame_get_filename(cur_frame) ;
273 ext_plist[det_nr-1]=cpl_propertylist_load(cur_fname,
281 if (merged_bpms[det_nr-1] == NULL) {
282 merged_bpms[det_nr-1] = cpl_image_duplicate(tmp_ima) ;
284 cpl_image_or(merged_bpms[det_nr-1], NULL, tmp_ima) ;
286 cpl_image_delete(tmp_ima);
288 cpl_msg_indent_less() ;
294 out_file=cpl_sprintf(
"%s.fits", RECIPE_STRING) ;
296 merged_bpms, NULL, ext_plist,
297 CR2RES_UTIL_BPM_MERGE_PROCATG, RECIPE_STRING) ;
300 for (det_nr=1 ; det_nr<=CR2RES_NB_DETECTORS ; det_nr++) {
301 if (merged_bpms[det_nr-1] != NULL)
302 cpl_image_delete(merged_bpms[det_nr-1]) ;
303 if (ext_plist[det_nr-1] != NULL)
304 cpl_propertylist_delete(ext_plist[det_nr-1]) ;
306 cpl_frameset_delete(rawframes) ;
307 return (
int)cpl_error_get_code();
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.
const char * cr2res_get_license(void)
Get the pipeline copyright and license.