uves_utl_remove_crh_single.c

00001 /* $Id: uves_utl_remove_crh_single.c,v 1.2 2009/02/20 18:20:03 amodigli Exp $
00002  *
00003  * This file is part of the UVES 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/02/20 18:20:03 $
00024  * $Revision: 1.2 $
00025  * $Name: uves-5_0_0 $
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 /*
00044 #include <uves_tpl_utils.h>
00045 #include <uves_pfits.h>
00046 #include <uves_key_names.h>
00047 #include <uves_raw_types.h>
00048 #include <uves_pro_types.h>
00049 #include <uves_functions.h>
00050 */
00051 
00052 #include <uves_dfs.h>
00053 #include <uves_msg.h>
00054 #include <uves_error.h>
00055 #include <uves_utils_wrappers.h>
00056 #include <uves_remove_crh_single.h>
00057 
00058 /*-----------------------------------------------------------------------------
00059                             Functions prototypes
00060  ----------------------------------------------------------------------------*/
00061 
00062 static int uves_utl_remove_crh_single_create(cpl_plugin *) ;
00063 static int uves_utl_remove_crh_single_exec(cpl_plugin *) ;
00064 static int uves_utl_remove_crh_single_destroy(cpl_plugin *) ;
00065 static int uves_utl_remove_crh_single(cpl_parameterlist *, cpl_frameset *) ;
00066 
00067 /*-----------------------------------------------------------------------------
00068                             Static variables
00069  ----------------------------------------------------------------------------*/
00070 
00071 static char uves_utl_remove_crh_single_description[] =
00072 "This recipe performs image computation.\n"
00073 "The input files is one image\n"
00074 "their associated tags should be IMA.\n"
00075 "The output is the image cleaned from CRHs\n"
00076 "Information on relevant parameters can be found with\n"
00077 "esorex --params uves_utl_remove_crh_single\n"
00078 "esorex --help uves_utl_remove_crh_single\n"
00079 "\n";
00080 
00081 /*-----------------------------------------------------------------------------
00082                                 Functions code
00083  ----------------------------------------------------------------------------*/
00084 /*---------------------------------------------------------------------------*/
00088 /*---------------------------------------------------------------------------*/
00089 
00091 /*---------------------------------------------------------------------------*/
00099 /*---------------------------------------------------------------------------*/
00100 int cpl_plugin_get_info(cpl_pluginlist * list)
00101 {
00102     cpl_recipe  *   recipe = cpl_calloc(1, sizeof *recipe ) ;
00103     cpl_plugin  *   plugin = &recipe->interface ;
00104 
00105     cpl_plugin_init(plugin,
00106                     CPL_PLUGIN_API,
00107                     UVES_BINARY_VERSION,
00108                     CPL_PLUGIN_TYPE_RECIPE,
00109                     "uves_utl_remove_crh_single",
00110                     "Remove CRHs from an image",
00111                     uves_utl_remove_crh_single_description,
00112                     "Andrea Modigliani",
00113                     "Andrea.Modigliani@eso.org",
00114                     uves_get_license(),
00115                     uves_utl_remove_crh_single_create,
00116                     uves_utl_remove_crh_single_exec,
00117                     uves_utl_remove_crh_single_destroy) ;
00118 
00119     cpl_pluginlist_append(list, plugin) ;
00120     
00121     return 0;
00122 }
00123 
00124 /*---------------------------------------------------------------------------*/
00133 /*---------------------------------------------------------------------------*/
00134 static int uves_utl_remove_crh_single_create(cpl_plugin * plugin)
00135 {
00136     cpl_recipe      * recipe ;
00137     cpl_parameter   * p ;
00138 
00139     /* Get the recipe out of the plugin */
00140     if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE) 
00141         recipe = (cpl_recipe *)plugin ;
00142     else return -1 ;
00143     cpl_error_reset();
00144     irplib_reset();
00145 
00146     /* Create the parameters list in the cpl_recipe object */
00147     recipe->parameters = cpl_parameterlist_new() ; 
00148 
00149     /* Fill the parameters list */
00150     /* --stropt */
00151     p = cpl_parameter_new_value("uves.uves_utl_remove_crh_single.crh_frac_max", 
00152                                 CPL_TYPE_DOUBLE, 
00153                                 "Maximum fraction of allowed CRHs", 
00154                                 "uves.uves_utl_remove_crh_single",0.7);
00155     cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "crh_frac_max") ;
00156     cpl_parameterlist_append(recipe->parameters, p) ;
00157 
00158     /* --doubleopt */
00159     p = cpl_parameter_new_value("uves.uves_utl_remove_crh_single.sigma_lim", 
00160                 CPL_TYPE_DOUBLE, 
00161                 "Maximum sigma in kappa-sigma clip",
00162                 "uves.uves_utl_remove_crh_single", 25.) ;
00163     cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "sigma_lim") ;
00164     cpl_parameterlist_append(recipe->parameters, p) ;
00165 
00166 
00167 
00168     p = cpl_parameter_new_value("uves.uves_utl_remove_crh_single.f_lim", 
00169                 CPL_TYPE_DOUBLE, 
00170                 "Max fraction of bad pixels allowed",
00171                 "uves.uves_utl_remove_crh_single", 0.7) ;
00172     cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "f_lim") ;
00173     cpl_parameterlist_append(recipe->parameters, p) ;
00174 
00175 
00176     p = cpl_parameter_new_value("uves.uves_utl_remove_crh_single.max_iter",
00177                 CPL_TYPE_INT, 
00178                 "Max fraction of bad pixels allowed",
00179                 "uves.uves_utl_remove_crh_single",5) ;
00180     cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "max_iter") ;
00181     cpl_parameterlist_append(recipe->parameters, p) ;
00182 
00183 
00184 
00185     p = cpl_parameter_new_value("uves.uves_utl_remove_crh_single.gain",
00186                 CPL_TYPE_DOUBLE, 
00187                 "Detector's gain",
00188                 "uves.uves_utl_remove_crh_single",2.42) ;
00189     cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "gain") ;
00190     cpl_parameterlist_append(recipe->parameters, p) ;
00191 
00192 
00193     p = cpl_parameter_new_value("uves.uves_utl_remove_crh_single.ron",
00194                 CPL_TYPE_DOUBLE, 
00195                 "Detector's ron",
00196                 "uves.uves_utl_remove_crh_single",1.) ;
00197     cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "ron") ;
00198     cpl_parameterlist_append(recipe->parameters, p) ;
00199 
00200 
00201 
00202  
00203     /* Return */
00204     return 0;
00205 }
00206 
00207 /*---------------------------------------------------------------------------*/
00213 /*---------------------------------------------------------------------------*/
00214 static int uves_utl_remove_crh_single_exec(cpl_plugin * plugin)
00215 {
00216     cpl_recipe  *   recipe ;
00217      int code=0;
00218      cpl_errorstate initial_errorstate = cpl_errorstate_get();
00219 
00220     /* Get the recipe out of the plugin */
00221     if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE) 
00222         recipe = (cpl_recipe *)plugin ;
00223     else return -1 ;
00224     cpl_error_reset();
00225     irplib_reset();
00226     code = uves_utl_remove_crh_single(recipe->parameters, recipe->frames) ;
00227 
00228 
00229     if (!cpl_errorstate_is_equal(initial_errorstate)) {                      
00230         /* Dump the error history since recipe execution start.                
00231            At this point the recipe cannot recover from the error */           
00232         cpl_errorstate_dump(initial_errorstate, CPL_FALSE, NULL);              
00233     } 
00234 
00235     return code ;
00236 }
00237 
00238 /*---------------------------------------------------------------------------*/
00244 /*---------------------------------------------------------------------------*/
00245 static int uves_utl_remove_crh_single_destroy(cpl_plugin * plugin)
00246 {
00247     cpl_recipe  *   recipe ;
00248     
00249     /* Get the recipe out of the plugin */
00250     if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE) 
00251         recipe = (cpl_recipe *)plugin ;
00252     else return -1 ;
00253 
00254     cpl_parameterlist_delete(recipe->parameters) ; 
00255     return 0 ;
00256 }
00257 
00258 /*---------------------------------------------------------------------------*/
00265 /*---------------------------------------------------------------------------*/
00266 static int 
00267 uves_utl_remove_crh_single( cpl_parameterlist   *   parlist, 
00268         cpl_frameset        *   framelist)
00269 {
00270     cpl_parameter       *   param= NULL ;
00271     cpl_frameset * raw_on=NULL;
00272     cpl_frameset * raw_off=NULL;
00273     double crh_frac_max=0;
00274     double sigma_lim=0;
00275     double f_lim=0;
00276     int max_iter=0;
00277     double gain=0;
00278     double ron=0;
00279     int nraw=0;
00280    
00281     cpl_image* ima_res=NULL;
00282     cpl_image* ima_on=NULL;
00283     cpl_image* ima_off=NULL;
00284 
00285 
00286     cpl_propertylist* plist1=NULL;
00287     cpl_propertylist* plist2=NULL;
00288     cpl_propertylist* pliste=NULL;
00289 
00290     const char* name_r="ima_res.fits";
00291 
00292     cpl_frame* product_frame=NULL;
00293     cpl_frame* frame_on=NULL;
00294     cpl_frame* frame_off=NULL;
00295     int next1=0;
00296     const char* name1=NULL;
00297 
00298     int next2=0;
00299     const char* name2=NULL;
00300 
00301     int noff=0;
00302     int chips=0;
00303     int nfrm=0;
00304     int i=0;
00305   
00306 
00307     uves_msg("Welcome to UVES Pipeline release %d.%d.%d",
00308         UVES_MAJOR_VERSION,UVES_MINOR_VERSION,UVES_MICRO_VERSION);
00309 
00310     /* HOW TO RETRIEVE INPUT PARAMETERS */
00311     check_nomsg(param=cpl_parameterlist_find(parlist, 
00312                                              "uves.uves_utl_remove_crh_single.crh_frac_max"));
00313     check_nomsg(crh_frac_max=cpl_parameter_get_double(param));
00314 
00315     check_nomsg(param=cpl_parameterlist_find(parlist,
00316                                              "uves.uves_utl_remove_crh_single.sigma_lim"));
00317     check_nomsg(sigma_lim = cpl_parameter_get_double(param)) ;
00318 
00319 
00320     check_nomsg(param=cpl_parameterlist_find(parlist,
00321                                              "uves.uves_utl_remove_crh_single.f_lim"));
00322     check_nomsg(f_lim = cpl_parameter_get_double(param)) ;
00323 
00324     check_nomsg(param=cpl_parameterlist_find(parlist,
00325                                              "uves.uves_utl_remove_crh_single.max_iter"));
00326     check_nomsg(max_iter = cpl_parameter_get_int(param)) ;
00327 
00328     check_nomsg(param=cpl_parameterlist_find(parlist,
00329                                              "uves.uves_utl_remove_crh_single.gain"));
00330     check_nomsg(gain = cpl_parameter_get_double(param)) ;
00331 
00332 
00333     check_nomsg(param=cpl_parameterlist_find(parlist,
00334                                              "uves.uves_utl_remove_crh_single.ron"));
00335     check_nomsg(ron = cpl_parameter_get_double(param)) ;
00336   
00337 
00338 
00339 
00340     /* Identify the RAW and CALIB frames in the input frameset */
00341     check(uves_dfs_set_groups(framelist),
00342          "Cannot identify RAW and CALIB frames") ;
00343     //cpl_frameset_dump(framelist,stdout);
00344 
00345     /* HOW TO ACCESS INPUT DATA */
00346     nfrm=cpl_frameset_get_size(framelist);
00347     if(nfrm<1) {
00348       uves_msg_error("Empty input frame list!");
00349       goto cleanup ;
00350     }
00351 
00352     /* HOW TO ACCESS INPUT DATA */
00353     check_nomsg(raw_on=cpl_frameset_new());
00354 
00355     check(uves_contains_frames_kind(framelist,raw_on,"RAW_IMA"),
00356      "Found no input frames with tag %s","RAW_IMA");
00357     check_nomsg(nraw=cpl_frameset_get_size(raw_on));
00358     if (nraw<1) {
00359       uves_msg_error("Found no input frames with tag %s","RAW_IMA");
00360       goto cleanup;
00361     }
00362 
00363     uves_msg("nraw=%d",nraw);
00364 
00365     check_nomsg(frame_on=cpl_frameset_get_first(raw_on));
00366     check_nomsg(next1=cpl_frame_get_nextensions(frame_on));
00367     check_nomsg(name1=cpl_frame_get_filename(frame_on));
00368     check_nomsg(plist1=cpl_propertylist_load(name1,0));
00369     uves_msg("CRH affected file name =%s",name1);
00370 
00371 
00372 
00373     if (nfrm>1) {
00374       /* if input has more than a frame, search for bias */
00375       check_nomsg(raw_off=cpl_frameset_new());
00376  
00377       chips=cpl_propertylist_get_int(plist1,"ESO DET CHIPS");
00378 
00379       /* deal with BLUE/RED arms and search for proper bias*/
00380       if(chips==2) {
00381     check(uves_contains_frames_kind(framelist,raw_off,"BIAS_RED"),
00382           "Found no input frames with tag %s","BIAS_RED");
00383       } else {
00384     check(uves_contains_frames_kind(framelist,raw_off,"BIAS_BLUE"),
00385           "Found no input frames with tag %s","BIAS_BLUE");
00386       }
00387  
00388       check_nomsg(noff=cpl_frameset_get_size(raw_off));
00389       if (noff<1) {
00390     uves_msg_error("Found no input bias frames");
00391       
00392       } else {
00393 
00394     frame_off=cpl_frameset_get_first(raw_off);
00395     next2=cpl_frame_get_nextensions(frame_off);
00396         /* check that raw frame and bias are coherent else exit */
00397         if(next2 != next1) {
00398       uves_msg_error("Raw frames with different number of extensions");
00399       uves_msg_error("Something wrong! Exit");
00400           goto cleanup;
00401     }
00402     name2=cpl_frame_get_filename(frame_off);
00403         uves_msg("Bias file name =%s",name2);
00404     check_nomsg(cpl_image_save(NULL, name_r,CPL_BPP_IEEE_FLOAT,
00405                    plist1,CPL_IO_DEFAULT));
00406 
00407         /* subtract bias on each extension */
00408     if(next1==0) {
00409           /*  
00410              subtract bias, 
00411              correct for cosmics, 
00412              save result 
00413           */
00414 
00415       check_nomsg(ima_on=cpl_image_load(name1,CPL_TYPE_FLOAT,0,0));
00416       check_nomsg(ima_off=cpl_image_load(name2,CPL_TYPE_FLOAT,0,0));
00417       check_nomsg(cpl_image_subtract(ima_on,ima_off));
00418 
00419 
00420       cpl_image_save(ima_on,"image_with_crh.fits",CPL_BPP_IEEE_FLOAT,
00421              NULL,CPL_IO_DEFAULT);
00422    
00423 
00424       check(ima_res=uves_remove_crh_single(ima_on,crh_frac_max,
00425                                                  sigma_lim,f_lim,
00426                          max_iter,gain,ron),
00427           "fail to remove CRHs");
00428 
00429       check_nomsg(cpl_image_add(ima_res,ima_off));
00430       
00431       check_nomsg(cpl_image_save(ima_res, name_r,CPL_BPP_IEEE_FLOAT,
00432                      plist1,CPL_IO_DEFAULT));
00433     } else {
00434       uves_msg("next=%d",next1);
00435           /* loop over extensions, 
00436              subtract bias, 
00437              correct for cosmics, 
00438              save result 
00439           */
00440 
00441 
00442 
00443       for(i=1;i<=next1;i++) {
00444             uves_msg("name1=%s",name1);
00445         uves_msg("name2=%s",name2);
00446         check_nomsg(ima_on=cpl_image_load(name1,CPL_TYPE_FLOAT,0,i));
00447 
00448 
00449         check_nomsg(pliste=cpl_propertylist_load(name1,i));
00450 
00451         if(next2==0) {
00452           check_nomsg(ima_off=cpl_image_load(name2,CPL_TYPE_FLOAT,0,0));
00453 
00454         } else {
00455           check_nomsg(ima_off=cpl_image_load(name2,CPL_TYPE_FLOAT,0,i));
00456 
00457         }
00458             uves_msg("ima_on=%p ima_off=%p",ima_on,ima_off);
00459         check_nomsg(cpl_image_subtract(ima_on,ima_off));
00460 
00461         cpl_image_save(ima_on,"image_with_crh.fits",CPL_BPP_IEEE_FLOAT,
00462                NULL,CPL_IO_DEFAULT);
00463    
00464             check(ima_res=uves_remove_crh_single(ima_on,crh_frac_max,
00465                                                  sigma_lim,f_lim,
00466                          max_iter,gain,ron),
00467           "fail to remove CRHs");
00468 
00469         check_nomsg(cpl_image_add(ima_res,ima_off));
00470 
00471  
00472         if(i>0) {
00473           check_nomsg(cpl_image_save(ima_res, name_r,CPL_BPP_IEEE_FLOAT,
00474                      pliste,CPL_IO_EXTEND));
00475         }
00476         uves_free_image(&ima_on);
00477         uves_free_image(&ima_off);
00478         uves_free_image(&ima_res);
00479         cpl_propertylist_delete(pliste); pliste=NULL; 
00480 
00481  
00482       }
00483 
00484 
00485     }
00486       }
00487 
00488       uves_free_frameset(&raw_off);
00489       uves_free_frameset(&raw_on);
00490 
00491 
00492     } else {
00493       uves_msg("Please, provide a bias frame. Exit.");
00494       goto cleanup;
00495     }
00496  
00497 
00498 
00499 
00500     /* HOW TO SAVE A PRODUCT ON DISK  */
00501     /* Set the file name */
00502  
00503     /* Create product frame */
00504     check_nomsg(product_frame = cpl_frame_new());
00505     check_nomsg(cpl_frame_set_filename(product_frame, name_r)) ;
00506     check_nomsg(cpl_frame_set_tag(product_frame, "PRODUCT")) ;
00507     check_nomsg(cpl_frame_set_type(product_frame, CPL_FRAME_TYPE_IMAGE)) ;
00508     check_nomsg(cpl_frame_set_group(product_frame, CPL_FRAME_GROUP_PRODUCT)) ;
00509     check(cpl_frame_set_level(product_frame, CPL_FRAME_LEVEL_FINAL),
00510       "Error while initialising the product frame") ;
00511     
00512     /* Add DataFlow keywords */
00513     check_nomsg(cpl_propertylist_erase_regexp(plist1, "^ESO PRO CATG",0));
00514     /*
00515     check(cpl_dfs_setup_product_header(plist, 
00516                                        product_frame, 
00517                                        framelist, 
00518                                        parlist,
00519                                        "uves_utl_remove_crh_single", 
00520                                        "UVES", 
00521                                        DICTIONARY),
00522       "Problem in the product DFS-compliance") ;
00523     */
00524     /* Save the file 
00525     check(cpl_image_save(ima_res, 
00526                          name_r, 
00527                          CPL_BPP_IEEE_FLOAT, 
00528                          plist1,
00529                          CPL_IO_DEFAULT),
00530       "Could not save product");
00531     */
00532     cpl_propertylist_delete(plist1) ; plist1=NULL;
00533 
00534     /* Log the saved file in the input frameset */
00535     check_nomsg(cpl_frameset_insert(framelist, product_frame)) ;
00536 
00537 
00538  cleanup:
00539      uves_free_frameset(&raw_off);
00540      uves_free_image(&ima_on);
00541      uves_free_image(&ima_off);
00542      uves_free_image(&ima_res);
00543 
00544      if(pliste!=NULL) cpl_propertylist_delete(pliste); pliste=NULL; 
00545 
00546      if (plist1!=NULL) cpl_propertylist_delete(plist1);plist1=NULL;
00547      if (plist2!=NULL) cpl_propertylist_delete(plist2);plist2=NULL;
00548 
00549 
00550     if (cpl_error_get_code()) {
00551         return -1 ;
00552     } else {
00553         return 0 ;
00554     }
00555 
00556 }

Generated on 9 Mar 2012 for UVES Pipeline Reference Manual by  doxygen 1.6.1