sinfo_utl_remove_crh_single.c

00001 /* $Id: sinfo_utl_remove_crh_single.c,v 1.2 2009/01/30 14:56:12 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: 2009/01/30 14:56:12 $
00024  * $Revision: 1.2 $
00025  * $Name: HEAD $
00026  */
00027 
00028 #ifdef HAVE_CONFIG_H
00029 #include <config.h>
00030 #endif
00031 
00032 /*-----------------------------------------------------------------------------
00033                                 Includes
00034  ----------------------------------------------------------------------------*/
00035 #include <string.h>
00036 
00037 /* cpl */
00038 #include <cpl.h>
00039 
00040 /* irplib */
00041 #include <irplib_utils.h>
00042 
00043 #include <sinfo_tpl_utils.h>
00044 #include <sinfo_pfits.h>
00045 #include <sinfo_tpl_dfs.h>
00046 #include <sinfo_key_names.h>
00047 #include <sinfo_raw_types.h>
00048 #include <sinfo_pro_types.h>
00049 #include <sinfo_functions.h>
00050 #include <sinfo_msg.h>
00051 #include <sinfo_error.h>
00052 #include <sinfo_utils_wrappers.h>
00053 #include <sinfo_remove_crh_single.h>
00054 
00055 /*-----------------------------------------------------------------------------
00056                             Functions prototypes
00057  ----------------------------------------------------------------------------*/
00058 
00059 static int sinfo_utl_remove_crh_single_create(cpl_plugin *) ;
00060 static int sinfo_utl_remove_crh_single_exec(cpl_plugin *) ;
00061 static int sinfo_utl_remove_crh_single_destroy(cpl_plugin *) ;
00062 static int sinfo_utl_remove_crh_single(cpl_parameterlist *, cpl_frameset *) ;
00063 
00064 /*-----------------------------------------------------------------------------
00065                             Static variables
00066  ----------------------------------------------------------------------------*/
00067 
00068 static char sinfo_utl_remove_crh_single_description[] =
00069 "This recipe performs image computation.\n"
00070 "The input files is one image\n"
00071 "their associated tags should be IMA.\n"
00072 "The output is the image cleaned from CRHs\n"
00073 "Information on relevant parameters can be found with\n"
00074 "esorex --params sinfo_utl_remove_crh_single\n"
00075 "esorex --help sinfo_utl_remove_crh_single\n"
00076 "\n";
00077 
00078 /*-----------------------------------------------------------------------------
00079                                 Functions code
00080  ----------------------------------------------------------------------------*/
00081 /*---------------------------------------------------------------------------*/
00085 /*---------------------------------------------------------------------------*/
00086 
00088 /*---------------------------------------------------------------------------*/
00096 /*---------------------------------------------------------------------------*/
00097 int cpl_plugin_get_info(cpl_pluginlist * list)
00098 {
00099     cpl_recipe  *   recipe = cpl_calloc(1, sizeof *recipe ) ;
00100     cpl_plugin  *   plugin = &recipe->interface ;
00101 
00102     cpl_plugin_init(plugin,
00103                     CPL_PLUGIN_API,
00104                     SINFONI_BINARY_VERSION,
00105                     CPL_PLUGIN_TYPE_RECIPE,
00106                     "sinfo_utl_remove_crh_single",
00107                     "Remove CRHs from an image",
00108                     sinfo_utl_remove_crh_single_description,
00109                     "Andrea Modigliani",
00110                     "Andrea.Modigliani@eso.org",
00111                     sinfo_get_license(),
00112                     sinfo_utl_remove_crh_single_create,
00113                     sinfo_utl_remove_crh_single_exec,
00114                     sinfo_utl_remove_crh_single_destroy) ;
00115 
00116     cpl_pluginlist_append(list, plugin) ;
00117     
00118     return 0;
00119 }
00120 
00121 /*---------------------------------------------------------------------------*/
00130 /*---------------------------------------------------------------------------*/
00131 static int sinfo_utl_remove_crh_single_create(cpl_plugin * plugin)
00132 {
00133     cpl_recipe      * recipe ;
00134     cpl_parameter   * p ;
00135 
00136     /* Get the recipe out of the plugin */
00137     if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE) 
00138         recipe = (cpl_recipe *)plugin ;
00139     else return -1 ;
00140     cpl_error_reset();
00141     irplib_reset();
00142 
00143     /* Create the parameters list in the cpl_recipe object */
00144     recipe->parameters = cpl_parameterlist_new() ; 
00145 
00146     /* Fill the parameters list */
00147     /* --stropt */
00148     p = cpl_parameter_new_value("sinfoni.sinfo_utl_remove_crh_single.crh_frac_max", 
00149                                 CPL_TYPE_DOUBLE, 
00150                                 "Maximum fraction of allowed CRHs", 
00151                                 "sinfoni.sinfo_utl_remove_crh_single",0.7);
00152     cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "crh_frac_max") ;
00153     cpl_parameterlist_append(recipe->parameters, p) ;
00154 
00155     /* --doubleopt */
00156     p = cpl_parameter_new_value("sinfoni.sinfo_utl_remove_crh_single.sigma_lim", 
00157                 CPL_TYPE_DOUBLE, 
00158                 "Maximum sigma in kappa-sigma clip",
00159                 "sinfoni.sinfo_utl_remove_crh_single", 25.) ;
00160     cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "sigma_lim") ;
00161     cpl_parameterlist_append(recipe->parameters, p) ;
00162 
00163 
00164 
00165     p = cpl_parameter_new_value("sinfoni.sinfo_utl_remove_crh_single.f_lim", 
00166                 CPL_TYPE_DOUBLE, 
00167                 "Max fraction of bad pixels allowed",
00168                 "sinfoni.sinfo_utl_remove_crh_single", 0.7) ;
00169     cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "f_lim") ;
00170     cpl_parameterlist_append(recipe->parameters, p) ;
00171 
00172 
00173     p = cpl_parameter_new_value("sinfoni.sinfo_utl_remove_crh_single.max_iter",
00174                 CPL_TYPE_INT, 
00175                 "Max fraction of bad pixels allowed",
00176                 "sinfoni.sinfo_utl_remove_crh_single",5) ;
00177     cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "max_iter") ;
00178     cpl_parameterlist_append(recipe->parameters, p) ;
00179 
00180 
00181 
00182     p = cpl_parameter_new_value("sinfoni.sinfo_utl_remove_crh_single.gain",
00183                 CPL_TYPE_DOUBLE, 
00184                 "Detector's gain",
00185                 "sinfoni.sinfo_utl_remove_crh_single",2.42) ;
00186     cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "gain") ;
00187     cpl_parameterlist_append(recipe->parameters, p) ;
00188 
00189 
00190     p = cpl_parameter_new_value("sinfoni.sinfo_utl_remove_crh_single.ron",
00191                 CPL_TYPE_DOUBLE, 
00192                 "Detector's ron",
00193                 "sinfoni.sinfo_utl_remove_crh_single",1.) ;
00194     cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "ron") ;
00195     cpl_parameterlist_append(recipe->parameters, p) ;
00196 
00197 
00198 
00199  
00200     /* Return */
00201     return 0;
00202 }
00203 
00204 /*---------------------------------------------------------------------------*/
00210 /*---------------------------------------------------------------------------*/
00211 static int sinfo_utl_remove_crh_single_exec(cpl_plugin * plugin)
00212 {
00213     cpl_recipe  *   recipe ;
00214      int code=0;
00215      cpl_errorstate initial_errorstate = cpl_errorstate_get();
00216 
00217     /* Get the recipe out of the plugin */
00218     if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE) 
00219         recipe = (cpl_recipe *)plugin ;
00220     else return -1 ;
00221     cpl_error_reset();
00222     irplib_reset();
00223     code = sinfo_utl_remove_crh_single(recipe->parameters, recipe->frames) ;
00224 
00225 
00226     if (!cpl_errorstate_is_equal(initial_errorstate)) {                      
00227         /* Dump the error history since recipe execution start.                
00228            At this point the recipe cannot recover from the error */           
00229         cpl_errorstate_dump(initial_errorstate, CPL_FALSE, NULL);              
00230     } 
00231 
00232     return code ;
00233 }
00234 
00235 /*---------------------------------------------------------------------------*/
00241 /*---------------------------------------------------------------------------*/
00242 static int sinfo_utl_remove_crh_single_destroy(cpl_plugin * plugin)
00243 {
00244     cpl_recipe  *   recipe ;
00245     
00246     /* Get the recipe out of the plugin */
00247     if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE) 
00248         recipe = (cpl_recipe *)plugin ;
00249     else return -1 ;
00250 
00251     cpl_parameterlist_delete(recipe->parameters) ; 
00252     return 0 ;
00253 }
00254 
00255 /*---------------------------------------------------------------------------*/
00262 /*---------------------------------------------------------------------------*/
00263 static int 
00264 sinfo_utl_remove_crh_single( cpl_parameterlist   *   parlist, 
00265         cpl_frameset        *   framelist)
00266 {
00267     cpl_parameter       *   param= NULL ;
00268     cpl_frameset * raw_on=NULL;
00269     cpl_frameset * raw_off=NULL;
00270     double crh_frac_max=0;
00271     double sigma_lim=0;
00272     double f_lim=0;
00273     int max_iter=0;
00274     double gain=0;
00275     double ron=0;
00276     int nraw=0;
00277     int n=0;
00278     cpl_image* ima_res=NULL;
00279     char name[256];
00280     cpl_image* image=NULL;
00281     cpl_image* img_on=NULL;
00282     cpl_image* img_off=NULL;
00283     cpl_propertylist* plist=NULL;
00284     const char* name_o=NULL;
00285     cpl_frame* product_frame=NULL;
00286     cpl_frame* frame_on=NULL;
00287     cpl_frame* frame_off=NULL;
00288 
00289           sinfo_msg("Welcome to SINFONI Pipeline release %d.%d.%d",
00290         SINFONI_MAJOR_VERSION,SINFONI_MINOR_VERSION,SINFONI_MICRO_VERSION);
00291 
00292     /* HOW TO RETRIEVE INPUT PARAMETERS */
00293     check_nomsg(param=cpl_parameterlist_find(parlist, 
00294                                              "sinfoni.sinfo_utl_remove_crh_single.crh_frac_max"));
00295     check_nomsg(crh_frac_max=cpl_parameter_get_double(param));
00296 
00297     check_nomsg(param=cpl_parameterlist_find(parlist,
00298                                              "sinfoni.sinfo_utl_remove_crh_single.sigma_lim"));
00299     check_nomsg(sigma_lim = cpl_parameter_get_double(param)) ;
00300 
00301 
00302     check_nomsg(param=cpl_parameterlist_find(parlist,
00303                                              "sinfoni.sinfo_utl_remove_crh_single.f_lim"));
00304     check_nomsg(f_lim = cpl_parameter_get_double(param)) ;
00305 
00306     check_nomsg(param=cpl_parameterlist_find(parlist,
00307                                              "sinfoni.sinfo_utl_remove_crh_single.max_iter"));
00308     check_nomsg(max_iter = cpl_parameter_get_int(param)) ;
00309 
00310     check_nomsg(param=cpl_parameterlist_find(parlist,
00311                                              "sinfoni.sinfo_utl_remove_crh_single.gain"));
00312     check_nomsg(gain = cpl_parameter_get_double(param)) ;
00313 
00314 
00315     check_nomsg(param=cpl_parameterlist_find(parlist,
00316                                              "sinfoni.sinfo_utl_remove_crh_single.ron"));
00317     check_nomsg(ron = cpl_parameter_get_double(param)) ;
00318   
00319     /* Identify the RAW and CALIB frames in the input frameset */
00320     check(sinfo_dfs_set_groups(framelist),
00321          "Cannot identify RAW and CALIB frames") ;
00322     //cpl_frameset_dump(framelist,stdout);
00323 
00324     /* HOW TO ACCESS INPUT DATA */
00325     n=cpl_frameset_get_size(framelist);
00326     if(n<1) {
00327       sinfo_msg_error("Empty input frame list!");
00328       goto cleanup ;
00329     }
00330 
00331     /* HOW TO ACCESS INPUT DATA */
00332     check_nomsg(raw_on=cpl_frameset_new());
00333 
00334     check(sinfo_contains_frames_kind(framelist,raw_on,RAW_ON),
00335      "Found no input frames with tag %s",RAW_ON);
00336     check_nomsg(nraw=cpl_frameset_get_size(raw_on));
00337     if (nraw<1) {
00338       sinfo_msg_error("Found no input frames with tag %s",RAW_ON);
00339       goto cleanup;
00340     }
00341 
00342 
00343     check_nomsg(raw_off=cpl_frameset_new());
00344 
00345     check(sinfo_contains_frames_kind(framelist,raw_off,RAW_OFF),
00346      "Found no input frames with tag %s",RAW_OFF);
00347     check_nomsg(nraw=cpl_frameset_get_size(raw_off));
00348     if (nraw<1) {
00349       sinfo_msg_error("Found no input frames with tag %s",RAW_OFF);
00350       goto cleanup;
00351     }
00352 
00353 
00354     check_nomsg(frame_on=cpl_frameset_get_first(raw_on));
00355     check_nomsg(strcpy(name,cpl_frame_get_filename(frame_on)));
00356     check_nomsg(sinfo_free_frameset(&raw_on));
00357     sinfo_msg("name=%s",name);
00358     check_nomsg(img_on=cpl_image_load(name,CPL_TYPE_FLOAT,0,0));
00359     check(plist=cpl_propertylist_load(name,0),
00360           "Cannot read the FITS header") ;
00361 
00362 
00363     frame_off=cpl_frameset_get_first(raw_off);
00364     strcpy(name,cpl_frame_get_filename(frame_off));
00365     sinfo_free_frameset(&raw_off);
00366 
00367     img_off=cpl_image_load(name,CPL_TYPE_FLOAT,0,0);
00368 
00369     image=cpl_image_duplicate(img_on);
00370     sinfo_free_image(&img_on);
00371 
00372     cpl_image_subtract(image,img_off);
00373     cpl_image_save(image,"image_with_crh.fits",CPL_BPP_IEEE_FLOAT,NULL,
00374            CPL_IO_DEFAULT);
00375 
00376     check(ima_res=sinfo_remove_crh_single(image,crh_frac_max,sigma_lim,f_lim,
00377                       max_iter,gain,ron),
00378       "fail to remove CRHs");
00379 
00380     sinfo_free_image(&image); 
00381 
00382 
00383 
00384     /* HOW TO SAVE A PRODUCT ON DISK  */
00385     /* Set the file name */
00386     name_o = "ima_res.fits" ;
00387 
00388     /* Create product frame */
00389     check_nomsg(product_frame = cpl_frame_new());
00390     check_nomsg(cpl_frame_set_filename(product_frame, name_o)) ;
00391     check_nomsg(cpl_frame_set_tag(product_frame, SI_UTL_IMA_ARITH_PROIMA)) ;
00392     check_nomsg(cpl_frame_set_type(product_frame, CPL_FRAME_TYPE_IMAGE)) ;
00393     check_nomsg(cpl_frame_set_group(product_frame, CPL_FRAME_GROUP_PRODUCT)) ;
00394     check(cpl_frame_set_level(product_frame, CPL_FRAME_LEVEL_FINAL),
00395       "Error while initialising the product frame") ;
00396     
00397     /* Add DataFlow keywords */
00398     check_nomsg(cpl_propertylist_erase_regexp(plist, "^ESO PRO CATG",0));
00399 #if defined CPL_VERSION_CODE && CPL_VERSION_CODE >= CPL_VERSION(4, 8, 0) 
00400     check(cpl_dfs_setup_product_header(plist, 
00401                                        product_frame, 
00402                                        framelist, 
00403                                        parlist,
00404                                        "sinfo_utl_remove_crh_single", 
00405                                        "SINFONI", 
00406                                        KEY_VALUE_HPRO_DID,NULL),
00407       "Problem in the product DFS-compliance") ;
00408 #else
00409     check(cpl_dfs_setup_product_header(plist, 
00410                                        product_frame, 
00411                                        framelist, 
00412                                        parlist,
00413                                        "sinfo_utl_remove_crh_single", 
00414                                        "SINFONI", 
00415                                        KEY_VALUE_HPRO_DID),
00416       "Problem in the product DFS-compliance") ;
00417 #endif
00418     /* Save the file */
00419     check(cpl_image_save(ima_res, 
00420                          name_o, 
00421                          CPL_BPP_IEEE_FLOAT, 
00422                          plist,
00423                          CPL_IO_DEFAULT),
00424       "Could not save product");
00425     sinfo_free_propertylist(&plist) ;
00426 
00427 
00428     /* Log the saved file in the input frameset */
00429     check_nomsg(cpl_frameset_insert(framelist, product_frame)) ;
00430 
00431 
00432  cleanup:
00433    
00434     sinfo_free_frameset(&raw_on);
00435     sinfo_free_frameset(&raw_off);
00436     sinfo_free_image(&img_on);
00437     sinfo_free_image(&img_off);
00438     sinfo_free_propertylist(&plist) ;
00439     /* This is usually freed by esorex: but what about if errors occurs?
00440     sinfo_free_frame(&product_frame) ;
00441     */
00442     sinfo_free_image(&image) ;
00443 
00444     if (cpl_error_get_code()) {
00445         return -1 ;
00446     } else {
00447         return 0 ;
00448     }
00449 
00450 }

Generated on 3 Mar 2013 for SINFONI Pipeline Reference Manual by  doxygen 1.6.1