uves_utl_physmod.c

00001 /* $Id: uves_utl_physmod.c,v 1.2 2010/06/15 16:17:35 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: 2010/06/15 16:17:35 $
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_physmod_create_table.h>
00057 #include <uves_utils.h>
00058 
00059 /*-----------------------------------------------------------------------------
00060                             Functions prototypes
00061  ----------------------------------------------------------------------------*/
00062 #define UVES_IMA_SIZE_X 4096
00063 #define UVES_IMA_SIZE_Y 2048
00064 #define UVES_LINE_SIGMA_X 2.0
00065 #define UVES_LINE_SIGMA_Y 2.0
00066 
00067 
00068 static int uves_utl_physmod_create(cpl_plugin *) ;
00069 static int uves_utl_physmod_exec(cpl_plugin *) ;
00070 static int uves_utl_physmod_destroy(cpl_plugin *) ;
00071 static int uves_utl_physmod(cpl_parameterlist *, cpl_frameset *) ;
00072 
00073 /*-----------------------------------------------------------------------------
00074                             Static variables
00075  ----------------------------------------------------------------------------*/
00076 
00077 static char uves_utl_physmod_description[] =
00078 "This recipe generates an image using the physical model.\n"
00079 "The input file is the reference arc line (LINE_REFER_TABLE)\n"
00080 "The output is the image simulated with the UVES physical model\n"
00081 "Information on relevant parameters can be found with\n"
00082 "esorex --params uves_utl_physmod\n"
00083 "esorex --help uves_utl_physmod\n"
00084 "\n";
00085 
00086 /*-----------------------------------------------------------------------------
00087                                 Functions code
00088  ----------------------------------------------------------------------------*/
00089 /*---------------------------------------------------------------------------*/
00093 /*---------------------------------------------------------------------------*/
00094 
00096 /*---------------------------------------------------------------------------*/
00104 /*---------------------------------------------------------------------------*/
00105 int cpl_plugin_get_info(cpl_pluginlist * list)
00106 {
00107     cpl_recipe  *   recipe = cpl_calloc(1, sizeof *recipe ) ;
00108     cpl_plugin  *   plugin = &recipe->interface ;
00109 
00110     cpl_plugin_init(plugin,
00111                     CPL_PLUGIN_API,
00112                     UVES_BINARY_VERSION,
00113                     CPL_PLUGIN_TYPE_RECIPE,
00114                     "uves_utl_physmod",
00115                     "Generates a simulated image",
00116                     uves_utl_physmod_description,
00117                     "Andrea Modigliani",
00118                     "Andrea.Modigliani@eso.org",
00119                     uves_get_license(),
00120                     uves_utl_physmod_create,
00121                     uves_utl_physmod_exec,
00122                     uves_utl_physmod_destroy) ;
00123 
00124     cpl_pluginlist_append(list, plugin) ;
00125     
00126     return 0;
00127 }
00128 
00129 /*---------------------------------------------------------------------------*/
00138 /*---------------------------------------------------------------------------*/
00139 static int uves_utl_physmod_create(cpl_plugin * plugin)
00140 {
00141     cpl_recipe      * recipe ;
00142     cpl_parameter   * p ;
00143 
00144     /* Get the recipe out of the plugin */
00145     if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE) 
00146         recipe = (cpl_recipe *)plugin ;
00147     else return -1 ;
00148     cpl_error_reset();
00149     irplib_reset();
00150 
00151     /* Create the parameters list in the cpl_recipe object */
00152     recipe->parameters = cpl_parameterlist_new() ; 
00153 
00154     /* Fill the parameters list */
00155     /* --stropt */
00156     p = cpl_parameter_new_value("uves.uves_utl_physmod.bias", 
00157                                 CPL_TYPE_DOUBLE, 
00158                                 "Bias level [ADUs]", 
00159                                 "uves.uves_utl_physmod",200.);
00160     cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "bias") ;
00161     cpl_parameterlist_append(recipe->parameters, p) ;
00162 
00163     /* --doubleopt */
00164     p = cpl_parameter_new_value("uves.uves_utl_physmod.ron", 
00165                 CPL_TYPE_DOUBLE, 
00166                 "RON [ADU]",
00167                 "uves.uves_utl_physmod", 3.) ;
00168     cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "ron") ;
00169     cpl_parameterlist_append(recipe->parameters, p) ;
00170 
00171 
00172 
00173     p = cpl_parameter_new_value("uves.uves_utl_physmod.gain", 
00174                 CPL_TYPE_DOUBLE, 
00175                 "Detector gain",
00176                 "uves.uves_utl_physmod", 0.7) ;
00177     cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "gain") ;
00178     cpl_parameterlist_append(recipe->parameters, p) ;
00179 
00180 
00181     p = cpl_parameter_new_value("uves.uves_utl_physmod.flux",
00182                 CPL_TYPE_DOUBLE, 
00183                 "Flux level",
00184                 "uves.uves_utl_physmod",1.0e4) ;
00185     cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "flux") ;
00186     cpl_parameterlist_append(recipe->parameters, p) ;
00187 
00188 
00189     p = cpl_parameter_new_value("uves.uves_utl_physmod.wlen",
00190                 CPL_TYPE_INT, 
00191                 "Wavelength setting",
00192                 "uves.uves_utl_physmod",580) ;
00193     cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "wlen") ;
00194     cpl_parameterlist_append(recipe->parameters, p) ;
00195 
00196 
00197 
00198     p = cpl_parameter_new_value("uves.uves_utl_physmod.binx",
00199                 CPL_TYPE_INT, 
00200                 "BINX",
00201                 "uves.uves_utl_physmod",1) ;
00202     cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "binx") ;
00203     cpl_parameterlist_append(recipe->parameters, p) ;
00204 
00205 
00206     p = cpl_parameter_new_value("uves.uves_utl_physmod.biny",
00207                 CPL_TYPE_INT, 
00208                 "BINY",
00209                 "uves.uves_utl_physmod",1) ;
00210     cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "biny") ;
00211     cpl_parameterlist_append(recipe->parameters, p) ;
00212 
00213 
00214 
00215 
00216     p = cpl_parameter_new_value("uves_utl_physmod.mbox_x",
00217                 CPL_TYPE_INT, 
00218                 "MBOX_X",
00219                 "uves_utl_physmod",40) ;
00220     cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "mbox_x") ;
00221     cpl_parameterlist_append(recipe->parameters, p) ;
00222 
00223 
00224     p = cpl_parameter_new_value("uves_utl_physmod.mbox_y",
00225                 CPL_TYPE_INT, 
00226                 "MBOX_Y",
00227                 "uves_utl_physmod",40) ;
00228     cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "mbox_y") ;
00229     cpl_parameterlist_append(recipe->parameters, p) ;
00230 
00231 
00232 
00233 
00234     p = cpl_parameter_new_value("uves_utl_physmod.trans_x",
00235                 CPL_TYPE_DOUBLE, 
00236                 "X-offset",
00237                 "uves_utl_physmod",0.) ;
00238     cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "trans_x") ;
00239     cpl_parameterlist_append(recipe->parameters, p) ;
00240 
00241 
00242 
00243     p = cpl_parameter_new_value("uves_utl_physmod.trans_y",
00244                 CPL_TYPE_DOUBLE, 
00245                 "Y-offset",
00246                 "uves_utl_physmod",0.) ;
00247     cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "trans_y") ;
00248     cpl_parameterlist_append(recipe->parameters, p) ;
00249 
00250 
00251     p = cpl_parameter_new_value("uves_utl_physmod.ech_angle_off",
00252                 CPL_TYPE_DOUBLE, 
00253                 "Offset on echelle angle",
00254                 "uves_utl_physmod",0.) ;
00255     cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "ech_angle_off") ;
00256     cpl_parameterlist_append(recipe->parameters, p) ;
00257 
00258 
00259 
00260     p = cpl_parameter_new_value("uves_utl_physmod.cd_angle_off",
00261                 CPL_TYPE_DOUBLE, 
00262                 "Offset on cross disperser angle",
00263                 "uves_utl_physmod",0.) ;
00264     cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "cd_angle_off") ;
00265     cpl_parameterlist_append(recipe->parameters, p) ;
00266 
00267 
00268     p = cpl_parameter_new_value("uves_utl_physmod.ccd_rot_angle_off",
00269                 CPL_TYPE_DOUBLE, 
00270                 "CCD rotation angle",
00271                 "uves_utl_physmod",0.) ;
00272     cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "ccd_rot_angle_off") ;
00273     cpl_parameterlist_append(recipe->parameters, p) ;
00274 
00275     p = cpl_parameter_new_value("uves_utl_physmod.compute_regression_sw",
00276                 CPL_TYPE_BOOL, 
00277                 "Compute regression",
00278                 "uves_utl_physmod",CPL_TRUE) ;
00279     cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "compute_regression_sw") ;
00280     cpl_parameterlist_append(recipe->parameters, p) ;
00281 
00282     p = cpl_parameter_new_value("uves_utl_physmod.process_chip",
00283                 CPL_TYPE_STRING, 
00284                 "Chip to be processed",
00285                 "uves_utl_physmod","both") ;
00286     cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "process_chip") ;
00287     cpl_parameterlist_append(recipe->parameters, p) ;
00288 
00289 
00290     /* Return */
00291     return 0;
00292 }
00293 
00294 /*---------------------------------------------------------------------------*/
00300 /*---------------------------------------------------------------------------*/
00301 static int uves_utl_physmod_exec(cpl_plugin * plugin)
00302 {
00303     cpl_recipe  *   recipe ;
00304      int code=0;
00305      cpl_errorstate initial_errorstate = cpl_errorstate_get();
00306 
00307     /* Get the recipe out of the plugin */
00308     if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE) 
00309         recipe = (cpl_recipe *)plugin ;
00310     else return -1 ;
00311     cpl_error_reset();
00312     irplib_reset();
00313     code = uves_utl_physmod(recipe->parameters, recipe->frames) ;
00314 
00315 
00316     if (!cpl_errorstate_is_equal(initial_errorstate)) {                      
00317         /* Dump the error history since recipe execution start.                
00318            At this point the recipe cannot recover from the error */           
00319         cpl_errorstate_dump(initial_errorstate, CPL_FALSE, NULL);              
00320     } 
00321 
00322     return code ;
00323 }
00324 
00325 /*---------------------------------------------------------------------------*/
00331 /*---------------------------------------------------------------------------*/
00332 static int uves_utl_physmod_destroy(cpl_plugin * plugin)
00333 {
00334     cpl_recipe  *   recipe ;
00335     
00336     /* Get the recipe out of the plugin */
00337     if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE) 
00338         recipe = (cpl_recipe *)plugin ;
00339     else return -1 ;
00340 
00341     cpl_parameterlist_delete(recipe->parameters) ; 
00342     return 0 ;
00343 }
00344 
00345 /*---------------------------------------------------------------------------*/
00352 /*---------------------------------------------------------------------------*/
00353 static int 
00354 uves_utl_physmod( cpl_parameterlist   *   parlist, 
00355           cpl_frameset        *   frames)
00356 {
00357   cpl_parameter       *   p= NULL ;
00358   double bias=200;
00359   double ron=0;
00360   double gain=0;
00361   double flux=0;
00362   double offx=0;
00363   double offy=0;
00364   int wlen=0;
00365   int binx=0;
00366   int biny=0;
00367   cpl_image* ima=NULL;
00368   cpl_image* noise=NULL;
00369   cpl_image* fck=NULL;
00370 
00371   int sx=0;
00372   int sy=0;
00373 
00374   double xc=100;
00375   double yc=100;
00376   char name[80];
00377   int nfrm=0;
00378   int nraw=0;
00379 
00380   uves_propertylist* plist=NULL;
00381   cpl_frame* product_frame=NULL;
00382   cpl_frameset* ref_set=NULL;
00383   cpl_frameset* fck_set=NULL;
00384   cpl_table* line_ref_tbl=NULL;
00385   cpl_frame* line_ref_frame=NULL;
00386   cpl_frame* fck_frame=NULL;
00387   cpl_table* mod_tbl=NULL;
00388   cpl_table* fsr_tbl=NULL;
00389   bool flames=false;
00390   enum uves_chip chip = UVES_CHIP_REDL;
00391   cpl_image        *raw_image[2]      = {NULL, NULL};
00392   uves_propertylist *raw_header[2]     = {NULL, NULL};
00393   uves_propertylist *rotated_header[2] = {NULL, NULL};
00394   const char *raw_filename = "";
00395   bool blue = false;
00396   const char *line_refer_filename = "";
00397   cpl_table        *line_refer            = NULL;
00398   const char* PROCESS_CHIP=NULL;
00399   int raw_index = 0;
00400   const char *product_filename = NULL;
00401 
00402   uves_msg("Welcome to UVES Pipeline release %d.%d.%d",
00403        UVES_MAJOR_VERSION,UVES_MINOR_VERSION,UVES_MICRO_VERSION);
00404 
00405   /* HOW TO RETRIEVE INPUT PARAMETERS */
00406   check_nomsg(p=cpl_parameterlist_find(parlist,"uves.uves_utl_physmod.bias"));
00407   check_nomsg(bias=cpl_parameter_get_double(p));
00408 
00409   check_nomsg(p=cpl_parameterlist_find(parlist,"uves.uves_utl_physmod.ron"));
00410   check_nomsg(ron = cpl_parameter_get_double(p)) ;
00411 
00412   check_nomsg(p=cpl_parameterlist_find(parlist,"uves.uves_utl_physmod.gain"));
00413   check_nomsg(gain = cpl_parameter_get_double(p)) ;
00414 
00415   check_nomsg(p=cpl_parameterlist_find(parlist,"uves.uves_utl_physmod.flux"));
00416   check_nomsg(flux = cpl_parameter_get_double(p)) ;
00417 
00418   check_nomsg(p=cpl_parameterlist_find(parlist,"uves.uves_utl_physmod.wlen"));
00419   check_nomsg(wlen = cpl_parameter_get_int(p)) ;
00420 
00421   check_nomsg(p=cpl_parameterlist_find(parlist,"uves.uves_utl_physmod.binx"));
00422   check_nomsg(binx = cpl_parameter_get_int(p)) ;
00423 
00424   check_nomsg(p=cpl_parameterlist_find(parlist,"uves.uves_utl_physmod.biny"));
00425   check_nomsg(biny = cpl_parameter_get_int(p)) ;
00426 
00427   check_nomsg(p=cpl_parameterlist_find(parlist,"uves_utl_physmod.trans_x"));
00428   check_nomsg(offx = cpl_parameter_get_double(p)) ;
00429 
00430   check_nomsg(p=cpl_parameterlist_find(parlist,"uves_utl_physmod.trans_y"));
00431   check_nomsg(offy = cpl_parameter_get_double(p)) ;
00432 
00433   check( uves_get_parameter(parlist, NULL, "uves_utl_physmod", "process_chip", 
00434                 CPL_TYPE_STRING, &PROCESS_CHIP),
00435      "Could not read parameter");
00436   uves_string_toupper((char*)PROCESS_CHIP);
00437 
00438   /* Identify the RAW and CALIB frames in the input frameset */
00439   check(uves_dfs_set_groups(frames),
00440     "Cannot identify RAW and CALIB frames") ;
00441   //cpl_frameset_dump(frames,stdout);
00442 
00443   /* HOW TO ACCESS INPUT DATA */
00444   nfrm=cpl_frameset_get_size(frames);
00445   if(nfrm<1) {
00446     uves_msg_error("Empty input frame list!");
00447     goto cleanup ;
00448   }
00449 
00450   /* HOW TO ACCESS INPUT DATA */
00451   check_nomsg(ref_set=cpl_frameset_new());
00452 
00453   check( uves_load_formatcheck(frames, flames, &raw_filename, raw_image, 
00454                    raw_header, rotated_header, &blue), 
00455      "Error loading raw frame");
00456 
00457   check( uves_load_linerefertable(frames, &line_refer_filename, 
00458                   &line_refer, NULL), 
00459      "Could not load line reference table");
00460 
00461   for (chip = uves_chip_get_first(blue); 
00462        chip != UVES_CHIP_INVALID; 
00463        chip = uves_chip_get_next(chip))
00464     {
00465  
00466       if(strcmp(PROCESS_CHIP,"REDU") == 0) {
00467     chip = uves_chip_get_next(chip);
00468       }
00469       uves_msg("processing frame %s",uves_chip_tostring_lower(chip));
00470       raw_index = uves_chip_get_index(chip);
00471       check_nomsg(uves_physmod_create_table(raw_header[raw_index],chip,flames,
00472                                             "uves_utl_physmod",
00473                                             parlist,line_refer,offx,offy,
00474                                             &mod_tbl,&fsr_tbl));
00475 
00476     }
00477   product_filename = "model_prediction";
00478   check( uves_save_table_local("Physical model table", 
00479                                product_filename, mod_tbl, 
00480                                chip, -1, -1, raw_header[raw_index], NULL),
00481          "Error saving physical model table");
00482 
00483   product_filename = "free_spectral_range";
00484   check( uves_save_table_local("FSR table",
00485                                product_filename, fsr_tbl,
00486                                chip, -1, -1, raw_header[raw_index], NULL),
00487          "Error saving free spectral range table");
00488   exit(0);
00489 
00490 
00491   /* Create product frame */
00492   check_nomsg(product_frame = cpl_frame_new());
00493   check_nomsg(cpl_frame_set_filename(product_frame, name)) ;
00494   check_nomsg(cpl_frame_set_tag(product_frame, "PRODUCT")) ;
00495   check_nomsg(cpl_frame_set_type(product_frame, CPL_FRAME_TYPE_IMAGE)) ;
00496   check_nomsg(cpl_frame_set_group(product_frame, CPL_FRAME_GROUP_PRODUCT)) ;
00497   check(cpl_frame_set_level(product_frame, CPL_FRAME_LEVEL_FINAL),
00498     "Error while initialising the product frame") ;
00499     
00500 
00501   /* Add DataFlow keywords */
00502   check_nomsg(uves_propertylist_erase_regexp(plist, "^ESO PRO CATG",0));
00503   /*
00504     check(cpl_dfs_setup_product_header(plist, 
00505     product_frame, 
00506     frames, 
00507     parlist,
00508     "uves_utl_physmod", 
00509     "UVES", 
00510     DICTIONARY),
00511     "Problem in the product DFS-compliance") ;
00512   */
00513   /* Save the file 
00514      check(cpl_image_save(ima_res, 
00515      name, 
00516      CPL_BPP_IEEE_FLOAT, 
00517      plist1,
00518      CPL_IO_DEFAULT),
00519      "Could not save product");
00520   */
00521   uves_free_propertylist(&plist); 
00522 
00523   /* Log the saved file in the input frameset */
00524   check_nomsg(cpl_frameset_insert(frames, product_frame)) ;
00525 
00526 
00527  cleanup:
00528 
00529 
00530 
00531   if (cpl_error_get_code()) {
00532     return -1 ;
00533   } else {
00534     return 0 ;
00535   }
00536 
00537 }

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