SINFONI Pipeline Reference Manual  2.6.0
sinfo_new_stdstar.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_new_stdstar.c
22  Author : J. Schreiber
23  Created on : December 3, 2003
24  Description : this routine doess the optimal extraction of a spectrum
25  of an already reduced data cube of a standard star
26  observation. Additionally, a conversion factor from
27  mag to counts/sec can be determined if the magnitude
28  of the standard star is known.
29  This is done for a number of jittered data cubes and
30  the results are averaged by rejecting the extreme
31  values
32 
33  ---------------------------------------------------------------------------*/
34 
35 #ifdef HAVE_CONFIG_H
36 # include <config.h>
37 #endif
38 /*----------------------------------------------------------------------------
39  Includes
40  ---------------------------------------------------------------------------*/
41 #include <math.h>
42 
43 //Used only for cpl_propertylist_has
44 #include <irplib_stdstar.h>
45 #include <sinfo_cpl_size.h>
46 #include "irplib_utils.h"
47 #include "sinfo_dfs.h"
48 #include "sinfo_tpl_utils.h"
49 
50 #include "sinfo_new_stdstar.h"
51 #include "sinfo_standstar_ini_by_cpl.h"
52 #include "sinfo_pro_save.h"
53 #include "sinfo_pfits.h"
54 #include "sinfo_utilities_scired.h"
55 #include "sinfo_spectrum_ops.h"
56 #include "sinfo_hidden.h"
57 #include "sinfo_functions.h"
58 #include "sinfo_utl_efficiency.h"
59 #include "sinfo_error.h"
60 #include "sinfo_utils_wrappers.h"
61 /*----------------------------------------------------------------------------
62  Defines
63  ---------------------------------------------------------------------------*/
64 
65 
66 /*----------------------------------------------------------------------------
67  Function Definitions
68  ---------------------------------------------------------------------------*/
69 /* temporally commented out as not yet used
70 static int
71 sinfo_compute_efficiency(cpl_frameset* sof,
72  const char* name,
73  standstar_config ** cfg,
74  cpl_imagelist * cube,
75  cpl_table** tbl_spectrum);
76 
77 */
78 
86 /*----------------------------------------------------------------------------
87  Function : sinfo_stdstar()
88  In : ini_file: file name of according .ini file
89  Out : integer (0 if it worked, -1 if it doesn't)
90  Job : this routine carries through the data cube creation of an
91  object science observation using object-sky nodding
92  and jittering. This script expects jittered frames that
93  were already sky-subtracted
94  averaged, flatfielded, spectral tilt corrected and
95  interleaved if necessary
96  ---------------------------------------------------------------------------*/
97 int
98 sinfo_new_stdstar(const char* plugin_id,
99  cpl_parameterlist* config,
100  cpl_frameset* sof,cpl_frameset* ref_set)
101 {
102 
103  cpl_errorstate clean_state = cpl_errorstate_get();
104  standstar_config * cfg=NULL ;
105  cpl_imagelist * cube=NULL ;
106 
107  cpl_imagelist * list_object=NULL ;
108  cpl_image ** spectrum=NULL ;
109  cpl_image * img_spct=NULL ;
110 
111 
112 
113  cpl_frameset* raw=NULL;
114  cpl_frame* frame=NULL;
115  cpl_image* std_med_ima=NULL;
116  cpl_image* std_med_dup=NULL;
117 
118  cpl_table* qclog_tbl=NULL;
119  cpl_table* tbl_spectrum=NULL;
120  cpl_propertylist* plist=NULL;
121  //char band[FILE_NAME_SZ];
122 
123  char * name=NULL ;
124  int fra=0;
125  float exptime=0 ;
126  double convfactor=0;
127  double cleanfactor=0;
128  float* factor=NULL;
129 
130  char std_med_filename[MAX_NAME_SIZE];
131  char std_cub_filename[MAX_NAME_SIZE];
132 
133  double max_ima_cx=0;
134  double max_ima_cy=0;
135  cpl_size max_ima_x=0;
136  cpl_size max_ima_y=0;
137  double norm=0;
138  double xcen=0;
139  double ycen=0;
140  double sig_x=0;
141  double sig_y=0;
142  double fwhm_x=0;
143  double fwhm_y=0;
144  double disp=0;
145  /* double dispersion=0; */
146  int i=0;
147  int wllx=0;
148  int wlly=0;
149  int wurx=0;
150  int wury=0;
151  int psf_sz=40;
152  int qc_info=0;
153  int ima_szx=0;
154  int ima_szy=0;
155  int check1=0;
156  int check2=0;
157  int check3=0;
158  int check4=0;
159  double xshift=0;
160  double yshift=0;
161 
162  float cenpix = 0;
163  float cenLambda = 0;
164 
165  int no=0;
166  double lo_cut=0.;
167  double hi_cut=0.;
168  /*
169  const char* stdstars=NULL;
170  const char* sed=NULL;
171  */
172  cpl_frame* frm_sci=NULL;
173  cpl_frame* frm_atmext=NULL;
174  cpl_frame* frm_std_cat=NULL;
175  cpl_table* tot_eff=NULL;
176  double fpar[7];
177  double dpar[7];
178  int mpar[7];
179  int do_compute_eff=0;
180  cpl_parameter* p=NULL;
181 
182 
183  //For new way to compute efficiency
184  //char band[80];
185  //const char * seds_file=NULL;
186  //const char * filter=NULL;
187  //cpl_table* tbl_eff=NULL;
188  /*
189  parse the file names and parameters to the cube_config
190  data structure cfg
191  */
192 
193  /* sinfo_msg("Parse cpl input"); */
194  check_nomsg(raw=cpl_frameset_new());
195 
196  cknull(cfg=sinfo_parse_cpl_input_standstar(config,sof,&raw),
197  "could not parse cpl input!") ;
198 
199 
200  cknull_nomsg(p = cpl_parameterlist_find(config,
201  "sinfoni.std_star.compute_eff"));
202 
203  check_nomsg(do_compute_eff = cpl_parameter_get_bool(p));
204  cknull(list_object = cpl_imagelist_new (),
205  "could not allocate memory");
206 
207  /*
208  sed = sinfo_extract_filename(sof, SINFO_CALIB_SED) ;
209  stdstars = sinfo_extract_filename(sof, SINFO_CALIB_STDSTARS) ;
210  */
211 
212  if (cfg->convInd == 1) {
213  factor = sinfo_new_floatarray(cfg->nframes);
214  }
215 
216  if(NULL != cpl_frameset_find(sof,PRO_COADD_STD)) {
217  frame = cpl_frameset_find(sof,PRO_COADD_STD);
218  strcpy(std_cub_filename,cpl_frame_get_filename(frame));
219  } else if(NULL != cpl_frameset_find(sof,PRO_COADD_PSF)) {
220  frame = cpl_frameset_find(sof,PRO_COADD_PSF);
221  strcpy(std_cub_filename,cpl_frame_get_filename(frame));
222  } else if(NULL != cpl_frameset_find(sof,PRO_COADD_OBJ)) {
223  frame = cpl_frameset_find(sof,PRO_COADD_OBJ);
224  strcpy(std_cub_filename,cpl_frame_get_filename(frame));
225  } else if(NULL != cpl_frameset_find(sof,PRO_COADD_PUPIL)) {
226  frame = cpl_frameset_find(sof,PRO_COADD_PUPIL);
227  strcpy(std_cub_filename,cpl_frame_get_filename(frame));
228  } else {
229  sinfo_msg_error("Frame %s, %s, %s or %s not found! Exit!",
230  PRO_COADD_STD,PRO_COADD_PSF,
231  PRO_COADD_OBJ,PRO_COADD_PUPIL );
232  goto cleanup;
233  }
234 
235 
236  cknull(plist = cpl_propertylist_load(std_cub_filename, 0),
237  "getting header from reference ima frame %s",std_cub_filename);
238 
239  cenpix = sinfo_pfits_get_crpix3(plist);
240  cenLambda = sinfo_pfits_get_crval3(plist);
241  /* dispersion = sinfo_pfits_get_cdelt3(plist); */
242 
243 
244  if (cpl_propertylist_has(plist, KEY_NAME_CDELT3)) {
245  disp=cpl_propertylist_get_double(plist, KEY_NAME_CDELT3);
246  } else {
247  sinfo_msg_warning("Keyword %s not found.",KEY_NAME_CDELT3);
248  }
249 
250 
251  sinfo_free_propertylist(&plist) ;
252 
253  /* we find automatiocally extraction parameters */
254 
255 
256  if(NULL != cpl_frameset_find(sof,PRO_MED_COADD_STD)) {
257  frame = cpl_frameset_find(sof,PRO_MED_COADD_STD);
258  strcpy(std_med_filename,cpl_frame_get_filename(frame));
259  check_nomsg(std_med_ima=cpl_image_load(std_med_filename,
260  CPL_TYPE_FLOAT,0,0));
261  } else if(NULL != cpl_frameset_find(sof,PRO_OBS_STD)) {
262  check_nomsg(frame = cpl_frameset_find(sof,PRO_OBS_STD));
263  strcpy(std_cub_filename,cpl_frame_get_filename(frame));
264  check_nomsg(cube = cpl_imagelist_load(std_cub_filename,
265  CPL_TYPE_FLOAT,0));
266  strcpy(std_med_filename,STDSTAR_OUT_MED_CUBE);
267  check_nomsg(std_med_ima=cpl_imagelist_collapse_median_create(cube));
268  sinfo_free_imagelist(&cube);
269 
270  ck0(sinfo_pro_save_ima(std_med_ima,ref_set,sof,STDSTAR_OUT_MED_CUBE,
271  PRO_MED_OBS_PSF,NULL,plugin_id,config),
272  "Error saving image %s tag %s",STDSTAR_OUT_MED_CUBE,PRO_MED_OBS_PSF);
273 
274  } else if(NULL != cpl_frameset_find(sof,PRO_MED_COADD_PSF)) {
275  check_nomsg(frame = cpl_frameset_find(sof,PRO_MED_COADD_PSF));
276  strcpy(std_med_filename,cpl_frame_get_filename(frame));
277  check_nomsg(std_med_ima=cpl_image_load(std_med_filename,
278  CPL_TYPE_FLOAT,0,0));
279  } else if(NULL != cpl_frameset_find(sof,PRO_OBS_PSF)) {
280  check_nomsg(frame = cpl_frameset_find(sof,PRO_OBS_PSF));
281  strcpy(std_cub_filename,cpl_frame_get_filename(frame));
282  check_nomsg(cube=cpl_imagelist_load(std_cub_filename,CPL_TYPE_FLOAT,0));
283  strcpy(std_med_filename,STDSTAR_OUT_MED_CUBE);
284  check_nomsg(std_med_ima=cpl_imagelist_collapse_median_create(cube));
285  sinfo_free_imagelist(&cube);
286 
287  ck0(sinfo_pro_save_ima(std_med_ima,ref_set,sof,STDSTAR_OUT_MED_CUBE,
288  PRO_MED_OBS_PSF,NULL,plugin_id,config),
289  "Error saving image %s tag %s",STDSTAR_OUT_MED_CUBE,PRO_MED_OBS_PSF);
290 
291  } else if(NULL != cpl_frameset_find(sof,PRO_MED_COADD_OBJ)) {
292  check_nomsg(frame = cpl_frameset_find(sof,PRO_MED_COADD_OBJ));
293  strcpy(std_med_filename,cpl_frame_get_filename(frame));
294  check_nomsg(std_med_ima=cpl_image_load(std_med_filename,
295  CPL_TYPE_FLOAT,0,0));
296  } else if(NULL != cpl_frameset_find(sof,PRO_OBS_OBJ)) {
297  check_nomsg(frame = cpl_frameset_find(sof,PRO_OBS_OBJ));
298  strcpy(std_cub_filename,cpl_frame_get_filename(frame));
299  check_nomsg(cube = cpl_imagelist_load(std_cub_filename,
300  CPL_TYPE_FLOAT,0));
301  strcpy(std_med_filename,STDSTAR_OUT_MED_CUBE);
302  check_nomsg(std_med_ima=cpl_imagelist_collapse_median_create(cube));
303  sinfo_free_imagelist(&cube);
304 
305  ck0(sinfo_pro_save_ima(std_med_ima,ref_set,sof,STDSTAR_OUT_MED_CUBE,
306  PRO_MED_OBS_OBJ,NULL,plugin_id,config),
307  "Error saving image %s tag %s",STDSTAR_OUT_MED_CUBE,PRO_MED_OBS_OBJ);
308  } else {
309  sinfo_msg_error("Frame %s %s %s %s %s %s not found! Exit!",
310  PRO_MED_COADD_STD, PRO_OBS_STD,
311  PRO_MED_COADD_PSF, PRO_OBS_PSF,
312  PRO_MED_COADD_OBJ, PRO_OBS_OBJ);
313  goto cleanup;
314  }
315 
316 
317  check_nomsg(std_med_dup=cpl_image_duplicate(std_med_ima));
318  sinfo_clean_nan(&std_med_dup);
319  ima_szx=cpl_image_get_size_x(std_med_ima);
320  ima_szy=cpl_image_get_size_y(std_med_ima);
321  /* TODO: Here we remove 3 pixels at the image margin to prevent problems
322  * with spikes at the image borders that could be more intense than the PSF
323  * image peak. On the other side this does not prevent that other pixels
324  * in the image are flagged. Better would be to flag any bad pixel/outlier
325  * using HDRL.
326  */
327  int margin=3;
328  check_nomsg(cpl_image_get_maxpos_window(std_med_dup,
329  margin,margin,ima_szx-margin,ima_szy-margin,
330  &max_ima_x,&max_ima_y));
331  sinfo_free_image(&std_med_dup);
332 
333 
334 
335  wllx= ((max_ima_x-psf_sz)>0) ? (max_ima_x-psf_sz) : 1;
336  wlly= ((max_ima_y-psf_sz)>0) ? (max_ima_y-psf_sz) : 1;
337  wurx= ((max_ima_x+psf_sz)<ima_szx) ? (max_ima_x+psf_sz) : ima_szx ;
338  wury= ((max_ima_y+psf_sz)<ima_szy) ? (max_ima_y+psf_sz) : ima_szy ;
339  /*
340  sinfo_msg("wllx=%d wlly=%d wurx=%d wury=%d\n",wllx,wlly,wurx,wury);
341  cpl_image_get_maxpos_window(std_med_ima,wllx,wlly,wurx,wury,
342  &max_ima_x,&max_ima_y);
343  */
344  check_nomsg(qclog_tbl = sinfo_qclog_init());
345  check_nomsg(max_ima_cx=cpl_image_get_centroid_x_window(std_med_ima,wllx,
346  wlly,wurx,wury));
347  check_nomsg(max_ima_cy=cpl_image_get_centroid_y_window(std_med_ima,wllx,
348  wlly,wurx,wury));
349 
350  xshift=max_ima_cx-ima_szx/2;
351  yshift=max_ima_cy-ima_szy/2;
352 
353  ck0_nomsg(sinfo_qclog_add_double(qclog_tbl,"QC SHIFTX",xshift,
354  "X shift centroid - center image","%f"));
355 
356  ck0_nomsg(sinfo_qclog_add_double(qclog_tbl,"QC SHIFTY",yshift,
357  "Y shift centroid - center image","%f"));
358 
359  if(
360  ((max_ima_x-psf_sz) < 1) ||
361  ((max_ima_y-psf_sz) < 1) ||
362  ((max_ima_x+psf_sz) > ima_szx) ||
363  ((max_ima_x+psf_sz) > ima_szy)
364  )
365  {
366  psf_sz = (psf_sz < (max_ima_x-1)) ? psf_sz : (max_ima_x-1);
367  psf_sz = (psf_sz < (max_ima_y-1)) ? psf_sz : (max_ima_y-1);
368  psf_sz = (psf_sz < ima_szx-max_ima_x) ? psf_sz : (ima_szx-max_ima_x);
369  psf_sz = (psf_sz < ima_szy-max_ima_y) ? psf_sz : (ima_szy-max_ima_y);
370  //added to prevent seg fault by cpl_image_fit_gaussian
371  psf_sz = (psf_sz > 4) ? psf_sz : 4;
372  }
373 
374  ck0_nomsg(sinfo_qclog_add_int(qclog_tbl,"QC FWHM LLX",cfg->llx,
375  "STD star FWHM LLX","%d"));
376  ck0_nomsg(sinfo_qclog_add_int(qclog_tbl,"QC FWHM LLY",cfg->lly,
377  "STD star FWHM LLY","%d"));
378  ck0_nomsg(sinfo_qclog_add_int(qclog_tbl,"QC FWHM HBX",cfg->halfbox_x,
379  "STD star FWHM HBX","%d"));
380  ck0_nomsg(sinfo_qclog_add_int(qclog_tbl,"QC FWHM HBX",cfg->halfbox_y,
381  "STD star FWHM HBY","%d"));
382 
383 
384  /* call the 2D-Gaussian fit routine */
385  for ( i = 0 ; i < 7 ; i++ )
386  {
387  mpar[i] = 1 ;
388  }
389 
390 
391  if(-1 == sinfo_new_fit_2d_gaussian(std_med_ima,
392  fpar,
393  dpar,
394  mpar,
395  cfg->llx,
396  cfg->lly,
397  cfg->halfbox_x,
398  cfg->halfbox_y,
399  &check4 ) ) {
400  irplib_error_recover(clean_state,"2d Gaussian fit failed");
401  /* return 0; */
402 
403  } else {
404 
405  ck0_nomsg(sinfo_qclog_add_double(qclog_tbl,"QC FWHM MAJ",fpar[4],
406  "STD star FWHM on major axis","%f"));
407  ck0_nomsg(sinfo_qclog_add_double(qclog_tbl,"QC FWHM MIN",fpar[5],
408  "STD star FWHM on minor axis","%f"));
409  ck0_nomsg(sinfo_qclog_add_double(qclog_tbl,"QC THETA",fpar[6],
410  "STD star ellipsis angle theta","%f"));
411 
412 
413  }
414 
415  /*
416  sinfo_msg("Gauss fit params: xc,yc,amp,bkg,fwhm_x,fwhm_y,angle\n");
417  for ( i = 0 ; i < 7 ; i++ )
418  {
419  sinfo_msg("fpar[%d]=%f dpar[%d]=%f\n",i,fpar[i],i,dpar[i]);
420  }
421  */
422 
423  if(CPL_ERROR_NONE == cpl_image_fit_gaussian(std_med_ima,
424  max_ima_x,
425  max_ima_y,
426  psf_sz,
427  &norm,
428  &xcen,
429  &ycen,
430  &sig_x,
431  &sig_y,
432  &fwhm_x,
433  &fwhm_y)) {
434 
435 
436  ck0_nomsg(sinfo_qclog_add_double(qclog_tbl,"QC FWHMX",fwhm_x,
437  "STD star FWHM on X","%f"));
438  ck0_nomsg(sinfo_qclog_add_double(qclog_tbl,"QC FWHMY",fwhm_y,
439  "STD star FWHM on Y","%f"));
440 
441  cfg -> halfbox_x = (floor)(0.5*(fwhm_x+fwhm_y)*cfg->fwhm_factor+0.5);
442 
443  cfg -> halfbox_y = (floor)(0.5*(fwhm_x+fwhm_y)*cfg->fwhm_factor+0.5);
444 
445  } else {
446 
447  irplib_error_recover(clean_state,"Problem fitting Gaussian");
448  cpl_error_reset();
449 
450  }
451 
452  sinfo_free_image(&std_med_ima);
453 
454  /*
455  sinfo_msg("max ima=%d %d psf_sz=%d\n",max_ima_x,max_ima_y,psf_sz);
456  sinfo_msg("centroid ima=%f %f\n",max_ima_cx,max_ima_cy);
457  sinfo_msg("gauss=norm=%f xcen=%f ycen=%f sig_x=%f "
458  "sig_y=%f fwhm_x=%f fwhm_y=%f\n",
459  norm,xcen,ycen,sig_x,sig_y,fwhm_x,fwhm_y);
460  */
461 
462  cfg -> llx = (int)(xcen-cfg->halfbox_x);
463  cfg -> llx = (cfg -> llx > 0 ) ? cfg -> llx : 1;
464 
465  if((cfg->llx+2*cfg->halfbox_x) >= ima_szx) {
466  cfg -> halfbox_x=(int) ((ima_szx-cfg->llx-1)/2);
467  check1++;
468  }
469 
470  cfg -> lly = (int)(ycen-cfg->halfbox_y);
471  cfg -> lly = (cfg -> lly > 0 ) ? cfg -> lly : 1;
472  if((cfg->lly+2*cfg->halfbox_y) >= ima_szy) {
473  cfg -> halfbox_y=(int) ((ima_szy-cfg->lly-1)/2);
474  check1++;
475  }
476  ck0_nomsg(sinfo_qclog_add_int(qclog_tbl,"QC CHECK1",check1,
477  "Check on evaluation box","%d"));
478 
479  /*
480  sinfo_msg("llx= %d lly= %d\n",cfg->llx, cfg->lly);
481  sinfo_msg("halfbox_x=%d halfbox_y=%d\n",cfg->halfbox_x,cfg->halfbox_y);
482  */
483 
484  /*
485 #----------------------------------------------------------------------
486 #---------------------------EXTRACTION---------------------------------
487 #----------------------------------------------------------------------
488  */
489 
490  sinfo_msg("Extraction");
491  cknull(spectrum = (cpl_image**) cpl_calloc (cfg -> nframes,
492  sizeof(cpl_image*)),
493  "Could not allocate memory for spectrum image");
494 
495  for (fra=0; fra < cfg->nframes; fra++) {
496  name = cfg->inFrameList[fra];
497  if(sinfo_is_fits_file(name) != 1) {
498  sinfo_msg_error("Input file %s is not FITS",name);
499  goto cleanup;
500  }
501  cknull(cube = cpl_imagelist_load(name,CPL_TYPE_FLOAT,0),
502  "could not load data cube" );
503 
504  if (exptime == FLAG) {
505  sinfo_msg_error("could not find exposure time in the fits header");
506  return -1;
507  }
508  exptime = sinfo_pfits_get_ditndit(name);
509 
510  sinfo_msg("cfg->gain %f",cfg->gain);
511  check_nomsg(tbl_spectrum=cpl_table_new(cpl_imagelist_get_size(cube)));
512  if(NULL==(spectrum[fra]=sinfo_new_optimal_extraction_from_cube( cube,
513  cfg->llx,
514  cfg->lly,
515  cfg->halfbox_x,
516  cfg->halfbox_y,
517  cfg->fwhm_factor,
518  BKG_VARIANCE,
519  SKY_FLUX,
520  cfg->gain,
521  exptime,
522  name,
523  &tbl_spectrum,
524  qc_info,
525  &check2))){
526 
527  irplib_error_recover(clean_state,
528  "could not do sinfo_optimalExtractionFromCube");
529  } else {
530  check_nomsg(cpl_imagelist_set(list_object,
531  cpl_image_duplicate(spectrum[fra]), fra));
532 
533  }
534 
535 
536  ck0_nomsg(sinfo_qclog_add_int(qclog_tbl,"QC CHECK2",
537  check2,"Check on evaluation box","%d"));
538 
539 
540  ck0(sinfo_pro_save_tbl(tbl_spectrum,ref_set,sof,
541  (char*)STDSTAR_OUT_TABLE,
542  PRO_STD_STAR_SPECTRA,qclog_tbl,
543  plugin_id,config),
544  "cannot dump ima %s", "out_std_star_spectrum.fits");
545 
546 
547  sinfo_free_table(&qclog_tbl);
548 
549  if (do_compute_eff!=0 && frm_std_cat !=NULL && frm_atmext != NULL) {
550  sinfo_msg("compute efficiency");
551  frm_sci = cpl_frameset_find(sof,PRO_STD_STAR_SPECTRA);
552  frm_std_cat = cpl_frameset_find(sof,FLUX_STD_CATALOG);
553  frm_atmext = cpl_frameset_find(sof,EXTCOEFF_TABLE);
554 
555 
556 
557  check_nomsg(tot_eff=sinfo_efficiency_compute(frm_sci,frm_std_cat,
558  frm_atmext));
559  ck0(sinfo_pro_save_tbl(tot_eff,ref_set,sof,(char*)EFFICIENCY_FILENAME,
560  PRO_EFFICIENCY,qclog_tbl,plugin_id,config),
561  "cannot dump ima %s", "out_.fits");
562  }
563 
564 
565 
566 
567  /*
568  if(spectrum[fra] != NULL ) {
569  sinfo_free_image(&(spectrum)[fra]);
570  }
571  */
572  /*----determine the intensity conversion factor if wished--------*/
573  if (cfg->convInd == 1) {
574  sinfo_msg("Determines convertion factor");
575 
576  convfactor = sinfo_new_determine_conversion_factor( cube,
577  cfg->mag,
578  exptime,
579  cfg->llx,
580  cfg->lly,
581  cfg->halfbox_x,
582  cfg->halfbox_y,
583  &check3 );
584 
585 
586  if (convfactor < -100000.) {
587  sinfo_msg_warning("could not do sinfo_determineConversionFactor!" );
588  /* goto cleanup; */
589  } else {
590  sinfo_new_array_set_value(factor, convfactor, fra);
591  }
592  }
593  sinfo_free_imagelist(&cube);
594  } /* end loop over fra */
595 
596  sinfo_free_table(&tbl_spectrum);
597  sinfo_free_image_array(&spectrum,cfg->nframes);
598  if (cfg->convInd == 1) {
599  sinfo_msg("Determines clean factor");
600  cleanfactor = sinfo_new_clean_mean(factor,
601  cfg->nframes,
602  cfg->lo_reject*100.,
603  cfg->hi_reject*100.);
604  }
605  if (cleanfactor > 100000. || cleanfactor == FLAG) {
606  sinfo_msg_error("could not do sinfo_clean_mean!" );
607  goto cleanup;
608  }
609 
610 
611  /*---read the fits header to change the gain and noise parameter-----*/
612  sinfo_msg("Average with rejection");
613 
614  no=cpl_imagelist_get_size(list_object);
615  lo_cut=(floor)(cfg->lo_reject*no+0.5);
616  hi_cut=(floor)(cfg->hi_reject*no+0.5);
617  if(no > 0) {
618  cknull(img_spct=cpl_imagelist_collapse_minmax_create(list_object,
619  lo_cut,hi_cut),
620  "sinfo_average_with_rejection failed" );
621  }
622 
623  sinfo_free_imagelist(&list_object);
624  if(no > 0) {
625  check_nomsg(qclog_tbl = sinfo_qclog_init());
626 
627  ck0_nomsg(sinfo_qclog_add_double(qclog_tbl,"QC CONVFCT",cleanfactor,
628  "Conversion factor","%g"));
629  ck0_nomsg(sinfo_qclog_add_int(qclog_tbl,"QC CHECK3",check3,
630  "Check evaluation box","%d"));
631  ck0(sinfo_pro_save_ima(img_spct,ref_set,sof,cfg->outName,
632  PRO_STD_STAR_SPECTRUM,qclog_tbl,
633  plugin_id,config),
634  "cannot dump ima %s", cfg->outName);
635 
636  sinfo_new_set_wcs_spectrum(img_spct,cfg->outName,cenLambda,disp,cenpix);
637  sinfo_free_table(&qclog_tbl);
638  }
639  /*#---free memory---*/
640  if(factor != NULL) sinfo_new_destroy_array(&factor);
641  sinfo_free_image(&img_spct);
642  sinfo_stdstar_free(&cfg);
643  sinfo_free_frameset(&raw);
644 
645  return 0;
646 
647  cleanup:
648  sinfo_free_table(&tbl_spectrum);
649  sinfo_free_table(&qclog_tbl);
650  sinfo_free_imagelist(&list_object);
651  if(spectrum != NULL) sinfo_free_image_array(&spectrum,cfg->nframes);
652  sinfo_free_image(&std_med_ima);
653  sinfo_free_image(&std_med_dup);
654  sinfo_free_imagelist(&cube);
655  sinfo_free_propertylist(&plist) ;
656  if(factor != NULL) sinfo_new_destroy_array(&factor);
657  sinfo_free_image(&img_spct);
658  sinfo_stdstar_free (&cfg);
659  sinfo_free_frameset(&raw);
660  return -1;
661 
662 }
663 
664 
#define sinfo_msg_error(...)
Print an error message.
Definition: sinfo_msg.h:69
#define sinfo_msg_warning(...)
Print an warning message.
Definition: sinfo_msg.h:93