00001 #include <cpl.h>
00002
00003 #include "midi_utils.h"
00004 #include "midi_pfits.h"
00005 #include "midi_dfs.h"
00006 #include "string.h"
00007 #include "midiTableToFits.h"
00008 #include <config.h>
00009
00010 int table_to_fitsimage(cpl_frameset * frameset,
00011 const cpl_parameterlist * parlist,
00012 const char * columname,
00013 const char * outputfilename,
00014 cpl_table * table)
00015 {
00016
00017 cpl_propertylist * qclist;
00018
00019 int dimenDATA;
00020 cpl_array * array_data=NULL;
00021 cpl_image * image_data_int=NULL;
00022 cpl_image * image_data=NULL;
00023 cpl_image * image_stacked=NULL;
00024 cpl_image * image_stacked_raw=NULL;
00025 cpl_image * image_stacked_ycollapsed=NULL;
00026 cpl_imagelist * imagelist_data=NULL;
00027 cpl_imagelist * imagelist_data_sky=NULL;
00028 cpl_imagelist * imagelist_data_target=NULL;
00029 cpl_imagelist * imagelist_data_result=NULL;
00030 int i;
00031 int csky=0, ctarget=0;
00032 int maximum_images=0;
00033 int undefined=0;
00034 char *cubename;
00035 cpl_errorstate prestate = cpl_errorstate_get();
00036 char ** target_type;
00037 cpl_type ctype;
00038
00039
00040
00041 dimenDATA=cpl_table_get_column_dimensions(table, columname);
00042 cpl_ensure_code(cpl_errorstate_is_equal(prestate), cpl_error_get_code());
00043
00044 if (dimenDATA != 2) {
00045 return (int)cpl_error_set_message(cpl_func, cpl_error_get_code(),
00046 "DATA has a wrong dimension");
00047 }
00048
00049 cpl_msg_info(cpl_func, "Number of images in the Table: %d",cpl_table_get_nrow(table));
00050 cpl_msg_info(cpl_func, "Extracting %d images: please wait ...",cpl_table_get_nrow(table));
00051
00052 imagelist_data =cpl_imagelist_new();
00053 imagelist_data_sky =cpl_imagelist_new();
00054 imagelist_data_target=cpl_imagelist_new();
00055
00056
00057
00058
00059
00060 if (cpl_table_has_column(table, "TARTYP2"))
00061 {
00062 target_type=cpl_table_get_data_string(table, "TARTYP2");
00063
00064 }
00065 else
00066 {
00067 cpl_msg_warning(cpl_func, "TARTYP2 not found, using TARTYP1");
00068 target_type=cpl_table_get_data_string(table, "TARTYP1");
00069 }
00070
00071
00072
00073 maximum_images=0;
00074 undefined=0;
00075
00076 for (i=0; i<cpl_table_get_nrow(table);i++){
00077 if(strcmp(target_type[i],"U")== 0 ){
00078 undefined=1;}
00079 if(strcmp(target_type[i],"S")== 0 && undefined!=0){
00080 csky++;
00081 }
00082 if(strcmp(target_type[i],"T")== 0 && undefined!=0){
00083 ctarget++;
00084 }
00085 }
00086
00087 if (csky>=ctarget) maximum_images=ctarget;
00088 else maximum_images=csky;
00089
00090
00091 csky=0, ctarget=0, undefined=0;
00092
00093
00094
00095
00096 ctype=cpl_table_get_column_type(table, columname);
00097
00098 for (i=0; i<cpl_table_get_nrow(table);i++){
00099 array_data=(cpl_array *)cpl_table_get_array(table,columname, i);
00100
00101 if(ctype&CPL_TYPE_INT){
00102 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) );
00103
00104 image_data=cpl_image_cast(image_data_int, CPL_TYPE_FLOAT);
00105 cpl_image_unwrap(image_data_int);
00106 }
00107
00108 if(ctype&CPL_TYPE_FLOAT){
00109 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) );
00110 image_data=cpl_image_cast(image_data_int, CPL_TYPE_FLOAT);
00111 cpl_image_unwrap(image_data_int);
00112 }
00113
00114
00115 cpl_imagelist_set(imagelist_data,image_data,i);
00116
00117 if(strcmp(target_type[i],"U")== 0 ){
00118 undefined=1;}
00119
00120 if(strcmp(target_type[i],"S")== 0 && csky<maximum_images && undefined!=0){
00121 cpl_imagelist_set(imagelist_data_sky,image_data,csky++);
00122 }
00123 if(strcmp(target_type[i],"T")== 0 && ctarget<maximum_images && undefined!=0){
00124 cpl_imagelist_set(imagelist_data_target,image_data,ctarget++);
00125 }
00126
00127 }
00128
00129 imagelist_data_result=cpl_imagelist_duplicate(imagelist_data_target);
00130 cpl_imagelist_subtract(imagelist_data_result,imagelist_data_sky);
00131
00132
00133 qclist = cpl_propertylist_new();
00134
00135
00136 cpl_frame_set_group (cpl_frameset_get_first(frameset), CPL_FRAME_GROUP_RAW) ;
00137
00138 cpl_propertylist_update_string(qclist, CPL_DFS_PRO_CATG, "MIDI_CUBE");
00139 cubename = cpl_sprintf("%s_%s", columname, outputfilename);
00140 if(cpl_dfs_save_imagelist(frameset,
00141 NULL,
00142 parlist,
00143 frameset,
00144 NULL,
00145 imagelist_data,
00146 CPL_BPP_IEEE_FLOAT,
00147 "midi_raw_to_fitsimage",
00148 qclist,
00149 NULL,
00150 PACKAGE "/" PACKAGE_VERSION,
00151 cubename)) {
00152
00153 (void)cpl_error_set_where(cpl_func);
00154 }
00155
00156
00157
00158 if (cpl_imagelist_get_size(imagelist_data)>1)
00159 {
00160 cpl_msg_info(cpl_func, "Saving clipping-collapsed raw fitsfile ...");
00161 image_stacked_raw=cpl_imagelist_collapse_sigclip_create(imagelist_data, 2.0, 2.0, 0.5, (1<<1), NULL);
00162 cpl_free(cubename);
00163 cubename = cpl_sprintf("%s_%s_%s", columname, "raw_timecollapsed", outputfilename);
00164
00165
00166
00167 cpl_propertylist_update_string(qclist, CPL_DFS_PRO_CATG, "MIDI_RAWTIMECOLLAPSED");
00168 if (cpl_dfs_save_image(frameset, NULL, parlist, frameset, NULL, image_stacked_raw,
00169 CPL_BPP_IEEE_FLOAT, "midi_raw_to_fitsimage",
00170 qclist, NULL,
00171 PACKAGE "/" PACKAGE_VERSION,
00172 cubename)) {
00173
00174 (void)cpl_error_set_where(cpl_func);
00175 }
00176 }
00177
00178 cpl_image_delete(image_stacked_raw);
00179
00180
00181 if(csky>0){
00182 cpl_msg_info(cpl_func, "Saving sky cube ...");
00183 cpl_free(cubename);
00184 cubename = cpl_sprintf("%s_%s_%s", columname, "sky", outputfilename);
00185
00186 cpl_propertylist_update_string(qclist, CPL_DFS_PRO_CATG, "MIDI_CUBE_SKY");
00187 if(cpl_dfs_save_imagelist(frameset,
00188 NULL,
00189 parlist,
00190 frameset,
00191 NULL,
00192 imagelist_data_sky,
00193 CPL_BPP_IEEE_FLOAT,
00194 "midi_raw_to_fitsimage",
00195 qclist,
00196 NULL,
00197 PACKAGE "/" PACKAGE_VERSION,
00198 cubename)) {
00199
00200 (void)cpl_error_set_where(cpl_func);
00201 }
00202 }
00203
00204 if(ctarget>0){
00205 cpl_msg_info(cpl_func, "Saving target cube ...");
00206 cpl_free(cubename);
00207 cubename = cpl_sprintf("%s_%s_%s", columname, "target", outputfilename);
00208
00209 cpl_propertylist_update_string(qclist, CPL_DFS_PRO_CATG, "MIDI_CUBE_TARGET");
00210 if(cpl_dfs_save_imagelist(frameset,
00211 NULL,
00212 parlist,
00213 frameset,
00214 NULL,
00215 imagelist_data_target,
00216 CPL_BPP_IEEE_FLOAT,
00217 "midi_raw_to_fitsimage",
00218 qclist,
00219 NULL,
00220 PACKAGE "/" PACKAGE_VERSION,
00221 cubename)) {
00222
00223 (void)cpl_error_set_where(cpl_func);
00224 }
00225 }
00226
00227 if(ctarget>0 && csky>0){
00228 cpl_msg_info(cpl_func, "Saving (target-sky) cube ...");
00229 cpl_free(cubename);
00230 cubename = cpl_sprintf("%s_%s_%s", columname, "result", outputfilename);
00231
00232 cpl_propertylist_update_string(qclist, CPL_DFS_PRO_CATG, "MIDI_CUBE_TARGET_MINUS_SKY");
00233 if(cpl_dfs_save_imagelist(frameset,
00234 NULL,
00235 parlist,
00236 frameset,
00237 NULL,
00238 imagelist_data_result,
00239 CPL_BPP_IEEE_FLOAT,
00240 "midi_raw_to_fitsimage",
00241 qclist,
00242 NULL,
00243 PACKAGE "/" PACKAGE_VERSION,
00244 cubename)) {
00245
00246 (void)cpl_error_set_where(cpl_func);
00247 }
00248 }
00249
00250
00251 if(ctarget>0 && csky>0){
00252
00253 cpl_msg_info(cpl_func, "Saving clipping-collapsed (target-sky) fitsfile ...");
00254 image_stacked=cpl_imagelist_collapse_sigclip_create(imagelist_data_result, 2.0, 2.0, 0.5, (1<<1), NULL);
00255 cpl_free(cubename);
00256 cubename = cpl_sprintf("%s_%s_%s", columname, "result_timecollapsed", outputfilename);
00257
00258 cpl_propertylist_update_string(qclist, CPL_DFS_PRO_CATG, "MIDI_TIMECOLLAPSED");
00259 if (cpl_dfs_save_image(frameset, NULL, parlist, frameset, NULL, image_stacked,
00260 CPL_BPP_IEEE_FLOAT, "midi_raw_to_fitsimage",
00261 qclist, NULL,
00262 PACKAGE "/" PACKAGE_VERSION,
00263 cubename)) {
00264
00265 (void)cpl_error_set_where(cpl_func);
00266 }
00267
00268 }
00269
00270 if(ctarget>0 && csky>0){
00271
00272 cpl_msg_info(cpl_func, "Saving clipping-collapsed (target-sky) plus collapsed in y direction ...");
00273
00274 image_stacked_ycollapsed=cpl_image_collapse_create(image_stacked,0);
00275 cpl_free(cubename);
00276 cubename = cpl_sprintf("%s_%s_%s", columname, "result_timecollapsed_ycollapsed", outputfilename);
00277
00278 cpl_propertylist_update_string(qclist, CPL_DFS_PRO_CATG, "MIDI_TIMECOLLAPSED_YCOLLAPSED");
00279 if (cpl_dfs_save_image(frameset, NULL, parlist, frameset, NULL, image_stacked_ycollapsed,
00280 CPL_BPP_IEEE_FLOAT, "midi_raw_to_fitsimage",
00281 qclist, NULL,
00282 PACKAGE "/" PACKAGE_VERSION,
00283 cubename)) {
00284
00285 (void)cpl_error_set_where(cpl_func);
00286 }
00287
00288 cpl_image_delete(image_stacked_ycollapsed);
00289 cpl_image_delete(image_stacked);
00290 }
00291
00292
00293
00294
00295
00296 cpl_propertylist_delete(qclist);
00297 cpl_free(cubename);
00298
00299 while(cpl_imagelist_get_size(imagelist_data_sky)>0)
00300 {
00301 cpl_imagelist_unset(imagelist_data_sky,0);
00302 }
00303
00304 while(cpl_imagelist_get_size(imagelist_data_target)>0)
00305 {
00306 cpl_imagelist_unset(imagelist_data_target,0);
00307 }
00308
00309
00310 cpl_imagelist_delete(imagelist_data);
00311 cpl_imagelist_delete(imagelist_data_result);
00312
00313 cpl_imagelist_delete(imagelist_data_sky);
00314 cpl_imagelist_delete(imagelist_data_target);
00315
00316
00317 return (int)cpl_error_get_code();
00318 }