SINFONI Pipeline Reference Manual  2.6.0
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  cpl_image_delete(im);
245  return -1;
246  }
247 
248  tbl_line_list_name=cfg->lineList;
249  tbl_line_list = cpl_table_load(tbl_line_list_name,1,0);
250  if(cpl_error_get_code() != CPL_ERROR_NONE) {
251  sinfo_msg_error("%s", (char* ) cpl_error_get_message());
252  sinfo_qc_wcal_delete(&qc);
253  return -1;
254  }
255  n = cpl_table_get_nrow(tbl_line_list);
256  n_lines = n;
257  if(cpl_error_get_code() != CPL_ERROR_NONE) {
258  sinfo_msg_error("%s", (char* ) cpl_error_get_message());
259  sinfo_qc_wcal_delete(&qc);
260  cpl_image_delete(im);
261  return -1;
262  }
263 
264  /* THIS ORIGINATES A MEMORY LEAK
265  wave = sinfo_new_floatarray (n);
266  intens = sinfo_new_floatarray (n);
267  if (wave == NULL || intens == NULL) {
268  sinfo_msg_error("could not allocate memory for the line list values\n" );
269  sinfo_qc_wcal_delete(&qc);
270  return -1;
271  }
272  */
273 
274  wave = cpl_table_get_data_float(tbl_line_list,"wave");
275  if(cpl_error_get_code() != CPL_ERROR_NONE) {
276  sinfo_msg_error("%s", (char* ) cpl_error_get_message());
277  sinfo_qc_wcal_delete(&qc);
278  cpl_image_delete(im);
279  return -1;
280  }
281 
282  intens = cpl_table_get_data_float(tbl_line_list,"int");
283  if(cpl_error_get_code() != CPL_ERROR_NONE) {
284  sinfo_msg_error("%s", (char* ) cpl_error_get_message());
285  sinfo_qc_wcal_delete(&qc);
286  cpl_image_delete(im);
287  return -1;
288  }
289 
290 }
291 
292 
293 
294 /*
295  ----------------------------------------------------------------------
296  ---------------------------FINDLINES----------------------------------
297  ----------------------------------------------------------------------
298  */
299 
300 
301 /*if not yet done:
302  do the wavelength calibration, that means:
303  find the dispersion relation and parameterize its coefficients
304  */
305 /*
306  sinfo_msg("guessBeginWave=%g",cfg->guessBeginWavelength);
307  sinfo_msg("guessDisp1=%g",cfg->guessDispersion1);
308  sinfo_msg("guessDisp2=%g",cfg->guessDispersion2);
309 */
310 if (cfg->calibIndicator == 1 && cfg->wavemapInd == 0) {
311  sinfo_msg("Findlines");
312  acoefs = sinfo_new_2Dfloatarray(cfg->nrDispCoefficients, lx);
313  /*allocate memory*/
314  n_found_lines = sinfo_new_intarray(lx);
315  row_clean = sinfo_new_2Dintarray(lx, n_lines);
316  wavelength_clean = sinfo_new_2Dfloatarray(lx, n_lines);
317  sum_pointer = sinfo_new_intarray(1) ;
318  /*find the emission lines in each image column*/
319  sinfo_new_intarray_set_value(sum_pointer, 0, 0);
320  check = sinfo_new_find_lines(im, wave, intens, n_lines, row_clean,
321  wavelength_clean, cfg->guessBeginWavelength,
322  cfg->guessDispersion1, cfg->guessDispersion2,
323  cfg->mindiff, cfg->halfWidth,
324  n_found_lines, cfg->sigma, sum_pointer );
325  if (-1 == check) {
326  sinfo_msg_error("sinfo_findLines failed!\n");
327  sinfo_qc_wcal_delete(&qc);
328  return -1;
329  }
330 
331 
332 /*---------------------------------------------------------------------
333  *-----------------------WAVE_CALIB-------------------------------------
334  *---------------------------------------------------------------------
335  */
336 
337 
338  sinfo_msg("Wave Calibration");
339  sum = sinfo_new_intarray_get_value(sum_pointer,0);
340  /* allocate memory for the fit parameters */
341  par = sinfo_new_fit_params( sum );
342  if (par == NULL) {
343  sinfo_msg_error("sinfo_newFitParams failed!\n");
344  sinfo_qc_wcal_delete(&qc);
345  return -1;
346  }
347 
348  /*
349  fit each line, make a polynomial fit and fit the resulting fit
350  coefficients across the columns of the slitlet
351  */
352  sinfo_slit_pos = sinfo_new_2Dfloatarray(32,2);
353 
354  map = sinfo_new_spred_wave_cal(im,
355  par,
356  acoefs,
357  cfg->nslitlets,
358  row_clean,
359  wavelength_clean,
360  n_found_lines,
361  cfg->guessDispersion1,
362  cfg->halfWidth,
363  cfg->minAmplitude,
364  cfg->maxResidual,
365  cfg->fwhm,
366  cfg->nrDispCoefficients,
367  cfg->nrCoefCoefficients,
368  cfg->sigmaFactor,
369  cfg->pixeldist,
370  cfg->pixel_tolerance,
371  sinfo_slit_pos);
372 
373 
374  if (map == NULL ) {
375  sinfo_msg_error("sinfo_wave_cal failed!\n");
376  sinfo_qc_wcal_delete(&qc);
377  return -1;
378  }
379  sinfo_msg("Check line positions");
380 
381  shift = sinfo_new_check_line_positions (im, acoefs,
382  cfg->nrDispCoefficients,
383  cfg->guessDispersion1, par);
384  if (FLAG == shift){
385  sinfo_msg_error("checkForLinePositions failed!\n");
386  }
387 
388 
389  sinfo_det_ncounts(raw, cfg->qc_thresh_max,qc);
390  qclog_tbl = sinfo_qclog_init();
391  ck0_nomsg(sinfo_qclog_add_int(qclog_tbl,"QC WAVE ALL",
392  n_lines,"Number of found lines","%d"));
393 
394  ck0_nomsg(sinfo_qclog_add_int(qclog_tbl,"QC WAVE NPIXSAT",
395  qc->nsat,"Number of saturated pixels","%d"));
396 
397  ck0_nomsg(sinfo_qclog_add_int(qclog_tbl,"QC WAVE MAXFLUX",
398  qc->max_di,"Max int off-lamp subracted frm","%g"));
399 
400  if(-1 == sinfo_pro_save_ima(map,raw,sof,cfg->outName,
401  PRO_WAVE_MAP,qclog_tbl,cpl_func,config)) {
402  sinfo_msg_error("cannot save ima %s", cfg->outName);
403  }
404  sinfo_free_table(&qclog_tbl);
405 
406 
407  /*
408  #store the resulting polynomial fit coefficients in an
409  ASCII file if wished
410  */
411 
412  if (cfg->writeCoeffsInd == 1) {
413  col_name = (char*) cpl_calloc(MAX_NAME_SIZE,sizeof(char*));
414  tbl_name = (char*) cpl_calloc(MAX_NAME_SIZE,sizeof(char*));
415  tbl_wcal = cpl_table_new(lx);
416  for (i=0; i< cfg->nrDispCoefficients; i++) {
417  snprintf(col_name,MAX_NAME_SIZE-1,"%s%d","coeff",i);
418  cpl_table_new_column(tbl_wcal,col_name, CPL_TYPE_DOUBLE);
419  }
420 
421 
422  qclog_tbl = sinfo_qclog_init();
423  key_name = cpl_calloc(FILE_NAME_SZ,sizeof(char));
424  ck0_nomsg(sinfo_qclog_add_int(qclog_tbl,"QC WAVE ALL",
425  n_lines,"Number of found lines","%d"));
426 
427 
428 
429  for (j=0; j< lx; j++) {
430  for (i=0; i< cfg->nrDispCoefficients; i++) {
431  snprintf(col_name,MAX_NAME_SIZE-1,"%s%d","coeff",i);
432  a = sinfo_new_array2D_get_value(acoefs, i, j);
433  /* fprintf(acoefs_file, "%15.13g ", a) ; */
434  cpl_table_set_double(tbl_wcal,col_name,j,a);
435  }
436 
437 
438  }
439 
440 
441  for (i=0; i< cfg->nrDispCoefficients; i++) {
442  snprintf(col_name,MAX_NAME_SIZE-1,"%s%d","coeff",i);
443  coef_avg=cpl_table_get_column_mean(tbl_wcal,col_name);
444  coef_med=cpl_table_get_column_median(tbl_wcal,col_name);
445 
446  //trow=1+i;
447  snprintf(key_name,MAX_NAME_SIZE-1,"%s%d%s","QC COEF",i," AVG");
448  ck0_nomsg(sinfo_qclog_add_double(qclog_tbl,key_name,coef_avg,
449  "Average wavecal Coef","%g"));
450 
451  //trow=1+i+cfg->nrDispCoefficients;
452  snprintf(key_name,MAX_NAME_SIZE-1,"%s%d%s","QC COEF",i," MED");
453  ck0_nomsg(sinfo_qclog_add_double(qclog_tbl,key_name,coef_med,
454  "Median wavecal Coef","%g"));
455 
456  }
457 
458  /*
459  fclose(acoefs_file);
460  */
461  strcpy(tbl_name,cfg->coeffsName);
462 
463  if(-1 == sinfo_pro_save_tbl(tbl_wcal,raw,sof,tbl_name,
464  PRO_WAVE_COEF_SLIT,qclog_tbl,cpl_func,config)) {
465  sinfo_msg_error("cannot save tbl %s", tbl_name);
466  }
467  sinfo_free_table(&tbl_wcal);
468  sinfo_free_table(&qclog_tbl);
469  cpl_free(key_name);
470 
471  cpl_free(col_name);
472  cpl_free(tbl_name);
473 
474  }
475 
476 
477  /*
478  #store the resulting Gaussian fit parameters in an ASCII file if wished
479  */
480  if (cfg->writeParInd == 1) {
481 
482 
483  sinfo_new_dump_fit_params_to_ascii(par,WAVECAL_FIT_PARAMS_OUT_FILEASCII);
484 
485  if ( NULL == par )
486  {
487  sinfo_msg_error ("no fit parameters available!") ;
488  sinfo_qc_wcal_delete(&qc);
489  return -1;
490  }
491 
492  if ( NULL == cfg->paramsList )
493  {
494  sinfo_msg_error ("no filename available!") ;
495  sinfo_qc_wcal_delete(&qc);
496  return -1;
497  }
498 
499  tbl_fp = cpl_table_new(par[0] -> n_params);
500  cpl_table_new_column(tbl_fp,"n_params", CPL_TYPE_INT);
501  cpl_table_new_column(tbl_fp,"column", CPL_TYPE_INT);
502  cpl_table_new_column(tbl_fp,"line", CPL_TYPE_INT);
503  col = (char*) cpl_calloc(MAX_NAME_SIZE,sizeof(char*));
504 
505  for(j=0;j<4;j++) {
506  snprintf(col,MAX_NAME_SIZE-1,"%s%d","fpar",j);
507  cpl_table_new_column(tbl_fp,col, CPL_TYPE_DOUBLE);
508  snprintf(col,MAX_NAME_SIZE-1,"%s%d","dpar",j);
509  cpl_table_new_column(tbl_fp,col, CPL_TYPE_DOUBLE);
510  }
511 
512 
513 
514  qclog_tbl = sinfo_qclog_init();
515  ck0_nomsg(sinfo_qclog_add_int(qclog_tbl,"QC NLINES",n_lines,
516  "Number of Found lines","%d"));
517 
518  for ( i = 0 ; i < par[0] -> n_params ; i++ )
519  {
520  cpl_table_set_int(tbl_fp,"n_params",i,par[i]->n_params);
521  cpl_table_set_int(tbl_fp,"column",i,par[i]->column);
522  cpl_table_set_int(tbl_fp,"line",i,par[i]->line);
523 
524 
525  for(j=0;j<4;j++) {
526  snprintf(col,MAX_NAME_SIZE-1,"%s%d","fpar",j);
527  cpl_table_set_double(tbl_fp,col,i,par[i]->fit_par[j]);
528  snprintf(col,MAX_NAME_SIZE-1,"%s%d","dpar",j);
529  cpl_table_set_double(tbl_fp,col,i,par[i]->derv_par[j]);
530  }
531  }
532 
533  fwhm_avg = cpl_table_get_column_mean(tbl_fp,"fpar1");
534  fwhm_med = cpl_table_get_column_median(tbl_fp,"fpar1");
535  ck0_nomsg(sinfo_qclog_add_double(qclog_tbl,"QC FWHM MED",fwhm_med,
536  "Median FWHM of found lines","%f"));
537 
538  ck0_nomsg(sinfo_qclog_add_double(qclog_tbl,"QC FWHM AVG",fwhm_avg,
539  "Average FWHM of found lines","%f"));
540 
541 
542  if(-1 == sinfo_pro_save_tbl(tbl_fp,raw,sof,cfg->paramsList,
543  PRO_WAVE_PAR_LIST,qclog_tbl,cpl_func,config)) {
544  sinfo_msg_error("cannot save tbl %s", cfg->paramsList);
545  }
546  sinfo_free_table(&qclog_tbl);
547 
548  sinfo_free_table(&tbl_fp) ;
549  cpl_free(col);
550 
551  }
552  /* free memory */
553  sinfo_new_destroy_2Dfloatarray ( &wavelength_clean, lx );
554  sinfo_new_destroy_2Dintarray (&row_clean, lx);
555  sinfo_new_destroy_intarray(&n_found_lines );
556  sinfo_new_destroy_intarray(&sum_pointer );
557  sinfo_new_destroy_2Dfloatarray ( &acoefs, cfg->nrDispCoefficients );
558 
559 /*----------------------------------------------------------------------
560  *-------------------WAVEMAP--------------------------------------------
561  *----------------------------------------------------------------------
562  */
563 
564 /*
565 #---now do the cross sinfo_correlation and produce a wavelength map---
566  */
567 } else if (cfg->wavemapInd == 1 && cfg->calibIndicator == 0) {
568  sinfo_msg("Wavemap");
569  acoefs = sinfo_new_2Dfloatarray ( cfg->nrDispCoefficients, lx);
570  /* #read the parameterized dispersion relation */
571 
572  tbl_name = (char*) cpl_calloc(MAX_NAME_SIZE,sizeof(char*));
573  col_name = (char*) cpl_calloc(MAX_NAME_SIZE,sizeof(char*));
574  strcpy(tbl_name,cfg->coeffsName);
575  tbl_wcal = cpl_table_load(tbl_name,1,0);
576  if(cpl_error_get_code() != CPL_ERROR_NONE) {
577  sinfo_msg("cannot load table %s",tbl_name);
578  sinfo_msg_error("%s", (char* ) cpl_error_get_message());
579  sinfo_qc_wcal_delete(&qc);
580  cpl_image_delete(im);
581  cpl_free(col_name);
582  return -1;
583  }
584  for (i =0; i < lx; i++) {
585  for (j = 0; j< cfg->nrDispCoefficients; j++) {
586  snprintf(col_name,MAX_NAME_SIZE-1,"%s%d","coeff",j);
587  acoefs[j][i]=cpl_table_get_double(tbl_wcal,col_name,i,status);
588  }
589  }
590  if(cpl_error_get_code() != CPL_ERROR_NONE) {
591  sinfo_msg("cannot read table %s",tbl_name);
592  sinfo_msg_error("%s", (char* ) cpl_error_get_message());
593  sinfo_qc_wcal_delete(&qc);
594  return -1;
595  }
596  cpl_free(col_name);
597  cpl_free(tbl_name);
598  sinfo_free_table(&tbl_wcal);
599 
600  map = sinfo_new_create_shifted_slit_wavemap2 ( im,
601  acoefs,
602  cfg->nrDispCoefficients,
603  wave,
604  intens,
605  n_lines,
606  cfg->magFactor,
607  cfg->guessDispersion1,
608  cfg->pixeldist );
609  if (map == NULL) {
610  sinfo_msg_error("sinfo_createShiftedSlitWavemap2 failed!\n");
611  sinfo_qc_wcal_delete(&qc);
612  return -1;
613  }
614 
615  par = sinfo_new_fit_params(15*n_lines);
616  sinfo_msg("Check shifts");
617 
618  shift = sinfo_new_check_correlated_line_positions ( im, acoefs,
619  cfg->nrDispCoefficients,
620  wave,
621  intens,
622  n_lines,
623  cfg->fwhm,
624  cfg->halfWidth,
625  cfg->minAmplitude,
626  cfg->guessDispersion1,
627  par );
628 
629 
630  if (FLAG == shift){
631  sinfo_msg_error("sinfo_checkCorrelatedLinePositions failed!\n");
632  }
633 
634 
635  sinfo_det_ncounts(raw, cfg->qc_thresh_max,qc);
636  qclog_tbl = sinfo_qclog_init();
637  ck0_nomsg(sinfo_qclog_add_int(qclog_tbl,"QC NLINES",n_lines,
638  "Number of found lines","%d"));
639 
640 
641 
642  fwhm_avg = cpl_table_get_column_mean(tbl_fp,"fpar1");
643  fwhm_med = cpl_table_get_column_median(tbl_fp,"fpar1");
644 
645  ck0_nomsg(sinfo_qclog_add_double(qclog_tbl,"QC FWHM MED",fwhm_med,
646  "Median FWHM of found lines","%f"));
647  ck0_nomsg(sinfo_qclog_add_double(qclog_tbl,"QC FWHM AVG",fwhm_avg,
648  "Average FWHM of found lines","%f"));
649 
650 
651  if(-1 == sinfo_pro_save_ima(map,raw,sof,cfg->outName,
652  PRO_WAVE_MAP,qclog_tbl,cpl_func,config)) {
653  sinfo_msg_error("cannot save ima %s", cfg->outName);
654  }
655  sinfo_free_table(&qclog_tbl);
656 
657 
658  /* # ---free memory--- */
659  sinfo_new_destroy_2Dfloatarray ( &acoefs, cfg->nrDispCoefficients );
660  /* To fix a memory bug we comment the following. But check! */
661 
662 } else if (cfg->wavemapInd == 1 && cfg->calibIndicator == 1) {
663  sinfo_msg_error("give either wavemapIndicator = yes and calibIndicator = no \
664  or wavemapIndicator = no and calibIndicator = yes") ;
665  sinfo_qc_wcal_delete(&qc);
666  return -1;
667 }
668 
669 
670 /*-------------------------------------------------------------------
671  *-------------------SLITFITS----------------------------------------
672  *-------------------------------------------------------------------
673  #--fit the slitlet sinfo_edge positions if desired--
674  */
675 if (cfg->slitposIndicator == 1) {
676  sinfo_msg("fit the slitlet sinfo_edge positions");
677 
678 
679  /* #store the resulting sitlet positions in an TFITS table */
680  tbl_name = (char*) cpl_calloc(MAX_NAME_SIZE,sizeof(char*));
681  tbl_spos = cpl_table_new(32);
682  cpl_table_new_column(tbl_spos,"pos1", CPL_TYPE_DOUBLE);
683  cpl_table_new_column(tbl_spos,"pos2", CPL_TYPE_DOUBLE);
684  cpl_table_set_column_format(tbl_spos,"pos1", "15.9f");
685  cpl_table_set_column_format(tbl_spos,"pos2", "15.9f");
686 
687  for (i =0; i< 32; i++) {
688  /*
689  fprintf( slitpos_file, "%15.9f %15.9f \n",
690  sinfo_new_array2D_get_value(sinfo_slit_pos,i,0),
691  sinfo_new_array2D_get_value(sinfo_slit_pos,i,1));
692  */
693  cpl_table_set_double(tbl_spos,"pos1",i,
694  sinfo_new_array2D_get_value(sinfo_slit_pos,i,0));
695  cpl_table_set_double(tbl_spos,"pos2",i,
696  sinfo_new_array2D_get_value(sinfo_slit_pos,i,1));
697 
698  }
699  /* strcpy(tbl_name,cfg->slitposName); */
700  strcpy(tbl_name,"out_guess_slit_pos.fits");
701  if(-1 == sinfo_pro_save_tbl(tbl_spos,raw,sof,tbl_name,
702  PRO_SLIT_POS,NULL,cpl_func,config)) {
703  sinfo_msg_error("cannot save tbl %s", tbl_name);
704  }
705  sinfo_free_table(&tbl_spos);
706  cpl_free(tbl_name);
707  /*# free memory*/
708  sinfo_new_destroy_2Dfloatarray ( &sinfo_slit_pos, 32 );
709 
710 
711 }
712 
713 /* #-----free the rest memory--*/
714 if ( (cfg->slitposIndicator == 1 && cfg->estimateIndicator != 1) ||
715  (cfg->calibIndicator == 1) || (cfg->wavemapInd == 1) ){
716  sinfo_new_destroy_fit_params(&par);
717 }
718 sinfo_free_image( &im );
719 sinfo_free_image( &map );
720 sinfo_wavecal_free(&cfg);
721 sinfo_qc_wcal_delete(&qc);
722 
723  return 0;
724 
725  cleanup:
726  return -1;
727 
728 
729 
730 }
731 
#define sinfo_msg_error(...)
Print an error message.
Definition: sinfo_msg.h:69