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>
86 static int sinfo_rec_distortion_create(cpl_plugin *plugin);
87 static int sinfo_rec_distortion_exec(cpl_plugin *plugin);
88 static int sinfo_rec_distortion_destroy(cpl_plugin *plugin);
89 static int sinfo_rec_distortion(cpl_parameterlist *config, cpl_frameset *set);
90 static int new_pre_process(cpl_frameset* set,
91 const float lo_rej,
const float hi_rej,
const char* name_o);
96 static char sinfo_rec_distortion_description1[] =
97 "This recipe determines the optical distortions and the slitlets distances.\n"
98 "Necessary input are:\n"
99 "Several (usually 75) raw frames classified as FIBRE_NS\n"
100 "Standard (an 'ON' and an 'OFF') flat frames having classified as FLAT_NS\n"
101 "Standard (an 'ON' and an 'OFF') arc lamp frames having classified as WAVE_NS\n"
102 "A corresponding (band) reference arc line list classified as REF_LINE_ARC\n"
103 "A reference table with data reduction parameters classified as DRS_SETUP_WAVE.\n";
106 static char sinfo_rec_distortion_description2[] =
107 "Default output are (with their PRO.CATG)\n"
108 "A master flat: MASTER_FLAT_LAMP\n"
109 "A Bad pixel map: BP_MAP_DI\n"
110 "A fake-off fibre stacked frame: FIBRE_NS_STACKED_OFF\n"
111 "A fake-on fibre stacked frame: FIBRE_NS_STACKED_ON\n"
112 "A fake on-off fibre stacked frame: FIBRE_NS_STACKED\n"
113 "A table with optical distortion coefficients: DISTORTION\n";
116 static char sinfo_rec_distortion_description3[] =
117 "A distortion corrected frame: FIBRE_NS_STACKED_DIST\n"
118 "A temporary frame: MASTER_SLIT\n"
119 "A table with the slitlets distances: SLITLETS_DISTANCE\n"
122 static char sinfo_rec_distortion_description[1300];
147 cpl_recipe *recipe = cpl_calloc(1,
sizeof *recipe);
148 cpl_plugin *plugin = &recipe->interface;
149 strcpy(sinfo_rec_distortion_description,sinfo_rec_distortion_description1);
150 strcat(sinfo_rec_distortion_description,sinfo_rec_distortion_description2);
151 strcat(sinfo_rec_distortion_description,sinfo_rec_distortion_description3);
154 cpl_plugin_init(plugin,
156 SINFONI_BINARY_VERSION,
157 CPL_PLUGIN_TYPE_RECIPE,
158 "sinfo_rec_distortion",
159 "Finds optical distortions and slitlets distances",
160 sinfo_rec_distortion_description,
162 "Andrea.Modigliani@eso.org",
164 sinfo_rec_distortion_create,
165 sinfo_rec_distortion_exec,
166 sinfo_rec_distortion_destroy);
168 cpl_pluginlist_append(list, plugin);
184 static int sinfo_rec_distortion_create(cpl_plugin *plugin)
186 cpl_recipe * recipe ;
189 if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
190 recipe = (cpl_recipe *)plugin ;
196 recipe->parameters = cpl_parameterlist_new() ;
205 sinfo_general_config_add(recipe->parameters);
206 sinfo_product_config_add(recipe->parameters);
207 sinfo_lamp_flats_config_add(recipe->parameters);
208 sinfo_bp_config_add(recipe->parameters);
210 sinfo_prepare_stacked_frames_config_add(recipe->parameters);
211 sinfo_distortion_config_add(recipe->parameters);
212 sinfo_north_south_test_config_add(recipe->parameters);
225 static int sinfo_rec_distortion_exec(cpl_plugin *plugin)
227 cpl_recipe * recipe ;
230 cpl_errorstate initial_errorstate = cpl_errorstate_get();
233 if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
234 recipe = (cpl_recipe *)plugin ;
236 status=sinfo_rec_distortion(recipe->parameters, recipe->frames);
238 if (!cpl_errorstate_is_equal(initial_errorstate)) {
241 cpl_errorstate_dump(initial_errorstate, CPL_FALSE, NULL);
254 static int sinfo_rec_distortion_destroy(cpl_plugin *plugin)
256 cpl_recipe * recipe ;
259 if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
260 recipe = (cpl_recipe *)plugin ;
263 cpl_parameterlist_delete(recipe->parameters);
282 sinfo_rec_distortion(cpl_parameterlist *config, cpl_frameset *set)
285 cpl_parameter *p=NULL;
286 cpl_frame* frame=NULL;
287 cpl_propertylist* plist=NULL;
289 cpl_frameset* raw_set=NULL;
290 cpl_frameset* set_off=NULL;
291 cpl_frameset* set_on=NULL;
292 cpl_frameset* set_fibre_ns=NULL;
293 cpl_frameset* set_flat_ns=NULL;
294 cpl_frameset* set_wave_ns=NULL;
295 char file_name[FILE_NAME_SZ];
297 fake* fk=sinfo_fake_new();
301 check_nomsg(p=cpl_parameterlist_find(config,
"sinfoni.product.density"));
302 check_nomsg(pdensity=cpl_parameter_get_int(p));
305 sinfo_msg(
"Welcome to SINFONI Pipeline release %d.%d.%d",
306 SINFONI_MAJOR_VERSION,SINFONI_MINOR_VERSION,SINFONI_MICRO_VERSION);
307 if(sinfo_dfs_set_groups(set)) {
312 check_nomsg(p=cpl_parameterlist_find(config,
"sinfoni.general.lc_sw"));
313 check_nomsg(line_cor=cpl_parameter_get_bool(p));
315 check_nomsg(sinfo_ima_line_cor(config,set));
318 cknull_nomsg(set_fibre_ns=sinfo_frameset_extract(set,RAW_FIBRE_NS));
319 cknull_nomsg(set_flat_ns=sinfo_frameset_extract(set,RAW_FLAT_NS));
320 cknull_nomsg(set_wave_ns=sinfo_frameset_extract(set,RAW_WAVE_NS));
321 check_nomsg(p=cpl_parameterlist_find(config,
"sinfoni.bp.method"));
322 check_nomsg(cpl_parameter_set_string(p,
"Normal"));
323 check_nomsg(raw_set=cpl_frameset_duplicate(set));
325 sinfo_msg(
"-----------------------------");
326 sinfo_msg(
" DETERMINE MASTER_LAMP_NS ");
327 sinfo_msg(
"-----------------------------");
329 sinfo_msg(
"REDUCE LAMPFLAT") ;
330 ck0(sinfo_new_lamp_flats(cpl_func,config, set,set_flat_ns ),
331 "reducing lampflats") ;
332 sinfo_msg(
"SUCCES: DETERMINATION MASTER_LAMP_NS") ;
334 sinfo_msg(
"-----------------------------");
335 sinfo_msg(
" DETERMINE BP_MAP_DI ");
336 sinfo_msg(
"-----------------------------");
338 check_nomsg(p=cpl_parameterlist_find(config,
"sinfoni.bp.method"));
339 check_nomsg(cpl_parameter_set_string(p,
"Normal"));
341 ck0(sinfo_new_bp_search_normal(cpl_func,config,set,set_flat_ns,
343 "determining %s",PRO_BP_MAP_DI);
344 sinfo_msg(
"SUCCESS DETERMINATION %s",PRO_BP_MAP_DI);
351 check_nomsg(set_off=cpl_frameset_duplicate(raw_set));
352 ck0_nomsg(new_pre_process(set_off, 0.0, 0.2,
"out_fibre_off.fits"));
356 if(NULL != cpl_frameset_find(set_off,RAW_FIBRE_NS)) {
357 frame = cpl_frameset_find(set_off,RAW_FIBRE_NS);
358 cpl_frameset_erase(set,RAW_FIBRE_NS);
359 cpl_frameset_insert(set,cpl_frame_duplicate(frame));
372 sinfo_msg(
"STACK FIBRE,NS TO GET FAKE OFF\n") ;
373 strcpy(fk->pro_class,RAW_FIBRE_NS);
383 ck0(sinfo_new_prepare_stacked_frames(cpl_func,config, set, set_fibre_ns,
384 PRO_FIBRE_NS_STACKED_OFF,0,fk),
385 "Error stacking frame %s",PRO_FIBRE_NS_STACKED_OFF);
387 strcpy(file_name,
"out_fibre_off.fits");
388 check_nomsg(ima=cpl_image_load(file_name,CPL_TYPE_FLOAT,0,0));
389 sinfo_free_frameset(&set_off);
391 cknull(plist = cpl_propertylist_load(file_name, 0),
392 "getting header from reference ima frame %s",file_name);
394 if (cpl_propertylist_has(plist, KEY_NAME_LAMP_HALO)) {
395 cpl_propertylist_set_bool(plist, KEY_NAME_LAMP_HALO, LAMP_OFF);
397 cpl_propertylist_append_bool(plist, KEY_NAME_LAMP_HALO,LAMP_OFF);
400 sinfo_free_propertylist(&plist);
401 sinfo_free_image(&ima);
402 sinfo_msg(
"SUCCESS: STACKED FIBRE,NS TO GET FAKE OFF\n") ;
410 check_nomsg(set_on=cpl_frameset_duplicate(raw_set));
412 ck0_nomsg(new_pre_process(set_on, 0.0, 0.0,
"out_fibre_on.fits"));
414 if(NULL != cpl_frameset_find(set_on,RAW_FIBRE_NS)) {
415 frame = cpl_frameset_find(set_on,RAW_FIBRE_NS);
416 cpl_frameset_erase(set,RAW_FIBRE_NS);
417 cpl_frameset_insert(set,cpl_frame_duplicate(frame));
424 sinfo_msg(
"STACK FIBRE,NS TO GET FAKE ON\n") ;
426 strcpy(fk->pro_class,RAW_FIBRE_NS);
436 ck0(sinfo_new_prepare_stacked_frames(cpl_func,config, set, set_fibre_ns,
437 PRO_FIBRE_NS_STACKED_ON,0,fk),
438 "error stacking frame %s",PRO_FIBRE_NS_STACKED_ON);
439 sinfo_msg(
"SUCCESS DETERMINATION %s",PRO_FIBRE_NS_STACKED_ON) ;
442 sinfo_free_frameset(&set_on);
451 sinfo_msg(
"COMBINES FAKE ON AND OFF\n") ;
452 strcpy(fk->pro_class,PRO_FIBRE_NS_STACKED);
461 ck0(sinfo_new_prepare_stacked_frames(cpl_func,config, set, set_fibre_ns,
462 PRO_FIBRE_NS_STACKED,0,fk),
463 "Stacking frame %s",PRO_FIBRE_NS_STACKED);
464 sinfo_msg(
"SUCCESS DETERMINATION %s",PRO_FIBRE_NS_STACKED) ;
474 sinfo_msg(
"STACK on WAVE frame\n") ;
475 strcpy(fk->pro_class,RAW_WAVE_NS);
484 sinfo_msg(
"STACK on WAVE frame\n") ;
485 ck0(sinfo_new_prepare_stacked_frames(cpl_func,config, set, set_wave_ns,
486 PRO_WAVE_LAMP_STACKED,0,fk),
487 "spacking frame %s",PRO_WAVE_LAMP_STACKED);
488 sinfo_msg(
"SUCCESS DETERMINATION %s",PRO_WAVE_LAMP_STACKED) ;
491 cpl_frameset_erase(set,PRO_FIBRE_NS_STACKED_OFF);
492 cpl_frameset_erase(set,PRO_FIBRE_NS_STACKED_ON);
493 cpl_frameset_erase(set,PRO_MASTER_FLAT_LAMP);
501 sinfo_msg(
"COMPUTE DISTORTIONS\n") ;
502 ck0(sinfo_new_find_distortions(cpl_func,config, set,set_fibre_ns),
503 "computing distortions");
506 if(NULL != cpl_frameset_find(set,PRO_FIBRE_NS_STACKED)) {
507 frame = cpl_frameset_find(set,PRO_FIBRE_NS_STACKED);
508 strcpy(file_name,cpl_frame_get_filename(frame));
513 check_nomsg(ima=cpl_image_load(file_name,CPL_TYPE_FLOAT,0,0));
515 check(plist = cpl_propertylist_load(file_name, 0),
516 "getting header from reference ima frame %s",file_name);
519 if (cpl_propertylist_has(plist, KEY_NAME_LAMP_HALO)) {
520 cpl_propertylist_set_bool(plist, KEY_NAME_LAMP_HALO, LAMP_ON);
522 cpl_propertylist_append_bool(plist, KEY_NAME_LAMP_HALO,LAMP_ON);
536 sinfo_free_image(&ima);
537 sinfo_free_propertylist(&plist);
538 sinfo_msg(
"SUCCESS: COMPUTED DISTORTIONS\n") ;
548 sinfo_msg(
"DISTORT FAKE FRAME\n") ;
550 strcpy(fk->pro_class,PRO_FIBRE_NS_STACKED_DIST);
559 ck0(sinfo_new_prepare_stacked_frames(cpl_func,config, set, set_fibre_ns,
560 PRO_FIBRE_NS_STACKED_DIST,0,fk),
561 "Stacking frame %s",PRO_FIBRE_NS_STACKED_DIST);
563 sinfo_msg(
"SUCCESS: DISTORTED FAKE FRAME\n") ;
567 if(NULL != cpl_frameset_find(set,PRO_FIBRE_NS_STACKED_DIST)) {
568 frame = cpl_frameset_find(set,PRO_FIBRE_NS_STACKED_DIST);
569 strcpy(file_name,cpl_frame_get_filename(frame));
574 check_nomsg(ima=cpl_image_load(file_name,CPL_TYPE_FLOAT,0,0));
575 check(plist = cpl_propertylist_load(file_name, 0),
576 "getting header from reference ima frame %s",file_name);
579 if (cpl_propertylist_has(plist, KEY_NAME_LAMP_HALO)) {
580 cpl_propertylist_set_bool(plist, KEY_NAME_LAMP_HALO, LAMP_ON);
582 cpl_propertylist_append_bool(plist, KEY_NAME_LAMP_HALO,LAMP_ON);
593 sinfo_free_propertylist(&plist);
594 sinfo_free_image(&ima);
603 cpl_frameset_erase(set,PRO_BP_MAP_DI);
604 cpl_frameset_erase(set,PRO_FIBRE_NS_STACKED);
605 cpl_frameset_erase(set,PRO_WAVE_LAMP_STACKED);
609 sinfo_msg(
"RUN NORD SOUTH TEST\n") ;
610 ck0(sinfo_new_nst(cpl_func,config, set,set_fibre_ns),
611 "Running north south test");
615 cpl_frameset_erase(set,PRO_FIBRE_NS_STACKED_DIST);
618 sinfo_msg(
"SUCCESS: RUNNED NORD SUD TEST\n") ;
619 sinfo_msg(
"SUCCESS: RECIPE\n") ;
621 sinfo_free_frameset(&raw_set);
622 sinfo_fake_delete(&fk);
627 sinfo_free_frameset(&set_on);
628 sinfo_free_frameset(&set_fibre_ns);
629 sinfo_free_frameset(&set_wave_ns);
630 sinfo_free_frameset(&set_flat_ns);
631 sinfo_free_image(&ima);
632 sinfo_free_propertylist(&plist) ;
633 sinfo_free_frameset(&set_off);
634 sinfo_free_frameset(&raw_set);
635 sinfo_free_frameset(&raw_set);
636 sinfo_fake_delete(&fk);
638 if (cpl_error_get_code() != CPL_ERROR_NONE) {
650 new_pre_process(cpl_frameset* set,
651 const float lo_rej,
const float hi_rej,
const char* name_o)
666 cpl_frame* frame=NULL;
667 cpl_frame* pframe=NULL;
668 cpl_frameset* sof=NULL;
669 cpl_frameset* fibre_raw_set=NULL;
670 char* file_name=NULL;
672 cpl_propertylist* plist=NULL;
674 cpl_imagelist* imset_tot=NULL;
675 cpl_image** avg_img_list=NULL;
679 cpl_image** img_list=NULL;
682 sof = cpl_frameset_duplicate(set);
683 fibre_raw_set = cpl_frameset_new();
685 sinfo_contains_frames_kind(sof,fibre_raw_set,RAW_FIBRE_NS);
687 nf = cpl_frameset_get_size(fibre_raw_set);
690 sinfo_free_frameset(&sof);
691 sinfo_free_frameset(&fibre_raw_set);
694 frame = cpl_frameset_get_frame(fibre_raw_set,0);
695 file_name=(
char*) cpl_frame_get_filename(frame);
698 if ((plist = cpl_propertylist_load(file_name, 0)) == NULL) {
706 sinfo_msg(
"Total raw frames nf=%d > max frm per group ng=%d",nf,ng);
709 imset_tot=cpl_imagelist_new();
710 avg_img_list=cpl_malloc((ns+1) *
sizeof(cpl_image *));
713 cpl_imagelist* imset_tmp=NULL;
715 sinfo_msg(
"iteration i=%d\n",i);
716 imset_tmp=cpl_imagelist_new();
717 img_list=cpl_malloc(ng *
sizeof(cpl_image *));
720 for (
int j=0;j<ng;j++) {
722 frame = cpl_frameset_get_frame(fibre_raw_set,k);
723 file_name=(
char*)cpl_frame_get_filename(frame);
724 cpl_image* img_tmp=cpl_image_load(file_name,CPL_TYPE_FLOAT,0,0);
725 cpl_image* img_dup=cpl_image_duplicate(img_tmp);
726 cpl_imagelist_set(imset_tmp,img_dup,j);
727 cpl_image_delete(img_tmp);
731 nt=cpl_imagelist_get_size(imset_tmp);
732 lo_cut=(floor)(lo_rej*nt+0.5);
733 hi_cut=(floor)(hi_rej*nt+0.5);
734 avg_img_list[i]=cpl_imagelist_collapse_minmax_create(imset_tmp,
736 cpl_imagelist_set(imset_tot,avg_img_list[i],i);
737 cpl_imagelist_delete(imset_tmp);
743 imset_tmp=cpl_imagelist_new();
744 img_list=cpl_malloc((nf-ns*ng) *
sizeof(cpl_image *));
747 frame = cpl_frameset_get_frame(fibre_raw_set,k);
748 file_name = (
char*) cpl_frame_get_filename(frame);
750 img_list[i]=cpl_image_load(file_name,CPL_TYPE_FLOAT,0,0);
751 cpl_imagelist_set(imset_tmp,img_list[i],i);
753 nt=cpl_imagelist_get_size(imset_tmp);
754 lo_cut=(floor)(lo_rej*nt+0.5);
755 hi_cut=(floor)(hi_rej*nt+0.5);
757 avg_img_list[ns]=cpl_imagelist_collapse_minmax_create(imset_tmp,
759 cpl_imagelist_set(imset_tot,avg_img_list[ns],ns);
762 cpl_imagelist_delete(imset_tmp);
768 sinfo_msg(
"Total raw frames nf=%d < max frm per group ng=%d",nf,ng);
769 imset_tot=cpl_imagelist_new();
770 img_list=cpl_malloc(nf *
sizeof(cpl_image *));
772 frame = cpl_frameset_get_frame(fibre_raw_set,i);
773 file_name = (
char*) cpl_frame_get_filename(frame);
774 img_list[i]=cpl_image_load(file_name,CPL_TYPE_FLOAT,0,0);
775 cpl_imagelist_set(imset_tot,img_list[i],i);
779 cpl_free(avg_img_list);
781 cpl_frameset_delete(fibre_raw_set);
782 nt=cpl_imagelist_get_size(imset_tot);
783 lo_cut=(floor)(lo_rej*nt+0.5);
784 hi_cut=(floor)(hi_rej*nt+0.5);
786 if( (img = cpl_imagelist_collapse_minmax_create(imset_tot,
787 lo_cut,hi_cut)) == NULL) {
790 cpl_imagelist_delete(imset_tot);
791 cpl_frameset_delete(sof);
792 cpl_propertylist_delete(plist) ;
796 if (cpl_image_save(img,name_o, CPL_BPP_IEEE_FLOAT,
797 plist,CPL_IO_DEFAULT)!=CPL_ERROR_NONE) {
799 cpl_imagelist_delete(imset_tot);
800 cpl_frameset_delete(sof);
801 cpl_propertylist_delete(plist) ;
804 cpl_imagelist_delete(imset_tot);
805 cpl_frameset_erase(set,RAW_FIBRE_NS);
808 pframe = cpl_frame_new();
809 cpl_frame_set_filename(pframe, name_o) ;
810 cpl_frame_set_tag(pframe,
"FIBRE_NS") ;
811 cpl_frame_set_type(pframe, CPL_FRAME_TYPE_IMAGE) ;
812 cpl_frame_set_group(pframe, CPL_FRAME_GROUP_RAW) ;
813 cpl_frame_set_level(pframe, CPL_FRAME_LEVEL_FINAL) ;
814 if (cpl_error_get_code()) {
816 cpl_propertylist_delete(plist) ;
817 cpl_frame_delete(pframe) ;
818 cpl_image_delete(img) ;
824 if (cpl_image_save(img, name_o, CPL_BPP_IEEE_FLOAT, plist,
825 CPL_IO_DEFAULT) != CPL_ERROR_NONE) {
827 cpl_propertylist_delete(plist) ;
828 cpl_frame_delete(pframe) ;
829 cpl_image_delete(img) ;
832 cpl_propertylist_delete(plist) ;
833 cpl_image_delete(img) ;
836 cpl_frameset_insert(set, pframe) ;
837 cpl_frameset_delete(sof);
void irplib_reset(void)
Reset IRPLIB state.
void sinfo_bp_dist_config_add(cpl_parameterlist *list)
Adds parameters for the spectrum extraction.
int cpl_plugin_get_info(cpl_pluginlist *list)
Build the list of available plugins, for this module.
#define sinfo_msg_error(...)
Print an error message.
#define sinfo_msg_warning(...)
Print an warning message.