SINFONI Pipeline Reference Manual  2.6.0
sinfo_rec_wavecal.c
1 /* $Id: sinfo_rec_wavecal.c,v 1.31 2012-09-17 09:06:39 amodigli Exp $
2  *
3  * This file is part of the SINFONI 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: 2012-09-17 09:06:39 $
24  * $Revision: 1.31 $
25  * $Name: not supported by cvs2svn $
26  */
27 
28 /****************************************************************
29  * Wave_Cal Frames Data Reduction *
30  ****************************************************************/
31 
32 /*-----------------------------------------------------------------------------
33  Includes
34  -----------------------------------------------------------------------------*/
35 
36 #ifdef HAVE_CONFIG_H
37 #include <config.h> /* allows the program compilation */
38 #endif
39 
40 /* std */
41 #include <strings.h>
42 #include <string.h>
43 #include <stdio.h>
44 
45 
46 /* cpl */
47 #include <cpl.h>
48 
49 /* irplib */
50 #include <irplib_utils.h>
51 
52 /* sinfoni */
53 #include <sinfo_pro_types.h>
54 #include <sinfo_general_config.h>
55 #include <sinfo_utilities.h>
56 #include <sinfo_product_config.h>
57 #include <sinfo_prepare_stacked_frames_config.h>
58 #include <sinfo_wavecal_config.h>
59 #include <sinfo_raw_types.h>
60 #include <sinfo_tpl_utils.h>
61 #include <sinfo_tpl_dfs.h>
62 #include <sinfo_hidden.h>
63 #include <sinfo_globals.h>
64 #include <sinfo_functions.h>
65 #include <sinfo_msg.h>
66 #include <sinfo_new_prepare_stacked_frames.h>
67 #include <sinfo_new_wave_cal_slit2.h>
68 #include <sinfo_error.h>
69 #include <sinfo_utils_wrappers.h>
70 
71 #include <sinfo_key_names.h>
72 
73 /*-----------------------------------------------------------------------------
74  Functions prototypes
75  -----------------------------------------------------------------------------*/
76 static int sinfo_rec_wavecal_create(cpl_plugin *);
77 static int sinfo_rec_wavecal_exec(cpl_plugin *);
78 static int sinfo_rec_wavecal_destroy(cpl_plugin *);
79 static int sinfo_rec_wavecal(cpl_parameterlist *, cpl_frameset *);
80 
81 /*-----------------------------------------------------------------------------
82  Static variables
83  -----------------------------------------------------------------------------*/
84 
85 static char sinfo_rec_wavecal_description1[] =
86  "This recipe performs wavelength calibration.\n"
87  "The input files are on/off arc lamp frames with tag WAVE_LAMP\n"
88  "Master calibration frame input is:\n"
89  "A corresponding (band) reference line table with tag REF_LINE_ARC\n"
90  "A corresponding (band) distortion table with tag DISTORTION\n"
91  "A corresponding (band,preoptic) master flat with tag MASTER_FLAT_LAMP\n"
92  "A corresponding (band,preoptics) master bad "
93  "pixel map with tag MASTER_BP_MAP\n";
94 
95 
96 static char sinfo_rec_wavecal_description2[] =
97  "If wcal-estimate_ind=TRUE, a corresponding (band,preoptics) slitlet position\n"
98  "guess table SLIT_POS_GUESS (for example a copy of the SLIT_POS product)\n"
99  "If wcal-calib_indicator=FALSE, a corresponding (band,preoptics) parabolic \n"
100  "fit coefficients table WAVE_COEF_SLIT\n"
101  "If sinfoni.wavecal.slitpos_boostrap_switch=FALSE \n"
102  "(wcal-calib-slitpos_bootstrap=0),\n"
103  "a corresponding (band,preoptics) slitlets position table with tag SLIT_POS\n";
104 
105 
106 
107 static char sinfo_rec_wavecal_description3[] =
108  "The main products are:\n"
109  "The master flat field corrected for distortions \n"
110  "(PRO.CATG=MFLAT_STACKED_DIST)\n"
111  "The arc lamp frames stacked (PRO.CATG=WAVE_LAMP_STACKED)\n"
112  "The wavelength map (PRO.CATG=WAVE_MAP)\n"
113  "The slitlet position table (PRO.CATG=SLIT_POS)\n"
114  "A parabolic fit coefficients table (PRO.CATG=WAVE_COEF_SLIT)\n"
115  "Parameters relative to arc lamp line fit: (PRO.CATG=WAVE_FIT_PARAMS)\n"
116  "\n";
117 
118 
119 static char sinfo_rec_wavecal_description[1500];
120 
121 /*-----------------------------------------------------------------------------
122  Functions code
123  ----------------------------------------------------------------------------*/
124 /*---------------------------------------------------------------------------*/
128 /*---------------------------------------------------------------------------*/
129 
131 /*---------------------------------------------------------------------------*/
140 /*---------------------------------------------------------------------------*/
141 
142 int
143 cpl_plugin_get_info(cpl_pluginlist *list)
144 {
145 
146  cpl_recipe *recipe = cpl_calloc(1, sizeof *recipe);
147  cpl_plugin *plugin = &recipe->interface;
148 
149 
150  strcpy(sinfo_rec_wavecal_description,sinfo_rec_wavecal_description1);
151  strcat(sinfo_rec_wavecal_description,sinfo_rec_wavecal_description2);
152  strcat(sinfo_rec_wavecal_description,sinfo_rec_wavecal_description3);
153 
154  cpl_plugin_init(plugin,
155  CPL_PLUGIN_API,
156  SINFONI_BINARY_VERSION,
157  CPL_PLUGIN_TYPE_RECIPE,
158  "sinfo_rec_wavecal",
159  "Wavelength calibration and slitpos determination",
160  sinfo_rec_wavecal_description,
161  "Andrea Modigliani",
162  "Andrea.Modigliani@eso.org",
163  sinfo_get_license(),
164  sinfo_rec_wavecal_create,
165  sinfo_rec_wavecal_exec,
166  sinfo_rec_wavecal_destroy);
167 
168  cpl_pluginlist_append(list, plugin);
169 
170  return 0;
171 
172 }
173 
174 /*---------------------------------------------------------------------------*/
182 /*---------------------------------------------------------------------------*/
183 
184 static int sinfo_rec_wavecal_create(cpl_plugin *plugin)
185 {
186  cpl_recipe * recipe ;
187 
188  /* Check that the plugin is part of a valid recipe */
189  if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
190  recipe = (cpl_recipe *)plugin ;
191  else return -1 ;
192  cpl_error_reset();
193  irplib_reset();
194 
195  /* Create the parameters list in the cpl_recipe object */
196  recipe->parameters = cpl_parameterlist_new() ;
197 
198 
199  /*
200  * Fill the parameter list.
201  */
202 
203  /* Wavelength calibration */
204  sinfo_general_config_add(recipe->parameters);
205  sinfo_product_config_add(recipe->parameters);
206  sinfo_prepare_stacked_frames_config_add(recipe->parameters);
207  sinfo_wavecal_config_add(recipe->parameters);
208  return 0;
209 
210 }
211 
212 /*---------------------------------------------------------------------------*/
218 /*---------------------------------------------------------------------------*/
219 
220 
221 static int sinfo_rec_wavecal_exec(cpl_plugin *plugin)
222 {
223 
224  cpl_recipe *recipe = (cpl_recipe *) plugin;
225 
226  /* Get the recipe out of the plugin */
227  if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
228  recipe = (cpl_recipe *)plugin ;
229  else return -1 ;
230 
231  return sinfo_rec_wavecal(recipe->parameters, recipe->frames);
232 
233 }
234 
235 /*---------------------------------------------------------------------------*/
241 /*---------------------------------------------------------------------------*/
242 static int sinfo_rec_wavecal_destroy(cpl_plugin *plugin)
243 {
244  cpl_recipe * recipe ;
245  /* Get the recipe out of the plugin */
246  if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
247  recipe = (cpl_recipe *)plugin ;
248  else return -1 ;
249 
250  cpl_parameterlist_delete(recipe->parameters);
251 
252  return 0;
253 
254 }
255 
256 /*---------------------------------------------------------------------------*/
263 /*---------------------------------------------------------------------------*/
264 /*
265  * The actual recipe actually start here.
266  */
267 
268 static int sinfo_rec_wavecal(cpl_parameterlist *config, cpl_frameset *set)
269 {
270 
271  int sw=0;
272  fake* fk=NULL;
273  cpl_parameter* p=NULL;
274  cpl_frameset* wrk_set=NULL;
275  cpl_frame* frame=NULL;
276  int ind_index=0;
277  cpl_frameset* ref_set=NULL;
278  int pdensity=0;
279  int line_cor=0;
280 
281  fk=sinfo_fake_new();
282 
283  sinfo_msg("Welcome to SINFONI Pipeline release %d.%d.%d",
284  SINFONI_MAJOR_VERSION,SINFONI_MINOR_VERSION,SINFONI_MICRO_VERSION);
285 
286 
287 
288  ck0(sinfo_dfs_set_groups(set),"Cannot indentify RAW and CALIB frames") ;
289  check_nomsg(p=cpl_parameterlist_find(config, "sinfoni.general.lc_sw"));
290  check_nomsg(line_cor=cpl_parameter_get_bool(p));
291  if(line_cor==1) {
292  check_nomsg(sinfo_ima_line_cor(config,set));
293  }
294 
295 
296  check_nomsg(ref_set=cpl_frameset_duplicate(set));
297 
298 
299  check_nomsg(p=cpl_parameterlist_find(config,"sinfoni.product.density"));
300  check_nomsg(pdensity=cpl_parameter_get_int(p));
301 
302  /* hidden parameters */
303  check_nomsg(p=cpl_parameterlist_find(config,
304  "sinfoni.wavecal.slitpos_boostrap"));
305  check_nomsg(sw=cpl_parameter_get_bool(p));
306  if( sw == 1 ) {
307  check_nomsg(wrk_set=cpl_frameset_duplicate(set));
308 
309  check_nomsg(p=cpl_parameterlist_find(config,"sinfoni.stacked.ind_index"));
310  check_nomsg(ind_index=cpl_parameter_get_bool(p));
311  check_nomsg(cpl_parameter_set_bool(p,TRUE));
312 
313  sinfo_msg("---------------------------------------");
314  sinfo_msg("%s FRAME DETERMINATION", PRO_WAVE_LAMP_STACKED);
315  sinfo_msg("---------------------------------------");
316  ck0(sinfo_new_prepare_stacked_frames(cpl_func,config, wrk_set, NULL,
317  PRO_WAVE_LAMP_STACKED,0,fk),
318  "FAILED STACKING FRAME %s",PRO_WAVE_LAMP_STACKED);
319 
320  sinfo_msg("%s FRAME DETERMINATION SUCCESS", PRO_WAVE_LAMP_STACKED);
321 
322  ck0(sinfo_new_wave_cal_slit2(cpl_func,config, wrk_set,ref_set),
323  "FIRST PART OF WAVELENGTH CALIBRATION FAILED") ;
324  sinfo_msg("FIRST PART OF WAVELENGTH CALIBRATION") ;
325 
326  check_nomsg(frame = cpl_frameset_find(wrk_set,PRO_SLIT_POS_GUESS));
327  check_nomsg(cpl_frameset_insert(set,cpl_frame_duplicate(frame)));
328  check_nomsg(p=cpl_parameterlist_find(config,"sinfoni.stacked.ind_index"));
329  if(ind_index == 1) {
330  check_nomsg(cpl_parameter_set_bool(p,TRUE));
331  check_nomsg(p=cpl_parameterlist_find(config,
332  "sinfoni.wavecal.slitpos_boostrap"));
333  check_nomsg(cpl_parameter_set_bool(p,TRUE));
334  sinfo_msg("Set ind_index to TRUE");
335  } else {
336  check_nomsg(cpl_parameter_set_bool(p,FALSE));
337  check_nomsg(p=cpl_parameterlist_find(config,
338  "sinfoni.wavecal.slitpos_boostrap"));
339  check_nomsg(cpl_parameter_set_bool(p,FALSE));
340  sinfo_msg("Set ind_index to FALSE");
341  }
342  sinfo_free_frameset(&wrk_set);
343  }
344 
345 
346  sinfo_msg("---------------------------------------------");
347  sinfo_msg("%s FRAME DETERMINATION", PRO_WAVE_LAMP_STACKED);
348  sinfo_msg("---------------------------------------------");
349 
350  /*
351  * Modified code begins here...
352  */
353  {
354  int nsky, ndark, nwave;
355 
356 
357 
358 
359 
360 
361  nsky = cpl_frameset_count_tags (set, RAW_SKY_NODDING);
362  nwave = cpl_frameset_count_tags (set, RAW_WAVE_LAMP);
363  ndark = cpl_frameset_count_tags (set, PRO_MASTER_DARK);
364 
365  if ( (nwave == 0) & (nsky>0) ) {
366  sinfo_msg ("No arc lamp frames found, using sky frames instead");
367  if (ndark == 0)
368  sinfo_msg ("Including Master dark is recommended when running wavecal on sky frames");
369 
370  int i = 0;
371  int non=0;
372  while (i < cpl_frameset_get_size(set)) {
373  cpl_frame* fr = cpl_frameset_get_frame(set,i);
374  const char* tag = cpl_frame_get_tag (fr);
375 
376  if (strcmp(tag, RAW_SKY_NODDING)==0)
377  {
378  cpl_frame * frnew = 0;
379  char name[32];
380  const char* fname = cpl_frame_get_filename(fr);
381  sprintf (&name[0], "fakelamp_%d.fits", non++);
382  cpl_image* im = cpl_image_load(fname, CPL_TYPE_FLOAT, 0, 0);
383  cpl_propertylist* plist = cpl_propertylist_load(fname,0);
384 
385  /* Both if these needs to be changed to make sure the sky frame is
386  interprated as ON-frame in sinfo_dfs.c
387 
388  The selection of KEY_NAME_LAMP_XE is arbitrary, but one of
389  KEY_NAME_LAMP_* has to be set */
390  cpl_propertylist_set_bool (plist, KEY_NAME_LAMP_XE, 1);
391  cpl_propertylist_set_string (plist, KEY_NAME_DPR_TYPE, "WAVE,LAMP");
392 
393  cpl_image_save (im, name, CPL_BPP_IEEE_FLOAT, plist, CPL_IO_DEFAULT);
394  cpl_image_delete (im);
395  cpl_propertylist_delete(plist);
396 
397  /* Looks like it's not possible to modify the frame directly
398  without duplicating it first. */
399  frnew = cpl_frame_duplicate (fr);
400  cpl_frame_set_filename(frnew, name);
401  cpl_frame_set_tag (frnew, RAW_WAVE_LAMP);
402  cpl_frameset_erase_frame (set, fr);
403  cpl_frameset_insert (set, frnew);
404  }
405  else
406  i++;
407  }
408  }
409 
410  /*
411  * Modified code ends here
412  */
413  }
414  ck0(sinfo_new_prepare_stacked_frames(cpl_func,config, set, NULL,
415  PRO_WAVE_LAMP_STACKED,0,fk),
416  "%s FRAME DETERMINATION FAILED",PRO_WAVE_LAMP_STACKED);
417  sinfo_msg("%s FRAME DETERMINATION SUCCESS", PRO_WAVE_LAMP_STACKED);
418 
419  sinfo_msg("---------------------------------------------");
420  sinfo_msg("WAVELENGTH CALIBRATION");
421  sinfo_msg("---------------------------------------------");
422 
423 
424  ck0(sinfo_new_wave_cal_slit2(cpl_func,config, set,ref_set),
425  "FAILED WAVELENGTH CALIBRATION");
426  sinfo_msg("SUCCESS WAVELENGTH CALIBRATION");
427 
428  sinfo_fake_delete(&fk);
429  if(pdensity < 2) {
430  check_nomsg(cpl_frameset_erase(set,PRO_STACK_MFLAT_DIST));
431  check_nomsg(cpl_frameset_erase(set,PRO_WAVE_LAMP_STACKED));
432  }
433 
434  cleanup:
435 
436  sinfo_free_frameset(&ref_set);
437  sinfo_free_frameset(&wrk_set);
438  sinfo_fake_delete(&fk);
439 
440 
441  if (cpl_error_get_code() != CPL_ERROR_NONE) {
442  return -1;
443  } else {
444  return 0;
445  }
446 
447 }
void irplib_reset(void)
Reset IRPLIB state.
int cpl_plugin_get_info(cpl_pluginlist *list)
Build the list of available plugins, for this module.