X-shooter Pipeline Reference Manual 3.8.15
xsh_predict.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/*
22 * $Author: amodigli $
23 * $Date: 2013-01-01 12:47:55 $
24 * $Revision: 1.157 $
25 * $Name: not supported by cvs2svn $
26 *
27 */
28
29#ifdef HAVE_CONFIG_H
30#include <config.h>
31#endif
32
33/*--------------------------------------------------------------------------*/
44/*--------------------------------------------------------------------------*/
47/*---------------------------------------------------------------------------
48 Includes
49 ---------------------------------------------------------------------------*/
50
51
52//#include <xsh_pfits.h>
53/* DRL steps */
54
55/* Error handling */
56#include <xsh_error.h>
57/* Utility fonctions */
58#include <xsh_utils.h>
59#include <xsh_msg.h>
60/* DFS functions */
61#include <xsh_dfs.h>
62#include <xsh_pfits.h>
63#include <xsh_utils_table.h>
64#include <xsh_data_resid_tab.h>
65/* DRL functions */
66#include <xsh_data_order.h>
67#include <xsh_drl.h>
68#include <xsh_drl_check.h>
69#include <xsh_model_kernel.h>
71
72
73/* Library */
74#include <cpl.h>
75
76
77
78#include <xsh_data_arclist.h>
79/*---------------------------------------------------------------------------
80 Defines
81 ---------------------------------------------------------------------------*/
82
83#define RECIPE_ID "xsh_predict"
84#define RECIPE_AUTHOR "P.Goldoni, L.Guglielmi, R. Haigron, F. Royer, P. Bristow, D. Bramich, A. Modigliani"
85#define RECIPE_CONTACT "amodigli@eso.org"
86
87/*---------------------------------------------------------------------------
88 Functions prototypes
89 ---------------------------------------------------------------------------*/
90
91/*
92 * Plugin initalization, execute and cleanup handlers
93 */
94
95static int xsh_predict_create(cpl_plugin *);
96static int xsh_predict_exec(cpl_plugin *);
97static int xsh_predict_destroy(cpl_plugin *);
98
99/* The actual executor function */
100static void xsh_predict(cpl_parameterlist *, cpl_frameset *);
101
102static void xsh_predict_qc(cpl_table* tab,cpl_propertylist* head,const char* arm);
103
104/*---------------------------------------------------------------------------
105 Static variables
106 ---------------------------------------------------------------------------*/
108"Compute a first guess dispersion solution and order table";
109
111"This recipe creates a wavelength solution and an order table.\n\
112 Input Frames :\n\
113 - [UVB, VIS] One RAW frame (Format = RAW, Tag = FMTCHK_arm)\n\
114 - [NIR] Two RAW frames ((Format = RAW, Tag = FMTCHK_arm_ON,\
115FMTCHK_arm_OFF)\n\
116 - A spectral format table (Format = PRE, Tag = SPECTRAL_FORMAT_TAB_arm)\n\
117 - An arc line list (Format = TABLE, Tag = ARC_LINE_LIST_arm)\n\
118 - [UVB,VIS,OPTIONAL] A master bias (Format = PRE, Tag = MASTER_BIAS_arm)\n\
119 - [UVB,VIS,OPTIONAL] A master dark (Format = PRE, Tag = MASTER_DARK_arm)\n\
120 - [OPTIONAL] A reference badpixel map (Format = QUP, Tag = BP_MAP_RP_arm)\n\
121 - [OPTIONAL] A non-linear badpixel map (Format = QUP, Tag = BP_MAP_NL_arm)\n\
122 - [OPTIONAL] Reference list to monitor line intensity (Tag = ARC_LINE_LIST_INTMON_arm)\n \
123 - [poly mode] A theoretical map (Format = TABLE, Tag = THEO_TAB_SING_arm)\n\
124 - [physical model mode] A model cfg table (Format = TABLE, Tag = XSH_MOD_CFG_TAB_arm)\n\
125 Products : \n\
126 - [poly mode] A wavelength solution (Format = TABLE, PRO.CATG = \
127WAVE_TAB_GUESS_arm)\n\
128 - An order table, PRO.CATG = ORDER_TAB_GUESS_arm\n\
129 (if at least degree+1 points are found in each order).\n \
130 - A line identification residual table, PRO.CATG = FMTCHK_RESID_TAB_LINES_arm\n\
131 - The bias subtracted formatcheck frame, PRO.CATG = FMTCHK_ON_arm\n\
132 - [physical model mode]An optimized model configuration table, PRO.CATG = XSH_MOD_CFG_OPT_FMT_ARM\n\
133 Prepare the frames.\n\
134 For UVB,VIS :\n\
135 Subtract Master Bias.\n\
136 Subtract Master Dark\n\
137 For NIR:\n\
138 Subtract ON OFF\n\
139 Compute guess order table and wavelength solution\n";
140
141/*---------------------------------------------------------------------------
142 Functions code
143 ---------------------------------------------------------------------------*/
144/*--------------------------------------------------------------------------*/
153/*--------------------------------------------------------------------------*/
154
155int cpl_plugin_get_info(cpl_pluginlist *list) {
156 cpl_recipe *recipe = NULL;
157 cpl_plugin *plugin = NULL;
158
159 recipe = cpl_calloc(1, sizeof(*recipe));
160 if ( recipe == NULL ){
161 return -1;
162 }
163
164 plugin = &recipe->interface ;
165
166 cpl_plugin_init(plugin,
167 CPL_PLUGIN_API, /* Plugin API */
168 XSH_BINARY_VERSION, /* Plugin version */
169 CPL_PLUGIN_TYPE_RECIPE, /* Plugin type */
170 RECIPE_ID, /* Plugin name */
171 xsh_predict_description_short, /* Short help */
172 xsh_predict_description, /* Detailed help */
173 RECIPE_AUTHOR, /* Author name */
174 RECIPE_CONTACT, /* Contact address */
175 xsh_get_license(), /* Copyright */
179
180 cpl_pluginlist_append(list, plugin);
181
182 return (cpl_error_get_code() != CPL_ERROR_NONE);
183 }
184
185/*--------------------------------------------------------------------------*/
195/*--------------------------------------------------------------------------*/
196
197static int xsh_predict_create(cpl_plugin *plugin){
198 cpl_recipe *recipe = NULL;
199 xsh_clipping_param detarc_clip_param = { 2.0, 10, 0.7, 0, 0.3};
200 xsh_detect_arclines_param detarc_param = {6, 3, 0, 5, 5, 0, 2, 5.0,
201 XSH_GAUSSIAN_METHOD, FALSE};
202 char paramname[256];
203 cpl_parameter* p=NULL;
204 const int ival=DECODE_BP_FLAG_DEF;
205 /* Reset library state */
206 xsh_init();
207
208 /* Check input */
209 assure( plugin != NULL, CPL_ERROR_NULL_INPUT, "Null plugin");
210
211 /* Get the recipe out of the plugin */
212 assure( cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE,
213 CPL_ERROR_TYPE_MISMATCH,
214 "Plugin is not a recipe");
215
216 recipe = (cpl_recipe *)plugin;
217
218 /* Create the parameter list in the cpl_recipe object */
219 recipe->parameters = cpl_parameterlist_new();
220 assure( recipe->parameters != NULL,
221 CPL_ERROR_ILLEGAL_OUTPUT,
222 "Memory allocation failed!");
223
224 /* Set generic parameters (common to all recipes) */
225 check( xsh_parameters_generic( RECIPE_ID, recipe->parameters ) ) ;
226 xsh_parameters_decode_bp(RECIPE_ID,recipe->parameters,ival);
227 check( xsh_parameters_pre_overscan( RECIPE_ID, recipe->parameters ) ) ;
228
229
230 /* detect arclines params */
232 detarc_param));
234 recipe->parameters, detarc_clip_param));
235
236
237
238 /* Annealing parameter */
239 check( xsh_parameters_new_int( recipe->parameters, RECIPE_ID,
240 "model-maxit",1000,
241 "Number/10 of annealing iterations "
242 "if in physical model mode."));
243
244 check( xsh_parameters_new_double( recipe->parameters, RECIPE_ID,
245 "model-anneal-factor",1.0,
246 "Multiplier applied to the automatic "
247 "parameter ranges (i.e. when scenario!=0). "
248 "For routine operations should be 1.0. "
249 "(physical model mode)."));
250
251
252 sprintf(paramname,"xsh.%s.%s",RECIPE_ID,"model-scenario");
253
254 /*Scenario descriptions for development use removed so as not to confuse users*/
255 check(p=cpl_parameter_new_enum(paramname,CPL_TYPE_INT,
256 "selects preset flag and range combinations "
257 "appropriate to common scenarios: \n"
258/* "-1 - Only the position across the slit and "
259 "camera focal length are open\n" */
260 " 0 - No scenario, input cfg flags and limits "
261 "used.\n"
262 " 1 - scenario appropriate for the startup "
263 "recipe (large ranges for parameters "
264 "affecting single ph exposures, dist "
265 "coeff fixed).\n"
266 " 2 - Like 1, but includes parameters "
267 "affecting all ph positions.\n"
268 " 3 - Scenario for use in fine tuning cfg "
269 "to match routine single pinhole exposures. "
270 "All parameters affecting 1ph exposures "
271 "except dist coeffs are included and "
272 "parameter ranges are small. (For use by "
273 "predict in 1ph case).\n"
274 " 4 - Like 3 but includes parameters "
275 "affecting all ph positions (Standard for "
276 "use by predict in 9ph case and 2dmap). \n"
277/* " 5 - Like 4 but includes also dist coeffs.\n"
278 " 6 - Just dist coeffs (and chipx, chipy).\n"*/
279 ,RECIPE_ID,3,9,-1,0,1,2,3,4,5,6,8));
280
281
282 check(cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI,
283 "model-scenario"));
284 check(cpl_parameterlist_append(recipe->parameters,p));
285 cleanup:
286 if ( cpl_error_get_code() != CPL_ERROR_NONE ){
287 xsh_error_dump(CPL_MSG_ERROR);
288 return 1;
289 }
290 else {
291 return 0;
292 }
293}
294
295/*--------------------------------------------------------------------------*/
301/*--------------------------------------------------------------------------*/
302
303static int xsh_predict_exec(cpl_plugin *plugin) {
304 cpl_recipe *recipe = NULL;
305
306 /* Check parameter */
307 assure( plugin != NULL, CPL_ERROR_NULL_INPUT, "Null plugin" );
308
309 /* Get the recipe out of the plugin */
310 assure( cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE,
311 CPL_ERROR_TYPE_MISMATCH, "Plugin is not a recipe");
312
313 recipe = (cpl_recipe *)plugin;
314
315 /* Check recipe */
316 xsh_predict(recipe->parameters, recipe->frames);
317
318 cleanup:
319 if ( cpl_error_get_code() != CPL_ERROR_NONE ) {
320 xsh_error_dump(CPL_MSG_ERROR);
321 cpl_error_reset();
322 return 1;
323 }
324 else {
325 return 0;
326 }
327}
328static void xsh_predict_qc(cpl_table* tab,cpl_propertylist* head,const char* arm){
329 /* calculate QC params */
330 for(cpl_size row=0;row<cpl_table_get_nrow(tab);row++){
331 double wave = cpl_table_get_double(tab,XSH_RESID_TAB_TABLE_COLNAME_WAVELENGTH,row,NULL);
332 /* In some instances the same line appears in multiple orders. In these cases we specify which is the preferred order */
333 int order = cpl_table_get_int(tab,XSH_RESID_TAB_TABLE_COLNAME_ORDER,row,NULL);
334 //cpl_msg_info(cpl_func,"Wave: %.2f",wave);
335 if(!strcmp(arm,"UVB")){
336 //303.35
337 if(wave >= 303 && wave <= 304){
338 cpl_propertylist_update_double(head,"ESO QC X LL",cpl_table_get_double(tab,XSH_RESID_TAB_TABLE_COLNAME_XGAUSS,row,NULL));
339 cpl_propertylist_update_double(head,"ESO QC Y LL",cpl_table_get_double(tab,XSH_RESID_TAB_TABLE_COLNAME_YGAUSS,row,NULL));
340 }
341 //550.0
342 //Appears in more than one order
343 if(wave >= 550.5 && wave <= 551 && order == 13){
344 cpl_propertylist_update_double(head,"ESO QC X LR",cpl_table_get_double(tab,XSH_RESID_TAB_TABLE_COLNAME_XGAUSS,row,NULL));
345 cpl_propertylist_update_double(head,"ESO QC Y LR",cpl_table_get_double(tab,XSH_RESID_TAB_TABLE_COLNAME_YGAUSS,row,NULL));
346 }
347 //325.75
348 if(wave >= 325.7 && wave <= 326 && order == 23){
349 cpl_propertylist_update_double(head,"ESO QC X UL",cpl_table_get_double(tab,XSH_RESID_TAB_TABLE_COLNAME_XGAUSS,row,NULL));
350 cpl_propertylist_update_double(head,"ESO QC Y UL",cpl_table_get_double(tab,XSH_RESID_TAB_TABLE_COLNAME_YGAUSS,row,NULL));
351 }
352 //586.03
353 if(wave >= 585 && wave <= 587){
354 cpl_propertylist_update_double(head,"ESO QC X UR",cpl_table_get_double(tab,XSH_RESID_TAB_TABLE_COLNAME_XGAUSS,row,NULL));
355 cpl_propertylist_update_double(head,"ESO QC Y UR",cpl_table_get_double(tab,XSH_RESID_TAB_TABLE_COLNAME_YGAUSS,row,NULL));
356 }
357 }
358 if(!strcmp(arm,"VIS")){
359 //561.206
360 if(wave >= 561.2 && wave <= 561.4 && order == 29){
361 cpl_propertylist_update_double(head,"ESO QC X LL",cpl_table_get_double(tab,XSH_RESID_TAB_TABLE_COLNAME_XGAUSS,row,NULL));
362 cpl_propertylist_update_double(head,"ESO QC Y LL",cpl_table_get_double(tab,XSH_RESID_TAB_TABLE_COLNAME_YGAUSS,row,NULL));
363 }
364 //933.476
365 if(wave >= 933 && wave <= 934 && order == 17){
366 cpl_propertylist_update_double(head,"ESO QC X LR",cpl_table_get_double(tab,XSH_RESID_TAB_TABLE_COLNAME_XGAUSS,row,NULL));
367 cpl_propertylist_update_double(head,"ESO QC Y LR",cpl_table_get_double(tab,XSH_RESID_TAB_TABLE_COLNAME_YGAUSS,row,NULL));
368 }
369 //567.383
370 if(wave >= 567.3 && wave <= 567.9 && order == 29){
371 cpl_propertylist_update_double(head,"ESO QC X UL",cpl_table_get_double(tab,XSH_RESID_TAB_TABLE_COLNAME_XGAUSS,row,NULL));
372 cpl_propertylist_update_double(head,"ESO QC Y UL",cpl_table_get_double(tab,XSH_RESID_TAB_TABLE_COLNAME_YGAUSS,row,NULL));
373 }
374 //1033.274
375 if(wave >= 1033 && wave <= 1034){
376 cpl_propertylist_update_double(head,"ESO QC X UR",cpl_table_get_double(tab,XSH_RESID_TAB_TABLE_COLNAME_XGAUSS,row,NULL));
377 cpl_propertylist_update_double(head,"ESO QC Y UR",cpl_table_get_double(tab,XSH_RESID_TAB_TABLE_COLNAME_YGAUSS,row,NULL));
378 }
379 }
380 if(!strcmp(arm,"NIR")){
381 //1013.975
382 if(wave >= 1013 && wave <= 1014){
383 cpl_propertylist_update_double(head,"ESO QC X LL",cpl_table_get_double(tab,XSH_RESID_TAB_TABLE_COLNAME_XGAUSS,row,NULL));
384 cpl_propertylist_update_double(head,"ESO QC Y LL",cpl_table_get_double(tab,XSH_RESID_TAB_TABLE_COLNAME_YGAUSS,row,NULL));
385 }
386 //2445.936
387 if(wave >= 2445 && wave <= 2446){
388 cpl_propertylist_update_double(head,"ESO QC X LR",cpl_table_get_double(tab,XSH_RESID_TAB_TABLE_COLNAME_XGAUSS,row,NULL));
389 cpl_propertylist_update_double(head,"ESO QC Y LR",cpl_table_get_double(tab,XSH_RESID_TAB_TABLE_COLNAME_YGAUSS,row,NULL));
390 }
391 //992.319
392 if(wave >= 992 && wave <= 993){
393 cpl_propertylist_update_double(head,"ESO QC X UL",cpl_table_get_double(tab,XSH_RESID_TAB_TABLE_COLNAME_XGAUSS,row,NULL));
394 cpl_propertylist_update_double(head,"ESO QC Y UL",cpl_table_get_double(tab,XSH_RESID_TAB_TABLE_COLNAME_YGAUSS,row,NULL));
395 }
396 //2310.045
397 if(wave >= 2310 && wave <= 2311){
398 cpl_propertylist_update_double(head,"ESO QC X UR",cpl_table_get_double(tab,XSH_RESID_TAB_TABLE_COLNAME_XGAUSS,row,NULL));
399 cpl_propertylist_update_double(head,"ESO QC Y UR",cpl_table_get_double(tab,XSH_RESID_TAB_TABLE_COLNAME_YGAUSS,row,NULL));
400 }
401 }
402
403 }
404
405
406}
407
408
409
410/*--------------------------------------------------------------------------*/
416/*--------------------------------------------------------------------------*/
417static int xsh_predict_destroy(cpl_plugin *plugin)
418{
419 cpl_recipe *recipe = NULL;
420
421 /* Check parameter */
422 assure( plugin != NULL, CPL_ERROR_NULL_INPUT, "Null plugin" );
423
424 /* Get the recipe out of the plugin */
425 assure( cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE,
426 CPL_ERROR_TYPE_MISMATCH, "Plugin is not a recipe");
427
428 recipe = (cpl_recipe *)plugin;
429
430 xsh_free_parameterlist(&recipe->parameters);
431
432 cleanup:
433 if (cpl_error_get_code() != CPL_ERROR_NONE)
434 {
435 return 1;
436 }
437 else
438 {
439 return 0;
440 }
441}
442
443/*--------------------------------------------------------------------------*/
451/*--------------------------------------------------------------------------*/
452static void xsh_predict(cpl_parameterlist* parameters, cpl_frameset* frameset)
453{
454 const char* recipe_tags[1] = {XSH_FMTCHK};
455 int recipe_tags_size = 1;
456 char tag[256];
457 /* RECIPES parameters */
458 xsh_clipping_param* detect_arclines_clipping = NULL;
459 xsh_detect_arclines_param* detect_arclines_p = NULL;
460
461 /* ALLOCATED locally */
462 cpl_frameset* raws = NULL;
463 cpl_frameset* calib = NULL;
464 cpl_frameset* on = NULL;
465 cpl_frameset* off = NULL;
466 cpl_frameset* on_off = NULL;
467 cpl_frame* predict_rmbias = NULL;
468 cpl_frame* predict_rmdark = NULL;
470 int solution_type=XSH_DETECT_ARCLINES_TYPE_MODEL;
471 /* Others */
472 cpl_frame* model_config_frame = NULL;
473 cpl_frame* spectralformat_frame = NULL;
474 cpl_frame* bpmap = NULL;
475 cpl_frame* master_bias = NULL;
476 cpl_frame* master_dark = NULL;
477 cpl_frame* theo_tab_sing = NULL;
478 cpl_frame* resid_map = NULL;
479 cpl_frame* resid_dan = NULL;
480 cpl_frame* resid_tab_orders_frame = NULL;
481 cpl_frame *wave_tab_guess_frame = NULL;
482 cpl_frame *order_tab_recov_frame = NULL;
483 cpl_frame* arclines = NULL;
484 cpl_frame* clean_arclines = NULL;
485 cpl_frame* guess_order_table = NULL;
486 cpl_frame* guess_wavesol = NULL;
487 const char* filename=NULL;
488 cpl_frame* MODEL_CONF_OPT_frame=NULL;
489
490 int maxit=200;
491
492 int scenario=3;
493
494 char paramname[256];
495 cpl_parameter * p =NULL;
496 int pre_overscan_corr=0;
497 cpl_frame * line_intmon = NULL ;
498 double exptime=0;
499 cpl_propertylist* plist=NULL;
500 cpl_boolean mode_phys;
501 int resid_name_sw=0;
502 /**************************************************************************/
503 /* DFS management */
504 /**************************************************************************/
505 check( xsh_begin( frameset, parameters, &instrument, &raws, &calib,
506 recipe_tags, recipe_tags_size,
507 RECIPE_ID, XSH_BINARY_VERSION,
510 /* check critical parameter values */
512 /**************************************************************************/
513 /* Recipe frames */
514 /**************************************************************************/
515 mode_phys=xsh_mode_is_physmod(calib,instrument);
517 check( arclines = xsh_find_arc_line_list( calib, instrument));
518 check( spectralformat_frame = xsh_find_frame_with_tag( calib,
520
521 /* Could be in sof */
522 if (!mode_phys) {
523 theo_tab_sing = xsh_find_frame_with_tag( calib, XSH_THEO_TAB_SING,
524 instrument);
525 wave_tab_guess_frame = xsh_find_frame_with_tag( calib, XSH_WAVE_TAB_GUESS,
526 instrument);
527 order_tab_recov_frame = xsh_find_frame_with_tag( calib, XSH_ORDER_TAB_RECOV,
528 instrument);
529 solution_type = XSH_DETECT_ARCLINES_TYPE_POLY;
530 } else {
531 if( NULL == (model_config_frame = xsh_find_frame_with_tag( calib,
533 instrument))){
534
535
536 model_config_frame=xsh_find_frame_with_tag( calib, XSH_MOD_CFG_OPT_REC,
537 instrument);
538 }
539 solution_type = XSH_DETECT_ARCLINES_TYPE_MODEL;
540 }
541
542 /* In UVB and VIS mode */
544 /* RAWS frameset must have only one file */
545 XSH_ASSURE_NOT_ILLEGAL_MSG(cpl_frameset_get_size(raws) == 1,
546 "Provide one formatcheck frame for UVB,VIS arm");
547 if((master_bias = xsh_find_frame_with_tag(calib,XSH_MASTER_BIAS,
548 instrument)) == NULL) {
549
550 xsh_msg_warning("Frame %s not provided",XSH_MASTER_BIAS);
552 }
553
554 if((master_dark = xsh_find_frame_with_tag(calib,XSH_MASTER_DARK,
555 instrument)) == NULL){
556 xsh_msg_warning("Frame %s not provided",XSH_MASTER_DARK);
558 }
559 }
560 /* IN NIR mode */
561 else {
562 /* RAWS frameset must have only two files */
563 check(xsh_dfs_split_nir(raws,&on,&off));
564 XSH_ASSURE_NOT_ILLEGAL_MSG(cpl_frameset_get_size(on) == 1,
565 "Provide one formatcheck on frame for NIR arm");
566 XSH_ASSURE_NOT_ILLEGAL_MSG(cpl_frameset_get_size(off) == 1,
567 "Provide one formatcheck off frame for NIR arm");
568 }
570 spectralformat_frame));
571
572
573 if((line_intmon = xsh_find_frame_with_tag( calib, XSH_ARC_LINE_LIST_INTMON,
574 instrument)) == NULL) {
576 }
577 /**************************************************************************/
578 /* Recipe parameters */
579 /**************************************************************************/
580 check(detect_arclines_clipping =
583 parameters));
584 check( pre_overscan_corr = xsh_parameters_get_int( parameters, RECIPE_ID,
585 "pre-overscan-corr"));
586
587 /**************************************************************************/
588 /* Recipe code */
589 /**************************************************************************/
590
591 /* In UVB and VIS mode */
593 cpl_frame* fmtchk = NULL;
594
595 /* prepare RAW frames in XSH format */
596 check(xsh_prepare( raws, bpmap, master_bias, XSH_FMTCHK, instrument,
597 pre_overscan_corr,CPL_TRUE));
598 check(fmtchk = cpl_frameset_get_frame(raws,0));
599
600 if(master_bias != NULL) {
601 /* subtract bias */
602 check(predict_rmbias = xsh_subtract_bias(fmtchk,master_bias,
603 instrument,"FMTCHK_",
604 pre_overscan_corr,0));
605 } else {
606 predict_rmbias =cpl_frame_duplicate(fmtchk);
607 }
608
609 if(master_dark != NULL) {
610 /* subtract dark */
611 filename = xsh_stringcat_any( "FMTCHK_DARK_",
613 ".fits", (void*)NULL ) ;
614
615
616 check(predict_rmdark = xsh_subtract_dark(predict_rmbias, master_dark,
617 filename, instrument));
618 } else {
619 predict_rmdark =cpl_frame_duplicate(predict_rmbias);
620 }
621
622 }
623 /* in NIR mode */
624 else{
625 /* prepare ON frames in XSH format */
626 check(xsh_prepare(on,bpmap, NULL, "ON", instrument,pre_overscan_corr,CPL_TRUE));
627 /* prepare OFF frames in XSH format */
628 check(xsh_prepare(off,bpmap, NULL, "OFF", instrument,pre_overscan_corr,CPL_TRUE));
629
630 /* subtract dark */
631 check(on_off = xsh_subtract_nir_on_off(on, off, instrument));
632 check(predict_rmdark = cpl_frame_duplicate(
633 cpl_frameset_get_frame(on_off,0)));
634 }
635 /* check on binning */
637 check(xsh_check_input_is_unbinned(predict_rmdark));
638 }
639 plist=cpl_propertylist_load(cpl_frame_get_filename(predict_rmdark),0);
641 xsh_free_propertylist(&plist);
642
643 xsh_msg("Calling the xsh_detect_arclines");
644
645 /* detect arclines */
646 /* dan method */
647 if(model_config_frame!=NULL) {
648 resid_name_sw=1;
649 }
650 check( xsh_detect_arclines_dan( predict_rmdark, theo_tab_sing,
651 arclines, wave_tab_guess_frame,
652 order_tab_recov_frame,
653 model_config_frame,
654 spectralformat_frame,
655 &resid_tab_orders_frame,
656 &clean_arclines, &guess_wavesol,
657 &resid_dan, XSH_SOLUTION_RELATIVE,
658 detect_arclines_p,
659 detect_arclines_clipping,
660 instrument,RECIPE_ID,0,resid_name_sw));
661
662/*
663 xsh_free_frame(&clean_arclines);
664 xsh_free_frame(&guess_wavesol);
665 xsh_free_frame(&resid_tab_orders_frame);
666*/
667
668 /*
669 From residual table with all flag values extracts the one for which flag=0
670 */
671
672 resid_map = cpl_frame_duplicate(resid_dan);
673 cpl_table* resid = NULL;
674 cpl_table* ext = NULL;
675 const char* fname = NULL;
676 char rtag[256];
677 char fout[256];
678 cpl_propertylist* phead = NULL;
679 cpl_propertylist* xhead = NULL;
680
681 fname = cpl_frame_get_filename(resid_dan);
682 resid = cpl_table_load(fname, 1, 0);
683 phead = cpl_propertylist_load(fname, 0);
684
685 cpl_propertylist* qhead = NULL;
686 qhead=cpl_propertylist_new();
687 fname = cpl_frame_get_filename(clean_arclines);
688 plist = cpl_propertylist_load(fname, 0);
689 cpl_propertylist_copy_property_regexp(qhead, plist, "ESO QC*",0);
690 cpl_propertylist_append(phead,qhead);
691 xsh_free_propertylist(&qhead);
692
693 sprintf(rtag, "FMTCHK_RESID_TAB_LINES_GFIT_%s",
695 sprintf(fout, "%s.fits", rtag);
696
697 cpl_table_and_selected_int(resid, "Flag", CPL_EQUAL_TO, 0);
698 ext = cpl_table_extract_selected(resid);
699
701
702
703 cpl_table_save(ext, phead, xhead, fout, CPL_IO_DEFAULT);
704 xsh_free_table(&resid);
705 cpl_frame_set_filename(resid_map, fout);
706 cpl_frame_set_tag(resid_map, rtag);
707 if(model_config_frame != NULL) {
709 }
710 xsh_free_propertylist(&phead);
711 xsh_free_table(&ext);
712/*
713 check( xsh_detect_arclines( predict_rmdark, theo_tab_sing,
714 arclines, wave_tab_guess_frame,
715 order_tab_recov_frame,
716 model_config_frame,
717 spectralformat_frame,
718 &resid_tab_orders_frame,
719 &clean_arclines, &guess_wavesol,
720 &resid_map, XSH_SOLUTION_RELATIVE,
721 detect_arclines_p,
722 detect_arclines_clipping,
723 instrument,RECIPE_ID,0,resid_name_sw));
724
725 xsh_msg("file name=%s",cpl_frame_get_filename(resid_map));
726*/
727
728
729 double ann_fac=1.0;
730 if ( model_config_frame != NULL){
731 xsh_msg("Produce new config file");
732
733 /*call the pipe_anneal function. The final 3 numeric parameters are:
734 param3: the number of iterations, we keep it low here since we are
735 just fine tuning an already good config.
736 param4: A multiplier that is applied to all parameter ranges (only
737 when param5!=0, see below), should be 1.0 here.
738
739 param5: The scenario parameter, we are only interested in scenario
740 3 (allows small ranges on all parameters except for dist coefficients)
741 and 0 (uses parameters and ranges found in the input FITS config, the
742 param4 multiplier is ignored) here.
743 A switch can be set here to check if there is a cl option that specifies
744 the use of input FITS config flags and ranges (param5=0) instead of the
745 default scenarios (param5=3)*/
746
747
748 sprintf(paramname,"xsh.%s.%s",RECIPE_ID, "model-maxit");
749 check(p = cpl_parameterlist_find(parameters,paramname));
750 check(maxit=cpl_parameter_get_int(p));
751
752 sprintf(paramname,"xsh.%s.%s",RECIPE_ID, "model-anneal-factor");
753 check(p = cpl_parameterlist_find(parameters,paramname));
754 check(ann_fac=cpl_parameter_get_double(p));
755
756 sprintf(paramname,"xsh.%s.%s",RECIPE_ID, "model-scenario");
757 check(p = cpl_parameterlist_find(parameters,paramname));
758 check(scenario=cpl_parameter_get_int(p));
759 xsh_msg("maxit=%d ann_fac=%g scenario=%d",maxit,ann_fac,scenario);
760
761 check(MODEL_CONF_OPT_frame=xsh_model_pipe_anneal( model_config_frame, resid_map,
762 maxit,ann_fac,scenario,1));
763
764 }
765 /*TODO: we need to generate the guess order table by the line table with flag=0 */
766 check( guess_order_table=xsh_create_order_table(predict_rmdark,
767 spectralformat_frame,
768 resid_tab_orders_frame,
769 clean_arclines,
770 detect_arclines_p,
771 detect_arclines_clipping,
772 instrument));
773
774
775 if(line_intmon) {
776 check(xsh_wavecal_qclog_intmon(resid_map,line_intmon,exptime,instrument));
777 }
778 //check(xsh_table_merge_clean_and_resid_tabs(resid_map,clean_arclines));
779 /**************************************************************************/
780 /* Products */
781 /**************************************************************************/
782 xsh_msg("Saving products");
783 /*
784 check(xsh_add_product_table( clean_arclines, frameset,
785 parameters, RECIPE_ID, instrument,NULL));
786 */
787 if ( guess_wavesol != NULL){
788 check(xsh_add_product_table( guess_wavesol, frameset,
789 parameters, RECIPE_ID, instrument,NULL));
790 }
791 if ( guess_order_table != NULL){
792 check(xsh_add_product_table( guess_order_table, frameset,
793 parameters, RECIPE_ID, instrument,NULL));
794 }
795 if(model_config_frame == NULL) {
796 check(xsh_wavetab_qc(resid_map,true));
797 } else {
798 check(xsh_wavetab_qc(resid_map,false));
799 }
800
801
802 if(model_config_frame!=NULL) {
803
804 check(xsh_frame_table_resid_merge(resid_dan,resid_map,solution_type));
805 /* as to anneal the mode we have corrected X one-pinhole positions to
806 * match the 2dmap position of pinhole N 4, for consistency in the
807 * comparisons with the actual image we need to put back the same correction
808 */
809 cpl_table* tab=NULL;
810 cpl_propertylist* header=NULL;
811 const char* name=cpl_frame_get_filename(resid_dan);
812 tab=cpl_table_load(name,1,0);
813 header=cpl_propertylist_load(name,0);
815 cpl_table_add_scalar(tab,XSH_RESID_TAB_TABLE_COLNAME_XTHPRE,-0.125);
816 cpl_table_add_scalar(tab,XSH_RESID_TAB_TABLE_COLNAME_XGAUSS,-0.125);
817 cpl_table_add_scalar(tab,XSH_RESID_TAB_TABLE_COLNAME_XTHANNEAL,-0.125);
819 cpl_table_add_scalar(tab,XSH_RESID_TAB_TABLE_COLNAME_XTHPRE,+0.51);
820 cpl_table_add_scalar(tab,XSH_RESID_TAB_TABLE_COLNAME_XGAUSS,+0.51);
821 cpl_table_add_scalar(tab,XSH_RESID_TAB_TABLE_COLNAME_XTHANNEAL,+0.51);
822 }
823
825
826 cpl_table_save(tab,header,NULL,name,CPL_IO_DEFAULT);
827 xsh_free_propertylist(&header);
828 xsh_free_table(&tab);
829 check(xsh_add_product_table( resid_dan, frameset,parameters, RECIPE_ID,
830 instrument,NULL));
831 /*
832 check(xsh_add_product_table( resid_map, frameset,parameters, RECIPE_ID,
833 instrument,NULL));
834 */
835 } else {
836 check(xsh_add_product_table( resid_map, frameset,parameters, RECIPE_ID,
837 instrument,NULL));
838
839 }
840
841 sprintf(tag,"%s_ON",XSH_FMTCHK);
843
844 check(xsh_add_product_image(predict_rmbias,frameset,parameters,
845 RECIPE_ID,instrument,tag));
846
847 } else {
848
849 check(xsh_add_product_image(predict_rmdark,frameset,parameters,
850 RECIPE_ID,instrument,tag));
851 }
852 if ( model_config_frame != NULL){
853 check(xsh_add_product_table(MODEL_CONF_OPT_frame,frameset,parameters,
854 RECIPE_ID, instrument,NULL));
855 }
856
857 xsh_msg("xsh_predict success !!");
858
859 cleanup:
860 xsh_end( RECIPE_ID, frameset, parameters );
861 XSH_FREE(detect_arclines_clipping);
862 XSH_FREE(detect_arclines_p);
863 xsh_free_frameset(&raws);
864 xsh_free_frameset(&calib);
866 xsh_free_frameset(&off);
867 xsh_free_frameset(&on_off);
868 xsh_free_frame(&guess_order_table);
869 xsh_free_frame(&clean_arclines);
870 xsh_free_frame(&resid_tab_orders_frame);
871 xsh_free_frame(&resid_map);
872 xsh_free_frame(&resid_dan);
873 xsh_free_frame(&guess_wavesol);
874 xsh_free_frame(&predict_rmbias);
875 xsh_free_frame(&predict_rmdark);
876 xsh_free_frame(&MODEL_CONF_OPT_frame);
877 xsh_free_frame(&bpmap);
878 xsh_free_propertylist(&plist);
879 // xsh_pre_free( &pre);
881 return;
882}
883
static double exptime
static xsh_instrument * instrument
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_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 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 XSH_ASSURE_NOT_ILLEGAL_MSG(cond, msg)
Definition: xsh_error.h:111
void xsh_instrument_set_mode(xsh_instrument *i, XSH_MODE mode)
Set a mode on instrument structure.
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(...)
Print a message on info level.
Definition: xsh_msg.h:121
double xsh_pfits_get_exptime(const cpl_propertylist *plist)
find out the exposure time
Definition: xsh_pfits.c:2254
static void xsh_predict(cpl_parameterlist *, cpl_frameset *)
Interpret the command line options and execute the data processing.
Definition: xsh_predict.c:452
#define RECIPE_CONTACT
Definition: xsh_predict.c:85
int cpl_plugin_get_info(cpl_pluginlist *list)
Build the list of available plugins, for this module.
Definition: xsh_predict.c:155
static int xsh_predict_create(cpl_plugin *)
Setup the recipe options.
Definition: xsh_predict.c:197
static char xsh_predict_description[]
Definition: xsh_predict.c:110
static int xsh_predict_destroy(cpl_plugin *)
Destroy what has been created by the 'create' function.
Definition: xsh_predict.c:417
static void xsh_predict_qc(cpl_table *tab, cpl_propertylist *head, const char *arm)
Definition: xsh_predict.c:328
#define RECIPE_ID
Definition: xsh_predict.c:83
#define RECIPE_AUTHOR
Definition: xsh_predict.c:84
static int xsh_predict_exec(cpl_plugin *)
Execute the plugin instance given by the interface.
Definition: xsh_predict.c:303
static char xsh_predict_description_short[]
Definition: xsh_predict.c:107
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
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_frame * xsh_create_order_table(cpl_frame *in_frame, cpl_frame *spectralformat_frame, cpl_frame *resid_frame, cpl_frame *arclines, xsh_detect_arclines_param *da, xsh_clipping_param *dac, xsh_instrument *instrument)
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.
@ XSH_ARM_UVB
@ XSH_ARM_NIR
@ XSH_MODE_SLIT
#define XSH_RESID_TAB_TABLE_COLNAME_ORDER
#define XSH_RESID_TAB_TABLE_COLNAME_XGAUSS
#define XSH_RESID_TAB_TABLE_COLNAME_WAVELENGTH
#define XSH_RESID_TAB_TABLE_COLNAME_YGAUSS
#define XSH_RESID_TAB_TABLE_COLNAME_XTHANNEAL
#define XSH_RESID_TAB_TABLE_COLNAME_XTHPRE
int order
Definition: xsh_detmon_lg.c:80
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_arc_line_list(cpl_frameset *frames, xsh_instrument *instr)
Find an arc line list frame.
Definition: xsh_dfs.c:3908
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_MOD_CFG_OPT_REC
Definition: xsh_dfs.h:1222
#define XSH_THEO_TAB_SING
Definition: xsh_dfs.h:1084
#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_FMTCHK
Definition: xsh_dfs.h:221
#define XSH_WAVE_TAB_GUESS
Definition: xsh_dfs.h:555
#define XSH_ORDER_TAB_RECOV
Definition: xsh_dfs.h:551
#define XSH_SPECTRAL_FORMAT
Definition: xsh_dfs.h:1196
@ XSH_SOLUTION_RELATIVE
Definition: xsh_drl.h:233
#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
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
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)
#define DECODE_BP_FLAG_DEF
@ XSH_GAUSSIAN_METHOD
#define XSH_FREE(POINTER)
Definition: xsh_utils.h:92