MOONS Pipeline Reference Manual 0.13.2
moons_wavecal.c
1/*
2 * This file is part of the MOONS Pipeline
3 * Copyright (C) 2002-2016 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 Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20#ifdef HAVE_CONFIG_H
21#include <config.h>
22#endif
23
24/*-----------------------------------------------------------------------------
25 Includes
26 -----------------------------------------------------------------------------*/
27
28#include "moo_dfs.h"
29#include "moo_drl.h"
30#include "moo_params.h"
31#include "moo_pfits.h"
32#include "moo_products.h"
33#include "moo_utils.h"
34#include <cpl.h>
35
36#include <string.h>
37
38/*-----------------------------------------------------------------------------
39 Plugin registration
40 -----------------------------------------------------------------------------*/
41
42int cpl_plugin_get_info(cpl_pluginlist *list);
43
44/*-----------------------------------------------------------------------------
45 Private function prototypes
46 -----------------------------------------------------------------------------*/
47
48static int _moons_wavecal_create(cpl_plugin *plugin);
49static int _moons_wavecal_exec(cpl_plugin *plugin);
50static int _moons_wavecal_destroy(cpl_plugin *plugin);
51static int
52_moons_wavecal(cpl_frameset *frameset, const cpl_parameterlist *parlist);
53
54/*-----------------------------------------------------------------------------
55 Static variables
56 -----------------------------------------------------------------------------*/
57
58static const char *const _moons_wavecal_description =
59 "This recipe aims at creating the products necessary to calibrate in"
60 " wavelength the extracted 1D spectra and rebin them. It can be used to"
61 " produce first the guess wavelength map, or to produce the final one,"
62 " from the guess map.\n"
63 "INPUT FRAMES\n"
64 " * Arc 1 file (RAW) with tag ARC : "
65 "arc file\n"
66 " * Arc_off 1 file (RAW) with tag ARC_OFF : "
67 "arc off file\n"
68 " * [OPTIONAL] ReferenceBadPixMask 1 file (QUA) with tag BP_MAP_RP : "
69 "cosmetic bad pixel map\n"
70 " * [OPTIONAL] NonLinearityBadPixMask 1 file (QUA) with tag BP_MAP_NL : "
71 "cosmetic bad pixel map coming from linearity recipe\n"
72 " * MasterBias 1 file (DET) with tag MASTER_BIAS : "
73 "master bias file\n"
74 " * [OPTIONAL] P2pMap 1 file (DET) with tag P2P_MAP : "
75 "pixel to pixel map\n"
76 " * LocTab 1 file (LOC) with tag FF_TRACE : "
77 "the localisation table\n"
78 "FF_EXTSPECTRA : "
79 "the extracted flat field\n"
80 " * F2f 1 file (F2F) with tag F2F_TABLE "
81 ": "
82 "the fibre-to-fibre relative response\n"
83 " * LineCat 1 file (CAT) with tag "
84 "ARC_LINE_LIST : "
85 "the reference line list\n"
86 " * SFormat 1 file (FMT) with tag "
87 "SPECTRAL_FORMAT : "
88 "the spectral format table\n"
89 " * [OPTIONAL] WaveMapGuess 1 file (EXT) with tag "
90 "WAVE_MAP_GUESS : "
91 "the first guess wavelength map\n"
92 "PRODUCTS\n"
93 " * ARC_EXTSPECTRA[_GUESS]_OFFSET[offset]_[insmode].fits (EXT) with tag "
94 "ARC_EXTSPECTRA : "
95 "Arc extracted spectra\n"
96 " * WAVE_MAP[_GUESS]_OFFSET[offset]_[insmode].fits (EXT) with tag "
97 "WAVE_MAP[_GUESS] : "
98 "Wavelength map\n"
99 " * ARC_RBNSPECTRA[_GUESS]_OFFSET[offset]_[insmode].fits (RBN) with tag "
100 "RBN_EXTSPECTRA : "
101 "Arc extracted spectra\n"
102 "\n";
103
104/*-----------------------------------------------------------------------------
105 Function code
106 -----------------------------------------------------------------------------*/
107
108/*----------------------------------------------------------------------------*/
118/*----------------------------------------------------------------------------*/
119
120int
121cpl_plugin_get_info(cpl_pluginlist *list)
122{
123 cpl_recipe *recipe = cpl_calloc(1, sizeof *recipe);
124 cpl_plugin *plugin = &recipe->interface;
125
126 if (cpl_plugin_init(plugin, CPL_PLUGIN_API, MOONS_BINARY_VERSION,
127 CPL_PLUGIN_TYPE_RECIPE, "moons_wavecal",
128 "Produces the wavelength solution",
129 _moons_wavecal_description, "Regis Haigron",
130 PACKAGE_BUGREPORT, moo_get_license(),
131 _moons_wavecal_create, _moons_wavecal_exec,
132 _moons_wavecal_destroy)) {
133 cpl_msg_error(cpl_func, "Plugin initialization failed");
134 (void)cpl_error_set_where(cpl_func);
135 return 1;
136 }
137
138 if (cpl_pluginlist_append(list, plugin)) {
139 cpl_msg_error(cpl_func, "Error adding plugin to list");
140 (void)cpl_error_set_where(cpl_func);
141 return 1;
142 }
143
144 return 0;
145}
146
147/*----------------------------------------------------------------------------*/
155/*----------------------------------------------------------------------------*/
156
157static int
158_moons_wavecal_create(cpl_plugin *plugin)
159{
160 cpl_recipe *recipe;
161
162 /* Do not create the recipe if an error code is already set */
163 if (cpl_error_get_code() != CPL_ERROR_NONE) {
164 cpl_msg_error(cpl_func, "%s():%d: An error is already set: %s",
165 cpl_func, __LINE__, cpl_error_get_where());
166 return (int)cpl_error_get_code();
167 }
168
169 if (plugin == NULL) {
170 cpl_msg_error(cpl_func, "Null plugin");
171 cpl_ensure_code(0, (int)CPL_ERROR_NULL_INPUT);
172 }
173
174 /* Verify plugin type */
175 if (cpl_plugin_get_type(plugin) != CPL_PLUGIN_TYPE_RECIPE) {
176 cpl_msg_error(cpl_func, "Plugin is not a recipe");
177 cpl_ensure_code(0, (int)CPL_ERROR_TYPE_MISMATCH);
178 }
179
180 /* Get the recipe */
181 recipe = (cpl_recipe *)plugin;
182
183 /* Create the parameters list in the cpl_recipe object */
184 recipe->parameters = cpl_parameterlist_new();
185 if (recipe->parameters == NULL) {
186 cpl_msg_error(cpl_func, "Parameter list allocation failed");
187 cpl_ensure_code(0, (int)CPL_ERROR_ILLEGAL_OUTPUT);
188 }
189
190 moo_params *params = moo_params_new("moons", "moons_wavecal");
191
192 /* Fill the parameters list */
193 moo_params_add_keep_temp(params, recipe->parameters);
194 moo_params_add_prepare(params, recipe->parameters);
195 moo_params_add_correct_bias(params, recipe->parameters,
196 MOO_CORRECT_BIAS_METHOD_MASTER);
197 moo_params_add_crh(params, recipe->parameters, MOO_CRH_METHOD_MEDIAN);
198 moo_params_add_extract(params, recipe->parameters);
199 moo_params_add_wavesol(params, recipe->parameters);
200 moo_params_add_rebin(params, recipe->parameters);
201
202 moo_params_delete(params);
203
204 return 0;
205}
206
207/*----------------------------------------------------------------------------*/
213/*----------------------------------------------------------------------------*/
214
215static int
216_moons_wavecal_exec(cpl_plugin *plugin)
217{
218 cpl_recipe *recipe;
219 int recipe_status;
220 cpl_errorstate initial_errorstate = cpl_errorstate_get();
221
222 /* Return immediately if an error code is already set */
223 if (cpl_error_get_code() != CPL_ERROR_NONE) {
224 cpl_msg_error(cpl_func, "%s():%d: An error is already set: %s",
225 cpl_func, __LINE__, cpl_error_get_where());
226 return (int)cpl_error_get_code();
227 }
228
229 if (plugin == NULL) {
230 cpl_msg_error(cpl_func, "Null plugin");
231 cpl_ensure_code(0, (int)CPL_ERROR_NULL_INPUT);
232 }
233
234 /* Verify plugin type */
235 if (cpl_plugin_get_type(plugin) != CPL_PLUGIN_TYPE_RECIPE) {
236 cpl_msg_error(cpl_func, "Plugin is not a recipe");
237 cpl_ensure_code(0, (int)CPL_ERROR_TYPE_MISMATCH);
238 }
239
240 /* Get the recipe */
241 recipe = (cpl_recipe *)plugin;
242
243 /* Verify parameter and frame lists */
244 if (recipe->parameters == NULL) {
245 cpl_msg_error(cpl_func, "Recipe invoked with NULL parameter list");
246 cpl_ensure_code(0, (int)CPL_ERROR_NULL_INPUT);
247 }
248 if (recipe->frames == NULL) {
249 cpl_msg_error(cpl_func, "Recipe invoked with NULL frame set");
250 cpl_ensure_code(0, (int)CPL_ERROR_NULL_INPUT);
251 }
252
253 /* Invoke the recipe */
254 recipe_status = _moons_wavecal(recipe->frames, recipe->parameters);
255
256 /* Ensure DFS-compliance of the products */
257 if (cpl_dfs_update_product_header(recipe->frames)) {
258 if (!recipe_status)
259 recipe_status = (int)cpl_error_get_code();
260 }
261
262 if (!cpl_errorstate_is_equal(initial_errorstate)) {
263 /* Dump the error history since recipe execution start.
264 At this point the recipe cannot recover from the error */
265 cpl_errorstate_dump(initial_errorstate, CPL_FALSE, NULL);
266 }
267
268 return recipe_status;
269}
270
271/*----------------------------------------------------------------------------*/
277/*----------------------------------------------------------------------------*/
278
279static int
280_moons_wavecal_destroy(cpl_plugin *plugin)
281{
282 cpl_recipe *recipe;
283
284 if (plugin == NULL) {
285 cpl_msg_error(cpl_func, "Null plugin");
286 cpl_ensure_code(0, (int)CPL_ERROR_NULL_INPUT);
287 }
288
289 /* Verify plugin type */
290 if (cpl_plugin_get_type(plugin) != CPL_PLUGIN_TYPE_RECIPE) {
291 cpl_msg_error(cpl_func, "Plugin is not a recipe");
292 cpl_ensure_code(0, (int)CPL_ERROR_TYPE_MISMATCH);
293 }
294
295 /* Get the recipe */
296 recipe = (cpl_recipe *)plugin;
297
298 cpl_parameterlist_delete(recipe->parameters);
299
300 return 0;
301}
302
303static cpl_error_code
304_moons_wavecal_check_sof(cpl_frameset *frameset,
305 cpl_frameset **arc_frameset,
306 cpl_frameset **arc_off_frameset,
307 const char **bpmap_rp_name,
308 const char **bpmap_nl_name,
309 const cpl_frame **masterbias,
310 const cpl_frame **masterdark_vis,
311 const cpl_frame **masterdark_nir,
312 const cpl_frame **p2pmap,
313 const cpl_frame **fftrace,
314 const cpl_frame **arc_line_list,
315 const cpl_frame **sformat,
316 const cpl_frame **wmap_guess,
317 const cpl_frame **flat_frame,
318 const cpl_frame **f2f_frame)
319{
320 cpl_ensure_code(moo_dfs_set_groups(frameset) == CPL_ERROR_NONE,
321 cpl_error_get_code());
322 int nraw = 0;
323 int nraw_off = 0;
324
325 *arc_frameset = cpl_frameset_new();
326 *arc_off_frameset = cpl_frameset_new();
327
328 int i;
329 for (i = 0; i < cpl_frameset_get_size(frameset); ++i) {
330 cpl_frame *current_frame = cpl_frameset_get_position(frameset, i);
331 if (!strcmp(cpl_frame_get_tag(current_frame), MOONS_TAG_ARC)) {
332 cpl_frame *new_frame = cpl_frame_duplicate(current_frame);
333 cpl_frameset_insert(*arc_frameset, new_frame);
334 ++nraw;
335 }
336 else if (!strcmp(cpl_frame_get_tag(current_frame), MOONS_TAG_ARC_OFF)) {
337 cpl_frame *new_frame = cpl_frame_duplicate(current_frame);
338 cpl_frameset_insert(*arc_off_frameset, new_frame);
339 ++nraw_off;
340 }
341 else if (!strcmp(cpl_frame_get_tag(current_frame),
342 MOONS_TAG_BP_MAP_RP)) {
343 *bpmap_rp_name = cpl_frame_get_filename(current_frame);
344 }
345 else if (!strcmp(cpl_frame_get_tag(current_frame),
346 MOONS_TAG_BP_MAP_NL)) {
347 *bpmap_nl_name = cpl_frame_get_filename(current_frame);
348 }
349 else if (!strcmp(cpl_frame_get_tag(current_frame),
350 MOONS_TAG_MASTER_BIAS)) {
351 *masterbias = current_frame;
352 }
353 else if (!strcmp(cpl_frame_get_tag(current_frame),
354 MOONS_TAG_MASTER_DARK_VIS)) {
355 *masterdark_vis = current_frame;
356 }
357 else if (!strcmp(cpl_frame_get_tag(current_frame),
358 MOONS_TAG_MASTER_DARK_NIR)) {
359 *masterdark_nir = current_frame;
360 }
361 else if (!strcmp(cpl_frame_get_tag(current_frame), MOONS_TAG_P2P_MAP)) {
362 *p2pmap = current_frame;
363 }
364 else if (!strcmp(cpl_frame_get_tag(current_frame),
365 MOONS_TAG_FF_TRACE)) {
366 *fftrace = current_frame;
367 }
368 else if (!strcmp(cpl_frame_get_tag(current_frame),
369 MOONS_TAG_ARC_LINE_LIST)) {
370 *arc_line_list = current_frame;
371 }
372 else if (!strcmp(cpl_frame_get_tag(current_frame),
373 MOONS_TAG_SPECTRAL_FORMAT)) {
374 *sformat = current_frame;
375 }
376 else if (!strcmp(cpl_frame_get_tag(current_frame),
377 MOONS_TAG_WAVEMAP_GUESS)) {
378 *wmap_guess = current_frame;
379 }
380 else if (!strcmp(cpl_frame_get_tag(current_frame),
381 MOONS_TAG_FF_EXTSPECTRA)) {
382 *flat_frame = current_frame;
383 }
384 else if (!strcmp(cpl_frame_get_tag(current_frame),
385 MOONS_TAG_F2F_TABLE)) {
386 *f2f_frame = current_frame;
387 }
388 }
389
390 if (nraw == 0) {
391 return (int)cpl_error_set_message(cpl_func, CPL_ERROR_DATA_NOT_FOUND,
392 "SOF does not have any file tagged "
393 "with %s",
394 MOONS_TAG_ARC);
395 }
396 if (nraw_off == 0) {
397 return (int)cpl_error_set_message(cpl_func, CPL_ERROR_DATA_NOT_FOUND,
398 "SOF does not have any file tagged "
399 "with %s",
400 MOONS_TAG_ARC_OFF);
401 }
402 if (nraw != nraw_off) {
403 return (int)cpl_error_set_message(
404 cpl_func, CPL_ERROR_DATA_NOT_FOUND,
405 "SOF does not have same number of file with %s (%d) and %s (%d)",
406 MOONS_TAG_ARC, nraw, MOONS_TAG_ARC_OFF, nraw_off);
407 }
408 if (*fftrace == NULL) {
409 return (int)cpl_error_set_message(cpl_func, CPL_ERROR_DATA_NOT_FOUND,
410 "SOF does not have any file tagged "
411 "with %s",
412 MOONS_TAG_FF_TRACE);
413 }
414 if (*arc_line_list == NULL) {
415 return (int)cpl_error_set_message(cpl_func, CPL_ERROR_DATA_NOT_FOUND,
416 "SOF does not have any file tagged "
417 "with %s",
418 MOONS_TAG_ARC_LINE_LIST);
419 }
420
421 if (*sformat == NULL) {
422 return (int)cpl_error_set_message(cpl_func, CPL_ERROR_DATA_NOT_FOUND,
423 "SOF does not have any file tagged "
424 "with %s",
425 MOONS_TAG_SPECTRAL_FORMAT);
426 }
427 return CPL_ERROR_NONE;
428}
429
430static cpl_error_code
431_moons_p2pmap(moo_det *arc, const cpl_frame *p2pmap)
432{
433 cpl_error_code status = CPL_ERROR_NONE;
434 moo_det *p2p = NULL;
435
436 if (p2pmap != NULL) {
437 moo_try_check(p2p = moo_det_create(p2pmap), " ");
438 moo_try_check(moo_apply_p2p(arc, p2p), " ");
439 }
440moo_try_cleanup:
441 moo_det_delete(p2p);
442 return status;
443}
444
445static cpl_error_code
446_moons_apply_flat(moo_ext *ext,
447 const cpl_frame *flat_frame,
448 const cpl_frame *f2f_frame)
449{
450 cpl_error_code status = CPL_ERROR_NONE;
451 moo_ext *flat = NULL;
452 moo_f2f *f2f = NULL;
453
454 if (flat_frame != NULL) {
455 moo_try_check(flat = moo_ext_create(flat_frame), " ");
456 if (f2f_frame != NULL) {
457 f2f = moo_f2f_load(f2f_frame);
458 }
459 moo_try_check(moo_apply_flat(ext, flat, f2f), " ");
460 }
461
462moo_try_cleanup:
463 moo_ext_delete(flat);
464 moo_f2f_delete(f2f);
465 return status;
466}
467
468static int
469_moons_get_offset(const cpl_frame *frame)
470{
471 const char *filename = NULL;
472 cpl_propertylist *header = NULL;
473 int result = 0;
474
475 cpl_ensure(frame != NULL, CPL_ERROR_NULL_INPUT, 0);
476
477 moo_try_check(filename = cpl_frame_get_filename(frame), " ");
478 moo_try_check(header = cpl_propertylist_load(filename, 0), " ");
479 moo_try_check(result = moo_pfits_get_slit_offset(header), " ");
480
481moo_try_cleanup:
482 cpl_propertylist_delete(header);
483 return result;
484}
485
486/*----------------------------------------------------------------------------*/
500/*----------------------------------------------------------------------------*/
501static cpl_frame *
502_moons_prepare(moo_products *products,
503 const cpl_frame *arc_frame,
504 const cpl_frame *arc_off_frame,
505 const char *bpmap_rp_name,
506 const char *bpmap_nl_name,
507 const cpl_frame *masterbias,
508 const cpl_frame *masterdark_vis,
509 const cpl_frame *masterdark_nir,
510 moo_prepare_params *prepare_params,
511 moo_correct_bias_params *correct_bias_params,
512 int i)
513{
514 cpl_frame *result = NULL;
515 cpl_frame *pframe = NULL;
516 char *detname1 = NULL;
517 moo_det *det_arc = NULL;
518 moo_det *det_arc_off = NULL;
519
520 cpl_ensure(arc_frame != NULL, CPL_ERROR_NULL_INPUT, NULL);
521 cpl_errorstate prestate = cpl_errorstate_get();
522
523 moo_try_check(det_arc = moo_prepare(arc_frame, bpmap_rp_name, bpmap_nl_name,
524 masterbias, NULL, prepare_params),
525 " ");
526
527 moo_try_check(det_arc_off =
528 moo_prepare(arc_off_frame, bpmap_rp_name, bpmap_nl_name,
529 masterbias, NULL, prepare_params),
530 " ");
531
532 moo_try_check(moo_correct_bias(det_arc, masterbias, correct_bias_params),
533 " ");
534 moo_try_check(detname1 =
535 cpl_sprintf("%s_%d.fits", MOONS_TAG_ARC_CORRECTBIAS, i),
536 " ");
537
538 moo_try_check(moo_products_add(products, det_arc,
539 CPL_FRAME_LEVEL_INTERMEDIATE,
540 MOONS_TAG_ARC_CORRECTBIAS, detname1,
541 arc_frame),
542 " ");
543 cpl_free(detname1);
544 detname1 = NULL;
545 moo_try_check(detname1 =
546 cpl_sprintf("%s_%d.fits", MOONS_TAG_ARCOFF_PREPARE, i),
547 " ");
548 moo_try_check(moo_products_add(products, det_arc_off,
549 CPL_FRAME_LEVEL_INTERMEDIATE,
550 MOONS_TAG_ARCOFF_PREPARE, detname1,
551 arc_off_frame),
552 " ");
553 cpl_free(detname1);
554 detname1 = NULL;
555 moo_try_check(moo_correct_dark(det_arc, det_arc_off, masterdark_vis,
556 masterdark_nir),
557 " ");
558 moo_try_check(detname1 =
559 cpl_sprintf("%s_%d.fits", MOONS_TAG_ARC_CORRECTDARK, i),
560 " ");
561 moo_try_check(pframe = moo_products_add(products, det_arc,
562 CPL_FRAME_LEVEL_INTERMEDIATE,
563 MOONS_TAG_ARC_CORRECTDARK, detname1,
564 arc_frame),
565 " ");
566
567 moo_try_check(result = cpl_frame_duplicate(pframe), " ");
568
569moo_try_cleanup:
570 if (!cpl_errorstate_is_equal(prestate)) {
571 cpl_frame_delete(result);
572 result = NULL;
573 }
574 moo_det_delete(det_arc);
575 moo_det_delete(det_arc_off);
576 cpl_free(detname1);
577 return result;
578}
579
580/*----------------------------------------------------------------------------*/
594/*----------------------------------------------------------------------------*/
595static cpl_frameset *
596_moons_prepare_set(cpl_frameset *raw_frames,
597 cpl_frameset *raw_off_frames,
598 const char *bpmap_rp_name,
599 const char *bpmap_nl_name,
600 const cpl_frame *masterbias,
601 const cpl_frame *masterdark_vis,
602 const cpl_frame *masterdark_nir,
603 moo_prepare_params *prepare_params,
604 moo_correct_bias_params *params,
605 moo_products *products)
606{
607 cpl_frameset *detframes = NULL;
608
609 cpl_ensure(raw_frames, CPL_ERROR_NULL_INPUT, NULL);
610 cpl_ensure(raw_off_frames, CPL_ERROR_NULL_INPUT, NULL);
611 cpl_ensure(products, CPL_ERROR_NULL_INPUT, NULL);
612
613 cpl_errorstate prestate = cpl_errorstate_get();
614
615 moo_try_check(detframes = cpl_frameset_new(), " ");
616
617 for (int i = 0; i < cpl_frameset_get_size(raw_frames); ++i) {
618 const cpl_frame *current_frame = NULL;
619 const cpl_frame *current_off_frame = NULL;
620 cpl_frame *frame = NULL;
621
622 moo_try_check(current_frame =
623 cpl_frameset_get_position_const(raw_frames, i),
624 " ");
625 moo_try_check(current_off_frame =
626 cpl_frameset_get_position_const(raw_off_frames, i),
627 " ");
628
629 moo_try_check(frame = _moons_prepare(products, current_frame,
630 current_off_frame, bpmap_rp_name,
631 bpmap_nl_name, masterbias,
632 masterdark_vis, masterdark_nir,
633 prepare_params, params, i),
634 " ");
635
636 moo_try_check(cpl_frameset_insert(detframes, frame), " ");
637 }
638moo_try_cleanup:
639 if (!cpl_errorstate_is_equal(prestate)) {
640 cpl_frameset_delete(detframes);
641 detframes = NULL;
642 }
643 return detframes;
644}
645/*----------------------------------------------------------------------------*/
652/*----------------------------------------------------------------------------*/
653
654static int
655_moons_wavecal(cpl_frameset *frameset, const cpl_parameterlist *parlist)
656{
657 /* parameters */
658 moo_prepare_params *prepare_params = NULL;
659 moo_correct_bias_params *correct_bias_params = NULL;
660 moo_crh_params *crh_params = NULL;
661 moo_extract_params *extract_params = NULL;
662 moo_wavesol_params *wavesol_params = NULL;
663 moo_rebin_params *rbn_params = NULL;
664 moo_detlist *arc_det_list = NULL;
665 moo_det *arc_det_med = NULL;
666 moo_spectral_format *sformat = NULL;
667 moo_loc *loc = NULL;
668 moo_ext *ext = NULL;
669 moo_map *wmap_guess = NULL;
670 moo_map *wmap_refit = NULL;
671 moo_map *wmap = NULL;
672 moo_rbn *rbn = NULL;
673 moo_rbn *refit_rbn = NULL;
674 moo_rbn *ppm_rbn = NULL;
675 moo_mode_type mode;
676 int offset = 0;
677 char *ext_filename = NULL;
678 char *ffext_filename = NULL;
679 char *ppm_rbn_filename = NULL;
680 char *ppm_wave_filename = NULL;
681 char *refit_rbn_filename = NULL;
682 char *refit_wave_filename = NULL;
683 char *rbn_filename = NULL;
684 char *wave_filename = NULL;
685
686 /* SOF file */
687 cpl_frameset *arc_frameset = NULL;
688 cpl_frameset *arc_off_frameset = NULL;
689 cpl_frameset *arc_det_frameset = NULL;
690 const char *bpmap_rp_name = NULL;
691 const char *bpmap_nl_name = NULL;
692 const cpl_frame *masterbias = NULL;
693 const cpl_frame *masterdark_vis = NULL;
694 const cpl_frame *masterdark_nir = NULL;
695 const cpl_frame *p2pmap = NULL;
696 const cpl_frame *fftrace = NULL;
697 const cpl_frame *flat_frame = NULL;
698 const cpl_frame *f2f_frame = NULL;
699
700 const cpl_frame *arc_line_list = NULL;
701 const cpl_frame *sformat_frame = NULL;
702 const cpl_frame *wmap_frame = NULL;
703
704 moo_products *products =
705 moo_products_new(frameset, parlist, "moons_wavecal",
706 PACKAGE "/" PACKAGE_VERSION);
707
708 const moo_params *params = moo_products_get_params(products);
709 moo_try_check(prepare_params = moo_params_get_prepare(params, parlist),
710 " ");
711 moo_try_check(correct_bias_params =
712 moo_params_get_correct_bias(params, parlist),
713 " ");
714 moo_try_check(crh_params = moo_params_get_crh(params, parlist), " ");
715 moo_try_check(extract_params = moo_params_get_extract(params, parlist),
716 " ");
717 if (strcmp(extract_params->method, MOO_EXTRACT_METHOD_SUM) != 0) {
718 cpl_msg_info(__func__,
719 "Extract method %s not supported : use %s instead",
720 extract_params->method, MOO_EXTRACT_METHOD_SUM);
721 extract_params->method = MOO_EXTRACT_METHOD_SUM;
722 }
723 moo_try_check(wavesol_params = moo_params_get_wavesol(params, parlist),
724 " ");
725 moo_try_check(rbn_params = moo_params_get_rebin(params, parlist), " ");
726
727 moo_try_check(_moons_wavecal_check_sof(frameset, &arc_frameset,
728 &arc_off_frameset, &bpmap_rp_name,
729 &bpmap_nl_name, &masterbias,
730 &masterdark_vis, &masterdark_nir,
731 &p2pmap, &fftrace, &arc_line_list,
732 &sformat_frame, &wmap_frame,
733 &flat_frame, &f2f_frame),
734 " ");
735
736 cpl_frame *arc_frame = cpl_frameset_get_position(arc_frameset, 0);
737
738 moo_try_check(mode = moo_mode_get(arc_frame), " ");
739 moo_try_check(offset = _moons_get_offset(arc_frame), " ");
740
741 cpl_msg_info(__func__, "Use mode %s", moo_mode_get_name(mode));
742
743 moo_try_check(arc_det_frameset =
744 _moons_prepare_set(arc_frameset, arc_off_frameset,
745 bpmap_rp_name, bpmap_nl_name,
746 masterbias, masterdark_vis,
747 masterdark_nir, prepare_params,
748 correct_bias_params, products),
749 " ");
750
751 moo_try_check(arc_det_list = moo_detlist_create(arc_det_frameset), " ");
752
753 moo_try_check(arc_det_med = moo_remove_CRH(arc_det_list, NULL, crh_params),
754 " ");
755
756 const char *arc_filename = MOONS_TAG_ARC_REMOVECRH ".fits";
757 moo_try_check(moo_products_add(products, arc_det_med,
758 CPL_FRAME_LEVEL_INTERMEDIATE,
759 MOONS_TAG_ARC_REMOVECRH, arc_filename,
760 arc_frame),
761 " ");
762
763 moo_try_check(_moons_p2pmap(arc_det_med, p2pmap), " ");
764
765 moo_try_check(loc = moo_loc_load(fftrace), " ");
766 moo_try_check(sformat = moo_spectral_format_load(sformat_frame, mode), " ");
767
768
769 const char *wtag = MOONS_TAG_WAVEMAP_GUESS;
770 const char *arc_line_list_name = cpl_frame_get_filename(arc_line_list);
771
772 if (wmap_frame != NULL) {
773 ext_filename =
774 cpl_sprintf("%s_OFFSET%d_%s.fits", MOONS_TAG_ARC_EXTSPECTRA, offset,
775 moo_mode_get_name(mode));
776
777 moo_try_check(ext = moo_extract(arc_det_med, loc, NULL, extract_params,
778 ext_filename),
779 " ");
780 moo_try_check(moo_products_add_ext(products, ext, CPL_FRAME_LEVEL_FINAL,
781 MOONS_TAG_ARC_EXTSPECTRA,
782 ext_filename, arc_frame),
783 " ");
784 moo_try_check(_moons_apply_flat(ext, flat_frame, f2f_frame), " ");
785 if (flat_frame != NULL) {
786 ffext_filename =
787 cpl_sprintf("%s_OFFSET%d_%s.fits", MOONS_TAG_ARC_FFEXTSPECTRA,
788 offset, moo_mode_get_name(mode));
789
790 moo_try_check(moo_products_add_ext(products, ext,
791 CPL_FRAME_LEVEL_FINAL,
792 MOONS_TAG_ARC_FFEXTSPECTRA,
793 ffext_filename, arc_frame),
794 " ");
795 }
796 moo_try_check(wmap_guess = moo_map_load(wmap_frame), " ");
797 wtag = MOONS_TAG_WAVEMAP;
798 rbn_filename =
799 cpl_sprintf("%s_OFFSET%d_%s.fits", MOONS_TAG_ARC_RBNSPECTRA, offset,
800 moo_mode_get_name(mode));
801 wave_filename = cpl_sprintf("%s_OFFSET%d_%s.fits", wtag, offset,
802 moo_mode_get_name(mode));
803 moo_try_check(wmap = moo_wavesol(ext, arc_line_list_name, sformat, loc,
804 wmap_guess, wavesol_params),
805 " ");
806
807 moo_try_check(rbn = moo_rebin(ext, wmap, sformat, rbn_params,
808 rbn_filename),
809 " ");
810
811 moo_try_check(moo_products_add_rbn(products, rbn, CPL_FRAME_LEVEL_FINAL,
812 MOONS_TAG_ARC_RBNSPECTRA,
813 rbn_filename, arc_frame),
814 " ");
815
816 moo_try_check(moo_products_add_map(products, wmap,
817 CPL_FRAME_LEVEL_FINAL, wtag,
818 wave_filename, arc_frame, rbn),
819 "can't create wave map product");
820 }
821 else {
822 ext_filename =
823 cpl_sprintf("%s_OFFSET%d_%s.fits", MOONS_TAG_ARC_EXTSPECTRA_GUESS,
824 offset, moo_mode_get_name(mode));
825
826 moo_try_check(ext = moo_extract(arc_det_med, loc, NULL, extract_params,
827 ext_filename),
828 " ");
829 moo_try_check(moo_products_add_ext(products, ext, CPL_FRAME_LEVEL_FINAL,
830 MOONS_TAG_ARC_EXTSPECTRA_GUESS,
831 ext_filename, arc_frame),
832 " ");
833 moo_try_check(_moons_apply_flat(ext, flat_frame, f2f_frame), " ");
834 if (flat_frame != NULL) {
835 ffext_filename =
836 cpl_sprintf("%s_OFFSET%d_%s.fits", MOONS_TAG_ARC_FFEXTSPECTRA,
837 offset, moo_mode_get_name(mode));
838
839 moo_try_check(moo_products_add_ext(products, ext,
840 CPL_FRAME_LEVEL_FINAL,
841 MOONS_TAG_ARC_FFEXTSPECTRA,
842 ffext_filename, arc_frame),
843 " ");
844 }
845 ppm_wave_filename =
846 cpl_sprintf("%s_OFFSET%d_%s.fits", MOONS_TAG_WAVEMAP_PPM, offset,
847 moo_mode_get_name(mode));
848 ppm_rbn_filename =
849 cpl_sprintf("%s_OFFSET%d_%s.fits", MOONS_TAG_ARC_RBNSPECTRA_PPM,
850 offset, moo_mode_get_name(mode));
851 refit_wave_filename =
852 cpl_sprintf("%s_OFFSET%d_%s.fits", MOONS_TAG_WAVEMAP_REFIT, offset,
853 moo_mode_get_name(mode));
854 refit_rbn_filename =
855 cpl_sprintf("%s_OFFSET%d_%s.fits", MOONS_TAG_ARC_RBNSPECTRA_REFIT,
856 offset, moo_mode_get_name(mode));
857 wave_filename =
858 cpl_sprintf("%s_OFFSET%d_%s.fits", MOONS_TAG_WAVEMAP_GUESS, offset,
859 moo_mode_get_name(mode));
860 rbn_filename =
861 cpl_sprintf("%s_OFFSET%d_%s.fits", MOONS_TAG_ARC_RBNSPECTRA_GUESS,
862 offset, moo_mode_get_name(mode));
863
864 moo_try_check(wmap_guess = moo_wavesol(ext, arc_line_list_name, sformat,
865 loc, NULL, wavesol_params),
866 " ");
867 moo_try_check(wmap_refit = moo_wavesol(ext, arc_line_list_name, sformat,
868 loc, wmap_guess, wavesol_params),
869 " ");
870
871 if (wmap_refit != NULL) {
872 moo_try_check(ppm_rbn = moo_rebin(ext, wmap_guess, sformat,
873 rbn_params, ppm_rbn_filename),
874 " ");
875 moo_try_check(refit_rbn = moo_rebin(ext, wmap_refit, sformat,
876 rbn_params, refit_rbn_filename),
877 " ");
878
879 moo_try_check(moo_products_add_rbn(products, ppm_rbn,
880 CPL_FRAME_LEVEL_INTERMEDIATE,
881 MOONS_TAG_ARC_RBNSPECTRA_PPM,
882 ppm_rbn_filename, arc_frame),
883 " ");
884
885 moo_try_check(moo_products_add_map(products, wmap_guess,
886 CPL_FRAME_LEVEL_INTERMEDIATE,
887 MOONS_TAG_WAVEMAP_PPM,
888 ppm_wave_filename, arc_frame,
889 ppm_rbn),
890 "can't create wave map product");
891
892 moo_try_check(moo_products_add_rbn(products, refit_rbn,
893 CPL_FRAME_LEVEL_INTERMEDIATE,
894 MOONS_TAG_ARC_RBNSPECTRA_REFIT,
895 refit_rbn_filename, arc_frame),
896 " ");
897
898 moo_try_check(moo_products_add_map(products, wmap_refit,
899 CPL_FRAME_LEVEL_INTERMEDIATE,
900 MOONS_TAG_WAVEMAP_REFIT,
901 refit_wave_filename, arc_frame,
902 refit_rbn),
903 "can't create wave map product");
904
905 for (int i = 0; i < 6; i++) {
906 wavesol_params->linefit_winhsize[i] =
907 wavesol_params->linedetect_winhsize[i];
908 }
909 wavesol_params->isrefit = 2;
910
911 moo_try_check(wmap = moo_wavesol(ext, arc_line_list_name, sformat,
912 loc, wmap_refit, wavesol_params),
913 " ");
914
915 moo_try_check(rbn = moo_rebin(ext, wmap, sformat, rbn_params,
916 rbn_filename),
917 " ");
918 moo_try_check(moo_products_add_rbn(products, rbn,
919 CPL_FRAME_LEVEL_FINAL,
920 MOONS_TAG_ARC_RBNSPECTRA_GUESS,
921 rbn_filename, arc_frame),
922 " ");
923
924 moo_try_check(moo_products_add_map(products, wmap,
925 CPL_FRAME_LEVEL_FINAL, wtag,
926 wave_filename, arc_frame, rbn),
927 "can't create wave map product");
928 }
929 else {
930 moo_try_check(rbn = moo_rebin(ext, wmap_guess, sformat, rbn_params,
931 rbn_filename),
932 " ");
933
934 moo_try_check(moo_products_add_map(products, wmap_guess,
935 CPL_FRAME_LEVEL_FINAL, wtag,
936 wave_filename, arc_frame, rbn),
937 "can't create wave map product");
938
939 moo_try_check(moo_products_add_rbn(products, rbn,
940 CPL_FRAME_LEVEL_FINAL,
941 MOONS_TAG_ARC_RBNSPECTRA,
942 rbn_filename, arc_frame),
943 " ");
944 }
945 }
946
947moo_try_cleanup:
948 cpl_free(ext_filename);
949 cpl_free(ffext_filename);
950 cpl_free(rbn_filename);
951 cpl_free(wave_filename);
952 cpl_free(refit_rbn_filename);
953 cpl_free(refit_wave_filename);
954 cpl_free(ppm_rbn_filename);
955 cpl_free(ppm_wave_filename);
956 moo_rbn_delete(rbn);
957 moo_rbn_delete(refit_rbn);
958 moo_rbn_delete(ppm_rbn);
959 moo_map_delete(wmap);
960 moo_map_delete(wmap_guess);
961 moo_map_delete(wmap_refit);
962 moo_ext_delete(ext);
963 moo_loc_delete(loc);
965 moo_det_delete(arc_det_med);
966 moo_detlist_delete(arc_det_list);
967 cpl_frameset_delete(arc_frameset);
968 cpl_frameset_delete(arc_off_frameset);
969
970 cpl_frameset_delete(arc_det_frameset);
971 moo_extract_params_delete(extract_params);
972 moo_wavesol_params_delete(wavesol_params);
973 moo_rebin_params_delete(rbn_params);
974 moo_crh_params_delete(crh_params);
975 moo_correct_bias_params_delete(correct_bias_params);
976 moo_prepare_params_delete(prepare_params);
977 moo_products_delete(products);
978 return (int)cpl_error_get_code();
979}
moo_det * moo_det_create(const cpl_frame *frame)
Create a new moo_det from the given DET frame.
Definition: moo_det.c:91
void moo_det_delete(moo_det *self)
Delete a moo_det.
Definition: moo_det.c:472
moo_mode_type moo_mode_get(const cpl_frame *frame)
Get the name of a mode from a frame.
Definition: moo_detector.c:224
const char * moo_mode_get_name(moo_mode_type type)
Get the name of a mode.
Definition: moo_detector.c:204
moo_detlist * moo_detlist_create(cpl_frameset *frameset)
Create a new moo_detlist from the given DET frameset.
Definition: moo_detlist.c:71
void moo_detlist_delete(moo_detlist *self)
Free all memory used by a moo_detlist object including the DET.
Definition: moo_detlist.c:559
moo_ext * moo_ext_create(const cpl_frame *frame)
Create a new empty EXT filename.
Definition: moo_ext.c:86
void moo_ext_delete(moo_ext *self)
Delete a moo_ext.
Definition: moo_ext.c:370
void moo_f2f_delete(moo_f2f *self)
Delete a moo_f2f.
Definition: moo_f2f.c:413
moo_f2f * moo_f2f_load(const cpl_frame *f2f_frame)
Load a F2F table from a fits file.
Definition: moo_f2f.c:139
moo_loc * moo_loc_load(const cpl_frame *locframe)
Load a LOC frame and create a moo_loc.
Definition: moo_loc.c:109
void moo_loc_delete(moo_loc *self)
Delete a moo_loc.
Definition: moo_loc.c:332
void moo_map_delete(moo_map *self)
Delete a moo_map.
Definition: moo_map.c:236
cpl_error_code moo_params_add_extract(moo_params *self, cpl_parameterlist *list)
Add default parameters for extraction.
Definition: moo_params.c:902
moo_prepare_params * moo_params_get_prepare(const moo_params *self, const cpl_parameterlist *list)
Get remove prepare parameters from moons parameters list.
Definition: moo_params.c:1073
moo_wavesol_params * moo_params_get_wavesol(const moo_params *self, const cpl_parameterlist *list)
Get wavesol parameters from moons parameters list.
Definition: moo_params.c:2714
moo_correct_bias_params * moo_params_get_correct_bias(const moo_params *self, const cpl_parameterlist *list)
Get correct_bias parameters from moons parameters list.
Definition: moo_params.c:1167
cpl_error_code moo_params_add_crh(moo_params *self, cpl_parameterlist *list, const char *method)
Add default parameters for remove crh.
Definition: moo_params.c:694
moo_extract_params * moo_params_get_extract(const moo_params *self, const cpl_parameterlist *list)
Get extraction parameters from moons parameters list.
Definition: moo_params.c:1197
void moo_params_delete(moo_params *self)
Delete a moo_params.
Definition: moo_params.c:85
moo_crh_params * moo_params_get_crh(const moo_params *self, const cpl_parameterlist *list)
Get remove crh parameters from moons parameters list.
Definition: moo_params.c:1099
cpl_error_code moo_params_add_correct_bias(moo_params *self, cpl_parameterlist *list, const char *method)
Add default parameters for correct_bias.
Definition: moo_params.c:860
cpl_error_code moo_params_add_keep_temp(moo_params *self, cpl_parameterlist *list)
Add default parameters for keep-temp.
Definition: moo_params.c:932
cpl_error_code moo_params_add_wavesol(moo_params *self, cpl_parameterlist *list)
Add default parameters for moo_wavesol.
Definition: moo_params.c:2000
cpl_error_code moo_params_add_prepare(moo_params *self, cpl_parameterlist *list)
Add default parameters for prepare.
Definition: moo_params.c:667
moo_rebin_params * moo_params_get_rebin(const moo_params *self, const cpl_parameterlist *list)
Get rebin parameters from moons parameters list.
Definition: moo_params.c:2866
moo_params * moo_params_new(const char *pid, const char *recipe_id)
Create a new moo_params.
Definition: moo_params.c:62
cpl_error_code moo_params_add_rebin(moo_params *self, cpl_parameterlist *list)
Add default parameters for moo_rebin.
Definition: moo_params.c:2152
void moo_rbn_delete(moo_rbn *self)
Delete a moo_rbn.
Definition: moo_rbn.c:120
void moo_spectral_format_delete(moo_spectral_format *self)
Delete a moo_spectral_format.
moo_spectral_format * moo_spectral_format_load(const cpl_frame *frame, moo_mode_type m)
Load a SPECTRAL FORMAT frame and create a moo_spectral_format.
cpl_error_code moo_dfs_set_groups(cpl_frameset *set)
Set the group as RAW or CALIB in a frameset.
Definition: moo_dfs.c:206
moo_det * moo_remove_CRH(moo_detlist *detlist, moo_masklist *cosmiclist, moo_crh_params *params)
Remove CRH in single frames or in a combination of multiple frames.
moo_ext * moo_extract(moo_det *det, moo_loc *loc, moo_psf *master_flat, moo_extract_params *params, const char *filename)
extract the 1D spectrum of fibres
Definition: moo_extract.c:523
moo_map * moo_wavesol(moo_ext *arc_ext, const char *lineCatname, moo_spectral_format *sformat, moo_loc *loc, moo_map *wmap, moo_wavesol_params *params)
Computes the wavelength solution from 1D extracted arc spectra.
Definition: moo_wavesol.c:2728
cpl_error_code moo_apply_p2p(moo_det *flat, moo_det *p2pmap)
Divide DET by the pixel-to-pixel variation map.
moo_rbn * moo_rebin(moo_ext *ext, moo_map *wmap, moo_spectral_format *sformat, moo_rebin_params *params, const char *filename)
Rebin an EXT spectra in RBN format.
Definition: moo_rebin.c:783
moo_det * moo_prepare(const cpl_frame *rawframe, const char *const badpixmask_rp, const char *const badpixmask_nl, const cpl_frame *masterbias, const cpl_frame *cube_frame, moo_prepare_params *params)
This function transforms RAW frames in DET frames attaching the default bad pixel map and an error im...
Definition: moo_prepare.c:324
cpl_error_code moo_apply_flat(moo_ext *ext, moo_ext *flat, moo_f2f *f2f)
Divide spectra by 1D flat-field.
cpl_error_code moo_correct_bias(moo_det *det, const cpl_frame *masterbias_frame, moo_correct_bias_params *params)
Subtracts the Master Bias frame from a DET frame.
Definition: moo_drl.c:86
cpl_error_code moo_correct_dark(moo_det *det, moo_det *detoff, const cpl_frame *masterDarkVis, const cpl_frame *masterDarkNir)
Subtracts the master dark frame from a frame after scaling for exposure time (RI)....
Definition: moo_drl.c:250
int moo_pfits_get_slit_offset(const cpl_propertylist *plist)
find out the INS SLIT OFFSET value
Definition: moo_pfits.c:1110
moo_products * moo_products_new(cpl_frameset *framelist, const cpl_parameterlist *parlist, const char *recid, const char *pipeline_id)
create a moo_product object for a recipe
Definition: moo_products.c:53
cpl_frame * moo_products_add_rbn(moo_products *self, moo_rbn *rbn, cpl_frame_level level, const char *tag, const char *filename, const cpl_frame *inherit_frame)
create a product from a RBN object
Definition: moo_products.c:376
cpl_frame * moo_products_add_ext(moo_products *self, moo_ext *ext, cpl_frame_level level, const char *tag, const char *filename, const cpl_frame *inherit_frame)
create a product from a EXT object
Definition: moo_products.c:333
cpl_frame * moo_products_add_map(moo_products *self, moo_map *map, cpl_frame_level level, const char *tag, const char *filename, const cpl_frame *inherit_frame, moo_rbn *rbn)
create a product from a EXT object
Definition: moo_products.c:554
cpl_frame * moo_products_add(moo_products *self, moo_det *det, cpl_frame_level level, const char *tag, const char *filename, const cpl_frame *inherit_frame)
create a product from a DET object
Definition: moo_products.c:203
const moo_params * moo_products_get_params(const moo_products *self)
get the moo_params object
Definition: moo_products.c:87
const char * moo_get_license(void)
Get the pipeline copyright and license.
Definition: moo_utils.c:81