X-shooter Pipeline Reference Manual 3.8.15
xsh_util_ima_arith.c
Go to the documentation of this file.
1/* $Id: xsh_util_ima_arith.c,v 1.12 2012-07-26 12:22:31 amodigli Exp $
2 *
3 * This file is part of the XSHOOTER 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: 2012-07-26 12:22:31 $
24 * $Revision: 1.12 $
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 <xsh_tpl_utils.h>
44//#include <xsh_pfits.h>
45//#include <xsh_tpl_dfs.h>
46//#include <xsh_key_names.h>
47//#include <xsh_pro_types.h>
48//#include <xsh_functions.h>
49#include <xsh_dfs.h>
50#include <xsh_data_pre.h>
51#include <xsh_parameters.h>
52#include <xsh_drl.h>
53#include <xsh_msg.h>
54#include <xsh_pfits.h>
55#include <xsh_error.h>
56//#include <xsh_utils_wrappers.h>
57
58
59/*-----------------------------------------------------------------------------
60 Defines
61 ----------------------------------------------------------------------------*/
62
63#define XSH_UTL_IMA_ARITH_RECIPE_ID "xsh_util_ima_arith"
64#define XSH_UTL_IMA_ARITH_RECIPE_AUTHOR "A.Modigliani"
65#define XSH_UTL_IMA_ARITH_RECIPE_CONTACT "Andrea.Modigliani@eso.org"
66#define PRO_IMA "PRO_IMA_UVB"
67#define KEY_VALUE_HPRO_DID "PRO-1.15"
68/*-----------------------------------------------------------------------------
69 Functions prototypes
70 ----------------------------------------------------------------------------*/
71
72static int xsh_util_ima_arith_create(cpl_plugin *) ;
73static int xsh_util_ima_arith_exec(cpl_plugin *) ;
74static int xsh_util_ima_arith_destroy(cpl_plugin *) ;
75static int xsh_util_ima_arith(cpl_parameterlist *, cpl_frameset *) ;
76
77/*-----------------------------------------------------------------------------
78 Static variables
79 ----------------------------------------------------------------------------*/
80
81static char
82xsh_util_ima_arith_description_short[] = "Computes result of ima1 op ima2";
84"This recipe performs image computation.\n"
85"The input files are 2 images\n"
86"their associated tags should be IMA.\n"
87"The output is an image resulting from the IMA op IMA where op indicates\n"
88"the operation to be performed specified by the parameter \n"
89"xsh.xsh_util_ima_arith.op having alias 'op'\n"
90"Information on relevant parameters can be found with\n"
91"esorex --params xsh_util_ima_arith\n"
92"esorex --help xsh_util_ima_arith\n"
93"\n";
94
95/*-----------------------------------------------------------------------------
96 Functions code
97 ----------------------------------------------------------------------------*/
98/*---------------------------------------------------------------------------*/
103/*---------------------------------------------------------------------------*/
104
106/*---------------------------------------------------------------------------*/
114/*---------------------------------------------------------------------------*/
115int cpl_plugin_get_info(cpl_pluginlist * list)
116{
117 cpl_recipe * recipe = cpl_calloc(1, sizeof *recipe ) ;
118 cpl_plugin * plugin = &recipe->interface ;
119
120 cpl_plugin_init(plugin,
121 CPL_PLUGIN_API,
122 XSH_BINARY_VERSION,
123 CPL_PLUGIN_TYPE_RECIPE,
133
134 cpl_pluginlist_append(list, plugin) ;
135
136 return 0;
137}
138
139/*---------------------------------------------------------------------------*/
148/*---------------------------------------------------------------------------*/
149static int xsh_util_ima_arith_create(cpl_plugin * plugin)
150{
151 cpl_recipe * recipe ;
152 cpl_parameter * p ;
153
154 /* Reset library state */
155 xsh_init();
156
157 /* Get the recipe out of the plugin */
158 if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
159 recipe = (cpl_recipe *)plugin ;
160 else return -1 ;
161 cpl_error_reset();
162
163
164 /* Create the parameters list in the cpl_recipe object */
165 recipe->parameters = cpl_parameterlist_new() ;
166
167
168 /* Set generic parameters (common to all recipes) */
170 recipe->parameters ) ) ;
172 /* Fill the parameters list */
173 /* --stropt */
174 p = cpl_parameter_new_value("xsh.xsh_util_ima_arith.op",
175 CPL_TYPE_STRING,
176 "A possible operation",
177 "xsh.xsh_util_ima_arith","+");
178 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "op") ;
179 cpl_parameterlist_append(recipe->parameters, p) ;
180
181 /* --doubleopt */
182 p = cpl_parameter_new_value("xsh.xsh_util_ima_arith.value",
183 CPL_TYPE_DOUBLE, "a value", "xsh.xsh_util_ima_arith", 9999.) ;
184 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "value") ;
185 cpl_parameterlist_append(recipe->parameters, p) ;
186 cleanup:
187
188 /* Return */
189 return 0;
190}
191
192/*---------------------------------------------------------------------------*/
198/*---------------------------------------------------------------------------*/
199static int xsh_util_ima_arith_exec(cpl_plugin * plugin)
200{
201 cpl_recipe * recipe ;
202 int code=0;
203 cpl_errorstate initial_errorstate = cpl_errorstate_get();
204
205 /* Get the recipe out of the plugin */
206 if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
207 recipe = (cpl_recipe *)plugin ;
208 else return -1 ;
209 cpl_error_reset();
210
211 code = xsh_util_ima_arith(recipe->parameters, recipe->frames) ;
212
213
214 if (!cpl_errorstate_is_equal(initial_errorstate)) {
215 /* Dump the error history since recipe execution start.
216 At this point the recipe cannot recover from the error */
217 cpl_errorstate_dump(initial_errorstate, CPL_FALSE, NULL);
218 }
219
220 return code ;
221}
222
223/*---------------------------------------------------------------------------*/
229/*---------------------------------------------------------------------------*/
230static int xsh_util_ima_arith_destroy(cpl_plugin * plugin)
231{
232 cpl_recipe * recipe ;
233
234 /* Get the recipe out of the plugin */
235 if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
236 recipe = (cpl_recipe *)plugin ;
237 else return -1 ;
238
239 cpl_parameterlist_delete(recipe->parameters) ;
240 return 0 ;
241}
242
243/*---------------------------------------------------------------------------*/
250/*---------------------------------------------------------------------------*/
251static int
252xsh_util_ima_arith( cpl_parameterlist * parlist,
253 cpl_frameset * framelist)
254{
255 const char* recipe_tags[1] = { XSH_RAW_IMA_SLIT_UVB};
256 int recipe_tags_size = 1;
257 cpl_parameter * param= NULL ;
258 const char * operation=NULL;
259 double value=1 ;
260 cpl_image * ima1=NULL ;
261 cpl_image * ima2=NULL ;
262 int switch_ima2 = 0;
263 const char * name_o=NULL ;
264 cpl_propertylist * plist=NULL ;
265 cpl_image * image=NULL ;
266 cpl_frame * product_frame=NULL;
267 cpl_frameset * raw_set=NULL;
268 int nraw=0;
269 int n=0;
271 cpl_frameset* raws=NULL;
272 cpl_frameset* calib=NULL;
273 xsh_pre* pre = NULL;
274 int pre_overscan_corr=0;
275 xsh_msg("Welcome to XSHOOTER Pipeline release %d.%d.%d",
276 XSH_MAJOR_VERSION,XSH_MINOR_VERSION,XSH_MICRO_VERSION);
277
278 /* HOW TO RETRIEVE INPUT PARAMETERS */
279 /* --stropt */
280 check(param=cpl_parameterlist_find(parlist,
281 "xsh.xsh_util_ima_arith.op"));
282 check(operation=cpl_parameter_get_string(param));
283
284 /* --boolopt */
285 check(param=cpl_parameterlist_find(parlist,
286 "xsh.xsh_util_ima_arith.value"));
287 check(value = cpl_parameter_get_double(param)) ;
288
289
290 /**************************************************************************/
291 /* DFS management */
292 /**************************************************************************/
293
294 check( xsh_begin( framelist, parlist, &instrument, &raws, &calib,
295 recipe_tags, recipe_tags_size,
297 XSH_UTL_IMA_ARITH, XSH_BINARY_VERSION,
299
300
301 /* Identify the RAW and CALIB frames in the input frameset */
302 check_msg(xsh_dfs_set_groups(framelist),
303 "Cannot identify RAW and CALIB frames") ;
304
305 /* HOW TO ACCESS INPUT DATA */
306 n=cpl_frameset_get_size(framelist);
307 if(n<1) {
308 xsh_msg_error("Empty input frame list!");
309 goto cleanup ;
310 }
311
312 /* HOW TO ACCESS INPUT DATA */
314 "Found no input frames with tag %s",XSH_RAW_IMA_SLIT_UVB);
315 check(nraw=cpl_frameset_get_size(raw_set));
316 if (nraw<1) {
317 xsh_msg_error("Found no input frames with tag %s",XSH_RAW_IMA_SLIT_UVB);
318 goto cleanup;
319 } else {
320 check(xsh_prepare(raw_set,NULL, NULL,"RAW_IMA", instrument,pre_overscan_corr,CPL_TRUE));
321 check(ima1=cpl_image_load("RAW_IMA_PRE_0.fits",CPL_TYPE_FLOAT,0,0));
322 if (nraw>1) {
323 check(ima2 = cpl_image_load("RAW_IMA_PRE_1.fits",CPL_TYPE_FLOAT,0,0));
324 switch_ima2=1;
325 } else if (value == 9999.) {
326 xsh_msg_error("Found only one input frames with tag %s",
328 goto cleanup;
329 } else {
330 xsh_msg("Perform image arithmetics on frame %s",
332 }
333 }
334
335 xsh_free_frameset(&raw_set);
336
337 /* HOW TO GET THE VALUE OF A FITS KEYWORD */
338 check_msg(plist=cpl_propertylist_load("RAW_IMA_PRE_0.fits",0),
339 "Cannot read the FITS header") ;
340
341 /* Now performing the data reduction */
342 /* Let's generate one image for the example */
343 if (value == 9999.) {
344
345 if(ima1 != NULL && ima2 != NULL) {
346 xsh_msg("ima1 %s ima2",operation);
347 if (strcmp(operation,"+") == 0 ) {
348 check_msg(image = cpl_image_add_create(ima1, ima2),
349 "Cannot generate the %s image",operation) ;
350 } else if (strcmp(operation,"-") == 0 ) {
351 check_msg(image = cpl_image_subtract_create(ima1, ima2),
352 "Cannot generate the %s image",operation) ;
353 } else if (strcmp(operation,"*") == 0 ) {
354 check_msg(image = cpl_image_multiply_create(ima1, ima2),
355 "Cannot generate the %s image",operation) ;
356 } else if (strcmp(operation,"/") == 0 ) {
357 check_msg(image = cpl_image_divide_create(ima1, ima2),
358 "Cannot generate the %s image",operation) ;
359 } else {
360 xsh_msg_error("Operation %s not supported",operation);
361 goto cleanup;
362 }
363 xsh_free_image(&ima1);
364 xsh_free_image(&ima2);
365
366 }
367
368 } else {
369 xsh_msg("ima1 %s %f",operation,value);
370
371 if(switch_ima2 == 1) {
372 xsh_free_image(&ima2);
373 }
374
375 if (strcmp(operation,"+") == 0 ) {
376 check_msg(image = cpl_image_add_scalar_create(ima1, value),
377 "Cannot apply the %s operator",operation) ;
378 } else if (strcmp(operation,"-") == 0 ) {
379 check_msg(image = cpl_image_subtract_scalar_create(ima1, value),
380 "Cannot apply the %s operator",operation) ;
381 } else if (strcmp(operation,"*") == 0 ) {
382 check_msg(image = cpl_image_multiply_scalar_create(ima1, value),
383 "Cannot apply the %s operator",operation) ;
384 } else if (strcmp(operation,"/") == 0 ) {
385 check_msg(image = cpl_image_divide_scalar_create(ima1, value),
386 "Cannot apply the %s operator",operation) ;
387 } else {
388 xsh_msg_error("Operation %s not supported",operation);
389 goto cleanup;
390 }
391
392 xsh_free_image(&ima1);
393
394 }
395
396
397 /* HOW TO SAVE A PRODUCT ON DISK */
398 /* Set the file name */
399 name_o = "ima_res.fits" ;
400
401 /* Create product frame */
402 check(product_frame = cpl_frame_new());
403 check(cpl_frame_set_filename(product_frame, name_o)) ;
404 check(cpl_frame_set_tag(product_frame, PRO_IMA)) ;
405 check(cpl_frame_set_type(product_frame, CPL_FRAME_TYPE_IMAGE)) ;
406 check(cpl_frame_set_group(product_frame, CPL_FRAME_GROUP_PRODUCT)) ;
407 check_msg(cpl_frame_set_level(product_frame, CPL_FRAME_LEVEL_FINAL),
408 "Error while initialising the product frame") ;
409
410 /* Add DataFlow keywords */
411 check(cpl_propertylist_erase_regexp(plist, "^ESO PRO CATG",0));
412 check_msg(cpl_dfs_setup_product_header(plist,
413 product_frame,
414 framelist,
415 parlist,
416 "xsh_util_ima_arith",
417 "XSHOOTER",
418 KEY_VALUE_HPRO_DID,NULL),
419 "Problem in the product DFS-compliance") ;
420
421 check_msg(cpl_image_save(image,
422 name_o,
423 CPL_BPP_IEEE_FLOAT,
424 plist,
425 CPL_IO_DEFAULT),
426 "Could not save product");
427 xsh_free_propertylist(&plist) ;
428 check(plist=cpl_propertylist_new());
429 check( xsh_pfits_set_extname(plist, "ERRS"));
430
431 cpl_image_power(image,0.5);
432 check_msg(cpl_image_save(image,
433 name_o,
434 CPL_BPP_IEEE_FLOAT,
435 plist,
436 CPL_IO_EXTEND),
437 "Could not save product");
438 xsh_free_propertylist(&plist) ;
439 check(plist=cpl_propertylist_new());
440 check( xsh_pfits_set_extname(plist, "QUAL"));
441
442
443 cpl_image_divide(image,image);
444 check_msg(cpl_image_save(image,
445 name_o,
446 CPL_BPP_IEEE_FLOAT,
447 plist,
448 CPL_IO_EXTEND),
449 "Could not save product");
450
451
452
453
454 //check(pre=xsh_pre_load(product_frame,instrument));
455
456 /* Save the file */
457 //check_msg(xsh_pre_save(pre,name_o,0),"Could not save product");
458
459 xsh_pre_free(&pre);
460
461 xsh_free_propertylist(&plist) ;
462 xsh_free_image(&image);
463
464 /* Log the saved file in the input frameset */
465 check(cpl_frameset_insert(framelist, product_frame)) ;
466
467
468 cleanup:
469
470 xsh_pre_free(&pre);
471 xsh_free_image(&ima1);
472 xsh_free_image(&ima2);
473 xsh_free_frameset(&raw_set);
474 xsh_free_propertylist(&plist) ;
475 /* This is usually freed by esorex: but what about if errors occurs?
476 xsh_free_frame(&product_frame) ;
477 */
478 xsh_free_image(&image) ;
479
480 if (cpl_error_get_code()) {
481 return -1 ;
482 } else {
483 return 0 ;
484 }
485
486}
static xsh_instrument * instrument
void xsh_pre_free(xsh_pre **pre)
Free a xsh_pre structure.
Definition: xsh_data_pre.c:823
void xsh_prepare(cpl_frameset *frames, cpl_frame *bpmap, cpl_frame *mbias, const char *prefix, xsh_instrument *instr, const int pre_overscan_corr, const bool flag_neg_and_thresh_pix)
This function transform RAW frames dataset in PRE frames dataset attaching the default bad pixel map ...
Definition: xsh_prepare.c:122
#define check(COMMAND)
Definition: xsh_error.h:71
#define check_msg(COMMAND,...)
Definition: xsh_error.h:62
#define xsh_msg_error(...)
Print an error message.
Definition: xsh_msg.h:62
#define xsh_msg(...)
Print a message on info level.
Definition: xsh_msg.h:121
void xsh_pfits_set_extname(cpl_propertylist *plist, const char *value)
Write the EXTNAME value.
Definition: xsh_pfits.c:979
int cpl_plugin_get_info(cpl_pluginlist *list)
Build the list of available plugins, for this module.
static int xsh_util_ima_arith(cpl_parameterlist *, cpl_frameset *)
Get the command line options and execute the data reduction.
static int xsh_util_ima_arith_create(cpl_plugin *)
Setup the recipe options
static int xsh_util_ima_arith_destroy(cpl_plugin *)
Destroy what has been created by the 'create' function.
static int xsh_util_ima_arith_exec(cpl_plugin *)
Execute the plugin instance given by the interface.
void xsh_free_image(cpl_image **i)
Deallocate an image and set the pointer to NULL.
Definition: xsh_utils.c:2116
void xsh_free_frameset(cpl_frameset **f)
Deallocate a frame set and set the pointer to NULL.
Definition: xsh_utils.c:2254
const char * xsh_get_license(void)
Get the pipeline copyright and license.
Definition: xsh_utils.c:1193
void xsh_init(void)
Reset library state.
Definition: xsh_utils.c:1160
cpl_error_code xsh_begin(cpl_frameset *frames, const cpl_parameterlist *parameters, xsh_instrument **instrument, cpl_frameset **raws, cpl_frameset **calib, const char *tag_list[], int tag_list_size, const char *recipe_id, unsigned int binary_version, const char *short_descr)
Recipe initialization.
Definition: xsh_utils.c:1244
void xsh_free_propertylist(cpl_propertylist **p)
Deallocate a property list and set the pointer to NULL.
Definition: xsh_utils.c:2179
int n
Definition: xsh_detmon_lg.c:92
xsh_instrument * xsh_dfs_set_groups(cpl_frameset *set)
Set the group as RAW or CALIB in a frameset and return the instrument detected.
Definition: xsh_dfs.c:1046
cpl_frameset * xsh_frameset_extract(const cpl_frameset *frames, const char *tag)
Extract frames with given tag from frameset.
Definition: xsh_dfs.c:983
#define XSH_RAW_IMA_SLIT_UVB
Definition: xsh_dfs.h:243
cpl_error_code xsh_parameters_decode_bp(const char *recipe_id, cpl_parameterlist *plist, const int ival)
void xsh_parameters_generic(const char *recipe_id, cpl_parameterlist *plist)
#define XSH_UTL_IMA_ARITH_RECIPE_AUTHOR
#define PRO_IMA
static char xsh_util_ima_arith_description_short[]
#define XSH_UTL_IMA_ARITH_RECIPE_ID
#define XSH_UTL_IMA_ARITH_RECIPE_CONTACT
#define KEY_VALUE_HPRO_DID
static char xsh_util_ima_arith_description[]