uves_utl_rcosmic.c

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

Generated on 8 Mar 2011 for UVES Pipeline Reference Manual by  doxygen 1.6.1