si_rec_distortion_new.c

00001 /* $Id: si_rec_distortion_new.c,v 1.4 2005/10/08 10:34:50 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/08 10:34:50 $
00024  * $Revision: 1.4 $
00025  * $Name:  $
00026  */
00027 
00028  /****************************************************************
00029   *          Distortions Frames Data Reduction                          *
00030   ****************************************************************/
00031 
00032 #ifdef HAVE_CONFIG_H
00033 #include <config.h>          /* allows the program compilation */
00034 #endif
00035 
00036 /*-----------------------------------------------------------------------------
00037                                 Includes
00038  -----------------------------------------------------------------------------*/
00039 
00040 /* std */
00041 #include <strings.h>
00042 #include <string.h>
00043 #include <stdio.h>
00044 
00045 
00046 /* cx */
00047 #include <cxmacros.h>
00048 #include <cxtypes.h>
00049 #include <cxmessages.h>     /* defines message level types */
00050 
00051 /* qfits */
00052 #include <xmemory.h>
00053 
00054 
00055 /* cpl */
00056 
00057 
00058 #include <cpl.h>     /* defines memory allocation functions */
00059 
00060 /* sinfoni */
00061 
00062 #include <sinfoni_prepare_stacked_frames_config.h>
00063 #include <sinfoni_north_south_test_config.h>
00064 #include <sinfoni_lamp_flats_config.h>
00065 #include <sinfoni_bp_config.h>
00066 #include <sinfoni_bp_dist_config.h>
00067 #include <sinfoni_distortion_config.h>
00068 
00069 #include <lamp_flats.h>
00070 #include <bp_norm.h>
00071 #include <prepare_stacked_frames.h>
00072 #include <find_distortions.h>
00073 #include <nst.h>
00074 
00075 #include <sinfoni_key_names.h>
00076 #include <sinfoni_pro_types.h>
00077 #include <sinfoni_globals.h>
00078 #include <sinfoni_raw_types.h>
00079 #include <sinfoni_tpl_utils.h>
00080 #include <sinfoni_tpl_dfs.h>
00081 #include <sinfoni_globals.h>
00082 #include <sinfoni_functions.h>
00083 #include <sinfoni_memory.h>
00084 
00085 /*-----------------------------------------------------------------------------
00086                             Functions prototypes
00087  -----------------------------------------------------------------------------*/
00088 
00089 static cxint si_rec_distortion_new(cpl_parameterlist *, cpl_frameset *);
00090 
00091 static cxint si_rec_distortion_new_create(cpl_plugin *plugin);
00092 static cxint si_rec_distortion_new_exec(cpl_plugin *plugin);
00093 static cxint si_rec_distortion_new_destroy(cpl_plugin *plugin);
00094 
00095 static cxint si_rec_distortion_new(cpl_parameterlist *config, cpl_frameset *set);
00096 
00097 int cpl_plugin_get_info(cpl_pluginlist *list);
00098 
00099 
00100 /*-----------------------------------------------------------------------------
00101                             Static variables
00102  -----------------------------------------------------------------------------*/
00103 
00104 
00105 static char si_rec_distortion_new_description1[] =
00106 "This recipe computes the detector's distortion and the slitlet distances.\n"
00107 "Additional relevant products are a master flat, a bad pixel map, a wavelength map.\n"
00108 "The input files are :\n"
00109 "raw flats having the first column of some slitlets illuminated by a fibre with tag is FIBRE_NS\n"
00110 "normal raw on/off flats with tag FLAT_NS and\n"
00111 "Arc lamp files with tag WAVE_NS\n"
00112 "A corresponding (band) reference line table with tag REF_LINE_ARC\n";
00113 
00114 
00115 
00116 static char si_rec_distortion_new_description2[] =
00117 "The main products are a distortion and a slitlet distances tables\n"
00118 "(PRO.CATG=DISTORTION and SLITLETS_DISTANCE)\n"
00119 "Information on relevant parameters can be found with\n"
00120 "esorex --params si_rec_distortion_new\n"
00121 "esorex --help si_rec_distortion_new\n"
00122 "\n";
00123 
00124 
00125 static char si_rec_distortion_new_description[800]; 
00126 
00127 /*-----------------------------------------------------------------------------
00128                                 Functions code
00129  -----------------------------------------------------------------------------*/
00130 
00131  /*
00132   *
00133   * Create the recipe instance, i.e. setup the parameter list for this
00134 
00135   *
00136   */
00137 
00138 
00139 static cxint
00140 si_rec_distortion_new_create(cpl_plugin *plugin)
00141 {
00142 
00143   /*
00144    * We have to provide the option we accept to the application.
00145    * We need to setup our parameter list and hook it into the recipe
00146    * interface.
00147    */
00148   cpl_recipe *recipe = (cpl_recipe *)plugin;
00149   recipe->parameters = cpl_parameterlist_new();
00150   if(recipe->parameters == NULL) {
00151     return 1;
00152   }
00153 
00154   /*
00155    * Fill the parameter list.
00156    */
00157 
00158 
00159   sinfoni_lamp_flats_config_add(recipe->parameters);
00160   sinfoni_bp_config_add(recipe->parameters);  
00161   sinfoni_bp_dist_config_add(recipe->parameters); 
00162   sinfoni_prepare_stacked_frames_config_add(recipe->parameters);
00163 
00164   sinfoni_distortion_config_add(recipe->parameters);
00165   sinfoni_north_south_test_config_add(recipe->parameters);
00166   
00167   return 0;
00168 
00169 }
00170 
00171 static cxint
00172 si_rec_distortion_new_exec(cpl_plugin *plugin)
00173 {
00174 
00175   cpl_recipe *recipe = (cpl_recipe *) plugin;
00176   if(recipe->parameters == NULL) {
00177     return 1;
00178   }
00179   if(recipe->frames == NULL) {
00180     return 1;
00181   }
00182 
00183   cpl_error_reset();
00184   return si_rec_distortion_new(recipe->parameters, recipe->frames);
00185 
00186 }
00187 
00188 static cxint
00189 si_rec_distortion_new_destroy(cpl_plugin *plugin)
00190 {
00191 
00192   cpl_recipe *recipe = (cpl_recipe *) plugin;
00193   /*
00194    * We just destroy what was created during the plugin initializzation phase
00195    * i.e. the parameter list. The frame set is managed by the application which
00196    * called us, so that we must not touch it.
00197    */
00198 
00199   cpl_parameterlist_delete(recipe->parameters);
00200 
00201   return 0;
00202 
00203 }
00204 
00205 int
00206 cpl_plugin_get_info(cpl_pluginlist *list)
00207 {
00208 
00209   cpl_recipe *recipe = cpl_calloc(1, sizeof *recipe);
00210   cpl_plugin *plugin = &recipe->interface;
00211 strcpy(si_rec_distortion_new_description,si_rec_distortion_new_description1);
00212 strcat(si_rec_distortion_new_description,si_rec_distortion_new_description2);
00213 
00214 
00215   cpl_plugin_init(plugin,
00216           CPL_PLUGIN_API,
00217           SINFONI_BINARY_VERSION,
00218           CPL_PLUGIN_TYPE_RECIPE,
00219           "si_rec_distortion_new",
00220           "Find detector's distortions and slitlets distances",
00221           si_rec_distortion_new_description,
00222           "Andrea Modigliani",
00223           "Andrea.Modigliani@eso.org",
00224           sinfoni_get_license(),
00225           si_rec_distortion_new_create,
00226           si_rec_distortion_new_exec,
00227           si_rec_distortion_new_destroy);
00228 
00229   cpl_pluginlist_append(list, plugin);
00230 
00231   return 0;
00232 
00233 }
00234 
00235 
00236 /*
00237  * The actual recipe actually start here.
00238  */
00239 
00240 static cxint
00241 si_rec_distortion_new(cpl_parameterlist *config, cpl_frameset *set)
00242 {
00243  
00244   const char *_id = "si_rec_distortion_new";
00245   int  ind =0;
00246   cpl_parameter* p=NULL;
00247   cpl_frame* frame=NULL;
00248   char file_name[FILE_NAME_SZ];
00249   cpl_propertylist* plist=NULL;
00250 
00251   cpl_image* ima=NULL;
00252   fake* fk=fake_new();
00253 
00254    if(sinfoni_dfs_set_groups(set)) {
00255 
00256        cpl_msg_error(_id, "Cannot indentify RAW and CALIB frames") ;
00257        fake_delete(fk);
00258        return -1;
00259 
00260     }
00261 
00262   cpl_msg_info(_id,"-----------------------------");
00263   cpl_msg_info(_id,"  DETERMINE MASTER_LAMP_NS ");
00264   cpl_msg_info(_id,"-----------------------------");
00265 
00266 
00267     { 
00268         if ( -1 == (ind = lamp_flats(config, set ) ) )
00269         {
00270         cpl_msg_error(_id,"no: %d\n", ind) ;
00271             fake_delete(fk);
00272         return -1 ;
00273     }
00274     cpl_msg_info (_id,"SUCCESS DETERMINATION MASTER_LAMP_NS") ; 
00275       
00276     }    
00277     sinfoni_memory_status();
00278   cpl_msg_info(_id,"-----------------------------");
00279   cpl_msg_info(_id,"  DETERMINE BP_MAP_DI        ");
00280   cpl_msg_info(_id,"-----------------------------");
00281 
00282     /* 
00283        ---------------------------------------------------------
00284                 BP_SEARCH
00285        ---------------------------------------------------------
00286      */
00287 
00288     {
00289 
00290             p = cpl_parameterlist_find(config,"sinfoni.bp.method");
00291             cpl_parameter_set_string(p,"Normal");   
00292 
00293             if ( -1 == (ind = badSearchNormal(config, set, PRO_BP_MAP_DI ) ) )
00294         {
00295             cpl_msg_error(_id,"badSearchNormal(), no: %d\n", ind) ;
00296                 fake_delete(fk);
00297         return -1 ;
00298         }
00299             cpl_msg_info(_id,"SUCCESS DETERMINATION %s",PRO_BP_MAP_DI);
00300     }
00301     sinfoni_memory_status();
00302 
00303     /* 
00304        ---------------------------------------------------------
00305         1st iteration: get off frame 
00306        ---------------------------------------------------------
00307      */
00308 
00309    {
00310 
00311 
00312         cpl_msg_info (_id,"STACK FIBRE,NS TO GET FAKE OFF\n") ; 
00313 
00314         strcpy(fk->pro_class,RAW_FIBRE_NS);
00315         fk->frm_switch=1;
00316         fk->mask_index=0;
00317         fk->ind_index=0;
00318         fk->flat_index=0;
00319         fk->wfix_index=0;
00320         fk->low_rej=0.0;
00321         fk->hig_rej=0.2;
00322 
00323    
00324         if ( -1 == (ind = prepare_stacked_frames(config, set,PRO_FIBRE_NS_STACKED_OFF,0,fk)))
00325         {
00326         cpl_msg_error(_id," no: %d\n", ind) ;
00327             fake_delete(fk);
00328         return -1 ;
00329     }
00330 
00331         if(NULL != cpl_frameset_find(set,PRO_FIBRE_NS_STACKED_OFF)) {
00332             frame = cpl_frameset_find(set,PRO_FIBRE_NS_STACKED_OFF);
00333             strcpy(file_name,cpl_frame_get_filename(frame));
00334         } else {
00335             cpl_msg_error(_id,"Frame %s not found!", PRO_FIBRE_NS_STACKED_OFF);
00336             fake_delete(fk);
00337             return -1;
00338     }
00339         ima=cpl_image_load(file_name,CPL_TYPE_FLOAT,0,0);
00340 
00341         
00342         if ((cpl_error_code)((plist = cpl_propertylist_load(file_name, 0)) == NULL)) {
00343            cpl_msg_error(_id, "getting header from reference ima frame %s",file_name);
00344            cpl_propertylist_delete(plist) ;
00345             fake_delete(fk);
00346            return -1 ;
00347     }
00348 
00349         if (cpl_propertylist_contains(plist, KEY_NAME_LAMP_HALO)) {
00350             cpl_propertylist_set_bool(plist, KEY_NAME_LAMP_HALO, LAMP_OFF);
00351         } else {
00352             cpl_propertylist_append_bool(plist, KEY_NAME_LAMP_HALO,LAMP_OFF) ;
00353         }
00354         /* Save the file */
00355     /*
00356         if (cpl_image_save(ima, file_name, CPL_BPP_DEFAULT, plist,CPL_IO_DEFAULT)!=CPL_ERROR_NONE) {
00357            cpl_msg_error(_id, "Cannot save the product %s",file_name);
00358            cpl_propertylist_delete(plist) ;
00359           return -1 ;
00360     }
00361     */
00362         cpl_propertylist_delete(plist);
00363     cpl_msg_info (_id,"SUCCESS DETERMINATION %s",PRO_FIBRE_NS_STACKED_OFF) ; 
00364 
00365     }    
00366         cpl_image_delete(ima);
00367     sinfoni_memory_status();
00368 
00369     /* 
00370        ---------------------------------------------------------
00371         2nd iteration: get on frame 
00372        ---------------------------------------------------------
00373      */
00374 
00375    {
00376 
00377         cpl_msg_info (_id,"STACK FIBRE,NS TO GET FAKE ON\n") ; 
00378 
00379         strcpy(fk->pro_class,RAW_FIBRE_NS);
00380         fk->frm_switch=1;
00381         fk->mask_index=0;
00382         fk->ind_index=0;
00383         fk->flat_index=0;
00384         fk->wfix_index=0;
00385         fk->low_rej=0.0;
00386         fk->hig_rej=0.0;
00387 
00388 
00389         if ( -1 == (ind = prepare_stacked_frames(config, set, PRO_FIBRE_NS_STACKED_ON,0,fk)))
00390         {
00391         cpl_msg_error(_id," no: %d\n", ind) ;
00392             fake_delete(fk);
00393         return -1 ;
00394     }
00395         cpl_msg_info (_id,"SUCCESS DETERMINATION %s",PRO_FIBRE_NS_STACKED_ON) ; 
00396     }    
00397     sinfoni_memory_status();
00398 
00399 
00400     /* 
00401        ---------------------------------------------------------
00402                 3rd iteration combines on and off fake frames
00403        ---------------------------------------------------------
00404      */
00405 
00406 
00407    {
00408 
00409     cpl_msg_info (_id,"COMBINES FAKE ON AND OFF\n") ; 
00410 
00411    
00412         strcpy(fk->pro_class,PRO_FIBRE_NS_STACKED);
00413         fk->frm_switch=1;
00414         fk->mask_index=0;
00415         fk->ind_index=0;
00416         fk->flat_index=1;
00417         fk->wfix_index=0;
00418         fk->low_rej=0.0;
00419         fk->hig_rej=0.0;
00420     
00421 
00422          if ( -1 == (ind = prepare_stacked_frames(config, set, PRO_FIBRE_NS_STACKED,0,fk)))
00423         {
00424         cpl_msg_error(_id," no: %d\n", ind) ;
00425             fake_delete(fk);
00426         return -1 ;
00427     }
00428         cpl_msg_info (_id,"SUCCESS DETERMINATION %s",PRO_FIBRE_NS_STACKED) ; 
00429     
00430     }    
00431     sinfoni_memory_status();
00432 
00433 
00434     /* 
00435        ---------------------------------------------------------
00436                 STACK WAVECAL
00437        ---------------------------------------------------------
00438      */
00439 
00440    {
00441 
00442     cpl_msg_info (_id,"STACK on WAVE frame\n") ;
00443 
00444         strcpy(fk->pro_class,RAW_WAVE_NS);
00445         fk->frm_switch=1;
00446         fk->mask_index=0;
00447         fk->ind_index=0;
00448         fk->flat_index=1;
00449         fk->wfix_index=0;
00450         fk->low_rej=0.1;
00451         fk->hig_rej=0.1;
00452    
00453     cpl_msg_info (_id,"STACK on WAVE frame\n") ; 
00454         if ( -1 == (ind = prepare_stacked_frames(config, set, PRO_WAVE_LAMP_STACKED,0,fk)))
00455         {
00456         cpl_msg_error(_id," no: %d\n", ind) ;
00457             fake_delete(fk);
00458         return -1 ;
00459     }
00460         cpl_msg_info (_id,"SUCCESS DETERMINATION %s",PRO_WAVE_LAMP_STACKED) ; 
00461    }    
00462     sinfoni_memory_status();
00463 
00464 
00465 
00466 
00467     /* 
00468        ---------------------------------------------------------
00469                 DISTORTIONS 
00470        ---------------------------------------------------------
00471      */
00472    
00473 
00474     {
00475       
00476     cpl_msg_info(_id,"COMPUTE DISTORTIONS\n") ; 
00477          if ( -1 == (ind = find_distortions(config, set ) ) )
00478         {
00479         cpl_msg_error(_id,"step_distortion(), no: %d\n", ind) ;
00480             fake_delete(fk);
00481         return -1 ;
00482     }
00483    
00484         if(NULL != cpl_frameset_find(set,PRO_FIBRE_NS_STACKED)) {
00485             frame = cpl_frameset_find(set,PRO_FIBRE_NS_STACKED);
00486             strcpy(file_name,cpl_frame_get_filename(frame));
00487         } else {
00488             cpl_msg_error(_id,"Frame %s not found!", PRO_FIBRE_NS_STACKED);
00489             fake_delete(fk);
00490             return -1;
00491     }
00492         ima=cpl_image_load(file_name,CPL_TYPE_FLOAT,0,0);
00493 
00494         if ((cpl_error_code)((plist = cpl_propertylist_load(file_name, 0)) == NULL)) {
00495            cpl_msg_error(_id, "getting header from reference ima frame %s",file_name);
00496            cpl_propertylist_delete(plist) ;
00497             fake_delete(fk);
00498            return -1 ;
00499     }
00500 
00501         if (cpl_propertylist_contains(plist, KEY_NAME_LAMP_HALO)) {
00502             cpl_propertylist_set_bool(plist, KEY_NAME_LAMP_HALO, LAMP_ON);
00503         } else {
00504             cpl_propertylist_append_bool(plist, KEY_NAME_LAMP_HALO,LAMP_ON) ;
00505         }
00506      
00507 
00508 
00509         /* Save the file */
00510     /*
00511         if (cpl_image_save(ima, file_name, CPL_BPP_DEFAULT, plist,CPL_IO_DEFAULT)!=CPL_ERROR_NONE) {
00512            cpl_msg_error(_id, "Cannot save the product %s",file_name);
00513            cpl_propertylist_delete(plist) ;
00514           return -1 ;
00515     }
00516     */
00517      
00518         cpl_propertylist_delete(plist);
00519     cpl_msg_info(_id,"SUCCESS: COMPUTED DISTORTIONS\n") ; 
00520     }
00521    cpl_image_delete(ima);
00522     sinfoni_memory_status();
00523       
00524 
00525     /* 
00526        ---------------------------------------------------------
00527        4th iteration: distort fake frame
00528        ---------------------------------------------------------
00529      */
00530 
00531   
00532      {
00533     cpl_msg_info (_id,"DISTORT FAKE FRAME\n") ; 
00534 
00535         strcpy(fk->pro_class,PRO_FIBRE_NS_STACKED_DIST);
00536         fk->frm_switch=1;
00537         fk->mask_index=1;
00538         fk->ind_index=1;
00539         fk->flat_index=0;
00540         fk->wfix_index=1;
00541         fk->low_rej=0.0;
00542         fk->hig_rej=0.0;
00543 
00544 
00545         if ( -1 == (ind = prepare_stacked_frames(config, set, PRO_FIBRE_NS_STACKED_DIST,0,fk)))
00546         {
00547         cpl_msg_error(_id," no: %d\n", ind) ;
00548             fake_delete(fk);
00549         return -1 ;
00550     }
00551     cpl_msg_info (_id,"SUCCESS: DISTORTED FAKE FRAME\n") ; 
00552     
00553 
00554         if(NULL != cpl_frameset_find(set,PRO_FIBRE_NS_STACKED_DIST)) {
00555             frame = cpl_frameset_find(set,PRO_FIBRE_NS_STACKED_DIST);
00556             strcpy(file_name,cpl_frame_get_filename(frame));
00557         } else {
00558             cpl_msg_error(_id,"Frame %s not found!", PRO_FIBRE_NS_STACKED_DIST);
00559             fake_delete(fk);
00560             return -1;
00561     }
00562         ima=cpl_image_load(file_name,CPL_TYPE_FLOAT,0,0);
00563 
00564 
00565     
00566         if ((cpl_error_code)((plist = cpl_propertylist_load(file_name, 0)) == NULL)) {
00567            cpl_msg_error(_id, "getting header from reference ima frame %s",file_name);
00568            cpl_propertylist_delete(plist) ;
00569            fake_delete(fk);
00570            return -1 ;
00571     }
00572 
00573         if (cpl_propertylist_contains(plist, KEY_NAME_LAMP_HALO)) {
00574             cpl_propertylist_set_bool(plist, KEY_NAME_LAMP_HALO, LAMP_ON);
00575         } else {
00576             cpl_propertylist_append_bool(plist, KEY_NAME_LAMP_HALO,LAMP_ON) ;
00577         }
00578   
00579 
00580         /* Save the file */
00581     /*
00582         if (cpl_image_save(ima, file_name, CPL_BPP_DEFAULT, plist,CPL_IO_DEFAULT)!=CPL_ERROR_NONE) {
00583            cpl_msg_error(_id, "Cannot save the product %s",file_name);
00584            cpl_propertylist_delete(plist) ;
00585           return -1 ;
00586     }
00587     */
00588   
00589         cpl_propertylist_delete(plist);
00590         cpl_image_delete(ima);
00591 
00592     cpl_msg_info(_id,"SUCCESS: COMPUTED DISTORTIONS\n") ; 
00593      }
00594     sinfoni_memory_status();
00595 
00596   
00597     /* 
00598        ---------------------------------------------------------
00599                                NST 
00600        ---------------------------------------------------------
00601      */
00602 
00603     {
00604     cpl_msg_info (_id,"RUN NORD SUD TEST\n") ; 
00605         if ( -1 == (ind = nst(config, set ) ) )
00606         {
00607         cpl_msg_error(_id,"no: %d\n", ind) ;
00608             fake_delete(fk);
00609         return -1 ;
00610     }
00611      
00612     cpl_msg_info (_id,"SUCCESS: RUNNED NORD SUD TEST\n") ; 
00613 
00614     }    
00615     cpl_msg_info (_id,"SUCCESS: RECIPE\n") ; 
00616     fake_delete(fk);
00617     sinfoni_memory_status();
00618     return 0 ;
00619 
00620 
00621 }
00622 
00623 
00624 
00625 
00626 
00627 
00628 
00629 
00630 
00631 
00632 
00633 
00634 

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