49 #include <irplib_utils.h>
51 #include <sinfo_hidden.h>
52 #include <sinfo_pro_types.h>
53 #include <sinfo_raw_types.h>
54 #include <sinfo_key_names.h>
56 #include <sinfo_globals.h>
57 #include <sinfo_general_config.h>
58 #include <sinfo_utilities.h>
59 #include <sinfo_product_config.h>
60 #include <sinfo_lamp_flats_config.h>
61 #include <sinfo_bp_config.h>
62 #include <sinfo_bp_dist_config.h>
63 #include <sinfo_prepare_stacked_frames_config.h>
65 #include <sinfo_north_south_test_config.h>
66 #include <sinfo_distortion_config.h>
67 #include <sinfo_new_lamp_flats.h>
68 #include <sinfo_bp_norm.h>
69 #include <sinfo_new_prepare_stacked_frames.h>
70 #include <sinfo_new_find_distortions.h>
71 #include <sinfo_new_nst.h>
73 #include <sinfo_functions.h>
74 #include <sinfo_tpl_utils.h>
75 #include <sinfo_tpl_dfs.h>
76 #include <sinfo_msg.h>
77 #include <sinfo_error.h>
78 #include <sinfo_utils_wrappers.h>
80 #include <sinfo_dfs.h>
88 static int sinfo_rec_distortion_create(cpl_plugin *plugin);
89 static int sinfo_rec_distortion_exec(cpl_plugin *plugin);
90 static int sinfo_rec_distortion_destroy(cpl_plugin *plugin);
91 static int sinfo_rec_distortion(cpl_parameterlist *config, cpl_frameset *
set);
92 static int new_pre_process(cpl_frameset*
set,
93 const float lo_rej,
const float hi_rej,
const char* name_o);
98 static char sinfo_rec_distortion_description1[] =
99 "This recipe determines the optical distortions and the slitlets distances.\n"
100 "Necessary input are:\n"
101 "Several (usually 75) raw frames classified as FIBRE_NS\n"
102 "Standard (an 'ON' and an 'OFF') flat frames having classified as FLAT_NS\n"
103 "Standard (an 'ON' and an 'OFF') arc lamp frames having classified as WAVE_NS\n"
104 "A corresponding (band) reference arc line list classified as REF_LINE_ARC\n"
105 "A reference table with data reduction parameters classified as DRS_SETUP_WAVE.\n";
108 static char sinfo_rec_distortion_description2[] =
109 "Default output are (with their PRO.CATG)\n"
110 "A master flat: MASTER_FLAT_LAMP\n"
111 "A Bad pixel map: BP_MAP_DI\n"
112 "A fake-off fibre stacked frame: FIBRE_NS_STACKED_OFF\n"
113 "A fake-on fibre stacked frame: FIBRE_NS_STACKED_ON\n"
114 "A fake on-off fibre stacked frame: FIBRE_NS_STACKED\n"
115 "A table with optical distortion coefficients: DISTORTION\n";
118 static char sinfo_rec_distortion_description3[] =
119 "A distortion corrected frame: FIBRE_NS_STACKED_DIST\n"
120 "A temporary frame: MASTER_SLIT\n"
121 "A table with the slitlets distances: SLITLETS_DISTANCE\n"
124 static char sinfo_rec_distortion_description[1300];
149 cpl_recipe *recipe = cpl_calloc(1,
sizeof *recipe);
150 cpl_plugin *plugin = &recipe->interface;
151 strcpy(sinfo_rec_distortion_description,sinfo_rec_distortion_description1);
152 strcat(sinfo_rec_distortion_description,sinfo_rec_distortion_description2);
153 strcat(sinfo_rec_distortion_description,sinfo_rec_distortion_description3);
156 cpl_plugin_init(plugin,
158 SINFONI_BINARY_VERSION,
159 CPL_PLUGIN_TYPE_RECIPE,
160 "sinfo_rec_distortion",
161 "Finds optical distortions and slitlets distances",
162 sinfo_rec_distortion_description,
164 "Andrea.Modigliani@eso.org",
166 sinfo_rec_distortion_create,
167 sinfo_rec_distortion_exec,
168 sinfo_rec_distortion_destroy);
170 cpl_pluginlist_append(list, plugin);
186 static int sinfo_rec_distortion_create(cpl_plugin *plugin)
188 cpl_recipe * recipe ;
191 if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
192 recipe = (cpl_recipe *)plugin ;
198 recipe->parameters = cpl_parameterlist_new() ;
207 sinfo_general_config_add(recipe->parameters);
208 sinfo_product_config_add(recipe->parameters);
209 sinfo_lamp_flats_config_add(recipe->parameters);
210 sinfo_bp_config_add(recipe->parameters);
212 sinfo_prepare_stacked_frames_config_add(recipe->parameters);
213 sinfo_distortion_config_add(recipe->parameters);
214 sinfo_north_south_test_config_add(recipe->parameters);
227 static int sinfo_rec_distortion_exec(cpl_plugin *plugin)
229 cpl_recipe * recipe ;
232 cpl_errorstate initial_errorstate = cpl_errorstate_get();
235 if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
236 recipe = (cpl_recipe *)plugin ;
238 status=sinfo_rec_distortion(recipe->parameters, recipe->frames);
240 if (!cpl_errorstate_is_equal(initial_errorstate)) {
243 cpl_errorstate_dump(initial_errorstate, CPL_FALSE, NULL);
256 static int sinfo_rec_distortion_destroy(cpl_plugin *plugin)
258 cpl_recipe * recipe ;
261 if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
262 recipe = (cpl_recipe *)plugin ;
265 cpl_parameterlist_delete(recipe->parameters);
284 sinfo_rec_distortion(cpl_parameterlist *config, cpl_frameset *
set)
287 cpl_parameter *p=NULL;
288 cpl_frame* frame=NULL;
289 cpl_propertylist* plist=NULL;
291 cpl_frameset* raw_set=NULL;
292 cpl_frameset* set_off=NULL;
293 cpl_frameset* set_on=NULL;
294 cpl_frameset* set_fibre_ns=NULL;
295 cpl_frameset* set_flat_ns=NULL;
296 cpl_frameset* set_wave_ns=NULL;
297 char file_name[FILE_NAME_SZ];
299 fake* fk=sinfo_fake_new();
303 check_nomsg(p=cpl_parameterlist_find(config,
"sinfoni.product.density"));
304 check_nomsg(pdensity=cpl_parameter_get_int(p));
307 sinfo_msg(
"Welcome to SINFONI Pipeline release %d.%d.%d",
308 SINFONI_MAJOR_VERSION,SINFONI_MINOR_VERSION,SINFONI_MICRO_VERSION);
309 if(sinfo_dfs_set_groups(
set)) {
314 check_nomsg(p=cpl_parameterlist_find(config,
"sinfoni.general.lc_sw"));
315 check_nomsg(line_cor=cpl_parameter_get_bool(p));
317 check_nomsg(sinfo_ima_line_cor(config,
set));
320 cknull_nomsg(set_fibre_ns=sinfo_frameset_extract(
set,RAW_FIBRE_NS));
321 cknull_nomsg(set_flat_ns=sinfo_frameset_extract(
set,RAW_FLAT_NS));
322 cknull_nomsg(set_wave_ns=sinfo_frameset_extract(
set,RAW_WAVE_NS));
323 check_nomsg(p=cpl_parameterlist_find(config,
"sinfoni.bp.method"));
324 check_nomsg(cpl_parameter_set_string(p,
"Normal"));
325 check_nomsg(raw_set=cpl_frameset_duplicate(
set));
327 sinfo_msg(
"-----------------------------");
328 sinfo_msg(
" DETERMINE MASTER_LAMP_NS ");
329 sinfo_msg(
"-----------------------------");
331 sinfo_msg(
"REDUCE LAMPFLAT") ;
332 ck0(sinfo_new_lamp_flats(cpl_func,config,
set,set_flat_ns ),
333 "reducing lampflats") ;
334 sinfo_msg(
"SUCCES: DETERMINATION MASTER_LAMP_NS") ;
336 sinfo_msg(
"-----------------------------");
337 sinfo_msg(
" DETERMINE BP_MAP_DI ");
338 sinfo_msg(
"-----------------------------");
340 check_nomsg(p=cpl_parameterlist_find(config,
"sinfoni.bp.method"));
341 check_nomsg(cpl_parameter_set_string(p,
"Normal"));
343 ck0(sinfo_new_bp_search_normal(cpl_func,config,
set,set_flat_ns,
345 "determining %s",PRO_BP_MAP_DI);
346 sinfo_msg(
"SUCCESS DETERMINATION %s",PRO_BP_MAP_DI);
353 check_nomsg(set_off=cpl_frameset_duplicate(raw_set));
354 ck0_nomsg(new_pre_process(set_off, 0.0, 0.2,
"out_fibre_off.fits"));
358 if(NULL != cpl_frameset_find(set_off,RAW_FIBRE_NS)) {
359 frame = cpl_frameset_find(set_off,RAW_FIBRE_NS);
360 cpl_frameset_erase(
set,RAW_FIBRE_NS);
361 cpl_frameset_insert(
set,cpl_frame_duplicate(frame));
374 sinfo_msg(
"STACK FIBRE,NS TO GET FAKE OFF\n") ;
375 strcpy(fk->pro_class,RAW_FIBRE_NS);
385 ck0(sinfo_new_prepare_stacked_frames(cpl_func,config,
set, set_fibre_ns,
386 PRO_FIBRE_NS_STACKED_OFF,0,fk),
387 "Error stacking frame %s",PRO_FIBRE_NS_STACKED_OFF);
389 strcpy(file_name,
"out_fibre_off.fits");
390 check_nomsg(ima=cpl_image_load(file_name,CPL_TYPE_FLOAT,0,0));
391 sinfo_free_frameset(&set_off);
393 cknull(plist = cpl_propertylist_load(file_name, 0),
394 "getting header from reference ima frame %s",file_name);
396 if (sinfo_propertylist_has(plist, KEY_NAME_LAMP_HALO)) {
397 cpl_propertylist_set_bool(plist, KEY_NAME_LAMP_HALO, LAMP_OFF);
399 cpl_propertylist_append_bool(plist, KEY_NAME_LAMP_HALO,LAMP_OFF);
402 sinfo_free_propertylist(&plist);
403 sinfo_free_image(&ima);
404 sinfo_msg(
"SUCCESS: STACKED FIBRE,NS TO GET FAKE OFF\n") ;
412 check_nomsg(set_on=cpl_frameset_duplicate(raw_set));
414 ck0_nomsg(new_pre_process(set_on, 0.0, 0.0,
"out_fibre_on.fits"));
416 if(NULL != cpl_frameset_find(set_on,RAW_FIBRE_NS)) {
417 frame = cpl_frameset_find(set_on,RAW_FIBRE_NS);
418 cpl_frameset_erase(
set,RAW_FIBRE_NS);
419 cpl_frameset_insert(
set,cpl_frame_duplicate(frame));
426 sinfo_msg(
"STACK FIBRE,NS TO GET FAKE ON\n") ;
428 strcpy(fk->pro_class,RAW_FIBRE_NS);
438 ck0(sinfo_new_prepare_stacked_frames(cpl_func,config,
set, set_fibre_ns,
439 PRO_FIBRE_NS_STACKED_ON,0,fk),
440 "error stacking frame %s",PRO_FIBRE_NS_STACKED_ON);
441 sinfo_msg(
"SUCCESS DETERMINATION %s",PRO_FIBRE_NS_STACKED_ON) ;
444 sinfo_free_frameset(&set_on);
453 sinfo_msg(
"COMBINES FAKE ON AND OFF\n") ;
454 strcpy(fk->pro_class,PRO_FIBRE_NS_STACKED);
463 ck0(sinfo_new_prepare_stacked_frames(cpl_func,config,
set, set_fibre_ns,
464 PRO_FIBRE_NS_STACKED,0,fk),
465 "Stacking frame %s",PRO_FIBRE_NS_STACKED);
466 sinfo_msg(
"SUCCESS DETERMINATION %s",PRO_FIBRE_NS_STACKED) ;
476 sinfo_msg(
"STACK on WAVE frame\n") ;
477 strcpy(fk->pro_class,RAW_WAVE_NS);
486 sinfo_msg(
"STACK on WAVE frame\n") ;
487 ck0(sinfo_new_prepare_stacked_frames(cpl_func,config,
set, set_wave_ns,
488 PRO_WAVE_LAMP_STACKED,0,fk),
489 "spacking frame %s",PRO_WAVE_LAMP_STACKED);
490 sinfo_msg(
"SUCCESS DETERMINATION %s",PRO_WAVE_LAMP_STACKED) ;
493 cpl_frameset_erase(
set,PRO_FIBRE_NS_STACKED_OFF);
494 cpl_frameset_erase(
set,PRO_FIBRE_NS_STACKED_ON);
495 cpl_frameset_erase(
set,PRO_MASTER_FLAT_LAMP);
503 sinfo_msg(
"COMPUTE DISTORTIONS\n") ;
504 ck0(sinfo_new_find_distortions(cpl_func,config,
set,set_fibre_ns),
505 "computing distortions");
508 if(NULL != cpl_frameset_find(
set,PRO_FIBRE_NS_STACKED)) {
509 frame = cpl_frameset_find(
set,PRO_FIBRE_NS_STACKED);
510 strcpy(file_name,cpl_frame_get_filename(frame));
515 check_nomsg(ima=cpl_image_load(file_name,CPL_TYPE_FLOAT,0,0));
517 check(plist = cpl_propertylist_load(file_name, 0),
518 "getting header from reference ima frame %s",file_name);
521 if (cpl_propertylist_has(plist, KEY_NAME_LAMP_HALO)) {
522 cpl_propertylist_set_bool(plist, KEY_NAME_LAMP_HALO, LAMP_ON);
524 cpl_propertylist_append_bool(plist, KEY_NAME_LAMP_HALO,LAMP_ON);
538 sinfo_free_image(&ima);
539 sinfo_free_propertylist(&plist);
540 sinfo_msg(
"SUCCESS: COMPUTED DISTORTIONS\n") ;
550 sinfo_msg(
"DISTORT FAKE FRAME\n") ;
552 strcpy(fk->pro_class,PRO_FIBRE_NS_STACKED_DIST);
561 ck0(sinfo_new_prepare_stacked_frames(cpl_func,config,
set, set_fibre_ns,
562 PRO_FIBRE_NS_STACKED_DIST,0,fk),
563 "Stacking frame %s",PRO_FIBRE_NS_STACKED_DIST);
565 sinfo_msg(
"SUCCESS: DISTORTED FAKE FRAME\n") ;
569 if(NULL != cpl_frameset_find(
set,PRO_FIBRE_NS_STACKED_DIST)) {
570 frame = cpl_frameset_find(
set,PRO_FIBRE_NS_STACKED_DIST);
571 strcpy(file_name,cpl_frame_get_filename(frame));
576 check_nomsg(ima=cpl_image_load(file_name,CPL_TYPE_FLOAT,0,0));
577 check(plist = cpl_propertylist_load(file_name, 0),
578 "getting header from reference ima frame %s",file_name);
581 if (cpl_propertylist_has(plist, KEY_NAME_LAMP_HALO)) {
582 cpl_propertylist_set_bool(plist, KEY_NAME_LAMP_HALO, LAMP_ON);
584 cpl_propertylist_append_bool(plist, KEY_NAME_LAMP_HALO,LAMP_ON);
595 sinfo_free_propertylist(&plist);
596 sinfo_free_image(&ima);
605 cpl_frameset_erase(
set,PRO_BP_MAP_DI);
606 cpl_frameset_erase(
set,PRO_FIBRE_NS_STACKED);
607 cpl_frameset_erase(
set,PRO_WAVE_LAMP_STACKED);
611 sinfo_msg(
"RUN NORD SOUTH TEST\n") ;
612 ck0(sinfo_new_nst(cpl_func,config,
set,set_fibre_ns),
613 "Running north south test");
617 cpl_frameset_erase(
set,PRO_FIBRE_NS_STACKED_DIST);
620 sinfo_msg(
"SUCCESS: RUNNED NORD SUD TEST\n") ;
621 sinfo_msg(
"SUCCESS: RECIPE\n") ;
623 sinfo_free_frameset(&raw_set);
624 sinfo_fake_delete(&fk);
629 sinfo_free_frameset(&set_on);
630 sinfo_free_frameset(&set_fibre_ns);
631 sinfo_free_frameset(&set_wave_ns);
632 sinfo_free_frameset(&set_flat_ns);
633 sinfo_free_image(&ima);
634 sinfo_free_propertylist(&plist) ;
635 sinfo_free_frameset(&set_off);
636 sinfo_free_frameset(&raw_set);
637 sinfo_free_frameset(&raw_set);
638 sinfo_fake_delete(&fk);
640 if (cpl_error_get_code() != CPL_ERROR_NONE) {
652 new_pre_process(cpl_frameset*
set,
653 const float lo_rej,
const float hi_rej,
const char* name_o)
669 cpl_frame* frame=NULL;
670 cpl_frame* pframe=NULL;
671 cpl_frameset* sof=NULL;
672 cpl_frameset* fibre_raw_set=NULL;
673 char* file_name=NULL;
675 cpl_propertylist* plist=NULL;
676 cpl_imagelist* imset_tmp=NULL;
677 cpl_imagelist* imset_tot=NULL;
678 cpl_image** avg_img_list=NULL;
681 cpl_image* img_tmp=NULL;
682 cpl_image* img_dup=NULL;
684 cpl_image** img_list=NULL;
687 sof = cpl_frameset_duplicate(
set);
688 fibre_raw_set = cpl_frameset_new();
690 sinfo_contains_frames_kind(sof,fibre_raw_set,RAW_FIBRE_NS);
692 nf = cpl_frameset_get_size(fibre_raw_set);
695 sinfo_free_frameset(&sof);
696 sinfo_free_frameset(&fibre_raw_set);
699 frame = cpl_frameset_get_frame(fibre_raw_set,0);
700 file_name=(
char*) cpl_frame_get_filename(frame);
703 if ((plist = cpl_propertylist_load(file_name, 0)) == NULL) {
711 sinfo_msg(
"Total raw frames nf=%d > max frm per group ng=%d",nf,ng);
714 imset_tot=cpl_imagelist_new();
715 avg_img_list=cpl_malloc((ns+1) *
sizeof(cpl_image *));
719 sinfo_msg(
"iteration i=%d\n",i);
720 imset_tmp=cpl_imagelist_new();
721 img_list=cpl_malloc(ng *
sizeof(cpl_image *));
726 frame = cpl_frameset_get_frame(fibre_raw_set,k);
727 file_name=(
char*)cpl_frame_get_filename(frame);
728 img_tmp=cpl_image_load(file_name,CPL_TYPE_FLOAT,0,0);
729 img_dup=cpl_image_duplicate(img_tmp);
730 cpl_imagelist_set(imset_tmp,img_dup,j);
731 cpl_image_delete(img_tmp);
735 nt=cpl_imagelist_get_size(imset_tmp);
736 lo_cut=(floor)(lo_rej*nt+0.5);
737 hi_cut=(floor)(hi_rej*nt+0.5);
738 avg_img_list[i]=cpl_imagelist_collapse_minmax_create(imset_tmp,
740 cpl_imagelist_set(imset_tot,avg_img_list[i],i);
741 cpl_imagelist_delete(imset_tmp);
747 imset_tmp=cpl_imagelist_new();
748 img_list=cpl_malloc((nf-ns*ng) *
sizeof(cpl_image *));
751 frame = cpl_frameset_get_frame(fibre_raw_set,k);
752 file_name = (
char*) cpl_frame_get_filename(frame);
754 img_list[i]=cpl_image_load(file_name,CPL_TYPE_FLOAT,0,0);
755 cpl_imagelist_set(imset_tmp,img_list[i],i);
757 nt=cpl_imagelist_get_size(imset_tmp);
758 lo_cut=(floor)(lo_rej*nt+0.5);
759 hi_cut=(floor)(hi_rej*nt+0.5);
761 avg_img_list[ns]=cpl_imagelist_collapse_minmax_create(imset_tmp,
763 cpl_imagelist_set(imset_tot,avg_img_list[ns],ns);
766 cpl_imagelist_delete(imset_tmp);
772 sinfo_msg(
"Total raw frames nf=%d < max frm per group ng=%d",nf,ng);
773 imset_tot=cpl_imagelist_new();
774 img_list=cpl_malloc(nf *
sizeof(cpl_image *));
776 frame = cpl_frameset_get_frame(fibre_raw_set,i);
777 file_name = (
char*) cpl_frame_get_filename(frame);
778 img_list[i]=cpl_image_load(file_name,CPL_TYPE_FLOAT,0,0);
779 cpl_imagelist_set(imset_tot,img_list[i],i);
783 cpl_free(avg_img_list);
785 cpl_frameset_delete(fibre_raw_set);
786 nt=cpl_imagelist_get_size(imset_tot);
787 lo_cut=(floor)(lo_rej*nt+0.5);
788 hi_cut=(floor)(hi_rej*nt+0.5);
790 if( (img = cpl_imagelist_collapse_minmax_create(imset_tot,
791 lo_cut,hi_cut)) == NULL) {
794 cpl_imagelist_delete(imset_tot);
795 cpl_frameset_delete(sof);
796 cpl_propertylist_delete(plist) ;
800 if (cpl_image_save(img,name_o, CPL_BPP_IEEE_FLOAT,
801 plist,CPL_IO_DEFAULT)!=CPL_ERROR_NONE) {
803 cpl_imagelist_delete(imset_tot);
804 cpl_frameset_delete(sof);
805 cpl_propertylist_delete(plist) ;
808 cpl_imagelist_delete(imset_tot);
809 cpl_frameset_erase(
set,RAW_FIBRE_NS);
812 pframe = cpl_frame_new();
813 cpl_frame_set_filename(pframe, name_o) ;
814 cpl_frame_set_tag(pframe,
"FIBRE_NS") ;
815 cpl_frame_set_type(pframe, CPL_FRAME_TYPE_IMAGE) ;
816 cpl_frame_set_group(pframe, CPL_FRAME_GROUP_RAW) ;
817 cpl_frame_set_level(pframe, CPL_FRAME_LEVEL_FINAL) ;
818 if (cpl_error_get_code()) {
820 cpl_propertylist_delete(plist) ;
821 cpl_frame_delete(pframe) ;
822 cpl_image_delete(img) ;
828 if (cpl_image_save(img, name_o, CPL_BPP_IEEE_FLOAT, plist,
829 CPL_IO_DEFAULT) != CPL_ERROR_NONE) {
831 cpl_propertylist_delete(plist) ;
832 cpl_frame_delete(pframe) ;
833 cpl_image_delete(img) ;
836 cpl_propertylist_delete(plist) ;
837 cpl_image_delete(img) ;
840 cpl_frameset_insert(
set, pframe) ;
841 cpl_frameset_delete(sof);