00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029 #ifdef HAVE_CONFIG_H
00030 #include <config.h>
00031 #endif
00032
00033
00044
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056 #include <xsh_error.h>
00057
00058 #include <xsh_utils.h>
00059 #include <xsh_msg.h>
00060
00061 #include <xsh_dfs.h>
00062
00063 #include <xsh_data_order.h>
00064 #include <xsh_drl.h>
00065 #include <xsh_model_kernel.h>
00066
00067 #include <cpl.h>
00068
00069
00070
00071
00072
00073 #define RECIPE_ID "xsh_predict"
00074 #define RECIPE_AUTHOR "P.Goldoni, L.Guglielmi, R. Haigron, F. Royer"
00075 #define RECIPE_CONTACT "amodigli@eso.org"
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085 static int xsh_predict_create(cpl_plugin *);
00086 static int xsh_predict_exec(cpl_plugin *);
00087 static int xsh_predict_destroy(cpl_plugin *);
00088
00089
00090 static void xsh_predict(cpl_parameterlist *, cpl_frameset *);
00091
00092
00093
00094
00095 static char xsh_predict_description_short[] =
00096 "Compute a first guess dispersion solution and order table";
00097
00098 static char xsh_predict_description[] =
00099 "This recipe creates a wavelength solution and an order table.\n\
00100 Input Frames :\n\
00101 - [UVB, VIS] One RAW frame (Format = RAW, Tag = FMTCHK_arm)\n\
00102 - [NIR] Two RAW frames ((Format = RAW, Tag = FMTCHK_arm_ON,\
00103 FMTCHK_arm_OFF)\n\
00104 - [OPTIONAL] A badpixel map (Format = QUP, Tag = BADPIXEL_MAP_arm)\n\
00105 - [poly mode]A theoretical map (Format = TABLE, Tag = THEO_TAB_SING_arm)\n\
00106 - [physical model mode]A model cfg table (Format = TABLE, Tag = XSH_MOD_CFG_TAB_arm)\n\
00107 - An arc line list (Format = TABLE, Tag = ARC_LINE_LIST_arm)\n\
00108 - [UVB,VIS] A master bias (Format = PRE, Tag = MASTER_BIAS_arm)\n\
00109 - [UVB,VIS] A master dark (Format = PRE, Tag = MASTER_DARK_arm)\n\
00110 Products : \n\
00111 - An updated clean arc line list, PRO.CATG=ARC_LINE_LIST_PREDICT_arm.\n\
00112 - A wavelength solution (Format = TABLE, PRO.CATG = \
00113 WAVE_TAB_GUESS_arm)\n\
00114 - A residual map, PRO.CATG = RESID_TAB_arm.\n\
00115 - An order table, PRO.CATG = ORDER_TABLE_GUESS_arm\n\
00116 (if at least degree+1 points are found in each order).\n \
00117 - A line identification residual table, PRO.CATG = FMTCHK_RESID_TAB_LINES_arm\n\
00118 - The bias subtracted formatcheck frame, PRO.CATG = FMTCHK_BIAS_SUBTRACTED_arm\n\
00119 Prepare the frames.\n\
00120 For UVB,VIS :\n\
00121 Subtract Master Bias.\n\
00122 Subtract Master Dark\n\
00123 For NIR:\n\
00124 Subtract ON OFF\n\
00125 Compute guess order table and wavelength solution\n";
00126
00127
00128
00129
00130
00139
00140
00141 int cpl_plugin_get_info(cpl_pluginlist *list) {
00142 cpl_recipe *recipe = NULL;
00143 cpl_plugin *plugin = NULL;
00144
00145 recipe = cpl_calloc(1, sizeof(*recipe));
00146 if ( recipe == NULL ){
00147 return -1;
00148 }
00149
00150 plugin = &recipe->interface ;
00151
00152 cpl_plugin_init(plugin,
00153 CPL_PLUGIN_API,
00154 XSH_BINARY_VERSION,
00155 CPL_PLUGIN_TYPE_RECIPE,
00156 RECIPE_ID,
00157 xsh_predict_description_short,
00158 xsh_predict_description,
00159 RECIPE_AUTHOR,
00160 RECIPE_CONTACT,
00161 xsh_get_license(),
00162 xsh_predict_create,
00163 xsh_predict_exec,
00164 xsh_predict_destroy);
00165
00166 cpl_pluginlist_append(list, plugin);
00167
00168 return (cpl_error_get_code() != CPL_ERROR_NONE);
00169 }
00170
00171
00181
00182
00183 static int xsh_predict_create(cpl_plugin *plugin){
00184 cpl_recipe *recipe = NULL;
00185 xsh_clipping_param detarc_clip_param = { 2.0, 10, 0.7, 0, 0.3};
00186 xsh_detect_arclines_param detarc_param = {4, 2, 0, 5, 5, 0, 2, 5.0,
00187 XSH_GAUSSIAN_METHOD, FALSE};
00188 char paramname[256];
00189 cpl_parameter* p=NULL;
00190
00191
00192 xsh_init();
00193
00194
00195 assure( plugin != NULL, CPL_ERROR_NULL_INPUT, "Null plugin");
00196
00197
00198 assure( cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE,
00199 CPL_ERROR_TYPE_MISMATCH,
00200 "Plugin is not a recipe");
00201
00202 recipe = (cpl_recipe *)plugin;
00203
00204
00205 recipe->parameters = cpl_parameterlist_new();
00206 assure( recipe->parameters != NULL,
00207 CPL_ERROR_ILLEGAL_OUTPUT,
00208 "Memory allocation failed!");
00209
00210
00211 check( xsh_parameters_generic( RECIPE_ID, recipe->parameters ) ) ;
00212 check( xsh_parameters_pre_overscan( RECIPE_ID, recipe->parameters ) ) ;
00213
00214
00215
00216 check(xsh_parameters_detect_arclines_create(RECIPE_ID,recipe->parameters,
00217 detarc_param));
00218 check(xsh_parameters_clipping_detect_arclines_create(RECIPE_ID,
00219 recipe->parameters, detarc_clip_param));
00220
00221
00222
00223
00224 check( xsh_parameters_new_int( recipe->parameters, RECIPE_ID,
00225 "model-maxit",1000,
00226 "Number/10 of annealing iterations "
00227 "if in physical model mode."));
00228
00229 check( xsh_parameters_new_double( recipe->parameters, RECIPE_ID,
00230 "model-anneal-factor",1.0,
00231 "Multiplier applied to the automatic "
00232 "parameter ranges (i.e. when scenario!=0). "
00233 "For routine operations should be 1.0. "
00234 "(physical model mode)."));
00235
00236
00237 sprintf(paramname,"xsh.%s.%s",RECIPE_ID,"model-scenario");
00238
00239
00240 check(p=cpl_parameter_new_enum(paramname,CPL_TYPE_INT,
00241 "selects preset flag and range combinations "
00242 "appropriate to common scenarios: \n"
00243
00244
00245 " 0 - No scenario, input cfg flags and limits "
00246 "used.\n"
00247 " 1 - scenario appropriate for the startup "
00248 "recipe (large ranges for parameters "
00249 "affecting single ph exposures, dist "
00250 "coeff fixed).\n"
00251 " 2 - Like 1, but includes parameters "
00252 "affecting all ph positions.\n"
00253 " 3 - Scenario for use in fine tuning cfg "
00254 "to match routine single pinhole exposures. "
00255 "All parameters affecting 1ph exposures "
00256 "except dist coeffs are included and "
00257 "parameter ranges are small. (For use by "
00258 "predict in 1ph case).\n"
00259 " 4 - Like 3 but includes parameters "
00260 "affecting all ph positions (Standard for "
00261 "use by predict in 9ph case and 2dmap). \n"
00262
00263
00264 ,RECIPE_ID,3,9,-1,0,1,2,3,4,5,6,8));
00265
00266
00267 check(cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI,
00268 "model-scenario"));
00269 check(cpl_parameterlist_append(recipe->parameters,p));
00270
00271
00272 cleanup:
00273 if ( cpl_error_get_code() != CPL_ERROR_NONE ){
00274 xsh_error_dump(CPL_MSG_ERROR);
00275 return 1;
00276 }
00277 else {
00278 return 0;
00279 }
00280 }
00281
00282
00288
00289
00290 static int xsh_predict_exec(cpl_plugin *plugin) {
00291 cpl_recipe *recipe = NULL;
00292
00293
00294 assure( plugin != NULL, CPL_ERROR_NULL_INPUT, "Null plugin" );
00295
00296
00297 assure( cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE,
00298 CPL_ERROR_TYPE_MISMATCH, "Plugin is not a recipe");
00299
00300 recipe = (cpl_recipe *)plugin;
00301
00302
00303 xsh_predict(recipe->parameters, recipe->frames);
00304
00305 cleanup:
00306 if ( cpl_error_get_code() != CPL_ERROR_NONE ) {
00307 xsh_error_dump(CPL_MSG_ERROR);
00308 cpl_error_reset();
00309 return 1;
00310 }
00311 else {
00312 return 0;
00313 }
00314 }
00315
00316
00322
00323 static int xsh_predict_destroy(cpl_plugin *plugin)
00324 {
00325 cpl_recipe *recipe = NULL;
00326
00327
00328 assure( plugin != NULL, CPL_ERROR_NULL_INPUT, "Null plugin" );
00329
00330
00331 assure( cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE,
00332 CPL_ERROR_TYPE_MISMATCH, "Plugin is not a recipe");
00333
00334 recipe = (cpl_recipe *)plugin;
00335
00336 xsh_free_parameterlist(&recipe->parameters);
00337
00338 cleanup:
00339 if (cpl_error_get_code() != CPL_ERROR_NONE)
00340 {
00341 return 1;
00342 }
00343 else
00344 {
00345 return 0;
00346 }
00347 }
00348
00349 static cpl_error_code
00350 xsh_params_set_defaults(cpl_parameterlist* pars,
00351 xsh_instrument* inst,
00352 xsh_detect_arclines_param* detect_arclines_p)
00353 {
00354 cpl_parameter* p=NULL;
00355
00356 if (xsh_instrument_get_arm(inst) == XSH_ARM_NIR){
00357 check(p=xsh_parameters_find(pars,RECIPE_ID,"detectarclines-min-sn"));
00358 if(xsh_parameter_get_default_flag(p) == 0) {
00359
00360 detect_arclines_p->min_sn=4;
00361 }
00362 } else if (xsh_instrument_get_arm(inst) == XSH_ARM_UVB){
00363 check(p=xsh_parameters_find(pars,RECIPE_ID,
00364 "detectarclines-fit-window-half-size"));
00365 if(xsh_parameter_get_default_flag(p) == 0) {
00366
00367 detect_arclines_p->min_sn=5.0;
00368 }
00369 }
00370 cleanup:
00371
00372 return cpl_error_get_code();
00373
00374 }
00375
00383
00384 static void xsh_predict(cpl_parameterlist* parameters, cpl_frameset* frameset)
00385 {
00386 const char* recipe_tags[1] = {XSH_FMTCHK};
00387 int recipe_tags_size = 1;
00388
00389
00390 xsh_clipping_param* detect_arclines_clipping = NULL;
00391 xsh_detect_arclines_param* detect_arclines_p = NULL;
00392
00393
00394 cpl_frameset* raws = NULL;
00395 cpl_frameset* calib = NULL;
00396 cpl_frameset* on = NULL;
00397 cpl_frameset* off = NULL;
00398 cpl_frameset* on_off = NULL;
00399 cpl_frame* predict_rmbias = NULL;
00400 cpl_frame* predict_rmdark = NULL;
00401 xsh_instrument* instrument = NULL;
00402
00403 cpl_frame* model_config_frame = NULL;
00404 cpl_frame* spectralformat_frame = NULL;
00405 cpl_frame* bpmap = NULL;
00406 cpl_frame* master_bias = NULL;
00407 cpl_frame* master_dark = NULL;
00408 cpl_frame* theo_tab_sing = NULL;
00409 cpl_frame* resid_map = NULL;
00410 cpl_frame* resid_tab_orders_frame = NULL;
00411 cpl_frame *wave_tab_guess_frame = NULL;
00412 cpl_frame *order_tab_recov_frame = NULL;
00413 cpl_frame* arclines = NULL;
00414 cpl_frame* clean_arclines = NULL;
00415 cpl_frame* guess_order_table = NULL;
00416 cpl_frame* guess_wavesol = NULL;
00417 const char* filename=NULL;
00418 cpl_frame* MODEL_CONF_OPT_frame=NULL;
00419
00420 XSH_INSTRCONFIG * config = NULL;
00421 int maxit=200;
00422 double ann_fac=1.0;
00423 int scenario=3;
00424
00425 char paramname[256];
00426 cpl_parameter * p =NULL;
00427 int pre_overscan_corr=0;
00428
00429
00430
00431
00432 check( xsh_begin( frameset, parameters, &instrument, &raws, &calib,
00433 recipe_tags, recipe_tags_size,
00434 RECIPE_ID, XSH_BINARY_VERSION,
00435 xsh_predict_description_short ) ) ;
00436 check( xsh_instrument_set_mode( instrument, XSH_MODE_SLIT));
00437
00438
00439
00440
00441
00442 bpmap = xsh_find_master_bpmap( calib);
00443 check( arclines = xsh_find_arc_line_list( calib, instrument));
00444 check( spectralformat_frame = xsh_find_frame_with_tag( calib,
00445 XSH_SPECTRAL_FORMAT, instrument));
00446
00447
00448
00449
00450 theo_tab_sing = xsh_find_frame_with_tag( calib, XSH_THEO_TAB_SING,
00451 instrument);
00452 xsh_error_reset();
00453
00454 if( NULL == (model_config_frame = xsh_find_frame_with_tag( calib, XSH_MOD_CFG_TAB,
00455 instrument))){
00456
00457
00458 model_config_frame=xsh_find_frame_with_tag( calib, XSH_MOD_CFG_OPT_REC,
00459 instrument);
00460 }
00461
00462
00463 xsh_error_reset();
00464
00465 wave_tab_guess_frame = xsh_find_frame_with_tag( calib, XSH_WAVE_TAB_GUESS,
00466 instrument);
00467 xsh_error_reset();
00468
00469 order_tab_recov_frame = xsh_find_frame_with_tag( calib, XSH_ORDER_TAB_RECOV,
00470 instrument);
00471 xsh_error_reset();
00472
00473 if ( xsh_instrument_get_arm(instrument) != XSH_ARM_NIR){
00474
00475 XSH_ASSURE_NOT_ILLEGAL(cpl_frameset_get_size(raws) == 1);
00476
00477 if((master_bias = xsh_find_frame_with_tag(calib,XSH_MASTER_BIAS,
00478 instrument)) == NULL) {
00479
00480 xsh_msg_warning("Frame %s not provided",XSH_MASTER_BIAS);
00481 xsh_error_reset();
00482 }
00483
00484
00485 if((master_dark = xsh_find_frame_with_tag(calib,XSH_MASTER_DARK,
00486 instrument)) == NULL){
00487 xsh_msg_warning("Frame %s not provided",XSH_MASTER_DARK);
00488 xsh_error_reset();
00489 }
00490 }
00491
00492 else {
00493
00494 check(xsh_dfs_split_nir(raws,&on,&off));
00495 XSH_ASSURE_NOT_ILLEGAL(cpl_frameset_get_size(on) == 1);
00496
00497 }
00498 check( xsh_instrument_update_from_spectralformat( instrument,
00499 spectralformat_frame));
00500 check( config = xsh_instrument_get_config( instrument));
00501
00502
00503
00504 check(detect_arclines_clipping =
00505 xsh_parameters_clipping_detect_arclines_get(RECIPE_ID, parameters));
00506 check(detect_arclines_p = xsh_parameters_detect_arclines_get(RECIPE_ID,
00507 parameters));
00508 check( pre_overscan_corr = xsh_parameters_get_int( parameters, RECIPE_ID,
00509 "pre-overscan-corr"));
00510
00511 check(xsh_params_set_defaults(parameters,instrument,detect_arclines_p));
00512
00513
00514
00515
00516
00517
00518 if ( xsh_instrument_get_arm(instrument) != XSH_ARM_NIR){
00519 cpl_frame* fmtchk = NULL;
00520
00521
00522 check(xsh_prepare( raws, bpmap, master_bias, XSH_FMTCHK, instrument,
00523 pre_overscan_corr));
00524 check(fmtchk = cpl_frameset_get_first(raws));
00525
00526 if(master_bias != NULL) {
00527
00528 check(predict_rmbias = xsh_subtract_bias(fmtchk,master_bias,
00529 instrument,"FMTCHK_",
00530 pre_overscan_corr));
00531 } else {
00532 predict_rmbias =cpl_frame_duplicate(fmtchk);
00533 }
00534
00535 if(master_dark != NULL) {
00536
00537 filename = xsh_stringcat_any( "FMTCHK_DARK_",
00538 xsh_instrument_arm_tostring( instrument ),
00539 ".fits", NULL ) ;
00540
00541
00542 check(predict_rmdark = xsh_subtract_dark(predict_rmbias, master_dark,
00543 filename, instrument));
00544 } else {
00545 predict_rmdark =cpl_frame_duplicate(predict_rmbias);
00546 }
00547
00548 }
00549
00550 else{
00551
00552 check(xsh_prepare(on,bpmap, NULL, "ON", instrument,pre_overscan_corr));
00553
00554 check(xsh_prepare(off,bpmap, NULL, "OFF", instrument,pre_overscan_corr));
00555
00556
00557 check(on_off = xsh_subtract_nir_on_off(on, off, instrument));
00558 check(predict_rmdark = cpl_frame_duplicate(
00559 cpl_frameset_get_first(on_off)));
00560 }
00561
00562 if ( xsh_instrument_get_arm(instrument) != XSH_ARM_NIR){
00563 check(xsh_check_input_is_unbinned(predict_rmdark));
00564 }
00565
00566 xsh_msg("Calling the xsh_detect_arclines");
00567
00568
00569 check( xsh_detect_arclines( predict_rmdark, theo_tab_sing,
00570 arclines, wave_tab_guess_frame,
00571 order_tab_recov_frame,
00572 model_config_frame,
00573 spectralformat_frame,
00574 &resid_tab_orders_frame,
00575 &clean_arclines, &guess_wavesol,
00576 &resid_map, XSH_SOLUTION_RELATIVE,
00577 detect_arclines_p,
00578 detect_arclines_clipping,
00579 instrument,RECIPE_ID));
00580
00581 if ( model_config_frame != NULL){
00582 xsh_msg("Produce new config file");
00583
00584
00585
00586
00587
00588
00589
00590
00591
00592
00593
00594
00595
00596
00597
00598
00599 sprintf(paramname,"xsh.%s.%s",RECIPE_ID, "model-maxit");
00600 check(p = cpl_parameterlist_find(parameters,paramname));
00601 check(maxit=cpl_parameter_get_int(p));
00602
00603 sprintf(paramname,"xsh.%s.%s",RECIPE_ID, "model-anneal-factor");
00604 check(p = cpl_parameterlist_find(parameters,paramname));
00605 check(ann_fac=cpl_parameter_get_double(p));
00606
00607 sprintf(paramname,"xsh.%s.%s",RECIPE_ID, "model-scenario");
00608 check(p = cpl_parameterlist_find(parameters,paramname));
00609 check(scenario=cpl_parameter_get_int(p));
00610 xsh_msg("maxit=%d ann_fac=%g scenario=%d",maxit,ann_fac,scenario);
00611
00612 check(MODEL_CONF_OPT_frame=xsh_model_pipe_anneal( model_config_frame, resid_map,
00613 maxit,ann_fac,scenario,1));
00614
00615 }
00616 check( guess_order_table=xsh_create_order_table(predict_rmdark,
00617 spectralformat_frame,
00618 resid_tab_orders_frame,
00619 clean_arclines,
00620 detect_arclines_p,
00621 detect_arclines_clipping,
00622 instrument));
00623
00624
00625
00626 xsh_msg("Saving products");
00627
00628 check(xsh_add_product_table( clean_arclines, frameset,
00629 parameters, RECIPE_ID, instrument));
00630
00631 if ( guess_wavesol != NULL){
00632 check(xsh_add_product_table( guess_wavesol, frameset,
00633 parameters, RECIPE_ID, instrument));
00634 }
00635 if ( guess_order_table != NULL){
00636 check(xsh_add_product_table_multi( guess_order_table, frameset,
00637 parameters, RECIPE_ID, instrument));
00638 }
00639 if(model_config_frame == NULL) {
00640 check(xsh_wavetab_qc(resid_map,true));
00641 } else {
00642 check(xsh_wavetab_qc(resid_map,false));
00643 }
00644 check(xsh_add_product_table_multi( resid_map, frameset,
00645 parameters, RECIPE_ID, instrument));
00646 if ( xsh_instrument_get_arm(instrument) != XSH_ARM_NIR){
00647 check(xsh_add_product_pre(predict_rmbias,frameset,parameters,
00648 RECIPE_ID,instrument));
00649
00650 } else {
00651 check(xsh_add_product_pre(predict_rmdark,frameset,parameters,
00652 RECIPE_ID,instrument));
00653 }
00654 if ( model_config_frame != NULL){
00655 check(xsh_add_product_table_multi(MODEL_CONF_OPT_frame,frameset,
00656 parameters, RECIPE_ID, instrument));
00657 }
00658
00659 xsh_msg("xsh_predict success !!");
00660
00661 cleanup:
00662 xsh_end( RECIPE_ID, frameset, parameters );
00663 XSH_FREE(detect_arclines_clipping);
00664 XSH_FREE(detect_arclines_p);
00665 xsh_free_frameset(&raws);
00666 xsh_free_frameset(&calib);
00667 xsh_free_frameset(&on);
00668 xsh_free_frameset(&off);
00669 xsh_free_frameset(&on_off);
00670 xsh_free_frame(&guess_order_table);
00671 xsh_free_frame(&clean_arclines);
00672 xsh_free_frame(&resid_tab_orders_frame);
00673 xsh_free_frame(&resid_map);
00674 xsh_free_frame(&guess_wavesol);
00675 xsh_free_frame(&predict_rmbias);
00676 xsh_free_frame(&predict_rmdark);
00677 xsh_free_frame(&MODEL_CONF_OPT_frame);
00678
00679 xsh_instrument_free(&instrument);
00680 return;
00681 }
00682