SINFONI Pipeline Reference Manual  2.5.2
sinfo_utl_skymap.c
1 /* $Id: sinfo_utl_skymap.c,v 1.13 2009-01-30 14:56:12 amodigli 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: amodigli $
23  * $Date: 2009-01-30 14:56:12 $
24  * $Revision: 1.13 $
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 
36 /* cpl */
37 #include <cpl.h>
38 /* irplib */
39 #include <irplib_utils.h>
40 
41 /* sinfoni */
42 #include <sinfo_tpl_utils.h>
43 #include <sinfo_pfits.h>
44 #include <sinfo_tpl_dfs.h>
45 #include <sinfo_raw_types.h>
46 #include <sinfo_pro_types.h>
47 #include <sinfo_functions.h>
48 #include <sinfo_key_names.h>
49 #include <sinfo_msg.h>
50 #include <sinfo_error.h>
51 #include <sinfo_utils_wrappers.h>
52 /*-----------------------------------------------------------------------------
53  Functions prototypes
54  ----------------------------------------------------------------------------*/
55 
56 static int sinfo_utl_skymap_create(cpl_plugin *) ;
57 static int sinfo_utl_skymap_exec(cpl_plugin *) ;
58 static int sinfo_utl_skymap_destroy(cpl_plugin *) ;
59 static int sinfo_utl_skymap(cpl_parameterlist *, cpl_frameset *) ;
60 /*-----------------------------------------------------------------------------
61  Static variables
62  ----------------------------------------------------------------------------*/
63 
64 static char sinfo_utl_skymap_description[] =
65  "This recipe flags as bad pixels sky lines.\n"
66  "Input are sky frames with tag SKY\n"
67  "Output image is called out_skymap.fits\n"
68  "\n";
69 
70 /*-----------------------------------------------------------------------------
71  Functions code
72  ----------------------------------------------------------------------------*/
73 /*---------------------------------------------------------------------------*/
77 /*---------------------------------------------------------------------------*/
78 
80 /*---------------------------------------------------------------------------*/
88 /*---------------------------------------------------------------------------*/
89 int cpl_plugin_get_info(cpl_pluginlist * list)
90 {
91  cpl_recipe * recipe = cpl_calloc(1, sizeof *recipe ) ;
92  cpl_plugin * plugin = &recipe->interface ;
93 
94  cpl_plugin_init(plugin,
95  CPL_PLUGIN_API,
96  SINFONI_BINARY_VERSION,
97  CPL_PLUGIN_TYPE_RECIPE,
98  "sinfo_utl_skymap",
99  "Flags sky lines as bad pixels, with map generation",
100  sinfo_utl_skymap_description,
101  "Andrea Modigliani",
102  "Andrea.Modigliani@eso.org",
103  sinfo_get_license(),
104  sinfo_utl_skymap_create,
105  sinfo_utl_skymap_exec,
106  sinfo_utl_skymap_destroy) ;
107 
108  cpl_pluginlist_append(list, plugin) ;
109 
110  return 0;
111 }
112 
113 /*---------------------------------------------------------------------------*/
122 /*---------------------------------------------------------------------------*/
123 static int sinfo_utl_skymap_create(cpl_plugin * plugin)
124 {
125  cpl_recipe * recipe ;
126  cpl_parameter * p ;
127 
128  /* Get the recipe out of the plugin */
129  if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
130  recipe = (cpl_recipe *)plugin ;
131  else return -1 ;
132 
133  /* Create the parameters list in the cpl_recipe object */
134  recipe->parameters = cpl_parameterlist_new() ;
135 
136  /* Fill the parameters list */
137 
138 
139  /* --doubleopt */
140  p = cpl_parameter_new_range("sinfoni.sinfo_utl_skymap.xsize",
141  CPL_TYPE_INT, "X box size", "sinfoni.sinfo_utl_skymap", 1,1,2047) ;
142  cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "xsize") ;
143  cpl_parameterlist_append(recipe->parameters, p) ;
144 
145  /* --doubleopt */
146  p = cpl_parameter_new_range("sinfoni.sinfo_utl_skymap.ysize",
147  CPL_TYPE_INT, "Y box size", "sinfoni.sinfo_utl_skymap", 30,1,2047) ;
148  cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "ysize") ;
149  cpl_parameterlist_append(recipe->parameters, p) ;
150 
151 
152 
153  /* --doubleopt */
154  p = cpl_parameter_new_value("sinfoni.sinfo_utl_skymap.threshold",
155  CPL_TYPE_DOUBLE, "Threshold", "sinfoni.sinfo_utl_skymap", 30.) ;
156  cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "thresh") ;
157  cpl_parameterlist_append(recipe->parameters, p) ;
158 
159  /* Return */
160  return 0;
161 }
162 
163 /*---------------------------------------------------------------------------*/
169 /*---------------------------------------------------------------------------*/
170 static int sinfo_utl_skymap_exec(cpl_plugin * plugin)
171 {
172  cpl_recipe * recipe ;
173  int code=0;
174  cpl_errorstate initial_errorstate = cpl_errorstate_get();
175 
176  /* Get the recipe out of the plugin */
177  if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
178  recipe = (cpl_recipe *)plugin ;
179  else return -1 ;
180  cpl_error_reset();
181  //irplib_reset();
182  check_nomsg(code = sinfo_utl_skymap(recipe->parameters, recipe->frames)) ;
183  if (!cpl_errorstate_is_equal(initial_errorstate)) {
184  /* Dump the error history since recipe execution start.
185  At this point the recipe cannot recover from the error */
186  cpl_errorstate_dump(initial_errorstate, CPL_FALSE, NULL);
187  }
188  cleanup:
189  return code ;
190 }
191 
192 /*---------------------------------------------------------------------------*/
198 /*---------------------------------------------------------------------------*/
199 static int sinfo_utl_skymap_destroy(cpl_plugin * plugin)
200 {
201  cpl_recipe * recipe ;
202 
203  /* Get the recipe out of the plugin */
204  if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
205  recipe = (cpl_recipe *)plugin ;
206  else return -1 ;
207 
208  cpl_parameterlist_delete(recipe->parameters) ;
209  return 0 ;
210 }
211 
212 static int sinfo_utl_skymap(
213  cpl_parameterlist * parlist,
214  cpl_frameset * framelist)
215 {
216  cpl_parameter * param =NULL;
217  const char * name_i=NULL;
218  int xsize=0;
219  int ysize=0;
220 
221  cpl_frame * sky_frm=NULL;
222 
223  const char * name_o=NULL ;
224  cpl_propertylist * plist =NULL;
225  cpl_frame * product_frame=NULL;
226  cpl_frameset * sky_set=NULL;
227  cpl_image * sky_ima=NULL;
228  cpl_image * sky_map=NULL;
229  double threshold=0;
230  int i=0;
231  int j=0;
232  double sinfo_median=0;
233  float* sky_ima_pix=NULL;
234  float* sky_map_pix=NULL;
235  int nx=0;
236  int ny=0;
237  int n=0;
238 
239  /* HOW TO RETRIEVE INPUT PARAMETERS */
240  /* --stropt */
241 
242  sinfo_msg("Welcome to SINFONI Pipeline release %d.%d.%d",
243  SINFONI_MAJOR_VERSION,SINFONI_MINOR_VERSION,SINFONI_MICRO_VERSION);
244 
245  param = cpl_parameterlist_find(parlist,
246  "sinfoni.sinfo_utl_skymap.out_filename");
247  name_o = "out_skymap.fits";
248 
249  /* --intopt */
250  param = cpl_parameterlist_find(parlist,"sinfoni.sinfo_utl_skymap.xsize");
251  xsize = cpl_parameter_get_int(param) ;
252 
253  /* --intopt */
254  param = cpl_parameterlist_find(parlist,"sinfoni.sinfo_utl_skymap.ysize");
255  ysize = cpl_parameter_get_int(param) ;
256 
257  param = cpl_parameterlist_find(parlist,
258  "sinfoni.sinfo_utl_skymap.threshold");
259  threshold = cpl_parameter_get_double(param) ;
260 
261  /* Identify the RAW and CALIB frames in the input frameset */
262  if (sinfo_dfs_set_groups(framelist)) {
263  sinfo_msg_error("Cannot identify RAW and CALIB frames") ;
264  return -1 ;
265  }
266 
267  /* HOW TO ACCESS INPUT DATA */
268  n=cpl_frameset_get_size(framelist);
269  if(n<1) {
270  sinfo_msg_error("Empty input frame list!");
271  return -1;
272  }
273  sky_set=cpl_frameset_new();
274  sinfo_extract_frames_type(framelist,sky_set,RAW_SKY);
275 
276  n=cpl_frameset_get_size(framelist);
277  if(n<1) {
278  sinfo_msg_error("No sky frames in input list!");
279  sinfo_free_frameset(&sky_set);
280  return -1;
281  }
282 
283 
284  check_nomsg(sky_frm = cpl_frameset_get_frame(sky_set,0));
285 
286  if ((plist=cpl_propertylist_load(cpl_frame_get_filename(sky_frm),
287  0)) == NULL) {
288  sinfo_msg_error("Cannot read the FITS header") ;
289  return -1 ;
290  }
291 
292 
293  name_i=cpl_frame_get_filename(sky_frm);
294  sky_ima = cpl_image_load(name_i,CPL_TYPE_FLOAT,0,0);
295  sky_map=cpl_image_duplicate(sky_ima);
296  sky_ima_pix=cpl_image_get_data(sky_ima);
297  sky_map_pix=cpl_image_get_data(sky_map);
298  nx = cpl_image_get_size_x(sky_ima);
299  ny = cpl_image_get_size_y(sky_ima);
300  if (nx != SIZEX || ny != SIZEY) {
301  sinfo_msg_error("nx=%d ny=%d, expected nx=%d ny=%d",nx,ny,SIZEX,SIZEY);
302  goto cleanup;
303 
304  }
305  for(i=1;i<nx;i++) {
306 
307  for(j=ysize+1;j<ny-ysize;j++) {
308 
309  sinfo_median=cpl_image_get_median_window(sky_ima,i,j-ysize,i,j+xsize);
310  if(cpl_error_get_code() != CPL_ERROR_NONE) {
311  sinfo_msg_error("Exit");
312  sinfo_free_image(&sky_ima);
313  sinfo_free_image(&sky_map);
314  sinfo_free_propertylist(&plist);
315  sinfo_free_frameset(&sky_set);
316  return -1;
317  }
318  if(sky_ima_pix[i+j*nx] > sinfo_median+threshold) {
319  sky_map_pix[i+j*nx]=0.;
320  } else {
321  sky_map_pix[i+j*nx]=1.;
322  }
323  }
324 
325  }
326 
327 
328 
329  for(i=1;i<nx;i++) {
330 
331  for(j=0;j<ysize+1;j++) {
332 
333  sky_map_pix[i+j*nx]=0.;
334 
335  }
336 
337  for(j=ny-ysize+1;j<ny;j++) {
338 
339  sky_map_pix[i+j*nx]=0.;
340 
341  }
342 
343  }
344 
345 
346 
347  /* Now performing the data reduction */
348  /* Let's generate one image for the example */
349 
350 
351 
352  /* HOW TO SAVE A PRODUCT ON DISK */
353  /* Set the file name */
354  name_o = "sky_map.fits" ;
355 
356  /* Create product frame */
357  product_frame = cpl_frame_new();
358  cpl_frame_set_filename(product_frame, name_o) ;
359  cpl_frame_set_tag(product_frame, PRO_SKY_DUMMY) ;
360  cpl_frame_set_type(product_frame, CPL_FRAME_TYPE_IMAGE) ;
361  cpl_frame_set_group(product_frame, CPL_FRAME_GROUP_PRODUCT) ;
362  cpl_frame_set_level(product_frame, CPL_FRAME_LEVEL_FINAL) ;
363 
364  if (cpl_error_get_code()) {
365  sinfo_msg_error("Error while initialising the product frame") ;
366  sinfo_free_propertylist(&plist) ;
367  sinfo_free_frame(&product_frame) ;
368  return -1 ;
369  }
370 
371  /* Add DataFlow keywords */
372  if (cpl_dfs_setup_product_header(plist, product_frame, framelist, parlist,
373  "sinfo_utl_skymap",
374  "SINFONI", KEY_VALUE_HPRO_DID,NULL)
375  != CPL_ERROR_NONE) {
376  sinfo_msg_error("Problem in the product DFS-compliance") ;
377  sinfo_free_propertylist(&plist) ;
378  sinfo_free_frame(&product_frame) ;
379  sinfo_free_image(&sky_ima);
380  sinfo_free_image(&sky_map);
381  sinfo_free_frameset(&sky_set);
382  return -1 ;
383  }
384 
385 
386  /* Save the file */
387  if (cpl_image_save(sky_map, name_o, CPL_BPP_IEEE_FLOAT, plist,
388  CPL_IO_DEFAULT) != CPL_ERROR_NONE) {
389  sinfo_msg_error("Could not save product");
390  sinfo_free_propertylist(&plist) ;
391  sinfo_free_frame(&product_frame) ;
392  sinfo_free_image(&sky_map) ;
393  return -1 ;
394  }
395  sinfo_free_propertylist(&plist) ;
396  sinfo_free_image(&sky_map) ;
397  sinfo_free_image(&sky_ima) ;
398 
399  /* Log the saved file in the input frameset */
400  cpl_frameset_insert(framelist, product_frame) ;
401  sinfo_free_frameset(&sky_set);
402 
403 
404 
405  cleanup:
406  sinfo_free_image(&sky_ima);
407  sinfo_free_image(&sky_map);
408  sinfo_free_propertylist(&plist);
409  sinfo_free_frameset(&sky_set);
410 
411  if( cpl_error_get_code()!=CPL_ERROR_NONE) {
412  return -1 ;
413  } else {
414  return 0 ;
415  }
416 }