SINFONI Pipeline Reference Manual  2.5.2
sinfo_utl_seds.c
1 /* $Id: sinfo_utl_seds.c,v 1.1 2008-06-11 13:42:20 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: 2008-06-11 13:42:20 $
24  * $Revision: 1.1 $
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 
36 #include <math.h>
37 #include <cpl.h>
38 
39 #include "irplib_utils.h"
40 
41 
42 #include "sinfo_raw_types.h"
43 #include "sinfo_pro_types.h"
44 #include "sinfo_tpl_utils.h"
45 #include "sinfo_tpl_dfs.h"
46 
47 /*
48 #include "isaac_utils.h"
49 #include "isaac_pfits.h"
50 #include "isaac_dfs.h"
51  */
52 
53 /*-----------------------------------------------------------------------------
54  Functions prototypes
55  -----------------------------------------------------------------------------*/
56 
57 static int sinfo_utl_seds_create(cpl_plugin *) ;
58 static int sinfo_utl_seds_exec(cpl_plugin *) ;
59 static int sinfo_utl_seds_destroy(cpl_plugin *) ;
60 static int sinfo_utl_seds(cpl_parameterlist *, cpl_frameset *) ;
61 static int sinfo_utl_seds_save(cpl_table *, cpl_parameterlist *,
62  cpl_frameset *) ;
63 
64 /*-----------------------------------------------------------------------------
65  Static variables
66  -----------------------------------------------------------------------------*/
67 
68 static char sinfo_utl_seds_description[] =
69  "sinfo_utl_seds -- SINFONI SEDS table creation.\n"
70  "The files listed in the Set Of Frames (sof-file) must be tagged:\n"
71  "raw-file.fits "SINFO_UTL_SEDS_RAW"\n" ;
72 
73 /*-----------------------------------------------------------------------------
74  Functions code
75  -----------------------------------------------------------------------------*/
76 
77 /*----------------------------------------------------------------------------*/
85 /*----------------------------------------------------------------------------*/
86 int cpl_plugin_get_info(cpl_pluginlist * list)
87 {
88  cpl_recipe * recipe = cpl_calloc(1, sizeof(*recipe)) ;
89  cpl_plugin * plugin = &recipe->interface ;
90 
91  cpl_plugin_init(plugin,
92  CPL_PLUGIN_API,
93  SINFONI_BINARY_VERSION,
94  CPL_PLUGIN_TYPE_RECIPE,
95  "sinfo_utl_seds",
96  "SEDS table creation",
97  sinfo_utl_seds_description,
98  "Yves Jung",
99  "yjung@eso.org",
100  sinfo_get_license(),
101  sinfo_utl_seds_create,
102  sinfo_utl_seds_exec,
103  sinfo_utl_seds_destroy) ;
104 
105  cpl_pluginlist_append(list, plugin) ;
106 
107  return 0;
108 }
109 
110 /*----------------------------------------------------------------------------*/
119 /*----------------------------------------------------------------------------*/
120 static int sinfo_utl_seds_create(cpl_plugin * plugin)
121 {
122  cpl_recipe * recipe ;
123 
124  /* Get the recipe out of the plugin */
125  if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
126  recipe = (cpl_recipe *)plugin ;
127  else return -1 ;
128 
129  /* Create the parameters list in the cpl_recipe object */
130  recipe->parameters = cpl_parameterlist_new() ;
131 
132  /* Return */
133  return 0;
134 }
135 
136 /*----------------------------------------------------------------------------*/
142 /*----------------------------------------------------------------------------*/
143 static int sinfo_utl_seds_exec(cpl_plugin * plugin)
144 {
145  cpl_recipe * recipe ;
146 
147  /* Get the recipe out of the plugin */
148  if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
149  recipe = (cpl_recipe *)plugin ;
150  else return -1 ;
151 
152  return sinfo_utl_seds(recipe->parameters, recipe->frames) ;
153 }
154 
155 /*----------------------------------------------------------------------------*/
161 /*----------------------------------------------------------------------------*/
162 static int sinfo_utl_seds_destroy(cpl_plugin * plugin)
163 {
164  cpl_recipe * recipe ;
165 
166  /* Get the recipe out of the plugin */
167  if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
168  recipe = (cpl_recipe *)plugin ;
169  else return -1 ;
170 
171  cpl_parameterlist_delete(recipe->parameters) ;
172  return 0 ;
173 }
174 
175 /*----------------------------------------------------------------------------*/
182 /*----------------------------------------------------------------------------*/
183 static int sinfo_utl_seds(
184  cpl_parameterlist * parlist,
185  cpl_frameset * framelist)
186 {
187  cpl_frame * cur_frame ;
188  cpl_frameset * rawframes ;
189  int nframes, nlines ;
190  cpl_table * out ;
191  cpl_bivector ** seds ;
192  char * name ;
193  cpl_vector * tmp_vec ;
194  int i, j ;
195 
196  /* Identify the RAW and CALIB frames in the input frameset */
197  if (sinfo_dfs_set_groups(framelist)) {
198  cpl_msg_error(__func__, "Cannot identify RAW and CALIB frames") ;
199  return -1 ;
200  }
201 
202  /* Retrieve raw frames */
203  if ((rawframes = sinfo_extract_frameset(framelist,
204  SINFO_UTL_SEDS_RAW)) == NULL) {
205  cpl_msg_error(__func__, "Cannot find raw frames in the input list") ;
206  return -1 ;
207  }
208  nframes = cpl_frameset_get_size(rawframes) ;
209 
210  /* Load */
211  seds = cpl_malloc(nframes * sizeof(cpl_bivector*)) ;
212  for (i=0 ; i<nframes ; i++) {
213  cur_frame = cpl_frameset_get_frame(rawframes, i) ;
214  if ((seds[i] = cpl_bivector_read(cpl_frame_get_filename(cur_frame)))
215  == NULL) {
216  cpl_msg_error(__func__, "Cannot load the sed %d", i+1) ;
217  for (j=0 ; j<i ; j++) cpl_bivector_delete(seds[j]) ;
218  cpl_free(seds) ;
219  cpl_frameset_delete(rawframes) ;
220  return -1 ;
221  }
222  }
223 
224  /* Verify the length */
225  nlines = cpl_bivector_get_size(seds[0]) ;
226  for (i=1 ; i<nframes ; i++) {
227  if (cpl_bivector_get_size(seds[i]) != nlines) {
228  cpl_msg_error(__func__, "Wrong SED size abort") ;
229  for (j=0 ; j<nframes ; j++) cpl_bivector_delete(seds[j]) ;
230  cpl_free(seds) ;
231  cpl_frameset_delete(rawframes) ;
232  return -1 ;
233  }
234  }
235 
236  /* Create the out table */
237  out = cpl_table_new(nlines) ;
238  tmp_vec = cpl_vector_duplicate(cpl_bivector_get_x(seds[0])) ;
239  cpl_table_wrap_double(out, cpl_vector_get_data(tmp_vec), "Wavelength") ;
240  cpl_vector_unwrap(tmp_vec) ;
241 
242  /* Fill the table */
243  for (i=0 ; i<nframes ; i++) {
244  /* Create the column */
245  cur_frame = cpl_frameset_get_frame(rawframes, i) ;
246  name = cpl_strdup(cpl_frame_get_filename(cur_frame)) ;
247  name[3] = (char)0 ;
248  tmp_vec = cpl_vector_duplicate(cpl_bivector_get_y(seds[i])) ;
249  cpl_table_wrap_double(out, cpl_vector_get_data(tmp_vec), name) ;
250  cpl_vector_unwrap(tmp_vec) ;
251  cpl_free(name) ;
252  }
253  cpl_frameset_delete(rawframes) ;
254  cpl_free(seds) ;
255 
256  /* Save the table */
257  if (sinfo_utl_seds_save(out, parlist, framelist) != 0) {
258  cpl_msg_error(__func__, "Cannot create product") ;
259  cpl_table_delete(out) ;
260  return -1 ;
261  }
262 
263  /* Free and return */
264  cpl_table_delete(out) ;
265  return 0 ;
266 }
267 
268 /*----------------------------------------------------------------------------*/
276 /*----------------------------------------------------------------------------*/
277 static int sinfo_utl_seds_save(
278  cpl_table * sed,
279  cpl_parameterlist * parlist,
280  cpl_frameset * set)
281 {
282  cpl_propertylist * plist ;
283 
284  /* Some keywords */
285  plist = cpl_propertylist_new() ;
286  cpl_propertylist_append_string(plist, "INSTRUME", "SINFO") ;
287 
288  /* Write the table */
289  cpl_dfs_save_table(set,
290  parlist,
291  set,
292  sed,
293  NULL,
294  "sinfo_utl_seds",
295  SINFO_UTL_SEDS_RES,
296  plist,
297  NULL,
298  PACKAGE "/" PACKAGE_VERSION,
299  "sinfo_utl_seds.fits") ;
300 
301  cpl_propertylist_delete(plist) ;
302 
303  return 0 ;
304 }
305 
306