SINFONI Pipeline Reference Manual  2.5.2
sinfo_bp_norm.c
1 /*
2  * This file is part of the ESO SINFONI Pipeline
3  * Copyright (C) 2004,2005 European Southern Observatory
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, 51 Franklin St, Fifth Floor, Boston, MA 02111-1307 USA
18  */
19 /*----------------------------------------------------------------------------
20 
21  File name : sinfo_bp_norm.c
22  Author : J. Schreiber
23  Created on : May 5, 2003
24  Description : Different methods for searching for bad pixels
25  used in the recipe sinfo_rec_mflat
26 
27  ---------------------------------------------------------------------------*/
28 
29 #ifdef HAVE_CONFIG_H
30 # include <config.h>
31 #endif
32 /*----------------------------------------------------------------------------
33  Includes
34  ---------------------------------------------------------------------------*/
35 #include "sinfo_bp_norm.h"
36 #include "sinfo_image_ops.h"
37 #include "sinfo_detlin.h"
38 #include "sinfo_badnorm_ini_by_cpl.h"
39 #include "sinfo_baddist_ini_by_cpl.h"
40 #include "sinfo_pro_save.h"
41 #include "sinfo_functions.h"
42 #include "sinfo_pro_types.h"
43 #include "sinfo_hidden.h"
44 #include "sinfo_error.h"
45 #include "sinfo_utils_wrappers.h"
52 /*----------------------------------------------------------------------------
53  Defines
54  ---------------------------------------------------------------------------*/
55 /*----------------------------------------------------------------------------
56  Function Definitions
57  ---------------------------------------------------------------------------*/
95 int
96 sinfo_new_bp_search_normal(const char* plugin_id, cpl_parameterlist* config,
97  cpl_frameset* sof, cpl_frameset* ref_set,
98  const char* procatg)
99 {
100  bad_config * cfg = NULL;
101  cpl_imagelist * image_list = NULL;
102  cpl_image ** med = NULL;
103  cpl_image * medImage = NULL;
104  cpl_image * medIm = NULL;
105  cpl_image * colImage = NULL;
106  cpl_image * compImage = NULL;
107  cpl_image * maskImage = NULL;
108  cpl_image * threshIm = NULL;
109 
110  Stats * stats = NULL;
111 
112  cpl_parameter *p = NULL;
113 
114  int no = 0;
115  float lo_cut = 0.;
116  float hi_cut = 0.;
117 
118  int i = 0;
119  int n = 0;
120  int half_box_size = 0;
121 
122  cpl_frameset* raw = NULL;
123  cpl_table* qclog_tbl = NULL;
124  char key_value[FILE_NAME_SZ];
125 
126  /* parse the file names and parameters to the bad_config data structure
127  cfg */
128 
129  sinfo_check_rec_status(0);
130  check_nomsg(raw = cpl_frameset_new());
131  sinfo_check_rec_status(1);
132  if (strcmp(procatg, PRO_BP_MAP_NO) == 0) {
133  cknull(cfg = sinfo_parse_cpl_input_badnorm(config, sof, procatg, &raw),
134  "could not parse cpl input!");
135  }
136  else if (strcmp(procatg, PRO_BP_MAP_DI) == 0) {
137  cknull(cfg = sinfo_parse_cpl_input_baddist(config, sof, procatg, &raw),
138  "could not parse cpl input!");
139  }
140  else if (strcmp(procatg, PRO_DEFAULT) == 0) {
141  cknull(cfg = sinfo_parse_cpl_input_badnorm(config, sof, procatg, &raw),
142  "could not parse cpl input!");
143  }
144  else {
145  sinfo_msg_error("Error: PRO.CATG %s, not supported!", procatg);
146  goto cleanup;
147  }
148  sinfo_check_rec_status(2);
149  /* take a clean mean of the frames */
150  sinfo_msg("Takes a clean mean of the frames");
151 
152  check_nomsg(image_list = cpl_imagelist_new());
153  sinfo_check_rec_status(3);
154 
155  for (i = 0; i < cfg->nframes; i++) {
156  if (sinfo_is_fits_file(cfg->framelist[i]) != 1) {
157  sinfo_msg_error("Input file %s is not FITS", cfg->framelist[i]);
158  goto cleanup;
159  }
160 
161  check_nomsg(
162  cpl_imagelist_set(image_list,
163  cpl_image_load(cfg->framelist[i],
164  CPL_TYPE_FLOAT, 0, 0),
165  i));
166 
167  }
168 
169  /* finally take the average image of the cube by
170  rejecting the extreme values */
171 
172  check_nomsg(no = cpl_imagelist_get_size(image_list));
173  lo_cut = (floor)(cfg->loReject * no + 0.5);
174  hi_cut = (floor)(cfg->hiReject * no + 0.5);
175  cknull(medImage=cpl_imagelist_collapse_minmax_create(image_list,
176  lo_cut,
177  hi_cut),
178  "error in sinfo_average_with_rejection");
179 
180  /* free memory */
181  sinfo_free_imagelist(&image_list);
182 
183  /*----------------------------------------------
184  * remove the intensity tilt from every column
185  * and compute the standard deviation on a rectangular zone
186  */
187  cknull(medIm = sinfo_new_thresh_image(medImage, cfg->mincut, cfg->maxcut),
188  "error sinfo_new_thresh_image");
189  cknull(colImage = sinfo_new_col_tilt( medIm, cfg->sigmaFactor ),
190  "sinfo_colTilt failed" );
191 
192  cknull(stats = sinfo_new_image_stats_on_rectangle(colImage,
193  cfg->loReject,
194  cfg->hiReject,
195  cfg->llx,
196  cfg->lly,
197  cfg->urx,
198  cfg->ury),
199  " sinfo_get_image_stats_on_vig failed");
200  if (stats != NULL ) {
201  sinfo_msg("Clean stdev: %f\n", stats->cleanstdev );
202  sinfo_msg("Clean mean: %f\n", stats->cleanmean );
203  }
204 
205  /*
206  cknull_nomsg(qclog_tbl = sinfo_qclog_init());
207  ck0_nomsg(sinfo_qclog_add_double(qclog_tbl,"QC ICTILT STDEV",
208  stats->cleanstdev,
209  "Intensity column clean stdev","%g"));
210 
211  ck0_nomsg(sinfo_qclog_add_double(qclog_tbl,"QC ICTILT MEAN",
212  stats->cleanmean,
213  "Intensity column clean mean","%g"));
214 
215  ck0(sinfo_pro_save_ima(colImage,raw,sof,
216  (char*) BP_NORM_INT_COL_TILT_CORR_OUT_FILENAME,
217  PRO_INT_COL_TILT_COR,qclog_tbl,plugin_id,config),
218  "cannot save ima %s",
219  BP_NORM_INT_COL_TILT_CORR_OUT_FILENAME);
220 
221  sinfo_free_table(&qclog_tbl);
222  */
223 
224  /* indicate pixels with great deviations from the clean mean as bad */
225  if (cfg->threshInd == 1) {
226  cknull(threshIm = sinfo_new_thresh_image(colImage,
227  stats->cleanmean-cfg->meanfactor*stats->cleanstdev,
228  stats->cleanmean+cfg->meanfactor*stats->cleanstdev),
229  " sinfo_threshImage failed" );
230 
231  }
232 
233  if (cfg->threshInd == 0) {
234  threshIm = colImage;
235  }
236  /* AMO here invalid fread? */
237  med = (cpl_image**) cpl_calloc(cfg->iterations, sizeof(cpl_image*));
238 
239  /* filter iteratively the images by a sinfo_median filter of the nearest
240  neighbors under the condition of a deviation greater than a factor
241  times the standard deviation */
242  sinfo_msg("Apply sinfo_median filter on pixel nearest neighbors");
243  if (cfg->methodInd == 1) {
244  if (cfg->factor > 0) {
245  cknull(med[0]=sinfo_new_median_image(threshIm,
246  -cfg->factor*stats->cleanstdev),
247  " sinfo_medianImage failed (1)" );
248 
249  for (i = 0; i < cfg->iterations - 1; i++) {
250  cknull(med[i + 1] = sinfo_new_median_image(med[i],
251  -cfg->factor* stats->cleanstdev),
252  "sinfo_medianImage failed (2)");
253  }
254 
255  }
256  else {
257  cknull(med[0] = sinfo_new_median_image(threshIm, -cfg->factor),
258  " sinfo_medianImage failed (1)" );
259 
260  for (i = 0; i < cfg->iterations - 1; i++) {
261  cknull(med[i+1] = sinfo_new_median_image(med[i], -cfg->factor),
262  " sinfo_medianImage failed (%d)",i );
263  }
264  }
265  }
266  else if (cfg->methodInd == 2) {
267 
268  cknull(med[0] = sinfo_new_abs_dist_image(threshIm, -cfg->factor),
269  " sinfo_absDistImage failed (1)" );
270 
271  for (i = 0; i < cfg->iterations - 1; i++) {
272  cknull(med[i+1] = sinfo_new_abs_dist_image(med[i], -cfg->factor),
273  " sinfo_absDistImage failed (2)" );
274  }
275  }
276  else if (cfg->methodInd == 3) {
277  cknull(
278  med[0] = sinfo_new_mean_image_in_spec(threshIm,
279  -cfg->factor * stats->cleanstdev),
280  "sinfo_meanImageInSpec failed (1)");
281  for (i = 0; i < cfg->iterations - 1; i++) {
282  cknull(
283  med[i + 1] = sinfo_new_mean_image_in_spec(med[i],
284  -cfg->factor * stats->cleanstdev),
285  " sinfo_meanImageInSpec failed (2)");
286  }
287  }
288  else if (cfg->methodInd == 4) {
289  half_box_size = (cfg->urx - cfg->llx) / 2;
290  cknull(med[0] = sinfo_new_local_median_image(threshIm,
291  -cfg->factor,
292  cfg->loReject,
293  cfg->hiReject,
294  half_box_size),
295  " sinfo_localMedianImage failed (1)" );
296 
297  for (i = 0; i < cfg->iterations - 1; i++) {
298  cknull(med[i+1] = sinfo_new_local_median_image(med[i],
299  -cfg->factor,
300  cfg->loReject,
301  cfg->hiReject,
302  half_box_size),
303  " sinfo_localMedianImage failed (2)" );
304  }
305  }
306  else {
307  sinfo_msg_error (" wrong indicator methodInd !" );
308  goto cleanup;
309  }
310 
311  /* compare the filtered image with the input image */
312  cknull(compImage = sinfo_new_compare_images(threshIm,
313  med[cfg->iterations - 1],
314  medImage),
315  " sinfo_compareImages failed" );
316 
317  /* generate the bad pixel mask */
318  sinfo_msg("Generates bad pixel map");
319  cknull(maskImage = sinfo_new_promote_image_to_mask( compImage, &n ),
320  " error in sinfo_promoteImageToMask" );
321  sinfo_msg("No of bad pixels: %d\n", n );
322 
323  cknull_nomsg(qclog_tbl = sinfo_qclog_init());
324  check_nomsg(p = cpl_parameterlist_find(config, "sinfoni.bp.method"));
325  snprintf(key_value, MAX_NAME_SIZE - 1, "%s", cpl_parameter_get_string(p));
326 
327  ck0_nomsg(
328  sinfo_qclog_add_string(qclog_tbl, "QC BP-MAP METHOD",
329  key_value, "BP search method", "%s"));
330 
331  ck0_nomsg(
332  sinfo_qclog_add_int(qclog_tbl, "QC BP-MAP NBADPIX", n,
333  "No of bad pixels", "%d"));
334 
335  ck0(
336  sinfo_pro_save_ima(maskImage, ref_set, sof, cfg->outName,
337  procatg, qclog_tbl, plugin_id, config),
338  "cannot save ima %s", cfg->outName);
339 
340  sinfo_free_table(&qclog_tbl);
341  sinfo_free_image(&maskImage);
342  sinfo_free_image(&compImage);
343  sinfo_free_image_array(&med, cfg->iterations);
344  /*
345  if (med != NULL) {
346  for ( i = 0 ; i < cfg->iterations ; i++ ) {
347  if(med[i] != NULL) {
348  cpl_image_delete(med[i]) ;
349  med[i]=NULL;
350  }
351  }
352  cpl_free(med) ;
353  med=NULL;
354  }
355  */
356  if (stats != NULL ) {
357  cpl_free(stats);
358  stats = NULL;
359  }
360  sinfo_free_image(&medIm);
361  sinfo_free_image(&medImage);
362  sinfo_free_image(&colImage);
363  if (cfg->threshInd == 1) {
364  sinfo_free_image(&threshIm);
365  }
366  sinfo_badnorm_free(&cfg);
367  sinfo_free_frameset(&raw);
368  return 0;
369 
370  cleanup:
371 
372  sinfo_free_image_array(&med, cfg->iterations);
373  /*
374  if (med != NULL) {
375  for ( i = 0 ; i < cfg->iterations ; i++ ) {
376  if(med[i] != NULL) {
377  cpl_image_delete(med[i]) ;
378  med[i]=NULL;
379  }
380  }
381  cpl_free(med) ;
382  med=NULL;
383  }
384  */
385  sinfo_free_image(&compImage);
386  sinfo_free_image(&maskImage);
387  sinfo_free_image(&threshIm);
388  sinfo_free_table(&qclog_tbl);
389  sinfo_free_image(&threshIm);
390  if (stats != NULL ) {
391  cpl_free(stats);
392  stats = NULL;
393  }
394 
395  sinfo_free_image(&medIm);
396  sinfo_free_image(&medImage);
397  sinfo_free_image(&colImage);
398  sinfo_free_imagelist(&image_list);
399  sinfo_free_frameset(&raw);
400  sinfo_badnorm_free(&cfg);
401 
402  return -1;
403 
404 }
405