SINFONI Pipeline Reference Manual  2.6.0
sinfo_utl_skycor.c
1 /* $Id: sinfo_utl_skycor.c,v 1.14 2013-09-09 15:23:10 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: 2013-09-09 15:23:10 $
24  * $Revision: 1.14 $
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 #include <cpl.h>
37 #include <irplib_utils.h>
38 #include <sinfo_skycor.h>
39 #include <sinfo_skycor_config.h>
40 #include <sinfo_tpl_utils.h>
41 #include <sinfo_tpl_dfs.h>
42 #include <sinfo_pro_types.h>
43 #include <sinfo_utils_wrappers.h>
44 #include <sinfo_error.h>
45 #include <sinfo_msg.h>
46 #include <sinfo_pro_save.h>
47 #include <sinfo_globals.h>
48 /*-----------------------------------------------------------------------------
49  Defines
50  ----------------------------------------------------------------------------*/
51 
52 /*-----------------------------------------------------------------------------
53  Functions prototypes
54 -----------------------------------------------------------------------------*/
55 static int sinfo_utl_skycor_create(cpl_plugin *) ;
56 static int sinfo_utl_skycor_exec(cpl_plugin *) ;
57 static int sinfo_utl_skycor_destroy(cpl_plugin *) ;
58 static int sinfo_utl_skycor(cpl_parameterlist * config, cpl_frameset* set);
59 /*-----------------------------------------------------------------------------
60  Static variables
61  -----------------------------------------------------------------------------*/
62 
63 static char sinfo_utl_skycor_description[] =
64  "This recipe perform a correction of possible sky line residuals in the \n"
65  "object cube after standard data reduction.\n"
66  "Input frames are cubes with target and sky observations.\n"
67  "Their tags can be respectively OBS_OBJ (or OBS_PSF or OBS_STD) and OBS_SKY.\n"
68  "The output is a cube with same tag as the corresponding input target frame.\n"
69  "\n";
70 
71 
72 /*-----------------------------------------------------------------------------
73  Functions code
74  ----------------------------------------------------------------------------*/
75 
76 /*---------------------------------------------------------------------------*/
80 /*---------------------------------------------------------------------------*/
82 /*---------------------------------------------------------------------------*/
90 /*---------------------------------------------------------------------------*/
91 int cpl_plugin_get_info(cpl_pluginlist * list)
92 {
93  cpl_recipe * recipe = cpl_calloc(1, sizeof *recipe ) ;
94  cpl_plugin * plugin = &recipe->interface ;
95 
96  cpl_plugin_init(plugin,
97  CPL_PLUGIN_API,
98  SINFONI_BINARY_VERSION,
99  CPL_PLUGIN_TYPE_RECIPE,
100  "sinfo_utl_skycor",
101  "Sky lines residuals correction",
102  sinfo_utl_skycor_description,
103  "Andrea Modigliani",
104  "Andrea.Modigliani@eso.org",
105  sinfo_get_license(),
106  sinfo_utl_skycor_create,
107  sinfo_utl_skycor_exec,
108  sinfo_utl_skycor_destroy) ;
109 
110  cpl_pluginlist_append(list, plugin) ;
111  return 0;
112 }
113 
114 /*---------------------------------------------------------------------------*/
123 /*--------------------------------------------------------------------------*/
124 static int sinfo_utl_skycor_create(cpl_plugin * plugin)
125 {
126  cpl_recipe * recipe ;
127 
128  /* Get the recipe out of the plugin */
129  if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
130  recipe = (cpl_recipe *)plugin ;
131  else return -1 ;
132 
133  /* reset error handling */
134  irplib_reset();
135  /* Create the parameters list in the cpl_recipe object */
136  recipe->parameters = cpl_parameterlist_new() ;
137  sinfo_skycor_config_add(recipe->parameters);
138 
139  /* Return */
140  return 0;
141 }
142 
143 /*---------------------------------------------------------------------------*/
149 /*---------------------------------------------------------------------------*/
150 static int sinfo_utl_skycor_exec(cpl_plugin * plugin)
151 {
152  cpl_recipe * recipe ;
153  int code=0;
154  /* Get the recipe out of the plugin */
155  cpl_errorstate initial_errorstate = cpl_errorstate_get();
156 
157  if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
158  recipe = (cpl_recipe *)plugin ;
159  else return -1 ;
160 
161 
162  code = sinfo_utl_skycor(recipe->parameters, recipe->frames) ;
163 
164  if (!cpl_errorstate_is_equal(initial_errorstate)) {
165  /* Dump the error history since recipe execution start.
166  At this point the recipe cannot recover from the error */
167  cpl_errorstate_dump(initial_errorstate, CPL_FALSE, NULL);
168  }
169  return code;
170 }
171 
172 /*---------------------------------------------------------------------------*/
178 /*---------------------------------------------------------------------------*/
179 static int sinfo_utl_skycor_destroy(cpl_plugin * plugin)
180 {
181  cpl_recipe * recipe ;
182 
183  /* Get the recipe out of the plugin */
184  if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
185  recipe = (cpl_recipe *)plugin ;
186  else return -1 ;
187 
188  cpl_parameterlist_delete(recipe->parameters) ;
189  return 0 ;
190 }
191 
192 
199 /*---------------------------------------------------------------------------*/
200 static int
201 sinfo_utl_skycor(cpl_parameterlist * config, cpl_frameset* set)
202 {
203 
204  cpl_frame* obj_frm=NULL;
205  cpl_frame* sky_frm=NULL;
206  cpl_imagelist* obj_cor=NULL;
207  const char * name_o=NULL ;
208  cpl_frame* product_frame=NULL;
209 
210  cpl_propertylist* plist=NULL;
211  sinfo_skycor_qc* sqc=NULL;
212  char obj_tag[MAX_NAME_SIZE];
213  cpl_table* int_obj=NULL;
214 
215  sinfo_msg("Welcome to SINFONI Pipeline release %d.%d.%d",
216  SINFONI_MAJOR_VERSION,SINFONI_MINOR_VERSION,SINFONI_MICRO_VERSION);
217 
218 
219  ck0(sinfo_dfs_set_groups(set),"Cannot indentify RAW and CALIB frames") ;
220 
221 
222  // get input data
223  obj_frm=cpl_frameset_find(set,PRO_OBS_OBJ);
224  if(obj_frm == NULL) {
225  obj_frm=cpl_frameset_find(set,PRO_OBS_PSF);
226  strcpy(obj_tag,PRO_OBS_PSF);
227  } else {
228  strcpy(obj_tag,PRO_OBS_OBJ);
229  }
230 
231 
232  if(obj_frm == NULL) {
233  obj_frm=cpl_frameset_find(set,PRO_OBS_STD);
234  strcpy(obj_tag,PRO_OBS_STD);
235  }
236  cknull(obj_frm,"No %s or %s or %s frame found",
237  PRO_OBS_OBJ,PRO_OBS_PSF,PRO_OBS_STD);
238  check(sky_frm=cpl_frameset_find(set,PRO_OBS_SKY),
239  "No %s found",PRO_OBS_SKY);
240  sqc=sinfo_skycor_qc_new();
241 
242  check(plist=cpl_propertylist_load(cpl_frame_get_filename(obj_frm),0),
243  "Cannot read the FITS header") ;
244 
245  ck0(sinfo_skycor(config, obj_frm,sky_frm,sqc,&obj_cor,&int_obj),
246  "determining sky residuals corrected object");
247  sinfo_msg("Write out adjusted cube");
248  /* Set the file name */
249 
250  cpl_frameset_erase(set,obj_tag);
251  cpl_frameset_erase(set,PRO_OBS_SKY);
252  name_o = "out_obj_cor.fits" ;
253 
254 
255  /* Create product frame */
256  check_nomsg(product_frame = cpl_frame_new());
257  check_nomsg(cpl_frame_set_filename(product_frame, name_o)) ;
258  check_nomsg(cpl_frame_set_tag(product_frame, obj_tag)) ;
259  check_nomsg(cpl_frame_set_type(product_frame, CPL_FRAME_TYPE_IMAGE)) ;
260  check_nomsg(cpl_frame_set_group(product_frame, CPL_FRAME_GROUP_PRODUCT)) ;
261  check(cpl_frame_set_level(product_frame, CPL_FRAME_LEVEL_FINAL),
262  "Error while initialising the product frame") ;
263  // Add DataFlow keywords
264 
265  /*
266  check(plist=cpl_propertylist_load(name_o,0),
267  "Cannot read the FITS header") ;
268  check_nomsg(cpl_propertylist_erase_regexp(plist, "^ESO PRO CATG",0));
269  check(cpl_dfs_setup_product_header(plist,
270  product_frame,
271  set,
272  parlist,
273  "sinfo_utl_skycor",
274  "SINFONI",
275  KEY_VALUE_HPRO_DID),
276  "Problem in the product DFS-compliance") ;
277  */
278 
279  //save the file
280 
281  check(cpl_imagelist_save(obj_cor,
282  name_o,
283  CPL_BPP_IEEE_FLOAT,
284  plist,
285  CPL_IO_DEFAULT),
286  "Could not save product");
287  // Log the saved file in the input frameset
288  check_nomsg(cpl_frameset_insert(set,cpl_frame_duplicate(product_frame))) ;
289  sinfo_free_frame(&product_frame);
290  /*
291  ck0(sinfo_pro_save_ims(obj_cor,set,set,"out_obj_cor.fits",
292  PRO_OBS_OBJ,NULL,cpl_func,config),
293  "cannot dump cube %s", "obj_cub.fits");
294  */
295 
296 
297 
298 
299  sinfo_free_imagelist(&obj_cor);
300 
301 
302 
303  name_o = "out_obj_int.fits" ;
304 
305  /* Create product frame */
306  check_nomsg(product_frame = cpl_frame_new());
307  check_nomsg(cpl_frame_set_filename(product_frame, name_o)) ;
308  check_nomsg(cpl_frame_set_tag(product_frame, PRO_SPECTRA_QC)) ;
309  check_nomsg(cpl_frame_set_type(product_frame, CPL_FRAME_TYPE_TABLE)) ;
310  check_nomsg(cpl_frame_set_group(product_frame, CPL_FRAME_GROUP_PRODUCT)) ;
311  check(cpl_frame_set_level(product_frame, CPL_FRAME_LEVEL_FINAL),
312  "Error while initialising the product frame") ;
313  check_nomsg(cpl_propertylist_update_string(plist, "ESO PRO CATG",
314  PRO_SPECTRA_QC));
315 
316 
317  /* Add DataFlow keywords */
318  /*
319  check(plist=cpl_propertylist_load(name_o,0),
320  "Cannot read the FITS header") ;
321 
322  check_nomsg(cpl_propertylist_erase_regexp(plist, "^ESO PRO CATG",0));
323  check(cpl_dfs_setup_product_header(plist,
324  product_frame,
325  set,
326  parlist,
327  "sinfo_utl_skycor",
328  "SINFONI",
329  KEY_VALUE_HPRO_DID),
330  "Problem in the product DFS-compliance") ;
331  */
332 
333  /* Save the file */
334  check(cpl_table_save(int_obj, plist, NULL, name_o,CPL_IO_DEFAULT),
335  "Could not save product");
336  sinfo_free_propertylist(&plist) ;
337 
338  /* Log the saved file in the input frameset */
339  check_nomsg(cpl_frameset_insert(set, cpl_frame_duplicate(product_frame))) ;
340  sinfo_free_frame(&product_frame);
341 
342 
343 
344 
345  cleanup:
346  sinfo_free_imagelist(&obj_cor);
347  sinfo_free_table(&int_obj);
348  sinfo_skycor_qc_delete(&sqc);
349  sinfo_free_propertylist(&plist);
350  sinfo_free_frame(&product_frame);
351  if (cpl_error_get_code() != CPL_ERROR_NONE) {
352  return -1;
353  } else {
354  return 0;
355  }
356 
357 
358 }
void irplib_reset(void)
Reset IRPLIB state.
int cpl_plugin_get_info(cpl_pluginlist *list)
Build the list of available plugins, for this module.