si_rec_mdark.c

00001 /* $Id: si_rec_mdark.c,v 1.20 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.20 $
00025  * $Name:  $
00026  */
00027 
00028  /****************************************************************
00029   *          Dark 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>  
00050 
00051 /* qfits */
00052 #include <qfits.h>    
00053 #include <xmemory.h>
00054 
00055 /* cpl */
00056 #include <cpl.h>    
00057 
00058 
00059 /* sinfoni */
00060 #include <bp_noise.h> 
00061 #include <dark.h> 
00062 #include <sinfoni_bp_config.h>
00063 #include <sinfoni_bp_noise_config.h>
00064 #include <sinfoni_dark_config.h> 
00065 #include <sinfoni_tpl_utils.h>
00066 #include <sinfoni_tpl_dfs.h>
00067 #include <sinfoni_tpl_pfits.h>
00068 #include <sinfoni_functions.h>
00069 #include <sinfoni_memory.h>
00070 
00071 /*-----------------------------------------------------------------------------
00072                             Functions prototypes
00073  -----------------------------------------------------------------------------*/
00074 
00075 cxint si_rec_mdark_create(cpl_plugin *plugin);
00076 cxint si_rec_mdark_create(cpl_plugin *plugin);
00077 cxint si_rec_mdark_exec(cpl_plugin *plugin);
00078 cxint si_rec_mdark_destroy(cpl_plugin *plugin);
00079 
00080 static cxint si_rec_mdark(cpl_parameterlist *config, cpl_frameset *set);
00081 static cxint si_rec_mdark(cpl_parameterlist *config, cpl_frameset *set);
00082 
00083 int cpl_plugin_get_info(cpl_pluginlist *list);
00084 int
00085 count_diff_ndit(cpl_frameset *set, cpl_vector** dit_val);
00086 double get_dit ( char * filename );
00087 
00088 /*-----------------------------------------------------------------------------
00089                             Static variables
00090  -----------------------------------------------------------------------------*/
00091 
00092 static char si_rec_mdark_description[] =
00093 "This recipe perform raw dark data reduction.\n"
00094 "The input files are raw dark images\n"
00095 "Their associated tags should be DARK.\n"
00096 "The output are a master dark (PRO.CATG=MASTER_DARK) and\n"
00097 "a hot pixels bad pixel map (PRO.CATG=BP_MAP_HP)\n"
00098 "Information on relevant parameters may be found with\n"
00099 "esorex --params si_rec_mdark\n"
00100 "esorex --help si_rec_mdark\n"
00101 "\n";
00102 
00103 
00104 /*-----------------------------------------------------------------------------
00105                                 Functions code
00106  -----------------------------------------------------------------------------*/
00107 
00108 
00109 /*
00110   *
00111   * Create the recipe instance, i.e. setup the parameter list for this
00112   * recipe and make it available to the application using the interface.
00113   *
00114   */
00115 
00116 cxint
00117 si_rec_mdark_create(cpl_plugin *plugin)
00118 {
00119 
00120   /*
00121    * We have to provide the option we accept to the application.
00122    * We need to setup our parameter list and hook it into the recipe
00123    * interface.
00124    */
00125   cpl_recipe *recipe = (cpl_recipe *)plugin;
00126   recipe->parameters = cpl_parameterlist_new();
00127   if(recipe->parameters == NULL) {
00128     return 1;
00129   }
00130 
00131   /*
00132    * Fill the parameter list.
00133    */
00134   sinfoni_bp_noise_config_add(recipe->parameters);
00135   sinfoni_dark_config_add(recipe->parameters);
00136   return 0;
00137 
00138 }
00139 
00140 cxint
00141 si_rec_mdark_exec(cpl_plugin *plugin)
00142 {
00143 
00144   cpl_recipe *recipe = (cpl_recipe *) plugin;
00145   if(recipe->parameters == NULL) {
00146     return 1;
00147   }
00148   if(recipe->frames == NULL) {
00149     return 1;
00150   }
00151 
00152   cpl_error_reset();
00153   return si_rec_mdark(recipe->parameters, recipe->frames);
00154 
00155 }
00156 
00157 cxint
00158 si_rec_mdark_destroy(cpl_plugin *plugin)
00159 {
00160 
00161   cpl_recipe *recipe = (cpl_recipe *) plugin;
00162   /*
00163    * We just destroy what was created during the plugin initializzation phase
00164    * i.e. the parameter list. The frame set is managed by the application which
00165    * called us, so that we must not touch it.
00166    */
00167 
00168   cpl_parameterlist_delete(recipe->parameters);
00169 
00170   return 0;
00171 
00172 }
00173 
00174 int
00175 cpl_plugin_get_info(cpl_pluginlist *list)
00176 {
00177 
00178   cpl_recipe *recipe = cpl_calloc(1, sizeof *recipe);
00179   cpl_plugin *plugin = &recipe->interface;
00180 
00181 
00182   cpl_plugin_init(plugin,
00183           CPL_PLUGIN_API,
00184           SINFONI_BINARY_VERSION,
00185           CPL_PLUGIN_TYPE_RECIPE,
00186           "si_rec_mdark",
00187           "Master dark and hot pixels mask generation.",
00188           si_rec_mdark_description,
00189           "Andrea Modigliani",
00190           "Andrea.Modigliani@eso.org",
00191           sinfoni_get_license(),
00192           si_rec_mdark_create,
00193           si_rec_mdark_exec,
00194           si_rec_mdark_destroy);
00195 
00196   cpl_pluginlist_append(list, plugin);
00197 
00198   return 0;
00199 
00200 }
00201 
00202 
00203 /*
00204  * The actual recipe actually start here.
00205  */
00206 
00207 static cxint
00208 si_rec_mdark(cpl_parameterlist *config, cpl_frameset *set)
00209 {
00210  
00211   const char *_id = "si_rec_mdark";
00212   int  ind =0;
00213   cpl_parameter* p=NULL;
00214   int nset=0;
00215   cpl_frameset  * wrk_set=NULL;
00216   cpl_frameset  * cdb_set=NULL;
00217   cpl_frameset  * tot_set=NULL;
00218 
00219   cpl_frame* tmp_frm=NULL;
00220   cpl_frame* dup_frm=NULL;
00221   cpl_frame* cdb_frm=NULL;
00222 
00223   char tmp_name[FILE_NAME_SZ];
00224   char out_bpmap_name[FILE_NAME_SZ];
00225   char out_dark_name[FILE_NAME_SZ];
00226   double tmp_dit=0;
00227   double ref_dit=0;
00228 
00229   cpl_vector* dit_val=NULL;
00230   int i=0;
00231   int j=0;
00232 
00233 
00234   int nraw=0;
00235   int ncdb=0;
00236   int nred=0;
00237   int ntot=0;
00238 
00239 
00240   if(sinfoni_dfs_set_groups(set)) {
00241     cpl_msg_error(_id, "Cannot indentify RAW and CALIB frames") ;
00242     return -1;
00243   }
00244 
00245 
00246   cdb_set=cpl_frameset_new();
00247   tot_set=cpl_frameset_new();
00248 
00249   sinfoni_extract_mst_frames(set,cdb_set);
00250 
00251   sinfoni_bp_config_add(config);
00252   nset=count_diff_ndit(set,&dit_val);
00253   nraw=cpl_frameset_get_size(set);
00254   ncdb=cpl_frameset_get_size(cdb_set);
00255 
00256 
00257   for(i=0;i<nset;i++) {
00258      wrk_set=cpl_frameset_new();
00259      ref_dit=cpl_vector_get(dit_val,i);
00260      for(j=0;j<nraw;j++) {
00261         tmp_frm=cpl_frameset_get_frame(set,j);
00262         strcpy(tmp_name,cpl_frame_get_filename(tmp_frm));
00263         tmp_dit=get_dit(tmp_name);
00264         if(tmp_dit==ref_dit) {
00265            dup_frm=cpl_frame_duplicate(tmp_frm);
00266            cpl_frameset_insert(wrk_set,dup_frm);
00267         }
00268      }
00269 
00270      for(j=0;j<ncdb;j++) {
00271        cdb_frm=cpl_frameset_get_frame(cdb_set,j);
00272        dup_frm=cpl_frame_duplicate(cdb_frm);
00273        cpl_frameset_insert(wrk_set,dup_frm);
00274      }
00275 
00276      /* reduce data */
00277      if(sinfoni_dfs_set_groups(wrk_set)) {
00278        cpl_msg_error(_id, "Cannot indentify RAW and CALIB frames") ;
00279        return -1;
00280      }
00281 
00282      /* Hidden parameters */
00283      p = cpl_parameterlist_find(config,"sinfoni.bp.method");
00284      cpl_parameter_set_string(p,"Noise");
00285      if(sinfoni_dfs_set_groups(set)) {
00286        cpl_msg_error(_id, "Cannot indentify RAW and CALIB frames") ;
00287        return -1;
00288      }
00289 
00290      if (nset>1) {
00291        sprintf(out_bpmap_name,"%s%d%s","out_bp_noise",i,".fits");
00292      } else {
00293        strcpy(out_bpmap_name,"out_bp_noise.fits");
00294      }
00295 
00296      cpl_msg_info(_id,"-----------------------------------------------");
00297      cpl_msg_info(_id,"BP_MAP_HP BAD PIXEL MAP DETERMINATION          ");
00298      cpl_msg_info(_id,"-----------------------------------------------");
00299      {
00300 
00301    
00302     if ( -1 == (ind = badSearchNoise(_id,config,wrk_set,out_bpmap_name ) ) )
00303       {
00304     cpl_msg_error(_id,"badSearchNoise(), no: %d\n", ind) ;
00305         cpl_frameset_delete(tot_set);
00306         cpl_vector_delete(dit_val);
00307         cpl_frameset_delete(cdb_set);
00308         cpl_frameset_delete(wrk_set);
00309 
00310     return -1 ;
00311       }
00312       cpl_msg_info (_id,"BP_MAP_HP BAD PIXEL MAP DETERMINATION SUCCESS") ; 
00313    
00314      }
00315 
00316   cpl_msg_info(_id,"-----------------------------------------------");
00317   cpl_msg_info(_id,"MASTER DARK DETERMINATION                      ");
00318   cpl_msg_info(_id,"-----------------------------------------------");
00319 
00320 
00321    if (nset>1) {
00322       sprintf(out_dark_name,"%s%d%s","out_dark",i,".fits");
00323    } else {
00324       strcpy(out_dark_name,"out_dark.fits");
00325    }
00326 
00327   {
00328     
00329     if ( -1 == (ind = dark(_id,config, wrk_set, out_dark_name ) ) )
00330       {
00331     cpl_msg_error(_id,"_rec_mdark(), no: %d\n", ind) ;
00332         cpl_frameset_delete(tot_set);
00333         cpl_vector_delete(dit_val);
00334         cpl_frameset_delete(cdb_set);
00335         cpl_frameset_delete(wrk_set);
00336     return -1 ;
00337       }
00338       cpl_msg_info (_id,"MASTER DARK DETERMINATION SUCCESS") ;
00339     
00340   } 
00341      nred=cpl_frameset_get_size(wrk_set);
00342      for(j=0;j<nred;j++) {
00343         tmp_frm=cpl_frameset_get_frame(wrk_set,j);
00344         dup_frm = cpl_frame_duplicate(tmp_frm);
00345         cpl_frameset_insert(tot_set,dup_frm);
00346      }
00347      cpl_frameset_delete(wrk_set);
00348 
00349 
00350 
00351   }
00352 
00353   ntot=cpl_frameset_get_size(tot_set);
00354   for(j=0;j<ntot;j++) {
00355       tmp_frm=cpl_frameset_get_frame(tot_set,j);
00356       dup_frm = cpl_frame_duplicate(tmp_frm);
00357       cpl_frameset_insert(set,dup_frm);
00358   }
00359   cpl_frameset_delete(tot_set);
00360   cpl_vector_delete(dit_val);
00361   cpl_frameset_delete(cdb_set);
00362   sinfoni_memory_status();
00363   return 0 ;
00364 
00365 }
00366 
00367 int
00368 count_diff_ndit(cpl_frameset *set, cpl_vector** dit_val)
00369 {
00370   cpl_frame* tmp_frm=NULL;
00371   cpl_frame* dup_frm=NULL;
00372 
00373 
00374   char  tmp_name[FILE_NAME_SZ];
00375   int nraw=0;
00376   int i=0;
00377   int ndit=1;
00378   double ref_dit=0;
00379   cpl_vector* dit=NULL; 
00380 
00381   nraw=cpl_frameset_get_size(set);
00382   dit=cpl_vector_new(nraw);
00383   for(i=0;i<nraw;i++) {
00384      tmp_frm = cpl_frameset_get_frame(set, i);
00385      strcpy(tmp_name,cpl_frame_get_filename(tmp_frm)); 
00386      cpl_vector_set(dit,i,get_dit(tmp_name));
00387   }
00388   /*
00389   for(i=0;i<nraw;i++) {
00390     cpl_msg_info(fctid,"dit=%g",cpl_vector_get(dit,i));
00391   }
00392   */
00393   cpl_vector_sort(dit,1);
00394   ref_dit=cpl_vector_get(dit,0);
00395   for(i=1;i<nraw;i++) {
00396     if(ref_dit != cpl_vector_get(dit,i)) {
00397       ref_dit=cpl_vector_get(dit,i);
00398       ndit++;
00399     }
00400   }
00401   *dit_val=cpl_vector_new(ndit);
00402   ref_dit=cpl_vector_get(dit,0);
00403   cpl_vector_set(*dit_val,0,cpl_vector_get(dit,0));
00404   ndit=1;
00405   for(i=1;i<nraw;i++) {
00406     if(ref_dit != cpl_vector_get(dit,i)) {
00407       cpl_vector_set(*dit_val,ndit,cpl_vector_get(dit,i));
00408       ref_dit=cpl_vector_get(dit,i);
00409       ndit++;
00410     }
00411   }
00412 
00413 
00414   /* cpl_msg_info(fctid,"ndit=%d",ndit); */
00415   cpl_vector_delete(dit);
00416   cpl_frame_delete(dup_frm);
00417 
00418   return ndit;
00419 }
00420 
00421 /*----------------------------------------------------------------------------
00422    Function     :       spiffi_get_dit()
00423    In           :       fits file name
00424    Out          :       total integration time in sec
00425    Job          :       reads the DIT keyword from the FITS-header 
00426  ---------------------------------------------------------------------------*/
00427 
00428 double get_dit ( char * filename )
00429 {
00430   const char* _id="get_dit";
00431   char * val;
00432   double dit=0. ;
00433 
00434   if ( NULL != (val = qfits_query_hdr(filename, "DET.DIT")) ) {
00435     dit = atof(val);
00436   } else {
00437     cpl_msg_error(_id,"Cannot read keyword DET.DIT");
00438     return -1 ;
00439   }
00440   return dit;
00441 
00442 }

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