SINFONI Pipeline Reference Manual  2.6.0
sinfo_utl_ima_gauss.c
1 /* $Id: sinfo_utl_ima_gauss.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_ima_gauss_create(cpl_plugin *) ;
58 static int sinfo_utl_ima_gauss_exec(cpl_plugin *) ;
59 static int sinfo_utl_ima_gauss_destroy(cpl_plugin *) ;
60 static int sinfo_utl_ima_gauss(cpl_parameterlist *, cpl_frameset *) ;
61 
62 /*-----------------------------------------------------------------------------
63  Static variables
64  ----------------------------------------------------------------------------*/
65 
66 static char sinfo_utl_ima_gauss_description[] =
67  "This recipe produce an image,\n"
68  "containing a 2D Gaussian.\n"
69  "\n";
70 
71 static char PARAM_NAME_SIZE_X[] = "sinfoni.sinfo_utl_ima_gauss.size_x";
72 static char PARAM_NAME_SIZE_Y[] = "sinfoni.sinfo_utl_ima_gauss.size_y";
73 static char PARAM_NAME_FWHM_X[] = "sinfoni.sinfo_utl_ima_gauss.fwhm_x";
74 static char PARAM_NAME_FWHM_Y[] = "sinfoni.sinfo_utl_ima_gauss.fwhm_y";
75 static char PARAM_NAME_SIGNAL[] = "sinfoni.sinfo_utl_ima_gauss.total_signal";
76 static char PARAM_NAME_BKGSIG[] = "sinfoni.sinfo_utl_ima_gauss.bkg_signal";
77 static char PARAM_NAME_NOISEL[] = "sinfoni.sinfo_utl_ima_gauss.noise_level";
78 
79 static char RECIPE_NAME[] = "sinfoni.sinfo_utl_ima_gauss";
80 
81 /*-----------------------------------------------------------------------------
82  Functions code
83  ----------------------------------------------------------------------------*/
84 /*---------------------------------------------------------------------------*/
88 /*---------------------------------------------------------------------------*/
89 
91 /*---------------------------------------------------------------------------*/
99 /*---------------------------------------------------------------------------*/
100 int cpl_plugin_get_info(cpl_pluginlist * list)
101 {
102  cpl_recipe * recipe = cpl_calloc(1, sizeof *recipe ) ;
103  cpl_plugin * plugin = &recipe->interface ;
104 
105  cpl_plugin_init(plugin,
106  CPL_PLUGIN_API,
107  SINFONI_BINARY_VERSION,
108  CPL_PLUGIN_TYPE_RECIPE,
109  "sinfo_utl_ima_gauss",
110  "Produce an image containing 2D Gaussian",
111  sinfo_utl_ima_gauss_description,
112  "Konstantin Mirny",
113  "kmirny@eso.org",
114  sinfo_get_license(),
115  sinfo_utl_ima_gauss_create,
116  sinfo_utl_ima_gauss_exec,
117  sinfo_utl_ima_gauss_destroy) ;
118 
119  cpl_pluginlist_append(list, plugin) ;
120 
121  return 0;
122 }
123 
124 /*---------------------------------------------------------------------------*/
133 /*---------------------------------------------------------------------------*/
134 static int sinfo_utl_ima_gauss_create(cpl_plugin * plugin)
135 {
136  const int SIZE_X_DEFAULT = 1024;
137  const int SIZE_Y_DEFAULT = 1024;
138  const int FWHM_X = 100;
139  const int FWHM_Y = 100;
140  const double TOTAL_SIGNAL = 10E5;
141  const double BKG_SIGNAL = 400.;
142  const double NOISE_VALUE = 20.;
143 
144  cpl_recipe * recipe ;
145  cpl_parameter * p ;
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  cpl_error_reset();
152  irplib_reset();
153 
154  /* Create the parameters list in the cpl_recipe object */
155  recipe->parameters = cpl_parameterlist_new() ;
156 
157  /* Fill the parameters list */
158  /* --stropt */
159  /* p = cpl_parameter_new_value("sinfoni.sinfo_utl_ima_gauss.op",
160  CPL_TYPE_STRING,
161  "A possible operation",
162  "sinfoni.sinfo_utl_ima_gauss","+");
163  cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "op") ;
164  cpl_parameterlist_append(recipe->parameters, p) ;*/
165 
166  /* --size_x */
167  p = cpl_parameter_new_value(PARAM_NAME_SIZE_X,
168  CPL_TYPE_INT, "size X axis", RECIPE_NAME, SIZE_X_DEFAULT) ;
169  cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "size_x") ;
170  cpl_parameterlist_append(recipe->parameters, p) ;
171 
172  /* --size_y */
173  p = cpl_parameter_new_value(PARAM_NAME_SIZE_Y,
174  CPL_TYPE_INT, "size Y axis", RECIPE_NAME, SIZE_Y_DEFAULT) ;
175  cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "size_y") ;
176  cpl_parameterlist_append(recipe->parameters, p) ;
177 
178  /* --fwhm_x */
179  p = cpl_parameter_new_value(PARAM_NAME_FWHM_X,
180  CPL_TYPE_INT, "FWHM X axis", RECIPE_NAME, FWHM_X) ;
181  cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "fwhm_x") ;
182  cpl_parameterlist_append(recipe->parameters, p) ;
183 
184  /* --fwhm_y */
185  p = cpl_parameter_new_value(PARAM_NAME_FWHM_Y,
186  CPL_TYPE_INT, "FWHM Y axis", RECIPE_NAME, FWHM_Y) ;
187  cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "fwhm_y") ;
188  cpl_parameterlist_append(recipe->parameters, p) ;
189 
190  /* --total_signal */
191  p = cpl_parameter_new_value(PARAM_NAME_SIGNAL,
192  CPL_TYPE_DOUBLE, "Total signal value", RECIPE_NAME, TOTAL_SIGNAL) ;
193  cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "total_signal") ;
194  cpl_parameterlist_append(recipe->parameters, p) ;
195 
196  /* --bkg_signal */
197  p = cpl_parameter_new_value(PARAM_NAME_BKGSIG,
198  CPL_TYPE_DOUBLE, "background signal level", RECIPE_NAME, BKG_SIGNAL) ;
199  cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "bkg_signal") ;
200  cpl_parameterlist_append(recipe->parameters, p) ;
201 
202  /* --noise_level */
203  p = cpl_parameter_new_value(PARAM_NAME_NOISEL,
204  CPL_TYPE_DOUBLE, "Noise level", RECIPE_NAME, NOISE_VALUE) ;
205  cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "noise_level") ;
206  cpl_parameterlist_append(recipe->parameters, p) ;
207  /* Return */
208  return 0;
209 }
210 
211 /*---------------------------------------------------------------------------*/
217 /*---------------------------------------------------------------------------*/
218 static int sinfo_utl_ima_gauss_exec(cpl_plugin * plugin)
219 {
220  cpl_recipe * recipe ;
221  int code=0;
222  cpl_errorstate initial_errorstate = cpl_errorstate_get();
223 
224  /* Get the recipe out of the plugin */
225  if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
226  recipe = (cpl_recipe *)plugin ;
227  else return -1 ;
228  cpl_error_reset();
229  irplib_reset();
230  code = sinfo_utl_ima_gauss(recipe->parameters, recipe->frames) ;
231 
232 
233  if (!cpl_errorstate_is_equal(initial_errorstate)) {
234  /* Dump the error history since recipe execution start.
235  At this point the recipe cannot recover from the error */
236  cpl_errorstate_dump(initial_errorstate, CPL_FALSE, NULL);
237  }
238 
239  return code ;
240 }
241 
242 /*---------------------------------------------------------------------------*/
248 /*---------------------------------------------------------------------------*/
249 static int sinfo_utl_ima_gauss_destroy(cpl_plugin * plugin)
250 {
251  cpl_recipe * recipe ;
252 
253  /* Get the recipe out of the plugin */
254  if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
255  recipe = (cpl_recipe *)plugin ;
256  else return -1 ;
257 
258  cpl_parameterlist_delete(recipe->parameters) ;
259  return 0 ;
260 }
261 
262 /*---------------------------------------------------------------------------*/
269 /*---------------------------------------------------------------------------*/
270 static int
271 sinfo_utl_ima_gauss( cpl_parameterlist * parlist,
272  cpl_frameset * framelist)
273 {
274  // parameters
275  int iSizeX = 0;
276  int iSizeY = 0;
277  int iFWHMX = 0;
278  int iFWHMY = 0;
279  double dSignal = 0;
280  double dBkgSignal = 0;
281  double dNoiseLvl = 0;
283 
284  const char * name_o = NULL ;
285  cpl_parameter * param = NULL ;
286 
287  cpl_propertylist * plist = NULL ;
288  cpl_image * imNoise = NULL ;
289  cpl_image * imGauss = NULL ;
290  cpl_image * imResult = NULL;
291  cpl_frame * product_frame = NULL;
292  const double K = 2.35482;//1. / (2. * sqrt(2.* ln(2.)));
293 
294  sinfo_msg("Welcome to SINFONI Pipeline release %d.%d.%d",
295  SINFONI_MAJOR_VERSION,SINFONI_MINOR_VERSION,SINFONI_MICRO_VERSION);
296  ck0(sinfo_dfs_set_groups(framelist),"Cannot indentify RAW and CALIB frames");
297 
298  /* HOW TO RETRIEVE INPUT PARAMETERS */
299  /* --size_x */
300  check_nomsg(param=cpl_parameterlist_find(parlist,
301  PARAM_NAME_SIZE_X));
302  check_nomsg(iSizeX=cpl_parameter_get_int(param));
303 
304  /* --size_y */
305  check_nomsg(param=cpl_parameterlist_find(parlist,
306  PARAM_NAME_SIZE_Y));
307  check_nomsg(iSizeY=cpl_parameter_get_int(param));
308 
309  /* --fwhm_x */
310  check_nomsg(param=cpl_parameterlist_find(parlist,
311  PARAM_NAME_FWHM_X));
312  check_nomsg(iFWHMX=cpl_parameter_get_int(param));
313 
314  /* --fwhm_y */
315  check_nomsg(param=cpl_parameterlist_find(parlist,
316  PARAM_NAME_FWHM_Y));
317  check_nomsg(iFWHMY=cpl_parameter_get_int(param));
318 
319  /* --total_signal */
320  check_nomsg(param=cpl_parameterlist_find(parlist,
321  PARAM_NAME_SIGNAL));
322  check_nomsg(dSignal=cpl_parameter_get_double(param));
323 
324  /* --bkg_signal */
325  check_nomsg(param=cpl_parameterlist_find(parlist,
326  PARAM_NAME_BKGSIG));
327  check_nomsg(dBkgSignal=cpl_parameter_get_double(param));
328 
329  /* --noise_level */
330  check_nomsg(param=cpl_parameterlist_find(parlist,
331  PARAM_NAME_NOISEL));
332  check_nomsg(dNoiseLvl=cpl_parameter_get_double(param));
333 
334  /* Identify the RAW and CALIB frames in the input frameset */
335  // check(sinfo_dfs_set_groups(framelist),
336  // "Cannot identify RAW and CALIB frames") ;
337 
338  /* HOW TO ACCESS INPUT DATA */
339  /* n=cpl_frameset_get_size(framelist);
340  if(n<1) {
341  sinfo_msg_error("Empty input frame list!");
342  goto cleanup ;
343  }
344  */
345  /* HOW TO ACCESS INPUT DATA */
346  // check_nomsg(raw_set=cpl_frameset_new());
347  /*
348  check(sinfo_contains_frames_kind(framelist,raw_set,PRO_IMA),
349  "Found no input frames with tag %s",PRO_IMA);
350  check_nomsg(nraw=cpl_frameset_get_size(raw_set));
351  if (nraw<1) {
352  sinfo_msg_error("Found no input frames with tag %s",PRO_IMA);
353  goto cleanup;
354  } else {
355  check_nomsg(frm_ima1=cpl_frameset_get_frame(framelist,0));
356  check_nomsg(ima1=cpl_image_load(cpl_frame_get_filename(frm_ima1),
357  CPL_TYPE_FLOAT,0,0));
358  if (nraw>1) {
359  check_nomsg(frm_ima2=cpl_frameset_get_frame(framelist,1));
360  check_nomsg(ima2 = cpl_image_load(cpl_frame_get_filename(frm_ima2),
361  CPL_TYPE_FLOAT,0,0));
362  switch_ima2=1;
363  } else if (value == 9999.) {
364  sinfo_msg_error("Found only one input frames with tag %s",PRO_IMA);
365  goto cleanup;
366  } else {
367  sinfo_msg("Produce an image %s",PRO_IMA);
368  }
369  }
370 
371  sinfo_free_frameset(&raw_set);
372  */
373  /* HOW TO GET THE VALUE OF A FITS KEYWORD */
374  check(plist=cpl_propertylist_new(),"Cannot create a Property List");
375 
376  /* Now performing the data reduction */
377  /* Let's generate one image for the example */
378 
379 
380  check(imNoise = cpl_image_fill_test_create(iSizeX, iSizeY),
381  "Cannot generate the image") ;
382  check_nomsg(cpl_image_fill_noise_uniform(imNoise, dBkgSignal - dNoiseLvl, dBkgSignal + dNoiseLvl));
383  check(imGauss = cpl_image_fill_test_create(iSizeX, iSizeY),
384  "Cannot generate the image") ;
385  // double dfwhm_x = iFWHMX;
386  // double dfwhm_y = iFWHMY;
387  // double xcen = 0;
388  // double ycen = 0;
389  // check_nomsg(cpl_image_fit_gaussian(imNoise, iSizeX/2, iSizeY/2, 5, &dSignal, &xcen, &ycen, 0, 0, &dfwhm_y, &dfwhm_y));
390  check(imResult = cpl_image_fill_test_create(iSizeX, iSizeY),
391  "Cannot generate the image") ;
392  check_nomsg(cpl_image_fill_gaussian(imGauss, iSizeX/2, iSizeY/2, dSignal, iFWHMX / K, iFWHMY / K));
393  check(imResult = cpl_image_add_create(imGauss, imNoise), "Cannot generate the image");
394  /* HOW TO SAVE A PRODUCT ON DISK */
395  /* Set the file name */
396  name_o = "ima_res.fits" ;
397 
398  /* Create product frame */
399  check_nomsg(product_frame = cpl_frame_new());
400  check_nomsg(cpl_frame_set_filename(product_frame, name_o)) ;
401  check_nomsg(cpl_frame_set_tag(product_frame,"image_gauss" )) ;
402  check_nomsg(cpl_frame_set_type(product_frame, CPL_FRAME_TYPE_IMAGE)) ;
403  check_nomsg(cpl_frame_set_group(product_frame, CPL_FRAME_GROUP_PRODUCT)) ;
404  check(cpl_frame_set_level(product_frame, CPL_FRAME_LEVEL_FINAL),
405  "Error while initialising the product frame") ;
406 
407  /* Add DataFlow keywords */
408  check_nomsg(cpl_propertylist_erase_regexp(plist, "^ESO PRO CATG",0));
409  // cpl_frameset_dump(framelist, stdout);
410  check(cpl_dfs_setup_product_header(plist,
411  product_frame,
412  framelist,
413  parlist,
414  "sinfo_utl_ima_gauss",
415  "SINFONI",
416  KEY_VALUE_HPRO_DID,NULL),
417  "Problem in the product DFS-compliance") ;
418 
419 
420  /* Save the file */
421  check(cpl_image_save(imResult,
422  name_o,
423  CPL_BPP_IEEE_FLOAT,
424  plist,
425  CPL_IO_DEFAULT),
426  "Could not save product");
427 
428 
429  sinfo_free_propertylist(&plist) ;
430  sinfo_free_image(&imNoise);
431  sinfo_free_image(&imGauss);
432  sinfo_free_image(&imResult);
433  /* Log the saved file in the input frameset */
434  check_nomsg(cpl_frameset_insert(framelist, product_frame)) ;
435 
436 
437  cleanup:
438 
439  // sinfo_free_frameset(&raw_set);
440  sinfo_free_propertylist(&plist) ;
441  /* This is usually freed by esorex: but what about if errors occurs?
442  sinfo_free_frame(&product_frame) ;
443  */
444  sinfo_free_image(&imNoise) ;
445 
446  if (cpl_error_get_code()) {
447  return -1 ;
448  } else {
449  return 0 ;
450  }
451 
452 }
int cpl_plugin_get_info(cpl_pluginlist *list)
Build the list of available plugins, for this module.
void irplib_reset(void)
Reset IRPLIB state.