SINFONI Pipeline Reference Manual  2.5.2
sinfo_rec_mdark.c
1 /* $Id: sinfo_rec_mdark.c,v 1.22 2008-02-28 10:36:10 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: 2008-02-28 10:36:10 $
24  * $Revision: 1.22 $
25  * $Name: not supported by cvs2svn $
26  */
27 
28 /****************************************************************
29  * Dark Frames Data Reduction *
30  ****************************************************************/
31 
32 #ifdef HAVE_CONFIG_H
33 #include <config.h> /* allows the program compilation */
34 #endif
35 
36 /*-----------------------------------------------------------------------------
37  Includes
38  ----------------------------------------------------------------------------*/
39 
40 /* std */
41 #include <strings.h>
42 #include <string.h>
43 #include <stdio.h>
44 
45 /* cpl */
46 #include <cpl.h>
47 
48 /* irplib */
49 #include <irplib_utils.h>
50 
51 /* sinfoni */
52 #include <sinfo_bp_noise.h> /* */
53 #include <sinfo_new_dark.h> /* */
54 #include <sinfo_utilities.h>
55 #include <sinfo_general_config.h>
56 #include <sinfo_bp_config.h>
57 #include <sinfo_bp_noise_config.h>
58 #include <sinfo_dark_config.h>
59 #include <sinfo_tpl_utils.h>
60 #include <sinfo_tpl_dfs.h>
61 #include <sinfo_pfits.h>
62 #include <sinfo_functions.h>
63 #include <sinfo_pfits.h>
64 #include <sinfo_msg.h>
65 #include <sinfo_error.h>
66 #include <sinfo_utils_wrappers.h>
67 /*-----------------------------------------------------------------------------
68  Functions prototypes
69  ----------------------------------------------------------------------------*/
70 
71 static int sinfo_rec_mdark_create(cpl_plugin *plugin);
72 static int sinfo_rec_mdark_exec(cpl_plugin *plugin);
73 static int sinfo_rec_mdark_destroy(cpl_plugin *plugin);
74 static int sinfo_rec_mdark(cpl_parameterlist *config, cpl_frameset *set);
75 static int count_diff_ndit(cpl_frameset *set, cpl_vector** dit_val);
76 
77 /*-----------------------------------------------------------------------------
78  Static variables
79  ----------------------------------------------------------------------------*/
80 
81 static char sinfo_rec_mdark_description[] =
82  "This recipe perform raw sinfo_dark data reduction.\n"
83  "The input files are raw sinfo_dark images\n"
84  "Their associated tags should be DARK.\n"
85  "The output are a master sinfo_dark (PRO.CATG=MASTER_DARK) and\n"
86  "a hot pixels bad pixel map (PRO.CATG=BP_MAP_HP)\n"
87  "\n";
88 
89 
90 /*-----------------------------------------------------------------------------
91  Functions code
92  ----------------------------------------------------------------------------*/
93 /*---------------------------------------------------------------------------*/
97 /*---------------------------------------------------------------------------*/
99 /*---------------------------------------------------------------------------*/
108 /*---------------------------------------------------------------------------*/
109 int
110 cpl_plugin_get_info(cpl_pluginlist *list)
111 {
112 
113  cpl_recipe *recipe = cpl_calloc(1, sizeof *recipe);
114  cpl_plugin *plugin = &recipe->interface;
115 
116 
117  cpl_plugin_init(plugin,
118  CPL_PLUGIN_API,
119  SINFONI_BINARY_VERSION,
120  CPL_PLUGIN_TYPE_RECIPE,
121  "sinfo_rec_mdark",
122  "Master dark and hot pixels mask generation.",
123  sinfo_rec_mdark_description,
124  "Andrea Modigliani",
125  "Andrea.Modigliani@eso.org",
126  sinfo_get_license(),
127  sinfo_rec_mdark_create,
128  sinfo_rec_mdark_exec,
129  sinfo_rec_mdark_destroy);
130 
131  cpl_pluginlist_append(list, plugin);
132 
133  return 0;
134 
135 }
136 
137 /*---------------------------------------------------------------------------*/
145 /*---------------------------------------------------------------------------*/
146 static int sinfo_rec_mdark_create(cpl_plugin *plugin)
147 {
148  cpl_recipe * recipe ;
149 
150  /* Check that the plugin is part of a valid recipe */
151  if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
152  recipe = (cpl_recipe *)plugin ;
153  else return -1 ;
154  cpl_error_reset();
155  irplib_reset();
156 
157  /* Create the parameters list in the cpl_recipe object */
158  recipe->parameters = cpl_parameterlist_new() ;
159 
160 
161  /*
162  * Fill the parameter list.
163  */
164  sinfo_general_config_add(recipe->parameters);
165  sinfo_bp_noise_config_add(recipe->parameters);
166  sinfo_dark_config_add(recipe->parameters);
167  return 0;
168 
169 }
170 /*---------------------------------------------------------------------------*/
176 /*---------------------------------------------------------------------------*/
177 static int sinfo_rec_mdark_exec(cpl_plugin *plugin)
178 {
179  cpl_recipe * recipe ;
180 
181  cpl_errorstate initial_errorstate = cpl_errorstate_get();
182 
183  /* Get the recipe out of the plugin */
184  if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
185  recipe = (cpl_recipe *)plugin ;
186  else return -1 ;
187 
188  if (!cpl_errorstate_is_equal(initial_errorstate)) {
189  /* Dump the error history since recipe execution start.
190  At this point the recipe cannot recover from the error */
191  cpl_errorstate_dump(initial_errorstate, CPL_FALSE, NULL);
192  }
193 
194  return sinfo_rec_mdark(recipe->parameters, recipe->frames);
195 
196 }
197 /*---------------------------------------------------------------------------*/
203 /*---------------------------------------------------------------------------*/
204 static int sinfo_rec_mdark_destroy(cpl_plugin *plugin)
205 {
206  cpl_recipe * recipe ;
207 
208  /* Get the recipe out of the plugin */
209  if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
210  recipe = (cpl_recipe *)plugin ;
211  else return -1 ;
212 
213  cpl_parameterlist_delete(recipe->parameters) ;
214  return 0 ;
215 
216 }
217 
218 
219 /*---------------------------------------------------------------------------*/
226 /*---------------------------------------------------------------------------*/
227 /*
228  * The actual recipe actually start here.
229  */
230 
231 static int
232 sinfo_rec_mdark(cpl_parameterlist *config, cpl_frameset *set)
233 {
234 
235  cpl_parameter* p=NULL;
236  int nset=0;
237  cpl_frameset * wrk_set=NULL;
238  cpl_frameset * cdb_set=NULL;
239  cpl_frameset * tot_set=NULL;
240 
241  cpl_frame* tmp_frm=NULL;
242  cpl_frame* dup_frm=NULL;
243  cpl_frame* cdb_frm=NULL;
244 
245  char tmp_name[FILE_NAME_SZ];
246  char out_bpmap_name[FILE_NAME_SZ];
247  char out_dark_name[FILE_NAME_SZ];
248  double tmp_dit=0;
249  double ref_dit=0;
250 
251  cpl_vector* dit_val=NULL;
252  cpl_propertylist* plist=NULL;
253 
254  int i=0;
255  int j=0;
256  int line_cor=0;
257 
258 
259  int nraw=0;
260  int ncdb=0;
261  int nred=0;
262  int ntot=0;
263  sinfo_msg("Welcome to SINFONI Pipeline release %d.%d.%d",
264  SINFONI_MAJOR_VERSION,SINFONI_MINOR_VERSION,SINFONI_MICRO_VERSION);
265  ck0(sinfo_dfs_set_groups(set),"Cannot indentify RAW and CALIB frames");
266 
267  check_nomsg(p=cpl_parameterlist_find(config, "sinfoni.general.lc_sw"));
268  check_nomsg(line_cor=cpl_parameter_get_bool(p));
269  if(line_cor==1) {
270  check_nomsg(sinfo_ima_line_cor(config,set));
271  }
272 
273 
274  check_nomsg(cdb_set=cpl_frameset_new());
275  check_nomsg(tot_set=cpl_frameset_new());
276 
277 
278  sinfo_extract_mst_frames(set,cdb_set);
279  sinfo_bp_config_add(config);
280  nset=count_diff_ndit(set,&dit_val);
281 
282  check_nomsg(nraw=cpl_frameset_get_size(set));
283  check_nomsg(ncdb=cpl_frameset_get_size(cdb_set));
284 
285  for(i=0;i<nset;i++) {
286  check_nomsg(wrk_set=cpl_frameset_new());
287  check_nomsg(ref_dit=cpl_vector_get(dit_val,i));
288 
289 
290  for(j=0;j<nraw;j++) {
291  check_nomsg(tmp_frm=cpl_frameset_get_frame(set,j));
292  check_nomsg(strcpy(tmp_name,cpl_frame_get_filename(tmp_frm)));
293  plist=cpl_propertylist_load(tmp_name,0);
294  tmp_dit=sinfo_pfits_get_dit(plist);
295  sinfo_free_propertylist(&plist);
296 
297  if(tmp_dit==ref_dit) {
298  check_nomsg(cpl_frameset_insert(wrk_set,cpl_frame_duplicate(tmp_frm)));
299  }
300  }
301 
302  for(j=0;j<ncdb;j++) {
303  check_nomsg(cdb_frm=cpl_frameset_get_frame(cdb_set,j));
304  check_nomsg(cpl_frameset_insert(wrk_set,cpl_frame_duplicate(cdb_frm)));
305  }
306 
307  /* reduce data */
308  ck0(sinfo_dfs_set_groups(wrk_set),"Cannot indentify RAW and CALIB frames");
309 
310  /* Hidden parameters */
311  check_nomsg(p = cpl_parameterlist_find(config,"sinfoni.bp.method"));
312  check_nomsg(cpl_parameter_set_string(p,"Noise"));
313  ck0(sinfo_dfs_set_groups(set),"Cannot indentify RAW and CALIB frames") ;
314 
315  if (nset>1) {
316  sprintf(out_bpmap_name,"%s%d%s","out_bp_noise",i,".fits");
317  } else {
318  strcpy(out_bpmap_name,"out_bp_noise.fits");
319  }
320 
321  sinfo_msg("-----------------------------------------------");
322  sinfo_msg("BP_MAP_HP BAD PIXEL MAP DETERMINATION ");
323  sinfo_msg("-----------------------------------------------");
324 
325  ck0(sinfo_new_bp_search_noise(cpl_func,config,wrk_set,out_bpmap_name),
326  "computing BP_MAP_HP") ;
327  sinfo_msg("BP_MAP_HP BAD PIXEL MAP DETERMINATION SUCCESS") ;
328 
329  sinfo_msg("-----------------------------------------------");
330  sinfo_msg("MASTER DARK DETERMINATION ");
331  sinfo_msg("-----------------------------------------------");
332 
333  if (nset>1) {
334  sprintf(out_dark_name,"%s%d%s","out_dark",i,".fits");
335  } else {
336  strcpy(out_dark_name,"out_dark.fits");
337  }
338 
339  ck0(sinfo_new_dark(cpl_func,config, wrk_set, out_dark_name),
340  "Computing master dark") ;
341  sinfo_msg("MASTER DARK DETERMINATION SUCCESS") ;
342 
343  nred=cpl_frameset_get_size(wrk_set);
344  for(j=0;j<nred;j++) {
345  check_nomsg(tmp_frm=cpl_frameset_get_frame(wrk_set,j));
346  check_nomsg(cpl_frameset_insert(tot_set,cpl_frame_duplicate(tmp_frm)));
347  }
348  sinfo_free_frameset(&wrk_set);
349 
350  }
351 
352  check_nomsg(ntot=cpl_frameset_get_size(tot_set));
353  for(j=0;j<ntot;j++) {
354  check_nomsg(tmp_frm=cpl_frameset_get_frame(tot_set,j));
355  check_nomsg(cpl_frameset_insert(set,cpl_frame_duplicate(tmp_frm)));
356  }
357 
358  sinfo_free_frameset(&tot_set);
359  sinfo_free_my_vector(&dit_val);
360  sinfo_free_frameset(&cdb_set);
361 
362 
363 
364 
365 
366 
367  cleanup:
368 
369  sinfo_free_propertylist(&plist);
370  sinfo_free_frame(&dup_frm);
371  sinfo_free_frameset(&wrk_set);
372  sinfo_free_frameset(&tot_set);
373  sinfo_free_my_vector(&dit_val);
374  sinfo_free_frameset(&cdb_set);
375 
376 
377  if (cpl_error_get_code() != CPL_ERROR_NONE) {
378  return -1;
379  } else {
380  return 0;
381  }
382 
383 
384 
385 }
386 
387 static int count_diff_ndit(cpl_frameset *set, cpl_vector** dit_val)
388 {
389  cpl_frame* tmp_frm=NULL;
390  cpl_frame* dup_frm=NULL;
391 
392 
393  char tmp_name[FILE_NAME_SZ];
394  int nraw=0;
395  int i=0;
396  int ndit=1;
397  double ref_dit=0;
398  cpl_vector* dit=NULL;
399  cpl_propertylist* plist=NULL;
400 
401  nraw=cpl_frameset_get_size(set);
402  dit=cpl_vector_new(nraw);
403  for(i=0;i<nraw;i++) {
404  tmp_frm = cpl_frameset_get_frame(set, i);
405  strcpy(tmp_name,cpl_frame_get_filename(tmp_frm));
406  plist= cpl_propertylist_load(tmp_name,0);
407  cpl_vector_set(dit,i,sinfo_pfits_get_dit(plist));
408  sinfo_free_propertylist(&plist);
409  }
410 
411 
412  cpl_vector_sort(dit,1);
413  ref_dit=cpl_vector_get(dit,0);
414 
415 
416  for(i=1;i<nraw;i++) {
417  if(ref_dit != cpl_vector_get(dit,i)) {
418  ref_dit=cpl_vector_get(dit,i);
419  ndit++;
420  }
421  }
422  *dit_val=cpl_vector_new(ndit);
423  ref_dit=cpl_vector_get(dit,0);
424  cpl_vector_set(*dit_val,0,cpl_vector_get(dit,0));
425  ndit=1;
426 
427  for(i=1;i<nraw;i++) {
428  if(ref_dit != cpl_vector_get(dit,i)) {
429  cpl_vector_set(*dit_val,ndit,cpl_vector_get(dit,i));
430  ref_dit=cpl_vector_get(dit,i);
431  ndit++;
432  }
433  }
434 
435  cpl_vector_delete(dit);
436  cpl_frame_delete(dup_frm);
437 
438  return ndit;
439 }
440