CR2RE Pipeline Reference Manual 1.6.8
cr2res_util_trace_map.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 <locale.h>
29#include <string.h>
30
31#include <cpl.h>
32#include "hdrl.h"
33
34#include "cr2res_utils.h"
35#include "cr2res_pfits.h"
36#include "cr2res_dfs.h"
37#include "cr2res_io.h"
38#include "cr2res_trace.h"
39#include "cr2res_wave.h"
40#include "cr2res_slit_curv.h"
41
42/*-----------------------------------------------------------------------------
43 Define
44 -----------------------------------------------------------------------------*/
45
46#define RECIPE_STRING "cr2res_util_trace_map"
47
48/*-----------------------------------------------------------------------------
49 Plugin registration
50 -----------------------------------------------------------------------------*/
51
52int cpl_plugin_get_info(cpl_pluginlist * list);
53
54/*-----------------------------------------------------------------------------
55 Private function prototypes
56 -----------------------------------------------------------------------------*/
57
58static int cr2res_util_trace_map_create(cpl_plugin *);
59static int cr2res_util_trace_map_exec(cpl_plugin *);
60static int cr2res_util_trace_map_destroy(cpl_plugin *);
61static int cr2res_util_trace_map(cpl_frameset *, const cpl_parameterlist *);
62
63/*-----------------------------------------------------------------------------
64 Static variables
65 -----------------------------------------------------------------------------*/
66
67static char cr2res_util_trace_map_description[] = "\
68Maps creation \n\
69 Each input TRACE_WAVE file is converted into maps to visualize the \n\
70 traces, wavelengths and the slit curvature \n\
71 \n\
72 Inputs \n\
73 raw.fits " CR2RES_TW_DRSTYPE " [1 to n] \n\
74 \n\
75 Outputs \n\
76 <input_name>_slit_curve.fits "
77 CR2RES_UTIL_TRACE_MAP_SLIT_CURVE_PROCATG "\n\
78 <input_name>_wave.fits "
79 CR2RES_UTIL_TRACE_MAP_WL_PROCATG "\n\
80 <input_name>_trace.fits "
81 CR2RES_UTIL_TRACE_MAP_TRACE_PROCATG "\n\
82 \n\
83 Algorithm \n\
84 loop on input raw frames f: \n\
85 loop on detectors d: \n\
86 Load the trace_wave extension \n\
87 Call cr2res_wave_gen_wave_map() to generate wave_map(d) \n\
88 Call cr2res_trace_gen_image() to generate traces_map(d) \n\
89 Call cr2res_slit_curv_gen_map() to generate slit_curv_map(d) \n\
90 Save wave_map, traces_map and slit_curv_map \n\
91 \n\
92 Library Functions used \n\
93 cr2res_io_load_TRACE_WAVE() \n\
94 cr2res_trace_gen_image() \n\
95 cr2res_slit_curv_gen_map() \n\
96 cr2res_io_save_SLIT_CURV_MAP() \n\
97 cr2res_io_save_WAVE_MAP() \n\
98 cr2res_io_save_TRACE_MAP() \n\
99";
100
101/*-----------------------------------------------------------------------------
102 Function code
103 -----------------------------------------------------------------------------*/
104
105/*----------------------------------------------------------------------------*/
115/*----------------------------------------------------------------------------*/
116int cpl_plugin_get_info(cpl_pluginlist * list)
117{
118 cpl_recipe * recipe = cpl_calloc(1, sizeof *recipe );
119 cpl_plugin * plugin = &recipe->interface;
120
121 if (cpl_plugin_init(plugin,
122 CPL_PLUGIN_API,
123 CR2RES_BINARY_VERSION,
124 CPL_PLUGIN_TYPE_RECIPE,
125 RECIPE_STRING,
126 "TRACE_WAVE maps creation",
127 cr2res_util_trace_map_description,
128 CR2RES_PIPELINE_AUTHORS,
129 PACKAGE_BUGREPORT,
131 cr2res_util_trace_map_create,
132 cr2res_util_trace_map_exec,
133 cr2res_util_trace_map_destroy)) {
134 cpl_msg_error(cpl_func, "Plugin initialization failed");
135 (void)cpl_error_set_where(cpl_func);
136 return 1;
137 }
138
139 if (cpl_pluginlist_append(list, plugin)) {
140 cpl_msg_error(cpl_func, "Error adding plugin to list");
141 (void)cpl_error_set_where(cpl_func);
142 return 1;
143 }
144
145 return 0;
146}
147
148/*----------------------------------------------------------------------------*/
156/*----------------------------------------------------------------------------*/
157static int cr2res_util_trace_map_create(cpl_plugin * plugin)
158{
159 cpl_recipe * recipe;
160 cpl_parameter * p;
161
162 /* Check that the plugin is part of a valid recipe */
163 if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
164 recipe = (cpl_recipe *)plugin;
165 else
166 return -1;
167
168 /* Create the parameters list in the cpl_recipe object */
169 recipe->parameters = cpl_parameterlist_new();
170
171 /* Fill the parameters list */
172 p = cpl_parameter_new_value("cr2res.cr2res_util_trace_map.detector",
173 CPL_TYPE_INT, "Only reduce the specified detector",
174 "cr2res.cr2res_util_trace_map", 0);
175 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "detector");
176 cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV);
177 cpl_parameterlist_append(recipe->parameters, p);
178
179 p = cpl_parameter_new_value("cr2res.cr2res_util_trace_map.order",
180 CPL_TYPE_INT, "Only reduce the specified order",
181 "cr2res.cr2res_util_trace_map", -1);
182 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "order");
183 cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV);
184 cpl_parameterlist_append(recipe->parameters, p);
185
186 p = cpl_parameter_new_value("cr2res.cr2res_util_trace_map.trace_nb",
187 CPL_TYPE_INT, "Only reduce the specified trace number",
188 "cr2res.cr2res_util_trace_map", -1);
189 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "trace_nb");
190 cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV);
191 cpl_parameterlist_append(recipe->parameters, p);
192
193 return 0;
194}
195
196/*----------------------------------------------------------------------------*/
202/*----------------------------------------------------------------------------*/
203static int cr2res_util_trace_map_exec(cpl_plugin * plugin)
204{
205 cpl_recipe *recipe;
206
207 /* Get the recipe out of the plugin */
208 if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
209 recipe = (cpl_recipe *)plugin;
210 else return -1;
211
212 return cr2res_util_trace_map(recipe->frames, recipe->parameters);
213}
214
215/*----------------------------------------------------------------------------*/
221/*----------------------------------------------------------------------------*/
222static int cr2res_util_trace_map_destroy(cpl_plugin * plugin)
223{
224 cpl_recipe *recipe;
225
226 /* Get the recipe out of the plugin */
227 if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
228 recipe = (cpl_recipe *)plugin;
229 else return -1 ;
230
231 cpl_parameterlist_delete(recipe->parameters);
232 return 0 ;
233}
234
235/*----------------------------------------------------------------------------*/
242/*----------------------------------------------------------------------------*/
243static int cr2res_util_trace_map(
244 cpl_frameset * frameset,
245 const cpl_parameterlist * parlist)
246{
247 const cpl_parameter * param;
248 int reduce_det, reduce_order, reduce_trace ;
249 cpl_frameset * rawframes ;
250 cpl_frameset * cur_fset ;
251 char * out_file;
252 cpl_image * img_tmp ;
253 hdrl_image * wl_maps[CR2RES_NB_DETECTORS] ;
254 hdrl_image * trace_maps[CR2RES_NB_DETECTORS] ;
255 hdrl_image * slit_curve_maps[CR2RES_NB_DETECTORS] ;
256 cpl_propertylist * ext_plist[CR2RES_NB_DETECTORS] ;
257 cpl_table * trace_table ;
258 int det_nr, ext_nr, i;
259
260 /* Initialise */
261
262 /* RETRIEVE INPUT PARAMETERS */
263 param = cpl_parameterlist_find_const(parlist,
264 "cr2res.cr2res_util_trace_map.detector");
265 reduce_det = cpl_parameter_get_int(param);
266 param = cpl_parameterlist_find_const(parlist,
267 "cr2res.cr2res_util_trace_map.order");
268 reduce_order = cpl_parameter_get_int(param);
269 param = cpl_parameterlist_find_const(parlist,
270 "cr2res.cr2res_util_trace_map.trace_nb");
271 reduce_trace = cpl_parameter_get_int(param);
272
273 /* Check Parameters */
274
275 /* Identify the RAW and CALIB frames in the input frameset */
276 if (cr2res_dfs_set_groups(frameset) != CPL_ERROR_NONE) {
277 cpl_msg_error(__func__, "Cannot identify RAW and CALIB frames") ;
278 cpl_error_set(__func__, CPL_ERROR_ILLEGAL_INPUT) ;
279 return -1 ;
280 }
281
282 /* Get Calibration frames */
283
284 /* Get the Rawframes */
285 rawframes = cr2res_extract_frameset(frameset, CR2RES_TW_DRSTYPE) ;
286 if (rawframes==NULL || cpl_frameset_get_size(rawframes) <= 0) {
287 cpl_msg_error(__func__, "Cannot find any RAW file") ;
288 cpl_error_set(__func__, CPL_ERROR_DATA_NOT_FOUND) ;
289 return -1 ;
290 }
291
292 /* Loop on the RAW frames */
293 for (i = 0; i < cpl_frameset_get_size(rawframes); i++) {
294 const cpl_frame *cur_frame;
295 const char *cur_fname;
296 /* Get the Current Frame */
297 cur_frame = cpl_frameset_get_position(rawframes, i) ;
298 cur_fname = cpl_frame_get_filename(cur_frame) ;
299 cpl_msg_info(__func__, "Reduce Frame %s", cur_fname) ;
300 cpl_msg_indent_more() ;
301
302 /* Loop over the detectors */
303 for (det_nr=1 ; det_nr<=CR2RES_NB_DETECTORS ; det_nr++) {
304
305 /* Initialise */
306 wl_maps[det_nr-1] = NULL ;
307 trace_maps[det_nr-1] = NULL ;
308 slit_curve_maps[det_nr-1] = NULL ;
309
310 /* Get Extension Numbers */
311 ext_nr = cr2res_io_get_ext_idx(cur_fname, det_nr, 1) ;
312 if (ext_nr < 0) continue ;
313
314 /* Store the extension header for product saving */
315 ext_plist[det_nr-1] = cpl_propertylist_load(cur_fname, ext_nr) ;
316
317 /* Compute only one detector */
318 if (reduce_det != 0 && det_nr != reduce_det) continue ;
319
320 cpl_msg_info(__func__, "Process detector number %d", det_nr) ;
321 cpl_msg_indent_more() ;
322
323 /* Load the trace table of this detector */
324 cpl_msg_info(__func__, "Load the trace table") ;
325 if ((trace_table = cr2res_io_load_TRACE_WAVE(cur_fname,
326 det_nr)) == NULL) {
327 cpl_msg_error(__func__,
328 "Failed to get trace table - skip detector");
329 cpl_error_reset() ;
330 cpl_msg_indent_less() ;
331 continue ;
332 }
333
334 /* Create WAVE_MAP */
335 wl_maps[det_nr-1] =
336 cr2res_wave_gen_wave_map(trace_table) ;
337
338 /* Create TRACE MAP */
339 img_tmp = cr2res_trace_gen_image(trace_table,
340 CR2RES_DETECTOR_SIZE, CR2RES_DETECTOR_SIZE) ;
341 trace_maps[det_nr-1] = hdrl_image_create(img_tmp, NULL);
342 cpl_image_delete(img_tmp);
343
344 /* Create SLIT_CURVE MAP */
345 slit_curve_maps[det_nr-1] =
346 cr2res_slit_curv_gen_map(trace_table, reduce_order,
347 reduce_trace, 50, 0) ;
348
349 cpl_table_delete(trace_table) ;
350 cpl_msg_indent_less() ;
351 }
352
353 /* Save the Products */
354 cur_fset = cpl_frameset_new() ;
355 cpl_frameset_insert(cur_fset, cpl_frame_duplicate(cur_frame)) ;
356
357 out_file = cpl_sprintf("%s_slit_curve.fits",
359 cr2res_io_save_SLIT_CURV_MAP(out_file, frameset, cur_fset, parlist,
360 slit_curve_maps, NULL, ext_plist,
361 CR2RES_UTIL_TRACE_MAP_SLIT_CURVE_PROCATG, RECIPE_STRING);
362 cpl_free(out_file);
363
364 out_file = cpl_sprintf("%s_wave.fits",
366 cr2res_io_save_WAVE_MAP(out_file, frameset, cur_fset, parlist, wl_maps,
367 NULL, ext_plist, CR2RES_UTIL_TRACE_MAP_WL_PROCATG,
368 RECIPE_STRING);
369 cpl_free(out_file);
370
371 out_file = cpl_sprintf("%s_trace.fits",
373 cr2res_io_save_TRACE_MAP(out_file, frameset, cur_fset, parlist,
374 trace_maps, NULL, ext_plist,
375 CR2RES_UTIL_TRACE_MAP_TRACE_PROCATG, RECIPE_STRING);
376 cpl_free(out_file);
377 cpl_frameset_delete(cur_fset) ;
378
379 /* Free and return */
380 for (det_nr=1 ; det_nr<=CR2RES_NB_DETECTORS ; det_nr++) {
381 if (ext_plist[det_nr-1] != NULL)
382 cpl_propertylist_delete(ext_plist[det_nr-1]) ;
383 if (slit_curve_maps[det_nr-1] != NULL)
384 hdrl_image_delete(slit_curve_maps[det_nr-1]) ;
385 if (trace_maps[det_nr-1] != NULL)
386 hdrl_image_delete(trace_maps[det_nr-1]) ;
387 if (wl_maps[det_nr-1] != NULL)
388 hdrl_image_delete(wl_maps[det_nr-1]) ;
389 }
390 cpl_msg_indent_less() ;
391 }
392 cpl_frameset_delete(rawframes) ;
393 return (int)cpl_error_get_code();
394}
395
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
int cr2res_io_save_WAVE_MAP(const char *filename, cpl_frameset *allframes, cpl_frameset *inframes, const cpl_parameterlist *parlist, hdrl_image **data, const cpl_propertylist *qc_list, cpl_propertylist **ext_plist, const char *procatg, const char *recipe)
Save a WAVE_MAP.
Definition: cr2res_io.c:1916
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_TRACE_MAP(const char *filename, cpl_frameset *allframes, cpl_frameset *inframes, const cpl_parameterlist *parlist, hdrl_image **data, const cpl_propertylist *qc_list, cpl_propertylist **ext_plist, const char *procatg, const char *recipe)
Save a TRACE_MAP.
Definition: cr2res_io.c:1885
int cr2res_io_save_SLIT_CURV_MAP(const char *filename, cpl_frameset *allframes, cpl_frameset *inframes, const cpl_parameterlist *parlist, hdrl_image **data, const cpl_propertylist *qc_list, cpl_propertylist **ext_plist, const char *procatg, const char *recipe)
Save a SLIT_CURV_MAP.
Definition: cr2res_io.c:1947
cpl_table * cr2res_io_load_TRACE_WAVE(const char *filename, int detector)
Load a table from a TRACE_WAVE.
Definition: cr2res_io.c:1109
hdrl_image * cr2res_slit_curv_gen_map(const cpl_table *trace_wave, int order, int trace_id, int spacing_pixels, int full_trace)
Compute the slit_curv map from the trace_wave table.
cpl_image * cr2res_trace_gen_image(cpl_table *trace, int nx, int ny)
Make an image out of the trace solution.
Definition: cr2res_trace.c:362
char * cr2res_get_root_name(const char *filename)
Find out the root part of a basename (name without extension).
cpl_frameset * cr2res_extract_frameset(const cpl_frameset *in, const char *tag)
Extract the frames with the given tag from a frameset.
char * cr2res_get_base_name(const char *filename)
Find out the base name of a file (i.e. without prefix path)
const char * cr2res_get_license(void)
Get the pipeline copyright and license.
hdrl_image * cr2res_wave_gen_wave_map(const cpl_table *trace_wave)
Compute the wavelength map from the trace_wave table.
Definition: cr2res_wave.c:1608
hdrl_image * hdrl_image_create(const cpl_image *image, const cpl_image *error)
create a new hdrl_image from to existing images by copying them
Definition: hdrl_image.c:295
void hdrl_image_delete(hdrl_image *himg)
delete hdrl_image
Definition: hdrl_image.c:379