CR2RE Pipeline Reference Manual 1.6.2
cr2res_util_extract.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_extract.h"
40#include "cr2res_bpm.h"
41
42/*-----------------------------------------------------------------------------
43 Define
44 -----------------------------------------------------------------------------*/
45
46#define RECIPE_STRING "cr2res_util_extract"
47
48/*-----------------------------------------------------------------------------
49 Plugin registration
50 -----------------------------------------------------------------------------*/
51
52int cpl_plugin_get_info(cpl_pluginlist * list);
53
54/*-----------------------------------------------------------------------------
55 Private function prototypes
56 -----------------------------------------------------------------------------*/
57
58static cpl_frameset * cr2res_util_extract_find_RAW(const cpl_frameset * in) ;
59static int cr2res_util_extract_create(cpl_plugin *);
60static int cr2res_util_extract_exec(cpl_plugin *);
61static int cr2res_util_extract_destroy(cpl_plugin *);
62static int cr2res_util_extract(cpl_frameset *, const cpl_parameterlist *);
63
64/*-----------------------------------------------------------------------------
65 Static variables
66 -----------------------------------------------------------------------------*/
67
68static char cr2res_util_extract_description[] = "\
69Spectrum Extraction \n\
70 This utility performs the optimal extraction along precomputed traces \n\
71 \n\
72 Inputs \n\
73 raw.fits " CR2RES_FLAT_RAW " [1 to n] \n\
74 or " CR2RES_UTIL_CALIB_PROCATG " \n\
75 or " CR2RES_WAVE_UNE_RAW " \n\
76 or " CR2RES_WAVE_FPET_RAW " \n\
77 or " CR2RES_CAL_NODDING_OTHER_RAW " \n\
78 or " CR2RES_CAL_NODDING_JITTER_RAW" \n\
79 or " CR2RES_OBS_NODDING_OTHER_RAW" \n\
80 or " CR2RES_OBS_NODDING_JITTER_RAW" \n\
81 or " CR2RES_OBS_ASTROMETRY_OTHER_RAW" \n\
82 or " CR2RES_OBS_ASTROMETRY_JITTER_RAW" \n\
83 or " CR2RES_OBS_STARING_OTHER_RAW" \n\
84 or " CR2RES_OBS_STARING_JITTER_RAW" \n\
85 or " CR2RES_OBS_STARING_WAVE_SKY_RAW" \n\
86 or " CR2RES_OBS_POLARIMETRY_OTHER_RAW" \n\
87 or " CR2RES_OBS_2D_OBJECT_RAW" \n\
88 or " CR2RES_OBS_2D_SKY_RAW" \n\
89 trace.fits " CR2RES_CAL_FLAT_TW_PROCATG " [1] \n\
90 or " CR2RES_CAL_FLAT_TW_MERGED_PROCATG " \n\
91 or " CR2RES_UTIL_TRACE_TW_PROCATG " \n\
92 or " CR2RES_UTIL_WAVE_TW_PROCATG " \n\
93 or " CR2RES_CAL_WAVE_TW_PROCATG " \n\
94 or " CR2RES_UTIL_SLIT_CURV_TW_PROCATG " \n\
95 slitfunc.fits " CR2RES_CAL_FLAT_SLIT_FUNC_PROCATG " [0 to 1] \n\
96 or " CR2RES_UTIL_SLIT_FUNC_PROCATG " \n\
97 or " CR2RES_OBS_NODDING_SLITFUNCA_PROCATG " \n\
98 or " CR2RES_OBS_NODDING_SLITFUNCB_PROCATG " \n\
99 or " CR2RES_OBS_STARING_SLITFUNC_PROCATG " \n\
100 bpm.fits " CR2RES_CAL_DARK_BPM_PROCATG " [0 to 1] \n\
101 or " CR2RES_CAL_FLAT_BPM_PROCATG " \n\
102 or " CR2RES_CAL_DETLIN_BPM_PROCATG " \n\
103 or " CR2RES_UTIL_BPM_MERGE_PROCATG " \n\
104 or " CR2RES_UTIL_BPM_SPLIT_PROCATG " \n\
105 \n\
106 Outputs \n\
107 <input_name>_extr1D.fits " CR2RES_UTIL_EXTRACT_1D_PROCATG " \n\
108 <input_name>_extrSlitFu.fits " CR2RES_UTIL_SLIT_FUNC_PROCATG " \n\
109 <input_name>_extrModel.fits " CR2RES_UTIL_SLIT_MODEL_PROCATG " \n\
110 \n\
111 Algorithm \n\
112 loop on raw frames f: \n\
113 loop on detectors d: \n\
114 Load the trace wave \n\
115 Recompute a new trace wave with the specified slit fraction \n\
116 (--slit_frac) if needed \n\
117 Load the image to extract \n\
118 Load the BPM and set them in the image \n\
119 Load the input slit_func if available \n\
120 Run the extraction cr2res_extract_traces(--method,--height, \n\
121 --swath_width,--oversample,--smooth_slit,--smooth_spec)\n\
122 -> creates SLIT_MODEL(f,d), SLIT_FUNC(f,d), EXTRACT_1D(f,d) \n\
123 Save SLIT_MODEL(f), SLIT_FUNC(f), EXTRACT_1D(f) \n\
124 \n\
125 Library functions used \n\
126 cr2res_io_load_TRACE_WAVE() \n\
127 cr2res_trace_new_slit_fraction() \n\
128 cr2res_io_load_image() \n\
129 cr2res_io_load_BPM() \n\
130 cr2res_extract_traces() \n\
131 cr2res_io_save_SLIT_MODEL() \n\
132 cr2res_io_save_SLIT_FUNC() \n\
133 cr2res_io_save_EXTRACT_1D() \n\
134" ;
135
136/*-----------------------------------------------------------------------------
137 Function code
138 -----------------------------------------------------------------------------*/
139
140/*----------------------------------------------------------------------------*/
150/*----------------------------------------------------------------------------*/
151int cpl_plugin_get_info(cpl_pluginlist * list)
152{
153 cpl_recipe * recipe = cpl_calloc(1, sizeof *recipe );
154 cpl_plugin * plugin = &recipe->interface;
155
156 if (cpl_plugin_init(plugin,
157 CPL_PLUGIN_API,
158 CR2RES_BINARY_VERSION,
159 CPL_PLUGIN_TYPE_RECIPE,
160 RECIPE_STRING,
161 "Optimal Extraction utility",
162 cr2res_util_extract_description,
163 CR2RES_PIPELINE_AUTHORS,
164 PACKAGE_BUGREPORT,
166 cr2res_util_extract_create,
167 cr2res_util_extract_exec,
168 cr2res_util_extract_destroy)) {
169 cpl_msg_error(cpl_func, "Plugin initialization failed");
170 (void)cpl_error_set_where(cpl_func);
171 return 1;
172 }
173
174 if (cpl_pluginlist_append(list, plugin)) {
175 cpl_msg_error(cpl_func, "Error adding plugin to list");
176 (void)cpl_error_set_where(cpl_func);
177 return 1;
178 }
179
180 return 0;
181}
182
183/*----------------------------------------------------------------------------*/
191/*----------------------------------------------------------------------------*/
192static int cr2res_util_extract_create(cpl_plugin * plugin)
193{
194 cpl_recipe * recipe;
195 cpl_parameter * p;
196
197 /* Check that the plugin is part of a valid recipe */
198 if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
199 recipe = (cpl_recipe *)plugin;
200 else
201 return -1;
202
203 /* Create the parameters list in the cpl_recipe object */
204 recipe->parameters = cpl_parameterlist_new();
205
206 /* Fill the parameters list */
207 p = cpl_parameter_new_value("cr2res.cr2res_util_extract.oversample",
208 CPL_TYPE_INT, "factor by which to oversample the extraction",
209 "cr2res.cr2res_util_extract", 5);
210 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "oversample");
211 cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV);
212 cpl_parameterlist_append(recipe->parameters, p);
213
214 p = cpl_parameter_new_value("cr2res.cr2res_util_extract.swath_width",
215 CPL_TYPE_INT, "The swath width", "cr2res.cr2res_util_extract", 800);
216 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "swath_width");
217 cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV);
218 cpl_parameterlist_append(recipe->parameters, p);
219
220 p = cpl_parameter_new_value("cr2res.cr2res_util_extract.height",
221 CPL_TYPE_INT, "Extraction height",
222 "cr2res.cr2res_util_extract", -1);
223 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "height");
224 cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV);
225 cpl_parameterlist_append(recipe->parameters, p);
226
227 p = cpl_parameter_new_value("cr2res.cr2res_util_extract.smooth_slit",
228 CPL_TYPE_DOUBLE,
229 "Smoothing along the slit at extraction",
230 "cr2res.cr2res_util_extract", 2.0);
231 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "smooth_slit");
232 cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV);
233 cpl_parameterlist_append(recipe->parameters, p);
234
235 p = cpl_parameter_new_value("cr2res.cr2res_util_extract.smooth_spec",
236 CPL_TYPE_DOUBLE,
237 "Smoothing spectrum at extraction",
238 "cr2res.cr2res_util_extract", 0.0);
239 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "smooth_spec");
240 cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV);
241 cpl_parameterlist_append(recipe->parameters, p);
242
243 p = cpl_parameter_new_value("cr2res.cr2res_util_extract.method",
244 CPL_TYPE_STRING, "Extraction method (SUM / MEDIAN / TILTSUM / "
245 "OPT_CURV )",
246 "cr2res.cr2res_util_extract", "OPT_CURV");
247 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "method");
248 cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV);
249 cpl_parameterlist_append(recipe->parameters, p);
250
251 p = cpl_parameter_new_value("cr2res.cr2res_util_extract.slit_frac",
252 CPL_TYPE_STRING, "Wished slit fraction",
253 "cr2res.cr2res_util_extract", "-1.0, -1.0");
254 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "slit_frac");
255 cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV);
256 cpl_parameterlist_append(recipe->parameters, p);
257
258 p = cpl_parameter_new_value("cr2res.cr2res_util_extract.detector",
259 CPL_TYPE_INT, "Only reduce the specified detector",
260 "cr2res.cr2res_util_extract", 0);
261 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "detector");
262 cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV);
263 cpl_parameterlist_append(recipe->parameters, p);
264
265 p = cpl_parameter_new_value("cr2res.cr2res_util_extract.order",
266 CPL_TYPE_INT, "Only reduce the specified order",
267 "cr2res.cr2res_util_extract", -1);
268 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "order");
269 cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV);
270 cpl_parameterlist_append(recipe->parameters, p);
271
272 p = cpl_parameter_new_value("cr2res.cr2res_util_extract.trace_nb",
273 CPL_TYPE_INT, "Only reduce the specified trace number",
274 "cr2res.cr2res_util_extract", -1);
275 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "trace_nb");
276 cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV);
277 cpl_parameterlist_append(recipe->parameters, p);
278
279 return 0;
280}
281
282/*----------------------------------------------------------------------------*/
288/*----------------------------------------------------------------------------*/
289static int cr2res_util_extract_exec(cpl_plugin * plugin)
290{
291 cpl_recipe *recipe;
292
293 /* Get the recipe out of the plugin */
294 if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
295 recipe = (cpl_recipe *)plugin;
296 else return -1;
297
298 return cr2res_util_extract(recipe->frames, recipe->parameters);
299}
300
301/*----------------------------------------------------------------------------*/
307/*----------------------------------------------------------------------------*/
308static int cr2res_util_extract_destroy(cpl_plugin * plugin)
309{
310 cpl_recipe *recipe;
311
312 /* Get the recipe out of the plugin */
313 if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
314 recipe = (cpl_recipe *)plugin;
315 else return -1 ;
316
317 cpl_parameterlist_delete(recipe->parameters);
318 return 0 ;
319}
320
321/*----------------------------------------------------------------------------*/
328/*----------------------------------------------------------------------------*/
329static int cr2res_util_extract(
330 cpl_frameset * frameset,
331 const cpl_parameterlist * parlist)
332{
333 const cpl_parameter * param;
334 int oversample, swath_width, extr_height,
335 reduce_det, reduce_order, reduce_trace ;
336 double smooth_slit, smooth_spec, slit_low, slit_up,
337 gain, error_factor ;
338 cpl_array * slit_frac ;
339 cpl_frameset * rawframes ;
340 cpl_frameset * cur_fset ;
341 const cpl_frame * trace_frame ;
342 const cpl_frame * bpm_frame ;
343 const cpl_frame * slit_func_frame ;
344 const char * sval ;
345 char * out_file;
346 hdrl_image * model_master[CR2RES_NB_DETECTORS] ;
347 cpl_table * slit_func_tab[CR2RES_NB_DETECTORS] ;
348 cpl_table * extract_tab[CR2RES_NB_DETECTORS] ;
349 cpl_propertylist * ext_plist[CR2RES_NB_DETECTORS] ;
350 cpl_table * trace_table ;
351 cpl_table * trace_table_new ;
352 hdrl_image * science_hdrl;
353 cpl_table * slit_func_in ;
354 int det_nr, ext_nr, i ;
355 cr2res_extr_method extr_method;
356
357 /* Needed for sscanf() */
358 setlocale(LC_NUMERIC, "C");
359
360 /* RETRIEVE INPUT PARAMETERS */
361 param = cpl_parameterlist_find_const(parlist,
362 "cr2res.cr2res_util_extract.oversample");
363 oversample = cpl_parameter_get_int(param);
364 param = cpl_parameterlist_find_const(parlist,
365 "cr2res.cr2res_util_extract.swath_width");
366 swath_width = cpl_parameter_get_int(param);
367 param = cpl_parameterlist_find_const(parlist,
368 "cr2res.cr2res_util_extract.smooth_slit");
369 smooth_slit = cpl_parameter_get_double(param);
370 param = cpl_parameterlist_find_const(parlist,
371 "cr2res.cr2res_util_extract.smooth_spec");
372 smooth_spec = cpl_parameter_get_double(param);
373 param = cpl_parameterlist_find_const(parlist,
374 "cr2res.cr2res_util_extract.detector");
375 reduce_det = cpl_parameter_get_int(param);
376 param = cpl_parameterlist_find_const(parlist,
377 "cr2res.cr2res_util_extract.order");
378 reduce_order = cpl_parameter_get_int(param);
379 param = cpl_parameterlist_find_const(parlist,
380 "cr2res.cr2res_util_extract.trace_nb");
381 reduce_trace = cpl_parameter_get_int(param);
382 param = cpl_parameterlist_find_const(parlist,
383 "cr2res.cr2res_util_extract.height");
384 extr_height = cpl_parameter_get_int(param);
385 param = cpl_parameterlist_find_const(parlist,
386 "cr2res.cr2res_util_extract.method");
387 sval = cpl_parameter_get_string(param);
388 if (!strcmp(sval, "")) extr_method = CR2RES_EXTR_OPT_CURV;
389 else if (!strcmp(sval, "OPT_CURV")) extr_method = CR2RES_EXTR_OPT_CURV;
390 else if (!strcmp(sval, "SUM")) extr_method = CR2RES_EXTR_SUM;
391 else if (!strcmp(sval, "MEDIAN")) extr_method = CR2RES_EXTR_MEDIAN;
392 else if (!strcmp(sval, "TILTSUM")) extr_method = CR2RES_EXTR_TILTSUM;
393 else {
394 cpl_msg_error(__func__, "Invalid Extraction Method specified");
395 cpl_error_set(__func__, CPL_ERROR_ILLEGAL_INPUT) ;
396 return -1;
397 }
398 param = cpl_parameterlist_find_const(parlist,
399 "cr2res.cr2res_util_extract.slit_frac");
400 sval = cpl_parameter_get_string(param) ;
401 if (sscanf(sval, "%lg,%lg", &slit_low, &slit_up) != 2) {
402 cpl_msg_error(__func__, "Invalid Slit Fraction specified");
403 cpl_error_set(__func__, CPL_ERROR_ILLEGAL_INPUT) ;
404 return -1 ;
405 }
406
407 /* TODO, make parameters */
408 int extract_niter = 30;
409 double extract_kappa = 10;
410
411 /* Check Parameters */
412 if (slit_low >= 0.0 && slit_up >= 0.0 && slit_low <= 1.0 && slit_up <= 1.0
413 && slit_up > slit_low) {
414 slit_frac = cpl_array_new(3, CPL_TYPE_DOUBLE) ;
415 cpl_array_set(slit_frac, 0, slit_low) ;
416 cpl_array_set(slit_frac, 1, (slit_low+slit_up)/2.0) ;
417 cpl_array_set(slit_frac, 2, slit_up) ;
418 } else {
419 slit_frac = NULL ;
420 }
421
422 /* Identify the RAW and CALIB frames in the input frameset */
423 if (cr2res_dfs_set_groups(frameset) != CPL_ERROR_NONE) {
424 cpl_msg_error(__func__, "Cannot identify RAW and CALIB frames") ;
425 cpl_error_set(__func__, CPL_ERROR_ILLEGAL_INPUT) ;
426 if (slit_frac != NULL) cpl_array_delete(slit_frac) ;
427 return -1 ;
428 }
429
430 /* Get Calibration frames */
431 trace_frame = cr2res_io_find_TRACE_WAVE(frameset) ;
432 bpm_frame = cr2res_io_find_BPM(frameset) ;
433 slit_func_frame = cr2res_io_find_SLIT_FUNC(frameset) ;
434
435 /* Get the rawframes */
436 rawframes = cr2res_util_extract_find_RAW(frameset) ;
437 if (rawframes==NULL || cpl_frameset_get_size(rawframes) <= 0) {
438 cpl_msg_error(__func__, "Cannot find any RAW file") ;
439 cpl_error_set(__func__, CPL_ERROR_DATA_NOT_FOUND) ;
440 if (rawframes!= NULL) cpl_frameset_delete(rawframes) ;
441 return -1 ;
442 }
443
444 if (trace_frame == NULL) {
445 cpl_msg_error(__func__, "The utility needs a trace wave frame");
446 cpl_error_set(__func__, CPL_ERROR_ILLEGAL_INPUT) ;
447 if (slit_frac != NULL) cpl_array_delete(slit_frac) ;
448 return -1 ;
449 }
450
451 /* Loop on the RAW frames */
452 for (i = 0; i < cpl_frameset_get_size(rawframes); i++) {
453 const cpl_frame *cur_frame;
454 const char *cur_fname;
455 cpl_propertylist *prim_plist;
456 /* Get the Current Frame */
457 cur_frame = cpl_frameset_get_position(rawframes, i) ;
458 cur_fname = cpl_frame_get_filename(cur_frame) ;
459 cpl_msg_info(__func__, "Reduce Frame %s", cur_fname) ;
460 cpl_msg_indent_more() ;
461
462 /* Needed for NDIT for error factor */
463 prim_plist = cpl_propertylist_load(cur_fname, 0);
464
465 /* Loop over the detectors */
466 for (det_nr=1 ; det_nr<=CR2RES_NB_DETECTORS ; det_nr++) {
467
468 /* Initialise */
469 model_master[det_nr-1] = NULL ;
470 slit_func_tab[det_nr-1] = NULL ;
471 extract_tab[det_nr-1] = NULL ;
472 ext_plist[det_nr-1] = NULL ;
473
474 /* Store the extension header for product saving */
475 ext_nr = cr2res_io_get_ext_idx(cur_fname, det_nr, 1) ;
476 if (ext_nr < 0) continue ;
477 ext_plist[det_nr-1] = cpl_propertylist_load(cur_fname, ext_nr) ;
478
479 /* Compute only one detector */
480 if (reduce_det != 0 && det_nr != reduce_det) continue ;
481
482 cpl_msg_info(__func__, "Process detector number %d", det_nr) ;
483 cpl_msg_indent_more() ;
484
485 /* Load the trace table of this detector */
486 cpl_msg_info(__func__, "Load the trace table") ;
487 if ((trace_table = cr2res_io_load_TRACE_WAVE(
488 cpl_frame_get_filename(trace_frame),
489 det_nr)) == NULL) {
490 cpl_msg_error(__func__,
491 "Failed to get trace table - skip detector");
492 cpl_error_reset() ;
493 cpl_msg_indent_less() ;
494 continue ;
495 }
496
497 /* Extract at the specified slit fraction */
498 if (slit_frac != NULL) {
499 if ((trace_table_new = cr2res_trace_new_slit_fraction(
500 trace_table, slit_frac)) == NULL) {
501 cpl_msg_warning(__func__,
502 "Failed to compute the traces for user specified slit fraction") ;
503 cpl_error_reset() ;
504 } else {
505 cpl_table_delete(trace_table) ;
506 trace_table = trace_table_new ;
507 trace_table_new = NULL ;
508 }
509 }
510
511 /* Load the image in which the traces are to extract */
512 cpl_msg_info(__func__, "Load the Image") ;
513 if ((science_hdrl = cr2res_io_load_image(cur_fname, det_nr))==NULL){
514 cpl_table_delete(trace_table) ;
515 cpl_msg_error(__func__,
516 "Failed to load the image - skip detector");
517 cpl_error_reset() ;
518 cpl_msg_indent_less() ;
519 continue ;
520 }
521 /* Load the BPM and assign to hdrl-mask*/
522 if (bpm_frame != NULL) {
523 cpl_msg_info(__func__, "Load and assign the BPM") ;
525 hdrl_image_get_image(science_hdrl),
526 cpl_frame_get_filename(bpm_frame), det_nr, 1)){
527 cpl_table_delete(trace_table) ;
528 hdrl_image_delete(science_hdrl) ;
529 cpl_msg_error(__func__,
530 "Failed to load BPM - skip detector");
531 cpl_error_reset() ;
532 cpl_msg_indent_less() ;
533 continue ;
534 }
535 }
536
537 /* Load the SLIT_FUNC table */
538 if (slit_func_frame != NULL) {
539 slit_func_in = cr2res_io_load_SLIT_FUNC(
540 cpl_frame_get_filename(slit_func_frame),
541 det_nr) ;
542 } else {
543 slit_func_in = NULL ;
544 }
545
546 /* Get the Gain and set error factor */
547 if (det_nr == 1) gain = CR2RES_GAIN_CHIP1 ;
548 else if (det_nr == 2) gain = CR2RES_GAIN_CHIP2 ;
549 else if (det_nr == 3) gain = CR2RES_GAIN_CHIP3 ;
550 else {
551 cpl_msg_error(__func__, "Failed to get the Gain value, det %d",
552 det_nr) ;
553 gain = -1.0 ;
554 }
555 error_factor = gain * cr2res_pfits_get_ndit(prim_plist);
556
557 /* Compute the extraction */
558 cpl_msg_info(__func__, "Spectra Extraction") ;
559 if (cr2res_extract_traces(science_hdrl, trace_table,
560 slit_func_in, NULL, 0, reduce_order, reduce_trace,
561 extr_method, extr_height, swath_width, oversample,
562 smooth_slit, smooth_spec,
563 extract_niter, extract_kappa, error_factor, 0, 0, 0,
564 &(extract_tab[det_nr-1]), &(slit_func_tab[det_nr-1]),
565 &(model_master[det_nr-1]))==-1) {
566 cpl_table_delete(trace_table) ;
567 hdrl_image_delete(science_hdrl) ;
568 if (slit_func_in != NULL) cpl_table_delete(slit_func_in) ;
569 cpl_msg_error(__func__, "Failed to extract - skip detector");
570 cpl_error_reset() ;
571 cpl_msg_indent_less() ;
572 continue ;
573 }
574 if (slit_func_in != NULL) cpl_table_delete(slit_func_in) ;
575 hdrl_image_delete(science_hdrl) ;
576 cpl_table_delete(trace_table) ;
577 cpl_msg_indent_less() ;
578 }
579 cpl_array_delete(slit_frac) ;
580 cpl_propertylist_delete(prim_plist);
581
582 /* Generate the currently used frameset */
583 /* TODO : add calibrations */
584 cur_fset = cpl_frameset_new() ;
585 cpl_frameset_insert(cur_fset, cpl_frame_duplicate(cur_frame)) ;
586
587 /* Save the Products */
588 out_file = cpl_sprintf("%s_extrModel.fits",
590 cr2res_io_save_SLIT_MODEL(out_file, frameset, cur_fset, parlist,
591 model_master, NULL, ext_plist, CR2RES_UTIL_SLIT_MODEL_PROCATG,
592 RECIPE_STRING) ;
593 cpl_free(out_file);
594 out_file = cpl_sprintf("%s_extrSlitFu.fits",
596 cr2res_io_save_SLIT_FUNC(out_file, frameset, cur_fset, parlist,
597 slit_func_tab, NULL, ext_plist, CR2RES_UTIL_SLIT_FUNC_PROCATG,
598 RECIPE_STRING) ;
599 cpl_free(out_file);
600 out_file = cpl_sprintf("%s_extr1D.fits",
602 cr2res_io_save_EXTRACT_1D(out_file, frameset, cur_fset, parlist,
603 extract_tab, NULL, ext_plist, CR2RES_UTIL_EXTRACT_1D_PROCATG,
604 RECIPE_STRING) ;
605 cpl_free(out_file);
606 cpl_frameset_delete(cur_fset) ;
607
608 /* Free and return */
609 for (det_nr=1 ; det_nr<=CR2RES_NB_DETECTORS ; det_nr++) {
610 if (ext_plist[det_nr-1] != NULL)
611 cpl_propertylist_delete(ext_plist[det_nr-1]) ;
612 if (slit_func_tab[det_nr-1] != NULL)
613 cpl_table_delete(slit_func_tab[det_nr-1]) ;
614 if (extract_tab[det_nr-1] != NULL)
615 cpl_table_delete(extract_tab[det_nr-1]) ;
616 if (model_master[det_nr-1] != NULL)
617 hdrl_image_delete(model_master[det_nr-1]) ;
618 }
619 cpl_msg_indent_less() ;
620 }
621 cpl_frameset_delete(rawframes) ;
622 return (int)cpl_error_get_code();
623}
624
625/*----------------------------------------------------------------------------*/
647/*----------------------------------------------------------------------------*/
648static cpl_frameset * cr2res_util_extract_find_RAW(const cpl_frameset * in)
649{
650 cpl_frameset * out ;
651 char ** tags ;
652 int i, ntags ;
653
654 /* Check entries */
655 if (in == NULL) return NULL ;
656
657 /* Create the tags list */
658 ntags = 16 ;
659 tags = cpl_malloc(ntags * sizeof(char *)) ;
660 tags[0] = cpl_sprintf(CR2RES_FLAT_RAW) ;
661 tags[1] = cpl_sprintf(CR2RES_UTIL_CALIB_PROCATG) ;
662 tags[2] = cpl_sprintf(CR2RES_WAVE_UNE_RAW) ;
663 tags[3] = cpl_sprintf(CR2RES_WAVE_FPET_RAW) ;
664 tags[4] = cpl_sprintf(CR2RES_CAL_NODDING_OTHER_RAW) ;
665 tags[5] = cpl_sprintf(CR2RES_CAL_NODDING_JITTER_RAW) ;
666 tags[6] = cpl_sprintf(CR2RES_OBS_NODDING_OTHER_RAW) ;
667 tags[7] = cpl_sprintf(CR2RES_OBS_NODDING_JITTER_RAW) ;
668 tags[8] = cpl_sprintf(CR2RES_OBS_ASTROMETRY_OTHER_RAW) ;
669 tags[9] = cpl_sprintf(CR2RES_OBS_ASTROMETRY_JITTER_RAW) ;
670 tags[10] = cpl_sprintf(CR2RES_OBS_STARING_OTHER_RAW) ;
671 tags[11] = cpl_sprintf(CR2RES_OBS_STARING_JITTER_RAW) ;
672 tags[12] = cpl_sprintf(CR2RES_OBS_STARING_WAVE_SKY_RAW) ;
673 tags[13] = cpl_sprintf(CR2RES_OBS_POLARIMETRY_OTHER_RAW) ;
674 tags[14] = cpl_sprintf(CR2RES_OBS_2D_OBJECT_RAW) ;
675 tags[15] = cpl_sprintf(CR2RES_OBS_2D_SKY_RAW) ;
676
677 /* Get the frameset */
678 out = cr2res_extract_frameset_several_tags(in, (const char**)tags, ntags) ;
679
680 for (i=0 ; i<ntags ; i++) cpl_free(tags[i]) ;
681 cpl_free(tags) ;
682 return out ;
683}
684
int cr2res_bpm_set_and_correct_image(cpl_image *in, const char *bpm, int chip, int correct)
Set the BPM and optionally apply the correction to an image.
Definition: cr2res_bpm.c:199
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_extract_traces(const hdrl_image *img, const cpl_table *traces, const cpl_table *slit_func_in, const cpl_table *blaze_table_in, float blaze_norm, int reduce_order, int reduce_trace, cr2res_extr_method extr_method, int extr_height, int swath_width, int oversample, double smooth_slit, double smooth_spec, int niter, double kappa, double error_factor, int display, int disp_order_idx, int disp_trace, cpl_table **extracted, cpl_table **slit_func, hdrl_image **model_master)
Extracts all the passed traces at once.
const cpl_frame * cr2res_io_find_BPM(const cpl_frameset *in)
Get the first CR2RES_BPM_DRSTYPE frame from a frameset.
Definition: cr2res_io.c:369
int cr2res_io_save_SLIT_MODEL(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_MODEL.
Definition: cr2res_io.c:1823
const cpl_frame * cr2res_io_find_SLIT_FUNC(const cpl_frameset *in)
Get the first CR2RES_SLIT_FUNC_DRSTYPE frame from a frameset.
Definition: cr2res_io.c:267
const cpl_frame * cr2res_io_find_TRACE_WAVE(const cpl_frameset *in)
Get the first CR2RES_TW_DRSTYPE frame from a frameset.
Definition: cr2res_io.c:231
int cr2res_io_save_EXTRACT_1D(const char *filename, cpl_frameset *allframes, cpl_frameset *inframes, const cpl_parameterlist *parlist, cpl_table **tables, const cpl_propertylist *qc_list, cpl_propertylist **ext_plist, const char *procatg, const char *recipe)
Save a 1D extracted spectrum.
Definition: cr2res_io.c:1732
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
cpl_table * cr2res_io_load_SLIT_FUNC(const char *filename, int detector)
Load a table from a SLIT_FUNC.
Definition: cr2res_io.c:1139
hdrl_image * cr2res_io_load_image(const char *in, int detector)
Load an hdrl image from a image file.
Definition: cr2res_io.c:704
int cr2res_io_save_SLIT_FUNC(const char *filename, cpl_frameset *allframes, cpl_frameset *inframes, const cpl_parameterlist *parlist, cpl_table **slit_func, const cpl_propertylist *qc_list, cpl_propertylist **ext_plist, const char *procatg, const char *recipe)
Save a SLIT_FUNC.
Definition: cr2res_io.c:1792
cpl_table * cr2res_io_load_TRACE_WAVE(const char *filename, int detector)
Load a table from a TRACE_WAVE.
Definition: cr2res_io.c:1109
int cr2res_pfits_get_ndit(const cpl_propertylist *plist)
find out the NDIT value
Definition: cr2res_pfits.c:363
cpl_table * cr2res_trace_new_slit_fraction(const cpl_table *traces, const cpl_array *new_slit_fraction)
Recompute the traces at a newly specified slit fraction.
char * cr2res_get_root_name(const char *filename)
Find out the root part of a basename (name without extension).
cpl_frameset * cr2res_extract_frameset_several_tags(const cpl_frameset *in, const char **tags, int ntags)
Extract the frames whose tag is within a list 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.
cpl_image * hdrl_image_get_image(hdrl_image *himg)
get data as cpl image
Definition: hdrl_image.c:105
void hdrl_image_delete(hdrl_image *himg)
delete hdrl_image
Definition: hdrl_image.c:379