midiTableToFits.c

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

Generated on 15 Mar 2012 for MIDI Pipeline Reference Manual by  doxygen 1.6.1