sinfo_rec_mdark.c

00001 /* $Id: sinfo_rec_mdark.c,v 1.22 2008/02/28 10:36:10 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: 2008/02/28 10:36:10 $
00024  * $Revision: 1.22 $
00025  * $Name: sinfo-2_2_5 $
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 /* cpl */
00046 #include <cpl.h>    
00047 
00048 /* irplib */
00049 #include <irplib_utils.h>
00050 
00051 /* sinfoni */
00052 #include <sinfo_bp_noise.h> /* */
00053 #include <sinfo_new_dark.h>     /*  */ 
00054 #include <sinfo_utilities.h>
00055 #include <sinfo_general_config.h>
00056 #include <sinfo_bp_config.h>
00057 #include <sinfo_bp_noise_config.h>
00058 #include <sinfo_dark_config.h> 
00059 #include <sinfo_tpl_utils.h>
00060 #include <sinfo_tpl_dfs.h>
00061 #include <sinfo_pfits.h>
00062 #include <sinfo_functions.h>
00063 #include <sinfo_pfits.h>
00064 #include <sinfo_msg.h>
00065 #include <sinfo_error.h>
00066 #include <sinfo_utils_wrappers.h>
00067 /*-----------------------------------------------------------------------------
00068                             Functions prototypes
00069  ----------------------------------------------------------------------------*/
00070 
00071 static int sinfo_rec_mdark_create(cpl_plugin *plugin);
00072 static int sinfo_rec_mdark_exec(cpl_plugin *plugin);
00073 static int sinfo_rec_mdark_destroy(cpl_plugin *plugin);
00074 static int sinfo_rec_mdark(cpl_parameterlist *config, cpl_frameset *set);
00075 static int count_diff_ndit(cpl_frameset *set, cpl_vector** dit_val);
00076 
00077 /*-----------------------------------------------------------------------------
00078                             Static variables
00079  ----------------------------------------------------------------------------*/
00080 
00081 static char sinfo_rec_mdark_description[] =
00082 "This recipe perform raw sinfo_dark data reduction.\n"
00083 "The input files are raw sinfo_dark images\n"
00084 "Their associated tags should be DARK.\n"
00085 "The output are a master sinfo_dark (PRO.CATG=MASTER_DARK) and\n"
00086 "a hot pixels bad pixel map (PRO.CATG=BP_MAP_HP)\n"
00087 "Information on relevant parameters may be found with\n"
00088 "esorex --params sinfo_rec_mdark\n"
00089 "esorex --help sinfo_rec_mdark\n"
00090 "\n";
00091 
00092 
00093 /*-----------------------------------------------------------------------------
00094                                 Functions code
00095  ----------------------------------------------------------------------------*/
00096 /*---------------------------------------------------------------------------*/
00100 /*---------------------------------------------------------------------------*/
00102 /*---------------------------------------------------------------------------*/
00111 /*---------------------------------------------------------------------------*/
00112 int
00113 cpl_plugin_get_info(cpl_pluginlist *list)
00114 {
00115 
00116   cpl_recipe *recipe = cpl_calloc(1, sizeof *recipe);
00117   cpl_plugin *plugin = &recipe->interface;
00118 
00119 
00120   cpl_plugin_init(plugin,
00121           CPL_PLUGIN_API,
00122           SINFONI_BINARY_VERSION,
00123           CPL_PLUGIN_TYPE_RECIPE,
00124           "sinfo_rec_mdark",
00125           "Master dark and hot pixels mask generation.",
00126           sinfo_rec_mdark_description,
00127           "Andrea Modigliani",
00128           "Andrea.Modigliani@eso.org",
00129           sinfo_get_license(),
00130           sinfo_rec_mdark_create,
00131           sinfo_rec_mdark_exec,
00132           sinfo_rec_mdark_destroy);
00133 
00134   cpl_pluginlist_append(list, plugin);
00135 
00136   return 0;
00137 
00138 }
00139 
00140 /*---------------------------------------------------------------------------*/
00148 /*---------------------------------------------------------------------------*/
00149 static int sinfo_rec_mdark_create(cpl_plugin *plugin)
00150 {
00151     cpl_recipe      * recipe ;
00152 
00153     /* Check that the plugin is part of a valid recipe */
00154     if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE) 
00155         recipe = (cpl_recipe *)plugin ;
00156     else return -1 ;
00157     cpl_error_reset();
00158     irplib_reset();
00159 
00160     /* Create the parameters list in the cpl_recipe object */
00161     recipe->parameters = cpl_parameterlist_new() ; 
00162 
00163 
00164   /*
00165    * Fill the parameter list.
00166    */
00167   sinfo_general_config_add(recipe->parameters);
00168   sinfo_bp_noise_config_add(recipe->parameters);
00169   sinfo_dark_config_add(recipe->parameters);
00170   return 0;
00171 
00172 }
00173 /*---------------------------------------------------------------------------*/
00179 /*---------------------------------------------------------------------------*/
00180 static int sinfo_rec_mdark_exec(cpl_plugin *plugin)
00181 {
00182     cpl_recipe  *   recipe ;
00183 
00184     cpl_errorstate initial_errorstate = cpl_errorstate_get();
00185 
00186     /* Get the recipe out of the plugin */
00187     if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE) 
00188         recipe = (cpl_recipe *)plugin ;
00189     else return -1 ;
00190 
00191     if (!cpl_errorstate_is_equal(initial_errorstate)) {                      
00192         /* Dump the error history since recipe execution start.                
00193            At this point the recipe cannot recover from the error */           
00194         cpl_errorstate_dump(initial_errorstate, CPL_FALSE, NULL);              
00195     } 
00196 
00197     return sinfo_rec_mdark(recipe->parameters, recipe->frames);
00198 
00199 }
00200 /*---------------------------------------------------------------------------*/
00206 /*---------------------------------------------------------------------------*/
00207 static int sinfo_rec_mdark_destroy(cpl_plugin *plugin)
00208 {
00209     cpl_recipe  *   recipe ;
00210     
00211     /* Get the recipe out of the plugin */
00212     if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE) 
00213         recipe = (cpl_recipe *)plugin ;
00214     else return -1 ;
00215 
00216     cpl_parameterlist_delete(recipe->parameters) ; 
00217     return 0 ;
00218 
00219 }
00220 
00221 
00222 /*---------------------------------------------------------------------------*/
00229 /*---------------------------------------------------------------------------*/
00230 /*
00231  * The actual recipe actually start here.
00232  */
00233 
00234 static int
00235 sinfo_rec_mdark(cpl_parameterlist *config, cpl_frameset *set)
00236 {
00237  
00238   cpl_parameter* p=NULL;
00239   int nset=0;
00240   cpl_frameset  * wrk_set=NULL;
00241   cpl_frameset  * cdb_set=NULL;
00242   cpl_frameset  * tot_set=NULL;
00243 
00244   cpl_frame* tmp_frm=NULL;
00245   cpl_frame* dup_frm=NULL;
00246   cpl_frame* cdb_frm=NULL;
00247 
00248   char tmp_name[FILE_NAME_SZ];
00249   char out_bpmap_name[FILE_NAME_SZ];
00250   char out_dark_name[FILE_NAME_SZ];
00251   double tmp_dit=0;
00252   double ref_dit=0;
00253 
00254   cpl_vector* dit_val=NULL;
00255   cpl_propertylist* plist=NULL;
00256 
00257   int i=0;
00258   int j=0;
00259   int line_cor=0;
00260 
00261 
00262   int nraw=0;
00263   int ncdb=0;
00264   int nred=0;
00265   int ntot=0;
00266   sinfo_msg("Welcome to SINFONI Pipeline release %d.%d.%d",
00267         SINFONI_MAJOR_VERSION,SINFONI_MINOR_VERSION,SINFONI_MICRO_VERSION);
00268   ck0(sinfo_dfs_set_groups(set),"Cannot indentify RAW and CALIB frames");
00269 
00270   check_nomsg(p=cpl_parameterlist_find(config, "sinfoni.general.lc_sw"));
00271   check_nomsg(line_cor=cpl_parameter_get_bool(p));
00272   if(line_cor==1) {
00273      check_nomsg(sinfo_ima_line_cor(config,set));
00274   }
00275 
00276 
00277   check_nomsg(cdb_set=cpl_frameset_new());
00278   check_nomsg(tot_set=cpl_frameset_new());
00279 
00280 
00281   sinfo_extract_mst_frames(set,cdb_set);
00282   sinfo_bp_config_add(config);
00283   nset=count_diff_ndit(set,&dit_val);
00284 
00285   check_nomsg(nraw=cpl_frameset_get_size(set));
00286   check_nomsg(ncdb=cpl_frameset_get_size(cdb_set));
00287 
00288   for(i=0;i<nset;i++) {
00289     check_nomsg(wrk_set=cpl_frameset_new());
00290     check_nomsg(ref_dit=cpl_vector_get(dit_val,i));
00291 
00292 
00293     for(j=0;j<nraw;j++) {
00294       check_nomsg(tmp_frm=cpl_frameset_get_frame(set,j));
00295       check_nomsg(strcpy(tmp_name,cpl_frame_get_filename(tmp_frm)));
00296       plist=cpl_propertylist_load(tmp_name,0);
00297       tmp_dit=sinfo_pfits_get_dit(plist);
00298       sinfo_free_propertylist(&plist);
00299 
00300       if(tmp_dit==ref_dit) {
00301         check_nomsg(cpl_frameset_insert(wrk_set,cpl_frame_duplicate(tmp_frm)));
00302       }
00303     }
00304 
00305     for(j=0;j<ncdb;j++) {
00306       check_nomsg(cdb_frm=cpl_frameset_get_frame(cdb_set,j));
00307       check_nomsg(cpl_frameset_insert(wrk_set,cpl_frame_duplicate(cdb_frm)));
00308     }
00309 
00310     /* reduce data */
00311     ck0(sinfo_dfs_set_groups(wrk_set),"Cannot indentify RAW and CALIB frames");
00312 
00313     /* Hidden parameters */
00314     check_nomsg(p = cpl_parameterlist_find(config,"sinfoni.bp.method"));
00315     check_nomsg(cpl_parameter_set_string(p,"Noise"));
00316     ck0(sinfo_dfs_set_groups(set),"Cannot indentify RAW and CALIB frames") ;
00317  
00318     if (nset>1) {
00319       sprintf(out_bpmap_name,"%s%d%s","out_bp_noise",i,".fits");
00320     } else {
00321       strcpy(out_bpmap_name,"out_bp_noise.fits");
00322     }
00323 
00324     sinfo_msg("-----------------------------------------------");
00325     sinfo_msg("BP_MAP_HP BAD PIXEL MAP DETERMINATION          ");
00326     sinfo_msg("-----------------------------------------------");
00327    
00328     ck0(sinfo_new_bp_search_noise(cpl_func,config,wrk_set,out_bpmap_name),
00329     "computing BP_MAP_HP") ;
00330     sinfo_msg("BP_MAP_HP BAD PIXEL MAP DETERMINATION SUCCESS") ; 
00331     
00332     sinfo_msg("-----------------------------------------------");
00333     sinfo_msg("MASTER DARK DETERMINATION                      ");
00334     sinfo_msg("-----------------------------------------------");
00335 
00336     if (nset>1) {
00337       sprintf(out_dark_name,"%s%d%s","out_dark",i,".fits");
00338     } else {
00339       strcpy(out_dark_name,"out_dark.fits");
00340     }
00341 
00342     ck0(sinfo_new_dark(cpl_func,config, wrk_set, out_dark_name),
00343      "Computing master dark") ;
00344     sinfo_msg("MASTER DARK DETERMINATION SUCCESS") ;
00345 
00346     nred=cpl_frameset_get_size(wrk_set);
00347     for(j=0;j<nred;j++) {
00348       check_nomsg(tmp_frm=cpl_frameset_get_frame(wrk_set,j));
00349       check_nomsg(cpl_frameset_insert(tot_set,cpl_frame_duplicate(tmp_frm)));
00350     }
00351     sinfo_free_frameset(&wrk_set);
00352 
00353   }
00354 
00355   check_nomsg(ntot=cpl_frameset_get_size(tot_set));
00356   for(j=0;j<ntot;j++) {
00357     check_nomsg(tmp_frm=cpl_frameset_get_frame(tot_set,j));
00358     check_nomsg(cpl_frameset_insert(set,cpl_frame_duplicate(tmp_frm)));
00359   }
00360 
00361   sinfo_free_frameset(&tot_set);
00362   sinfo_free_my_vector(&dit_val);
00363   sinfo_free_frameset(&cdb_set);
00364 
00365 
00366 
00367 
00368 
00369 
00370  cleanup:
00371 
00372   sinfo_free_propertylist(&plist);
00373   sinfo_free_frame(&dup_frm);
00374   sinfo_free_frameset(&wrk_set);
00375   sinfo_free_frameset(&tot_set);
00376   sinfo_free_my_vector(&dit_val);
00377   sinfo_free_frameset(&cdb_set);
00378 
00379 
00380   if (cpl_error_get_code() != CPL_ERROR_NONE) {
00381     return -1;
00382   } else {
00383     return 0;
00384   }
00385 
00386 
00387 
00388 }
00389 
00390 static int count_diff_ndit(cpl_frameset *set, cpl_vector** dit_val)
00391 {
00392   cpl_frame* tmp_frm=NULL;
00393   cpl_frame* dup_frm=NULL;
00394 
00395 
00396   char  tmp_name[FILE_NAME_SZ];
00397   int nraw=0;
00398   int i=0;
00399   int ndit=1;
00400   double ref_dit=0;
00401   cpl_vector* dit=NULL; 
00402   cpl_propertylist* plist=NULL;
00403 
00404   nraw=cpl_frameset_get_size(set);
00405   dit=cpl_vector_new(nraw);
00406   for(i=0;i<nraw;i++) {
00407      tmp_frm = cpl_frameset_get_frame(set, i);
00408      strcpy(tmp_name,cpl_frame_get_filename(tmp_frm));
00409      plist= cpl_propertylist_load(tmp_name,0);
00410      cpl_vector_set(dit,i,sinfo_pfits_get_dit(plist));
00411      sinfo_free_propertylist(&plist);
00412   }
00413 
00414 
00415   cpl_vector_sort(dit,1);
00416   ref_dit=cpl_vector_get(dit,0);
00417 
00418 
00419   for(i=1;i<nraw;i++) {
00420     if(ref_dit != cpl_vector_get(dit,i)) {
00421       ref_dit=cpl_vector_get(dit,i);
00422       ndit++;
00423     }
00424   }
00425   *dit_val=cpl_vector_new(ndit);
00426   ref_dit=cpl_vector_get(dit,0);
00427   cpl_vector_set(*dit_val,0,cpl_vector_get(dit,0));
00428   ndit=1;
00429 
00430    for(i=1;i<nraw;i++) {
00431     if(ref_dit != cpl_vector_get(dit,i)) {
00432       cpl_vector_set(*dit_val,ndit,cpl_vector_get(dit,i));
00433       ref_dit=cpl_vector_get(dit,i);
00434       ndit++;
00435     }
00436   }
00437 
00438   cpl_vector_delete(dit);
00439   cpl_frame_delete(dup_frm);
00440 
00441   return ndit;
00442 }
00443 

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