3 #include "midi_utils.h"
4 #include "midi_pfits.h"
7 #include "midiTableToFits.h"
9 #include "midi_cplupgrade.h"
11 int table_to_fitsimage(cpl_frameset * frameset,
12 const cpl_parameterlist * parlist,
13 const char * columname,
14 const char * outputfilename,
18 cpl_propertylist * qclist;
21 cpl_array * array_data=NULL;
22 cpl_image * image_data_int=NULL;
23 cpl_image * image_data=NULL;
24 cpl_image * image_stacked=NULL;
25 cpl_image * image_stacked_raw=NULL;
26 cpl_image * image_stacked_ycollapsed=NULL;
27 cpl_imagelist * imagelist_data=NULL;
28 cpl_imagelist * imagelist_data_sky=NULL;
29 cpl_imagelist * imagelist_data_target=NULL;
30 cpl_imagelist * imagelist_data_result=NULL;
32 int csky=0, ctarget=0;
36 cpl_errorstate prestate = cpl_errorstate_get();
42 dimenDATA=cpl_table_get_column_dimensions(table, columname);
43 cpl_ensure_code(cpl_errorstate_is_equal(prestate), cpl_error_get_code());
46 return (
int)cpl_error_set_message(cpl_func, cpl_error_get_code(),
47 "DATA has a wrong dimension");
50 cpl_msg_info(cpl_func,
"Number of images in the Table: %" CPL_SIZE_FORMAT
"",cpl_table_get_nrow(table));
51 cpl_msg_info(cpl_func,
"Extracting %" CPL_SIZE_FORMAT
" images: please wait ...",cpl_table_get_nrow(table));
53 imagelist_data =cpl_imagelist_new();
54 imagelist_data_sky =cpl_imagelist_new();
55 imagelist_data_target=cpl_imagelist_new();
61 if (cpl_table_has_column(table,
"TARTYP2"))
63 target_type=cpl_table_get_data_string(table,
"TARTYP2");
68 cpl_msg_warning(cpl_func,
"TARTYP2 not found, using TARTYP1");
69 target_type=cpl_table_get_data_string(table,
"TARTYP1");
77 for (i=0; i<cpl_table_get_nrow(table);i++){
78 if(strcmp(target_type[i],
"U")== 0 ){
80 if(strcmp(target_type[i],
"S")== 0 && undefined!=0){
83 if(strcmp(target_type[i],
"T")== 0 && undefined!=0){
88 if (csky>=ctarget) maximum_images=ctarget;
89 else maximum_images=csky;
92 csky=0, ctarget=0, undefined=0;
97 ctype=cpl_table_get_column_type(table, columname);
99 for (i=0; i<cpl_table_get_nrow(table);i++){
100 array_data=(cpl_array *)cpl_table_get_array(table,columname, i);
102 if(ctype&CPL_TYPE_INT){
103 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) );
105 image_data=cpl_image_cast(image_data_int, CPL_TYPE_FLOAT);
106 cpl_image_unwrap(image_data_int);
109 if(ctype&CPL_TYPE_FLOAT){
110 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) );
111 image_data=cpl_image_cast(image_data_int, CPL_TYPE_FLOAT);
112 cpl_image_unwrap(image_data_int);
116 cpl_imagelist_set(imagelist_data,image_data,i);
118 if(strcmp(target_type[i],
"U")== 0 ){
121 if(strcmp(target_type[i],
"S")== 0 && csky<maximum_images && undefined!=0){
122 cpl_imagelist_set(imagelist_data_sky,image_data,csky++);
124 if(strcmp(target_type[i],
"T")== 0 && ctarget<maximum_images && undefined!=0){
125 cpl_imagelist_set(imagelist_data_target,image_data,ctarget++);
130 imagelist_data_result=cpl_imagelist_duplicate(imagelist_data_target);
131 cpl_imagelist_subtract(imagelist_data_result,imagelist_data_sky);
134 qclist = cpl_propertylist_new();
137 cpl_frame_set_group (cpl_frameset_get_first(frameset), CPL_FRAME_GROUP_RAW) ;
139 cpl_propertylist_update_string(qclist, CPL_DFS_PRO_CATG,
"MIDI_CUBE");
140 cubename = cpl_sprintf(
"%s_%s", columname, outputfilename);
141 if(cpl_dfs_save_imagelist(frameset,
148 "midi_raw_to_fitsimage",
151 PACKAGE
"/" PACKAGE_VERSION,
154 (void)cpl_error_set_where(cpl_func);
159 if (cpl_imagelist_get_size(imagelist_data)>1)
161 cpl_msg_info(cpl_func,
"Saving clipping-collapsed raw fitsfile ...");
162 image_stacked_raw=cpl_imagelist_collapse_sigclip_create(imagelist_data, 2.0, 2.0, 0.5, (1<<1), NULL);
164 cubename = cpl_sprintf(
"%s_%s_%s", columname,
"raw_timecollapsed", outputfilename);
168 cpl_propertylist_update_string(qclist, CPL_DFS_PRO_CATG,
"MIDI_RAWTIMECOLLAPSED");
169 if (cpl_dfs_save_image(frameset, NULL, parlist, frameset, NULL, image_stacked_raw,
170 CPL_BPP_IEEE_FLOAT,
"midi_raw_to_fitsimage",
172 PACKAGE
"/" PACKAGE_VERSION,
175 (void)cpl_error_set_where(cpl_func);
179 cpl_image_delete(image_stacked_raw);
183 cpl_msg_info(cpl_func,
"Saving sky cube ...");
185 cubename = cpl_sprintf(
"%s_%s_%s", columname,
"sky", outputfilename);
187 cpl_propertylist_update_string(qclist, CPL_DFS_PRO_CATG,
"MIDI_CUBE_SKY");
188 if(cpl_dfs_save_imagelist(frameset,
195 "midi_raw_to_fitsimage",
198 PACKAGE
"/" PACKAGE_VERSION,
201 (void)cpl_error_set_where(cpl_func);
206 cpl_msg_info(cpl_func,
"Saving target cube ...");
208 cubename = cpl_sprintf(
"%s_%s_%s", columname,
"target", outputfilename);
210 cpl_propertylist_update_string(qclist, CPL_DFS_PRO_CATG,
"MIDI_CUBE_TARGET");
211 if(cpl_dfs_save_imagelist(frameset,
216 imagelist_data_target,
218 "midi_raw_to_fitsimage",
221 PACKAGE
"/" PACKAGE_VERSION,
224 (void)cpl_error_set_where(cpl_func);
228 if(ctarget>0 && csky>0){
229 cpl_msg_info(cpl_func,
"Saving (target-sky) cube ...");
231 cubename = cpl_sprintf(
"%s_%s_%s", columname,
"result", outputfilename);
233 cpl_propertylist_update_string(qclist, CPL_DFS_PRO_CATG,
"MIDI_CUBE_TARGET_MINUS_SKY");
234 if(cpl_dfs_save_imagelist(frameset,
239 imagelist_data_result,
241 "midi_raw_to_fitsimage",
244 PACKAGE
"/" PACKAGE_VERSION,
247 (void)cpl_error_set_where(cpl_func);
252 if(ctarget>0 && csky>0){
254 cpl_msg_info(cpl_func,
"Saving clipping-collapsed (target-sky) fitsfile ...");
255 image_stacked=cpl_imagelist_collapse_sigclip_create(imagelist_data_result, 2.0, 2.0, 0.5, (1<<1), NULL);
257 cubename = cpl_sprintf(
"%s_%s_%s", columname,
"result_timecollapsed", outputfilename);
259 cpl_propertylist_update_string(qclist, CPL_DFS_PRO_CATG,
"MIDI_TIMECOLLAPSED");
260 if (cpl_dfs_save_image(frameset, NULL, parlist, frameset, NULL, image_stacked,
261 CPL_BPP_IEEE_FLOAT,
"midi_raw_to_fitsimage",
263 PACKAGE
"/" PACKAGE_VERSION,
266 (void)cpl_error_set_where(cpl_func);
271 if(ctarget>0 && csky>0){
273 cpl_msg_info(cpl_func,
"Saving clipping-collapsed (target-sky) plus collapsed in y direction ...");
275 image_stacked_ycollapsed=cpl_image_collapse_create(image_stacked,0);
277 cubename = cpl_sprintf(
"%s_%s_%s", columname,
"result_timecollapsed_ycollapsed", outputfilename);
279 cpl_propertylist_update_string(qclist, CPL_DFS_PRO_CATG,
"MIDI_TIMECOLLAPSED_YCOLLAPSED");
280 if (cpl_dfs_save_image(frameset, NULL, parlist, frameset, NULL, image_stacked_ycollapsed,
281 CPL_BPP_IEEE_FLOAT,
"midi_raw_to_fitsimage",
283 PACKAGE
"/" PACKAGE_VERSION,
286 (void)cpl_error_set_where(cpl_func);
289 cpl_image_delete(image_stacked_ycollapsed);
290 cpl_image_delete(image_stacked);
297 cpl_propertylist_delete(qclist);
300 while(cpl_imagelist_get_size(imagelist_data_sky)>0)
302 cpl_imagelist_unset(imagelist_data_sky,0);
305 while(cpl_imagelist_get_size(imagelist_data_target)>0)
307 cpl_imagelist_unset(imagelist_data_target,0);
311 cpl_imagelist_delete(imagelist_data);
312 cpl_imagelist_delete(imagelist_data_result);
314 cpl_imagelist_delete(imagelist_data_sky);
315 cpl_imagelist_delete(imagelist_data_target);
318 return (
int)cpl_error_get_code();