X-shooter Pipeline Reference Manual 3.8.15
xsh_2dmap.c
Go to the documentation of this file.
1/* *
2 * This file is part of the ESO X-shooter Pipeline *
3 * Copyright (C) 2006 European Southern Observatory *
4 * *
5 * This library 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, 51 Franklin St, Fifth Floor, Boston, MA 02111-1307 USA *
18 * */
19
20/*
21 * $Author: amodigli $
22 * $Date: 2013-03-18 11:40:23 $
23 * $Revision: 1.154 $
24 * $Name: not supported by cvs2svn $
25 */
26
27#ifdef HAVE_CONFIG_H
28#include <config.h>
29#endif
30
31/*--------------------------------------------------------------------------*/
42/*--------------------------------------------------------------------------*/
45/*---------------------------------------------------------------------------
46 Includes
47 ---------------------------------------------------------------------------*/
48
49
50/* DRL steps */
51
52/* Error handling */
53#include <xsh_error.h>
54/* Utility fonctions */
55#include <xsh_utils.h>
56#include <xsh_utils_table.h>
57#include <xsh_pfits.h>
58#include <xsh_msg.h>
59/* DFS functions */
60#include <xsh_dfs.h>
61/* DRL functions */
62#include <xsh_data_resid_tab.h>
63
64#include <xsh_data_order.h>
65#include <xsh_drl.h>
66#include <xsh_drl_check.h>
67#include <xsh_model_kernel.h>
69
70/* Library */
71#include <cpl.h>
72
73/*---------------------------------------------------------------------------
74 Defines
75 ---------------------------------------------------------------------------*/
76
77#define RECIPE_ID "xsh_2dmap"
78#define RECIPE_AUTHOR "R. Haigron, P. Bristow, D. Bramich, A. Modigliani"
79#define RECIPE_CONTACT "amodigli@eso.org"
80
81/*---------------------------------------------------------------------------
82 Functions prototypes
83 ---------------------------------------------------------------------------*/
84
85/*
86 * Plugin initalization, execute and cleanup handlers
87 */
88
89static int xsh_2dmap_create(cpl_plugin *);
90static int xsh_2dmap_exec(cpl_plugin *);
91static int xsh_2dmap_destroy(cpl_plugin *);
92
93/* The actual executor function */
94static void xsh_2dmap(cpl_parameterlist *, cpl_frameset *);
95
96/*---------------------------------------------------------------------------
97 Static variables
98 ---------------------------------------------------------------------------*/
100"Creates a wavelength and spatial resampling solution, a clean arc line list";
101
103"This recipe creates a wavelength and spatial resampling solution, a clean arc line list.\n\
104 a residual map and a wave map.\n\
105Input Frames:\n\
106 Raw Frame (Tag = WAVE_arm)\n\
107 Arc Line List (Tag = ARC_LINE_LIST_arm)\n\
108 Order Table (Tag = ORDER_TAB_EDGES_arm)\n\
109 Master Bias (Tag = MASTER_BIAS_arm)\n\
110 [poly mode] Wave Solution (Tag = WAVE_TAB_GUESS_arm)\n\
111 [poly mode] Theoretical Map (Tag = THEO_TAB_MULT_arm)\n\
112 [physical model mode] model cfg table (Tag = XSH_MOD_CFG_OPT_FMT_arm)\n\
113 [OPTIONAL] Master Dark (Tag = MASTER_DARK_arm)\n\
114 [OPTIONAL] Non-linear Bad Pixel Map (Tag = BP_MAP_NL_arm)\n\
115 [OPTIONAL] Reference Bad Pixel Map (Tag = BP_MAP_RP_arm)\n\
116 [OPTIONAL] Reference list to monitor line intensity (Tag = ARC_LINE_LIST_INTMON_arm)\n \
117Prepare the frames.\n\
118For UVB,VIS:\n\
119 Subtract Master Bias.\n\
120 Subtract Master Dark.\n\
121For NIR:\n\
122 Subtract ON OFF \n\
123 Compute Wavelength Solution, clean arc line list, residual table, wave map\n\
124 Products:\n\
125 Wavelength table solution, PRO.CATG = WAVE_TAB_2D_arm [poly mode]\n\
126 A Residual tab, PRO.CATG=WAVE_RESID_TAB_SLIT_arm\n\
127 A Wavelelength image map, PRO.CATG=WAVE_MAP_arm. [if model-wavemap-compute=TRUE]\n\
128 A Slit image map, PRO.CATG=SLIT_MAP_NIR [if model-wavemap-compute=TRUE]\n\
129 A Dispersion solution table, PRO.CATG=DISP_TAB_NIR\n\
130 An Arc frame in pre format bias subtracted, \n\
131 PRO.CATG=WAVE_ON_arm\n\
132 The optimized model cfg frame, PRO.CATG=XSH_MOD_CFG_OPT_2D_arm [if physical model mode].\n";
133
134
135
136/*---------------------------------------------------------------------------
137 Functions code
138 ---------------------------------------------------------------------------*/
139
140/*--------------------------------------------------------------------------*/
149/*--------------------------------------------------------------------------*/
150
151int cpl_plugin_get_info(cpl_pluginlist *list) {
152 cpl_recipe *recipe = NULL;
153 cpl_plugin *plugin = NULL;
154
155 recipe = cpl_calloc(1, sizeof(*recipe));
156 if ( recipe == NULL ){
157 return -1;
158 }
159
160 plugin = &recipe->interface ;
161
162 cpl_plugin_init(plugin,
163 CPL_PLUGIN_API, /* Plugin API */
164 XSH_BINARY_VERSION, /* Plugin version */
165 CPL_PLUGIN_TYPE_RECIPE, /* Plugin type */
166 RECIPE_ID, /* Plugin name */
167 xsh_2dmap_description_short, /* Short help */
168 xsh_2dmap_description, /* Detailed help */
169 RECIPE_AUTHOR, /* Author name */
170 RECIPE_CONTACT, /* Contact address */
171 xsh_get_license(), /* Copyright */
175
176 cpl_pluginlist_append(list, plugin);
177
178 return (cpl_error_get_code() != CPL_ERROR_NONE);
179 }
180
181/*--------------------------------------------------------------------------*/
191/*--------------------------------------------------------------------------*/
192
193static int xsh_2dmap_create(cpl_plugin *plugin){
194 cpl_recipe *recipe = NULL;
195 xsh_clipping_param detarc_clip_param = {2.0, 0, 0.7, 0, 0.3};
196 xsh_detect_arclines_param detarc_param =
197 {6, 3, 0, 5, 4, 1, 5, 5.0,
198 XSH_GAUSSIAN_METHOD, FALSE};
199 xsh_dispersol_param dispsol_param = { 4, 5 } ;
200 char paramname[256];
201 cpl_parameter* p=NULL;
202 int ival=DECODE_BP_FLAG_DEF;
203
204 /* Reset library state */
205 xsh_init();
206
207 /* Check input */
208 assure( plugin != NULL, CPL_ERROR_NULL_INPUT, "Null plugin");
209
210 /* Get the recipe out of the plugin */
211 assure( cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE,
212 CPL_ERROR_TYPE_MISMATCH,
213 "Plugin is not a recipe");
214
215 recipe = (cpl_recipe *)plugin;
216
217 /* Create the parameter list in the cpl_recipe object */
218 recipe->parameters = cpl_parameterlist_new();
219 assure( recipe->parameters != NULL,
220 CPL_ERROR_ILLEGAL_OUTPUT,
221 "Memory allocation failed!");
222
223 /* Set generic parameters (common to all recipes) */
224 check( xsh_parameters_generic( RECIPE_ID, recipe->parameters ) ) ;
225 xsh_parameters_decode_bp(RECIPE_ID,recipe->parameters,ival);
226 check( xsh_parameters_pre_overscan( RECIPE_ID, recipe->parameters ) ) ;
227
228 /* General 2dmap parameters */
229
230 /* detect arclines params */
232 detarc_param));
234 recipe->parameters, detarc_clip_param));
236 recipe->parameters,
237 dispsol_param ) ) ;
238
239 /* Annealing parameter */
240 check( xsh_parameters_new_int( recipe->parameters, RECIPE_ID,
241 "model-maxit",500,
242 "Number/10 of annealing iterations "
243 "(physical model mode)."));
244
245
246 check( xsh_parameters_new_double( recipe->parameters, RECIPE_ID,
247 "model-anneal-factor",1.0,
248 "Multiplier applied to the automatic "
249 "parameter ranges (i.e. when scenario!=0). "
250 "For routine operations should be 1.0. "
251 "(physical model mode)."));
252
253
254 sprintf(paramname,"xsh.%s.%s",RECIPE_ID,"model-scenario");
255
256 /*Scenario descriptions for development use removed so as not to confuse users*/
257 check(p=cpl_parameter_new_enum(paramname,CPL_TYPE_INT,
258 "selects preset flag and range combinations "
259 "appropriate to common scenarios: \n"
260/* "-1- Only the position across the slit and"
261 " camera focal length are open\n" */
262 " 0 - No scenario, input cfg flags and limits"
263 "used.\n"
264 " 1 - scenario appropriate for the startup"
265 "recipe (large ranges for parameters "
266 "affecting single ph exposures, dist "
267 "coeff fixed)\n"
268 " 2 - Like 1, but includes parameters "
269 "affecting all ph positions\n"
270 " 3 - Scenario for use in fine tuning cfg "
271 "to match routine single pinhole exposures. "
272 "All parameters affecting 1ph exposures "
273 "except dist coeffs are included and "
274 "parameter ranges are small. (For use by "
275 "predict in 1ph case).\n"
276 " 4 - Like 3 but includes parameters "
277 "affecting all ph positions (Standard for "
278 "use by predict in 9ph case and 2dmap).\n"
279/* " 5 - Like 4 but includes also dist coeffs\n"
280 " 6 - Just dist coeffs (and chipx, chipy)\n" */
281 ,RECIPE_ID,4,9,-1,0,1,2,3,4,5,6,8));
282
283 check(cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI,
284 "model-scenario"));
285 check(cpl_parameterlist_append(recipe->parameters,p));
286
287 cleanup:
288 if ( cpl_error_get_code() != CPL_ERROR_NONE ){
289 xsh_error_dump(CPL_MSG_ERROR);
290 return 1;
291 }
292 else {
293 return 0;
294 }
295}
296
297/*--------------------------------------------------------------------------*/
303/*--------------------------------------------------------------------------*/
304
305static int xsh_2dmap_exec(cpl_plugin *plugin) {
306 cpl_recipe *recipe = NULL;
307
308 /* Check parameter */
309 assure( plugin != NULL, CPL_ERROR_NULL_INPUT, "Null plugin" );
310
311 /* Get the recipe out of the plugin */
312 assure( cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE,
313 CPL_ERROR_TYPE_MISMATCH, "Plugin is not a recipe");
314
315 recipe = (cpl_recipe *)plugin;
316 /* Check recipe */
317 xsh_2dmap(recipe->parameters, recipe->frames);
318
319 cleanup:
320 if ( cpl_error_get_code() != CPL_ERROR_NONE ) {
321 xsh_error_dump(CPL_MSG_ERROR);
322 cpl_error_reset();
323 return 1;
324 }
325 else {
326 return 0;
327 }
328}
329
330/*--------------------------------------------------------------------------*/
336/*--------------------------------------------------------------------------*/
337static int xsh_2dmap_destroy(cpl_plugin *plugin)
338{
339 cpl_recipe *recipe = NULL;
340
341 /* Check parameter */
342 assure( plugin != NULL, CPL_ERROR_NULL_INPUT, "Null plugin" );
343
344 /* Get the recipe out of the plugin */
345 assure( cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE,
346 CPL_ERROR_TYPE_MISMATCH, "Plugin is not a recipe");
347
348 recipe = (cpl_recipe *)plugin;
349
350 xsh_free_parameterlist(&recipe->parameters);
351
352 cleanup:
353 if (cpl_error_get_code() != CPL_ERROR_NONE)
354 {
355 return 1;
356 }
357 else
358 {
359 return 0;
360 }
361}
362
363
364
365static cpl_error_code
366xsh_verify_2dmap_poly_input(cpl_frame* order_tab_edges_frame,
367 cpl_frame* spectralformat_frame,
368 cpl_frame* theo_tab_frame)
369{
370
371 cpl_table* tab_edges=NULL;
372 cpl_table* tab_spectral_format=NULL;
373 cpl_table* theo_tab=NULL;
374 const char* name=NULL;
375 int ord_min_ref=0;
376 int ord_max_ref=0;
377 int ord_min=0;
378 int ord_max=0;
379
380 check(name=cpl_frame_get_filename(order_tab_edges_frame));
381 check(tab_edges=cpl_table_load(name,1,0));
382 check(ord_min_ref=cpl_table_get_column_min(tab_edges,"ABSORDER"));
383 check(ord_max_ref=cpl_table_get_column_max(tab_edges,"ABSORDER"));
384
385 check(name=cpl_frame_get_filename(spectralformat_frame));
386 check(tab_spectral_format=cpl_table_load(name,1,0));
387 check(ord_min=cpl_table_get_column_min(tab_spectral_format,"ORDER"));
388 check(ord_max=cpl_table_get_column_max(tab_spectral_format,"ORDER"));
389
390 if(ord_min != ord_min_ref) {
391 xsh_msg_error("Edge order table's ord_min != spectral format table's ord_min");
392 return CPL_ERROR_INCOMPATIBLE_INPUT;
393 }
394
395
396 if(ord_max != ord_max_ref) {
397 xsh_msg_error("Edge order table's ord_max != spectral format table's ord_max");
398 return CPL_ERROR_INCOMPATIBLE_INPUT;
399 }
400
401
402 if(theo_tab_frame != NULL) {
403 check(name=cpl_frame_get_filename(theo_tab_frame));
404 check(theo_tab=cpl_table_load(name,1,0));
405 check(ord_min=cpl_table_get_column_min(theo_tab,"Order"));
406 check(ord_max=cpl_table_get_column_max(theo_tab,"Order"));
407
408 if(ord_min != ord_min_ref) {
409 xsh_msg_error("Theo table's ord_min != spectral format table's ord_min");
410 return CPL_ERROR_INCOMPATIBLE_INPUT;
411 }
412
413
414 if(ord_max != ord_max_ref) {
415 xsh_msg_error("Theo table's ord_max != spectral format table's ord_max");
416 return CPL_ERROR_INCOMPATIBLE_INPUT;
417 }
418
419
420 }
421
422
423
424 cleanup:
425 xsh_free_table(&tab_edges);
426 xsh_free_table(&tab_spectral_format);
427 xsh_free_table(&theo_tab);
428
429 return cpl_error_get_code();
430
431}
432
433static cpl_error_code
434xsh_params_set_defaults(cpl_parameterlist* pars,
435 xsh_detect_arclines_param* arclines_p,
436 xsh_instrument* inst)
437{
438 cpl_parameter* p=NULL;
439 check(p=xsh_parameters_find(pars,RECIPE_ID,"detectarclines-min-sn"));
440 if(cpl_parameter_get_double(p) <= 0) {
442 arclines_p->min_sn=3;
443 } else if (xsh_instrument_get_arm(inst) == XSH_ARM_VIS) {
444 arclines_p->min_sn=6;
445 } else {
446 arclines_p->min_sn=10;
447 }
448 }
449
450 check(p=xsh_parameters_find(pars,RECIPE_ID,"detectarclines-fit-win-hsize"));
451 if(cpl_parameter_get_int(p) <= 0) {
453 arclines_p->fit_window_hsize=3;
454 } else {
455 arclines_p->fit_window_hsize=4;
456 }
457 }
458 cleanup:
459
460 return cpl_error_get_code();
461
462}
463
464
465/*--------------------------------------------------------------------------*/
473/*--------------------------------------------------------------------------*/
474static void xsh_2dmap(cpl_parameterlist* parameters, cpl_frameset* frameset)
475{
476 const char* recipe_tags[1] = {XSH_WAVE};
477 int recipe_tags_size = 1;
478
479 /* RECIPES parameters */
480 xsh_clipping_param* detect_arclines_clipping = NULL;
481 xsh_detect_arclines_param* detect_arclines_p = NULL;
482 xsh_dispersol_param *dispsol_param = NULL ;
483 int model_map_compute=CPL_TRUE;
484
485 /* ALLOCATED locally */
486 cpl_frameset* raws = NULL;
487 cpl_frameset* calib = NULL;
488 cpl_frameset* on = NULL;
489 cpl_frameset* off = NULL;
490 cpl_frameset* on_off = NULL;
491 cpl_frame* dmap_rmbias = NULL;
492 cpl_frame* dmap_rmdark = NULL;
494 /* Others */
495 cpl_frame* model_config_frame = NULL;
496 cpl_frame* opt_model_config_frame = NULL;
497 cpl_frame* spectralformat_frame = NULL;
498 cpl_frame* bpmap = NULL;
499 cpl_frame* master_bias = NULL;
500 cpl_frame* master_dark = NULL;
501 cpl_frame* theo_tab_mult = NULL;
502 cpl_frame* resid_map = NULL;
503 cpl_frame* resid_dan = NULL;
504 cpl_frame* arclines = NULL;
505 cpl_frame* clean_arclines = NULL;
506 cpl_frame* wave_tab_guess = NULL;
507 cpl_frame* wave_tab_2d = NULL;
508 cpl_frame * order_tab_edges = NULL ;
509 cpl_frame * wave_map = NULL ;
510 cpl_frame * line_intmon = NULL ;
511 char wave_map_tag[256];
512 char slit_map_tag[256];
513
514 char *prefix = NULL ;
515 char filename[256];
516 int solution_type=XSH_DETECT_ARCLINES_TYPE_MODEL;
517 char paramname[256];
518 cpl_parameter * p =NULL;
519 cpl_frame *dispersol_frame = NULL;
520 cpl_frame *slitmap_frame = NULL;
521 int pre_overscan_corr=0;
522 cpl_propertylist* plist=NULL;
523 double exptime=0;
524 cpl_boolean mode_phys;
525 char tag[256];
526 int resid_name_sw=0;
527 /**************************************************************************/
528 /* DFS management */
529 /**************************************************************************/
530 check( xsh_begin( frameset, parameters, &instrument, &raws, &calib, recipe_tags,
531 recipe_tags_size,
532 RECIPE_ID, XSH_BINARY_VERSION,
534
536 /**************************************************************************/
537 /* Recipe frames */
538 /**************************************************************************/
539 mode_phys=xsh_mode_is_physmod(calib,instrument);
540
542 //check(arclines = xsh_find_arc_line_list_clean( calib, instrument));
543 cknull_msg(arclines = xsh_find_frame_with_tag(calib,
545 instrument),
546 "Missing %s frame",XSH_ARC_LINE_LIST);
547 check(order_tab_edges = xsh_find_order_tab_edges(calib,instrument));
548
549 if (!mode_phys) {
550 if((theo_tab_mult = xsh_find_frame_with_tag( calib,XSH_THEO_TAB_MULT,
551 instrument)) == NULL) {
552
553 }
554 solution_type=XSH_DETECT_ARCLINES_TYPE_POLY;
555 } else {
556 if((model_config_frame = xsh_find_frame_with_tag(calib,
558 instrument)) == NULL) {
559
561
562 if ((model_config_frame = xsh_find_frame_with_tag(calib,XSH_MOD_CFG_TAB,
563 instrument)) == NULL) {
565 }
566
567 }
568 solution_type=XSH_DETECT_ARCLINES_TYPE_MODEL;
569 }
570 if( (model_config_frame!=NULL) && (theo_tab_mult != NULL) ) {
571
572 xsh_msg_error("You cannot provide both a %s and a %s frame. Decide if you are in poly or physical model mode. We exit",
574 goto cleanup;
575 }
576
577 if((model_config_frame==NULL) && (theo_tab_mult == NULL) ) {
578 xsh_msg_error("You must provide either a %s or a %s frame",
580 goto cleanup;
581
582 }
583
584 /* Could be in sof */
585 check( spectralformat_frame = xsh_find_frame_with_tag( calib,
587
588 /* TODO: remove the following that looks like duplication of same task done previous line */
589 if( (spectralformat_frame = xsh_find_frame_with_tag( calib,
591 instrument)) == NULL) {
592 xsh_msg("Frame %s not provided",
595 }
596
597
598 if((line_intmon = xsh_find_frame_with_tag( calib, XSH_ARC_LINE_LIST_INTMON,
599 instrument)) == NULL) {
601 }
602
603
604/*
605 AMO: To be implemented, small function to verify that those frame
606 are compativble one with another, if provided in input
607*/
608 if(theo_tab_mult!=NULL) {
609 check(xsh_verify_2dmap_poly_input(order_tab_edges, spectralformat_frame,
610 theo_tab_mult));
611 }
612
613
614 /* In UVB and VIS mode */
616 /* RAWS frameset must have only one file */
617 XSH_ASSURE_NOT_ILLEGAL(cpl_frameset_get_size(raws) == 1);
618 if((master_bias = xsh_find_frame_with_tag(calib,XSH_MASTER_BIAS,
619 instrument)) == NULL) {
620
621 xsh_msg_warning("Frame %s not provided",XSH_MASTER_BIAS);
623 }
624
625 //check(master_dark = xsh_find_master_dark(calib,instrument));
626 if((master_dark = xsh_find_frame_with_tag(calib,XSH_MASTER_DARK,
627 instrument)) == NULL){
628 xsh_msg_warning("Frame %s not provided",XSH_MASTER_DARK);
630 }
631 }
632 /* IN NIR mode */
633 else {
634 /* RAWS frameset must have only two files */
635 check(xsh_dfs_split_nir(raws,&on,&off));
636 XSH_ASSURE_NOT_ILLEGAL(cpl_frameset_get_size(on) == 1);
637 XSH_ASSURE_NOT_ILLEGAL(cpl_frameset_get_size(off) == 1);
638 }
640 spectralformat_frame));
641
642 /**************************************************************************/
643 /* Recipe parameters */
644 /**************************************************************************/
645 check( pre_overscan_corr = xsh_parameters_get_int( parameters, RECIPE_ID,
646 "pre-overscan-corr"));
647
648 check( detect_arclines_clipping =
651 parameters));
652 check( dispsol_param = xsh_parameters_dispersol_get( RECIPE_ID,parameters)) ;
653
654 check(xsh_params_set_defaults(parameters,detect_arclines_p,instrument));
655
656 /**************************************************************************/
657 /* Recipe code */
658 /**************************************************************************/
659 /* In UVB and VIS mode */
661 cpl_frame* multi_pin_hole = NULL;
662
663 /* prepare RAW frames in XSH format */
664 check(xsh_prepare(raws,bpmap, master_bias, XSH_WAVE, instrument,
665 pre_overscan_corr,CPL_TRUE));
666 check(multi_pin_hole = cpl_frameset_get_frame(raws,0));
667 if(master_bias != NULL) {
668 /* subtract bias */
669 check(dmap_rmbias = xsh_subtract_bias(multi_pin_hole,master_bias,
670 instrument,"WAVE_",pre_overscan_corr,0));
671 } else {
672 dmap_rmbias=cpl_frame_duplicate(multi_pin_hole);
673 }
674 if(master_dark != NULL) {
675 /* subtract dark */
676 strcpy(filename,xsh_stringcat_any( "WAVE_DARK_",
678 ".fits", (void*)NULL )) ;
679
680 check(dmap_rmdark = xsh_subtract_dark(dmap_rmbias, master_dark,
681 filename, instrument));
682 } else {
683 dmap_rmdark=cpl_frame_duplicate(dmap_rmbias);
684 }
685 }
686 /* in NIR mode */
687 else{
688 /* prepare ON frames in XSH format */
689 check(xsh_prepare(on,bpmap, NULL, "ON", instrument,pre_overscan_corr,CPL_TRUE));
690 /* prepare OFF frames in XSH format */
691 check(xsh_prepare(off,bpmap, NULL, "OFF", instrument,pre_overscan_corr,CPL_TRUE));
692
693 /* subtract dark */
694
695 check(on_off = xsh_subtract_nir_on_off(on, off, instrument));
696 check(dmap_rmdark = cpl_frame_duplicate(
697 cpl_frameset_get_frame(on_off,0)));
698 }
699 /* check on binning */
702 }
703 /* detect arclines */
704 /* TODO: the exptime is used only in qc function to monitor line intensity
705 * we could move this within that function
706 */
707 plist=cpl_propertylist_load(cpl_frame_get_filename(dmap_rmdark),0);
710
711 /* commented out: not yet robust on some data */
712 if(model_config_frame!=NULL) {
713 resid_name_sw=1;
714 check( xsh_detect_arclines_dan(dmap_rmdark, theo_tab_mult,
715 arclines, wave_tab_guess, NULL,
716 model_config_frame, spectralformat_frame, NULL,
717 &clean_arclines, &wave_tab_2d,
718 &resid_dan, XSH_SOLUTION_ABSOLUTE,
719 detect_arclines_p, detect_arclines_clipping,
720 instrument,RECIPE_ID,0,resid_name_sw));
721
722 //xsh_free_frame(&clean_arclines);
723 xsh_free_frame(&wave_tab_2d);
724
725
726 check(resid_map = cpl_frame_duplicate(resid_dan));
727 cpl_table* resid = NULL;
728 cpl_table* ext = NULL;
729 const char* fname = NULL;
730 char rtag[256];
731 char fout[256];
732 cpl_propertylist* phead = NULL;
733 cpl_propertylist* xhead = NULL;
734
735 fname = cpl_frame_get_filename(resid_dan);
736 resid = cpl_table_load(fname, 1, 0);
737 phead = cpl_propertylist_load(fname, 0);
738
739 sprintf(rtag, "WAVE_RESID_TAB_LINES_GFIT_%s",
741 sprintf(fout, "%s.fits", rtag);
742
743 check(cpl_table_and_selected_int(resid, "Flag", CPL_EQUAL_TO, 0));
744 ext = cpl_table_extract_selected(resid);
745 cpl_table_save(ext, phead, xhead, fout, CPL_IO_DEFAULT);
746 xsh_free_table(&ext);
747 xsh_free_table(&resid);
748 xsh_free_propertylist(&phead);
749
750 cpl_frame_set_filename(resid_map, fout);
751 check(cpl_frame_set_tag(resid_map, rtag));
753
754 } else {
755 /* TODO: remove this second call as we did in xsh_predict and make sure that
756 * the table input of annealing uses only flag==0 values */
757
758 check( xsh_detect_arclines(dmap_rmdark, theo_tab_mult,
759 arclines, wave_tab_guess, NULL,
760 model_config_frame, spectralformat_frame, NULL,
761 &clean_arclines, &wave_tab_2d,
762 &resid_map, XSH_SOLUTION_ABSOLUTE,
763 detect_arclines_p, detect_arclines_clipping,
764 instrument,RECIPE_ID,0,resid_name_sw));
765
766 }
767 /* Now call xsh_create_wavemap */
768
769 sprintf(slit_map_tag,"SLIT_MAP_%s",
771
772 sprintf(wave_map_tag,"WAVE_MAP_%s",
774
775
776 check( xsh_data_check_spectralformat( spectralformat_frame,
777 order_tab_edges, wave_tab_2d, model_config_frame, instrument));
778
779 /* force scenario to a given value */
780 int maxit=200;
781 double ann_fac=1.0;
782 int scenario=4;
783 if ( model_config_frame == NULL){
784
785 check( wave_map = xsh_create_poly_wavemap( dmap_rmdark, wave_tab_2d,
786 order_tab_edges,
787 spectralformat_frame,
788 dispsol_param, instrument,
789 wave_map_tag, &dispersol_frame,
790 &slitmap_frame));
791 }
792 else {
793 /* Number of iterations is maxit*10 */
794 sprintf(paramname,"xsh.%s.%s",RECIPE_ID, "model-maxit");
795 check(p = cpl_parameterlist_find(parameters,paramname));
796 check(maxit=cpl_parameter_get_int(p));
797
798 sprintf(paramname,"xsh.%s.%s",RECIPE_ID, "model-anneal-factor");
799 check(p = cpl_parameterlist_find(parameters,paramname));
800 check(ann_fac=cpl_parameter_get_double(p));
801
802 sprintf(paramname,"xsh.%s.%s",RECIPE_ID, "model-scenario");
803 check(p = cpl_parameterlist_find(parameters,paramname));
804 check(scenario=cpl_parameter_get_int(p));
805 xsh_msg_dbg_low("maxit=%d ann_fac=%g scenario=%d",maxit,ann_fac,scenario);
806
807 check(opt_model_config_frame=xsh_model_pipe_anneal(model_config_frame,
808 resid_map,
809 maxit,ann_fac,
810 scenario,2));
811
812
813 if (model_map_compute){
814 check( xsh_create_model_map( opt_model_config_frame, instrument,
815 wave_map_tag,slit_map_tag,
816 &wave_map, &slitmap_frame,0));
817
818
819
820 check(dispersol_frame=xsh_create_dispersol_physmod(dmap_rmdark,
821 order_tab_edges,
822 opt_model_config_frame,
823 wave_map,
824 slitmap_frame,
825 dispsol_param,
826 spectralformat_frame,
827 instrument,1));
828 }
829 }
830 if(wave_map!=NULL) {
831 check(xsh_wavemap_qc(wave_map,order_tab_edges));
832 }
833
834 xsh_msg_dbg_low("QC wavetab");
835 if ( model_config_frame == NULL){
836 check(xsh_wavetab_qc(resid_map,true));
837 } else {
838 check(xsh_wavetab_qc(resid_map,false));
839 }
840
841 if(line_intmon) {
842 check(xsh_wavecal_qclog_intmon(resid_map,line_intmon,exptime,instrument));
843 }
844 check(xsh_table_merge_clean_and_resid_tabs(resid_map,clean_arclines));
845 /**************************************************************************/
846 /* Products */
847 /**************************************************************************/
848 xsh_msg("Saving products");
849
850 if ( wave_tab_2d != NULL){
851 check(xsh_add_product_table(wave_tab_2d, frameset,
852 parameters, RECIPE_ID, instrument,NULL));
853 }
854
855 if(model_config_frame!=NULL) {
856
857 check(xsh_frame_table_resid_merge(resid_dan,resid_map,solution_type));
858 check(xsh_add_product_table( resid_dan, frameset,
859 parameters, RECIPE_ID, instrument,NULL));
860 /*
861 check(xsh_add_product_table( resid_map, frameset,parameters, RECIPE_ID,
862 instrument,"WAVE_RESID_TAB_LINES"));
863 */
864 } else {
865 check(xsh_add_product_table( resid_map, frameset,parameters, RECIPE_ID,
866 instrument,"WAVE_RESID_TAB_LINES"));
867 }
868 if ( wave_map != NULL){
869 check(xsh_add_product_image( wave_map, frameset,
870 parameters, RECIPE_ID, instrument, XSH_WAVE_MAP ));
871 }
872 if (slitmap_frame != NULL){
873 check(xsh_add_product_image( slitmap_frame, frameset,
874 parameters, RECIPE_ID, instrument, XSH_SLIT_MAP ));
875 }
876 if ( dispersol_frame != NULL){
877 check(xsh_add_product_table( dispersol_frame, frameset,parameters,
879 }
880 sprintf(tag,"WAVE_ON");
882 check(xsh_add_product_image(dmap_rmbias,frameset,parameters,
883 RECIPE_ID,instrument,tag));
884 } else {
885
886 check(xsh_add_product_image(dmap_rmdark,frameset,parameters,
887 RECIPE_ID,instrument,tag));
888
889 }
890
891 if(opt_model_config_frame!=NULL) {
892 check(xsh_add_product_table(opt_model_config_frame, frameset,
893 parameters, RECIPE_ID, instrument,NULL));
894 }
895
896 xsh_msg("xsh_2dmap success!!");
897
898 cleanup:
899 xsh_end( RECIPE_ID, frameset, parameters );
900 /* free parameters */
901 XSH_FREE( detect_arclines_clipping);
902 XSH_FREE( detect_arclines_p);
903 XSH_FREE( dispsol_param);
904
905 xsh_free_frameset(&raws);
906 xsh_free_frameset(&calib);
908 xsh_free_frameset(&off);
909 xsh_free_frameset(&on_off);
910 xsh_free_frame(&clean_arclines);
911 xsh_free_frame(&resid_map);
912 xsh_free_frame(&resid_dan);
913 xsh_free_frame(&bpmap);
914 xsh_free_frame(&wave_tab_2d);
915 xsh_free_frame(&dmap_rmbias);
916 xsh_free_frame(&dmap_rmdark);
917 xsh_free_frame(&dispersol_frame);
918 xsh_free_frame(&slitmap_frame);
919 xsh_free_frame(&opt_model_config_frame);
920 xsh_free_propertylist(&plist);
921
923 xsh_free_frame( &wave_map ) ;
924 XSH_FREE( prefix ) ;
925 return;
926}
927
static double exptime
static xsh_instrument * instrument
static int xsh_2dmap_exec(cpl_plugin *)
Execute the plugin instance given by the interface.
Definition: xsh_2dmap.c:305
static cpl_error_code xsh_params_set_defaults(cpl_parameterlist *pars, xsh_detect_arclines_param *arclines_p, xsh_instrument *inst)
Definition: xsh_2dmap.c:434
#define RECIPE_CONTACT
Definition: xsh_2dmap.c:79
static char xsh_2dmap_description_short[]
Definition: xsh_2dmap.c:99
static int xsh_2dmap_create(cpl_plugin *)
Setup the recipe options.
Definition: xsh_2dmap.c:193
int cpl_plugin_get_info(cpl_pluginlist *list)
Build the list of available plugins, for this module.
Definition: xsh_2dmap.c:151
static int xsh_2dmap_destroy(cpl_plugin *)
Destroy what has been created by the 'create' function.
Definition: xsh_2dmap.c:337
static char xsh_2dmap_description[]
Definition: xsh_2dmap.c:102
#define RECIPE_ID
Definition: xsh_2dmap.c:77
#define RECIPE_AUTHOR
Definition: xsh_2dmap.c:78
static void xsh_2dmap(cpl_parameterlist *, cpl_frameset *)
Interpret the command line options and execute the data processing.
Definition: xsh_2dmap.c:474
static cpl_error_code xsh_verify_2dmap_poly_input(cpl_frame *order_tab_edges_frame, cpl_frame *spectralformat_frame, cpl_frame *theo_tab_frame)
Definition: xsh_2dmap.c:366
void xsh_data_check_spectralformat(cpl_frame *spectralformat_frame, cpl_frame *orderlist_frame, cpl_frame *wavesol_frame, cpl_frame *model_config_frame, xsh_instrument *instr)
Check the borders of wavelength foreach orders in spectral format.
cpl_error_code xsh_frame_table_resid_merge(cpl_frame *self, cpl_frame *right, const int solution_type)
void xsh_detect_arclines_dan(cpl_frame *frame, cpl_frame *theo_tab_frame, cpl_frame *arc_lines_tab_frame, cpl_frame *wave_tab_guess_frame, cpl_frame *order_tab_recov_frame, cpl_frame *config_model_frame, cpl_frame *spectralformat_frame, cpl_frame **resid_tab_orders_frame, cpl_frame **arc_lines_clean_tab_frame, cpl_frame **wave_tab_frame, cpl_frame **resid_tab_frame, xsh_sol_wavelength solwave_type, xsh_detect_arclines_param *da, xsh_clipping_param *dac, xsh_instrument *instr, const char *rec_id, const int clean_tmp, const int resid_tab_name_sw)
detect the position on the detector of emission lines listed in a catalogue, from expected position v...
void xsh_detect_arclines(cpl_frame *frame, cpl_frame *theo_tab_frame, cpl_frame *arc_lines_tab_frame, cpl_frame *wave_tab_guess_frame, cpl_frame *order_tab_recov_frame, cpl_frame *config_model_frame, cpl_frame *spectralformat_frame, cpl_frame **resid_tab_orders_frame, cpl_frame **arc_lines_clean_tab_frame, cpl_frame **wave_tab_frame, cpl_frame **resid_tab_frame, xsh_sol_wavelength solwave_type, xsh_detect_arclines_param *da, xsh_clipping_param *dac, xsh_instrument *instr, const char *rec_id, const int clean_tmp, const int resid_tab_name_sw)
detect the position on the detector of emission lines listed in a catalogue, from expected position v...
void xsh_prepare(cpl_frameset *frames, cpl_frame *bpmap, cpl_frame *mbias, const char *prefix, xsh_instrument *instr, const int pre_overscan_corr, const bool flag_neg_and_thresh_pix)
This function transform RAW frames dataset in PRE frames dataset attaching the default bad pixel map ...
Definition: xsh_prepare.c:122
#define XSH_ASSURE_NOT_ILLEGAL(cond)
Definition: xsh_error.h:107
#define assure(CONDITION, ERROR_CODE,...)
Definition: xsh_error.h:54
#define check(COMMAND)
Definition: xsh_error.h:71
#define xsh_error_dump(level)
Definition: xsh_error.h:92
#define xsh_error_reset()
Definition: xsh_error.h:87
#define cknull_msg(NULLEXP,...)
Definition: xsh_error.h:73
void xsh_instrument_update_from_spectralformat(xsh_instrument *i, cpl_frame *spectralformat_frame)
const char * xsh_instrument_arm_tostring(xsh_instrument *i)
Get the string associated with an arm.
XSH_ARM xsh_instrument_get_arm(xsh_instrument *i)
Get an arm on instrument structure.
void xsh_instrument_free(xsh_instrument **instrument)
free an instrument structure
cpl_frame * xsh_model_pipe_anneal(cpl_frame *cfg_frame, cpl_frame *resid_frame, int maxit, double ann_fac, int scenario, int rec_id)
Run the annealing (optimisation) algoritm to improve the fit of the model parameter set to a given wa...
#define xsh_msg_warning(...)
Print an warning message.
Definition: xsh_msg.h:88
#define xsh_msg_error(...)
Print an error message.
Definition: xsh_msg.h:62
#define xsh_msg(...)
Print a message on info level.
Definition: xsh_msg.h:121
#define xsh_msg_dbg_low(...)
Definition: xsh_msg.h:48
double xsh_pfits_get_exptime(const cpl_propertylist *plist)
find out the exposure time
Definition: xsh_pfits.c:2254
cpl_frameset * xsh_subtract_nir_on_off(cpl_frameset *on, cpl_frameset *off, xsh_instrument *instr)
(NIR only) subtract the OFF set of files from the On set of files
Definition: xsh_subtract.c:194
cpl_frame * xsh_subtract_bias(cpl_frame *frame, cpl_frame *bias, xsh_instrument *instr, const char *type, const int pre_overscan_corr, const int save_tmp)
Subtract the master bias frame from PRE frame.
Definition: xsh_subtract.c:120
cpl_frame * xsh_subtract_dark(cpl_frame *frame, cpl_frame *dark, const char *filename, xsh_instrument *instr)
subtract the master dark frame from PRE frame
Definition: xsh_subtract.c:247
cpl_error_code xsh_wavecal_qclog_intmon(cpl_frame *line_check, const cpl_frame *line_intmon, const double exptime, xsh_instrument *inst)
computes intmon QC log
void xsh_free_parameterlist(cpl_parameterlist **p)
Deallocate a parameter list and set the pointer to NULL.
Definition: xsh_utils.c:2224
cpl_error_code xsh_check_input_is_unbinned(cpl_frame *in)
Check if an input frame is not binned.
Definition: xsh_utils.c:375
cpl_error_code xsh_table_merge_clean_and_resid_tabs(cpl_frame *frm_resid, cpl_frame *frm_clean)
void xsh_free_frame(cpl_frame **f)
Deallocate a frame and set the pointer to NULL.
Definition: xsh_utils.c:2269
void xsh_free_frameset(cpl_frameset **f)
Deallocate a frame set and set the pointer to NULL.
Definition: xsh_utils.c:2254
const char * xsh_get_license(void)
Get the pipeline copyright and license.
Definition: xsh_utils.c:1193
char * xsh_stringcat_any(const char *s,...)
Concatenate an arbitrary number of strings.
Definition: xsh_utils.c:1925
void xsh_init(void)
Reset library state.
Definition: xsh_utils.c:1160
cpl_error_code xsh_begin(cpl_frameset *frames, const cpl_parameterlist *parameters, xsh_instrument **instrument, cpl_frameset **raws, cpl_frameset **calib, const char *tag_list[], int tag_list_size, const char *recipe_id, unsigned int binary_version, const char *short_descr)
Recipe initialization.
Definition: xsh_utils.c:1244
void xsh_free_table(cpl_table **t)
Deallocate a table and set the pointer to NULL.
Definition: xsh_utils.c:2133
void xsh_free_propertylist(cpl_propertylist **p)
Deallocate a property list and set the pointer to NULL.
Definition: xsh_utils.c:2179
cpl_error_code xsh_end(const char *recipe_id, cpl_frameset *frames, cpl_parameterlist *parameters)
Recipe termination.
Definition: xsh_utils.c:1519
void xsh_add_temporary_file(const char *name)
Add temporary file to temprary files list.
Definition: xsh_utils.c:1432
cpl_error_code xsh_wavetab_qc(cpl_frame *frm_tab, const int is_poly)
Monitor min/max/med/avg distance between detected lines on each ordee.
cpl_frame * xsh_create_dispersol_physmod(cpl_frame *pre_frame, cpl_frame *order_tab_frame, cpl_frame *mod_cfg_frame, cpl_frame *wave_map_frame, cpl_frame *slit_map_frame, xsh_dispersol_param *dispsol_param, cpl_frame *spectral_format_frame, xsh_instrument *instrument, const int clean_tmp)
cpl_error_code xsh_wavemap_qc(cpl_frame *frm_map, const cpl_frame *frm_tab)
Monitor Flux level along the orders traces given by an input table.
void xsh_create_model_map(cpl_frame *model_frame, xsh_instrument *instrument, const char *wtag, const char *stag, cpl_frame **wavemap_frame, cpl_frame **slitmap_frame, const int save_tmp)
cpl_frame * xsh_create_poly_wavemap(cpl_frame *pre_frame, cpl_frame *wave_tab_2d_frame, cpl_frame *order_tab_frame, cpl_frame *spectral_format_frame, xsh_dispersol_param *dispsol_par, xsh_instrument *instrument, const char *wm_tag, cpl_frame **dispersol_frame, cpl_frame **slitmap_frame)
@ XSH_ARM_UVB
@ XSH_ARM_NIR
@ XSH_ARM_VIS
cpl_frame * xsh_find_order_tab_edges(cpl_frameset *frames, xsh_instrument *instr)
Find an order tab EDGES.
Definition: xsh_dfs.c:3595
cpl_boolean xsh_mode_is_physmod(cpl_frameset *set, xsh_instrument *instrument)
Definition: xsh_dfs.c:4411
void xsh_add_product_image(cpl_frame *frame, cpl_frameset *frameset, const cpl_parameterlist *parameters, const char *recipe_id, xsh_instrument *instrument, const char *final_prefix)
Definition: xsh_dfs.c:2965
void xsh_add_product_table(cpl_frame *frame, cpl_frameset *frameset, const cpl_parameterlist *parameters, const char *recipe_id, xsh_instrument *instrument, const char *final_prefix)
Save Table product (input frame has several extensions, 1 table per extension)
Definition: xsh_dfs.c:3146
void xsh_dfs_split_nir(cpl_frameset *input, cpl_frameset **on, cpl_frameset **off)
split input RAW NIR sof in ON and OFF
Definition: xsh_dfs.c:1297
cpl_frame * xsh_find_frame_with_tag(cpl_frameset *frames, const char *tag, xsh_instrument *instr)
Find frame with a given tag.
Definition: xsh_dfs.c:3347
#define XSH_WAVE_MAP
Definition: xsh_dfs.h:928
#define XSH_ARC_LINE_LIST_INTMON
Definition: xsh_dfs.h:63
#define XSH_MASTER_BIAS
Definition: xsh_dfs.h:549
#define XSH_MASTER_DARK
Definition: xsh_dfs.h:550
#define XSH_MOD_CFG_TAB
Definition: xsh_dfs.h:1251
#define XSH_DISP_TAB
Definition: xsh_dfs.h:897
#define XSH_SLIT_MAP
Definition: xsh_dfs.h:907
#define XSH_WAVE
Definition: xsh_dfs.h:225
#define XSH_MOD_CFG_OPT_FMT
Definition: xsh_dfs.h:1227
#define XSH_SPECTRAL_FORMAT
Definition: xsh_dfs.h:1196
#define XSH_ARC_LINE_LIST
Definition: xsh_dfs.h:955
#define XSH_THEO_TAB_MULT
Definition: xsh_dfs.h:1089
@ XSH_SOLUTION_ABSOLUTE
Definition: xsh_drl.h:232
#define XSH_DETECT_ARCLINES_TYPE_POLY
Definition: xsh_drl.h:225
#define XSH_DETECT_ARCLINES_TYPE_MODEL
Definition: xsh_drl.h:226
cpl_frame * xsh_check_load_master_bpmap(cpl_frameset *calib, xsh_instrument *inst, const char *rec_id)
Definition: xsh_drl_check.c:51
cpl_parameter * xsh_parameters_find(cpl_parameterlist *list, const char *recipe_id, const char *name)
find a parameter
void xsh_parameters_pre_overscan(const char *recipe_id, cpl_parameterlist *plist)
void xsh_parameters_clipping_detect_arclines_create(const char *recipe_id, cpl_parameterlist *list, xsh_clipping_param p)
Create the arclines clipping parameters in a parameters list.
cpl_error_code xsh_parameters_decode_bp(const char *recipe_id, cpl_parameterlist *plist, const int ival)
void xsh_parameters_new_double(cpl_parameterlist *list, const char *recipe_id, const char *name, double value, const char *comment)
xsh_detect_arclines_param * xsh_parameters_detect_arclines_get(const char *recipe_id, cpl_parameterlist *list)
get the detect arclines parameters in a parameters list
void xsh_parameters_dispersol_create(const char *recipe_id, cpl_parameterlist *list, xsh_dispersol_param p)
Create the dispersol compute parameters in a parameters list.
xsh_clipping_param * xsh_parameters_clipping_detect_arclines_get(const char *recipe_id, cpl_parameterlist *list)
Get the arclines clipping parameters in a parameters list.
int xsh_parameters_get_int(const cpl_parameterlist *list, const char *recipe_id, const char *name)
void xsh_parameters_generic(const char *recipe_id, cpl_parameterlist *plist)
void xsh_parameters_detect_arclines_create(const char *recipe_id, cpl_parameterlist *list, xsh_detect_arclines_param p)
create the detect arclines parameters in a parameters list
cpl_error_code xsh_recipe_params_check(cpl_parameterlist *parameters, xsh_instrument *instrument, const char *rec_id)
void xsh_parameters_new_int(cpl_parameterlist *list, const char *recipe_id, const char *name, int value, const char *comment)
xsh_dispersol_param * xsh_parameters_dispersol_get(const char *recipe_id, cpl_parameterlist *list)
Get the dispersion solution compute parameters in a parameters list.
#define DECODE_BP_FLAG_DEF
@ XSH_GAUSSIAN_METHOD
#define XSH_FREE(POINTER)
Definition: xsh_utils.h:92