SINFONI Pipeline Reference Manual  2.6.0
sinfo_utl_table_ex.c
1 /* $Id: sinfo_utl_table_ex.c,v 1.1 2009-05-20 15:22:42 kmirny 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: kmirny $
23  * $Date: 2009-05-20 15:22:42 $
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 #include <string.h>
36 
37 /* cpl */
38 #include <cpl.h>
39 
40 /* irplib */
41 #include <irplib_utils.h>
42 
43 #include <sinfo_tpl_utils.h>
44 #include <sinfo_pfits.h>
45 #include <sinfo_tpl_dfs.h>
46 #include <sinfo_key_names.h>
47 #include <sinfo_pro_types.h>
48 #include <sinfo_functions.h>
49 #include <sinfo_msg.h>
50 #include <sinfo_error.h>
51 #include <sinfo_utils_wrappers.h>
52 
53 /*-----------------------------------------------------------------------------
54  Functions prototypes
55  ----------------------------------------------------------------------------*/
56 
57 static int sinfo_utl_table_ex_create(cpl_plugin *) ;
58 static int sinfo_utl_table_ex_exec(cpl_plugin *) ;
59 static int sinfo_utl_table_ex_destroy(cpl_plugin *) ;
60 static int sinfo_utl_table_ex(cpl_parameterlist *, cpl_frameset *) ;
61 
62 /*-----------------------------------------------------------------------------
63  Static variables
64  ----------------------------------------------------------------------------*/
65 
66 static char sinfo_utl_table_ex_description[] =
67  "This recipe produce a table,\n";
68 
69 /*static char PARAM_NAME_SIZE_X[] = "sinfoni.sinfo_utl_table_ex.size_x";
70 static char PARAM_NAME_SIZE_Y[] = "sinfoni.sinfo_utl_table_ex.size_y";
71 static char PARAM_NAME_FWHM_X[] = "sinfoni.sinfo_utl_table_ex.fwhm_x";
72 static char PARAM_NAME_FWHM_Y[] = "sinfoni.sinfo_utl_table_ex.fwhm_y";
73 static char PARAM_NAME_SIGNAL[] = "sinfoni.sinfo_utl_table_ex.total_signal";
74 static char PARAM_NAME_BKGSIG[] = "sinfoni.sinfo_utl_table_ex.bkg_signal";
75 static char PARAM_NAME_NOISEL[] = "sinfoni.sinfo_utl_table_ex.noise_level";
76  */
77 static char RECIPE_NAME[] = "sinfoni.sinfo_utl_table_ex";
78 static char CLMN_NAME_SPECTRUM[] = "spectrum";
79 static char CLMN_NAME_WAVELENGTH[] = "wavelength";
80 static char CLMN_NAME_BKG_TOT[] = "bkg_tot";
81 static char CLMN_NAME_COUNTS_BKG[] = "counts_bkg";
82 /*-----------------------------------------------------------------------------
83  Functions code
84  ----------------------------------------------------------------------------*/
85 /*---------------------------------------------------------------------------*/
89 /*---------------------------------------------------------------------------*/
90 
92 /*---------------------------------------------------------------------------*/
100 /*---------------------------------------------------------------------------*/
101 int cpl_plugin_get_info(cpl_pluginlist * list)
102 {
103  cpl_recipe * recipe = cpl_calloc(1, sizeof *recipe ) ;
104  cpl_plugin * plugin = &recipe->interface ;
105 
106  cpl_plugin_init(plugin,
107  CPL_PLUGIN_API,
108  SINFONI_BINARY_VERSION,
109  CPL_PLUGIN_TYPE_RECIPE,
110  "sinfo_utl_table_ex",
111  "Test example shows table manipulating utilities",
112  sinfo_utl_table_ex_description,
113  "Konstantin Mirny",
114  "kmirny@eso.org",
115  sinfo_get_license(),
116  sinfo_utl_table_ex_create,
117  sinfo_utl_table_ex_exec,
118  sinfo_utl_table_ex_destroy) ;
119 
120  cpl_pluginlist_append(list, plugin) ;
121 
122  return 0;
123 }
124 
125 /*---------------------------------------------------------------------------*/
134 /*---------------------------------------------------------------------------*/
135 static int sinfo_utl_table_ex_create(cpl_plugin * plugin)
136 {
137  cpl_recipe * recipe ;
138  // cpl_parameter * p ;
139 
140  /* Get the recipe out of the plugin */
141  if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
142  recipe = (cpl_recipe *)plugin ;
143  else return -1 ;
144  cpl_error_reset();
145  irplib_reset();
146 
147  /* Create the parameters list in the cpl_recipe object */
148  recipe->parameters = cpl_parameterlist_new() ;
149 
150  /* Fill the parameters list */
151  /* --stropt */
152  /* p = cpl_parameter_new_value("sinfoni.sinfo_utl_table_ex.op",
153  CPL_TYPE_STRING,
154  "A possible operation",
155  "sinfoni.sinfo_utl_table_ex","+");
156  cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "op") ;
157  cpl_parameterlist_append(recipe->parameters, p) ;*/
158 
159  /* --size_x */
160  /* p = cpl_parameter_new_value(PARAM_NAME_SIZE_X,
161  CPL_TYPE_INT, "size X axis", RECIPE_NAME, SIZE_X_DEFAULT) ;
162  cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "size_x") ;
163  cpl_parameterlist_append(recipe->parameters, p) ;
164  */
165  /* Return */
166  return 0;
167 }
168 
169 /*---------------------------------------------------------------------------*/
175 /*---------------------------------------------------------------------------*/
176 static int sinfo_utl_table_ex_exec(cpl_plugin * plugin)
177 {
178  cpl_recipe * recipe ;
179  int code=0;
180  cpl_errorstate initial_errorstate = cpl_errorstate_get();
181 
182  /* Get the recipe out of the plugin */
183  if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
184  recipe = (cpl_recipe *)plugin ;
185  else return -1 ;
186  cpl_error_reset();
187  irplib_reset();
188  code = sinfo_utl_table_ex(recipe->parameters, recipe->frames) ;
189 
190 
191  if (!cpl_errorstate_is_equal(initial_errorstate)) {
192  /* Dump the error history since recipe execution start.
193  At this point the recipe cannot recover from the error */
194  cpl_errorstate_dump(initial_errorstate, CPL_FALSE, NULL);
195  }
196 
197  return code ;
198 }
199 
200 /*---------------------------------------------------------------------------*/
206 /*---------------------------------------------------------------------------*/
207 static int sinfo_utl_table_ex_destroy(cpl_plugin * plugin)
208 {
209  cpl_recipe * recipe ;
210 
211  /* Get the recipe out of the plugin */
212  if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
213  recipe = (cpl_recipe *)plugin ;
214  else return -1 ;
215 
216  cpl_parameterlist_delete(recipe->parameters) ;
217  return 0 ;
218 }
219 
220 /*---------------------------------------------------------------------------*/
227 /*---------------------------------------------------------------------------*/
228 static int
229 sinfo_utl_table_ex( cpl_parameterlist * parlist,
230  cpl_frameset * framelist)
231 {
232  // parameters
233  double range_min = 1.9;
234  double range_max = 2.;
236 
237  const char * name_o = NULL ;
238  const char * sourceTableName = NULL;
239  // cpl_parameter * param = NULL ;
240 
241  cpl_propertylist * plist = NULL ;
242  cpl_frame * product_frame = NULL;
243  int nrow = 0; // number of rows in the table
244  cpl_table * tblSource = NULL; // source table
245  cpl_table * tblResult = NULL; // result table
246  float * pValues = NULL; // source column
247 
248  sinfo_msg("Welcome to SINFONI Pipeline release %d.%d.%d",
249  SINFONI_MAJOR_VERSION,SINFONI_MINOR_VERSION,SINFONI_MICRO_VERSION);
250  ck0(sinfo_dfs_set_groups(framelist),"Cannot indentify RAW and CALIB frames");
251 
252  /* HOW TO RETRIEVE INPUT PARAMETERS */
253  /* --size_x */
254  // check_nomsg(param=cpl_parameterlist_find(parlist,
255  // PARAM_NAME_SIZE_X));
256  // check_nomsg(iSizeX=cpl_parameter_get_int(param));
257 
258 
259 
260 
261  /* HOW TO GET THE VALUE OF A FITS KEYWORD */
262  check(plist=cpl_propertylist_new(),"Cannot create a Property List");
263 
264  /* Now performing the data reduction */
265  /* Let's generate one image for the example */
266 
267  // get the source table
268  cpl_frameset_iterator* it = cpl_frameset_iterator_new(framelist);
269  cpl_frame *product_frame = cpl_frameset_iterator_get(it);
270 
271  check(sourceTableName = cpl_frame_get_filename(product_frame),"Cannot get source table name");
272  check_nomsg(tblSource = cpl_table_load(sourceTableName, 1, 0));
273  // generate result table
274  // 1. duplicate the table
275  check(tblResult = cpl_table_duplicate(tblSource), "cannot duplicate table");
276  // 2. Cycle through the table - select only predefined interval
277  check_nomsg(nrow = cpl_table_get_nrow(tblResult));
278  sinfo_msg("%d rows in the source table", nrow);
279  if (nrow)
280  {
281  int i = 0;
282  int m = 0;
283  check_nomsg(pValues = cpl_table_get_data_float(tblResult, CLMN_NAME_WAVELENGTH));
284  sinfo_msg("checking wavelenghts...");
285  check(cpl_table_unselect_all(tblResult),"cannot unselect all");
286  for (i = 0; i < nrow; ++i)
287  {
288  if ((*(pValues + i) < range_min) || (*(pValues + i) > range_max))
289  {
290  // mark for removing
291  check(cpl_table_select_row(tblResult, i), "Cannot select a row in a table");
292  ++m;
293  }
294  }
295  sinfo_msg("%d row(s) would be removed from the result", m);
296  // delete select rows from the table
297  check(cpl_table_erase_selected(tblResult),"Cannot delete selected rows from a table");
298  }
299  // 3. create a new column for the result table
300  // 4. Fill in the new column with values
301  check(cpl_table_duplicate_column(tblResult,CLMN_NAME_SPECTRUM, tblResult, CLMN_NAME_BKG_TOT),"Unable to create new column in the table");
302  check(cpl_table_add_columns(tblResult, CLMN_NAME_SPECTRUM, CLMN_NAME_COUNTS_BKG), "Error during sum the columns");
303 
304  /* Set the file name */
305  name_o = "ima_res.fits" ;
306  /* Create product frame */
307 
308  check_nomsg(product_frame = cpl_frame_new());
309  check_nomsg(cpl_frame_set_filename(product_frame, name_o)) ;
310  check_nomsg(cpl_frame_set_tag(product_frame,"table_example" )) ;
311  check_nomsg(cpl_frame_set_type(product_frame, CPL_FRAME_TYPE_TABLE)) ;
312  check_nomsg(cpl_frame_set_group(product_frame, CPL_FRAME_GROUP_PRODUCT)) ;
313  check(cpl_frame_set_level(product_frame, CPL_FRAME_LEVEL_FINAL),
314  "Error while initialising the product frame") ;
315 
316  /* Add DataFlow keywords */
317  check_nomsg(cpl_propertylist_erase_regexp(plist, "^ESO PRO CATG",0));
318  // cpl_frameset_dump(framelist, stdout);
319  check(cpl_dfs_setup_product_header(plist,
320  product_frame,
321  framelist,
322  parlist,
323  "sinfo_utl_table_ex",
324  "SINFONI",
325  KEY_VALUE_HPRO_DID,NULL),
326  "Problem in the product DFS-compliance") ;
327 
328 
329  /* Save the file */
330  cpl_table_dump(tblResult, 0, 3, stdout);
331  check(cpl_table_save(tblResult,
332  NULL, NULL,
333  name_o,
334  CPL_IO_DEFAULT), "Cannot save table");
335 
336  sinfo_free_propertylist(&plist) ;
337  /* Log the saved file in the input frameset */
338  check_nomsg(cpl_frameset_insert(framelist, product_frame)) ;
339 
340 
341  cleanup:
342  cpl_frameset_iterator_delete(it);
343  sinfo_free_propertylist(&plist) ;
344  cpl_table_delete(tblSource);
345  cpl_table_delete(tblResult);
346  /* This is usually freed by esorex: but what about if errors occurs?
347  sinfo_free_frame(&product_frame) ;
348  */
349 
350 
351  return ((cpl_error_get_code()) ? -1 : 0 );
352 
353 }
void irplib_reset(void)
Reset IRPLIB state.
int cpl_plugin_get_info(cpl_pluginlist *list)
Build the list of available plugins, for this module.