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, A. Modigliani"
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] A map of non linear bad pixels (Format=QUP, Tag = BP_MAP_NL_arm)\n\
00106 - [OPTIONAL] A map of reference bad pixels (Format = QUP,RAW, Tag = BP_MAP_RP_arm)\n\
00107 Prepare PRE structures.\n\
00108 For NIR, subtract NIR-OFF from NIR-ON.\n\
00109 For UVB and NIR, Substract the master Bias and master dark.\n\
00110 Detect Orders and calculate the order table.\n\
00111 The final products are:\n\
00112 An updated Order Table, PRO.CATG=ORDER_TABLE_CENTR_arm.\n\
00113 A order trace residuals Table, PRO.CATG=ORDERPOS_RESID_TAB_arm.\n\
00114 The order pos frame bias subtracted, PRO.CATG=ORDERDEF_ON_arm.\n";
00115
00116
00117
00118
00119
00120
00129
00130
00131 int cpl_plugin_get_info(cpl_pluginlist *list) {
00132 cpl_recipe *recipe = NULL;
00133 cpl_plugin *plugin = NULL;
00134
00135 recipe = cpl_calloc(1, sizeof(*recipe));
00136 if ( recipe == NULL ){
00137 return -1;
00138 }
00139
00140 plugin = &recipe->interface ;
00141
00142 cpl_plugin_init(plugin,
00143 CPL_PLUGIN_API,
00144 XSH_BINARY_VERSION,
00145 CPL_PLUGIN_TYPE_RECIPE,
00146 RECIPE_ID,
00147 xsh_orderpos_description_short,
00148 xsh_orderpos_description,
00149 RECIPE_AUTHOR,
00150 RECIPE_CONTACT,
00151 xsh_get_license(),
00152 xsh_orderpos_create,
00153 xsh_orderpos_exec,
00154 xsh_orderpos_destroy);
00155
00156 cpl_pluginlist_append(list, plugin);
00157
00158 return (cpl_error_get_code() != CPL_ERROR_NONE);
00159 }
00160
00161
00171
00172
00173 static int xsh_orderpos_create(cpl_plugin *plugin){
00174 cpl_recipe *recipe = NULL;
00175 xsh_detect_continuum_param param = { 5, 0, 5,
00176 DETECT_CONTINUUM_POLYNOMIAL_DEGREE,
00177 1, 0.,
00178 20, 50, 140., 2., 0 } ;
00179
00180
00181 xsh_init();
00182
00183
00184 assure( plugin != NULL, CPL_ERROR_NULL_INPUT, "Null plugin");
00185
00186
00187 assure( cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE,
00188 CPL_ERROR_TYPE_MISMATCH,
00189 "Plugin is not a recipe");
00190
00191 recipe = (cpl_recipe *)plugin;
00192
00193
00194 recipe->parameters = cpl_parameterlist_new();
00195 assure( recipe->parameters != NULL,
00196 CPL_ERROR_ILLEGAL_OUTPUT,
00197 "Memory allocation failed!");
00198
00199
00200 check( xsh_parameters_generic( RECIPE_ID, recipe->parameters ) ) ;
00201 xsh_parameters_decode_bp(RECIPE_ID,recipe->parameters,-1);
00202 check( xsh_parameters_pre_overscan( RECIPE_ID, recipe->parameters ) ) ;
00203
00204 check(xsh_parameters_detect_continuum_create(RECIPE_ID,
00205 recipe->parameters,
00206 param));
00207 check( xsh_parameters_clipping_dcn_create( RECIPE_ID,
00208 recipe->parameters ) ) ;
00209 cleanup:
00210 if ( cpl_error_get_code() != CPL_ERROR_NONE ){
00211 xsh_error_dump(CPL_MSG_ERROR);
00212 return 1;
00213 }
00214 else {
00215 return 0;
00216 }
00217 }
00218
00219
00225
00226
00227 static int xsh_orderpos_exec(cpl_plugin *plugin) {
00228 cpl_recipe *recipe = NULL;
00229
00230
00231 assure( plugin != NULL, CPL_ERROR_NULL_INPUT, "Null plugin" );
00232
00233
00234 assure( cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE,
00235 CPL_ERROR_TYPE_MISMATCH, "Plugin is not a recipe");
00236
00237 recipe = (cpl_recipe *)plugin;
00238
00239
00240 xsh_orderpos(recipe->parameters, recipe->frames);
00241
00242
00243 cleanup:
00244 if ( cpl_error_get_code() != CPL_ERROR_NONE ) {
00245 xsh_error_dump(CPL_MSG_ERROR);
00246
00247 cpl_error_reset();
00248 return 1;
00249 }
00250 else {
00251 return 0;
00252 }
00253 }
00254
00255
00261
00262 static int xsh_orderpos_destroy(cpl_plugin *plugin)
00263 {
00264 cpl_recipe *recipe = NULL;
00265
00266
00267 assure( plugin != NULL, CPL_ERROR_NULL_INPUT, "Null plugin" );
00268
00269
00270 assure( cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE,
00271 CPL_ERROR_TYPE_MISMATCH, "Plugin is not a recipe");
00272
00273 recipe = (cpl_recipe *)plugin;
00274
00275 xsh_free_parameterlist(&recipe->parameters);
00276
00277 cleanup:
00278 if (cpl_error_get_code() != CPL_ERROR_NONE)
00279 {
00280 return 1;
00281 }
00282 else
00283 {
00284 return 0;
00285 }
00286 }
00287
00288
00296
00297 static void xsh_orderpos(cpl_parameterlist* parameters,
00298 cpl_frameset* frameset)
00299 {
00300 const char* recipe_tags[1] = {XSH_ORDERDEF};
00301 int recipe_tags_size = 1;
00302
00303
00304 cpl_frameset* raws = NULL;
00305 cpl_frameset* calib = NULL;
00306 cpl_frame* bpmap = NULL;
00307 cpl_frame* master_bias = NULL;
00308 cpl_frame* master_dark = NULL;
00309 cpl_frame * order_tab_guess = NULL ;
00310 cpl_frame * orderframe = NULL ;
00311 cpl_frame * spectralformat_frame = NULL ;
00312 cpl_frame * rmbias = NULL ;
00313 cpl_frame * intFrame = NULL ;
00314 cpl_frame * nir_on = NULL ;
00315 cpl_frame * nir_off = NULL ;
00317
00318 cpl_frame * resFrame = NULL ;
00319
00320
00321 xsh_detect_continuum_param * detect_param = NULL ;
00322 xsh_instrument* instrument = NULL;
00323 xsh_clipping_param * dcn_clipping_param = NULL;
00324 cpl_frame* resid_tab=NULL;
00325 char tag[128];
00326 char fname[128] ;
00327 int pre_overscan_corr=0;
00328
00329
00330
00331
00332 check( xsh_begin( frameset, parameters, &instrument, &raws, &calib,
00333 recipe_tags, recipe_tags_size,
00334 RECIPE_ID, XSH_BINARY_VERSION,
00335 xsh_orderpos_description_short ) ) ;
00336
00337
00338 check( xsh_instrument_update_lamp( instrument, XSH_LAMP_UNDEFINED));
00339
00340 xsh_recipe_params_check(parameters,instrument,RECIPE_ID);
00341
00342
00343
00344 check( spectralformat_frame = xsh_find_frame_with_tag( calib,
00345 XSH_SPECTRAL_FORMAT, instrument));
00346
00347 check(bpmap=xsh_check_load_master_bpmap(calib,instrument,RECIPE_ID));
00348 if ( xsh_instrument_get_arm(instrument) == XSH_ARM_NIR ) {
00349 check( nir_on = xsh_find_raw_orderdef_nir ( raws ) ) ;
00350 check( nir_off = xsh_find_raw_orderdef_nir_off ( raws ) ) ;
00351 }
00352 else {
00353 check( orderframe = xsh_find_raw_orderdef_vis_uvb( raws ) ) ;
00354 if((master_bias = xsh_find_frame_with_tag(calib,XSH_MASTER_BIAS,
00355 instrument)) == NULL) {
00356
00357 xsh_msg_warning("Frame %s not provided",XSH_MASTER_BIAS);
00358 xsh_error_reset();
00359 }
00360
00361 if((master_dark = xsh_find_frame_with_tag(calib,XSH_MASTER_DARK,
00362 instrument)) == NULL){
00363 xsh_msg_warning("Frame %s not provided",XSH_MASTER_DARK);
00364 xsh_error_reset();
00365 }
00366 }
00367
00368 if(NULL== (order_tab_guess = xsh_find_frame_with_tag(calib,
00369 XSH_ORDER_TAB_GUESS,
00370 instrument)) ) {
00371 xsh_msg_error("you must provide an input %s_%s table",XSH_ORDER_TAB_GUESS,
00372 xsh_instrument_arm_tostring(instrument));
00373 goto cleanup;
00374 }
00375
00376
00377 check( xsh_instrument_update_from_spectralformat( instrument,
00378 spectralformat_frame));
00379
00380
00381
00382
00383 check( pre_overscan_corr = xsh_parameters_get_int( parameters, RECIPE_ID,
00384 "pre-overscan-corr"));
00385
00386 check(detect_param=xsh_parameters_detect_continuum_get( RECIPE_ID,
00387 parameters)) ;
00388
00389
00390 xsh_msg_dbg_low("Search Window: %d, Running Window: %d, Fit Window: %d",
00391 detect_param->search_window, detect_param->running_window,
00392 detect_param->fit_window ) ;
00393 xsh_msg_dbg_low( "Polynomial degree: %d, Step: %d", detect_param->poly_degree,
00394 detect_param->poly_step ) ;
00395
00396 check( dcn_clipping_param = xsh_parameters_clipping_dcn_get( RECIPE_ID,
00397 parameters ));
00398
00399
00400
00401
00402
00403
00404
00405
00406
00407
00408 xsh_msg( "Working on Arm %s", xsh_instrument_arm_tostring(instrument ) ) ;
00409
00410 check(xsh_prepare(raws, bpmap, master_bias, XSH_ORDERDEF, instrument,pre_overscan_corr,CPL_TRUE));
00411
00412
00413
00414
00415
00416 if ( xsh_instrument_get_arm(instrument) == XSH_ARM_NIR ) {
00417
00418
00419 check(intFrame=xsh_pre_frame_subtract( nir_on, nir_off,
00420 "ON-OFF_NIR.fits",instrument,1 ) ) ;
00421 }
00422 else {
00423
00424 if(master_bias!= NULL) {
00425
00426 xsh_msg( "Substract bias" ) ;
00427 check(rmbias = xsh_subtract_bias( orderframe, master_bias, instrument,"ORDERDEF_",pre_overscan_corr,0));
00428 } else {
00429 rmbias=cpl_frame_duplicate(orderframe);
00430 }
00431
00432
00433 if(master_dark!= NULL) {
00434 xsh_msg( "Substract dark" ) ;
00435 sprintf( fname, "ORDERPOS_%s_DARK.fits",
00436 xsh_instrument_arm_tostring(instrument ) ) ;
00437 check(intFrame = xsh_subtract_dark(rmbias, master_dark,
00438 fname, instrument));
00439 } else {
00440 intFrame=cpl_frame_duplicate(rmbias);
00441 }
00442 }
00443
00444
00445 if ( xsh_instrument_get_arm(instrument) != XSH_ARM_NIR){
00446 check(xsh_check_input_is_unbinned(intFrame));
00447 }
00448
00449
00450
00451
00452
00453
00454
00455
00456
00457 xsh_msg("Calling detect continuum" ) ;
00458
00459 check_msg( resFrame = xsh_detect_continuum( intFrame, order_tab_guess,
00460 spectralformat_frame,
00461 detect_param,
00462 dcn_clipping_param,
00463 instrument, &resid_tab ),
00464 "Error in xsh_detect_continuum, try to increase detectcontinuum-fit-window-half-size or detectcontinuum-ordertab-deg-y or detectcontinuum-clip-sigma or detectcontinuum-search-window-half-size" ) ;
00465
00466 check(xsh_monitor_flux( intFrame, resFrame, instrument));
00467
00468
00469
00470 xsh_msg( "Save Order Table product" ) ;
00471 check(xsh_add_product_table( resFrame, frameset, parameters, RECIPE_ID,
00472 instrument,NULL));
00473
00474 check(xsh_add_product_table(resid_tab, frameset, parameters, RECIPE_ID,
00475 instrument,NULL));
00476
00477 if ( xsh_instrument_get_arm(instrument) == XSH_ARM_NIR ) {
00478 sprintf(tag,"ORDERDEF_ON");
00479 check(xsh_add_product_image(intFrame,frameset,parameters,RECIPE_ID,instrument,tag));
00480 } else {
00481 sprintf(tag,"ORDERDEF_ON");
00482 check(xsh_add_product_image(rmbias,frameset,parameters,RECIPE_ID,instrument,tag));
00483 }
00484 cleanup:
00485 xsh_end( RECIPE_ID, frameset, parameters );
00486 XSH_FREE( dcn_clipping_param );
00487 XSH_FREE( detect_param );
00488 xsh_instrument_free(&instrument );
00489 xsh_free_frameset(&raws);
00490 xsh_free_frameset(&calib);
00491 xsh_free_frame( &resFrame) ;
00492 xsh_free_frame( &resid_tab) ;
00493 xsh_free_frame(&rmbias);
00494 xsh_free_frame(&bpmap);
00495 xsh_free_frame(&intFrame);
00496 return;
00497 }
00498