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 #ifdef HAVE_CONFIG_H
00027 #include <config.h>
00028 #endif
00029
00030
00038
00041
00042
00043
00044
00045
00046
00047
00048 #include <xsh_error.h>
00049
00050 #include <xsh_pfits_qc.h>
00051 #include <xsh_utils.h>
00052 #include <xsh_msg.h>
00053
00054 #include <xsh_dfs.h>
00055
00056 #include <xsh_drl.h>
00057 #include <xsh_drl_check.h>
00058 #include <xsh_data_instrument.h>
00059 #include <xsh_model_arm_constants.h>
00060
00061
00062 #include <cpl.h>
00063
00064
00065
00066
00067
00068 #define RECIPE_ID "xsh_orderpos"
00069 #define RECIPE_AUTHOR "L.Guglielmi,R.Haigron,P.Goldoni,F.Royer"
00070 #define RECIPE_CONTACT "amodigli@eso.org"
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080 static int xsh_orderpos_create(cpl_plugin *);
00081 static int xsh_orderpos_exec(cpl_plugin *);
00082 static int xsh_orderpos_destroy(cpl_plugin *);
00083
00084
00085 static void xsh_orderpos(cpl_parameterlist *, cpl_frameset *);
00086
00087
00088
00089
00090 static char xsh_orderpos_description_short[] =
00091 "Create the orders centre traces table file";
00092
00093 static char xsh_orderpos_description[] =
00094 "This recipe creates the orders centre traces table.\n\
00095 Input Frames for UVB and VIS:\n\
00096 Raw file (Tag = ORDERDEF_arm_D2)\n\
00097 Master Dark (Tag = MASTER_DARK_arm)\n\
00098 Master Bias (Tag = MASTER_BIAS_arm)\n\
00099 Input Frames for NIR:\n\
00100 Raw file ON(Tag = ORDERDEF_NIR_ON)\n\
00101 Raw file OFF(Tag = ORDERDEF_NIR_OFF)\n\
00102 Input Frames for all arms\n\
00103 Guess order table (Tag = ORDER_TAB_GUESS_arm)\n\
00104 Spectral format table (Tag = SPECTRAL_FORMAT_TAB_arm)\n\
00105 [OPTIONAL] Bad Pixel Map (Tag = BADPIXEL_MAP_arm)\n\
00106 Prepare PRE structures.\n\
00107 For NIR, subtract NIR-OFF from NIR-ON.\n\
00108 For UVB and NIR, Substract the master Bias and master dark.\n\
00109 Detect Orders and calculate the order table.\n\
00110 The final products are:\n\
00111 An updated Order Table, PRO.CATG=ORDER_TABLE_CENTR_arm.\n\
00112 A order trace residuals Table, PRO.CATG=ORDERPOS_RESID_TAB_arm.\n\
00113 The order pos frame bias subtracted, PRO.CATG=ORDERDEF_BIAS_SUBTRACTED_arm.\n";
00114
00115
00116
00117
00118
00119
00128
00129
00130 int cpl_plugin_get_info(cpl_pluginlist *list) {
00131 cpl_recipe *recipe = NULL;
00132 cpl_plugin *plugin = NULL;
00133
00134 recipe = cpl_calloc(1, sizeof(*recipe));
00135 if ( recipe == NULL ){
00136 return -1;
00137 }
00138
00139 plugin = &recipe->interface ;
00140
00141 cpl_plugin_init(plugin,
00142 CPL_PLUGIN_API,
00143 XSH_BINARY_VERSION,
00144 CPL_PLUGIN_TYPE_RECIPE,
00145 RECIPE_ID,
00146 xsh_orderpos_description_short,
00147 xsh_orderpos_description,
00148 RECIPE_AUTHOR,
00149 RECIPE_CONTACT,
00150 xsh_get_license(),
00151 xsh_orderpos_create,
00152 xsh_orderpos_exec,
00153 xsh_orderpos_destroy);
00154
00155 cpl_pluginlist_append(list, plugin);
00156
00157 return (cpl_error_get_code() != CPL_ERROR_NONE);
00158 }
00159
00160
00170
00171
00172 static int xsh_orderpos_create(cpl_plugin *plugin){
00173 cpl_recipe *recipe = NULL;
00174 xsh_detect_continuum_param param = { -1., 2, 5,
00175 DETECT_CONTINUUM_POLYNOMIAL_DEGREE,
00176 1, 0.,
00177 20, 50, 140., 2., 0 } ;
00178
00179
00180 xsh_init();
00181
00182
00183 assure( plugin != NULL, CPL_ERROR_NULL_INPUT, "Null plugin");
00184
00185
00186 assure( cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE,
00187 CPL_ERROR_TYPE_MISMATCH,
00188 "Plugin is not a recipe");
00189
00190 recipe = (cpl_recipe *)plugin;
00191
00192
00193 recipe->parameters = cpl_parameterlist_new();
00194 assure( recipe->parameters != NULL,
00195 CPL_ERROR_ILLEGAL_OUTPUT,
00196 "Memory allocation failed!");
00197
00198
00199 check( xsh_parameters_generic( RECIPE_ID, recipe->parameters ) ) ;
00200 check( xsh_parameters_pre_overscan( RECIPE_ID, recipe->parameters ) ) ;
00201
00202 check(xsh_parameters_detect_continuum_create(RECIPE_ID,
00203 recipe->parameters,
00204 param));
00205 check( xsh_parameters_clipping_dcn_create( RECIPE_ID,
00206 recipe->parameters ) ) ;
00207 cleanup:
00208 if ( cpl_error_get_code() != CPL_ERROR_NONE ){
00209 xsh_error_dump(CPL_MSG_ERROR);
00210 return 1;
00211 }
00212 else {
00213 return 0;
00214 }
00215 }
00216
00217
00223
00224
00225 static int xsh_orderpos_exec(cpl_plugin *plugin) {
00226 cpl_recipe *recipe = NULL;
00227
00228
00229 assure( plugin != NULL, CPL_ERROR_NULL_INPUT, "Null plugin" );
00230
00231
00232 assure( cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE,
00233 CPL_ERROR_TYPE_MISMATCH, "Plugin is not a recipe");
00234
00235 recipe = (cpl_recipe *)plugin;
00236
00237
00238 xsh_orderpos(recipe->parameters, recipe->frames);
00239
00240
00241 cleanup:
00242 if ( cpl_error_get_code() != CPL_ERROR_NONE ) {
00243 xsh_error_dump(CPL_MSG_ERROR);
00244
00245 cpl_error_reset();
00246 return 1;
00247 }
00248 else {
00249 return 0;
00250 }
00251 }
00252
00253
00259
00260 static int xsh_orderpos_destroy(cpl_plugin *plugin)
00261 {
00262 cpl_recipe *recipe = NULL;
00263
00264
00265 assure( plugin != NULL, CPL_ERROR_NULL_INPUT, "Null plugin" );
00266
00267
00268 assure( cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE,
00269 CPL_ERROR_TYPE_MISMATCH, "Plugin is not a recipe");
00270
00271 recipe = (cpl_recipe *)plugin;
00272
00273 xsh_free_parameterlist(&recipe->parameters);
00274
00275 cleanup:
00276 if (cpl_error_get_code() != CPL_ERROR_NONE)
00277 {
00278 return 1;
00279 }
00280 else
00281 {
00282 return 0;
00283 }
00284 }
00285
00286 static cpl_error_code
00287 xsh_params_set_defaults(cpl_parameterlist* pars,
00288 xsh_instrument* inst,
00289 xsh_detect_continuum_param* det_order,
00290 xsh_clipping_param* clip)
00291 {
00292
00293 cpl_parameter* p=NULL;
00294
00295 check(p=xsh_parameters_find(pars,RECIPE_ID,
00296 "detectcontinuum-search-window-half-size"));
00297 if(cpl_parameter_get_int(p) <= 0) {
00298 if (xsh_instrument_get_arm(inst) == XSH_ARM_NIR){
00299 det_order->search_window=2;
00300 }
00301 else {
00302 det_order->search_window=10;
00303 }
00304 }
00305
00306
00307
00308
00309
00310
00311
00312
00313
00314
00315 check(p=xsh_parameters_find(pars,RECIPE_ID,
00316 "detectcontinuum-clip-res-max"));
00317 if(cpl_parameter_get_double(p) <= 0) {
00318 if (xsh_instrument_get_arm(inst) == XSH_ARM_NIR){
00319 check(clip->res_max=0.4);
00320 } else {
00321 check(clip->res_max=0.3);
00322 }
00323 }
00324
00325
00326 cleanup:
00327
00328 return cpl_error_get_code();
00329
00330 }
00331
00332
00340
00341 static void xsh_orderpos(cpl_parameterlist* parameters,
00342 cpl_frameset* frameset)
00343 {
00344 const char* recipe_tags[1] = {XSH_ORDERDEF};
00345 int recipe_tags_size = 1;
00346
00347
00348 cpl_frameset* raws = NULL;
00349 cpl_frameset* calib = NULL;
00350 cpl_frame* bpmap = NULL;
00351 cpl_frame* master_bias = NULL;
00352 cpl_frame* master_dark = NULL;
00353 cpl_frame * order_tab_guess = NULL ;
00354 cpl_frame * orderframe = NULL ;
00355 cpl_frame * spectralformat_frame = NULL ;
00356 cpl_frame * rmbias = NULL ;
00357 cpl_frame * intFrame = NULL ;
00358 cpl_frame * nir_on = NULL ;
00359 cpl_frame * nir_off = NULL ;
00361
00362 cpl_frame * resFrame = NULL ;
00363
00364
00365 xsh_detect_continuum_param * detect_param = NULL ;
00366 xsh_instrument* instrument = NULL;
00367 xsh_clipping_param * dcn_clipping_param = NULL;
00368 cpl_frame* resid_tab=NULL;
00369 char tag[128];
00370 char fname[128] ;
00371 int pre_overscan_corr=0;
00372
00373
00374
00375
00376 check( xsh_begin( frameset, parameters, &instrument, &raws, &calib,
00377 recipe_tags, recipe_tags_size,
00378 RECIPE_ID, XSH_BINARY_VERSION,
00379 xsh_orderpos_description_short ) ) ;
00380 check( xsh_instrument_update_lamp( instrument, XSH_LAMP_UNDEFINED));
00381
00382 xsh_recipe_params_check(parameters,instrument,RECIPE_ID);
00383
00384
00385
00386 check( spectralformat_frame = xsh_find_frame_with_tag( calib,
00387 XSH_SPECTRAL_FORMAT, instrument));
00388
00389 check(bpmap=xsh_check_load_master_bpmap(calib,instrument,RECIPE_ID));
00390 if ( xsh_instrument_get_arm(instrument) == XSH_ARM_NIR ) {
00391 check( nir_on = xsh_find_raw_orderdef_nir ( raws ) ) ;
00392 check( nir_off = xsh_find_raw_orderdef_nir_off ( raws ) ) ;
00393 }
00394 else {
00395 check( orderframe = xsh_find_raw_orderdef_vis_uvb( raws ) ) ;
00396 if((master_bias = xsh_find_frame_with_tag(calib,XSH_MASTER_BIAS,
00397 instrument)) == NULL) {
00398
00399 xsh_msg_warning("Frame %s not provided",XSH_MASTER_BIAS);
00400 xsh_error_reset();
00401 }
00402
00403
00404 if((master_dark = xsh_find_frame_with_tag(calib,XSH_MASTER_DARK,
00405 instrument)) == NULL){
00406 xsh_msg_warning("Frame %s not provided",XSH_MASTER_DARK);
00407 xsh_error_reset();
00408 }
00409 }
00410
00411
00412 if(NULL== (order_tab_guess = xsh_find_frame_with_tag(calib,
00413 XSH_ORDER_TAB_GUESS,
00414 instrument)) ) {
00415 xsh_msg_error("you must provide an input %s_%s table",XSH_ORDER_TAB_GUESS,
00416 xsh_instrument_arm_tostring(instrument));
00417 goto cleanup;
00418 }
00419
00420
00421 check( xsh_instrument_update_from_spectralformat( instrument,
00422 spectralformat_frame));
00423
00424
00425
00426
00427 check( pre_overscan_corr = xsh_parameters_get_int( parameters, RECIPE_ID,
00428 "pre-overscan-corr"));
00429
00430 check(detect_param=xsh_parameters_detect_continuum_get( RECIPE_ID,
00431 parameters)) ;
00432
00433
00434 xsh_msg_dbg_low("Search Window: %d, Running Window: %d, Fit Window: %d",
00435 detect_param->search_window, detect_param->running_window,
00436 detect_param->fit_window ) ;
00437 xsh_msg_dbg_low( "Polynomial degree: %d, Step: %d", detect_param->poly_degree,
00438 detect_param->poly_step ) ;
00439
00440 check( dcn_clipping_param = xsh_parameters_clipping_dcn_get( RECIPE_ID,
00441 parameters ));
00442
00443
00444 check(xsh_params_set_defaults(parameters,instrument,detect_param,
00445 dcn_clipping_param));
00446
00447
00448
00449
00450
00451 xsh_msg( "Working on Arm %s", xsh_instrument_arm_tostring(instrument ) ) ;
00452
00453 check(xsh_prepare(raws, bpmap, master_bias, XSH_ORDERDEF, instrument,pre_overscan_corr));
00454
00455
00456
00457
00458
00459 if ( xsh_instrument_get_arm(instrument) == XSH_ARM_NIR ) {
00460
00461
00462 check(intFrame=xsh_pre_frame_subtract( nir_on, nir_off,
00463 "ON-OFF_NIR.fits",instrument,1 ) ) ;
00464 }
00465 else {
00466
00467 if(master_bias!= NULL) {
00468
00469 xsh_msg( "Substract bias" ) ;
00470 check(rmbias = xsh_subtract_bias( orderframe, master_bias, instrument,"ORDERDEF_",pre_overscan_corr,0));
00471 } else {
00472 rmbias=cpl_frame_duplicate(orderframe);
00473 }
00474
00475 if(master_dark!= NULL) {
00476 xsh_msg( "Substract dark" ) ;
00477 sprintf( fname, "ORDERPOS_%s_DARK.fits",
00478 xsh_instrument_arm_tostring(instrument ) ) ;
00479 check(intFrame = xsh_subtract_dark(rmbias, master_dark,
00480 fname, instrument));
00481 } else {
00482 intFrame=cpl_frame_duplicate(rmbias);
00483 }
00484 }
00485
00486 if ( xsh_instrument_get_arm(instrument) != XSH_ARM_NIR){
00487 check(xsh_check_input_is_unbinned(intFrame));
00488 }
00489
00490
00491
00492
00493
00494
00495
00496
00497
00498 xsh_msg("Calling detect continuum" ) ;
00499
00500 check_msg( resFrame = xsh_detect_continuum( intFrame, order_tab_guess,
00501 spectralformat_frame,
00502 detect_param,
00503 dcn_clipping_param,
00504 instrument, &resid_tab ),
00505 "Error in xsh_detect_continuum, try to increase detectcontinuum-fit-window-half-size or detectcontinuum-ordertab-deg-y or detectcontinuum-clip-sigma" ) ;
00506
00507 check(xsh_monitor_flux( intFrame, resFrame, instrument));
00508
00509
00510
00511 xsh_msg( "Save Order Table product" ) ;
00512 check(xsh_add_product_table( resFrame, frameset, parameters, RECIPE_ID,
00513 instrument,NULL));
00514
00515 check(xsh_add_product_table(resid_tab, frameset, parameters, RECIPE_ID,
00516 instrument,NULL));
00517
00518 if ( xsh_instrument_get_arm(instrument) == XSH_ARM_NIR ) {
00519 sprintf(tag,"ORDERDEF_ON");
00520 check(xsh_add_product_image(intFrame,frameset,parameters,RECIPE_ID,instrument,tag));
00521 } else {
00522 check(xsh_add_product_image(rmbias,frameset,parameters,RECIPE_ID,instrument,NULL));
00523 }
00524 cleanup:
00525 xsh_end( RECIPE_ID, frameset, parameters );
00526 XSH_FREE( dcn_clipping_param );
00527 XSH_FREE( detect_param );
00528 xsh_instrument_free(&instrument );
00529 xsh_free_frameset(&raws);
00530 xsh_free_frameset(&calib);
00531 xsh_free_frame( &resFrame) ;
00532 xsh_free_frame( &resid_tab) ;
00533 xsh_free_frame(&rmbias);
00534 xsh_free_frame(&bpmap);
00535 xsh_free_frame(&intFrame);
00536 return;
00537 }
00538