X-shooter Pipeline Reference Manual 3.8.15
xsh_util_bpmap_coadd.c
Go to the documentation of this file.
1/* $Id: xsh_util_bpmap_coadd.c,v 1.17 2012-12-18 14:12:51 amodigli Exp $
2 *
3 * This file is part of the CPL (Common Pipeline Library)
4 * Copyright (C) 2002 European Southern Observatory
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This library 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 GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20/*
21 * $Author: amodigli $
22 * $Date: 2012-12-18 14:12:51 $
23 * $Revision: 1.17 $
24 * $Name: not supported by cvs2svn $
25 */
26
27 /****************************************************************
28 * Bad pixel search (normal method) *
29 ****************************************************************/
30
31/* ---------------------------------------------------------------
32 INCLUDES
33 --------------------------------------------------------------- */
34#ifdef HAVE_CONFIG_H
35#include <config.h>
36#endif
37
38/* std libraries */
39#include <strings.h>
40#include <string.h>
41#include <stdio.h>
42
43/* cpl */
44#include <cpl.h>
45/* irplib */
46#include <xsh_irplib_utils.h>
47
48/* xsh */
49#include <xsh_dfs.h>
50#include <xsh_pfits.h>
51#include <xsh_msg.h>
52#include <xsh_error.h>
53#include <xsh_utils_wrappers.h>
54#include <xsh_parameters.h>
55#include <xsh_badpixelmap.h>
56
57/* ---------------------------------------------------------------
58 DEFINES
59 --------------------------------------------------------------- */
60
61#define RECIPE_ID "xsh_util_bpmap_coadd"
62#define RECIPE_AUTHOR "A.Modigliani"
63#define RECIPE_CONTACT "amodigli@eso.org"
64
65/* ---------------------------------------------------------------
66 FUNCTIONS PROTOTYPES
67 --------------------------------------------------------------- */
68
69const char * xsh_get_licence(void);
70static int xsh_util_bpmap_coadd_create(cpl_plugin *plugin);
71static int xsh_util_bpmap_coadd_exec(cpl_plugin *plugin);
72static int xsh_util_bpmap_coadd_destroy(cpl_plugin *plugin);
73static int xsh_util_bpmap_coadd(cpl_parameterlist *, cpl_frameset *);
74
75/* ---------------------------------------------------------------
76 STATIC VARIABLES
77 --------------------------------------------------------------- */
78
80"This recipe performs bad pixel maps combination via bitwise OR.\n"
81"The input files are several (at least 2) bad pixel masks in the sof file\n"
82"Their tab should contain the string BP_MAP.\n"
83"The output is an image resulting from the logical operator OR \n"
84"applied to all the masks.\n"
85"Information on relevant parameters can be found with\n"
86"esorex --params xsh_util_bpmap_coadd\n"
87"esorex --help xsh_util_bpmap_coadd\n"
88"\n";
89
90static char xsh_util_bpmap_coadd_description_short[] ="Add bad pixels masks";
91
92/* ---------------------------------------------------------------
93 FUNCTIONS CODE
94 --------------------------------------------------------------- */
95
96/*---------------------------------------------------------------------------*/
100/*---------------------------------------------------------------------------*/
102/* --------------------------------------------------------------- */
103
104int
105cpl_plugin_get_info(cpl_pluginlist *list)
106{
107
108 cpl_recipe *recipe = cpl_calloc(1, sizeof *recipe);
109 cpl_plugin *plugin = &recipe->interface;
110
111
112 cpl_plugin_init(plugin,
113 CPL_PLUGIN_API,
114 XSH_BINARY_VERSION,
115 CPL_PLUGIN_TYPE_RECIPE,
116 "xsh_util_bpmap_coadd",
119 "Andrea Modigliani",
120 "Andrea.Modigliani@eso.org",
125
126 cpl_pluginlist_append(list, plugin);
127
128 return 0;
129
130}
131
132/* --------------------------------------------------------------- */
139static int
141{
142 cpl_recipe* recipe=NULL;
143
144 /*
145 * We have to provide the option we accept to the application.
146 * We need to setup our parameter list and hook it into the recipe
147 * interface.
148 */
149
150 /* Reset library state */
151 xsh_init();
152
153 /* Check input */
154 assure( plugin != NULL, CPL_ERROR_NULL_INPUT, "Null plugin");
155
156 /* Get the recipe out of the plugin */
157 assure( cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE,
158 CPL_ERROR_TYPE_MISMATCH,
159 "Plugin is not a recipe");
160
161 recipe = (cpl_recipe *)plugin;
162
163 recipe->parameters = cpl_parameterlist_new();
164 if(recipe->parameters == NULL) {
165 return 1;
166 }
167 xsh_parameters_generic( RECIPE_ID, recipe->parameters ) ;
168 xsh_parameters_decode_bp(RECIPE_ID,recipe->parameters,-1);
169 cpl_error_reset();
170 irplib_reset();
171
172 /*
173 * Fill the parameter list.
174 */
175
176 cleanup:
177 if ( cpl_error_get_code() != CPL_ERROR_NONE ){
178 xsh_error_dump(CPL_MSG_ERROR);
179 return 1;
180 }
181 else {
182 return 0;
183 }
184
185}
186
187/* --------------------------------------------------------------- */
193static int
194xsh_util_bpmap_coadd_exec(cpl_plugin *plugin)
195{
196
197
198 cpl_recipe *recipe = (cpl_recipe *) plugin;
199 cpl_errorstate initial_errorstate = cpl_errorstate_get();
200 int code=0;
201
202 if(recipe->parameters == NULL ) {
203 return 1;
204 }
205 if(recipe->frames == NULL) {
206 return 1;
207 }
208
209 check(code=xsh_util_bpmap_coadd(recipe->parameters, recipe->frames));
210
211 if (!cpl_errorstate_is_equal(initial_errorstate)) {
212 /* Dump the error history since recipe execution start.
213 At this point the recipe cannot recover from the error */
214 cpl_errorstate_dump(initial_errorstate, CPL_FALSE, NULL);
215 }
216 cleanup:
217
218 return code;
219
220}
221
222/* --------------------------------------------------------------- */
229static int
231{
232 cpl_recipe *recipe = (cpl_recipe *) plugin;
233 /*
234 * We just destroy what was created during the plugin initializzation phase
235 * i.e. the parameter list. The frame set is managed by the application which
236 * called us, so that we must not touch it.
237 */
238
239 cpl_parameterlist_delete(recipe->parameters);
240 return 0;
241
242}
243
244/*
245 * The actual recipe actually start here.
246 */
247
248
249/* --------------------------------------------------------------- */
263static int
264xsh_util_bpmap_coadd(cpl_parameterlist *parameters, cpl_frameset *sof)
265{
266 const char* recipe_tags[1] = {"xsh_util_bpmap_coadd"};
267 int recipe_tags_size = 1;
268 cpl_frameset* bpf_set=NULL;
269 int n=0;
270 cpl_frame* frm=NULL;
271 cpl_frame* frm_tmp=NULL;
272 char name[256];
273 char tag_pro[256];
274 const char* tag=NULL;
275 const char* filename=NULL;
276 int found=false;
277 int i=0;
279 cpl_frameset* calib=0;
280 cpl_frameset* raws=0;
281 cpl_frame* msk_frm=NULL;
282 cpl_propertylist* plist=NULL;
283 cpl_image* ima=NULL;
284
285 xsh_msg("Welcome to Shooter Pipeline release %d.%d.%d",
286 XSH_MAJOR_VERSION,XSH_MINOR_VERSION,XSH_MICRO_VERSION);
287
288 check( xsh_begin( sof, parameters, &instrument, &raws, &calib,
289 recipe_tags, recipe_tags_size, RECIPE_ID,
290 XSH_BINARY_VERSION,
292
293 if(xsh_dfs_set_groups(sof)) {
294 xsh_msg_error("Cannot indentify RAW and CALIB frames") ;
295 }
296
297 n=cpl_frameset_get_size(calib);
298 if(n<1) {
299 xsh_msg_error("Empty input frame list!");
300 goto cleanup ;
301 }
302 bpf_set=cpl_frameset_new();
303 for(i=0;i<n;i++) {
304 found=false;
305 frm=cpl_frameset_get_frame(calib,i);
306 tag=cpl_frame_get_tag(frm);
307 if(strstr(tag,XSH_BP_MAP_REF) != NULL) {
308 found=true;
309 } else if(strstr(tag,XSH_BP_MAP_NL) != NULL) {
310 found=true;
311 } else if(strstr(tag,XSH_BP_MAP_RP) != NULL) {
312 found=true;
313 } else if(strstr(tag,XSH_BP_MAP_HP) != NULL) {
314 found=true;
315 } else if(strstr(tag,XSH_BP_MAP_CP) != NULL) {
316 found=true;
317 } else if(strstr(tag,XSH_BP_MAP_NP) != NULL) {
318 found=true;
319 } else if(strstr(tag,XSH_BP_MAP_DP) != NULL) {
320 found=true;
321 } else if(strstr(tag,XSH_BP_MAP_SP) != NULL) {
322 found=true;
323 } else if(strstr(tag,XSH_BP_MAP_PN) != NULL) {
324 found=true;
325 } else if(strstr(tag,XSH_MASTER_BP_MAP_NLIN) != NULL) {
326 found=true;
327 } else if(strstr(tag,XSH_MASTER_BP_MAP_BIAS) != NULL) {
328 found=true;
329 } else if(strstr(tag,XSH_MASTER_BP_MAP_DARK) != NULL) {
330 found=true;
331 } else if(strstr(tag,XSH_MASTER_BP_MAP_FLAT) != NULL) {
332 found=true;
333 } else if(strstr(tag,XSH_MASTER_BP_MAP) != NULL) {
334 found=true;
335 } else {
336 xsh_msg_warning("Frame with tag %s is not a supported bad pixel map",
337 tag);
338 }
339 if(found) {
340 cpl_frameset_insert(bpf_set,cpl_frame_duplicate(frm));
341 }
342 }
343 n=cpl_frameset_get_size(bpf_set);
344 for(i=0;i<n;i++) {
345 if(i==0) {
346 frm=cpl_frameset_get_frame(bpf_set,i);
347 } else {
348 frm_tmp=cpl_frameset_get_frame(bpf_set,i);
349 xsh_msg("combine %s with %s via bitwise OR",
350 cpl_frame_get_tag(frm),cpl_frame_get_tag(frm_tmp));
351
352 xsh_badpixelmap_coadd(frm,frm_tmp);
353 }
354 }
355
356 sprintf(tag_pro,"%s_%s",XSH_MASTER_BP_MAP,
358 sprintf(name,"%s.fits",tag_pro);
359 filename=cpl_frame_get_filename(frm);
360 ima=cpl_image_load(filename,CPL_TYPE_FLOAT,0,0);
361 plist=cpl_propertylist_load(filename,0);
362 xsh_pfits_set_pcatg(plist,tag_pro);
363 //check(cpl_image_save(ima,name,CPL_BPP_IEEE_FLOAT,plist,CPL_IO_DEFAULT));
364
365 check(msk_frm=xsh_frame_product(name,tag_pro,CPL_FRAME_TYPE_IMAGE,
366 CPL_FRAME_GROUP_PRODUCT,
367 CPL_FRAME_LEVEL_FINAL));
368
369 check( xsh_add_product_image(msk_frm, sof,
370 parameters, RECIPE_ID, instrument,NULL));
371
372 cleanup:
373 xsh_free_frameset(&bpf_set);
374 xsh_free_image(&ima);
375 xsh_free_propertylist(&plist);
376
377 if (cpl_error_get_code() != CPL_ERROR_NONE) {
378 return -1;
379 } else {
380 return 0;
381 }
382
383
384
385}
static xsh_instrument * instrument
cpl_error_code xsh_badpixelmap_coadd(cpl_frame *self, const cpl_frame *right, const int mode)
#define assure(CONDITION, ERROR_CODE,...)
Definition: xsh_error.h:54
#define check(COMMAND)
Definition: xsh_error.h:71
#define xsh_error_dump(level)
Definition: xsh_error.h:92
const char * xsh_instrument_arm_tostring(xsh_instrument *i)
Get the string associated with an arm.
#define xsh_msg_warning(...)
Print an warning message.
Definition: xsh_msg.h:88
#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_pcatg(cpl_propertylist *plist, const char *value)
Write the PCATG value.
Definition: xsh_pfits.c:1008
static int xsh_util_bpmap_coadd_create(cpl_plugin *plugin)
Set up the recipe options.
int cpl_plugin_get_info(cpl_pluginlist *list)
static int xsh_util_bpmap_coadd(cpl_parameterlist *, cpl_frameset *)
Do bad pixel search. Monitor parameters. Do something.
static int xsh_util_bpmap_coadd_destroy(cpl_plugin *plugin)
Destroy what has been created by the 'create' function.
static int xsh_util_bpmap_coadd_exec(cpl_plugin *plugin)
Executes 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
void xsh_add_product_image(cpl_frame *frame, cpl_frameset *frameset, const cpl_parameterlist *parameters, const char *recipe_id, xsh_instrument *instrument, const char *final_prefix)
Definition: xsh_dfs.c:2965
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_frame * xsh_frame_product(const char *fname, const char *tag, cpl_frame_type type, cpl_frame_group group, cpl_frame_level level)
Creates a frame with given characteristics.
Definition: xsh_dfs.c:930
#define XSH_MASTER_BP_MAP_BIAS
Definition: xsh_dfs.h:664
#define XSH_MASTER_BP_MAP_NLIN
Definition: xsh_dfs.h:659
#define XSH_BP_MAP_PN
Definition: xsh_dfs.h:349
#define XSH_BP_MAP_RP
Definition: xsh_dfs.h:355
#define XSH_BP_MAP_REF
Definition: xsh_dfs.h:610
#define XSH_BP_MAP_NP
Definition: xsh_dfs.h:343
#define XSH_BP_MAP_HP
Definition: xsh_dfs.h:620
#define XSH_BP_MAP_SP
Definition: xsh_dfs.h:631
#define XSH_MASTER_BP_MAP
Definition: xsh_dfs.h:643
#define XSH_MASTER_BP_MAP_FLAT
Definition: xsh_dfs.h:674
#define XSH_BP_MAP_CP
Definition: xsh_dfs.h:625
#define XSH_BP_MAP_DP
Definition: xsh_dfs.h:637
#define XSH_MASTER_BP_MAP_DARK
Definition: xsh_dfs.h:669
#define XSH_BP_MAP_NL
Definition: xsh_dfs.h:360
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)
static char xsh_util_bpmap_coadd_description_short[]
static char xsh_util_bpmap_coadd_description[]
const char * xsh_get_licence(void)
#define RECIPE_ID