si_rec_distortion.c

00001 /* $Id: si_rec_distortion.c,v 1.29 2005/10/18 12:03:00 amodigli Exp $
00002  *
00003  * This file is part of the CPL (Common Pipeline Library)
00004  * Copyright (C) 2002 European Southern Observatory
00005  *
00006  * This library is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU Lesser General Public
00008  * License as published by the Free Software Foundation; either
00009  * version 2.1 of the License, or (at your option) any later version.
00010  *
00011  * This library 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 GNU
00014  * Lesser General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU Lesser General Public
00017  * License along with this library; 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: 2005/10/18 12:03:00 $
00024  * $Revision: 1.29 $
00025  * $Name:  $
00026  */
00027 
00028  /****************************************************************
00029   *          Distortions Frames Data Reduction                          *
00030   ****************************************************************/
00031 #include <strings.h>
00032 #include <string.h>
00033 
00034 #include <stdio.h>
00035 #include <math.h>
00036 
00037 #ifdef HAVE_CONFIG_H
00038 #include <config.h>          /* allows the program compilation */
00039 #endif
00040 
00041 #include <xmemory.h>
00042 
00043 #include <cxmacros.h>
00044 #include <cxtypes.h>
00045 #include <cxmessages.h>   
00046 
00047 #include <qfits.h>       
00048 #include <cpl.h>       
00049 #include <cpl_imagelist_basic.h>
00050 #include <sinfoni_pro_types.h>
00051 #include <sinfoni_raw_types.h>
00052 #include <sinfoni_key_names.h>
00053 
00054 #include <sinfoni_globals.h>
00055 
00056 #include <sinfoni_general_config.h>
00057 #include <sinfoni_lamp_flats_config.h>
00058 #include <sinfoni_bp_config.h>
00059 #include <sinfoni_bp_dist_config.h>
00060 #include <sinfoni_prepare_stacked_frames_config.h>
00061 
00062 #include <sinfoni_north_south_test_config.h>
00063 #include <sinfoni_distortion_config.h>
00064 #include <lamp_flats.h>
00065 #include <bp_norm.h>
00066 #include <prepare_stacked_frames.h>
00067 #include <find_distortions.h>
00068 #include <nst.h>
00069 
00070 #include <sinfoni_functions.h>
00071 #include "sinfoni_tpl_utils.h"
00072 #include "sinfoni_tpl_dfs.h"
00073 #include <sinfoni_memory.h>
00074 
00075 /*
00076 #include <sinfoni_dfs.h>
00077 */
00078 
00079 #define CPL_BPP_DEFAULT  CPL_BPP_IEEE_FLOAT
00080 
00081 
00082 static cxint si_rec_distortion_fast(cpl_parameterlist *, cpl_frameset *);
00083 const char * sinfoni_get_licence(void);
00084 cxint si_rec_distortion_fast_create(cpl_plugin *plugin);
00085 cxint si_rec_distortion_fast_exec(cpl_plugin *plugin);
00086 cxint si_rec_distortion_fast_destroy(cpl_plugin *plugin);
00087 static cxint si_rec_distortion_fast(cpl_parameterlist *config, cpl_frameset *set);
00088 int cpl_plugin_get_info(cpl_pluginlist *list);
00089 void sinfoni_distortion_fast_config_add(cpl_parameterlist *); 
00090 int 
00091 pre_process(cpl_parameterlist* config, cpl_frameset* set, 
00092         const float lo_rej, const float hi_rej, char* name_o);
00093 
00094 static char si_rec_distortion_description1[] =
00095 "This recipe determines the optical distortions and the slitlets distances.\n"
00096 "Necessary input are:\n"
00097 "Several raw frames classified as FIBRE_NS\n"
00098 "Standard flat frames having classified as FLAT_NS\n"
00099 "Standard arc lamp frames having classified as WAVE_NS\n"
00100 "A corresponding (band) reference arc line list classified as REF_LINE_ARC\n"
00101 "A reference table with data reduction parameters classified as DRS_WAVE_NS.\n";
00102 
00103 
00104 static char si_rec_distortion_description2[] =
00105 "Output are (with their PRO.CATG)\n"
00106 "A master flat: MASTER_FLAT_LAMP\n"
00107 "A Bad pixel map: BP_MAP_DI\n"
00108 "A fake-off fibre stacked frame: FIBRE_NS_STACKED_OFF\n"
00109 "A fake-on fibre  stacked frame: FIBRE_NS_STACKED_ON\n"
00110 "A fake on-off fibre  stacked frame: FIBRE_NS_STACKED\n"
00111 "A table with optical distortion coefficients: DISTORTION\n";
00112 
00113 
00114 static char si_rec_distortion_description3[] =
00115 "A distortion corrected frame: FIBRE_NS_STACKED_DIST\n"
00116 "A temporary frame: MASTER_SLIT\n"
00117 "A table with the slitlets distances: SLITLETS_DISTANCE\n"
00118 "Information on relevant parameters can be found with:\n"
00119 "esorex --params si_rec_distortion\n"
00120 "esorex --help si_rec_distortion\n"
00121 "\n";
00122 
00123 static char si_rec_distortion_description[1300];
00124 
00125 
00126 const char * sinfoni_get_licence(void)
00127 {
00128   return "bla bla";
00129 
00130 } 
00131 
00132 
00133  /*
00134   *
00135   * Create the recipe instance, i.e. setup the parameter list for this
00136   * recipe and make it available to the application using the interface.
00137   *
00138   */
00139 
00140 
00141 cxint
00142 si_rec_distortion_fast_create(cpl_plugin *plugin)
00143 {
00144 
00145   /*
00146    * We have to provide the option we accept to the application.
00147    * We need to setup our parameter list and hook it into the recipe
00148    * interface.
00149    */
00150   cpl_recipe *recipe = (cpl_recipe *)plugin;
00151   recipe->parameters = cpl_parameterlist_new();
00152   if(recipe->parameters == NULL) {
00153     return 1;
00154   }
00155 
00156   /*
00157    * Fill the parameter list.
00158    */
00159 
00160 
00161   /* Output file name */
00162   sinfoni_general_config_add(recipe->parameters);
00163   sinfoni_lamp_flats_config_add(recipe->parameters);
00164   sinfoni_bp_config_add(recipe->parameters);  
00165   sinfoni_bp_dist_config_add(recipe->parameters); 
00166   sinfoni_prepare_stacked_frames_config_add(recipe->parameters);
00167   sinfoni_distortion_config_add(recipe->parameters);
00168   sinfoni_north_south_test_config_add(recipe->parameters);
00169   
00170 
00171   return 0;
00172 
00173 }
00174 
00175 cxint
00176 si_rec_distortion_fast_exec(cpl_plugin *plugin)
00177 {
00178 
00179   cpl_recipe *recipe = (cpl_recipe *) plugin;
00180   if(recipe->parameters == NULL) {
00181     return 1;
00182   }
00183   if(recipe->frames == NULL) {
00184     return 1;
00185   }
00186 
00187   cpl_error_reset();
00188   return si_rec_distortion_fast(recipe->parameters, recipe->frames);
00189 
00190 }
00191 
00192 cxint
00193 si_rec_distortion_fast_destroy(cpl_plugin *plugin)
00194 {
00195 
00196   cpl_recipe *recipe = (cpl_recipe *) plugin;
00197   /*
00198    * We just destroy what was created during the plugin initializzation phase
00199    * i.e. the parameter list. The frame set is managed by the application which
00200    * called us, so that we must not touch it.
00201    */
00202 
00203   cpl_parameterlist_delete(recipe->parameters);
00204 
00205   return 0;
00206 
00207 }
00208 
00209 int
00210 cpl_plugin_get_info(cpl_pluginlist *list)
00211 {
00212 
00213   cpl_recipe *recipe = cpl_calloc(1, sizeof *recipe);
00214   cpl_plugin *plugin = &recipe->interface;
00215   strcpy(si_rec_distortion_description,si_rec_distortion_description1);
00216   strcat(si_rec_distortion_description,si_rec_distortion_description2);
00217   strcat(si_rec_distortion_description,si_rec_distortion_description3);
00218  
00219 
00220   cpl_plugin_init(plugin,
00221           CPL_PLUGIN_API,
00222           SINFONI_BINARY_VERSION,
00223           CPL_PLUGIN_TYPE_RECIPE,
00224           "si_rec_distortion",
00225           "Finds optical distortions and slitlets distances",
00226           si_rec_distortion_description,
00227           "Andrea Modigliani",
00228           "Andrea.Modigliani@eso.org",
00229           sinfoni_get_license(),
00230           si_rec_distortion_fast_create,
00231           si_rec_distortion_fast_exec,
00232           si_rec_distortion_fast_destroy);
00233 
00234   cpl_pluginlist_append(list, plugin);
00235 
00236   return 0;
00237 
00238 }
00239 
00240 
00241 /*
00242  * The actual recipe actually start here.
00243  */
00244 
00245 static cxint
00246 si_rec_distortion_fast(cpl_parameterlist *config, cpl_frameset *set)
00247 {
00248  
00249   const char *_id = "si_rec_distortion";
00250   int  ind =0;
00251   cpl_parameter *p=NULL;
00252 
00253 
00254   cpl_frame* frame=NULL;
00255   cpl_frame* frm_dup=NULL;
00256 
00257   char file_name[FILE_NAME_SZ];
00258 
00259   cpl_propertylist* plist=NULL;
00260 
00261   cpl_image* ima=NULL;
00262 
00263   cpl_frameset* raw_set=NULL;
00264   cpl_frameset* set_off=NULL;
00265   cpl_frameset* set_on=NULL;
00266   fake* fk=fake_new();
00267   
00268   if(sinfoni_dfs_set_groups(set)) {
00269     cpl_msg_error(_id, "Cannot indentify RAW and CALIB frames") ;
00270     return -1;
00271   }
00272   
00273   p = cpl_parameterlist_find(config,"sinfoni.bp.method");
00274   cpl_parameter_set_string(p,"Normal");   
00275 
00276      raw_set=cpl_frameset_duplicate(set);
00277 
00278     /* 
00279        ---------------------------------------------------------
00280                 LAMPFLAT
00281        ---------------------------------------------------------
00282      */
00283     
00284    cpl_msg_info(_id,"-----------------------------");
00285    cpl_msg_info(_id,"  DETERMINE MASTER_LAMP_NS ");
00286    cpl_msg_info(_id,"-----------------------------");
00287 
00288     
00289     {
00290     cpl_msg_info (_id,"REDUCE LAMPFLAT\n") ; 
00291        
00292         if ( -1 == (ind = lamp_flats(_id,config, set ) ) )
00293         {
00294         cpl_msg_error(_id,"no: %d\n", ind) ;
00295             cpl_frameset_delete(raw_set);
00296             fake_delete(fk);
00297         return -1 ;
00298     }
00299        
00300     cpl_msg_info (_id,"SUCCES: DETERMINATION MASTER_LAMP_NS") ; 
00301       
00302     }  
00303   
00304     /* 
00305        ---------------------------------------------------------
00306                 BP_SEARCH
00307        ---------------------------------------------------------
00308      */
00309 
00310   
00311    cpl_msg_info(_id,"-----------------------------");
00312    cpl_msg_info(_id,"  DETERMINE BP_MAP_DI        ");
00313    cpl_msg_info(_id,"-----------------------------");
00314    
00315     {
00316 
00317             p = cpl_parameterlist_find(config,"sinfoni.bp.method");
00318             cpl_parameter_set_string(p,"Normal");   
00319        
00320             if ( -1 == (ind = badSearchNormal(_id,config,set,PRO_BP_MAP_DI)))
00321         {
00322             cpl_msg_error(_id,"badSearchNormal(), no: %d\n", ind) ;
00323                 cpl_frameset_delete(raw_set);
00324         fake_delete(fk);
00325         return -1 ;
00326         }
00327        
00328             cpl_msg_info(_id,"SUCCESS DETERMINATION %s",PRO_BP_MAP_DI);
00329     }
00330 
00331   
00332     sinfoni_memory_status();
00333   
00334    {
00335 
00336  
00337     /* 
00338        ---------------------------------------------------------
00339         pre iteration: get a collassed frame 
00340        ---------------------------------------------------------
00341      */
00342 
00343 
00344   
00345      set_off=cpl_frameset_duplicate(raw_set);
00346      pre_process(config,set_off, 0.0, 0.2,"fibre_off.fits");
00347   
00348      /* add the off artificial frame to the frameset */
00349      if(NULL != cpl_frameset_find(set_off,RAW_FIBRE_NS)) {
00350        frame = cpl_frameset_find(set_off,RAW_FIBRE_NS);
00351        frm_dup = cpl_frame_duplicate(frame);
00352      } else {
00353        cpl_msg_error(_id,"Frame %s not found!", RAW_FIBRE_NS);
00354        cpl_frameset_delete(set_off);
00355        cpl_frameset_delete(raw_set);
00356        fake_delete(fk);
00357        return -1;
00358      }
00359      cpl_frameset_erase(set,RAW_FIBRE_NS);
00360      cpl_frameset_insert(set,frm_dup);
00361  
00362     /* 
00363        ---------------------------------------------------------
00364         1st iteration: get off frame 
00365        ---------------------------------------------------------
00366      */
00367 
00368         cpl_msg_info (_id,"STACK FIBRE,NS TO GET FAKE OFF\n") ; 
00369          strcpy(fk->pro_class,RAW_FIBRE_NS);
00370          fk->frm_switch=1;
00371          fk->mask_index=0;
00372          fk->ind_index=0;
00373          fk->flat_index=0;
00374          fk->wfix_index=0;
00375          fk->low_rej=0.0;
00376          fk->hig_rej=0.2;
00377  
00378          check_rec_status(_id,0);
00379          if ( -1 == (ind = prepare_stacked_frames(_id,config, set,PRO_FIBRE_NS_STACKED_OFF,0,fk)))
00380         {
00381         cpl_msg_error(_id," no: %d\n", ind) ;
00382             cpl_frameset_delete(set_off);
00383             cpl_frameset_delete(raw_set);
00384             fake_delete(fk);
00385         return -1 ;
00386     }
00387 
00388 
00389         if(NULL != cpl_frameset_find(set,PRO_FIBRE_NS_STACKED_OFF)) {
00390             frame = cpl_frameset_find(set,PRO_FIBRE_NS_STACKED_OFF);
00391            strcpy(file_name,cpl_frame_get_filename(frame));
00392         } else {
00393             cpl_msg_error(_id,"Frame %s not found!", PRO_FIBRE_NS_STACKED_OFF);
00394             cpl_frameset_delete(set_off);
00395             cpl_frameset_delete(raw_set);
00396             fake_delete(fk);
00397             return -1;
00398     }
00399         ima=cpl_image_load(file_name,CPL_TYPE_FLOAT,0,0);
00400 
00401 
00402     cpl_frameset_delete(set_off);
00403         
00404 
00405         if ((cpl_error_code)((plist = cpl_propertylist_load(file_name, 0)) == NULL)) {
00406            cpl_msg_error(_id, "getting header from reference ima frame %s",file_name);
00407            cpl_propertylist_delete(plist) ;
00408            cpl_frameset_delete(raw_set);
00409            fake_delete(fk);
00410            return -1 ;
00411     }
00412 
00413         if (cpl_propertylist_contains(plist, KEY_NAME_LAMP_HALO)) {
00414             cpl_propertylist_set_bool(plist, KEY_NAME_LAMP_HALO, LAMP_OFF);
00415         } else {
00416             cpl_propertylist_append_bool(plist, KEY_NAME_LAMP_HALO,LAMP_OFF) ;
00417         }
00418 
00419 
00420         cpl_propertylist_delete(plist);
00421         cpl_msg_info (_id,"SUCCESS: STACKED FIBRE,NS TO GET FAKE OFF\n") ;  
00422 
00423     }    
00424     cpl_image_delete(ima);
00425      sinfoni_memory_status();
00426 
00427     /* 
00428        ---------------------------------------------------------
00429         2nd iteration: get on frame 
00430        ---------------------------------------------------------
00431      */
00432 
00433 
00434      set_on=cpl_frameset_duplicate(raw_set);
00435      pre_process(config,set_on, 0.0, 0.0,"fibre_on.fits");
00436 
00437 
00438       if(NULL != cpl_frameset_find(set_on,RAW_FIBRE_NS)) {
00439        frame = cpl_frameset_find(set_on,RAW_FIBRE_NS);
00440        frm_dup=cpl_frame_duplicate(frame);
00441      } else {
00442        cpl_msg_error(_id,"Frame %s not found!", RAW_FIBRE_NS);
00443 
00444 
00445        sinfoni_memory_status();
00446        return -1;
00447      }
00448      cpl_frameset_erase(set,RAW_FIBRE_NS);
00449      cpl_frameset_insert(set,frm_dup);
00450 
00451 
00452    {
00453         cpl_msg_info (_id,"STACK FIBRE,NS TO GET FAKE ON\n") ; 
00454 
00455         strcpy(fk->pro_class,RAW_FIBRE_NS);
00456         fk->frm_switch=1;
00457         fk->mask_index=0;
00458         fk->ind_index=0;
00459         fk->flat_index=0;
00460         fk->wfix_index=0;
00461         fk->low_rej=0.0;
00462         fk->hig_rej=0.0;
00463 
00464 
00465         if ( -1 == (ind = prepare_stacked_frames(_id,config, set, PRO_FIBRE_NS_STACKED_ON,0,fk)))
00466         {
00467         cpl_msg_error(_id," no: %d\n", ind) ;
00468             cpl_frameset_delete(set_on);
00469             cpl_frameset_delete(raw_set);
00470 
00471             fake_delete(fk);
00472             sinfoni_memory_status();
00473         return -1 ;
00474     }
00475         cpl_msg_info (_id,"SUCCESS DETERMINATION %s",PRO_FIBRE_NS_STACKED_ON) ; 
00476 
00477     }    
00478    /* cpl_frameset_erase(set,RAW_FIBRE_NS); */
00479     cpl_frameset_delete(set_on);
00480 
00481     /* here we have a problem with pipefile of following step product */
00482     /* 
00483        ---------------------------------------------------------
00484                 3rd iteration combines on and off fake frames
00485        ---------------------------------------------------------
00486      */
00487 
00488 
00489    {
00490     cpl_msg_info (_id,"COMBINES FAKE ON AND OFF\n") ; 
00491 
00492    
00493         strcpy(fk->pro_class,PRO_FIBRE_NS_STACKED);
00494         fk->frm_switch=1;
00495         fk->mask_index=0;
00496         fk->ind_index=0;
00497         fk->flat_index=1;
00498         fk->wfix_index=0;
00499         fk->low_rej=0.0;
00500         fk->hig_rej=0.0;
00501     
00502 
00503          if ( -1 == (ind = prepare_stacked_frames(_id,config, set, PRO_FIBRE_NS_STACKED,0,fk)))
00504         {
00505         cpl_msg_error(_id," no: %d\n", ind) ;
00506             cpl_frameset_delete(raw_set);
00507             fake_delete(fk);
00508             sinfoni_memory_status();
00509         return -1 ;
00510     }
00511         cpl_msg_info (_id,"SUCCESS DETERMINATION %s",PRO_FIBRE_NS_STACKED) ; 
00512     }    
00513    /* cpl_frameset_erase(set,RAW_FIBRE_NS); */
00514 
00515     /* 
00516        ---------------------------------------------------------
00517                 STACK WAVECAL
00518        ---------------------------------------------------------
00519      */
00520 
00521    {
00522     cpl_msg_info (_id,"STACK on WAVE frame\n") ;
00523 
00524 
00525         strcpy(fk->pro_class,RAW_WAVE_NS);
00526         fk->frm_switch=1;
00527         fk->mask_index=0;
00528         fk->ind_index=0;
00529         fk->flat_index=1;
00530         fk->wfix_index=0;
00531         fk->low_rej=0.1;
00532         fk->hig_rej=0.1;
00533    
00534     cpl_msg_info (_id,"STACK on WAVE frame\n") ; 
00535         if ( -1 == (ind = prepare_stacked_frames(_id,config, set, PRO_WAVE_LAMP_STACKED,0,fk)))
00536         {
00537         cpl_msg_error(_id," no: %d\n", ind) ;
00538             cpl_frameset_delete(raw_set);
00539             fake_delete(fk);
00540         return -1 ;
00541     }
00542         cpl_msg_info (_id,"SUCCESS DETERMINATION %s",PRO_WAVE_LAMP_STACKED) ; 
00543 
00544     }    
00545 
00546     /* 
00547        ---------------------------------------------------------
00548                 DISTORTIONS 
00549        ---------------------------------------------------------
00550      */
00551 
00552 {
00553     cpl_msg_info(_id,"COMPUTE DISTORTIONS\n") ; 
00554          if ( -1 == (ind = find_distortions(_id,config, set) ) )
00555         {
00556         cpl_msg_error(_id,"step_distortion(), no: %d\n", ind) ;
00557             cpl_frameset_delete(raw_set);
00558             fake_delete(fk);
00559         return -1 ;
00560     }
00561 
00562 
00563         if(NULL != cpl_frameset_find(set,PRO_FIBRE_NS_STACKED)) {
00564             frame = cpl_frameset_find(set,PRO_FIBRE_NS_STACKED);
00565             strcpy(file_name,cpl_frame_get_filename(frame));
00566         } else {
00567             cpl_msg_error(_id,"Frame %s not found!", PRO_FIBRE_NS_STACKED);
00568             cpl_frameset_delete(raw_set);
00569             fake_delete(fk);
00570             return -1;
00571     }
00572         ima=cpl_image_load(file_name,CPL_TYPE_FLOAT,0,0);
00573 
00574 
00575 
00576 
00577         if ((cpl_error_code)((plist = cpl_propertylist_load(file_name, 0)) == NULL)) {
00578            cpl_msg_error(_id, "getting header from reference ima frame %s",file_name);
00579            cpl_propertylist_delete(plist) ;
00580             cpl_frameset_delete(raw_set);
00581             cpl_image_delete(ima);
00582            fake_delete(fk);
00583            return -1 ;
00584     }
00585 
00586         if (cpl_propertylist_contains(plist, KEY_NAME_LAMP_HALO)) {
00587             cpl_propertylist_set_bool(plist, KEY_NAME_LAMP_HALO, LAMP_ON);
00588         } else {
00589             cpl_propertylist_append_bool(plist, KEY_NAME_LAMP_HALO,LAMP_ON) ;
00590         }
00591      
00592 
00593         /* Save the file */
00594     /*
00595         if (cpl_image_save(ima, file_name, CPL_BPP_DEFAULT, plist,CPL_IO_DEFAULT)!=CPL_ERROR_NONE) {
00596            cpl_msg_error(_id, "Cannot save the product %s",file_name);
00597            cpl_propertylist_delete(plist) ;
00598           return -1 ;
00599     }
00600     */
00601      
00602         cpl_image_delete(ima);
00603         cpl_propertylist_delete(plist);
00604     cpl_msg_info(_id,"SUCCESS: COMPUTED DISTORTIONS\n") ; 
00605 
00606     }
00607 
00608     /* 
00609        ---------------------------------------------------------
00610        4th iteration: distort fake frame
00611        ---------------------------------------------------------
00612      */
00613 
00614 
00615      {
00616     cpl_msg_info (_id,"DISTORT FAKE FRAME\n") ; 
00617 
00618         strcpy(fk->pro_class,PRO_FIBRE_NS_STACKED_DIST);
00619         fk->frm_switch=1;
00620         fk->mask_index=1;
00621         fk->ind_index=1;
00622         fk->flat_index=0;
00623         fk->wfix_index=1;
00624         fk->low_rej=0.0;
00625         fk->hig_rej=0.0;
00626 
00627 
00628 
00629 
00630         if ( -1 == (ind = prepare_stacked_frames(_id,config, set, PRO_FIBRE_NS_STACKED_DIST,0,fk)))
00631         {
00632         cpl_msg_error(_id," no: %d\n", ind) ;
00633             cpl_frameset_delete(raw_set);
00634             fake_delete(fk);
00635         return -1 ;
00636     }
00637     cpl_msg_info (_id,"SUCCESS: DISTORTED FAKE FRAME\n") ; 
00638 
00639 
00640 
00641         if(NULL != cpl_frameset_find(set,PRO_FIBRE_NS_STACKED_DIST)) {
00642             frame = cpl_frameset_find(set,PRO_FIBRE_NS_STACKED_DIST);
00643             strcpy(file_name,cpl_frame_get_filename(frame));
00644         } else {
00645             cpl_msg_error(_id,"Frame %s not found!", PRO_FIBRE_NS_STACKED_DIST);
00646             cpl_frameset_delete(raw_set);
00647             fake_delete(fk);
00648             return -1;
00649     }
00650         ima=cpl_image_load(file_name,CPL_TYPE_FLOAT,0,0);
00651 
00652 
00653     
00654         if ((cpl_error_code)((plist = cpl_propertylist_load(file_name, 0)) == NULL)) {
00655            cpl_msg_error(_id, "getting header from reference ima frame %s",file_name);
00656            cpl_propertylist_delete(plist) ;
00657            cpl_image_delete(ima);
00658             cpl_frameset_delete(raw_set);
00659             fake_delete(fk);
00660            return -1 ;
00661     }
00662 
00663         if (cpl_propertylist_contains(plist, KEY_NAME_LAMP_HALO)) {
00664             cpl_propertylist_set_bool(plist, KEY_NAME_LAMP_HALO, LAMP_ON);
00665         } else {
00666             cpl_propertylist_append_bool(plist, KEY_NAME_LAMP_HALO,LAMP_ON) ;
00667         }
00668   
00669 
00670         /* Save the file */
00671     /*
00672         if (cpl_image_save(ima, file_name, CPL_BPP_DEFAULT, plist,CPL_IO_DEFAULT)!=CPL_ERROR_NONE) {
00673            cpl_msg_error(_id, "Cannot save the product %s",file_name);
00674            cpl_propertylist_delete(plist) ;
00675           return -1 ;
00676     }
00677     */
00678   
00679         cpl_propertylist_delete(plist);
00680         cpl_image_delete(ima);
00681 
00682      }
00683 
00684 
00685     
00686 
00687     /* 
00688        ---------------------------------------------------------
00689                                NST 
00690        ---------------------------------------------------------
00691      */
00692     {
00693     cpl_msg_info (_id,"RUN NORD SUD TEST\n") ; 
00694         if ( -1 == (ind = nst(_id,config, set ) ) )
00695         {
00696         cpl_msg_error(_id,"no: %d\n", ind) ;
00697             cpl_frameset_delete(raw_set);
00698             fake_delete(fk);
00699         return -1 ;
00700     }
00701      
00702     cpl_msg_info (_id,"SUCCESS: RUNNED NORD SUD TEST\n") ; 
00703 
00704     }    
00705     cpl_msg_info (_id,"SUCCESS: RECIPE\n") ; 
00706  
00707 
00708          cpl_frameset_delete(raw_set);
00709      fake_delete(fk);
00710 
00711     return 0 ;
00712 
00713 }
00714 
00715 
00716 
00717 int 
00718 pre_process(cpl_parameterlist* config, cpl_frameset* set, 
00719 const float lo_rej, const float hi_rej, char* name_o)
00720 {
00721 
00722   const char* _id="pre_process";
00723   int nf=0;
00724   int ng=10;
00725   int ns=0;
00726   int nr=0;
00727   int nt=0;
00728 
00729   int i=0;
00730   int j=0;
00731   int k=0;
00732   int lo_cut=0;
00733   int hi_cut=0;
00734 
00735 
00736   cpl_frame* frame=NULL;
00737   cpl_frame* pframe=NULL;
00738   cpl_frameset* sof=NULL;
00739   cpl_frameset* fibre_raw_set=NULL;
00740   char* file_name=NULL;
00741 
00742   cpl_propertylist* plist=NULL;
00743   cpl_imagelist* imset_tmp=NULL;
00744   cpl_imagelist* imset_tot=NULL;
00745   cpl_image** avg_img_list=NULL;
00746 
00747   cpl_image* img=NULL;
00748   cpl_image* img_tmp=NULL;
00749   cpl_image* img_dup=NULL;
00750 
00751   cpl_image** img_list=NULL;
00752 
00753          sof = cpl_frameset_duplicate(set);
00754          fibre_raw_set = cpl_frameset_new();
00755 
00756          sinfoni_contains_frames_kind(sof,fibre_raw_set,RAW_FIBRE_NS);
00757 
00758 
00759          nf = cpl_frameset_get_size(fibre_raw_set);
00760          frame = cpl_frameset_get_frame(fibre_raw_set,0);
00761          file_name=(char*) cpl_frame_get_filename(frame);
00762 
00763 
00764      if ((plist = cpl_propertylist_load(file_name, 0)) == NULL) {
00765        cpl_msg_error(_id, "getting header from  ima frame %s",file_name);
00766             return -1 ;
00767      }
00768 
00769 
00770          if( nf > ng) {
00771 
00772        cpl_msg_info(_id,"Total raw frames nf=%d > max frm per group ng=%d\n",nf,ng);
00773        ns = (nf+1)/ng;
00774            nr = nf-ns*ng;
00775            imset_tot=cpl_imagelist_new();
00776            avg_img_list=cpl_malloc((ns+1) * sizeof(cpl_image *));
00777 
00778 
00779        for (i=0;i<ns;i++) {
00780              printf("iteration i=%d\n",i);
00781              imset_tmp=cpl_imagelist_new();
00782              img_list=cpl_malloc(ng * sizeof(cpl_image *));
00783 
00784 
00785              for (j=0;j<ng;j++) {
00786            k=i*ng+j;
00787                frame = cpl_frameset_get_frame(fibre_raw_set,k);
00788                file_name=(char*)cpl_frame_get_filename(frame);
00789                img_tmp=cpl_image_load(file_name,CPL_TYPE_FLOAT,0,0);
00790                img_dup=cpl_image_duplicate(img_tmp); 
00791            cpl_imagelist_set(imset_tmp,img_dup,j);
00792                cpl_image_delete(img_tmp);
00793          }
00794 
00795          
00796              nt=cpl_imagelist_get_size(imset_tmp);
00797              lo_cut=(floor)(lo_rej*nt+0.5);
00798              hi_cut=(floor)(hi_rej*nt+0.5);
00799              avg_img_list[i]=cpl_imagelist_collapse_minmax_create(imset_tmp,lo_cut,hi_cut);
00800              cpl_imagelist_set(imset_tot,avg_img_list[i],i);
00801              cpl_imagelist_delete(imset_tmp);
00802          cpl_free(img_list);
00803 
00804 
00805 
00806  
00807 
00808        }
00809            cpl_free(avg_img_list);
00810 
00811 
00812        /* printf("ndata=%d\n",cpl_imagelist_get_size(imset_tot)); */
00813            if(ns*ng<nf) {
00814              imset_tmp=cpl_imagelist_new();
00815          img_list=cpl_malloc((nf-ns*ng) * sizeof(cpl_image *));
00816          for(i=0;i<nr;i++) {
00817                k=i+ns*ng;
00818                frame = cpl_frameset_get_frame(fibre_raw_set,k);
00819                file_name  = (char*) cpl_frame_get_filename(frame);
00820 
00821                img_list[i]=cpl_image_load(file_name,CPL_TYPE_FLOAT,0,0);
00822            cpl_imagelist_set(imset_tmp,img_list[i],i);
00823          }
00824              nt=cpl_imagelist_get_size(imset_tmp);
00825              lo_cut=(floor)(lo_rej*nt+0.5);
00826              hi_cut=(floor)(hi_rej*nt+0.5);
00827 
00828          avg_img_list[ns] = cpl_imagelist_collapse_minmax_create(imset_tmp,lo_cut,hi_cut);
00829          cpl_imagelist_set(imset_tot,avg_img_list[ns],ns);
00830 
00831          /* printf("ndata=%d\n",cpl_imagelist_get_size(imset_tot)); */
00832          cpl_free(img);
00833          cpl_imagelist_delete(imset_tmp);
00834              cpl_free(img_list);
00835        }
00836 
00837      } else {
00838 
00839        cpl_msg_info(_id,"Total raw frames nf=%d < max frm per group ng=%d\n",nf,ng);
00840            imset_tot=cpl_imagelist_new();
00841        img_list=cpl_malloc(nf * sizeof(cpl_image *));
00842        for (i=0;i<nf;i++) {
00843          frame = cpl_frameset_get_frame(fibre_raw_set,i);
00844          file_name  = (char*) cpl_frame_get_filename(frame);
00845          img_list[i]=cpl_image_load(file_name,CPL_TYPE_FLOAT,0,0);
00846              cpl_imagelist_set(imset_tot,img_list[i],i);
00847        }
00848            cpl_free(img_list);
00849      }
00850 
00851      /* printf("ndata=%d\n",cpl_imagelist_get_size(imset_tot)); */
00852          cpl_frameset_delete(fibre_raw_set);
00853          nt=cpl_imagelist_get_size(imset_tot);
00854          lo_cut=(floor)(lo_rej*nt+0.5);
00855          hi_cut=(floor)(hi_rej*nt+0.5);
00856 
00857      if( (img = cpl_imagelist_collapse_minmax_create(imset_tot,lo_cut,hi_cut)) == NULL) {
00858        cpl_msg_error(_id, "Error code");
00859        cpl_msg_error(_id,(char* ) cpl_error_get_message());
00860        cpl_imagelist_delete(imset_tot);
00861        cpl_frameset_delete(sof);
00862        cpl_propertylist_delete(plist) ;
00863        return -1;
00864      }
00865 
00866      if (cpl_image_save(img,name_o, CPL_BPP_DEFAULT,
00867                 plist,CPL_IO_DEFAULT)!=CPL_ERROR_NONE) {
00868        cpl_msg_error(_id, "Cannot save the product %s",name_o);
00869        cpl_imagelist_delete(imset_tot);
00870        cpl_frameset_delete(sof);
00871        cpl_propertylist_delete(plist) ;
00872        return -1 ;
00873      }
00874      cpl_imagelist_delete(imset_tot);
00875      cpl_frameset_erase(set,RAW_FIBRE_NS);
00876          
00877          /* Create product frame */
00878          pframe = cpl_frame_new();
00879      cpl_frame_set_filename(pframe, name_o) ;
00880      cpl_frame_set_tag(pframe, "FIBRE_NS") ;
00881      cpl_frame_set_type(pframe, CPL_FRAME_TYPE_IMAGE) ;
00882      cpl_frame_set_group(pframe, CPL_FRAME_GROUP_RAW) ;
00883      cpl_frame_set_level(pframe, CPL_FRAME_LEVEL_FINAL) ;
00884      if (cpl_error_get_code()) {
00885        cpl_msg_error(_id, "Error while initialising the product frame") ;
00886        cpl_propertylist_delete(plist) ;
00887        cpl_frame_delete(pframe) ;
00888        cpl_image_delete(img) ;
00889        return -1 ;
00890      }
00891     
00892      /* Add DataFlow keywords */
00893      if (cpl_dfs_setup_product_header(plist, pframe, set, config,
00894                       _id, "SINFONI", KEY_VALUE_HPRO_DID) != CPL_ERROR_NONE) {
00895        cpl_msg_error(_id, "Problem in the product DFS-compliance") ;
00896        cpl_propertylist_delete(plist) ;
00897        cpl_frame_delete(pframe) ;
00898        cpl_image_delete(img) ;
00899        return -1 ;
00900      }
00901    
00902 
00903      /* Save the file */
00904      if (cpl_image_save(img, name_o, CPL_BPP_DEFAULT, plist,
00905                 CPL_IO_DEFAULT) != CPL_ERROR_NONE) {
00906        cpl_msg_error(_id, "Could not save product");
00907        cpl_propertylist_delete(plist) ;
00908        cpl_frame_delete(pframe) ;
00909        cpl_image_delete(img) ;
00910        return -1 ;
00911      }
00912      cpl_propertylist_delete(plist) ;
00913      cpl_image_delete(img) ;
00914 
00915      /* Log the saved file in the input frameset */
00916      cpl_frameset_insert(set, pframe) ;
00917          cpl_frameset_delete(sof);
00918 return 0;
00919 }

Generated on Wed Oct 26 13:08:54 2005 for SINFONI Pipeline Reference Manual by doxygen1.2.13.1 written by Dimitri van Heesch, © 1997-2001