sinfo_rec_distortion.c

00001 /* $Id: sinfo_rec_distortion.c,v 1.22 2006/12/28 14:32:21 amodigli Exp $
00002  *
00003  * This file is part of the SINFONI Pipeline
00004  * Copyright (C) 2002,2003 European Southern Observatory
00005  *
00006  * This program is free software; you can redistribute it and/or modify
00007  * it under the terms of the GNU General Public License as published by
00008  * the Free Software Foundation; either version 2 of the License, or
00009  * (at your option) any later version.
00010  *
00011  * This program is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU General Public License
00017  * along with this program; if not, write to the Free Software
00018  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019  */
00020 
00021 /*
00022  * $Author: amodigli $
00023  * $Date: 2006/12/28 14:32:21 $
00024  * $Revision: 1.22 $
00025  * $Name:  $
00026  */
00027 
00028  /****************************************************************
00029   *          Distortions Frames Data Reduction                          *
00030   ****************************************************************/
00031 #ifdef HAVE_CONFIG_H
00032 #include <config.h>          /* allows the program compilation */
00033 #endif
00034 
00035 /*-----------------------------------------------------------------------------
00036                                 Includes
00037  ----------------------------------------------------------------------------*/
00038 /* std */
00039 #include <strings.h>
00040 #include <string.h>
00041 
00042 #include <stdio.h>
00043 #include <math.h>
00044 
00045 /* cpl */       
00046 #include <cpl.h>       
00047 #include <cpl_imagelist_basic.h>
00048 
00049 /* irplib */
00050 #include <irplib_utils.h>
00051 
00052 #include <sinfo_pro_types.h>
00053 #include <sinfo_raw_types.h>
00054 #include <sinfo_key_names.h>
00055 
00056 #include <sinfo_globals.h>
00057 #include <sinfo_general_config.h>
00058 #include <sinfo_product_config.h>
00059 #include <sinfo_lamp_flats_config.h>
00060 #include <sinfo_bp_config.h>
00061 #include <sinfo_bp_dist_config.h>
00062 #include <sinfo_prepare_stacked_frames_config.h>
00063 
00064 #include <sinfo_north_south_test_config.h>
00065 #include <sinfo_distortion_config.h>
00066 #include <sinfo_new_lamp_flats.h>
00067 #include <sinfo_bp_norm.h>
00068 #include <sinfo_new_prepare_stacked_frames.h>
00069 #include <sinfo_new_find_distortions.h>
00070 #include <sinfo_new_nst.h>
00071 
00072 #include <sinfo_functions.h>
00073 #include <sinfo_tpl_utils.h>
00074 #include <sinfo_tpl_dfs.h>
00075 #include <sinfo_msg.h>
00076 #include <sinfo_error.h>
00077 #include <sinfo_utils_wrappers.h>
00078 #define CPL_BPP_DEFAULT  CPL_BPP_IEEE_FLOAT
00079 
00080 
00081 
00082 /*-----------------------------------------------------------------------------
00083                             Functions prototypes
00084  ----------------------------------------------------------------------------*/
00085 
00086 static int sinfo_rec_distortion_create(cpl_plugin *plugin);
00087 static int sinfo_rec_distortion_exec(cpl_plugin *plugin);
00088 static int sinfo_rec_distortion_destroy(cpl_plugin *plugin);
00089 static int sinfo_rec_distortion(cpl_parameterlist *config, cpl_frameset *set);
00090 static int new_pre_process(cpl_parameterlist* config, cpl_frameset* set, 
00091         const float lo_rej, const float hi_rej, const char* name_o);
00092 
00093 /*-----------------------------------------------------------------------------
00094                             Static variables
00095  ----------------------------------------------------------------------------*/
00096 static char sinfo_rec_distortion_description1[] =
00097 "This recipe determines the optical distortions and the slitlets distances.\n"
00098 "Necessary input are:\n"
00099 "Several raw frames classified as FIBRE_NS\n"
00100 "Standard flat frames having classified as FLAT_NS\n"
00101 "Standard arc lamp frames having classified as WAVE_NS\n"
00102 "A corresponding (band) reference arc line list classified as REF_LINE_ARC\n"
00103 "A reference table with data reduction parameters classified as DRS_WAVE_NS.\n";
00104 
00105 
00106 static char sinfo_rec_distortion_description2[] =
00107 "Output are (with their PRO.CATG)\n"
00108 "A master flat: MASTER_FLAT_LAMP\n"
00109 "A Bad pixel map: BP_MAP_DI\n"
00110 "A fake-off fibre stacked frame: FIBRE_NS_STACKED_OFF\n"
00111 "A fake-on fibre  stacked frame: FIBRE_NS_STACKED_ON\n"
00112 "A fake on-off fibre  stacked frame: FIBRE_NS_STACKED\n"
00113 "A table with optical distortion coefficients: DISTORTION\n";
00114 
00115 
00116 static char sinfo_rec_distortion_description3[] =
00117 "A distortion corrected frame: FIBRE_NS_STACKED_DIST\n"
00118 "A temporary frame: MASTER_SLIT\n"
00119 "A table with the slitlets distances: SLITLETS_DISTANCE\n"
00120 "Information on relevant parameters can be found with:\n"
00121 "esorex --params sinfo_rec_distortion\n"
00122 "esorex --help sinfo_rec_distortion\n"
00123 "\n";
00124 
00125 static char sinfo_rec_distortion_description[1300];
00126 
00127 /*-----------------------------------------------------------------------------
00128                                 Functions code
00129  ----------------------------------------------------------------------------*/
00130 /*---------------------------------------------------------------------------*/
00134 /*---------------------------------------------------------------------------*/
00136 /*---------------------------------------------------------------------------*/
00145 /*---------------------------------------------------------------------------*/
00146 int
00147 cpl_plugin_get_info(cpl_pluginlist *list)
00148 {
00149 
00150   cpl_recipe *recipe = cpl_calloc(1, sizeof *recipe);
00151   cpl_plugin *plugin = &recipe->interface;
00152   strcpy(sinfo_rec_distortion_description,sinfo_rec_distortion_description1);
00153   strcat(sinfo_rec_distortion_description,sinfo_rec_distortion_description2);
00154   strcat(sinfo_rec_distortion_description,sinfo_rec_distortion_description3);
00155  
00156 
00157   cpl_plugin_init(plugin,
00158           CPL_PLUGIN_API,
00159           SINFONI_BINARY_VERSION,
00160           CPL_PLUGIN_TYPE_RECIPE,
00161           "sinfo_rec_distortion",
00162           "Finds optical distortions and slitlets distances",
00163           sinfo_rec_distortion_description,
00164           "Andrea Modigliani",
00165           "Andrea.Modigliani@eso.org",
00166           sinfo_get_license(),
00167           sinfo_rec_distortion_create,
00168           sinfo_rec_distortion_exec,
00169           sinfo_rec_distortion_destroy);
00170 
00171   cpl_pluginlist_append(list, plugin);
00172 
00173   return 0;
00174 
00175 }
00176 
00177 
00178 /*---------------------------------------------------------------------------*/
00186 /*---------------------------------------------------------------------------*/
00187 static int sinfo_rec_distortion_create(cpl_plugin *plugin)
00188 {
00189     cpl_recipe      * recipe ;
00190 
00191     /* Check that the plugin is part of a valid recipe */
00192     if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE) 
00193         recipe = (cpl_recipe *)plugin ;
00194     else return -1 ;
00195     cpl_error_reset();
00196     irplib_reset();
00197 
00198     /* Create the parameters list in the cpl_recipe object */
00199     recipe->parameters = cpl_parameterlist_new() ; 
00200 
00201 
00202   /*
00203    * Fill the parameter list.
00204    */
00205 
00206 
00207   /* Output file name */
00208   sinfo_product_config_add(recipe->parameters);
00209   sinfo_general_config_add(recipe->parameters);
00210   sinfo_lamp_flats_config_add(recipe->parameters);
00211   sinfo_bp_config_add(recipe->parameters);  
00212   sinfo_bp_dist_config_add(recipe->parameters); 
00213   sinfo_prepare_stacked_frames_config_add(recipe->parameters);
00214   sinfo_distortion_config_add(recipe->parameters);
00215   sinfo_north_south_test_config_add(recipe->parameters);
00216   
00217 
00218   return 0;
00219 
00220 }
00221 /*---------------------------------------------------------------------------*/
00227 /*---------------------------------------------------------------------------*/
00228 static int sinfo_rec_distortion_exec(cpl_plugin *plugin)
00229 {
00230     cpl_recipe  *   recipe ;
00231     
00232     /* Get the recipe out of the plugin */
00233     if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE) 
00234         recipe = (cpl_recipe *)plugin ;
00235     else return -1 ;
00236 
00237   return sinfo_rec_distortion(recipe->parameters, recipe->frames);
00238 
00239 }
00240 
00241 /*---------------------------------------------------------------------------*/
00247 /*---------------------------------------------------------------------------*/
00248 static int sinfo_rec_distortion_destroy(cpl_plugin *plugin)
00249 {
00250     cpl_recipe  *   recipe ;
00251     
00252     /* Get the recipe out of the plugin */
00253     if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE) 
00254         recipe = (cpl_recipe *)plugin ;
00255     else return -1 ;
00256 
00257   cpl_parameterlist_delete(recipe->parameters);
00258 
00259   return 0;
00260 
00261 }
00262 
00263 /*---------------------------------------------------------------------------*/
00270 /*---------------------------------------------------------------------------*/
00271 /*
00272  * The actual recipe actually start here.
00273  */
00274 
00275 static int
00276 sinfo_rec_distortion(cpl_parameterlist *config, cpl_frameset *set)
00277 {
00278  
00279   cpl_parameter *p=NULL;
00280   cpl_frame* frame=NULL;
00281   cpl_propertylist* plist=NULL;
00282   cpl_image* ima=NULL;
00283   cpl_frameset* raw_set=NULL;
00284   cpl_frameset* set_off=NULL;
00285   cpl_frameset* set_on=NULL;
00286   char file_name[FILE_NAME_SZ];
00287 
00288   fake* fk=sinfo_fake_new();
00289   
00290   sinfo_msg("Welcome to SINFONI Pipeline release %d.%d.%d",
00291         SINFONI_MAJOR_VERSION,SINFONI_MINOR_VERSION,SINFONI_MICRO_VERSION);
00292   if(sinfo_dfs_set_groups(set)) {
00293     sinfo_msg_error("Cannot indentify RAW and CALIB frames") ;
00294     return -1;
00295   }
00296 
00297   check_nomsg(p=cpl_parameterlist_find(config,"sinfoni.bp.method"));
00298   check_nomsg(cpl_parameter_set_string(p,"Normal"));   
00299   check_nomsg(raw_set=cpl_frameset_duplicate(set));
00300     
00301   sinfo_msg("-----------------------------");
00302   sinfo_msg("  DETERMINE MASTER_LAMP_NS ");
00303   sinfo_msg("-----------------------------");
00304 
00305   sinfo_msg("REDUCE LAMPFLAT") ;    
00306   ck0(sinfo_new_lamp_flats(cpl_func,config, set ),"reducing lampflats") ;
00307   sinfo_msg("SUCCES: DETERMINATION MASTER_LAMP_NS") ; 
00308 
00309   sinfo_msg("-----------------------------");
00310   sinfo_msg("  DETERMINE BP_MAP_DI        ");
00311   sinfo_msg("-----------------------------");
00312   
00313   check_nomsg(p=cpl_parameterlist_find(config,"sinfoni.bp.method"));
00314   check_nomsg(cpl_parameter_set_string(p,"Normal"));   
00315        
00316   ck0(sinfo_new_bp_search_normal(cpl_func,config,set,PRO_BP_MAP_DI),
00317       "determining %s",PRO_BP_MAP_DI);
00318   sinfo_msg("SUCCESS DETERMINATION %s",PRO_BP_MAP_DI);
00319   /* 
00320        ---------------------------------------------------------
00321         pre iteration: get a collassed frame 
00322        ---------------------------------------------------------
00323    */
00324 
00325    check_nomsg(set_off=cpl_frameset_duplicate(raw_set));
00326 
00327    ck0_nomsg(new_pre_process(config,set_off, 0.0, 0.2,"out_fibre_off.fits"));
00328 
00329   
00330    /* add the off artificial frame to the frameset */
00331    if(NULL != cpl_frameset_find(set_off,RAW_FIBRE_NS)) {
00332        frame = cpl_frameset_find(set_off,RAW_FIBRE_NS);
00333        cpl_frameset_erase(set,RAW_FIBRE_NS);
00334        cpl_frameset_insert(set,cpl_frame_duplicate(frame));
00335    } else {
00336        sinfo_msg_error("Frame %s not found!", RAW_FIBRE_NS);
00337        goto cleanup;
00338    }
00339 
00340  
00341    /* 
00342        ---------------------------------------------------------
00343         1st iteration: get off frame 
00344        ---------------------------------------------------------
00345     */
00346 
00347    sinfo_msg("STACK FIBRE,NS TO GET FAKE OFF\n") ; 
00348    strcpy(fk->pro_class,RAW_FIBRE_NS);
00349    fk->frm_switch=1;
00350    fk->mask_index=0;
00351    fk->ind_index=0;
00352    fk->flat_index=0;
00353    fk->wfix_index=0;
00354    fk->low_rej=0.0;
00355    fk->hig_rej=0.2;
00356 
00357  
00358    ck0(sinfo_new_prepare_stacked_frames(cpl_func,config, set,
00359                                         PRO_FIBRE_NS_STACKED_OFF,0,fk),
00360        "Error stacking frame %s",PRO_FIBRE_NS_STACKED_OFF);
00361 
00362 
00363    if(NULL != cpl_frameset_find(set,PRO_FIBRE_NS_STACKED_OFF)) {
00364      frame = cpl_frameset_find(set,PRO_FIBRE_NS_STACKED_OFF);
00365      strcpy(file_name,cpl_frame_get_filename(frame));
00366    } else {
00367      sinfo_msg_error("Frame %s not found!", PRO_FIBRE_NS_STACKED_OFF);
00368      goto cleanup;
00369    }
00370    check_nomsg(ima=cpl_image_load(file_name,CPL_TYPE_FLOAT,0,0));
00371    sinfo_free_frameset(&set_off);
00372         
00373 
00374    cknull(plist = cpl_propertylist_load(file_name, 0),
00375            "getting header from reference ima frame %s",file_name);
00376 
00377    if (cpl_propertylist_contains(plist, KEY_NAME_LAMP_HALO)) {
00378      cpl_propertylist_set_bool(plist, KEY_NAME_LAMP_HALO, LAMP_OFF);
00379    } else {
00380      cpl_propertylist_append_bool(plist, KEY_NAME_LAMP_HALO,LAMP_OFF) ;
00381    }
00382    sinfo_free_propertylist(&plist);
00383    sinfo_free_image(&ima);
00384    sinfo_msg("SUCCESS: STACKED FIBRE,NS TO GET FAKE OFF\n") ;      
00385 
00386    /* 
00387        ---------------------------------------------------------
00388         2nd iteration: get on frame 
00389        ---------------------------------------------------------
00390     */
00391 
00392    check_nomsg(set_on=cpl_frameset_duplicate(raw_set));
00393 
00394    ck0_nomsg(new_pre_process(config,set_on, 0.0, 0.0,"out_fibre_on.fits"));
00395 
00396    if(NULL != cpl_frameset_find(set_on,RAW_FIBRE_NS)) {
00397      frame = cpl_frameset_find(set_on,RAW_FIBRE_NS);
00398      cpl_frameset_erase(set,RAW_FIBRE_NS);
00399      cpl_frameset_insert(set,cpl_frame_duplicate(frame));
00400    } else {
00401      sinfo_msg_error("Frame %s not found!", RAW_FIBRE_NS);
00402      goto cleanup;
00403    }
00404 
00405 
00406    sinfo_msg("STACK FIBRE,NS TO GET FAKE ON\n") ; 
00407 
00408    strcpy(fk->pro_class,RAW_FIBRE_NS);
00409    fk->frm_switch=1;
00410    fk->mask_index=0;
00411    fk->ind_index=0;
00412    fk->flat_index=0;
00413    fk->wfix_index=0;
00414    fk->low_rej=0.0;
00415    fk->hig_rej=0.0;
00416 
00417 
00418    ck0(sinfo_new_prepare_stacked_frames(cpl_func,config, set, 
00419                                         PRO_FIBRE_NS_STACKED_ON,0,fk),
00420                         "error stacking frame %s",PRO_FIBRE_NS_STACKED_ON);
00421         sinfo_msg("SUCCESS DETERMINATION %s",PRO_FIBRE_NS_STACKED_ON) ; 
00422 
00423    /* cpl_frameset_erase(set,RAW_FIBRE_NS); */
00424    sinfo_free_frameset(&set_on);
00425 
00426    /* here we have a problem with pipefile of following step product */
00427    /* 
00428        ---------------------------------------------------------
00429                 3rd iteration combines on and off fake frames
00430        ---------------------------------------------------------
00431    */
00432 
00433    sinfo_msg("COMBINES FAKE ON AND OFF\n") ; 
00434    strcpy(fk->pro_class,PRO_FIBRE_NS_STACKED);
00435    fk->frm_switch=1;
00436    fk->mask_index=0;
00437    fk->ind_index=0;
00438    fk->flat_index=1;
00439    fk->wfix_index=0;
00440    fk->low_rej=0.0;
00441    fk->hig_rej=0.0;
00442 
00443    ck0(sinfo_new_prepare_stacked_frames(cpl_func,config, set, 
00444                                         PRO_FIBRE_NS_STACKED,0,fk),
00445                           "Stacking frame %s",PRO_FIBRE_NS_STACKED);
00446    sinfo_msg("SUCCESS DETERMINATION %s",PRO_FIBRE_NS_STACKED) ; 
00447        
00448    /* cpl_frameset_erase(set,RAW_FIBRE_NS); */
00449 
00450    /* 
00451        ---------------------------------------------------------
00452                 STACK WAVECAL
00453        ---------------------------------------------------------
00454    */
00455  
00456    sinfo_msg("STACK on WAVE frame\n") ;
00457    strcpy(fk->pro_class,RAW_WAVE_NS);
00458    fk->frm_switch=1;
00459    fk->mask_index=0;
00460    fk->ind_index=0;
00461    fk->flat_index=1;
00462    fk->wfix_index=0;
00463    fk->low_rej=0.1;
00464    fk->hig_rej=0.1;
00465    
00466    sinfo_msg("STACK on WAVE frame\n") ; 
00467    ck0(sinfo_new_prepare_stacked_frames(cpl_func,config, set, 
00468                                         PRO_WAVE_LAMP_STACKED,0,fk),
00469                           "spacking frame %s",PRO_WAVE_LAMP_STACKED);
00470    sinfo_msg("SUCCESS DETERMINATION %s",PRO_WAVE_LAMP_STACKED) ; 
00471 
00472    /* 
00473        ---------------------------------------------------------
00474                 DISTORTIONS 
00475        ---------------------------------------------------------
00476    */
00477 
00478    sinfo_msg("COMPUTE DISTORTIONS\n") ; 
00479    ck0(sinfo_new_find_distortions(cpl_func,config, set),"computing distortions");
00480 
00481    if(NULL != cpl_frameset_find(set,PRO_FIBRE_NS_STACKED)) {
00482      frame = cpl_frameset_find(set,PRO_FIBRE_NS_STACKED);
00483      strcpy(file_name,cpl_frame_get_filename(frame));
00484    } else {
00485      sinfo_msg_error("Frame %s not found!", PRO_FIBRE_NS_STACKED);
00486      goto cleanup;
00487    }
00488    check_nomsg(ima=cpl_image_load(file_name,CPL_TYPE_FLOAT,0,0));
00489 
00490    check(plist = cpl_propertylist_load(file_name, 0),
00491      "getting header from reference ima frame %s",file_name);
00492 
00493    if (cpl_propertylist_contains(plist, KEY_NAME_LAMP_HALO)) {
00494      cpl_propertylist_set_bool(plist, KEY_NAME_LAMP_HALO, LAMP_ON);
00495    } else {
00496      cpl_propertylist_append_bool(plist, KEY_NAME_LAMP_HALO,LAMP_ON) ;
00497    }
00498 
00499    /* Save the file */
00500    /*
00501    if (cpl_image_save(ima, file_name, CPL_BPP_DEFAULT, 
00502                       plist,CPL_IO_DEFAULT)!=CPL_ERROR_NONE) {
00503        sinfo_msg_error("Cannot save the product %s",file_name);
00504        goto cleanup;
00505 
00506    }
00507    */
00508      
00509    sinfo_free_image(&ima);
00510    sinfo_free_propertylist(&plist);
00511    sinfo_msg("SUCCESS: COMPUTED DISTORTIONS\n") ; 
00512 
00513    /* 
00514        ---------------------------------------------------------
00515        4th iteration: distort fake frame
00516        ---------------------------------------------------------
00517    */
00518 
00519 
00520    
00521    sinfo_msg("DISTORT FAKE FRAME\n") ; 
00522 
00523    strcpy(fk->pro_class,PRO_FIBRE_NS_STACKED_DIST);
00524    fk->frm_switch=1;
00525    fk->mask_index=1;
00526    fk->ind_index=1;
00527    fk->flat_index=0;
00528    fk->wfix_index=1;
00529    fk->low_rej=0.0;
00530    fk->hig_rej=0.0;
00531 
00532    ck0(sinfo_new_prepare_stacked_frames(cpl_func,config, set, 
00533                                         PRO_FIBRE_NS_STACKED_DIST,0,fk),
00534                         "Stacking frame %s",PRO_FIBRE_NS_STACKED_DIST);
00535 
00536    sinfo_msg("SUCCESS: DISTORTED FAKE FRAME\n") ; 
00537    if(NULL != cpl_frameset_find(set,PRO_FIBRE_NS_STACKED_DIST)) {
00538      frame = cpl_frameset_find(set,PRO_FIBRE_NS_STACKED_DIST);
00539      strcpy(file_name,cpl_frame_get_filename(frame));
00540    } else {
00541      sinfo_msg_error("Frame %s not found!", PRO_FIBRE_NS_STACKED_DIST);
00542      goto cleanup;
00543    }
00544    check_nomsg(ima=cpl_image_load(file_name,CPL_TYPE_FLOAT,0,0));
00545    check(plist = cpl_propertylist_load(file_name, 0),
00546      "getting header from reference ima frame %s",file_name);
00547 
00548    if (cpl_propertylist_contains(plist, KEY_NAME_LAMP_HALO)) {
00549        cpl_propertylist_set_bool(plist, KEY_NAME_LAMP_HALO, LAMP_ON);
00550    } else {
00551        cpl_propertylist_append_bool(plist, KEY_NAME_LAMP_HALO,LAMP_ON) ;
00552    }
00553    /* Save the file */
00554    /*
00555       if (cpl_image_save(ima, file_name, CPL_BPP_DEFAULT, 
00556                          plist,CPL_IO_DEFAULT)!=CPL_ERROR_NONE) {
00557        sinfo_msg_error("Cannot save the product %s",file_name);
00558        goto cleanup;
00559       }
00560    */
00561    sinfo_free_propertylist(&plist);
00562    sinfo_free_image(&ima);
00563 
00564    /* 
00565        ---------------------------------------------------------
00566                                NST 
00567        ---------------------------------------------------------
00568    */
00569    
00570    sinfo_msg("RUN NORD SOUTH TEST\n") ; 
00571    ck0(sinfo_new_nst(cpl_func,config, set ),"Running north south test");
00572    sinfo_msg("SUCCESS: RUNNED NORD SUD TEST\n") ; 
00573    sinfo_msg("SUCCESS: RECIPE\n") ; 
00574 
00575    sinfo_free_frameset(&raw_set);
00576    sinfo_fake_delete(&fk);
00577 
00578 
00579    return 0 ;
00580 
00581  cleanup:
00582 
00583 
00584    sinfo_free_frameset(&set_on);
00585    sinfo_free_image(&ima);
00586    sinfo_free_propertylist(&plist) ;
00587    sinfo_free_frameset(&set_off);
00588    sinfo_free_frameset(&raw_set);
00589    sinfo_free_frameset(&raw_set);
00590    sinfo_fake_delete(&fk);
00591    return -1 ;
00592 
00593 }
00594 
00595 
00596 
00597 static int 
00598 new_pre_process(cpl_parameterlist* config, cpl_frameset* set, 
00599 const float lo_rej, const float hi_rej, const char* name_o)
00600 {
00601 
00602   int nf=0;
00603   int ng=10;
00604   int ns=0;
00605   int nr=0;
00606   int nt=0;
00607 
00608   int i=0;
00609   int j=0;
00610   int k=0;
00611   int lo_cut=0;
00612   int hi_cut=0;
00613 
00614 
00615   cpl_frame* frame=NULL;
00616   cpl_frame* pframe=NULL;
00617   cpl_frameset* sof=NULL;
00618   cpl_frameset* fibre_raw_set=NULL;
00619   char* file_name=NULL;
00620 
00621   cpl_propertylist* plist=NULL;
00622   cpl_imagelist* imset_tmp=NULL;
00623   cpl_imagelist* imset_tot=NULL;
00624   cpl_image** avg_img_list=NULL;
00625 
00626   cpl_image* img=NULL;
00627   cpl_image* img_tmp=NULL;
00628   cpl_image* img_dup=NULL;
00629 
00630   cpl_image** img_list=NULL;
00631 
00632 
00633          sof = cpl_frameset_duplicate(set);
00634          fibre_raw_set = cpl_frameset_new();
00635 
00636          sinfo_contains_frames_kind(sof,fibre_raw_set,RAW_FIBRE_NS);
00637 
00638 
00639          nf = cpl_frameset_get_size(fibre_raw_set);
00640          if(nf < 1) {
00641        sinfo_msg_warning("No input frames in data set");
00642            sinfo_free_frameset(&sof);
00643            sinfo_free_frameset(&fibre_raw_set);
00644            return -1;
00645      }
00646          frame = cpl_frameset_get_frame(fibre_raw_set,0);
00647          file_name=(char*) cpl_frame_get_filename(frame);
00648 
00649 
00650      if ((plist = cpl_propertylist_load(file_name, 0)) == NULL) {
00651         sinfo_msg_error("getting header from  ima frame %s",file_name);
00652             return -1 ;
00653      }
00654 
00655 
00656          if( nf > ng) {
00657 
00658        sinfo_msg("Total raw frames nf=%d > max frm per group ng=%d",nf,ng);
00659        ns = (nf+1)/ng;
00660            nr = nf-ns*ng;
00661            imset_tot=cpl_imagelist_new();
00662            avg_img_list=cpl_malloc((ns+1) * sizeof(cpl_image *));
00663 
00664 
00665        for (i=0;i<ns;i++) {
00666              sinfo_msg("iteration i=%d\n",i);
00667              imset_tmp=cpl_imagelist_new();
00668              img_list=cpl_malloc(ng * sizeof(cpl_image *));
00669 
00670 
00671              for (j=0;j<ng;j++) {
00672            k=i*ng+j;
00673                frame = cpl_frameset_get_frame(fibre_raw_set,k);
00674                file_name=(char*)cpl_frame_get_filename(frame);
00675                img_tmp=cpl_image_load(file_name,CPL_TYPE_FLOAT,0,0);
00676                img_dup=cpl_image_duplicate(img_tmp); 
00677            cpl_imagelist_set(imset_tmp,img_dup,j);
00678                cpl_image_delete(img_tmp);
00679          }
00680 
00681          
00682              nt=cpl_imagelist_get_size(imset_tmp);
00683              lo_cut=(floor)(lo_rej*nt+0.5);
00684              hi_cut=(floor)(hi_rej*nt+0.5);
00685              avg_img_list[i]=cpl_imagelist_collapse_minmax_create(imset_tmp,
00686                                                                  lo_cut,hi_cut);
00687              cpl_imagelist_set(imset_tot,avg_img_list[i],i);
00688              cpl_imagelist_delete(imset_tmp);
00689          cpl_free(img_list);
00690 
00691 
00692 
00693  
00694 
00695        }
00696            cpl_free(avg_img_list);
00697 
00698 
00699            if(ns*ng<nf) {
00700              imset_tmp=cpl_imagelist_new();
00701          img_list=cpl_malloc((nf-ns*ng) * sizeof(cpl_image *));
00702          for(i=0;i<nr;i++) {
00703                k=i+ns*ng;
00704                frame = cpl_frameset_get_frame(fibre_raw_set,k);
00705                file_name  = (char*) cpl_frame_get_filename(frame);
00706 
00707                img_list[i]=cpl_image_load(file_name,CPL_TYPE_FLOAT,0,0);
00708            cpl_imagelist_set(imset_tmp,img_list[i],i);
00709          }
00710              nt=cpl_imagelist_get_size(imset_tmp);
00711              lo_cut=(floor)(lo_rej*nt+0.5);
00712              hi_cut=(floor)(hi_rej*nt+0.5);
00713 
00714          avg_img_list[ns]=cpl_imagelist_collapse_minmax_create(imset_tmp,
00715                                                                 lo_cut,hi_cut);
00716          cpl_imagelist_set(imset_tot,avg_img_list[ns],ns);
00717 
00718          cpl_free(img);
00719          cpl_imagelist_delete(imset_tmp);
00720              cpl_free(img_list);
00721        }
00722 
00723      } else {
00724 
00725        sinfo_msg("Total raw frames nf=%d < max frm per group ng=%d",nf,ng);
00726            imset_tot=cpl_imagelist_new();
00727        img_list=cpl_malloc(nf * sizeof(cpl_image *));
00728        for (i=0;i<nf;i++) {
00729          frame = cpl_frameset_get_frame(fibre_raw_set,i);
00730          file_name  = (char*) cpl_frame_get_filename(frame);
00731          img_list[i]=cpl_image_load(file_name,CPL_TYPE_FLOAT,0,0);
00732              cpl_imagelist_set(imset_tot,img_list[i],i);
00733        }
00734            cpl_free(img_list);
00735      }
00736 
00737          cpl_frameset_delete(fibre_raw_set);
00738          nt=cpl_imagelist_get_size(imset_tot);
00739          lo_cut=(floor)(lo_rej*nt+0.5);
00740          hi_cut=(floor)(hi_rej*nt+0.5);
00741 
00742      if( (img = cpl_imagelist_collapse_minmax_create(imset_tot,
00743                                                      lo_cut,hi_cut)) == NULL) {
00744        sinfo_msg_error("Error code");
00745        sinfo_msg_error((char* ) cpl_error_get_message());
00746        cpl_imagelist_delete(imset_tot);
00747        cpl_frameset_delete(sof);
00748        cpl_propertylist_delete(plist) ;
00749        return -1;
00750      }
00751 
00752      if (cpl_image_save(img,name_o, CPL_BPP_DEFAULT,
00753                 plist,CPL_IO_DEFAULT)!=CPL_ERROR_NONE) {
00754        sinfo_msg_error("Cannot save the product %s",name_o);
00755        cpl_imagelist_delete(imset_tot);
00756        cpl_frameset_delete(sof);
00757        cpl_propertylist_delete(plist) ;
00758        return -1 ;
00759      }
00760      cpl_imagelist_delete(imset_tot);
00761      cpl_frameset_erase(set,RAW_FIBRE_NS);
00762          
00763          /* Create product frame */
00764          pframe = cpl_frame_new();
00765      cpl_frame_set_filename(pframe, name_o) ;
00766      cpl_frame_set_tag(pframe, "FIBRE_NS") ;
00767      cpl_frame_set_type(pframe, CPL_FRAME_TYPE_IMAGE) ;
00768      cpl_frame_set_group(pframe, CPL_FRAME_GROUP_RAW) ;
00769      cpl_frame_set_level(pframe, CPL_FRAME_LEVEL_FINAL) ;
00770      if (cpl_error_get_code()) {
00771        sinfo_msg_error("Error while initialising the product frame") ;
00772        cpl_propertylist_delete(plist) ;
00773        cpl_frame_delete(pframe) ;
00774        cpl_image_delete(img) ;
00775        return -1 ;
00776      }
00777     
00778      /* Add DataFlow keywords */
00779      if (cpl_dfs_setup_product_header(plist, pframe, set, config,
00780                       cpl_func, "SINFONI", 
00781                                        KEY_VALUE_HPRO_DID) != CPL_ERROR_NONE) {
00782        sinfo_msg_error("Problem in the product DFS-compliance") ;
00783        cpl_propertylist_delete(plist) ;
00784        cpl_frame_delete(pframe) ;
00785        cpl_image_delete(img) ;
00786        return -1 ;
00787      }
00788    
00789 
00790      /* Save the file */
00791      if (cpl_image_save(img, name_o, CPL_BPP_DEFAULT, plist,
00792                 CPL_IO_DEFAULT) != CPL_ERROR_NONE) {
00793        sinfo_msg_error("Could not save product");
00794        cpl_propertylist_delete(plist) ;
00795        cpl_frame_delete(pframe) ;
00796        cpl_image_delete(img) ;
00797        return -1 ;
00798      }
00799      cpl_propertylist_delete(plist) ;
00800      cpl_image_delete(img) ;
00801 
00802      /* Log the saved file in the input frameset */
00803      cpl_frameset_insert(set, pframe) ;
00804          cpl_frameset_delete(sof);
00805 return 0;
00806 }

Generated on Wed Jan 17 08:33:43 2007 for SINFONI Pipeline Reference Manual by  doxygen 1.4.4