SINFONI Pipeline Reference Manual  2.6.0
sinfo_bp_lin.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_lin.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 spiffi_badsearch
26 
27  ---------------------------------------------------------------------------*/
28 #ifdef HAVE_CONFIG_H
29 # include <config.h>
30 #endif
31 
32 /*----------------------------------------------------------------------------
33  Includes
34  ---------------------------------------------------------------------------*/
35 //#include <cpl_imagelist_basic.h>
36 #include <cpl.h>
37 #include "sinfo_bp_lin.h"
38 #include "sinfo_detlin.h"
39 #include "sinfo_detlin_ini_by_cpl.h"
40 #include "sinfo_pro_save.h"
41 #include "sinfo_pro_types.h"
42 #include "sinfo_functions.h"
43 #include "sinfo_hidden.h"
44 #include "sinfo_error.h"
45 #include "sinfo_utils_wrappers.h"
46 
47 
54 /*----------------------------------------------------------------------------
55  Defines
56  ---------------------------------------------------------------------------*/
57 /*----------------------------------------------------------------------------
58  Function Definitions
59  ---------------------------------------------------------------------------*/
86 int
87 sinfo_new_bp_search_lin(const char* plugin_id,
88  cpl_parameterlist* config,
89  cpl_frameset* sof)
90 {
91  detlin_config * cfg=NULL;
92  cpl_imagelist * imgl=NULL ;
93  cpl_imagelist * coeffs_imgl=NULL;
94  cpl_image * img_tmp=NULL ;
95  cpl_image * mask=NULL;
96  cpl_vector* cube_mean=NULL;
97  cpl_frameset* raw=NULL;
98  cpl_frameset* raw_on=NULL;
99  cpl_frameset* raw_of=NULL;
100  cpl_frame* frm=NULL;
101  cpl_frame* frm_dup=NULL;
102 
103  cpl_table* qclog_tbl=NULL;
104 
105  cpl_parameter *p=NULL;
106  cpl_polynomial *pol=NULL;
107  cpl_vector* vec_adl=NULL;
108  cpl_vector* vec_med=NULL;
109  double* mse=NULL;
110  int i=0;
111  int n_bad=0 ;
112  int nff=0;
113  char key_value[FILE_NAME_SZ];
114  char key_name[FILE_NAME_SZ];
115  cpl_table* det_lin=NULL;
116  int nraw=0;
117  int* status=NULL;
118  cpl_table* gain=NULL;
119  int ngain=0;
120  int sz_imgl=0;
121  /*
122  int llx=270;
123  int lly=1030;
124  int urx=310;
125  int ury=1060;
126  */
127  //int zone[4];
128  //int kappa=5;
129  //int nclip=25;
130  int pdensity=0;
131 
132 
133  check_nomsg(p=cpl_parameterlist_find(config,"sinfoni.product.density"));
134  check_nomsg(pdensity=cpl_parameter_get_int(p));
135 
136 
137  /* parse the file names and parameters to the bad_config
138  data structure cfg */
139  check_nomsg(raw=cpl_frameset_new());
140  cknull(cfg = sinfo_parse_cpl_input_detlin(config,sof,&raw),
141  "could not parse .ini file!");
142 
143  /* ======================================================================
144  DETERMINES LINEARITY COEFF AS DFO DOES
145  ======================================================================
146  */
147  nff=cpl_frameset_get_size(raw);
148  raw_on=cpl_frameset_new();
149  raw_of=cpl_frameset_new();
150  /* separates on and off frames */
151 
152  for(i=0;i<nff;i++) {
153  frm=cpl_frameset_get_frame(raw,i);
154  frm_dup=cpl_frame_duplicate(frm);
155  if(sinfo_frame_is_on(frm)) {
156  cpl_frameset_insert(raw_on,frm_dup);
157  } else {
158  cpl_frameset_insert(raw_of,frm_dup);
159  }
160  }
161  /*
162  cknull(det_lin=sinfo_get_linearity(raw_on,raw_of),
163  "Error computing linearity");
164  */
165  /*
166  zone[0]=llx;
167  zone[1]=lly;
168  zone[2]=urx;
169  zone[3]=ury;
170  */
171  //If one refers to a larger area
172  /*
173  zone[0]=20;
174  zone[1]=2028;
175  zone[2]=20;
176  zone[3]=2028;
177  */
178  /*
179  cknull(det_lin=irplib_compute_linearity(raw_on,raw_of),
180  "Error computing linearity");
181  */
182 
183  if(pdensity > 1 ) {
184 
185  sinfo_msg("Computes linearity");
186  cknull(det_lin=sinfo_compute_linearity(raw_on,raw_of),
187  "Error computing linearity");
188 
189  check_nomsg(nraw=cpl_table_get_nrow(det_lin));
190  check_nomsg(vec_adl=cpl_vector_new(nraw));
191  check_nomsg(vec_med=cpl_vector_new(nraw));
192 
193  for(i=0;i<nraw;i++) {
194  cpl_vector_set(vec_adl,i,cpl_table_get_double(det_lin,"adl",i,status));
195  cpl_vector_set(vec_med,i,cpl_table_get_double(det_lin,"med",i,status));
196  }
197  check_nomsg(pol=sinfo_polynomial_fit_1d_create(vec_adl,vec_med,
198  cfg->order,mse));
199  sinfo_free_my_vector(&vec_adl);
200  sinfo_free_my_vector(&vec_med);
201 
202  cknull_nomsg(qclog_tbl=sinfo_qclog_init());
203  cpl_size deg=0;
204  for(deg=0;deg<cfg->order+1;deg++) {
205 
206  snprintf(key_name,MAX_NAME_SIZE-1,"%s%" CPL_SIZE_FORMAT "%s",
207  "QC BP-MAP LIN",deg," MED");
208  sinfo_qclog_add_double(qclog_tbl,key_name,
209  cpl_polynomial_get_coeff(pol,&deg),
210  "Linearity Polynomial Coeff","%g");
211  }
212  sinfo_free_polynomial(&pol);
213 
214  check_nomsg(p = cpl_parameterlist_find(config, "sinfoni.bp.method"));
215  snprintf(key_value, MAX_NAME_SIZE-1,"%s",cpl_parameter_get_string(p));
216  ck0_nomsg(sinfo_qclog_add_string(qclog_tbl,"QC BP-MAP METHOD",key_value,
217  "BP search method","%s"));
218 
219  ck0(sinfo_pro_save_tbl(det_lin,raw,sof,BP_LIN_LIN_DET_INFO_OUT_FILENAME,
220  PRO_LIN_DET_INFO,qclog_tbl,plugin_id,config),
221  "cannot dump ims %s",BP_LIN_LIN_DET_INFO_OUT_FILENAME);
222 
223  sinfo_free_table(&det_lin);
224  sinfo_free_table(&qclog_tbl);
225  }
226 
227  /*=======================================================*/
228  if(pdensity > 1 ) {
229  sinfo_msg("Computes gain");
230 
231  cknull(gain=sinfo_compute_gain(raw_on,raw_of),"Error computing gain");
232 
233  //cknull(gain=irplib_compute_gain(raw_on,raw_of,zone,kappa,nclip),
234  // "Error computing gain");
235 
236  sinfo_free_frameset(&raw_on);
237  sinfo_free_frameset(&raw_of);
238 
239  check_nomsg(ngain=cpl_table_get_nrow(gain));
240  cknull_nomsg(qclog_tbl=sinfo_qclog_init());
241 
242  for(i=0;i<ngain;i++) {
243  snprintf(key_name,MAX_NAME_SIZE-1,"%s%i","QC GAIN",i);
244  ck0_nomsg(sinfo_qclog_add_double(qclog_tbl,key_name,
245  cpl_table_get_double(gain,"gain",i,status),
246  "Detector gain","%g"));
247  }
248 
249  ck0_nomsg(sinfo_qclog_add_double(qclog_tbl,"QC GAIN",
250  cpl_table_get_column_median(gain,"gain"),
251  "Detector gain median","%g"));
252  ck0_nomsg(sinfo_qclog_add_double(qclog_tbl,"QC GAINERR",
253  cpl_table_get_column_stdev(gain,"gain"),
254  "Detector gain error","%g"));
255 
256  ck0(sinfo_pro_save_tbl(gain,raw,sof,BP_LIN_GAIN_OUT_FILENAME,
257  PRO_GAIN_INFO,qclog_tbl,plugin_id,config),
258  "cannot dump tbl %s", BP_LIN_GAIN_OUT_FILENAME);
259 
260  sinfo_free_table(&gain);
261  sinfo_free_table(&qclog_tbl);
262  }
263 
264  /* =======================================================================
265  DETERMINES LINEARITY COEFF AS MPE DOES
266  =======================================================================
267  */
268 
269 
270  check_nomsg(imgl = cpl_imagelist_new());
271  for ( i = 0 ; i < cfg->nframes ; i++ )
272  {
273  if(sinfo_is_fits_file (cfg->framelist[i]) != 1) {
274  sinfo_msg_error("Input file %s is not FITS",cfg->framelist[i] );
275  return -1;
276  }
277  check_nomsg(img_tmp=cpl_image_load(cfg->framelist[i],CPL_TYPE_FLOAT,0,0));
278  check_nomsg(cpl_imagelist_set(imgl,img_tmp,i));
279  }
280 
281  /*----------------------------------------------------------------
282  *---------------------- SEARCH FOR BAD PIXELS---------------------
283  *--------------------------------------------------------------*/
284  sinfo_msg("Search for bad pixels");
285  cknull(coeffs_imgl=sinfo_new_fit_intensity_course(imgl,
286  cfg->order,
287  cfg->loReject,
288  cfg->hiReject),
289  "could not fit polynomial and store coeffs in a data cube!");
290 
291  sinfo_free_imagelist(&imgl) ;
292  check_nomsg(sz_imgl=cpl_imagelist_get_size(coeffs_imgl));
293 
294 
295  /*---store the polynomial fit coefficients in a data cube----*/
296  /* CUBE NOT DUMPED BECAUSE PROBLEMS ON LAST PLANE */
297  cube_mean=cpl_vector_new(sz_imgl);
298 
299  /* QC LOG */
300  cknull_nomsg(qclog_tbl=sinfo_qclog_init());
301  for(i=0;i<sz_imgl;i++) {
302  cpl_vector_set(cube_mean,i,
303  cpl_image_get_median(cpl_imagelist_get(coeffs_imgl,i)));
304 
305  snprintf(key_name,MAX_NAME_SIZE-1,"%s%i%s","QC BP-MAP LIN",i," MEAN");
306  snprintf(key_value,MAX_NAME_SIZE-1,"%g",cpl_vector_get(cube_mean,i));
307  ck0_nomsg(sinfo_qclog_add_double(qclog_tbl,key_name,
308  cpl_vector_get(cube_mean,i),
309  "Linearity Polynomial Coeff","%g"));
310 
311  }
312  sinfo_free_my_vector(&cube_mean);
313  check_nomsg(p = cpl_parameterlist_find(config, "sinfoni.bp.method"));
314  snprintf(key_value, MAX_NAME_SIZE-1,"%s",cpl_parameter_get_string(p));
315  ck0_nomsg(sinfo_qclog_add_string(qclog_tbl,"QC BP-MAP METHOD",key_value,
316  "BP search method","%s"));
317 
318 
319  ck0(sinfo_pro_save_ims(coeffs_imgl,raw,sof,cfg->coeffsCubeName,
320  PRO_BP_COEFF,qclog_tbl,plugin_id,config),
321  "cannot dump ims %s", cfg->coeffsCubeName);
322 
323  sinfo_free_table(&qclog_tbl);
324 
325  /* =======================================================================
326  DETERMINES BAD PIXEL MAP
327  =======================================================================
328  */
329  sinfo_msg("Generates bad pixel map");
330  cknull(mask = sinfo_new_search_bad_pixels (coeffs_imgl,
331  cfg->threshSigmaFactor,
332  cfg->nonlinearThresh,
333  cfg->loReject,
334  cfg->hiReject),
335  "could not create bad pixel mask!") ;
336 
337  sinfo_free_imagelist(&coeffs_imgl) ;
338 
339  check_nomsg(n_bad = sinfo_new_count_bad_pixels(mask)) ;
340  sinfo_msg( "No of bad pixels: %d", n_bad ) ;
341 
342  /* QC LOG */
343  cknull_nomsg(qclog_tbl=sinfo_qclog_init());
344  check_nomsg(p = cpl_parameterlist_find(config, "sinfoni.bp.method"));
345  snprintf(key_value, MAX_NAME_SIZE-1,"%s",cpl_parameter_get_string(p));
346  ck0_nomsg(sinfo_qclog_add_string(qclog_tbl,"QC BP-MAP METHOD",key_value,
347  "BP search method","%s"));
348 
349  ck0_nomsg(sinfo_qclog_add_int(qclog_tbl,"QC BP-MAP NBADPIX",n_bad,
350  "No of bad pixels","%d"));
351 
352  ck0(sinfo_pro_save_ima(mask,raw,sof,cfg->outName,PRO_BP_MAP_NL,qclog_tbl,
353  plugin_id,config),
354  "cannot save ima %s", cfg->outName);
355 
356 
357  /* ======================================================================
358  FREE MEMORY
359  ======================================================================
360  */
361 
362 
363 
364  sinfo_free_table(&qclog_tbl);
365  sinfo_free_image(&mask);
366  sinfo_free_frameset(&raw);
367  sinfo_detlin_free(&cfg);
368 
369  return 0;
370 
371  cleanup:
372  sinfo_free_frameset(&raw_on);
373  sinfo_free_frameset(&raw_of);
374 
375  sinfo_free_image(&mask) ;
376  sinfo_free_my_vector(&cube_mean);
377  sinfo_free_imagelist(&imgl);
378  sinfo_free_imagelist(&coeffs_imgl);
379  sinfo_free_table(&gain);
380  sinfo_free_table(&qclog_tbl);
381  sinfo_free_table(&det_lin);
382  sinfo_free_my_vector(&vec_adl);
383  sinfo_free_my_vector(&vec_med);
384  sinfo_free_polynomial(&pol);
385  sinfo_detlin_free(&cfg);
386  sinfo_free_frameset(&raw);
387 
388  return -1;
389 
390 
391 }
#define sinfo_msg_error(...)
Print an error message.
Definition: sinfo_msg.h:69