sinfo_rec_wavecal.c

00001 /* $Id: sinfo_rec_wavecal.c,v 1.30 2009/09/02 12:00:56 kmirny 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: kmirny $
00023  * $Date: 2009/09/02 12:00:56 $
00024  * $Revision: 1.30 $
00025  * $Name: sinfo-2_2_5 $
00026  */
00027 
00028  /****************************************************************
00029   *          Wave_Cal Frames Data Reduction                          *
00030   ****************************************************************/
00031 
00032 /*-----------------------------------------------------------------------------
00033                                 Includes
00034  -----------------------------------------------------------------------------*/
00035 
00036 #ifdef HAVE_CONFIG_H
00037 #include <config.h>          /* allows the program compilation */
00038 #endif
00039 
00040 /* std */
00041 #include <strings.h>
00042 #include <string.h>
00043 #include <stdio.h>
00044 
00045 
00046 /* cpl */
00047 #include <cpl.h>
00048 
00049 /* irplib */
00050 #include <irplib_utils.h>
00051 
00052 /* sinfoni */
00053 #include <sinfo_pro_types.h>
00054 #include <sinfo_general_config.h>
00055 #include <sinfo_utilities.h>
00056 #include <sinfo_product_config.h>
00057 #include <sinfo_prepare_stacked_frames_config.h>
00058 #include <sinfo_wavecal_config.h>
00059 #include <sinfo_raw_types.h>
00060 #include <sinfo_tpl_utils.h>
00061 #include <sinfo_tpl_dfs.h>
00062 #include <sinfo_hidden.h>
00063 #include <sinfo_globals.h>
00064 #include <sinfo_functions.h>
00065 #include <sinfo_msg.h>
00066 #include <sinfo_new_prepare_stacked_frames.h>
00067 #include <sinfo_new_wave_cal_slit2.h>
00068 #include <sinfo_error.h>
00069 #include <sinfo_utils_wrappers.h>
00070 
00071 #include <sinfo_key_names.h>
00072 
00073 /*-----------------------------------------------------------------------------
00074                             Functions prototypes
00075  -----------------------------------------------------------------------------*/
00076 static int sinfo_rec_wavecal_create(cpl_plugin *);
00077 static int sinfo_rec_wavecal_exec(cpl_plugin *);
00078 static int sinfo_rec_wavecal_destroy(cpl_plugin *);
00079 static int sinfo_rec_wavecal(cpl_parameterlist *, cpl_frameset *);
00080 
00081 /*-----------------------------------------------------------------------------
00082                             Static variables
00083  -----------------------------------------------------------------------------*/
00084 
00085 static char sinfo_rec_wavecal_description1[] =
00086 "This recipe performs wavelength calibration.\n"
00087 "The input files are on/off arc lamp frames with tag WAVE_LAMP\n"
00088 "Master calibration frame input is:\n"
00089 "A corresponding (band) reference line table with tag REF_LINE_ARC\n"
00090 "A corresponding (band) distortion table with tag DISTORTION\n"
00091 "A corresponding (band,preoptic) master flat with tag MASTER_FLAT_LAMP\n"
00092 "A corresponding (band,preoptics) master bad "
00093 "pixel map with tag MASTER_BP_MAP\n";
00094 
00095 
00096 static char sinfo_rec_wavecal_description2[] =
00097 "If wcal-estimate_ind=TRUE, a corresponding (band,preoptics) slitlet position\n"
00098 "guess table SLIT_POS_GUESS (for example a copy of the SLIT_POS product)\n"
00099 "If wcal-calib_indicator=FALSE, a corresponding (band,preoptics) parabolic \n"
00100 "fit coefficients table WAVE_COEF_SLIT\n"
00101 "If sinfoni.wavecal.slitpos_boostrap_switch=FALSE \n"
00102 "(wcal-calib-slitpos_bootstrap=0),\n"
00103 "a corresponding (band,preoptics) slitlets position table with tag SLIT_POS\n";
00104 
00105 
00106 
00107 static char sinfo_rec_wavecal_description3[] =
00108 "The main products are:\n"
00109 "The master flat field corrected for distortions \n"
00110 "(PRO.CATG=MFLAT_STACKED_DIST)\n"
00111 "The arc lamp frames stacked (PRO.CATG=WAVE_LAMP_STACKED)\n"
00112 "The wavelength map (PRO.CATG=WAVE_MAP)\n"
00113 "The slitlet position table (PRO.CATG=SLIT_POS)\n"
00114 "A parabolic fit coefficients table (PRO.CATG=WAVE_COEF_SLIT)\n"
00115 "Parameters relative to arc lamp line fit: (PRO.CATG=WAVE_FIT_PARAMS)\n"
00116 "Information on relevant parameters can be found with\n"
00117 "esorex --params sinfo_rec_wavecal\n"
00118 "esorex --help sinfo_rec_wavecal\n"
00119 "\n";
00120 
00121 
00122 static char sinfo_rec_wavecal_description[1500];
00123 
00124 /*-----------------------------------------------------------------------------
00125                                 Functions code
00126  ----------------------------------------------------------------------------*/
00127 /*---------------------------------------------------------------------------*/
00131 /*---------------------------------------------------------------------------*/
00132 
00134 /*---------------------------------------------------------------------------*/
00143 /*---------------------------------------------------------------------------*/
00144 
00145 int
00146 cpl_plugin_get_info(cpl_pluginlist *list)
00147 {
00148 
00149   cpl_recipe *recipe = cpl_calloc(1, sizeof *recipe);
00150   cpl_plugin *plugin = &recipe->interface;
00151 
00152 
00153   strcpy(sinfo_rec_wavecal_description,sinfo_rec_wavecal_description1);
00154   strcat(sinfo_rec_wavecal_description,sinfo_rec_wavecal_description2);
00155   strcat(sinfo_rec_wavecal_description,sinfo_rec_wavecal_description3);
00156 
00157   cpl_plugin_init(plugin,
00158           CPL_PLUGIN_API,
00159           SINFONI_BINARY_VERSION,
00160           CPL_PLUGIN_TYPE_RECIPE,
00161           "sinfo_rec_wavecal",
00162           "Wavelength calibration and slitpos determination",
00163           sinfo_rec_wavecal_description,
00164           "Andrea Modigliani",
00165           "Andrea.Modigliani@eso.org",
00166           sinfo_get_license(),
00167           sinfo_rec_wavecal_create,
00168           sinfo_rec_wavecal_exec,
00169           sinfo_rec_wavecal_destroy);
00170 
00171   cpl_pluginlist_append(list, plugin);
00172 
00173   return 0;
00174 
00175 }
00176 
00177 /*---------------------------------------------------------------------------*/
00185 /*---------------------------------------------------------------------------*/
00186 
00187 static int sinfo_rec_wavecal_create(cpl_plugin *plugin)
00188 {
00189    cpl_recipe      * recipe ;
00190 
00191     /* Check that the plugin is part of a valid recipe */
00192     if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
00193         recipe = (cpl_recipe *)plugin ;
00194     else return -1 ;
00195     cpl_error_reset();
00196     irplib_reset();
00197 
00198     /* Create the parameters list in the cpl_recipe object */
00199     recipe->parameters = cpl_parameterlist_new() ;
00200 
00201 
00202   /*
00203    * Fill the parameter list.
00204    */
00205 
00206   /* Wavelength calibration  */
00207   sinfo_general_config_add(recipe->parameters);
00208   sinfo_product_config_add(recipe->parameters);
00209   sinfo_prepare_stacked_frames_config_add(recipe->parameters);
00210   sinfo_wavecal_config_add(recipe->parameters);
00211   return 0;
00212 
00213 }
00214 
00215 /*---------------------------------------------------------------------------*/
00221 /*---------------------------------------------------------------------------*/
00222 
00223 
00224 static int sinfo_rec_wavecal_exec(cpl_plugin *plugin)
00225 {
00226 
00227   cpl_recipe *recipe = (cpl_recipe *) plugin;
00228 
00229   /* Get the recipe out of the plugin */
00230   if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
00231       recipe = (cpl_recipe *)plugin ;
00232   else return -1 ;
00233 
00234   return sinfo_rec_wavecal(recipe->parameters, recipe->frames);
00235 
00236 }
00237 
00238 /*---------------------------------------------------------------------------*/
00244 /*---------------------------------------------------------------------------*/
00245 static int sinfo_rec_wavecal_destroy(cpl_plugin *plugin)
00246 {
00247   cpl_recipe  *   recipe ;
00248     /* Get the recipe out of the plugin */
00249     if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
00250         recipe = (cpl_recipe *)plugin ;
00251     else return -1 ;
00252 
00253   cpl_parameterlist_delete(recipe->parameters);
00254 
00255   return 0;
00256 
00257 }
00258 
00259 /*---------------------------------------------------------------------------*/
00266 /*---------------------------------------------------------------------------*/
00267 /*
00268  * The actual recipe actually start here.
00269  */
00270 
00271 static int sinfo_rec_wavecal(cpl_parameterlist *config, cpl_frameset *set)
00272 {
00273 
00274   int sw=0;
00275   fake* fk=NULL;
00276   cpl_parameter* p=NULL;
00277   cpl_frameset* wrk_set=NULL;
00278   cpl_frame* frame=NULL;
00279   int ind_index=0;
00280   cpl_frameset* ref_set=NULL;
00281   int pdensity=0;
00282   int line_cor=0;
00283 
00284   fk=sinfo_fake_new();
00285 
00286    sinfo_msg("Welcome to SINFONI Pipeline release %d.%d.%d",
00287         SINFONI_MAJOR_VERSION,SINFONI_MINOR_VERSION,SINFONI_MICRO_VERSION);
00288 
00289 
00290 
00291   ck0(sinfo_dfs_set_groups(set),"Cannot indentify RAW and CALIB frames") ;
00292   check_nomsg(p=cpl_parameterlist_find(config, "sinfoni.general.lc_sw"));
00293   check_nomsg(line_cor=cpl_parameter_get_bool(p));
00294   if(line_cor==1) {
00295      check_nomsg(sinfo_ima_line_cor(config,set));
00296   }
00297 
00298 
00299   check_nomsg(ref_set=cpl_frameset_duplicate(set));
00300 
00301 
00302   check_nomsg(p=cpl_parameterlist_find(config,"sinfoni.product.density"));
00303   check_nomsg(pdensity=cpl_parameter_get_int(p));
00304 
00305   /* hidden parameters */
00306   check_nomsg(p=cpl_parameterlist_find(config,
00307                                        "sinfoni.wavecal.slitpos_boostrap"));
00308   check_nomsg(sw=cpl_parameter_get_bool(p));
00309   if( (sw == 1) ) {
00310     check_nomsg(wrk_set=cpl_frameset_duplicate(set));
00311 
00312     check_nomsg(p=cpl_parameterlist_find(config,"sinfoni.stacked.ind_index"));
00313     check_nomsg(ind_index=cpl_parameter_get_bool(p));
00314     check_nomsg(cpl_parameter_set_bool(p,TRUE));
00315 
00316     sinfo_msg("---------------------------------------");
00317     sinfo_msg("%s FRAME DETERMINATION", PRO_WAVE_LAMP_STACKED);
00318     sinfo_msg("---------------------------------------");
00319     ck0(sinfo_new_prepare_stacked_frames(cpl_func,config, wrk_set, NULL,
00320                      PRO_WAVE_LAMP_STACKED,0,fk),
00321        "FAILED STACKING FRAME %s",PRO_WAVE_LAMP_STACKED);
00322 
00323     sinfo_msg("%s FRAME DETERMINATION SUCCESS", PRO_WAVE_LAMP_STACKED);
00324 
00325     ck0(sinfo_new_wave_cal_slit2(cpl_func,config, wrk_set,ref_set),
00326     "FIRST PART OF WAVELENGTH CALIBRATION FAILED") ;
00327     sinfo_msg("FIRST PART OF WAVELENGTH CALIBRATION") ;
00328 
00329     check_nomsg(frame = cpl_frameset_find(wrk_set,PRO_SLIT_POS_GUESS));
00330     check_nomsg(cpl_frameset_insert(set,cpl_frame_duplicate(frame)));
00331     check_nomsg(p=cpl_parameterlist_find(config,"sinfoni.stacked.ind_index"));
00332     if(ind_index == 1) {
00333        check_nomsg(cpl_parameter_set_bool(p,TRUE));
00334        check_nomsg(p=cpl_parameterlist_find(config,
00335                                            "sinfoni.wavecal.slitpos_boostrap"));
00336        check_nomsg(cpl_parameter_set_bool(p,TRUE));
00337        sinfo_msg("Set ind_index to TRUE");
00338     } else {
00339        check_nomsg(cpl_parameter_set_bool(p,FALSE));
00340        check_nomsg(p=cpl_parameterlist_find(config,
00341                                           "sinfoni.wavecal.slitpos_boostrap"));
00342        check_nomsg(cpl_parameter_set_bool(p,FALSE));
00343        sinfo_msg("Set ind_index to FALSE");
00344     }
00345     sinfo_free_frameset(&wrk_set);
00346   }
00347 
00348 
00349   sinfo_msg("---------------------------------------------");
00350   sinfo_msg("%s FRAME DETERMINATION", PRO_WAVE_LAMP_STACKED);
00351   sinfo_msg("---------------------------------------------");
00352 
00353   /*
00354    *  Modified code begins here...
00355    */
00356   {
00357       int i, nsky, ndark, nwave, non=0;
00358       cpl_frame * fr;
00359       const char * tag, * fname;
00360       char name[32];
00361       cpl_image * im;
00362       cpl_propertylist * plist;
00363 
00364       nsky = cpl_frameset_count_tags (set, RAW_SKY_NODDING);
00365       nwave = cpl_frameset_count_tags (set, RAW_WAVE_LAMP);
00366       ndark = cpl_frameset_count_tags (set, PRO_MASTER_DARK);
00367 
00368       if ( (nwave == 0) & (nsky>0) ) {
00369         sinfo_msg ("No arc lamp frames found, using sky frames instead");
00370         if (ndark == 0)
00371           sinfo_msg ("Including Master dark is recommended when running wavecal on sky frames");
00372 
00373         i = 0;
00374         while (i < cpl_frameset_get_size(set)) {
00375           fr = cpl_frameset_get_frame(set,i);
00376           tag = cpl_frame_get_tag (fr);
00377 
00378           if (strcmp(tag, RAW_SKY_NODDING)==0)
00379           {
00380                 cpl_frame * frnew = 0;
00381                 fname = cpl_frame_get_filename(fr);
00382                 sprintf (&name[0], "fakelamp_%d.fits", non++);
00383                 im = cpl_image_load(fname, CPL_TYPE_FLOAT, 0, 0);
00384                 plist = cpl_propertylist_load(fname,0);
00385 
00386                 /* Both if these needs to be changed to make sure the sky frame is
00387                    interprated as ON-frame in sinfo_dfs.c
00388 
00389                    The selection of KEY_NAME_LAMP_XE is arbitrary, but one of
00390                    KEY_NAME_LAMP_* has to be set */
00391                 cpl_propertylist_set_bool (plist, KEY_NAME_LAMP_XE, 1);
00392                 cpl_propertylist_set_string (plist, KEY_NAME_DPR_TYPE, "WAVE,LAMP");
00393 
00394                 cpl_image_save (im, name, CPL_BPP_IEEE_FLOAT, plist, CPL_IO_DEFAULT);
00395                 cpl_image_delete (im);
00396                 cpl_propertylist_delete(plist);
00397 
00398                 /* Looks like it's not possible to modify the frame directly
00399                    without duplicating it first. */
00400                 frnew = cpl_frame_duplicate (fr);
00401                 cpl_frame_set_filename(frnew, name);
00402                 cpl_frame_set_tag (frnew, RAW_WAVE_LAMP);
00403                 cpl_frameset_erase_frame (set, fr);
00404                 cpl_frameset_insert (set, frnew);
00405           }
00406           else
00407         i++;
00408         }
00409       }
00410 
00411       /*
00412        * Modified code ends here
00413        */
00414   }
00415   ck0(sinfo_new_prepare_stacked_frames(cpl_func,config, set, NULL,
00416                        PRO_WAVE_LAMP_STACKED,0,fk),
00417       "%s FRAME DETERMINATION FAILED",PRO_WAVE_LAMP_STACKED);
00418   sinfo_msg("%s FRAME DETERMINATION SUCCESS", PRO_WAVE_LAMP_STACKED);
00419 
00420   sinfo_msg("---------------------------------------------");
00421   sinfo_msg("WAVELENGTH CALIBRATION");
00422   sinfo_msg("---------------------------------------------");
00423 
00424 
00425   ck0(sinfo_new_wave_cal_slit2(cpl_func,config, set,ref_set),
00426       "FAILED WAVELENGTH CALIBRATION");
00427   sinfo_msg("SUCCESS WAVELENGTH CALIBRATION");
00428 
00429   sinfo_fake_delete(&fk);
00430    if(pdensity < 2) {
00431      check_nomsg(cpl_frameset_erase(set,PRO_STACK_MFLAT_DIST));
00432      check_nomsg(cpl_frameset_erase(set,PRO_WAVE_LAMP_STACKED));
00433    }
00434 
00435  cleanup:
00436 
00437   sinfo_free_frameset(&ref_set);
00438   sinfo_free_frameset(&wrk_set);
00439   sinfo_fake_delete(&fk);
00440 
00441 
00442   if (cpl_error_get_code() != CPL_ERROR_NONE) {
00443     return -1;
00444   } else {
00445     return 0;
00446   }
00447 
00448 }

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