UVES Pipeline Reference Manual  5.4.6
flames_utl_unpack.c
1 /* $Id: flames_utl_unpack.c,v 1.7 2010-09-24 09:30:44 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-09-24 09:30:44 $
24  * $Revision: 1.7 $
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 #include <uves_tpl_utils.h>
44 #include <uves_key_names.h>
45 #include <uves_pro_types.h>
46 #include <uves_functions.h>
47 */
48 
49 #include <flames_utils_science.h>
50 #include <flames_utils.h>
51 #include <uves_dfs.h>
52 #include <flames_dfs.h>
53 #include <uves_chip.h>
54 #include <uves_pfits.h>
55 #include <uves_msg.h>
56 #include <uves_error.h>
57 #include <uves_utils_wrappers.h>
58 
59 #define recipe_id "flames_utl_unpack"
60 
61 /*-----------------------------------------------------------------------------
62  Functions prototypes
63  ----------------------------------------------------------------------------*/
64 
65 static int flames_utl_unpack_create(cpl_plugin *) ;
66 static int flames_utl_unpack_exec(cpl_plugin *) ;
67 static int flames_utl_unpack_destroy(cpl_plugin *) ;
68 static int flames_utl_unpack(cpl_parameterlist *, cpl_frameset *) ;
69 
70 
71 static cpl_error_code
72 flames_unpack_spectra_from_image(cpl_frame* frm,
73  cpl_parameterlist * parameters,
74  cpl_frameset* set);
75 static cpl_error_code
76 flames_unpack_image_from_cube(cpl_frame* frm,
77  cpl_parameterlist * parameters,
78  cpl_frameset* set);
79 
80 /*-----------------------------------------------------------------------------
81  Static variables
82  ----------------------------------------------------------------------------*/
83 
84 static char flames_utl_unpack_description[] =
85 "This recipe unpack flames-uves packed products.\n"
86 "Information on relevant parameters can be found with\n"
87 "esorex --params flames_utl_unpack\n"
88 "esorex --help flames_utl_unpack\n"
89 "\n";
90 
91 /*-----------------------------------------------------------------------------
92  Functions code
93  ----------------------------------------------------------------------------*/
94 /*---------------------------------------------------------------------------*/
98 /*---------------------------------------------------------------------------*/
99 
101 /*---------------------------------------------------------------------------*/
109 /*---------------------------------------------------------------------------*/
110 int cpl_plugin_get_info(cpl_pluginlist * list)
111 {
112  cpl_recipe * recipe = cpl_calloc(1, sizeof *recipe ) ;
113  cpl_plugin * plugin = &recipe->interface ;
114 
115  cpl_plugin_init(plugin,
116  CPL_PLUGIN_API,
117  UVES_BINARY_VERSION,
118  CPL_PLUGIN_TYPE_RECIPE,
119  "flames_utl_unpack",
120  "Unpack flames-uves packed frames",
121  flames_utl_unpack_description,
122  "Andrea Modigliani",
123  "Andrea.Modigliani@eso.org",
128 
129  cpl_pluginlist_append(list, plugin) ;
130 
131  return 0;
132 }
133 
134 /*---------------------------------------------------------------------------*/
143 /*---------------------------------------------------------------------------*/
144 static int flames_utl_unpack_create(cpl_plugin * plugin)
145 {
146  cpl_recipe * recipe ;
147 
148  /* Get the recipe out of the plugin */
149  if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
150  recipe = (cpl_recipe *)plugin ;
151  else return -1 ;
152  cpl_error_reset();
153  irplib_reset();
154 
155  /* Create the parameters list in the cpl_recipe object */
156  recipe->parameters = cpl_parameterlist_new() ;
157 
158  /* Fill the parameters list */
159  /* --stropt */
160 
161  /* Return */
162  return 0;
163 }
164 
165 /*---------------------------------------------------------------------------*/
171 /*---------------------------------------------------------------------------*/
172 static int flames_utl_unpack_exec(cpl_plugin * plugin)
173 {
174  cpl_recipe * recipe ;
175  int code=0;
176  cpl_errorstate initial_errorstate = cpl_errorstate_get();
177 
178  /* Get the recipe out of the plugin */
179  if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
180  recipe = (cpl_recipe *)plugin ;
181  else return -1 ;
182  cpl_error_reset();
183  irplib_reset();
184  code = flames_utl_unpack(recipe->parameters, recipe->frames) ;
185 
186 
187  if (!cpl_errorstate_is_equal(initial_errorstate)) {
188  /* Dump the error history since recipe execution start.
189  At this point the recipe cannot recover from the error */
190  cpl_errorstate_dump(initial_errorstate, CPL_FALSE, NULL);
191  }
192 
193  return code ;
194 }
195 
196 /*---------------------------------------------------------------------------*/
202 /*---------------------------------------------------------------------------*/
203 static int flames_utl_unpack_destroy(cpl_plugin * plugin)
204 {
205  cpl_recipe * recipe ;
206 
207  /* Get the recipe out of the plugin */
208  if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
209  recipe = (cpl_recipe *)plugin ;
210  else return -1 ;
211 
212  cpl_parameterlist_delete(recipe->parameters) ;
213  return 0 ;
214 }
215 
216 /*---------------------------------------------------------------------------*/
223 /*---------------------------------------------------------------------------*/
224 static int
225 flames_utl_unpack( cpl_parameterlist * parlist,
226  cpl_frameset * set)
227 {
228  uves_propertylist * plist=NULL ;
229  int n=0;
230  int i=0;
231  int naxis=0;
232  cpl_frame* frm=NULL;
233  const char* tag=NULL;
234  const char* name=NULL;
235 
236  uves_msg("Welcome to UVES Pipeline release %d.%d.%d",
237  UVES_MAJOR_VERSION,UVES_MINOR_VERSION,UVES_MICRO_VERSION);
238 
239  /* HOW TO RETRIEVE INPUT PARAMETERS */
240  /* --stropt */
241 
242  /* Identify the RAW and CALIB frames in the input frameset */
243  //check(uves_dfs_set_groups(set),
244  // "Cannot identify RAW and CALIB frames") ;
245 
246  /* HOW TO ACCESS INPUT DATA */
247  n=cpl_frameset_get_size(set);
248  if(n<1) {
249  uves_msg_error("Empty input frame list!");
250  goto cleanup ;
251  }
252 
253  for(i=0;i<n;i++) {
254  check_nomsg(frm=cpl_frameset_get_frame(set,i));
255  check_nomsg(tag=cpl_frame_get_tag(frm));
256  check_nomsg(name=cpl_frame_get_filename(frm));
257  check_nomsg(plist=uves_propertylist_load(name,0));
258  check_nomsg(naxis=uves_pfits_get_naxis(plist));
259  //uves_msg("name=%s",name);
260  if(naxis==3) {
261  check_nomsg(flames_unpack_image_from_cube(frm,parlist,set));
262  } else if (naxis==2) {
263  check_nomsg(flames_unpack_spectra_from_image(frm,parlist,set));
264  } else {
265 
266  }
267 
268  uves_free_propertylist(&plist);
269  }
270 
271 
272  cleanup:
273 
274 
275  if (cpl_error_get_code()) {
276  return -1 ;
277  } else {
278  return 0 ;
279  }
280 
281 }
282 
283 
284 static cpl_error_code
285 flames_unpack_image_from_cube(cpl_frame* frm,
286  cpl_parameterlist * parameters,
287  cpl_frameset* set)
288 {
289 
290  const char* name=NULL;
291  const char* fname=NULL;
292  const char* tag=NULL;
293  uves_propertylist* href=NULL;
294  uves_propertylist* hout=NULL;
295  cpl_imagelist* iml=NULL;
296  int size=0;
297  int i=0;
298  cpl_frame* frame=NULL;
299  cpl_image* img=NULL;
300 
301  cknull_nomsg(parameters);
302  check_nomsg(name=cpl_frame_get_filename(frm));
303  check_nomsg(tag=cpl_frame_get_tag(frm));
304  check_nomsg(href=uves_propertylist_load(name,0));
306 
307  check_nomsg(iml=cpl_imagelist_load(name,CPL_TYPE_FLOAT,0));
308  check_nomsg(size=cpl_imagelist_get_size(iml));
309  for(i=0;i<size;i++) {
310  frame=cpl_frame_new();
311  check_nomsg(img=cpl_imagelist_get(iml,i));
312  check_nomsg(fname=uves_sprintf("slic_%d_%s",i,flames_get_basename(name)));
313  check_nomsg( uves_save_image(img,fname,hout,true, true) );
314 
315  check_nomsg(cpl_frame_set_filename(frame, fname)) ;
316  check_nomsg(cpl_frame_set_tag(frame, tag)) ;
317  check_nomsg(cpl_frame_set_type(frame, CPL_FRAME_TYPE_IMAGE)) ;
318  check_nomsg(cpl_frame_set_group(frame, CPL_FRAME_GROUP_PRODUCT)) ;
319  check(cpl_frame_set_level(frame, CPL_FRAME_LEVEL_FINAL),
320  "Error while initialising the product frame") ;
321 
322  cpl_frameset_insert(set,frame);
323  /*
324  check_nomsg(cpl_propertylist_erase_regexp(hout, "^ESO PRO CATG",0));
325  check(cpl_dfs_setup_product_header(hout,
326  frame,
327  set,
328  parameters,
329  "flames_utl_unpack",
330  PACKAGE "/" PACKAGE_VERSION,
331  "PRO-1.15"),
332  "Problem in the product DFS-compliance") ;
333 
334  check( flames_frameset_insert(
335  set,
336  CPL_FRAME_GROUP_PRODUCT,
337  CPL_FRAME_TYPE_IMAGE,
338  CPL_FRAME_LEVEL_INTERMEDIATE,
339  fname,
340  tag,
341  hout,
342  parameters,
343  recipe_id,
344  PACKAGE "/" PACKAGE_VERSION,
345  NULL, NULL, true, 0),
346  "Could not add image %s (%s) to frameset",
347  fname, tag);
348  */
349 
350  }
351 
352 
353  cleanup:
354  uves_free_propertylist(&href);
355  uves_free_propertylist(&hout);
356  uves_free_imagelist(&iml);
357 
358 
359  return cpl_error_get_code() ;
360 
361 }
362 
363 
364 
365 static cpl_error_code
366 flames_unpack_spectra_from_image(cpl_frame* frm,
367  cpl_parameterlist * parameters,
368  cpl_frameset* set)
369 {
370 
371 
372 
373  const char* name=NULL;
374  const char* fname=NULL;
375  const char* tag=NULL;
376  cpl_propertylist* href=NULL;
377  cpl_propertylist* hout=NULL;
378  cpl_image* ima=NULL;
379 
380  int i=0;
381  cpl_frame* frame=NULL;
382  cpl_image* img=NULL;
383  int sx=0;
384  int sy=0;
385 
386  cknull_nomsg(parameters);
387  check_nomsg(name=cpl_frame_get_filename(frm));
388  check_nomsg(tag=cpl_frame_get_tag(frm));
389  check_nomsg(href=cpl_propertylist_load(name,0));
390  check_nomsg(hout=cpl_propertylist_duplicate(href));
391 
392  check_nomsg(ima=cpl_image_load(name,CPL_TYPE_FLOAT,0,0));
393  check_nomsg(sx=cpl_image_get_size_x(ima));
394  check_nomsg(sy=cpl_image_get_size_y(ima));
395 
396  for(i=0;i<sy;i++) {
397  frame=cpl_frame_new();
398  //img=cpl_image_new(sx,1,CPL_TYPE_FLOAT);
399  check_nomsg(img=cpl_image_extract(ima,1,i+1,sx,i+1));
400  check_nomsg(fname=uves_sprintf("slic_%d_%s",i,flames_get_basename(name)));
401  cpl_image_save(img,fname,CPL_BPP_IEEE_FLOAT,hout,CPL_IO_DEFAULT);
402  //uves_msg("i=%d tag=%s name=%s",i,tag,name);
403 
404 
405 
406 
407  check_nomsg(cpl_frame_set_filename(frame, fname)) ;
408  check_nomsg(cpl_frame_set_tag(frame, tag)) ;
409  check_nomsg(cpl_frame_set_type(frame, CPL_FRAME_TYPE_IMAGE)) ;
410  check_nomsg(cpl_frame_set_group(frame, CPL_FRAME_GROUP_PRODUCT)) ;
411  check(cpl_frame_set_level(frame, CPL_FRAME_LEVEL_FINAL),
412  "Error while initialising the product frame") ;
413  cpl_frameset_insert(set,frame);
414 
415  /*
416  check_nomsg(cpl_propertylist_erase_regexp(hout, "^ESO PRO CATG",0));
417  check(cpl_dfs_setup_product_header(hout,
418  frame,
419  set,
420  parameters,
421  "flames_utl_unpack",
422  PACKAGE "/" PACKAGE_VERSION,
423  "PRO-1.15"),
424  "Problem in the product DFS-compliance") ;
425 
426 
427  check( flames_frameset_insert(
428  set,
429  CPL_FRAME_GROUP_PRODUCT,
430  CPL_FRAME_TYPE_IMAGE,
431  CPL_FRAME_LEVEL_INTERMEDIATE,
432  fname,
433  tag,
434  hout,
435  parameters,
436  "pippo",
437  PACKAGE "/" PACKAGE_VERSION,
438  NULL, NULL, true, 0),
439  "Could not add image %s (%s) to frameset",
440  fname, tag);
441 
442  */
443  uves_free_image(&img);
444 
445  }
446 
447 
448  cleanup:
449  cpl_propertylist_delete(href);
450  cpl_propertylist_delete(hout);
451  uves_free_image(&ima);
452  uves_free_image(&img);
453 
454  return cpl_error_get_code() ;
455 
456 
457 }
458 
459 
460 
461 
#define uves_msg_error(...)
Print an error message.
Definition: uves_msg.h:64
static int flames_utl_unpack(cpl_parameterlist *, cpl_frameset *)
Get the command line options and execute the data reduction.
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_pfits_get_naxis(const uves_propertylist *plist)
Find out the NAXIS.
Definition: uves_pfits.c:2259
#define uves_msg(...)
Print a message on 'info' or 'debug' level.
Definition: uves_msg.h:119
uves_propertylist * uves_propertylist_load(const char *name, int position)
Create a property list from a file.
static int flames_utl_unpack_create(cpl_plugin *)
Setup the recipe options.
uves_propertylist * uves_propertylist_duplicate(const uves_propertylist *self)
Create a copy of the given property list.
#define check(CMD,...)
Definition: uves_error.h:198
static int flames_utl_unpack_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 flames_utl_unpack_destroy(cpl_plugin *)
Destroy what has been created by the 'create' function.