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
00030
00031
00032 #ifdef HAVE_CONFIG_H
00033 #include <config.h>
00034 #endif
00035
00036
00037
00038
00039
00040
00041 #include <strings.h>
00042 #include <string.h>
00043 #include <stdio.h>
00044
00045
00046
00047 #include <cxmacros.h>
00048 #include <cxtypes.h>
00049 #include <cxmessages.h>
00050
00051
00052 #include <xmemory.h>
00053
00054
00055
00056
00057
00058 #include <cpl.h>
00059
00060
00061
00062 #include <sinfoni_prepare_stacked_frames_config.h>
00063 #include <sinfoni_north_south_test_config.h>
00064 #include <sinfoni_lamp_flats_config.h>
00065 #include <sinfoni_bp_config.h>
00066 #include <sinfoni_bp_dist_config.h>
00067 #include <sinfoni_distortion_config.h>
00068
00069 #include <lamp_flats.h>
00070 #include <bp_norm.h>
00071 #include <prepare_stacked_frames.h>
00072 #include <find_distortions.h>
00073 #include <nst.h>
00074
00075 #include <sinfoni_key_names.h>
00076 #include <sinfoni_pro_types.h>
00077 #include <sinfoni_globals.h>
00078 #include <sinfoni_raw_types.h>
00079 #include <sinfoni_tpl_utils.h>
00080 #include <sinfoni_tpl_dfs.h>
00081 #include <sinfoni_globals.h>
00082 #include <sinfoni_functions.h>
00083 #include <sinfoni_memory.h>
00084
00085
00086
00087
00088
00089 static cxint si_rec_distortion_new(cpl_parameterlist *, cpl_frameset *);
00090
00091 static cxint si_rec_distortion_new_create(cpl_plugin *plugin);
00092 static cxint si_rec_distortion_new_exec(cpl_plugin *plugin);
00093 static cxint si_rec_distortion_new_destroy(cpl_plugin *plugin);
00094
00095 static cxint si_rec_distortion_new(cpl_parameterlist *config, cpl_frameset *set);
00096
00097 int cpl_plugin_get_info(cpl_pluginlist *list);
00098
00099
00100
00101
00102
00103
00104
00105 static char si_rec_distortion_new_description1[] =
00106 "This recipe computes the detector's distortion and the slitlet distances.\n"
00107 "Additional relevant products are a master flat, a bad pixel map, a wavelength map.\n"
00108 "The input files are :\n"
00109 "raw flats having the first column of some slitlets illuminated by a fibre with tag is FIBRE_NS\n"
00110 "normal raw on/off flats with tag FLAT_NS and\n"
00111 "Arc lamp files with tag WAVE_NS\n"
00112 "A corresponding (band) reference line table with tag REF_LINE_ARC\n";
00113
00114
00115
00116 static char si_rec_distortion_new_description2[] =
00117 "The main products are a distortion and a slitlet distances tables\n"
00118 "(PRO.CATG=DISTORTION and SLITLETS_DISTANCE)\n"
00119 "Information on relevant parameters can be found with\n"
00120 "esorex --params si_rec_distortion_new\n"
00121 "esorex --help si_rec_distortion_new\n"
00122 "\n";
00123
00124
00125 static char si_rec_distortion_new_description[800];
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139 static cxint
00140 si_rec_distortion_new_create(cpl_plugin *plugin)
00141 {
00142
00143
00144
00145
00146
00147
00148 cpl_recipe *recipe = (cpl_recipe *)plugin;
00149 recipe->parameters = cpl_parameterlist_new();
00150 if(recipe->parameters == NULL) {
00151 return 1;
00152 }
00153
00154
00155
00156
00157
00158
00159 sinfoni_lamp_flats_config_add(recipe->parameters);
00160 sinfoni_bp_config_add(recipe->parameters);
00161 sinfoni_bp_dist_config_add(recipe->parameters);
00162 sinfoni_prepare_stacked_frames_config_add(recipe->parameters);
00163
00164 sinfoni_distortion_config_add(recipe->parameters);
00165 sinfoni_north_south_test_config_add(recipe->parameters);
00166
00167 return 0;
00168
00169 }
00170
00171 static cxint
00172 si_rec_distortion_new_exec(cpl_plugin *plugin)
00173 {
00174
00175 cpl_recipe *recipe = (cpl_recipe *) plugin;
00176 if(recipe->parameters == NULL) {
00177 return 1;
00178 }
00179 if(recipe->frames == NULL) {
00180 return 1;
00181 }
00182
00183 cpl_error_reset();
00184 return si_rec_distortion_new(recipe->parameters, recipe->frames);
00185
00186 }
00187
00188 static cxint
00189 si_rec_distortion_new_destroy(cpl_plugin *plugin)
00190 {
00191
00192 cpl_recipe *recipe = (cpl_recipe *) plugin;
00193
00194
00195
00196
00197
00198
00199 cpl_parameterlist_delete(recipe->parameters);
00200
00201 return 0;
00202
00203 }
00204
00205 int
00206 cpl_plugin_get_info(cpl_pluginlist *list)
00207 {
00208
00209 cpl_recipe *recipe = cpl_calloc(1, sizeof *recipe);
00210 cpl_plugin *plugin = &recipe->interface;
00211 strcpy(si_rec_distortion_new_description,si_rec_distortion_new_description1);
00212 strcat(si_rec_distortion_new_description,si_rec_distortion_new_description2);
00213
00214
00215 cpl_plugin_init(plugin,
00216 CPL_PLUGIN_API,
00217 SINFONI_BINARY_VERSION,
00218 CPL_PLUGIN_TYPE_RECIPE,
00219 "si_rec_distortion_new",
00220 "Find detector's distortions and slitlets distances",
00221 si_rec_distortion_new_description,
00222 "Andrea Modigliani",
00223 "Andrea.Modigliani@eso.org",
00224 sinfoni_get_license(),
00225 si_rec_distortion_new_create,
00226 si_rec_distortion_new_exec,
00227 si_rec_distortion_new_destroy);
00228
00229 cpl_pluginlist_append(list, plugin);
00230
00231 return 0;
00232
00233 }
00234
00235
00236
00237
00238
00239
00240 static cxint
00241 si_rec_distortion_new(cpl_parameterlist *config, cpl_frameset *set)
00242 {
00243
00244 const char *_id = "si_rec_distortion_new";
00245 int ind =0;
00246 cpl_parameter* p=NULL;
00247 cpl_frame* frame=NULL;
00248 char file_name[FILE_NAME_SZ];
00249 cpl_propertylist* plist=NULL;
00250
00251 cpl_image* ima=NULL;
00252 fake* fk=fake_new();
00253
00254 if(sinfoni_dfs_set_groups(set)) {
00255
00256 cpl_msg_error(_id, "Cannot indentify RAW and CALIB frames") ;
00257 fake_delete(fk);
00258 return -1;
00259
00260 }
00261
00262 cpl_msg_info(_id,"-----------------------------");
00263 cpl_msg_info(_id," DETERMINE MASTER_LAMP_NS ");
00264 cpl_msg_info(_id,"-----------------------------");
00265
00266
00267 {
00268 if ( -1 == (ind = lamp_flats(config, set ) ) )
00269 {
00270 cpl_msg_error(_id,"no: %d\n", ind) ;
00271 fake_delete(fk);
00272 return -1 ;
00273 }
00274 cpl_msg_info (_id,"SUCCESS DETERMINATION MASTER_LAMP_NS") ;
00275
00276 }
00277 sinfoni_memory_status();
00278 cpl_msg_info(_id,"-----------------------------");
00279 cpl_msg_info(_id," DETERMINE BP_MAP_DI ");
00280 cpl_msg_info(_id,"-----------------------------");
00281
00282
00283
00284
00285
00286
00287
00288 {
00289
00290 p = cpl_parameterlist_find(config,"sinfoni.bp.method");
00291 cpl_parameter_set_string(p,"Normal");
00292
00293 if ( -1 == (ind = badSearchNormal(config, set, PRO_BP_MAP_DI ) ) )
00294 {
00295 cpl_msg_error(_id,"badSearchNormal(), no: %d\n", ind) ;
00296 fake_delete(fk);
00297 return -1 ;
00298 }
00299 cpl_msg_info(_id,"SUCCESS DETERMINATION %s",PRO_BP_MAP_DI);
00300 }
00301 sinfoni_memory_status();
00302
00303
00304
00305
00306
00307
00308
00309 {
00310
00311
00312 cpl_msg_info (_id,"STACK FIBRE,NS TO GET FAKE OFF\n") ;
00313
00314 strcpy(fk->pro_class,RAW_FIBRE_NS);
00315 fk->frm_switch=1;
00316 fk->mask_index=0;
00317 fk->ind_index=0;
00318 fk->flat_index=0;
00319 fk->wfix_index=0;
00320 fk->low_rej=0.0;
00321 fk->hig_rej=0.2;
00322
00323
00324 if ( -1 == (ind = prepare_stacked_frames(config, set,PRO_FIBRE_NS_STACKED_OFF,0,fk)))
00325 {
00326 cpl_msg_error(_id," no: %d\n", ind) ;
00327 fake_delete(fk);
00328 return -1 ;
00329 }
00330
00331 if(NULL != cpl_frameset_find(set,PRO_FIBRE_NS_STACKED_OFF)) {
00332 frame = cpl_frameset_find(set,PRO_FIBRE_NS_STACKED_OFF);
00333 strcpy(file_name,cpl_frame_get_filename(frame));
00334 } else {
00335 cpl_msg_error(_id,"Frame %s not found!", PRO_FIBRE_NS_STACKED_OFF);
00336 fake_delete(fk);
00337 return -1;
00338 }
00339 ima=cpl_image_load(file_name,CPL_TYPE_FLOAT,0,0);
00340
00341
00342 if ((cpl_error_code)((plist = cpl_propertylist_load(file_name, 0)) == NULL)) {
00343 cpl_msg_error(_id, "getting header from reference ima frame %s",file_name);
00344 cpl_propertylist_delete(plist) ;
00345 fake_delete(fk);
00346 return -1 ;
00347 }
00348
00349 if (cpl_propertylist_contains(plist, KEY_NAME_LAMP_HALO)) {
00350 cpl_propertylist_set_bool(plist, KEY_NAME_LAMP_HALO, LAMP_OFF);
00351 } else {
00352 cpl_propertylist_append_bool(plist, KEY_NAME_LAMP_HALO,LAMP_OFF) ;
00353 }
00354
00355
00356
00357
00358
00359
00360
00361
00362 cpl_propertylist_delete(plist);
00363 cpl_msg_info (_id,"SUCCESS DETERMINATION %s",PRO_FIBRE_NS_STACKED_OFF) ;
00364
00365 }
00366 cpl_image_delete(ima);
00367 sinfoni_memory_status();
00368
00369
00370
00371
00372
00373
00374
00375 {
00376
00377 cpl_msg_info (_id,"STACK FIBRE,NS TO GET FAKE ON\n") ;
00378
00379 strcpy(fk->pro_class,RAW_FIBRE_NS);
00380 fk->frm_switch=1;
00381 fk->mask_index=0;
00382 fk->ind_index=0;
00383 fk->flat_index=0;
00384 fk->wfix_index=0;
00385 fk->low_rej=0.0;
00386 fk->hig_rej=0.0;
00387
00388
00389 if ( -1 == (ind = prepare_stacked_frames(config, set, PRO_FIBRE_NS_STACKED_ON,0,fk)))
00390 {
00391 cpl_msg_error(_id," no: %d\n", ind) ;
00392 fake_delete(fk);
00393 return -1 ;
00394 }
00395 cpl_msg_info (_id,"SUCCESS DETERMINATION %s",PRO_FIBRE_NS_STACKED_ON) ;
00396 }
00397 sinfoni_memory_status();
00398
00399
00400
00401
00402
00403
00404
00405
00406
00407 {
00408
00409 cpl_msg_info (_id,"COMBINES FAKE ON AND OFF\n") ;
00410
00411
00412 strcpy(fk->pro_class,PRO_FIBRE_NS_STACKED);
00413 fk->frm_switch=1;
00414 fk->mask_index=0;
00415 fk->ind_index=0;
00416 fk->flat_index=1;
00417 fk->wfix_index=0;
00418 fk->low_rej=0.0;
00419 fk->hig_rej=0.0;
00420
00421
00422 if ( -1 == (ind = prepare_stacked_frames(config, set, PRO_FIBRE_NS_STACKED,0,fk)))
00423 {
00424 cpl_msg_error(_id," no: %d\n", ind) ;
00425 fake_delete(fk);
00426 return -1 ;
00427 }
00428 cpl_msg_info (_id,"SUCCESS DETERMINATION %s",PRO_FIBRE_NS_STACKED) ;
00429
00430 }
00431 sinfoni_memory_status();
00432
00433
00434
00435
00436
00437
00438
00439
00440 {
00441
00442 cpl_msg_info (_id,"STACK on WAVE frame\n") ;
00443
00444 strcpy(fk->pro_class,RAW_WAVE_NS);
00445 fk->frm_switch=1;
00446 fk->mask_index=0;
00447 fk->ind_index=0;
00448 fk->flat_index=1;
00449 fk->wfix_index=0;
00450 fk->low_rej=0.1;
00451 fk->hig_rej=0.1;
00452
00453 cpl_msg_info (_id,"STACK on WAVE frame\n") ;
00454 if ( -1 == (ind = prepare_stacked_frames(config, set, PRO_WAVE_LAMP_STACKED,0,fk)))
00455 {
00456 cpl_msg_error(_id," no: %d\n", ind) ;
00457 fake_delete(fk);
00458 return -1 ;
00459 }
00460 cpl_msg_info (_id,"SUCCESS DETERMINATION %s",PRO_WAVE_LAMP_STACKED) ;
00461 }
00462 sinfoni_memory_status();
00463
00464
00465
00466
00467
00468
00469
00470
00471
00472
00473
00474 {
00475
00476 cpl_msg_info(_id,"COMPUTE DISTORTIONS\n") ;
00477 if ( -1 == (ind = find_distortions(config, set ) ) )
00478 {
00479 cpl_msg_error(_id,"step_distortion(), no: %d\n", ind) ;
00480 fake_delete(fk);
00481 return -1 ;
00482 }
00483
00484 if(NULL != cpl_frameset_find(set,PRO_FIBRE_NS_STACKED)) {
00485 frame = cpl_frameset_find(set,PRO_FIBRE_NS_STACKED);
00486 strcpy(file_name,cpl_frame_get_filename(frame));
00487 } else {
00488 cpl_msg_error(_id,"Frame %s not found!", PRO_FIBRE_NS_STACKED);
00489 fake_delete(fk);
00490 return -1;
00491 }
00492 ima=cpl_image_load(file_name,CPL_TYPE_FLOAT,0,0);
00493
00494 if ((cpl_error_code)((plist = cpl_propertylist_load(file_name, 0)) == NULL)) {
00495 cpl_msg_error(_id, "getting header from reference ima frame %s",file_name);
00496 cpl_propertylist_delete(plist) ;
00497 fake_delete(fk);
00498 return -1 ;
00499 }
00500
00501 if (cpl_propertylist_contains(plist, KEY_NAME_LAMP_HALO)) {
00502 cpl_propertylist_set_bool(plist, KEY_NAME_LAMP_HALO, LAMP_ON);
00503 } else {
00504 cpl_propertylist_append_bool(plist, KEY_NAME_LAMP_HALO,LAMP_ON) ;
00505 }
00506
00507
00508
00509
00510
00511
00512
00513
00514
00515
00516
00517
00518 cpl_propertylist_delete(plist);
00519 cpl_msg_info(_id,"SUCCESS: COMPUTED DISTORTIONS\n") ;
00520 }
00521 cpl_image_delete(ima);
00522 sinfoni_memory_status();
00523
00524
00525
00526
00527
00528
00529
00530
00531
00532 {
00533 cpl_msg_info (_id,"DISTORT FAKE FRAME\n") ;
00534
00535 strcpy(fk->pro_class,PRO_FIBRE_NS_STACKED_DIST);
00536 fk->frm_switch=1;
00537 fk->mask_index=1;
00538 fk->ind_index=1;
00539 fk->flat_index=0;
00540 fk->wfix_index=1;
00541 fk->low_rej=0.0;
00542 fk->hig_rej=0.0;
00543
00544
00545 if ( -1 == (ind = prepare_stacked_frames(config, set, PRO_FIBRE_NS_STACKED_DIST,0,fk)))
00546 {
00547 cpl_msg_error(_id," no: %d\n", ind) ;
00548 fake_delete(fk);
00549 return -1 ;
00550 }
00551 cpl_msg_info (_id,"SUCCESS: DISTORTED FAKE FRAME\n") ;
00552
00553
00554 if(NULL != cpl_frameset_find(set,PRO_FIBRE_NS_STACKED_DIST)) {
00555 frame = cpl_frameset_find(set,PRO_FIBRE_NS_STACKED_DIST);
00556 strcpy(file_name,cpl_frame_get_filename(frame));
00557 } else {
00558 cpl_msg_error(_id,"Frame %s not found!", PRO_FIBRE_NS_STACKED_DIST);
00559 fake_delete(fk);
00560 return -1;
00561 }
00562 ima=cpl_image_load(file_name,CPL_TYPE_FLOAT,0,0);
00563
00564
00565
00566 if ((cpl_error_code)((plist = cpl_propertylist_load(file_name, 0)) == NULL)) {
00567 cpl_msg_error(_id, "getting header from reference ima frame %s",file_name);
00568 cpl_propertylist_delete(plist) ;
00569 fake_delete(fk);
00570 return -1 ;
00571 }
00572
00573 if (cpl_propertylist_contains(plist, KEY_NAME_LAMP_HALO)) {
00574 cpl_propertylist_set_bool(plist, KEY_NAME_LAMP_HALO, LAMP_ON);
00575 } else {
00576 cpl_propertylist_append_bool(plist, KEY_NAME_LAMP_HALO,LAMP_ON) ;
00577 }
00578
00579
00580
00581
00582
00583
00584
00585
00586
00587
00588
00589 cpl_propertylist_delete(plist);
00590 cpl_image_delete(ima);
00591
00592 cpl_msg_info(_id,"SUCCESS: COMPUTED DISTORTIONS\n") ;
00593 }
00594 sinfoni_memory_status();
00595
00596
00597
00598
00599
00600
00601
00602
00603 {
00604 cpl_msg_info (_id,"RUN NORD SUD TEST\n") ;
00605 if ( -1 == (ind = nst(config, set ) ) )
00606 {
00607 cpl_msg_error(_id,"no: %d\n", ind) ;
00608 fake_delete(fk);
00609 return -1 ;
00610 }
00611
00612 cpl_msg_info (_id,"SUCCESS: RUNNED NORD SUD TEST\n") ;
00613
00614 }
00615 cpl_msg_info (_id,"SUCCESS: RECIPE\n") ;
00616 fake_delete(fk);
00617 sinfoni_memory_status();
00618 return 0 ;
00619
00620
00621 }
00622
00623
00624
00625
00626
00627
00628
00629
00630
00631
00632
00633
00634