CR2RE Pipeline Reference Manual 1.6.7
cr2res_util_bpm_merge.c
1/*
2 * This file is part of the CR2RES Pipeline
3 * Copyright (C) 2002,2003 European Southern Observatory
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02111-1307 USA
18 */
19
20#ifdef HAVE_CONFIG_H
21#include <config.h>
22#endif
23
24/*-----------------------------------------------------------------------------
25 Includes
26 -----------------------------------------------------------------------------*/
27
28#include <cpl.h>
29
30#include "cr2res_utils.h"
31#include "cr2res_bpm.h"
32#include "cr2res_dfs.h"
33#include "cr2res_io.h"
34
35/*-----------------------------------------------------------------------------
36 Define
37 -----------------------------------------------------------------------------*/
38
39#define RECIPE_STRING "cr2res_util_bpm_merge"
40
41/*-----------------------------------------------------------------------------
42 Plugin registration
43 -----------------------------------------------------------------------------*/
44
45int cpl_plugin_get_info(cpl_pluginlist * list);
46
47/*-----------------------------------------------------------------------------
48 Private function prototypes
49 -----------------------------------------------------------------------------*/
50
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 *);
55
56/*-----------------------------------------------------------------------------
57 Static variables
58 -----------------------------------------------------------------------------*/
59
60static char cr2res_util_bpm_merge_description[] = "\
61BPM merging \n\
62 Each input BPM is split into several BPMs \n\
63 \n\
64 Inputs \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\
71 \n\
72 Outputs \n\
73 <recipe_name>.fits "
74 CR2RES_UTIL_BPM_MERGE_PROCATG "\n\
75 \n\
76 Algorithm \n\
77 read input BPMs and merge their types by adding the \n\
78 integers, in powers of 2. \n\
79 \n\
80 Library functions used: \n\
81 cr2res_io_load_BPM() \n\
82 cr2res_io_save_BPM() \n\
83";
84
85/*-----------------------------------------------------------------------------
86 Function code
87 -----------------------------------------------------------------------------*/
88
89/*----------------------------------------------------------------------------*/
99/*----------------------------------------------------------------------------*/
100int cpl_plugin_get_info(cpl_pluginlist * list)
101{
102 cpl_recipe * recipe = cpl_calloc(1, sizeof *recipe );
103 cpl_plugin * plugin = &recipe->interface;
104
105 if (cpl_plugin_init(plugin,
106 CPL_PLUGIN_API,
107 CR2RES_BINARY_VERSION,
108 CPL_PLUGIN_TYPE_RECIPE,
109 RECIPE_STRING,
110 "BPM merging utility",
111 cr2res_util_bpm_merge_description,
112 CR2RES_PIPELINE_AUTHORS,
113 PACKAGE_BUGREPORT,
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);
120 return 1;
121 }
122
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);
126 return 1;
127 }
128
129 return 0;
130}
131
132/*----------------------------------------------------------------------------*/
140/*----------------------------------------------------------------------------*/
141static int cr2res_util_bpm_merge_create(cpl_plugin * plugin)
142{
143 cpl_recipe * recipe ;
144 cpl_parameter * p ;
145
146 /* Check that the plugin is part of a valid recipe */
147 if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
148 recipe = (cpl_recipe *)plugin;
149 else
150 return -1;
151
152 /* Create the parameters list in the cpl_recipe object */
153 recipe->parameters = cpl_parameterlist_new();
154
155 /* Fill the parameters list */
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);
162
163 return 0;
164}
165
166/*----------------------------------------------------------------------------*/
172/*----------------------------------------------------------------------------*/
173static int cr2res_util_bpm_merge_exec(cpl_plugin * plugin)
174{
175 cpl_recipe *recipe;
176
177 /* Get the recipe out of the plugin */
178 if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
179 recipe = (cpl_recipe *)plugin;
180 else return -1;
181
182 return cr2res_util_bpm_merge(recipe->frames, recipe->parameters);
183}
184
185/*----------------------------------------------------------------------------*/
191/*----------------------------------------------------------------------------*/
192static int cr2res_util_bpm_merge_destroy(cpl_plugin * plugin)
193{
194 cpl_recipe *recipe;
195
196 /* Get the recipe out of the plugin */
197 if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
198 recipe = (cpl_recipe *)plugin;
199 else return -1 ;
200
201 cpl_parameterlist_delete(recipe->parameters);
202 return 0 ;
203}
204
205/*----------------------------------------------------------------------------*/
212/*----------------------------------------------------------------------------*/
213static int cr2res_util_bpm_merge(
214 cpl_frameset * frameset,
215 const cpl_parameterlist * parlist)
216{
217 const cpl_parameter * param ;
218 int reduce_det ;
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] ;
225 char * out_file;
226 int i, det_nr, wished_ext_nb;
227
228 /* Initialise */
229
230 /* RETRIEVE INPUT PARAMETERS */
231 param = cpl_parameterlist_find_const(parlist,
232 "cr2res.cr2res_util_bpm_merge.detector");
233 reduce_det = cpl_parameter_get_int(param);
234
235 /* Identify the RAW and CALIB frames in the input frameset */
236 if (cr2res_dfs_set_groups(frameset)) {
237 cpl_msg_error(__func__, "Cannot identify RAW and CALIB frames") ;
238 cpl_error_set(__func__, CPL_ERROR_ILLEGAL_INPUT) ;
239 return -1 ;
240 }
241
242 /* Get Calibration frames */
243
244 /* Get the rawframes */
245 rawframes = cr2res_io_find_BPM_all(frameset) ;
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) ;
249 return -1 ;
250 }
251
252 /* Loop on the detectors */
253 for (det_nr=1 ; det_nr<=CR2RES_NB_DETECTORS ; det_nr++) {
254 /* Initialise */
255 merged_bpms[det_nr-1] = NULL ;
256 ext_plist[det_nr-1] = NULL ;
257
258 /* Compute only one detector */
259 if (reduce_det != 0 && det_nr != reduce_det) continue ;
260
261 cpl_msg_info(__func__, "Process Detector %d", det_nr) ;
262 cpl_msg_indent_more() ;
263
264 /* Loop on the rawframes */
265 for (i=0 ; i<cpl_frameset_get_size(rawframes) ; i++) {
266 /* Get the Current Frame */
267 cur_frame = cpl_frameset_get_position(rawframes, i) ;
268 cur_fname = cpl_frame_get_filename(cur_frame) ;
269
270 if (i==0) {
271 /* Create the header */
272 wished_ext_nb = cr2res_io_get_ext_idx(cur_fname, det_nr, 1) ;
273 ext_plist[det_nr-1]=cpl_propertylist_load(cur_fname,
274 wished_ext_nb);
275 }
276
277 /* Load the image */
278 tmp_ima = cr2res_io_load_BPM(cur_fname, det_nr, 1);
279
280 /* Accumulate on the merged image */
281 if (merged_bpms[det_nr-1] == NULL) {
282 merged_bpms[det_nr-1] = cpl_image_duplicate(tmp_ima) ;
283 } else {
284 cpl_image_or(merged_bpms[det_nr-1], NULL, tmp_ima) ;
285 }
286 cpl_image_delete(tmp_ima);
287 }
288 cpl_msg_indent_less() ;
289 }
290
291 /* Save Products */
292
293 /* MERGED_BPM */
294 out_file=cpl_sprintf("%s.fits", RECIPE_STRING) ;
295 cr2res_io_save_BPM(out_file, frameset, rawframes, parlist,
296 merged_bpms, NULL, ext_plist,
297 CR2RES_UTIL_BPM_MERGE_PROCATG, RECIPE_STRING) ;
298 cpl_free(out_file);
299 /* Free */
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]) ;
305 }
306 cpl_frameset_delete(rawframes) ;
307 return (int)cpl_error_get_code();
308}
309
cpl_error_code cr2res_dfs_set_groups(cpl_frameset *set)
Set the group as RAW or CALIB in a frameset.
Definition: cr2res_dfs.c:53
cpl_frameset * cr2res_io_find_BPM_all(const cpl_frameset *in)
Get the CR2RES_BPM_DRSTYPE frames from a frameset.
Definition: cr2res_io.c:397
int cr2res_io_get_ext_idx(const char *filename, int detector, int data)
Get the wished extension number for a detector.
Definition: cr2res_io.c:644
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.
Definition: cr2res_io.c:1555
cpl_image * cr2res_io_load_BPM(const char *filename, int detector, int data)
Load an image from a BPM.
Definition: cr2res_io.c:957
const char * cr2res_get_license(void)
Get the pipeline copyright and license.