CR2RE Pipeline Reference Manual 1.6.11
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.pclip",
215 CPL_TYPE_DOUBLE, "percentage of high and low pixels to clip on first iteration",
216 "cr2res_util_extract.pclip", 0.1);
217 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "pclip");
218 cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV);
219 cpl_parameterlist_append(recipe->parameters, p);
220
221 p = cpl_parameter_new_value("cr2res.cr2res_util_extract.swath_width",
222 CPL_TYPE_INT, "The swath width", "cr2res.cr2res_util_extract", 800);
223 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "swath_width");
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.height",
228 CPL_TYPE_INT, "Extraction height",
229 "cr2res.cr2res_util_extract", -1);
230 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "height");
231 cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV);
232 cpl_parameterlist_append(recipe->parameters, p);
233
234 p = cpl_parameter_new_value("cr2res.cr2res_util_extract.smooth_slit",
235 CPL_TYPE_DOUBLE,
236 "Smoothing along the slit at extraction",
237 "cr2res.cr2res_util_extract", 2.0);
238 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "smooth_slit");
239 cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV);
240 cpl_parameterlist_append(recipe->parameters, p);
241
242 p = cpl_parameter_new_value("cr2res.cr2res_util_extract.smooth_spec",
243 CPL_TYPE_DOUBLE,
244 "Smoothing spectrum at extraction",
245 "cr2res.cr2res_util_extract", 0.0);
246 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "smooth_spec");
247 cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV);
248 cpl_parameterlist_append(recipe->parameters, p);
249
250 p = cpl_parameter_new_value("cr2res.cr2res_util_extract.method",
251 CPL_TYPE_STRING, "Extraction method (SUM / MEDIAN / TILTSUM / "
252 "OPT_CURV )",
253 "cr2res.cr2res_util_extract", "OPT_CURV");
254 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "method");
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.slit_frac",
259 CPL_TYPE_STRING, "Wished slit fraction",
260 "cr2res.cr2res_util_extract", "-1.0, -1.0");
261 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "slit_frac");
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.detector",
266 CPL_TYPE_INT, "Only reduce the specified detector",
267 "cr2res.cr2res_util_extract", 0);
268 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "detector");
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.order",
273 CPL_TYPE_INT, "Only reduce the specified order",
274 "cr2res.cr2res_util_extract", -1);
275 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "order");
276 cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV);
277 cpl_parameterlist_append(recipe->parameters, p);
278
279 p = cpl_parameter_new_value("cr2res.cr2res_util_extract.trace_nb",
280 CPL_TYPE_INT, "Only reduce the specified trace number",
281 "cr2res.cr2res_util_extract", -1);
282 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "trace_nb");
283 cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV);
284 cpl_parameterlist_append(recipe->parameters, p);
285
286 return 0;
287}
288
289/*----------------------------------------------------------------------------*/
295/*----------------------------------------------------------------------------*/
296static int cr2res_util_extract_exec(cpl_plugin * plugin)
297{
298 cpl_recipe *recipe;
299
300 /* Get the recipe out of the plugin */
301 if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
302 recipe = (cpl_recipe *)plugin;
303 else return -1;
304
305 return cr2res_util_extract(recipe->frames, recipe->parameters);
306}
307
308/*----------------------------------------------------------------------------*/
314/*----------------------------------------------------------------------------*/
315static int cr2res_util_extract_destroy(cpl_plugin * plugin)
316{
317 cpl_recipe *recipe;
318
319 /* Get the recipe out of the plugin */
320 if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
321 recipe = (cpl_recipe *)plugin;
322 else return -1 ;
323
324 cpl_parameterlist_delete(recipe->parameters);
325 return 0 ;
326}
327
328/*----------------------------------------------------------------------------*/
335/*----------------------------------------------------------------------------*/
336static int cr2res_util_extract(
337 cpl_frameset * frameset,
338 const cpl_parameterlist * parlist)
339{
340 const cpl_parameter * param;
341 int oversample, swath_width, extr_height,
342 reduce_det, reduce_order, reduce_trace ;
343 double smooth_slit, smooth_spec, slit_low, slit_up,
344 pclip, gain, error_factor ;
345 cpl_array * slit_frac ;
346 cpl_frameset * rawframes ;
347 cpl_frameset * cur_fset ;
348 const cpl_frame * trace_frame ;
349 const cpl_frame * bpm_frame ;
350 const cpl_frame * slit_func_frame ;
351 const char * sval ;
352 char * out_file;
353 hdrl_image * model_master[CR2RES_NB_DETECTORS] ;
354 cpl_table * slit_func_tab[CR2RES_NB_DETECTORS] ;
355 cpl_table * extract_tab[CR2RES_NB_DETECTORS] ;
356 cpl_propertylist * ext_plist[CR2RES_NB_DETECTORS] ;
357 cpl_table * trace_table ;
358 cpl_table * trace_table_new ;
359 hdrl_image * science_hdrl;
360 cpl_table * slit_func_in ;
361 int det_nr, ext_nr, i ;
362 cr2res_extr_method extr_method;
363
364 /* Needed for sscanf() */
365 setlocale(LC_NUMERIC, "C");
366
367 /* RETRIEVE INPUT PARAMETERS */
368 param = cpl_parameterlist_find_const(parlist,
369 "cr2res.cr2res_util_extract.oversample");
370 oversample = cpl_parameter_get_int(param);
371 param = cpl_parameterlist_find_const(parlist,
372 "cr2res.cr2res_util_extract.pclip");
373 pclip = cpl_parameter_get_double(param);
374 param = cpl_parameterlist_find_const(parlist,
375 "cr2res.cr2res_util_extract.swath_width");
376 swath_width = cpl_parameter_get_int(param);
377 param = cpl_parameterlist_find_const(parlist,
378 "cr2res.cr2res_util_extract.smooth_slit");
379 smooth_slit = cpl_parameter_get_double(param);
380 param = cpl_parameterlist_find_const(parlist,
381 "cr2res.cr2res_util_extract.smooth_spec");
382 smooth_spec = cpl_parameter_get_double(param);
383 param = cpl_parameterlist_find_const(parlist,
384 "cr2res.cr2res_util_extract.detector");
385 reduce_det = cpl_parameter_get_int(param);
386 param = cpl_parameterlist_find_const(parlist,
387 "cr2res.cr2res_util_extract.order");
388 reduce_order = cpl_parameter_get_int(param);
389 param = cpl_parameterlist_find_const(parlist,
390 "cr2res.cr2res_util_extract.trace_nb");
391 reduce_trace = cpl_parameter_get_int(param);
392 param = cpl_parameterlist_find_const(parlist,
393 "cr2res.cr2res_util_extract.height");
394 extr_height = cpl_parameter_get_int(param);
395 param = cpl_parameterlist_find_const(parlist,
396 "cr2res.cr2res_util_extract.method");
397 sval = cpl_parameter_get_string(param);
398 if (!strcmp(sval, "")) extr_method = CR2RES_EXTR_OPT_CURV;
399 else if (!strcmp(sval, "OPT_CURV")) extr_method = CR2RES_EXTR_OPT_CURV;
400 else if (!strcmp(sval, "SUM")) extr_method = CR2RES_EXTR_SUM;
401 else if (!strcmp(sval, "MEDIAN")) extr_method = CR2RES_EXTR_MEDIAN;
402 else if (!strcmp(sval, "TILTSUM")) extr_method = CR2RES_EXTR_TILTSUM;
403 else {
404 cpl_msg_error(__func__, "Invalid Extraction Method specified");
405 cpl_error_set(__func__, CPL_ERROR_ILLEGAL_INPUT) ;
406 return -1;
407 }
408 param = cpl_parameterlist_find_const(parlist,
409 "cr2res.cr2res_util_extract.slit_frac");
410 sval = cpl_parameter_get_string(param) ;
411 if (sscanf(sval, "%lg,%lg", &slit_low, &slit_up) != 2) {
412 cpl_msg_error(__func__, "Invalid Slit Fraction specified");
413 cpl_error_set(__func__, CPL_ERROR_ILLEGAL_INPUT) ;
414 return -1 ;
415 }
416
417 /* TODO, make parameters */
418 int extract_niter = 30;
419 double extract_kappa = 10;
420
421 /* Check Parameters */
422 if (slit_low >= 0.0 && slit_up >= 0.0 && slit_low <= 1.0 && slit_up <= 1.0
423 && slit_up > slit_low) {
424 slit_frac = cpl_array_new(3, CPL_TYPE_DOUBLE) ;
425 cpl_array_set(slit_frac, 0, slit_low) ;
426 cpl_array_set(slit_frac, 1, (slit_low+slit_up)/2.0) ;
427 cpl_array_set(slit_frac, 2, slit_up) ;
428 } else {
429 slit_frac = NULL ;
430 }
431
432 /* Identify the RAW and CALIB frames in the input frameset */
433 if (cr2res_dfs_set_groups(frameset) != CPL_ERROR_NONE) {
434 cpl_msg_error(__func__, "Cannot identify RAW and CALIB frames") ;
435 cpl_error_set(__func__, CPL_ERROR_ILLEGAL_INPUT) ;
436 if (slit_frac != NULL) cpl_array_delete(slit_frac) ;
437 return -1 ;
438 }
439
440 /* Get Calibration frames */
441 trace_frame = cr2res_io_find_TRACE_WAVE(frameset) ;
442 bpm_frame = cr2res_io_find_BPM(frameset) ;
443 slit_func_frame = cr2res_io_find_SLIT_FUNC(frameset) ;
444
445 /* Get the rawframes */
446 rawframes = cr2res_util_extract_find_RAW(frameset) ;
447 if (rawframes==NULL || cpl_frameset_get_size(rawframes) <= 0) {
448 cpl_msg_error(__func__, "Cannot find any RAW file") ;
449 cpl_error_set(__func__, CPL_ERROR_DATA_NOT_FOUND) ;
450 if (rawframes!= NULL) cpl_frameset_delete(rawframes) ;
451 return -1 ;
452 }
453
454 if (trace_frame == NULL) {
455 cpl_msg_error(__func__, "The utility needs a trace wave frame");
456 cpl_error_set(__func__, CPL_ERROR_ILLEGAL_INPUT) ;
457 if (slit_frac != NULL) cpl_array_delete(slit_frac) ;
458 return -1 ;
459 }
460
461 /* Loop on the RAW frames */
462 for (i = 0; i < cpl_frameset_get_size(rawframes); i++) {
463 const cpl_frame *cur_frame;
464 const char *cur_fname;
465 cpl_propertylist *prim_plist;
466 /* Get the Current Frame */
467 cur_frame = cpl_frameset_get_position(rawframes, i) ;
468 cur_fname = cpl_frame_get_filename(cur_frame) ;
469 cpl_msg_info(__func__, "Reduce Frame %s", cur_fname) ;
470 cpl_msg_indent_more() ;
471
472 /* Needed for NDIT for error factor */
473 prim_plist = cpl_propertylist_load(cur_fname, 0);
474
475 /* Loop over the detectors */
476 for (det_nr=1 ; det_nr<=CR2RES_NB_DETECTORS ; det_nr++) {
477
478 /* Initialise */
479 model_master[det_nr-1] = NULL ;
480 slit_func_tab[det_nr-1] = NULL ;
481 extract_tab[det_nr-1] = NULL ;
482 ext_plist[det_nr-1] = NULL ;
483
484 /* Store the extension header for product saving */
485 ext_nr = cr2res_io_get_ext_idx(cur_fname, det_nr, 1) ;
486 if (ext_nr < 0) continue ;
487 ext_plist[det_nr-1] = cpl_propertylist_load(cur_fname, ext_nr) ;
488
489 /* Compute only one detector */
490 if (reduce_det != 0 && det_nr != reduce_det) continue ;
491
492 cpl_msg_info(__func__, "Process detector number %d", det_nr) ;
493 cpl_msg_indent_more() ;
494
495 /* Load the trace table of this detector */
496 cpl_msg_info(__func__, "Load the trace table") ;
497 if ((trace_table = cr2res_io_load_TRACE_WAVE(
498 cpl_frame_get_filename(trace_frame),
499 det_nr)) == NULL) {
500 cpl_msg_error(__func__,
501 "Failed to get trace table - skip detector");
502 cpl_error_reset() ;
503 cpl_msg_indent_less() ;
504 continue ;
505 }
506
507 /* Extract at the specified slit fraction */
508 if (slit_frac != NULL) {
509 if ((trace_table_new = cr2res_trace_new_slit_fraction(
510 trace_table, slit_frac)) == NULL) {
511 cpl_msg_warning(__func__,
512 "Failed to compute the traces for user specified slit fraction") ;
513 cpl_error_reset() ;
514 } else {
515 cpl_table_delete(trace_table) ;
516 trace_table = trace_table_new ;
517 trace_table_new = NULL ;
518 }
519 }
520
521 /* Load the image in which the traces are to extract */
522 cpl_msg_info(__func__, "Load the Image") ;
523 if ((science_hdrl = cr2res_io_load_image(cur_fname, det_nr))==NULL){
524 cpl_table_delete(trace_table) ;
525 cpl_msg_error(__func__,
526 "Failed to load the image - skip detector");
527 cpl_error_reset() ;
528 cpl_msg_indent_less() ;
529 continue ;
530 }
531 /* Load the BPM and assign to hdrl-mask*/
532 if (bpm_frame != NULL) {
533 cpl_msg_info(__func__, "Load and assign the BPM") ;
535 hdrl_image_get_image(science_hdrl),
536 cpl_frame_get_filename(bpm_frame), det_nr, 1)){
537 cpl_table_delete(trace_table) ;
538 hdrl_image_delete(science_hdrl) ;
539 cpl_msg_error(__func__,
540 "Failed to load BPM - skip detector");
541 cpl_error_reset() ;
542 cpl_msg_indent_less() ;
543 continue ;
544 }
545 }
546
547 /* Load the SLIT_FUNC table */
548 if (slit_func_frame != NULL) {
549 slit_func_in = cr2res_io_load_SLIT_FUNC(
550 cpl_frame_get_filename(slit_func_frame),
551 det_nr) ;
552 } else {
553 slit_func_in = NULL ;
554 }
555
556 /* Get the Gain and set error factor */
557 if (det_nr == 1) gain = CR2RES_GAIN_CHIP1 ;
558 else if (det_nr == 2) gain = CR2RES_GAIN_CHIP2 ;
559 else if (det_nr == 3) gain = CR2RES_GAIN_CHIP3 ;
560 else {
561 cpl_msg_error(__func__, "Failed to get the Gain value, det %d",
562 det_nr) ;
563 gain = -1.0 ;
564 }
565 error_factor = gain * cr2res_pfits_get_ndit(prim_plist);
566
567 /* Compute the extraction */
568 cpl_msg_info(__func__, "Spectra Extraction") ;
569 if (cr2res_extract_traces(science_hdrl, trace_table,
570 slit_func_in, NULL, 0, reduce_order, reduce_trace,
571 extr_method, extr_height, swath_width, oversample, pclip,
572 smooth_slit, smooth_spec,
573 extract_niter, extract_kappa, error_factor, 0, 0, 0,
574 &(extract_tab[det_nr-1]), &(slit_func_tab[det_nr-1]),
575 &(model_master[det_nr-1]))==-1) {
576 cpl_table_delete(trace_table) ;
577 hdrl_image_delete(science_hdrl) ;
578 if (slit_func_in != NULL) cpl_table_delete(slit_func_in) ;
579 cpl_msg_error(__func__, "Failed to extract - skip detector");
580 cpl_error_reset() ;
581 cpl_msg_indent_less() ;
582 continue ;
583 }
584 if (slit_func_in != NULL) cpl_table_delete(slit_func_in) ;
585 hdrl_image_delete(science_hdrl) ;
586 cpl_table_delete(trace_table) ;
587 cpl_msg_indent_less() ;
588 }
589 cpl_array_delete(slit_frac) ;
590 cpl_propertylist_delete(prim_plist);
591
592 /* Generate the currently used frameset */
593 /* TODO : add calibrations */
594 cur_fset = cpl_frameset_new() ;
595 cpl_frameset_insert(cur_fset, cpl_frame_duplicate(cur_frame)) ;
596
597 /* Save the Products */
598 out_file = cpl_sprintf("%s_extrModel.fits",
600 cr2res_io_save_SLIT_MODEL(out_file, frameset, cur_fset, parlist,
601 model_master, NULL, ext_plist, CR2RES_UTIL_SLIT_MODEL_PROCATG,
602 RECIPE_STRING) ;
603 cpl_free(out_file);
604 out_file = cpl_sprintf("%s_extrSlitFu.fits",
606 cr2res_io_save_SLIT_FUNC(out_file, frameset, cur_fset, parlist,
607 slit_func_tab, NULL, ext_plist, CR2RES_UTIL_SLIT_FUNC_PROCATG,
608 RECIPE_STRING) ;
609 cpl_free(out_file);
610 out_file = cpl_sprintf("%s_extr1D.fits",
612 cr2res_io_save_EXTRACT_1D(out_file, frameset, cur_fset, parlist,
613 extract_tab, NULL, ext_plist, CR2RES_UTIL_EXTRACT_1D_PROCATG,
614 RECIPE_STRING) ;
615 cpl_free(out_file);
616 cpl_frameset_delete(cur_fset) ;
617
618 /* Free and return */
619 for (det_nr=1 ; det_nr<=CR2RES_NB_DETECTORS ; det_nr++) {
620 if (ext_plist[det_nr-1] != NULL)
621 cpl_propertylist_delete(ext_plist[det_nr-1]) ;
622 if (slit_func_tab[det_nr-1] != NULL)
623 cpl_table_delete(slit_func_tab[det_nr-1]) ;
624 if (extract_tab[det_nr-1] != NULL)
625 cpl_table_delete(extract_tab[det_nr-1]) ;
626 if (model_master[det_nr-1] != NULL)
627 hdrl_image_delete(model_master[det_nr-1]) ;
628 }
629 cpl_msg_indent_less() ;
630 }
631 cpl_frameset_delete(rawframes) ;
632 return (int)cpl_error_get_code();
633}
634
635/*----------------------------------------------------------------------------*/
657/*----------------------------------------------------------------------------*/
658static cpl_frameset * cr2res_util_extract_find_RAW(const cpl_frameset * in)
659{
660 cpl_frameset * out ;
661 char ** tags ;
662 int i, ntags ;
663
664 /* Check entries */
665 if (in == NULL) return NULL ;
666
667 /* Create the tags list */
668 ntags = 16 ;
669 tags = cpl_malloc(ntags * sizeof(char *)) ;
670 tags[0] = cpl_sprintf(CR2RES_FLAT_RAW) ;
671 tags[1] = cpl_sprintf(CR2RES_UTIL_CALIB_PROCATG) ;
672 tags[2] = cpl_sprintf(CR2RES_WAVE_UNE_RAW) ;
673 tags[3] = cpl_sprintf(CR2RES_WAVE_FPET_RAW) ;
674 tags[4] = cpl_sprintf(CR2RES_CAL_NODDING_OTHER_RAW) ;
675 tags[5] = cpl_sprintf(CR2RES_CAL_NODDING_JITTER_RAW) ;
676 tags[6] = cpl_sprintf(CR2RES_OBS_NODDING_OTHER_RAW) ;
677 tags[7] = cpl_sprintf(CR2RES_OBS_NODDING_JITTER_RAW) ;
678 tags[8] = cpl_sprintf(CR2RES_OBS_ASTROMETRY_OTHER_RAW) ;
679 tags[9] = cpl_sprintf(CR2RES_OBS_ASTROMETRY_JITTER_RAW) ;
680 tags[10] = cpl_sprintf(CR2RES_OBS_STARING_OTHER_RAW) ;
681 tags[11] = cpl_sprintf(CR2RES_OBS_STARING_JITTER_RAW) ;
682 tags[12] = cpl_sprintf(CR2RES_OBS_STARING_WAVE_SKY_RAW) ;
683 tags[13] = cpl_sprintf(CR2RES_OBS_POLARIMETRY_OTHER_RAW) ;
684 tags[14] = cpl_sprintf(CR2RES_OBS_2D_OBJECT_RAW) ;
685 tags[15] = cpl_sprintf(CR2RES_OBS_2D_SKY_RAW) ;
686
687 /* Get the frameset */
688 out = cr2res_extract_frameset_several_tags(in, (const char**)tags, ntags) ;
689
690 for (i=0 ; i<ntags ; i++) cpl_free(tags[i]) ;
691 cpl_free(tags) ;
692 return out ;
693}
694
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 pclip, 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