SINFONI Pipeline Reference Manual  2.5.2
sinfo_new_slit_pos.c
1 /*----------------------------------------------------------------------------
2 
3  File name : sinfo_new_slit_pos.c
4  Author : A. Modigliani
5  Created on : Sep 17, 2003
6  Description :
7 
8  ---------------------------------------------------------------------------*/
9 /*
10  * This file is part of the ESO SINFONI Pipeline
11  * Copyright (C) 2004,2005 European Southern Observatory
12  *
13  * This program is free software; you can redistribute it and/or modify
14  * it under the terms of the GNU General Public License as published by
15  * the Free Software Foundation; either version 2 of the License, or
16  * (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software
25  * Foundation, 51 Franklin St, Fifth Floor, Boston, MA 02111-1307 USA
26  */
27 #ifdef HAVE_CONFIG_H
28 # include <config.h>
29 #endif
30 
31 /*----------------------------------------------------------------------------
32  Includes
33  ---------------------------------------------------------------------------*/
34 #include "sinfo_new_slit_pos.h"
35 #include "sinfo_pro_save.h"
36 #include "sinfo_pro_types.h"
37 #include "sinfo_wavecal_ini_by_cpl.h"
38 #include "sinfo_wcal_functions.h"
39 #include "sinfo_wave_calibration.h"
40 #include "sinfo_utilities.h"
41 #include "sinfo_utils_wrappers.h"
42 #include "sinfo_hidden.h"
43 #include "sinfo_error.h"
44 #include "sinfo_globals.h"
45 
46 /*----------------------------------------------------------------------------
47  Defines
48  ---------------------------------------------------------------------------*/
49 
50 /*----------------------------------------------------------------------------
51  Function Definitions
52  ---------------------------------------------------------------------------*/
60 /*----------------------------------------------------------------------------
61  Function : sinfo_new_slit_pos()
62  In : ini_file: file name of according .ini file
63  Out : integer (0 if it worked, -1 if it doesn't)
64  Job :
65 
66 
67  Normal method:
68 
69  does the wavelength calibration and the fitting of the slitlet sinfo_edge
70  positions (ASCII file 32 x 2 values) if wished
71  produces an array of the bcoefs and of the fit parameters if wished and a
72  wavelength calibration map input is an emission line frame and a line list
73 
74 
75  o searching for lines by cross sinfo_correlation with a line list
76  o Gaussian fitting of emission lines in each column->positions of the lines->
77  resulting fit parameters can be stored in an ASCII file
78  o Fitting of a polynomial to the line positions for each column
79  o Smoothing: fitting of each polynomial coefficient by another polynomial
80  across the whole frame -> resulting polynomial coefficients can be stored
81  in an ASCII file.
82  o Wavelength calibration map (micron value for each frame pixel) can be
83  produced by using these coefficients and a cross sinfo_correlation to the
84  original frame
85 
86  o The slitlet sinfo_edge positions can be fitted:
87  1) Automatically (not really stable) or by using guess sinfo_edge positions
88  2) By using a Boltzmann or a linear slope function
89 
90  Slit method:
91 
92  does the wavelength calibration and the fitting of the slitlet sinfo_edge
93  positions (ASCII file 32 x 2 values) if wished produces a list of the fit
94  parameters and of the smoothed coefficients if wished and a wavelength
95  calibration map input is an emission line frame and a line list
96 
97  o Does the same as other method but smoothes the found polynomial
98  coefficients within each slitlet and not over the whole frame.
99 
100  o Produces always a wavelength calibration map and does not crosscorrelate.
101 
102  ---------------------------------------------------------------------------*/
103 
104 
105 
106 int sinfo_new_slit_pos (cpl_parameterlist* config, cpl_frameset* sof)
107 {
108  wave_config * cfg =NULL;
109  int check = 0;
110  int lx = 0;
111  /* int ly = 0; */
112  int n_lines=0;
113  int i = 0;
114  int j = 0;
115  int n = 0;
116 
117  int sum=0;
118 
119  int* n_found_lines=NULL;
120  int* sum_pointer=NULL;
121  int** row_clean=NULL;
122 
123  float a=0;
124  float shift=0;
125  float* wave=NULL;
126  float* intens=NULL;
127 
128  float** acoefs=NULL;
129  float** wavelength_clean=NULL;
130 
131  float** sinfo_slit_pos=NULL;
132 
133  cpl_image * map=NULL ;
134  cpl_image * im=NULL ;
135 
136  FitParams** par=NULL;
137 
138  cpl_table* tbl_wcal=NULL;
139  cpl_table* tbl_spos=NULL;
140 
141  char* col_name=NULL;
142  char* tbl_name=NULL;
143 
144  char* tbl_line_list_name=NULL;
145  cpl_table* tbl_line_list = NULL;
146  int* status=NULL;
147 
148  cpl_frameset* raw=NULL;
149 
150 
151  cpl_table * tbl_fp =NULL;
152  char* col=NULL;
153  cpl_table* qclog_tbl=NULL;
154  char* key_name=NULL;
155  double fwhm_med=0;
156  double fwhm_avg=0;
157  double coef_med=0;
158  double coef_avg=0;
159  /* int trow=0; */
160  qc_wcal* qc=sinfo_qc_wcal_new();
161  /* -----------------------------------------------------------------
162  1) parse the file names and parameters to the ns_config data
163  structure cfg
164  -----------------------------------------------------------------
165  */
166 
167  sinfo_msg("Parsing cpl input");
168  cfg = sinfo_parse_cpl_input_wave(config,sof,&raw) ;
169 
170  if(cfg!=NULL) {
171  cfg->nslitlets=32;
172  cfg->calibIndicator=1;
173  cfg->wavemapInd=0;
174  cfg->slitposIndicator=1;
175  }
176  if(cpl_error_get_code() != CPL_ERROR_NONE) {
177  sinfo_msg_error("%s", (char* ) cpl_error_get_message());
178  sinfo_qc_wcal_delete(&qc);
179  return -1;
180  }
181 
182  if (cfg == NULL)
183  {
184  sinfo_msg_error("could not parse cpl input!\n") ;
185  sinfo_qc_wcal_delete(&qc);
186  return -1 ;
187  }
188  if(sinfo_is_fits_file(cfg->inFrame) != 1) {
189  sinfo_msg_error("Input file %s is not FITS",cfg->inFrame);
190  sinfo_qc_wcal_delete(&qc);
191  return -1;
192  }
193 
194 
195  if (cfg->slitposIndicator == 1 && cfg->estimateIndicator == 1) {
196  if (sinfo_is_fits_file(cfg->slitposGuessName) != 1) {
197  sinfo_msg_error("slitlet position guess list not given!");
198  sinfo_qc_wcal_delete(&qc);
199  return -1;
200  }
201  }
202 
203  if (cfg->calibIndicator == 0 && cfg->wavemapInd == 1) {
204  if (sinfo_is_fits_file(cfg->coeffsName) != 1) {
205  sinfo_msg_error("coefficients list not given!");
206  sinfo_qc_wcal_delete(&qc);
207  return -1;
208  }
209  }
210 
211 
212  if (cfg->slitposIndicator == 1) {
213  if (cfg->calibIndicator != 1 && cfg->estimateIndicator != 1) {
214 
215  if (sinfo_is_fits_file(cfg->paramsList) != 1) {
216  sinfo_msg_error("parameter list not given!");
217  sinfo_qc_wcal_delete(&qc);
218  return -1;
219  }
220 
221  }
222  }
223 
224 /*---load the emission line frame---*/
225  im = cpl_image_load(cfg->inFrame,CPL_TYPE_FLOAT,0,0);
226  if (im == NULL) {
227  sinfo_msg_error("could not load image\n");
228  sinfo_qc_wcal_delete(&qc);
229  return -1;
230  }
231 
232 
233  lx = cpl_image_get_size_x(im);
234  /* ly = cpl_image_get_size_y(im); */
235 
236 
237 
238 if (cfg->calibIndicator == 1 || cfg->wavemapInd == 1) {
239  /*---open the line list and read the number of lines---*/
240 
241  if(cpl_error_get_code() != CPL_ERROR_NONE) {
242  sinfo_msg_error("%s", (char* ) cpl_error_get_message());
243  sinfo_qc_wcal_delete(&qc);
244  return -1;
245  }
246 
247  tbl_line_list_name=cfg->lineList;
248  tbl_line_list = cpl_table_load(tbl_line_list_name,1,0);
249  if(cpl_error_get_code() != CPL_ERROR_NONE) {
250  sinfo_msg_error("%s", (char* ) cpl_error_get_message());
251  sinfo_qc_wcal_delete(&qc);
252  return -1;
253  }
254  n = cpl_table_get_nrow(tbl_line_list);
255  n_lines = n;
256  if(cpl_error_get_code() != CPL_ERROR_NONE) {
257  sinfo_msg_error("%s", (char* ) cpl_error_get_message());
258  sinfo_qc_wcal_delete(&qc);
259  return -1;
260  }
261 
262  /* THIS ORIGINATES A MEMORY LEAK
263  wave = sinfo_new_floatarray (n);
264  intens = sinfo_new_floatarray (n);
265  if (wave == NULL || intens == NULL) {
266  sinfo_msg_error("could not allocate memory for the line list values\n" );
267  sinfo_qc_wcal_delete(&qc);
268  return -1;
269  }
270  */
271 
272  wave = cpl_table_get_data_float(tbl_line_list,"wave");
273  if(cpl_error_get_code() != CPL_ERROR_NONE) {
274  sinfo_msg_error("%s", (char* ) cpl_error_get_message());
275  sinfo_qc_wcal_delete(&qc);
276  return -1;
277  }
278 
279  intens = cpl_table_get_data_float(tbl_line_list,"int");
280  if(cpl_error_get_code() != CPL_ERROR_NONE) {
281  sinfo_msg_error("%s", (char* ) cpl_error_get_message());
282  sinfo_qc_wcal_delete(&qc);
283  return -1;
284  }
285 
286 }
287 
288 
289 
290 /*
291  ----------------------------------------------------------------------
292  ---------------------------FINDLINES----------------------------------
293  ----------------------------------------------------------------------
294  */
295 
296 
297 /*if not yet done:
298  do the wavelength calibration, that means:
299  find the dispersion relation and parameterize its coefficients
300  */
301 /*
302  sinfo_msg("guessBeginWave=%g",cfg->guessBeginWavelength);
303  sinfo_msg("guessDisp1=%g",cfg->guessDispersion1);
304  sinfo_msg("guessDisp2=%g",cfg->guessDispersion2);
305 */
306 if (cfg->calibIndicator == 1 && cfg->wavemapInd == 0) {
307  sinfo_msg("Findlines");
308  acoefs = sinfo_new_2Dfloatarray(cfg->nrDispCoefficients, lx);
309  /*allocate memory*/
310  n_found_lines = sinfo_new_intarray(lx);
311  row_clean = sinfo_new_2Dintarray(lx, n_lines);
312  wavelength_clean = sinfo_new_2Dfloatarray(lx, n_lines);
313  sum_pointer = sinfo_new_intarray(1) ;
314  /*find the emission lines in each image column*/
315  sinfo_new_intarray_set_value(sum_pointer, 0, 0);
316  check = sinfo_new_find_lines(im, wave, intens, n_lines, row_clean,
317  wavelength_clean, cfg->guessBeginWavelength,
318  cfg->guessDispersion1, cfg->guessDispersion2,
319  cfg->mindiff, cfg->halfWidth,
320  n_found_lines, cfg->sigma, sum_pointer );
321  if (-1 == check) {
322  sinfo_msg_error("sinfo_findLines failed!\n");
323  sinfo_qc_wcal_delete(&qc);
324  return -1;
325  }
326 
327 
328 /*---------------------------------------------------------------------
329  *-----------------------WAVE_CALIB-------------------------------------
330  *---------------------------------------------------------------------
331  */
332 
333 
334  sinfo_msg("Wave Calibration");
335  sum = sinfo_new_intarray_get_value(sum_pointer,0);
336  /* allocate memory for the fit parameters */
337  par = sinfo_new_fit_params( sum );
338  if (par == NULL) {
339  sinfo_msg_error("sinfo_newFitParams failed!\n");
340  sinfo_qc_wcal_delete(&qc);
341  return -1;
342  }
343 
344  /*
345  fit each line, make a polynomial fit and fit the resulting fit
346  coefficients across the columns of the slitlet
347  */
348  sinfo_slit_pos = sinfo_new_2Dfloatarray(32,2);
349 
350  map = sinfo_new_spred_wave_cal(im,
351  par,
352  acoefs,
353  cfg->nslitlets,
354  row_clean,
355  wavelength_clean,
356  n_found_lines,
357  cfg->guessDispersion1,
358  cfg->halfWidth,
359  cfg->minAmplitude,
360  cfg->maxResidual,
361  cfg->fwhm,
362  cfg->nrDispCoefficients,
363  cfg->nrCoefCoefficients,
364  cfg->sigmaFactor,
365  cfg->pixeldist,
366  cfg->pixel_tolerance,
367  sinfo_slit_pos);
368 
369 
370  if (map == NULL ) {
371  sinfo_msg_error("sinfo_wave_cal failed!\n");
372  sinfo_qc_wcal_delete(&qc);
373  return -1;
374  }
375  sinfo_msg("Check line positions");
376 
377  shift = sinfo_new_check_line_positions (im, acoefs,
378  cfg->nrDispCoefficients,
379  cfg->guessDispersion1, par);
380  if (FLAG == shift){
381  sinfo_msg_error("checkForLinePositions failed!\n");
382  }
383 
384 
385  sinfo_det_ncounts(raw, cfg->qc_thresh_max,qc);
386  qclog_tbl = sinfo_qclog_init();
387  ck0_nomsg(sinfo_qclog_add_int(qclog_tbl,"QC WAVE ALL",
388  n_lines,"Number of found lines","%d"));
389 
390  ck0_nomsg(sinfo_qclog_add_int(qclog_tbl,"QC WAVE NPIXSAT",
391  qc->nsat,"Number of saturated pixels","%d"));
392 
393  ck0_nomsg(sinfo_qclog_add_int(qclog_tbl,"QC WAVE MAXFLUX",
394  qc->max_di,"Max int off-lamp subracted frm","%g"));
395 
396  if(-1 == sinfo_pro_save_ima(map,raw,sof,cfg->outName,
397  PRO_WAVE_MAP,qclog_tbl,cpl_func,config)) {
398  sinfo_msg_error("cannot save ima %s", cfg->outName);
399  }
400  sinfo_free_table(&qclog_tbl);
401 
402 
403  /*
404  #store the resulting polynomial fit coefficients in an
405  ASCII file if wished
406  */
407 
408  if (cfg->writeCoeffsInd == 1) {
409  col_name = (char*) cpl_calloc(MAX_NAME_SIZE,sizeof(char*));
410  tbl_name = (char*) cpl_calloc(MAX_NAME_SIZE,sizeof(char*));
411  tbl_wcal = cpl_table_new(lx);
412  for (i=0; i< cfg->nrDispCoefficients; i++) {
413  snprintf(col_name,MAX_NAME_SIZE-1,"%s%d","coeff",i);
414  cpl_table_new_column(tbl_wcal,col_name, CPL_TYPE_DOUBLE);
415  }
416 
417 
418  qclog_tbl = sinfo_qclog_init();
419  key_name = cpl_calloc(FILE_NAME_SZ,sizeof(char));
420  ck0_nomsg(sinfo_qclog_add_int(qclog_tbl,"QC WAVE ALL",
421  n_lines,"Number of found lines","%d"));
422 
423 
424 
425  for (j=0; j< lx; j++) {
426  for (i=0; i< cfg->nrDispCoefficients; i++) {
427  snprintf(col_name,MAX_NAME_SIZE-1,"%s%d","coeff",i);
428  a = sinfo_new_array2D_get_value(acoefs, i, j);
429  /* fprintf(acoefs_file, "%15.13g ", a) ; */
430  cpl_table_set_double(tbl_wcal,col_name,j,a);
431  }
432 
433 
434  }
435 
436 
437  for (i=0; i< cfg->nrDispCoefficients; i++) {
438  snprintf(col_name,MAX_NAME_SIZE-1,"%s%d","coeff",i);
439  coef_avg=cpl_table_get_column_mean(tbl_wcal,col_name);
440  coef_med=cpl_table_get_column_median(tbl_wcal,col_name);
441 
442  //trow=1+i;
443  snprintf(key_name,MAX_NAME_SIZE-1,"%s%d%s","QC COEF",i," AVG");
444  ck0_nomsg(sinfo_qclog_add_double(qclog_tbl,key_name,coef_avg,
445  "Average wavecal Coef","%g"));
446 
447  //trow=1+i+cfg->nrDispCoefficients;
448  snprintf(key_name,MAX_NAME_SIZE-1,"%s%d%s","QC COEF",i," MED");
449  ck0_nomsg(sinfo_qclog_add_double(qclog_tbl,key_name,coef_med,
450  "Median wavecal Coef","%g"));
451 
452  }
453 
454  /*
455  fclose(acoefs_file);
456  */
457  strcpy(tbl_name,cfg->coeffsName);
458 
459  if(-1 == sinfo_pro_save_tbl(tbl_wcal,raw,sof,tbl_name,
460  PRO_WAVE_COEF_SLIT,qclog_tbl,cpl_func,config)) {
461  sinfo_msg_error("cannot save tbl %s", tbl_name);
462  }
463  sinfo_free_table(&tbl_wcal);
464  sinfo_free_table(&qclog_tbl);
465  cpl_free(key_name);
466 
467  cpl_free(col_name);
468  cpl_free(tbl_name);
469 
470  }
471 
472 
473  /*
474  #store the resulting Gaussian fit parameters in an ASCII file if wished
475  */
476  if (cfg->writeParInd == 1) {
477 
478 
479  sinfo_new_dump_fit_params_to_ascii(par,WAVECAL_FIT_PARAMS_OUT_FILEASCII);
480 
481  if ( NULL == par )
482  {
483  sinfo_msg_error ("no fit parameters available!") ;
484  sinfo_qc_wcal_delete(&qc);
485  return -1;
486  }
487 
488  if ( NULL == cfg->paramsList )
489  {
490  sinfo_msg_error ("no filename available!") ;
491  sinfo_qc_wcal_delete(&qc);
492  return -1;
493  }
494 
495  tbl_fp = cpl_table_new(par[0] -> n_params);
496  cpl_table_new_column(tbl_fp,"n_params", CPL_TYPE_INT);
497  cpl_table_new_column(tbl_fp,"column", CPL_TYPE_INT);
498  cpl_table_new_column(tbl_fp,"line", CPL_TYPE_INT);
499  col = (char*) cpl_calloc(MAX_NAME_SIZE,sizeof(char*));
500 
501  for(j=0;j<4;j++) {
502  snprintf(col,MAX_NAME_SIZE-1,"%s%d","fpar",j);
503  cpl_table_new_column(tbl_fp,col, CPL_TYPE_DOUBLE);
504  snprintf(col,MAX_NAME_SIZE-1,"%s%d","dpar",j);
505  cpl_table_new_column(tbl_fp,col, CPL_TYPE_DOUBLE);
506  }
507 
508 
509 
510  qclog_tbl = sinfo_qclog_init();
511  ck0_nomsg(sinfo_qclog_add_int(qclog_tbl,"QC NLINES",n_lines,
512  "Number of Found lines","%d"));
513 
514  for ( i = 0 ; i < par[0] -> n_params ; i++ )
515  {
516  cpl_table_set_int(tbl_fp,"n_params",i,par[i]->n_params);
517  cpl_table_set_int(tbl_fp,"column",i,par[i]->column);
518  cpl_table_set_int(tbl_fp,"line",i,par[i]->line);
519 
520 
521  for(j=0;j<4;j++) {
522  snprintf(col,MAX_NAME_SIZE-1,"%s%d","fpar",j);
523  cpl_table_set_double(tbl_fp,col,i,par[i]->fit_par[j]);
524  snprintf(col,MAX_NAME_SIZE-1,"%s%d","dpar",j);
525  cpl_table_set_double(tbl_fp,col,i,par[i]->derv_par[j]);
526  }
527  }
528 
529  fwhm_avg = cpl_table_get_column_mean(tbl_fp,"fpar1");
530  fwhm_med = cpl_table_get_column_median(tbl_fp,"fpar1");
531  ck0_nomsg(sinfo_qclog_add_double(qclog_tbl,"QC FWHM MED",fwhm_med,
532  "Median FWHM of found lines","%f"));
533 
534  ck0_nomsg(sinfo_qclog_add_double(qclog_tbl,"QC FWHM AVG",fwhm_avg,
535  "Average FWHM of found lines","%f"));
536 
537 
538  if(-1 == sinfo_pro_save_tbl(tbl_fp,raw,sof,cfg->paramsList,
539  PRO_WAVE_PAR_LIST,qclog_tbl,cpl_func,config)) {
540  sinfo_msg_error("cannot save tbl %s", cfg->paramsList);
541  }
542  sinfo_free_table(&qclog_tbl);
543 
544  sinfo_free_table(&tbl_fp) ;
545  cpl_free(col);
546 
547  }
548  /* free memory */
549  sinfo_new_destroy_2Dfloatarray ( &wavelength_clean, lx );
550  sinfo_new_destroy_2Dintarray (&row_clean, lx);
551  sinfo_new_destroy_intarray(&n_found_lines );
552  sinfo_new_destroy_intarray(&sum_pointer );
553  sinfo_new_destroy_2Dfloatarray ( &acoefs, cfg->nrDispCoefficients );
554 
555 /*----------------------------------------------------------------------
556  *-------------------WAVEMAP--------------------------------------------
557  *----------------------------------------------------------------------
558  */
559 
560 /*
561 #---now do the cross sinfo_correlation and produce a wavelength map---
562  */
563 } else if (cfg->wavemapInd == 1 && cfg->calibIndicator == 0) {
564  sinfo_msg("Wavemap");
565  acoefs = sinfo_new_2Dfloatarray ( cfg->nrDispCoefficients, lx);
566  /* #read the parameterized dispersion relation */
567 
568  tbl_name = (char*) cpl_calloc(MAX_NAME_SIZE,sizeof(char*));
569  col_name = (char*) cpl_calloc(MAX_NAME_SIZE,sizeof(char*));
570  strcpy(tbl_name,cfg->coeffsName);
571  tbl_wcal = cpl_table_load(tbl_name,1,0);
572  if(cpl_error_get_code() != CPL_ERROR_NONE) {
573  sinfo_msg("cannot load table %s",tbl_name);
574  sinfo_msg_error("%s", (char* ) cpl_error_get_message());
575  sinfo_qc_wcal_delete(&qc);
576  return -1;
577  }
578  for (i =0; i < lx; i++) {
579  for (j = 0; j< cfg->nrDispCoefficients; j++) {
580  snprintf(col_name,MAX_NAME_SIZE-1,"%s%d","coeff",j);
581  acoefs[j][i]=cpl_table_get_double(tbl_wcal,col_name,i,status);
582  }
583  }
584  if(cpl_error_get_code() != CPL_ERROR_NONE) {
585  sinfo_msg("cannot read table %s",tbl_name);
586  sinfo_msg_error("%s", (char* ) cpl_error_get_message());
587  sinfo_qc_wcal_delete(&qc);
588  return -1;
589  }
590  cpl_free(col_name);
591  cpl_free(tbl_name);
592  sinfo_free_table(&tbl_wcal);
593 
594  map = sinfo_new_create_shifted_slit_wavemap2 ( im,
595  acoefs,
596  cfg->nrDispCoefficients,
597  wave,
598  intens,
599  n_lines,
600  cfg->magFactor,
601  cfg->guessDispersion1,
602  cfg->pixeldist );
603  if (map == NULL) {
604  sinfo_msg_error("sinfo_createShiftedSlitWavemap2 failed!\n");
605  sinfo_qc_wcal_delete(&qc);
606  return -1;
607  }
608 
609  par = sinfo_new_fit_params(15*n_lines);
610  sinfo_msg("Check shifts");
611 
612  shift = sinfo_new_check_correlated_line_positions ( im, acoefs,
613  cfg->nrDispCoefficients,
614  wave,
615  intens,
616  n_lines,
617  cfg->fwhm,
618  cfg->halfWidth,
619  cfg->minAmplitude,
620  cfg->guessDispersion1,
621  par );
622 
623 
624  if (FLAG == shift){
625  sinfo_msg_error("sinfo_checkCorrelatedLinePositions failed!\n");
626  }
627 
628 
629  sinfo_det_ncounts(raw, cfg->qc_thresh_max,qc);
630  qclog_tbl = sinfo_qclog_init();
631  ck0_nomsg(sinfo_qclog_add_int(qclog_tbl,"QC NLINES",n_lines,
632  "Number of found lines","%d"));
633 
634 
635 
636  fwhm_avg = cpl_table_get_column_mean(tbl_fp,"fpar1");
637  fwhm_med = cpl_table_get_column_median(tbl_fp,"fpar1");
638 
639  ck0_nomsg(sinfo_qclog_add_double(qclog_tbl,"QC FWHM MED",fwhm_med,
640  "Median FWHM of found lines","%f"));
641  ck0_nomsg(sinfo_qclog_add_double(qclog_tbl,"QC FWHM AVG",fwhm_avg,
642  "Average FWHM of found lines","%f"));
643 
644 
645  if(-1 == sinfo_pro_save_ima(map,raw,sof,cfg->outName,
646  PRO_WAVE_MAP,qclog_tbl,cpl_func,config)) {
647  sinfo_msg_error("cannot save ima %s", cfg->outName);
648  }
649  sinfo_free_table(&qclog_tbl);
650 
651 
652  /* # ---free memory--- */
653  sinfo_new_destroy_2Dfloatarray ( &acoefs, cfg->nrDispCoefficients );
654  /* To fix a memory bug we comment the following. But check! */
655 
656 } else if (cfg->wavemapInd == 1 && cfg->calibIndicator == 1) {
657  sinfo_msg_error("give either wavemapIndicator = yes and calibIndicator = no \
658  or wavemapIndicator = no and calibIndicator = yes") ;
659  sinfo_qc_wcal_delete(&qc);
660  return -1;
661 }
662 
663 
664 /*-------------------------------------------------------------------
665  *-------------------SLITFITS----------------------------------------
666  *-------------------------------------------------------------------
667  #--fit the slitlet sinfo_edge positions if desired--
668  */
669 if (cfg->slitposIndicator == 1) {
670  sinfo_msg("fit the slitlet sinfo_edge positions");
671 
672 
673  /* #store the resulting sitlet positions in an TFITS table */
674  tbl_name = (char*) cpl_calloc(MAX_NAME_SIZE,sizeof(char*));
675  tbl_spos = cpl_table_new(32);
676  cpl_table_new_column(tbl_spos,"pos1", CPL_TYPE_DOUBLE);
677  cpl_table_new_column(tbl_spos,"pos2", CPL_TYPE_DOUBLE);
678  cpl_table_set_column_format(tbl_spos,"pos1", "15.9f");
679  cpl_table_set_column_format(tbl_spos,"pos2", "15.9f");
680 
681  for (i =0; i< 32; i++) {
682  /*
683  fprintf( slitpos_file, "%15.9f %15.9f \n",
684  sinfo_new_array2D_get_value(sinfo_slit_pos,i,0),
685  sinfo_new_array2D_get_value(sinfo_slit_pos,i,1));
686  */
687  cpl_table_set_double(tbl_spos,"pos1",i,
688  sinfo_new_array2D_get_value(sinfo_slit_pos,i,0));
689  cpl_table_set_double(tbl_spos,"pos2",i,
690  sinfo_new_array2D_get_value(sinfo_slit_pos,i,1));
691 
692  }
693  /* strcpy(tbl_name,cfg->slitposName); */
694  strcpy(tbl_name,"out_guess_slit_pos.fits");
695  if(-1 == sinfo_pro_save_tbl(tbl_spos,raw,sof,tbl_name,
696  PRO_SLIT_POS,NULL,cpl_func,config)) {
697  sinfo_msg_error("cannot save tbl %s", tbl_name);
698  }
699  sinfo_free_table(&tbl_spos);
700  cpl_free(tbl_name);
701  /*# free memory*/
702  sinfo_new_destroy_2Dfloatarray ( &sinfo_slit_pos, 32 );
703 
704 
705 }
706 
707 /* #-----free the rest memory--*/
708 if ( (cfg->slitposIndicator == 1 && cfg->estimateIndicator != 1) ||
709  (cfg->calibIndicator == 1) || (cfg->wavemapInd == 1) ){
710  sinfo_new_destroy_fit_params(&par);
711 }
712 sinfo_free_image( &im );
713 sinfo_free_image( &map );
714 sinfo_wavecal_free(&cfg);
715 sinfo_qc_wcal_delete(&qc);
716 
717  return 0;
718 
719  cleanup:
720  return -1;
721 
722 
723 
724 }
725