UVES Pipeline Reference Manual  5.4.6
uves_utl_physmod.c
1 /* $Id: uves_utl_physmod.c,v 1.2 2010-06-15 16:17:35 amodigli Exp $
2  *
3  * This file is part of the UVES Pipeline
4  * Copyright (C) 2002,2003 European Southern Observatory
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  */
20 
21 /*
22  * $Author: amodigli $
23  * $Date: 2010-06-15 16:17:35 $
24  * $Revision: 1.2 $
25  * $Name: not supported by cvs2svn $
26  */
27 
28 #ifdef HAVE_CONFIG_H
29 #include <config.h>
30 #endif
31 
32 /*-----------------------------------------------------------------------------
33  Includes
34  ----------------------------------------------------------------------------*/
35 #include <string.h>
36 
37 /* cpl */
38 #include <cpl.h>
39 
40 /* irplib */
41 #include <irplib_utils.h>
42 
43 /*
44 #include <uves_tpl_utils.h>
45 #include <uves_pfits.h>
46 #include <uves_key_names.h>
47 #include <uves_raw_types.h>
48 #include <uves_pro_types.h>
49 #include <uves_functions.h>
50 */
51 
52 #include <uves_dfs.h>
53 #include <uves_msg.h>
54 #include <uves_error.h>
55 #include <uves_utils_wrappers.h>
56 #include <uves_physmod_create_table.h>
57 #include <uves_utils.h>
58 
59 /*-----------------------------------------------------------------------------
60  Functions prototypes
61  ----------------------------------------------------------------------------*/
62 #define UVES_IMA_SIZE_X 4096
63 #define UVES_IMA_SIZE_Y 2048
64 #define UVES_LINE_SIGMA_X 2.0
65 #define UVES_LINE_SIGMA_Y 2.0
66 
67 
68 static int uves_utl_physmod_create(cpl_plugin *) ;
69 static int uves_utl_physmod_exec(cpl_plugin *) ;
70 static int uves_utl_physmod_destroy(cpl_plugin *) ;
71 static int uves_utl_physmod(cpl_parameterlist *, cpl_frameset *) ;
72 
73 /*-----------------------------------------------------------------------------
74  Static variables
75  ----------------------------------------------------------------------------*/
76 
77 static char uves_utl_physmod_description[] =
78 "This recipe generates an image using the physical model.\n"
79 "The input file is the reference arc line (LINE_REFER_TABLE)\n"
80 "The output is the image simulated with the UVES physical model\n"
81 "Information on relevant parameters can be found with\n"
82 "esorex --params uves_utl_physmod\n"
83 "esorex --help uves_utl_physmod\n"
84 "\n";
85 
86 /*-----------------------------------------------------------------------------
87  Functions code
88  ----------------------------------------------------------------------------*/
89 /*---------------------------------------------------------------------------*/
93 /*---------------------------------------------------------------------------*/
94 
96 /*---------------------------------------------------------------------------*/
104 /*---------------------------------------------------------------------------*/
105 int cpl_plugin_get_info(cpl_pluginlist * list)
106 {
107  cpl_recipe * recipe = cpl_calloc(1, sizeof *recipe ) ;
108  cpl_plugin * plugin = &recipe->interface ;
109 
110  cpl_plugin_init(plugin,
111  CPL_PLUGIN_API,
112  UVES_BINARY_VERSION,
113  CPL_PLUGIN_TYPE_RECIPE,
114  "uves_utl_physmod",
115  "Generates a simulated image",
116  uves_utl_physmod_description,
117  "Andrea Modigliani",
118  "Andrea.Modigliani@eso.org",
123 
124  cpl_pluginlist_append(list, plugin) ;
125 
126  return 0;
127 }
128 
129 /*---------------------------------------------------------------------------*/
138 /*---------------------------------------------------------------------------*/
139 static int uves_utl_physmod_create(cpl_plugin * plugin)
140 {
141  cpl_recipe * recipe ;
142  cpl_parameter * p ;
143 
144  /* Get the recipe out of the plugin */
145  if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
146  recipe = (cpl_recipe *)plugin ;
147  else return -1 ;
148  cpl_error_reset();
149  irplib_reset();
150 
151  /* Create the parameters list in the cpl_recipe object */
152  recipe->parameters = cpl_parameterlist_new() ;
153 
154  /* Fill the parameters list */
155  /* --stropt */
156  p = cpl_parameter_new_value("uves.uves_utl_physmod.bias",
157  CPL_TYPE_DOUBLE,
158  "Bias level [ADUs]",
159  "uves.uves_utl_physmod",200.);
160  cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "bias") ;
161  cpl_parameterlist_append(recipe->parameters, p) ;
162 
163  /* --doubleopt */
164  p = cpl_parameter_new_value("uves.uves_utl_physmod.ron",
165  CPL_TYPE_DOUBLE,
166  "RON [ADU]",
167  "uves.uves_utl_physmod", 3.) ;
168  cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "ron") ;
169  cpl_parameterlist_append(recipe->parameters, p) ;
170 
171 
172 
173  p = cpl_parameter_new_value("uves.uves_utl_physmod.gain",
174  CPL_TYPE_DOUBLE,
175  "Detector gain",
176  "uves.uves_utl_physmod", 0.7) ;
177  cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "gain") ;
178  cpl_parameterlist_append(recipe->parameters, p) ;
179 
180 
181  p = cpl_parameter_new_value("uves.uves_utl_physmod.flux",
182  CPL_TYPE_DOUBLE,
183  "Flux level",
184  "uves.uves_utl_physmod",1.0e4) ;
185  cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "flux") ;
186  cpl_parameterlist_append(recipe->parameters, p) ;
187 
188 
189  p = cpl_parameter_new_value("uves.uves_utl_physmod.wlen",
190  CPL_TYPE_INT,
191  "Wavelength setting",
192  "uves.uves_utl_physmod",580) ;
193  cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "wlen") ;
194  cpl_parameterlist_append(recipe->parameters, p) ;
195 
196 
197 
198  p = cpl_parameter_new_value("uves.uves_utl_physmod.binx",
199  CPL_TYPE_INT,
200  "BINX",
201  "uves.uves_utl_physmod",1) ;
202  cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "binx") ;
203  cpl_parameterlist_append(recipe->parameters, p) ;
204 
205 
206  p = cpl_parameter_new_value("uves.uves_utl_physmod.biny",
207  CPL_TYPE_INT,
208  "BINY",
209  "uves.uves_utl_physmod",1) ;
210  cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "biny") ;
211  cpl_parameterlist_append(recipe->parameters, p) ;
212 
213 
214 
215 
216  p = cpl_parameter_new_value("uves_utl_physmod.mbox_x",
217  CPL_TYPE_INT,
218  "MBOX_X",
219  "uves_utl_physmod",40) ;
220  cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "mbox_x") ;
221  cpl_parameterlist_append(recipe->parameters, p) ;
222 
223 
224  p = cpl_parameter_new_value("uves_utl_physmod.mbox_y",
225  CPL_TYPE_INT,
226  "MBOX_Y",
227  "uves_utl_physmod",40) ;
228  cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "mbox_y") ;
229  cpl_parameterlist_append(recipe->parameters, p) ;
230 
231 
232 
233 
234  p = cpl_parameter_new_value("uves_utl_physmod.trans_x",
235  CPL_TYPE_DOUBLE,
236  "X-offset",
237  "uves_utl_physmod",0.) ;
238  cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "trans_x") ;
239  cpl_parameterlist_append(recipe->parameters, p) ;
240 
241 
242 
243  p = cpl_parameter_new_value("uves_utl_physmod.trans_y",
244  CPL_TYPE_DOUBLE,
245  "Y-offset",
246  "uves_utl_physmod",0.) ;
247  cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "trans_y") ;
248  cpl_parameterlist_append(recipe->parameters, p) ;
249 
250 
251  p = cpl_parameter_new_value("uves_utl_physmod.ech_angle_off",
252  CPL_TYPE_DOUBLE,
253  "Offset on echelle angle",
254  "uves_utl_physmod",0.) ;
255  cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "ech_angle_off") ;
256  cpl_parameterlist_append(recipe->parameters, p) ;
257 
258 
259 
260  p = cpl_parameter_new_value("uves_utl_physmod.cd_angle_off",
261  CPL_TYPE_DOUBLE,
262  "Offset on cross disperser angle",
263  "uves_utl_physmod",0.) ;
264  cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "cd_angle_off") ;
265  cpl_parameterlist_append(recipe->parameters, p) ;
266 
267 
268  p = cpl_parameter_new_value("uves_utl_physmod.ccd_rot_angle_off",
269  CPL_TYPE_DOUBLE,
270  "CCD rotation angle",
271  "uves_utl_physmod",0.) ;
272  cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "ccd_rot_angle_off") ;
273  cpl_parameterlist_append(recipe->parameters, p) ;
274 
275  p = cpl_parameter_new_value("uves_utl_physmod.compute_regression_sw",
276  CPL_TYPE_BOOL,
277  "Compute regression",
278  "uves_utl_physmod",CPL_TRUE) ;
279  cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "compute_regression_sw") ;
280  cpl_parameterlist_append(recipe->parameters, p) ;
281 
282  p = cpl_parameter_new_value("uves_utl_physmod.process_chip",
283  CPL_TYPE_STRING,
284  "Chip to be processed",
285  "uves_utl_physmod","both") ;
286  cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "process_chip") ;
287  cpl_parameterlist_append(recipe->parameters, p) ;
288 
289 
290  /* Return */
291  return 0;
292 }
293 
294 /*---------------------------------------------------------------------------*/
300 /*---------------------------------------------------------------------------*/
301 static int uves_utl_physmod_exec(cpl_plugin * plugin)
302 {
303  cpl_recipe * recipe ;
304  int code=0;
305  cpl_errorstate initial_errorstate = cpl_errorstate_get();
306 
307  /* Get the recipe out of the plugin */
308  if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
309  recipe = (cpl_recipe *)plugin ;
310  else return -1 ;
311  cpl_error_reset();
312  irplib_reset();
313  code = uves_utl_physmod(recipe->parameters, recipe->frames) ;
314 
315 
316  if (!cpl_errorstate_is_equal(initial_errorstate)) {
317  /* Dump the error history since recipe execution start.
318  At this point the recipe cannot recover from the error */
319  cpl_errorstate_dump(initial_errorstate, CPL_FALSE, NULL);
320  }
321 
322  return code ;
323 }
324 
325 /*---------------------------------------------------------------------------*/
331 /*---------------------------------------------------------------------------*/
332 static int uves_utl_physmod_destroy(cpl_plugin * plugin)
333 {
334  cpl_recipe * recipe ;
335 
336  /* Get the recipe out of the plugin */
337  if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
338  recipe = (cpl_recipe *)plugin ;
339  else return -1 ;
340 
341  cpl_parameterlist_delete(recipe->parameters) ;
342  return 0 ;
343 }
344 
345 /*---------------------------------------------------------------------------*/
352 /*---------------------------------------------------------------------------*/
353 static int
354 uves_utl_physmod( cpl_parameterlist * parlist,
355  cpl_frameset * frames)
356 {
357  cpl_parameter * p= NULL ;
358  double bias=200;
359  double ron=0;
360  double gain=0;
361  double flux=0;
362  double offx=0;
363  double offy=0;
364  int wlen=0;
365  int binx=0;
366  int biny=0;
367  cpl_image* ima=NULL;
368  cpl_image* noise=NULL;
369  cpl_image* fck=NULL;
370 
371  int sx=0;
372  int sy=0;
373 
374  double xc=100;
375  double yc=100;
376  char name[80];
377  int nfrm=0;
378  int nraw=0;
379 
380  uves_propertylist* plist=NULL;
381  cpl_frame* product_frame=NULL;
382  cpl_frameset* ref_set=NULL;
383  cpl_frameset* fck_set=NULL;
384  cpl_table* line_ref_tbl=NULL;
385  cpl_frame* line_ref_frame=NULL;
386  cpl_frame* fck_frame=NULL;
387  cpl_table* mod_tbl=NULL;
388  cpl_table* fsr_tbl=NULL;
389  bool flames=false;
390  enum uves_chip chip = UVES_CHIP_REDL;
391  cpl_image *raw_image[2] = {NULL, NULL};
392  uves_propertylist *raw_header[2] = {NULL, NULL};
393  uves_propertylist *rotated_header[2] = {NULL, NULL};
394  const char *raw_filename = "";
395  bool blue = false;
396  const char *line_refer_filename = "";
397  cpl_table *line_refer = NULL;
398  const char* PROCESS_CHIP=NULL;
399  int raw_index = 0;
400  const char *product_filename = NULL;
401 
402  uves_msg("Welcome to UVES Pipeline release %d.%d.%d",
403  UVES_MAJOR_VERSION,UVES_MINOR_VERSION,UVES_MICRO_VERSION);
404 
405  /* HOW TO RETRIEVE INPUT PARAMETERS */
406  check_nomsg(p=cpl_parameterlist_find(parlist,"uves.uves_utl_physmod.bias"));
407  check_nomsg(bias=cpl_parameter_get_double(p));
408 
409  check_nomsg(p=cpl_parameterlist_find(parlist,"uves.uves_utl_physmod.ron"));
410  check_nomsg(ron = cpl_parameter_get_double(p)) ;
411 
412  check_nomsg(p=cpl_parameterlist_find(parlist,"uves.uves_utl_physmod.gain"));
413  check_nomsg(gain = cpl_parameter_get_double(p)) ;
414 
415  check_nomsg(p=cpl_parameterlist_find(parlist,"uves.uves_utl_physmod.flux"));
416  check_nomsg(flux = cpl_parameter_get_double(p)) ;
417 
418  check_nomsg(p=cpl_parameterlist_find(parlist,"uves.uves_utl_physmod.wlen"));
419  check_nomsg(wlen = cpl_parameter_get_int(p)) ;
420 
421  check_nomsg(p=cpl_parameterlist_find(parlist,"uves.uves_utl_physmod.binx"));
422  check_nomsg(binx = cpl_parameter_get_int(p)) ;
423 
424  check_nomsg(p=cpl_parameterlist_find(parlist,"uves.uves_utl_physmod.biny"));
425  check_nomsg(biny = cpl_parameter_get_int(p)) ;
426 
427  check_nomsg(p=cpl_parameterlist_find(parlist,"uves_utl_physmod.trans_x"));
428  check_nomsg(offx = cpl_parameter_get_double(p)) ;
429 
430  check_nomsg(p=cpl_parameterlist_find(parlist,"uves_utl_physmod.trans_y"));
431  check_nomsg(offy = cpl_parameter_get_double(p)) ;
432 
433  check( uves_get_parameter(parlist, NULL, "uves_utl_physmod", "process_chip",
434  CPL_TYPE_STRING, &PROCESS_CHIP),
435  "Could not read parameter");
436  uves_string_toupper((char*)PROCESS_CHIP);
437 
438  /* Identify the RAW and CALIB frames in the input frameset */
439  check(uves_dfs_set_groups(frames),
440  "Cannot identify RAW and CALIB frames") ;
441  //cpl_frameset_dump(frames,stdout);
442 
443  /* HOW TO ACCESS INPUT DATA */
444  nfrm=cpl_frameset_get_size(frames);
445  if(nfrm<1) {
446  uves_msg_error("Empty input frame list!");
447  goto cleanup ;
448  }
449 
450  /* HOW TO ACCESS INPUT DATA */
451  check_nomsg(ref_set=cpl_frameset_new());
452 
453  check( uves_load_formatcheck(frames, flames, &raw_filename, raw_image,
454  raw_header, rotated_header, &blue),
455  "Error loading raw frame");
456 
457  check( uves_load_linerefertable(frames, &line_refer_filename,
458  &line_refer, NULL),
459  "Could not load line reference table");
460 
461  for (chip = uves_chip_get_first(blue);
462  chip != UVES_CHIP_INVALID;
463  chip = uves_chip_get_next(chip))
464  {
465 
466  if(strcmp(PROCESS_CHIP,"REDU") == 0) {
467  chip = uves_chip_get_next(chip);
468  }
469  uves_msg("processing frame %s",uves_chip_tostring_lower(chip));
470  raw_index = uves_chip_get_index(chip);
471  check_nomsg(uves_physmod_create_table(raw_header[raw_index],chip,flames,
472  "uves_utl_physmod",
473  parlist,line_refer,offx,offy,
474  &mod_tbl,&fsr_tbl));
475 
476  }
477  product_filename = "model_prediction";
478  check( uves_save_table_local("Physical model table",
479  product_filename, mod_tbl,
480  chip, -1, -1, raw_header[raw_index], NULL),
481  "Error saving physical model table");
482 
483  product_filename = "free_spectral_range";
484  check( uves_save_table_local("FSR table",
485  product_filename, fsr_tbl,
486  chip, -1, -1, raw_header[raw_index], NULL),
487  "Error saving free spectral range table");
488  exit(0);
489 
490 
491  /* Create product frame */
492  check_nomsg(product_frame = cpl_frame_new());
493  check_nomsg(cpl_frame_set_filename(product_frame, name)) ;
494  check_nomsg(cpl_frame_set_tag(product_frame, "PRODUCT")) ;
495  check_nomsg(cpl_frame_set_type(product_frame, CPL_FRAME_TYPE_IMAGE)) ;
496  check_nomsg(cpl_frame_set_group(product_frame, CPL_FRAME_GROUP_PRODUCT)) ;
497  check(cpl_frame_set_level(product_frame, CPL_FRAME_LEVEL_FINAL),
498  "Error while initialising the product frame") ;
499 
500 
501  /* Add DataFlow keywords */
502  check_nomsg(uves_propertylist_erase_regexp(plist, "^ESO PRO CATG",0));
503  /*
504  check(cpl_dfs_setup_product_header(plist,
505  product_frame,
506  frames,
507  parlist,
508  "uves_utl_physmod",
509  "UVES",
510  DICTIONARY),
511  "Problem in the product DFS-compliance") ;
512  */
513  /* Save the file
514  check(cpl_image_save(ima_res,
515  name,
516  CPL_BPP_IEEE_FLOAT,
517  plist1,
518  CPL_IO_DEFAULT),
519  "Could not save product");
520  */
521  uves_free_propertylist(&plist);
522 
523  /* Log the saved file in the input frameset */
524  check_nomsg(cpl_frameset_insert(frames, product_frame)) ;
525 
526 
527  cleanup:
528 
529 
530 
531  if (cpl_error_get_code()) {
532  return -1 ;
533  } else {
534  return 0 ;
535  }
536 
537 }
#define uves_msg_error(...)
Print an error message.
Definition: uves_msg.h:64
static int uves_utl_physmod_destroy(cpl_plugin *)
Destroy what has been created by the 'create' function.
static int uves_utl_physmod_create(cpl_plugin *)
Setup the recipe options.
void irplib_reset(void)
Reset IRPLIB state.
#define check_nomsg(CMD)
Definition: uves_error.h:204
const char * uves_get_license(void)
Get the pipeline copyright and license.
Definition: uves_utils.c:1676
int uves_physmod_create_table(const uves_propertylist *raw_header, enum uves_chip chip, bool flames, const char *recipe_id, const cpl_parameterlist *parameters, cpl_table *line_refer, const double physmod_shift_x, const double physmod_shift_y, cpl_table **mod_tbl, cpl_table **fsr_tbl)
Generates two tables with results of the UVES physical model.
static int uves_utl_physmod_exec(cpl_plugin *)
Execute the plugin instance given by the interface.
int cpl_plugin_get_info(cpl_pluginlist *list)
Build the list of available plugins, for this module.
static int uves_utl_physmod(cpl_parameterlist *, cpl_frameset *)
Get the command line options and execute the data reduction.
#define uves_msg(...)
Print a message on 'info' or 'debug' level.
Definition: uves_msg.h:119
int uves_chip_get_index(enum uves_chip chip)
Convert to integer.
Definition: uves_chip.c:124
enum uves_chip uves_chip_get_first(bool blue)
Get first chip for blue or red arm.
Definition: uves_chip.c:92
const char * uves_chip_tostring_lower(enum uves_chip chip)
Convert to string.
Definition: uves_chip.c:139
int uves_propertylist_erase_regexp(uves_propertylist *self, const char *regexp, int invert)
Erase all properties with name matching a given regular expression.
enum uves_chip uves_chip_get_next(enum uves_chip chip)
Get next chip.
Definition: uves_chip.c:108
const char * uves_string_toupper(char *s)
Convert all lowercase characters in a string into uppercase characters.
Definition: uves_utils.c:1493
#define check(CMD,...)
Definition: uves_error.h:198