CR2RE Pipeline Reference Manual 1.6.8
cr2res_util_bpm_split.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_split"
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_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 *);
55
56/*-----------------------------------------------------------------------------
57 Static variables
58 -----------------------------------------------------------------------------*/
59
60static char cr2res_util_bpm_split_description[] = "\
61BPM splitting \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_MERGE_PROCATG " \n\
69 or " CR2RES_UTIL_BPM_SPLIT_PROCATG " \n\
70 or " CR2RES_UTIL_NORM_BPM_PROCATG " \n\
71 \n\
72 Outputs \n\
73 <input_name>_split_<bpm_code>.fits "
74 CR2RES_UTIL_BPM_SPLIT_PROCATG "\n\
75 \n\
76 Algorithm \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\
84 \n\
85 Library functions used: \n\
86 cr2res_io_load_BPM() \n\
87 cr2res_bpm_from_mask() \n\
88 cr2res_io_save_BPM() \n\
89";
90
91/*-----------------------------------------------------------------------------
92 Function code
93 -----------------------------------------------------------------------------*/
94
95/*----------------------------------------------------------------------------*/
105/*----------------------------------------------------------------------------*/
106int cpl_plugin_get_info(cpl_pluginlist * list)
107{
108 cpl_recipe * recipe = cpl_calloc(1, sizeof *recipe );
109 cpl_plugin * plugin = &recipe->interface;
110
111 if (cpl_plugin_init(plugin,
112 CPL_PLUGIN_API,
113 CR2RES_BINARY_VERSION,
114 CPL_PLUGIN_TYPE_RECIPE,
115 RECIPE_STRING,
116 "BPM splitting utility",
117 cr2res_util_bpm_split_description,
118 CR2RES_PIPELINE_AUTHORS,
119 PACKAGE_BUGREPORT,
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);
126 return 1;
127 }
128
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);
132 return 1;
133 }
134
135 return 0;
136}
137
138/*----------------------------------------------------------------------------*/
146/*----------------------------------------------------------------------------*/
147static int cr2res_util_bpm_split_create(cpl_plugin * plugin)
148{
149 cpl_recipe * recipe ;
150 cpl_parameter * p ;
151
152 /* Check that the plugin is part of a valid recipe */
153 if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
154 recipe = (cpl_recipe *)plugin;
155 else
156 return -1;
157
158 /* Create the parameters list in the cpl_recipe object */
159 recipe->parameters = cpl_parameterlist_new();
160
161 /* Fill the parameters list */
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);
168
169 return 0;
170}
171
172/*----------------------------------------------------------------------------*/
178/*----------------------------------------------------------------------------*/
179static int cr2res_util_bpm_split_exec(cpl_plugin * plugin)
180{
181 cpl_recipe *recipe;
182
183 /* Get the recipe out of the plugin */
184 if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
185 recipe = (cpl_recipe *)plugin;
186 else return -1;
187
188 return cr2res_util_bpm_split(recipe->frames, recipe->parameters);
189}
190
191/*----------------------------------------------------------------------------*/
197/*----------------------------------------------------------------------------*/
198static int cr2res_util_bpm_split_destroy(cpl_plugin * plugin)
199{
200 cpl_recipe *recipe;
201
202 /* Get the recipe out of the plugin */
203 if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
204 recipe = (cpl_recipe *)plugin;
205 else return -1 ;
206
207 cpl_parameterlist_delete(recipe->parameters);
208 return 0 ;
209}
210
211/*----------------------------------------------------------------------------*/
218/*----------------------------------------------------------------------------*/
219static int cr2res_util_bpm_split(
220 cpl_frameset * frameset,
221 const cpl_parameterlist * parlist)
222{
223 const cpl_parameter * param ;
224 int reduce_det ;
225 cpl_frameset * rawframes ;
226 cpl_frameset * cur_fset ;
227 cpl_image * ima ;
228 cpl_image * split_bpms[CR2RES_NB_BPM_TYPES][CR2RES_NB_DETECTORS] ;
229 cpl_mask * my_mask ;
230 cpl_propertylist * ext_plist[CR2RES_NB_DETECTORS] ;
231 char * out_file;
232 int i, j, det_nr, wished_ext_nb;
233
234 /* Initialise */
235
236 /* RETRIEVE INPUT PARAMETERS */
237 param = cpl_parameterlist_find_const(parlist,
238 "cr2res.cr2res_util_bpm_split.detector");
239 reduce_det = cpl_parameter_get_int(param);
240
241 /* Identify the RAW and CALIB frames in the input frameset */
242 if (cr2res_dfs_set_groups(frameset)) {
243 cpl_msg_error(__func__, "Cannot identify RAW and CALIB frames") ;
244 cpl_error_set(__func__, CPL_ERROR_ILLEGAL_INPUT) ;
245 return -1 ;
246 }
247
248 /* Get Calibration frames */
249
250 /* Get the rawframes */
251 rawframes = cr2res_io_find_BPM_all(frameset) ;
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) ;
255 return -1 ;
256 }
257
258 /* Loop on the RAW frames */
259 for (i = 0; i < cpl_frameset_get_size(rawframes); i++) {
260 const cpl_frame *cur_frame;
261 const char *cur_fname;
262 /* Get the Current Frame */
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() ;
267
268 /* Loop on the detectors */
269 for (det_nr=1 ; det_nr<=CR2RES_NB_DETECTORS ; det_nr++) {
270 /* Initialise */
271 for (j=0 ; j<CR2RES_NB_BPM_TYPES ; j++)
272 split_bpms[j][det_nr-1] = NULL ;
273 wished_ext_nb = cr2res_io_get_ext_idx(cur_fname, det_nr, 1) ;
274 ext_plist[det_nr-1]=cpl_propertylist_load(cur_fname,wished_ext_nb);
275
276 /* Compute only one detector */
277 if (reduce_det != 0 && det_nr != reduce_det) continue ;
278
279 cpl_msg_info(__func__, "Process Detector %d", det_nr) ;
280 cpl_msg_indent_more() ;
281
282 /* Load the image to calibrate */
283 ima = cr2res_io_load_BPM(cur_fname, det_nr, 1);
284
285 /* Loop on the BPM types */
286 for (j=0 ; j<CR2RES_NB_BPM_TYPES ; j++) {
287 my_mask = cr2res_bpm_extract_mask(ima, bpm_types[j]) ;
288 split_bpms[j][det_nr-1] =
289 cr2res_bpm_from_mask(my_mask, bpm_types[j]) ;
290 cpl_mask_delete(my_mask) ;
291 }
292 cpl_image_delete(ima);
293 cpl_msg_indent_less() ;
294 }
295
296 /* Save Products */
297
298 /* SPLITTED_BPM */
299 for (j=0 ; j<CR2RES_NB_BPM_TYPES ; j++) {
300 out_file=cpl_sprintf("%s_split_%d.fits",
301 cr2res_get_root_name(cur_fname), bpm_types[j]) ;
302 cur_fset = cpl_frameset_new() ;
303 cpl_frameset_insert(cur_fset, cpl_frame_duplicate(cur_frame)) ;
304 cr2res_io_save_BPM(out_file, frameset, cur_fset, parlist,
305 split_bpms[j], NULL, ext_plist,
306 CR2RES_UTIL_BPM_SPLIT_PROCATG, RECIPE_STRING) ;
307 cpl_frameset_delete(cur_fset) ;
308 cpl_free(out_file);
309 }
310 /* Free */
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]) ;
315 }
316 if (ext_plist[det_nr-1] != NULL)
317 cpl_propertylist_delete(ext_plist[det_nr-1]) ;
318 }
319 cpl_msg_indent_less() ;
320 }
321 cpl_frameset_delete(rawframes) ;
322 return (int)cpl_error_get_code();
323}
324
cpl_image * cr2res_bpm_from_mask(cpl_mask *mask, cr2res_bpm_type type)
Create a BPM from a mask.
Definition: cr2res_bpm.c:175
cpl_mask * cr2res_bpm_extract_mask(const cpl_image *bpm_ima, cr2res_bpm_type bpm_type)
Extract a mask from a BPM image.
Definition: cr2res_bpm.c:247
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
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.