SINFONI Pipeline Reference Manual  2.5.2
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 sinfo_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 (sinfo_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  check_nomsg(cpl_image_get_maxpos(std_med_dup,&max_ima_x,&max_ima_y));
320  sinfo_free_image(&std_med_dup);
321 
322 
323  ima_szx=cpl_image_get_size_x(std_med_ima);
324  ima_szy=cpl_image_get_size_y(std_med_ima);
325  wllx= ((max_ima_x-psf_sz)>0) ? (max_ima_x-psf_sz) : 1;
326  wlly= ((max_ima_y-psf_sz)>0) ? (max_ima_y-psf_sz) : 1;
327  wurx= ((max_ima_x+psf_sz)<ima_szx) ? (max_ima_x+psf_sz) : ima_szx ;
328  wury= ((max_ima_y+psf_sz)<ima_szy) ? (max_ima_y+psf_sz) : ima_szy ;
329  /*
330  sinfo_msg("wllx=%d wlly=%d wurx=%d wury=%d\n",wllx,wlly,wurx,wury);
331  cpl_image_get_maxpos_window(std_med_ima,wllx,wlly,wurx,wury,
332  &max_ima_x,&max_ima_y);
333  */
334  check_nomsg(qclog_tbl = sinfo_qclog_init());
335  check_nomsg(max_ima_cx=cpl_image_get_centroid_x_window(std_med_ima,wllx,
336  wlly,wurx,wury));
337  check_nomsg(max_ima_cy=cpl_image_get_centroid_y_window(std_med_ima,wllx,
338  wlly,wurx,wury));
339 
340 
341  xshift=max_ima_cx-ima_szx/2;
342  yshift=max_ima_cy-ima_szy/2;
343 
344  ck0_nomsg(sinfo_qclog_add_double(qclog_tbl,"QC SHIFTX",xshift,
345  "X shift centroid - center image","%f"));
346 
347  ck0_nomsg(sinfo_qclog_add_double(qclog_tbl,"QC SHIFTY",yshift,
348  "Y shift centroid - center image","%f"));
349 
350  if(
351  ((max_ima_x-psf_sz) < 1) ||
352  ((max_ima_y-psf_sz) < 1) ||
353  ((max_ima_x+psf_sz) > ima_szx) ||
354  ((max_ima_x+psf_sz) > ima_szy)
355  )
356  {
357  psf_sz = (psf_sz < (max_ima_x-1)) ? psf_sz : (max_ima_x-1);
358  psf_sz = (psf_sz < (max_ima_y-1)) ? psf_sz : (max_ima_y-1);
359  psf_sz = (psf_sz < ima_szx-max_ima_x) ? psf_sz : (ima_szx-max_ima_x);
360  psf_sz = (psf_sz < ima_szy-max_ima_y) ? psf_sz : (ima_szy-max_ima_y);
361  //added to prevent seg fault by cpl_image_fit_gaussian
362  psf_sz = (psf_sz > 4) ? psf_sz : 4;
363  }
364 
365 
366  ck0_nomsg(sinfo_qclog_add_int(qclog_tbl,"QC FWHM LLX",cfg->llx,
367  "STD star FWHM LLX","%d"));
368  ck0_nomsg(sinfo_qclog_add_int(qclog_tbl,"QC FWHM LLY",cfg->lly,
369  "STD star FWHM LLY","%d"));
370  ck0_nomsg(sinfo_qclog_add_int(qclog_tbl,"QC FWHM HBX",cfg->halfbox_x,
371  "STD star FWHM HBX","%d"));
372  ck0_nomsg(sinfo_qclog_add_int(qclog_tbl,"QC FWHM HBX",cfg->halfbox_y,
373  "STD star FWHM HBY","%d"));
374 
375 
376  /* call the 2D-Gaussian fit routine */
377  for ( i = 0 ; i < 7 ; i++ )
378  {
379  mpar[i] = 1 ;
380  }
381 
382 
383  if(-1 == sinfo_new_fit_2d_gaussian(std_med_ima,
384  fpar,
385  dpar,
386  mpar,
387  cfg->llx,
388  cfg->lly,
389  cfg->halfbox_x,
390  cfg->halfbox_y,
391  &check4 ) ) {
392  irplib_error_recover(clean_state,"2d Gaussian fit failed");
393  /* return 0; */
394 
395  } else {
396 
397  ck0_nomsg(sinfo_qclog_add_double(qclog_tbl,"QC FWHM MAJ",fpar[4],
398  "STD star FWHM on major axis","%f"));
399  ck0_nomsg(sinfo_qclog_add_double(qclog_tbl,"QC FWHM MIN",fpar[5],
400  "STD star FWHM on minor axis","%f"));
401  ck0_nomsg(sinfo_qclog_add_double(qclog_tbl,"QC THETA",fpar[6],
402  "STD star ellipsis angle theta","%f"));
403 
404 
405  }
406 
407 
408  /*
409  sinfo_msg("Gauss fit params: xc,yc,amp,bkg,fwhm_x,fwhm_y,angle\n");
410  for ( i = 0 ; i < 7 ; i++ )
411  {
412  sinfo_msg("fpar[%d]=%f dpar[%d]=%f\n",i,fpar[i],i,dpar[i]);
413  }
414  */
415  if(CPL_ERROR_NONE == cpl_image_fit_gaussian(std_med_ima,
416  max_ima_x,
417  max_ima_y,
418  psf_sz,
419  &norm,
420  &xcen,
421  &ycen,
422  &sig_x,
423  &sig_y,
424  &fwhm_x,
425  &fwhm_y)) {
426 
427 
428  ck0_nomsg(sinfo_qclog_add_double(qclog_tbl,"QC FWHMX",fwhm_x,
429  "STD star FWHM on X","%f"));
430  ck0_nomsg(sinfo_qclog_add_double(qclog_tbl,"QC FWHMY",fwhm_y,
431  "STD star FWHM on Y","%f"));
432 
433  cfg -> halfbox_x = (floor)(0.5*(fwhm_x+fwhm_y)*cfg->fwhm_factor+0.5);
434 
435  cfg -> halfbox_y = (floor)(0.5*(fwhm_x+fwhm_y)*cfg->fwhm_factor+0.5);
436 
437  } else {
438 
439  irplib_error_recover(clean_state,"Problem fitting Gaussian");
440  cpl_error_reset();
441 
442  }
443  sinfo_free_image(&std_med_ima);
444 
445  /*
446  sinfo_msg("max ima=%d %d psf_sz=%d\n",max_ima_x,max_ima_y,psf_sz);
447  sinfo_msg("centroid ima=%f %f\n",max_ima_cx,max_ima_cy);
448  sinfo_msg("gauss=norm=%f xcen=%f ycen=%f sig_x=%f "
449  "sig_y=%f fwhm_x=%f fwhm_y=%f\n",
450  norm,xcen,ycen,sig_x,sig_y,fwhm_x,fwhm_y);
451  */
452 
453  cfg -> llx = (int)(xcen-cfg->halfbox_x);
454  cfg -> llx = (cfg -> llx > 0 ) ? cfg -> llx : 1;
455 
456  if((cfg->llx+2*cfg->halfbox_x) >= ima_szx) {
457  cfg -> halfbox_x=(int) ((ima_szx-cfg->llx-1)/2);
458  check1++;
459  }
460 
461  cfg -> lly = (int)(ycen-cfg->halfbox_y);
462  cfg -> lly = (cfg -> lly > 0 ) ? cfg -> lly : 1;
463  if((cfg->lly+2*cfg->halfbox_y) >= ima_szy) {
464  cfg -> halfbox_y=(int) ((ima_szy-cfg->lly-1)/2);
465  check1++;
466  }
467  ck0_nomsg(sinfo_qclog_add_int(qclog_tbl,"QC CHECK1",check1,
468  "Check on evaluation box","%d"));
469 
470 
471  /*
472  sinfo_msg("llx= %d lly= %d\n",cfg->llx, cfg->lly);
473  sinfo_msg("halfbox_x=%d halfbox_y=%d\n",cfg->halfbox_x,cfg->halfbox_y);
474  */
475 
476  /*
477 #----------------------------------------------------------------------
478 #---------------------------EXTRACTION---------------------------------
479 #----------------------------------------------------------------------
480  */
481 
482  sinfo_msg("Extraction");
483  cknull(spectrum = (cpl_image**) cpl_calloc (cfg -> nframes,
484  sizeof(cpl_image*)),
485  "Could not allocate memory for spectrum image");
486 
487  for (fra=0; fra < cfg->nframes; fra++) {
488  name = cfg->inFrameList[fra];
489  if(sinfo_is_fits_file(name) != 1) {
490  sinfo_msg_error("Input file %s is not FITS",name);
491  goto cleanup;
492  }
493  cknull(cube = cpl_imagelist_load(name,CPL_TYPE_FLOAT,0),
494  "could not load data cube" );
495 
496  if (exptime == FLAG) {
497  sinfo_msg_error("could not find exposure time in the fits header");
498  return -1;
499  }
500  exptime = sinfo_pfits_get_ditndit(name);
501 
502  sinfo_msg("cfg->gain %f",cfg->gain);
503  check_nomsg(tbl_spectrum=cpl_table_new(cpl_imagelist_get_size(cube)));
504  if(NULL==(spectrum[fra]=sinfo_new_optimal_extraction_from_cube( cube,
505  cfg->llx,
506  cfg->lly,
507  cfg->halfbox_x,
508  cfg->halfbox_y,
509  cfg->fwhm_factor,
510  BKG_VARIANCE,
511  SKY_FLUX,
512  cfg->gain,
513  exptime,
514  name,
515  &tbl_spectrum,
516  qc_info,
517  &check2))){
518 
519  irplib_error_recover(clean_state,
520  "could not do sinfo_optimalExtractionFromCube");
521  } else {
522  check_nomsg(cpl_imagelist_set(list_object,
523  cpl_image_duplicate(spectrum[fra]), fra));
524 
525  }
526 
527 
528 
529  ck0_nomsg(sinfo_qclog_add_int(qclog_tbl,"QC CHECK2",
530  check2,"Check on evaluation box","%d"));
531 
532 
533  ck0(sinfo_pro_save_tbl(tbl_spectrum,ref_set,sof,
534  (char*)STDSTAR_OUT_TABLE,
535  PRO_STD_STAR_SPECTRA,qclog_tbl,
536  plugin_id,config),
537  "cannot dump ima %s", "out_std_star_spectrum.fits");
538 
539 
540  sinfo_free_table(&qclog_tbl);
541 
542  if (do_compute_eff!=0 && frm_std_cat !=NULL && frm_atmext != NULL) {
543  sinfo_msg("compute efficiency");
544  frm_sci = cpl_frameset_find(sof,PRO_STD_STAR_SPECTRA);
545  frm_std_cat = cpl_frameset_find(sof,FLUX_STD_CATALOG);
546  frm_atmext = cpl_frameset_find(sof,EXTCOEFF_TABLE);
547 
548 
549 
550  check_nomsg(tot_eff=sinfo_efficiency_compute(frm_sci,frm_std_cat,
551  frm_atmext));
552  ck0(sinfo_pro_save_tbl(tot_eff,ref_set,sof,(char*)EFFICIENCY_FILENAME,
553  PRO_EFFICIENCY,qclog_tbl,plugin_id,config),
554  "cannot dump ima %s", "out_.fits");
555  }
556 
557 
558 
559 
560  /*
561  if(spectrum[fra] != NULL ) {
562  sinfo_free_image(&(spectrum)[fra]);
563  }
564  */
565  /*----determine the intensity conversion factor if wished--------*/
566  if (cfg->convInd == 1) {
567  sinfo_msg("Determines convertion factor");
568 
569  convfactor = sinfo_new_determine_conversion_factor( cube,
570  cfg->mag,
571  exptime,
572  cfg->llx,
573  cfg->lly,
574  cfg->halfbox_x,
575  cfg->halfbox_y,
576  &check3 );
577 
578 
579  if (convfactor < -100000.) {
580  sinfo_msg_warning("could not do sinfo_determineConversionFactor!" );
581  /* goto cleanup; */
582  } else {
583  sinfo_new_array_set_value(factor, convfactor, fra);
584  }
585  }
586  sinfo_free_imagelist(&cube);
587  } /* end loop over fra */
588 
589  sinfo_free_table(&tbl_spectrum);
590  sinfo_free_image_array(&spectrum,cfg->nframes);
591  if (cfg->convInd == 1) {
592  sinfo_msg("Determines clean factor");
593  cleanfactor = sinfo_new_clean_mean(factor,
594  cfg->nframes,
595  cfg->lo_reject*100.,
596  cfg->hi_reject*100.);
597  }
598  if (cleanfactor > 100000. || cleanfactor == FLAG) {
599  sinfo_msg_error("could not do sinfo_clean_mean!" );
600  goto cleanup;
601  }
602 
603 
604  /*---read the fits header to change the gain and noise parameter-----*/
605  sinfo_msg("Average with rejection");
606 
607  no=cpl_imagelist_get_size(list_object);
608  lo_cut=(floor)(cfg->lo_reject*no+0.5);
609  hi_cut=(floor)(cfg->hi_reject*no+0.5);
610  if(no > 0) {
611  cknull(img_spct=cpl_imagelist_collapse_minmax_create(list_object,
612  lo_cut,hi_cut),
613  "sinfo_average_with_rejection failed" );
614  }
615 
616  sinfo_free_imagelist(&list_object);
617  if(no > 0) {
618  check_nomsg(qclog_tbl = sinfo_qclog_init());
619 
620  ck0_nomsg(sinfo_qclog_add_double(qclog_tbl,"QC CONVFCT",cleanfactor,
621  "Conversion factor","%g"));
622 
623  ck0_nomsg(sinfo_qclog_add_int(qclog_tbl,"QC CHECK3",check3,
624  "Check evaluation box","%d"));
625 
626 
627  ck0(sinfo_pro_save_ima(img_spct,ref_set,sof,cfg->outName,
628  PRO_STD_STAR_SPECTRUM,qclog_tbl,
629  plugin_id,config),
630  "cannot dump ima %s", cfg->outName);
631 
632  sinfo_new_set_wcs_spectrum(img_spct,cfg->outName,cenLambda,disp,cenpix);
633  sinfo_free_table(&qclog_tbl);
634  }
635  /*#---free memory---*/
636  if(factor != NULL) sinfo_new_destroy_array(&factor);
637  sinfo_free_image(&img_spct);
638  sinfo_stdstar_free(&cfg);
639  sinfo_free_frameset(&raw);
640 
641  return 0;
642 
643  cleanup:
644  sinfo_free_table(&tbl_spectrum);
645  sinfo_free_table(&qclog_tbl);
646  sinfo_free_imagelist(&list_object);
647  if(spectrum != NULL) sinfo_free_image_array(&spectrum,cfg->nframes);
648  sinfo_free_image(&std_med_ima);
649  sinfo_free_image(&std_med_dup);
650  sinfo_free_imagelist(&cube);
651  sinfo_free_propertylist(&plist) ;
652  if(factor != NULL) sinfo_new_destroy_array(&factor);
653  sinfo_free_image(&img_spct);
654  sinfo_stdstar_free (&cfg);
655  sinfo_free_frameset(&raw);
656  return -1;
657 
658 }
659 
660