38 #include "midi_utils.h"
39 #include "midi_pfits.h"
42 #include "midiTableToFits.h"
44 #include "midi_cplupgrade.h"
50 static int midi_profile_create(cpl_plugin *);
51 static int midi_profile_exec(cpl_plugin *);
52 static int midi_profile_destroy(cpl_plugin *);
53 static int midi_profile(cpl_frameset *,
const cpl_parameterlist *);
54 static int midi_normalize_image_y(cpl_image * image);
55 static int table_to_imglst_sky_target(
const char * ,
60 static int append_image_to_table(cpl_table * table,
const char * columname,
61 cpl_image * image,
int row);
62 static int midi_copy_extension(
const char * infile ,
const char * outfile,
63 const char * extension_name);
64 static void midi_profile_qc_barycenter(cpl_propertylist * pro_list,
int index,
65 cpl_image * image,
const int * x_start,
const int * x_end);
66 static void midi_profile_qc_maximum(cpl_propertylist * pro_list,
int index,
67 cpl_image * image,
const int * x_start,
const int * x_end);
73 static char midi_profile_description[] =
74 "This recipe derives the 2D profile of the spectral dispersed signal in\n"
75 "the SCI_PHOT and HIGH_SENS mode. It combines a set of AOPEN and BOPEN\n"
76 "photometry files with the following tags:\n"
78 "PHOTOM_SP_CALIB/PHOTOM_SP_SCIENCE \n"
80 "PHOTOM_HS_CALIB/PHOTOM_HS_SCIENCE. \n"
82 "The sky subtracted data are co-added and then a two pass threshold is\n"
83 "applied in order to get a clean profile of the signal: In a first pass\n"
84 "the standard deviation of the image is derived and all the pixels\n"
85 "exceeding the latter are marked as they are supposed to belong to the\n"
86 "source and not to the background. In a second pass the standard\n"
87 "deviation is recalculated excluding these source-pixels. After that,\n"
88 "all pixels of the image not exceeding (<threshold> times\n"
89 "standard-deviation) are set to zero. Please note, that the <threshold>\n"
90 "variable can be set as a recipe option in sigma units. Finally the\n"
91 "integral of the profile perpendicular to the dispersion direction is\n"
92 "normalized to unity.\n"
95 " DO category: Type: Explanation: Required:\n"
96 " PHOTOM_SP_CALIB Raw Raw data frame \n"
98 " PHOTOM_SP_SCIENCE Raw Raw data frame \n"
100 " PHOTOM_HS_CALIB Raw Raw data frame \n"
102 " PHOTOM_HS_SCIENCE Raw Raw data frame \n\n"
104 " DO category: Data type: Explanation:\n"
105 " KAPPA_HS_MASK_PRISM FITS table Spectral profile: Main product\n"
107 " KAPPA_HS_MASK_GRISM FITS table Spectral profile: Main product \n\n"
108 " KAPPA_HS_MASK_PRISM_PROFILE FITS image single profile: For diagnostics\n"
110 " KAPPA_HS_MASK_GRISM_PROFILE FITS image single profile: For diagnostics\n\n"
111 " KAPPA_HS_MASK_PRISM_SIGNAL FITS image single signal: For diagnostics\n"
113 " KAPPA_HS_MASK_GRISM_SIGNAL FITS image single signal: For diagnostics\n";
141 cpl_recipe * recipe = cpl_calloc(1,
sizeof *recipe );
142 cpl_plugin * plugin = &recipe->interface;
144 if (cpl_plugin_init(plugin,
147 CPL_PLUGIN_TYPE_RECIPE,
149 "Derives the Profile of the spectrum",
150 midi_profile_description,
156 midi_profile_destroy)) {
157 cpl_msg_error(cpl_func,
"Plugin initialization failed");
158 (void)cpl_error_set_where(cpl_func);
162 if (cpl_pluginlist_append(list, plugin)) {
163 cpl_msg_error(cpl_func,
"Error adding plugin to list");
164 (void)cpl_error_set_where(cpl_func);
180 static int midi_profile_create(cpl_plugin * plugin)
186 if (cpl_error_get_code() != CPL_ERROR_NONE) {
187 cpl_msg_error(cpl_func,
"%s():%d: An error is already set: %s",
188 cpl_func, __LINE__, cpl_error_get_where());
189 return (
int)cpl_error_get_code();
192 if (plugin == NULL) {
193 cpl_msg_error(cpl_func,
"Null plugin");
194 cpl_ensure_code(0, (
int)CPL_ERROR_NULL_INPUT);
198 if (cpl_plugin_get_type(plugin) != CPL_PLUGIN_TYPE_RECIPE) {
199 cpl_msg_error(cpl_func,
"Plugin is not a recipe");
200 cpl_ensure_code(0, (
int)CPL_ERROR_TYPE_MISMATCH);
204 recipe = (cpl_recipe *)plugin;
207 recipe->parameters = cpl_parameterlist_new();
208 if (recipe->parameters == NULL) {
209 cpl_msg_error(cpl_func,
"Parameter list allocation failed");
210 cpl_ensure_code(0, (
int)CPL_ERROR_ILLEGAL_OUTPUT);
215 p = cpl_parameter_new_value(
"midi.midi_profile.threshold",
216 CPL_TYPE_DOUBLE,
"The threshold to discriminate between source and background pixels in sigma units",
217 "midi.midi_profile",1.7);
218 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI,
"threshold");
219 cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV);
220 cpl_parameterlist_append(recipe->parameters, p);
233 static int midi_profile_exec(cpl_plugin * plugin)
238 cpl_errorstate initial_errorstate = cpl_errorstate_get();
241 if (cpl_error_get_code() != CPL_ERROR_NONE) {
242 cpl_msg_error(cpl_func,
"%s():%d: An error is already set: %s",
243 cpl_func, __LINE__, cpl_error_get_where());
244 return (
int)cpl_error_get_code();
247 if (plugin == NULL) {
248 cpl_msg_error(cpl_func,
"Null plugin");
249 cpl_ensure_code(0, (
int)CPL_ERROR_NULL_INPUT);
253 if (cpl_plugin_get_type(plugin) != CPL_PLUGIN_TYPE_RECIPE) {
254 cpl_msg_error(cpl_func,
"Plugin is not a recipe");
255 cpl_ensure_code(0, (
int)CPL_ERROR_TYPE_MISMATCH);
259 recipe = (cpl_recipe *)plugin;
262 if (recipe->parameters == NULL) {
263 cpl_msg_error(cpl_func,
"Recipe invoked with NULL parameter list");
264 cpl_ensure_code(0, (
int)CPL_ERROR_NULL_INPUT);
266 if (recipe->frames == NULL) {
267 cpl_msg_error(cpl_func,
"Recipe invoked with NULL frame set");
268 cpl_ensure_code(0, (
int)CPL_ERROR_NULL_INPUT);
272 recipe_status = midi_profile(recipe->frames, recipe->parameters);
275 if (cpl_dfs_update_product_header(recipe->frames)) {
276 if (!recipe_status) recipe_status = (int)cpl_error_get_code();
279 if (!cpl_errorstate_is_equal(initial_errorstate)) {
282 cpl_errorstate_dump(initial_errorstate, CPL_FALSE, NULL);
285 return recipe_status;
295 static int midi_profile_destroy(cpl_plugin * plugin)
299 if (plugin == NULL) {
300 cpl_msg_error(cpl_func,
"Null plugin");
301 cpl_ensure_code(0, (
int)CPL_ERROR_NULL_INPUT);
305 if (cpl_plugin_get_type(plugin) != CPL_PLUGIN_TYPE_RECIPE) {
306 cpl_msg_error(cpl_func,
"Plugin is not a recipe");
307 cpl_ensure_code(0, (
int)CPL_ERROR_TYPE_MISMATCH);
311 recipe = (cpl_recipe *)plugin;
313 cpl_parameterlist_delete(recipe->parameters);
326 static int midi_profile(cpl_frameset * frameset,
327 const cpl_parameterlist * parlist)
329 cpl_propertylist * plist=NULL;
330 cpl_propertylist * pro_list=NULL;
332 cpl_propertylist * qclist_all_extension;
333 cpl_propertylist **pHeader_ext= NULL;
335 const cpl_parameter * param;
337 cpl_frame * cur_frame=NULL;
339 cpl_table * table=NULL;
340 cpl_table * profile_table=NULL;
341 cpl_table ** pTable_ext= NULL;
343 cpl_imagelist * imglst_AOPEN_DATA_S[4];
344 cpl_imagelist * imglst_AOPEN_DATA_T[4];
345 cpl_imagelist * imglst_BOPEN_DATA_S[4];
346 cpl_imagelist * imglst_BOPEN_DATA_T[4];
349 cpl_image * image_AOPEN_DATA_T[4];
350 cpl_image * image_BOPEN_DATA_T[4];
351 cpl_image * image_AOPEN_DATA_T_float[4];
352 cpl_image * image_BOPEN_DATA_T_float[4];
354 cpl_mask * image_AOPEN_DATA_T_mask[4];
355 cpl_mask * image_BOPEN_DATA_T_mask[4];
357 cpl_image * dummy_image;
359 cpl_errorstate prestate = cpl_errorstate_get();
362 char * dataname=NULL;
363 char * dataname1=NULL;
364 char gris_name[100]=
"";
365 char * first_valid_frame=NULL;
368 const char * shutter_id =NULL;
371 int ext_imaging_data=0;
372 int ext_imaging_detector=0;
381 double std_AOPEN [4];
388 const int x_pos_grism_start[]={38, 52, 116, 176, 217};
389 const int x_pos_grism_end[] ={51, 69, 137, 201, 246};
391 const int x_pos_prism_start[]={120, 113, 88, 60, 36};
392 const int x_pos_prism_end[] ={127, 122, 99, 73, 51};
394 param = cpl_parameterlist_find_const(parlist,
395 "midi.midi_profile.threshold");
396 threshold = cpl_parameter_get_double(param);
399 cur_frame = cpl_frameset_get_first(frameset);
400 if (cur_frame == NULL) {
401 return (
int)cpl_error_set_message(cpl_func, CPL_ERROR_DATA_NOT_FOUND,
402 "SOF does not have any file");
406 pTable_ext = cpl_malloc((extnum) *
sizeof(cpl_table *));
407 pHeader_ext = cpl_malloc((extnum) *
sizeof(cpl_propertylist *));
412 cpl_error_get_code());
415 cur_frame = cpl_frameset_get_first(frameset);
420 tag = (
char*)cpl_frame_get_tag(cur_frame);
421 if (strcmp(tag,MIDI_PHOTOM_SP_CALIB) ==0 || strcmp(tag,MIDI_PHOTOM_SP_SCIENCE)==0) {
425 if (strcmp(tag,MIDI_PHOTOM_HS_CALIB) ==0 || strcmp(tag,MIDI_PHOTOM_HS_SCIENCE)==0) {
432 cur_frame = cpl_frameset_get_next( frameset );
438 if (isHS>0 && isSP>0) {
439 return (
int)cpl_error_set_message(cpl_func, CPL_ERROR_DATA_NOT_FOUND,
440 "SOF has mixed data: SCI_PHOT and HIGH_SENS. This is not allowed!");
444 for (i=0; i<dimenDATA;i++){
446 imglst_AOPEN_DATA_S[i]=cpl_imagelist_new();
447 imglst_AOPEN_DATA_T[i]=cpl_imagelist_new();
449 imglst_BOPEN_DATA_S[i]=cpl_imagelist_new();
450 imglst_BOPEN_DATA_T[i]=cpl_imagelist_new();
461 cur_frame = cpl_frameset_get_first(frameset);
462 if (cur_frame == NULL) {
463 return (
int)cpl_error_set_message(cpl_func, CPL_ERROR_DATA_NOT_FOUND,
464 "SOF does not have any file");
470 tag = (
char*)cpl_frame_get_tag(cur_frame);
471 if (strcmp(tag, MIDI_PHOTOM_SP_SCIENCE) && strcmp(tag, MIDI_PHOTOM_SP_CALIB) &&
472 strcmp(tag, MIDI_PHOTOM_HS_SCIENCE) && strcmp(tag, MIDI_PHOTOM_HS_CALIB)) {
473 cur_frame = cpl_frameset_get_next( frameset );
477 cpl_msg_info(cpl_func,
"Processing file %s",cpl_frame_get_filename(cur_frame));
478 ext_imaging_data=cpl_fits_find_extension(cpl_frame_get_filename(cur_frame),
"IMAGING_DATA");
479 ext_imaging_detector=cpl_fits_find_extension(cpl_frame_get_filename(cur_frame),
"IMAGING_DETECTOR");
488 first_valid_frame=cpl_sprintf(cpl_frame_get_filename(cur_frame));
490 for (i=0; i<extnum; i++)
492 pHeader_ext[i] = cpl_propertylist_load(cpl_frame_get_filename(cur_frame), ext_imaging_detector );
493 pTable_ext[i] = cpl_table_load(cpl_frame_get_filename(cur_frame), ext_imaging_detector, 1 );
495 if (pHeader_ext[i] ==NULL || pTable_ext[i]==NULL)
497 cpl_msg_error(cpl_func,
"Error loading the extended header or table of %s",cpl_frame_get_filename(cur_frame));
499 for (i=0; i<extnum; i++)
501 cpl_propertylist_delete(pHeader_ext[i]);
502 cpl_table_delete(pTable_ext[i]);
504 cpl_free (pHeader_ext);
505 cpl_free (pTable_ext);
517 plist = cpl_propertylist_load(cpl_frame_get_filename(cur_frame), 0);
518 if (cpl_propertylist_has(plist,
"ESO INS SHUT ID") == 1)
520 shutter_id=(cpl_propertylist_get_string(plist,
"ESO INS SHUT ID"));
524 if (cpl_propertylist_has(plist,
"ESO INS GRIS NAME") == 1)
526 strcpy(gris_name,cpl_propertylist_get_string(plist,
"ESO INS GRIS NAME"));
529 if (!cpl_errorstate_is_equal(prestate)) {
530 return (
int)cpl_error_set_message(cpl_func, cpl_error_get_code(),
"Could not ...");
533 if (strcmp(shutter_id,
"AOPEN")==0)
539 table = cpl_table_load(cpl_frame_get_filename(cur_frame), ext_imaging_data, 1);
541 return (
int)cpl_error_set_message(cpl_func, cpl_error_get_code(),
542 "Could not load the table");
544 cpl_ensure_code(cpl_errorstate_is_equal(prestate), cpl_error_get_code());
547 for (i=0; i<dimenDATA;i++){
549 dataname=cpl_sprintf(
"DATA%d",i+1);
552 if (cpl_table_has_column(table,dataname)){
553 table_to_imglst_sky_target(dataname,
"TARTYP2",imglst_AOPEN_DATA_S[i],imglst_AOPEN_DATA_T[i],table);
556 cpl_msg_info(cpl_func,
"Number of so far processed AOPEN %s patches: % " CPL_SIZE_FORMAT
"",dataname,cpl_imagelist_get_size(imglst_AOPEN_DATA_T[i]));
560 cpl_ensure_code(cpl_errorstate_is_equal(prestate), cpl_error_get_code());
562 cpl_table_delete(table);
565 if (strcmp(shutter_id,
"BOPEN")==0)
571 table = cpl_table_load(cpl_frame_get_filename(cur_frame), ext_imaging_data, 1);
573 return (
int)cpl_error_set_message(cpl_func, cpl_error_get_code(),
574 "Could not load the table");
576 cpl_ensure_code(cpl_errorstate_is_equal(prestate), cpl_error_get_code());
579 for (i=0; i<dimenDATA;i++){
581 dataname=cpl_sprintf(
"DATA%d",i+1);
583 if (cpl_table_has_column(table,dataname)){
584 table_to_imglst_sky_target(dataname,
"TARTYP2",imglst_BOPEN_DATA_S[i],imglst_BOPEN_DATA_T[i],table);
587 cpl_msg_info(cpl_func,
"Number of so far processed BOPEN %s patches: % " CPL_SIZE_FORMAT
"",dataname,cpl_imagelist_get_size(imglst_BOPEN_DATA_T[i]));
591 cpl_ensure_code(cpl_errorstate_is_equal(prestate), cpl_error_get_code());
593 cpl_table_delete(table);
597 cpl_propertylist_delete(plist);
600 cur_frame = cpl_frameset_get_next( frameset );
605 if (isPHOTAdata==0 || isPHOTBdata==0)
607 cpl_msg_error(cpl_func,
"No suitable SetOfFrame fround");
608 (void)cpl_error_set_where(cpl_func);
609 return (
int)cpl_error_set_message(cpl_func, cpl_error_get_code(),
"No suitable SetOfFrame fround");
679 cpl_msg_info(cpl_func,
"Doing sky subtraction ...");
682 for (i=0; i<dimenDATA;i++){
684 if(cpl_imagelist_get_size(imglst_AOPEN_DATA_T[i])==cpl_imagelist_get_size(imglst_AOPEN_DATA_S[i])
685 && cpl_imagelist_get_size(imglst_BOPEN_DATA_T[i])==cpl_imagelist_get_size(imglst_BOPEN_DATA_S[i])
688 cpl_imagelist_subtract(imglst_AOPEN_DATA_T[i],imglst_AOPEN_DATA_S[i]);
689 cpl_imagelist_subtract(imglst_BOPEN_DATA_T[i],imglst_BOPEN_DATA_S[i]);
694 cpl_msg_info(cpl_func,
"The number of Skyframes and Targetframes differ");
695 return (
int)cpl_error_set_message(cpl_func, cpl_error_get_code(),
696 "The number of Skyframes and Targetframes differ");
702 cpl_msg_info(cpl_func,
"Collapsing the images in the time domain ...");
705 for (i=0; i<dimenDATA;i++){
707 dummy_image=cpl_imagelist_collapse_create(imglst_AOPEN_DATA_T[i]);
708 image_AOPEN_DATA_T[i]=cpl_image_cast(dummy_image,CPL_TYPE_DOUBLE);
709 cpl_image_delete(dummy_image);
711 dummy_image=cpl_imagelist_collapse_create(imglst_BOPEN_DATA_T[i]);
712 image_BOPEN_DATA_T[i]=cpl_image_cast(dummy_image,CPL_TYPE_DOUBLE);
713 cpl_image_delete(dummy_image);
721 cpl_image_add(image_AOPEN_DATA_T[0], image_BOPEN_DATA_T[0]);
722 cpl_image_add(image_AOPEN_DATA_T[1], image_BOPEN_DATA_T[1]);
728 cpl_image_add(image_AOPEN_DATA_T[1], image_BOPEN_DATA_T[1]);
729 cpl_image_add(image_AOPEN_DATA_T[2], image_BOPEN_DATA_T[2]);
733 pro_list = cpl_propertylist_new();
738 for (i=0; i<dimenDATA;i++){
739 if (strcmp(gris_name,
"GRISM")==0 && isSP>0) cpl_propertylist_update_string(pro_list, CPL_DFS_PRO_CATG,
"KAPPA_SP_MASK_GRISM_SIGNAL");
740 if (strcmp(gris_name,
"PRISM")==0 && isSP>0) cpl_propertylist_update_string(pro_list, CPL_DFS_PRO_CATG,
"KAPPA_SP_MASK_PRISM_SIGNAL");
741 if (strcmp(gris_name,
"GRISM")==0 && isHS>0) cpl_propertylist_update_string(pro_list, CPL_DFS_PRO_CATG,
"KAPPA_HS_MASK_GRISM_SIGNAL");
742 if (strcmp(gris_name,
"PRISM")==0 && isHS>0) cpl_propertylist_update_string(pro_list, CPL_DFS_PRO_CATG,
"KAPPA_HS_MASK_PRISM_SIGNAL");
745 dataname1=cpl_sprintf(
"image_AOPEN_DATA%d_signal.fits",i+1);
746 if (cpl_dfs_save_image(frameset, NULL, parlist, frameset, NULL, image_AOPEN_DATA_T[i],
747 CPL_BPP_IEEE_FLOAT,
"midi_profile",
749 PACKAGE
"/" PACKAGE_VERSION,
752 (void)cpl_error_set_where(cpl_func);
757 dataname1=cpl_sprintf(
"image_BOPEN_DATA%d_signal.fits",i+1);
758 if (cpl_dfs_save_image(frameset, NULL, parlist, frameset, NULL, image_BOPEN_DATA_T[i],
759 CPL_BPP_IEEE_FLOAT,
"midi_profile",
761 PACKAGE
"/" PACKAGE_VERSION,
764 (void)cpl_error_set_where(cpl_func);
774 for (i=0; i<dimenDATA;i++){
775 std_AOPEN[i]=cpl_image_get_stdev(image_AOPEN_DATA_T[i]);
776 std_BOPEN[i]=cpl_image_get_stdev(image_BOPEN_DATA_T[i]);
777 cpl_msg_info(cpl_func,
"First pass STDEV of AOPEN DATA%d: %f", i+1, std_AOPEN[i]);
778 cpl_msg_info(cpl_func,
"First pass STDEV of BOPEN DATA%d: %f", i+1, std_BOPEN[i]);
785 for (i=0; i<dimenDATA;i++){
786 image_AOPEN_DATA_T_mask[i] = cpl_mask_threshold_image_create(image_AOPEN_DATA_T[i], std_AOPEN[i], FLT_MAX);
787 image_BOPEN_DATA_T_mask[i] = cpl_mask_threshold_image_create(image_BOPEN_DATA_T[i], std_BOPEN[i], FLT_MAX);
788 cpl_image_reject_from_mask(image_AOPEN_DATA_T[i], image_AOPEN_DATA_T_mask[i]) ;
789 cpl_image_reject_from_mask(image_BOPEN_DATA_T[i], image_BOPEN_DATA_T_mask[i]) ;
795 cpl_msg_info(cpl_func,
" ");
796 for (i=0; i<dimenDATA;i++){
797 std_AOPEN[i]=cpl_image_get_stdev(image_AOPEN_DATA_T[i]);
798 std_BOPEN[i]=cpl_image_get_stdev(image_BOPEN_DATA_T[i]);
799 cpl_msg_info(cpl_func,
"Second pass STDEV of AOPEN DATA%d: %f", i+1, std_AOPEN[i]);
800 cpl_msg_info(cpl_func,
"Second pass STDEV of BOPEN DATA%d: %f", i+1, std_BOPEN[i]);
804 for (i=0; i<dimenDATA;i++){
805 cpl_image_accept_all(image_AOPEN_DATA_T[i]) ;
806 cpl_image_accept_all(image_BOPEN_DATA_T[i]) ;
808 cpl_mask_delete(image_AOPEN_DATA_T_mask[i]);
809 cpl_mask_delete(image_BOPEN_DATA_T_mask[i]);
818 for (i=0; i<dimenDATA;i++){
819 cpl_image_threshold(image_AOPEN_DATA_T[i],threshold*std_AOPEN[i],FLT_MAX,0.,FLT_MAX);
820 cpl_image_threshold(image_BOPEN_DATA_T[i],threshold*std_BOPEN[i],FLT_MAX,0.,FLT_MAX);
827 for (i=0; i<dimenDATA;i++){
828 midi_normalize_image_y(image_AOPEN_DATA_T[i]);
829 midi_normalize_image_y(image_BOPEN_DATA_T[i]);
836 for (i=0; i<dimenDATA;i++){
837 image_AOPEN_DATA_T_float[i]=cpl_image_cast(image_AOPEN_DATA_T[i],CPL_TYPE_FLOAT);
838 image_BOPEN_DATA_T_float[i]=cpl_image_cast(image_BOPEN_DATA_T[i],CPL_TYPE_FLOAT);
844 cpl_msg_info(cpl_func,
"Deriving QC parameters ...");
845 image_size_y=cpl_image_get_size_y(image_AOPEN_DATA_T[0]);
855 for (i = 0; i < maxloop; i++){
857 if (strcmp(gris_name,
"GRISM")==0)
861 midi_profile_qc_barycenter(pro_list, i, image_BOPEN_DATA_T[i],
862 x_pos_grism_start,x_pos_grism_end);
863 midi_profile_qc_maximum(pro_list, i, image_BOPEN_DATA_T[i],
864 x_pos_grism_start,x_pos_grism_end);
867 midi_profile_qc_barycenter(pro_list, i, image_AOPEN_DATA_T[i],
868 x_pos_grism_start,x_pos_grism_end);
869 midi_profile_qc_maximum(pro_list, i, image_AOPEN_DATA_T[i],
870 x_pos_grism_start,x_pos_grism_end);
874 if (strcmp(gris_name,
"PRISM")==0)
878 midi_profile_qc_barycenter(pro_list, i, image_BOPEN_DATA_T[i],
879 x_pos_prism_start,x_pos_prism_end);
880 midi_profile_qc_maximum(pro_list, i, image_BOPEN_DATA_T[i],
881 x_pos_prism_start,x_pos_prism_end);
884 midi_profile_qc_barycenter(pro_list, i, image_AOPEN_DATA_T[i],
885 x_pos_prism_start,x_pos_prism_end);
886 midi_profile_qc_maximum(pro_list, i, image_AOPEN_DATA_T[i],
887 x_pos_prism_start,x_pos_prism_end);
899 profile_table=cpl_table_new(1);
902 qclist_all_extension = cpl_propertylist_new();
903 cpl_propertylist_append_string (qclist_all_extension,
"EXTNAME",
"IMAGING_DATA");
906 append_image_to_table(profile_table,
"DATA1",image_AOPEN_DATA_T_float[0],0);
907 append_image_to_table(profile_table,
"DATA2",image_AOPEN_DATA_T_float[1],0);
911 append_image_to_table(profile_table,
"DATA1",image_BOPEN_DATA_T_float[0],0);
912 append_image_to_table(profile_table,
"DATA2",image_AOPEN_DATA_T_float[1],0);
913 append_image_to_table(profile_table,
"DATA3",image_AOPEN_DATA_T_float[2],0);
914 append_image_to_table(profile_table,
"DATA4",image_AOPEN_DATA_T_float[3],0);
918 if (strcmp(gris_name,
"GRISM")==0 && isSP>0) cpl_propertylist_update_string(pro_list, CPL_DFS_PRO_CATG,
"KAPPA_SP_MASK_GRISM");
919 if (strcmp(gris_name,
"PRISM")==0 && isSP>0) cpl_propertylist_update_string(pro_list, CPL_DFS_PRO_CATG,
"KAPPA_SP_MASK_PRISM");
920 if (strcmp(gris_name,
"GRISM")==0 && isHS>0) cpl_propertylist_update_string(pro_list, CPL_DFS_PRO_CATG,
"KAPPA_HS_MASK_GRISM");
921 if (strcmp(gris_name,
"PRISM")==0 && isHS>0) cpl_propertylist_update_string(pro_list, CPL_DFS_PRO_CATG,
"KAPPA_HS_MASK_PRISM");
923 if (strcmp(gris_name,
"GRISM")==0)
925 cpl_dfs_save_table(frameset, NULL, parlist, frameset, NULL, profile_table,
926 qclist_all_extension,
"midi_profile",
928 PACKAGE
"/" PACKAGE_VERSION,
929 "midi_profile_grism.fits");
930 cpl_table_delete(profile_table);
936 midi_copy_extension(first_valid_frame,
"midi_profile_grism.fits",
"IMAGING_DETECTOR");
944 if (strcmp(gris_name,
"PRISM")==0)
946 cpl_dfs_save_table(frameset, NULL, parlist, frameset, NULL, profile_table,
947 qclist_all_extension,
"midi_profile",
949 PACKAGE
"/" PACKAGE_VERSION,
950 "midi_profile_prism.fits");
951 cpl_table_delete(profile_table);
957 midi_copy_extension(first_valid_frame,
"midi_profile_prism.fits",
"IMAGING_DETECTOR");
966 cpl_propertylist_delete(qclist_all_extension);
969 for (i=0; i<extnum; i++)
971 cpl_propertylist_delete(pHeader_ext[i]);
972 cpl_table_delete(pTable_ext[i]);
974 cpl_free (pHeader_ext);
975 cpl_free (pTable_ext);
976 cpl_free(first_valid_frame);
981 for (i=0; i<dimenDATA;i++){
982 if (strcmp(gris_name,
"GRISM")==0 && isSP>0) cpl_propertylist_update_string(pro_list, CPL_DFS_PRO_CATG,
"KAPPA_SP_MASK_GRISM_PROFILE");
983 if (strcmp(gris_name,
"PRISM")==0 && isSP>0) cpl_propertylist_update_string(pro_list, CPL_DFS_PRO_CATG,
"KAPPA_SP_MASK_PRISM_PROFILE");
984 if (strcmp(gris_name,
"GRISM")==0 && isHS>0) cpl_propertylist_update_string(pro_list, CPL_DFS_PRO_CATG,
"KAPPA_HS_MASK_GRISM_PROFILE");
985 if (strcmp(gris_name,
"PRISM")==0 && isHS>0) cpl_propertylist_update_string(pro_list, CPL_DFS_PRO_CATG,
"KAPPA_HS_MASK_PRISM_PROFILE");
988 dataname1=cpl_sprintf(
"image_AOPEN_DATA%d_profile.fits",i+1);
989 if (cpl_dfs_save_image(frameset, NULL, parlist, frameset, NULL, image_AOPEN_DATA_T[i],
990 CPL_BPP_IEEE_FLOAT,
"midi_profile",
992 PACKAGE
"/" PACKAGE_VERSION,
995 (void)cpl_error_set_where(cpl_func);
1000 dataname1=cpl_sprintf(
"image_BOPEN_DATA%d_profile.fits",i+1);
1001 if (cpl_dfs_save_image(frameset, NULL, parlist, frameset, NULL, image_BOPEN_DATA_T[i],
1002 CPL_BPP_IEEE_FLOAT,
"midi_profile",
1004 PACKAGE
"/" PACKAGE_VERSION,
1007 (void)cpl_error_set_where(cpl_func);
1010 cpl_free(dataname1);
1018 cpl_propertylist_delete(pro_list);
1021 for (i=0; i<dimenDATA;i++){
1022 cpl_image_delete(image_AOPEN_DATA_T[i]);
1023 cpl_image_delete(image_BOPEN_DATA_T[i]);
1024 cpl_image_delete(image_AOPEN_DATA_T_float[i]);
1025 cpl_image_delete(image_BOPEN_DATA_T_float[i]);
1075 for (i=0; i<dimenDATA;i++){
1076 cpl_imagelist_delete(imglst_AOPEN_DATA_T[i]);
1077 cpl_imagelist_delete(imglst_AOPEN_DATA_S[i]);
1079 cpl_imagelist_delete(imglst_BOPEN_DATA_T[i]);
1080 cpl_imagelist_delete(imglst_BOPEN_DATA_S[i]);
1090 return (
int)cpl_error_get_code();
1095 static int table_to_imglst_sky_target(
const char * columname,
1096 const char * columntype,
1097 cpl_imagelist * imglst_sky,
1098 cpl_imagelist * imglst_target,
1103 int i,csky=0, ctarget=0,csky_tmp=0, ctarget_tmp=0,first_unknown=0;
1104 cpl_array * array_data=NULL;
1105 cpl_image * image_data_int=NULL;
1106 cpl_errorstate prestate = cpl_errorstate_get();
1107 char ** target_type;
1110 int flag_istarget=0;
1112 cpl_imagelist * imglst_sky_tmp=NULL;
1113 cpl_imagelist * imglst_target_tmp=NULL;
1115 imglst_sky_tmp=cpl_imagelist_new();
1116 imglst_target_tmp=cpl_imagelist_new();
1120 dimenDATA=cpl_table_get_column_dimensions(table, columname);
1121 cpl_ensure_code(cpl_errorstate_is_equal(prestate), cpl_error_get_code());
1122 if (dimenDATA != 2) {
1123 return (
int)cpl_error_set_message(cpl_func, cpl_error_get_code(),
1124 "DATA has a wrong dimension");
1130 if (cpl_table_has_column(table, columntype))
1132 target_type=cpl_table_get_data_string(table, columntype);
1137 return (
int)cpl_error_set_message(cpl_func, cpl_error_get_code(),
1138 "TYPE of the Column not found");
1144 ctype=cpl_table_get_column_type(table, columname);
1149 csky=cpl_imagelist_get_size(imglst_sky);
1151 ctarget=cpl_imagelist_get_size(imglst_target);
1162 for (i=0; i<cpl_table_get_nrow(table);i++){
1163 if(strcmp(target_type[i],
"U")== 0){
1171 for (i=first_unknown; i<cpl_table_get_nrow(table);i++){
1173 array_data=(cpl_array *)cpl_table_get_array(table,columname, i);
1174 if(ctype&CPL_TYPE_INT){
1175 image_data_int=cpl_image_wrap_int(cpl_table_get_column_dimension(table,columname,0 ), cpl_table_get_column_dimension(table,columname,1 ), cpl_array_get_data_int(array_data) );
1177 if(ctype&CPL_TYPE_FLOAT){
1178 image_data_int=cpl_image_wrap_float(cpl_table_get_column_dimension(table,columname,0 ), cpl_table_get_column_dimension(table,columname,1 ), cpl_array_get_data_float(array_data) );
1185 if(strcmp(target_type[i],
"S")== 0){
1186 cpl_imagelist_set(imglst_sky_tmp,cpl_image_cast(image_data_int, CPL_TYPE_FLOAT),csky_tmp++);
1188 cpl_image_unwrap(image_data_int);
1191 if(strcmp(target_type[i],
"T")== 0){
1192 cpl_imagelist_set(imglst_target_tmp,cpl_image_cast(image_data_int, CPL_TYPE_FLOAT),ctarget_tmp++);
1194 cpl_image_unwrap(image_data_int);
1199 if(image_data_int!=NULL){
1200 cpl_image_unwrap(image_data_int);
1202 if(flag_issky >0 && flag_istarget >0){
1204 cpl_imagelist_set(imglst_sky,cpl_imagelist_collapse_create(imglst_sky_tmp),csky++);
1205 cpl_imagelist_set(imglst_target,cpl_imagelist_collapse_create(imglst_target_tmp),ctarget++);
1212 while(cpl_imagelist_get_size(imglst_sky_tmp)>0){
1213 cpl_image_delete(cpl_imagelist_unset(imglst_sky_tmp,0));
1215 while(cpl_imagelist_get_size(imglst_target_tmp)>0){
1216 cpl_image_delete(cpl_imagelist_unset(imglst_target_tmp,0));
1225 while(cpl_imagelist_get_size(imglst_sky_tmp)>0){
1226 cpl_image_delete(cpl_imagelist_unset(imglst_sky_tmp,0));
1228 while(cpl_imagelist_get_size(imglst_target_tmp)>0){
1229 cpl_image_delete(cpl_imagelist_unset(imglst_target_tmp,0));
1232 cpl_imagelist_delete(imglst_sky_tmp);
1233 cpl_imagelist_delete(imglst_target_tmp);
1237 return (
int)cpl_error_get_code();
1249 int midi_normalize_image_y(cpl_image * image)
1257 double * pimage=NULL;
1258 double * pimage_norm=NULL;
1260 cpl_image * image_norm;
1262 cpl_msg_info(cpl_func,
"Normalizing the integral of the profile to unity ...");
1264 image_norm=cpl_image_collapse_create(image,0);
1266 nx_image=cpl_image_get_size_x(image);
1267 ny_image=cpl_image_get_size_y(image);
1269 pimage = cpl_image_get_data_double(image);
1270 pimage_norm= cpl_image_get_data_double(image_norm);
1273 for (xpos=0; xpos<nx_image; ++xpos)
1275 for(ypos=0; ypos<ny_image;++ypos)
1277 if (pimage_norm[xpos]>0) pimage[xpos+nx_image*ypos] /= pimage_norm[xpos];
1278 if (pimage_norm[xpos]<=0) pimage[xpos+nx_image*ypos] = 0.;
1283 cpl_image_delete(image_norm);
1299 static int append_image_to_table(cpl_table * table,
const char * columname, cpl_image * image,
int row)
1302 cpl_array * array_dimension=NULL;
1303 cpl_array * array_dummy=NULL;
1305 array_dimension=cpl_array_new(2,CPL_TYPE_INT);
1306 cpl_array_set(array_dimension, 0,cpl_image_get_size_x(image));
1307 cpl_array_set(array_dimension, 1,cpl_image_get_size_y(image));
1309 cpl_table_new_column_array(table, columname, CPL_TYPE_FLOAT, cpl_image_get_size_x(image)*cpl_image_get_size_y(image));
1310 cpl_table_set_column_dimensions(table,columname,array_dimension);
1311 array_dummy = cpl_array_wrap_float(cpl_image_get_data_float(image), cpl_image_get_size_x(image)*cpl_image_get_size_y(image));
1312 cpl_table_set_array(table, columname, row, array_dummy);
1313 cpl_array_unwrap(array_dummy);
1316 cpl_array_delete(array_dimension);
1331 static int midi_copy_extension(
const char * infile ,
const char * outfile,
const char * extension_name)
1333 fitsfile * fptrin=NULL;
1334 fitsfile * fptrout=NULL;
1337 fits_open_diskfile(&fptrin, infile, READONLY, &status);
1338 fits_open_diskfile(&fptrout, outfile, READWRITE, &status);
1339 fits_movnam_hdu(fptrin, ANY_HDU, (
char *)extension_name, 0, &status);
1340 fits_copy_hdu(fptrin, fptrout, 0, &status);
1341 fits_close_file(fptrin, &status);
1342 fits_close_file(fptrout, &status);
1345 cpl_msg_error(cpl_func,
"A problem occured while copying the EXTENSION: %s", extension_name);
1365 static void midi_profile_qc_barycenter(cpl_propertylist * pro_list,
int index,
1366 cpl_image * image,
const int * x_start,
const int * x_end)
1372 image_size_y=cpl_image_get_size_y(image);
1373 for (i = 0; i < 5; i++){
1375 qcname=cpl_sprintf(
"ESO QC Y%d CENT BINNED%d",index+1,i+1);
1376 cpl_propertylist_update_double(pro_list, qcname,
1377 cpl_image_get_centroid_y_window(image, x_start[i], 1,
1378 x_end[i], image_size_y));
1396 static void midi_profile_qc_maximum(cpl_propertylist * pro_list,
int index,
1397 cpl_image * image,
const int * x_start,
const int * x_end)
1400 char * qcname_collapsed=NULL;
1403 cpl_image * image_collapsed=NULL;
1405 image_size_y=cpl_image_get_size_y(image);
1407 for (i = 0; i < 5; i++){
1408 image_collapsed=cpl_image_collapse_window_create(image, x_start[i], 1,
1409 x_end[i], image_size_y,1);
1410 qcname=cpl_sprintf(
"ESO QC Y%d MAX BIN%d",index+1,i+1);
1411 qcname_collapsed=cpl_sprintf(
"ESO QC Y%d MAXSUM BIN%d",index+1,i+1);
1413 cpl_propertylist_update_double(pro_list, qcname,
1414 cpl_image_get_max_window(image, x_start[i], 1,
1415 x_end[i], image_size_y));
1416 cpl_propertylist_update_double(pro_list, qcname_collapsed,
1417 cpl_image_get_max_window(image_collapsed, 1, 1,
1422 cpl_free(qcname_collapsed);
1423 cpl_image_delete(image_collapsed);