SINFONI Pipeline Reference Manual  2.6.0
sinfo_new_wave_cal_slit2.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_wave_cal_slit2.c
22  Author : A. Modigliani
23  Created on : Sep 17, 2003
24  Description :
25 
26  ---------------------------------------------------------------------------*/
27 
28 #ifdef HAVE_CONFIG_H
29 # include <config.h>
30 #endif
31 /*----------------------------------------------------------------------------
32  Includes
33  ---------------------------------------------------------------------------*/
34 #include "sinfo_new_wave_cal_slit2.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_absolute.h"
40 #include "sinfo_wave_calibration.h"
41 #include "sinfo_wavecal.h"
42 #include "sinfo_globals.h"
43 #include "sinfo_hidden.h"
44 
45 #include "sinfo_utilities.h"
46 #include "sinfo_utilities_scired.h"
47 #include "sinfo_utils_wrappers.h"
48 #include "sinfo_error.h"
49 
50 
51 /*----------------------------------------------------------------------------
52  Defines
53  ---------------------------------------------------------------------------*/
54 
55 /*----------------------------------------------------------------------------
56  Function Definitions
57  ---------------------------------------------------------------------------*/
58 static cpl_error_code
59 sinfo_image_resample(const char* plugin_id,
60  cpl_parameterlist* config,
61  cpl_frameset* sof,
62  cpl_frameset* ref_set);
63 
71 /*----------------------------------------------------------------------------
72  Function : sinfo_wave_cal_slit()
73  In : ini_file: file name of according .ini file
74  Out : integer (0 if it worked, -1 if it doesn't)
75  Job :
76 
77 
78  Normal method:
79 
80  does the wavelength calibration and the fitting of the slitlet sinfo_edge
81  positions (ASCII file 32 x 2 values) if wished
82  produces an array of the bcoefs and of the fit parameters if wished and a
83  wavelength calibration map input is an emission line frame and a line list
84 
85 
86  o searching for lines by cross sinfo_correlation with a line list
87  o Gaussian fitting of emission lines in each column->positions of the lines->
88  resulting fit parameters can be stored in an ASCII file
89  o Fitting of a polynomial to the line positions for each column
90  o Smoothing: fitting of each polynomial coefficient by another polynomial
91  across the whole frame -> resulting polynomial coefficients can be stored
92  in an ASCII file.
93  o Wavelength calibration map (micron value for each frame pixel) can be
94  produced by using these coefficients and a cross sinfo_correlation to the
95  original frame
96 
97  o The slitlet sinfo_edge positions can be fitted:
98  1) Automatically (not really stable) or by using guess sinfo_edge positions
99  2) By using a Boltzmann or a linear slope function
100 
101  Slit method:
102 
103  does the wavelength calibration and the fitting of the slitlet sinfo_edge
104  positions (ASCII file 32 x 2 values) if wished produces a list of the fit
105  parameters and of the smoothed coefficients if wished and a wavelength
106  calibration map input is an emission line frame and a line list
107 
108  o Does the same as other method but smoothes the found polynomial
109  coefficients within each slitlet and not over the whole frame.
110 
111  o Produces always a wavelength calibration map and does not crosscorrelate.
112 
113  ---------------------------------------------------------------------------*/
114 
115 
116 int
117 sinfo_new_wave_cal_slit2(const char* plugin_id,
118  cpl_parameterlist* config,
119  cpl_frameset* sof,cpl_frameset* ref_set)
120 {
121  wave_config * cfg =NULL;
122  char col_name[MAX_NAME_SIZE];
123  char tbl_name[MAX_NAME_SIZE];
124  char tbl_fitpar_name[MAX_NAME_SIZE];
125  char tbl_line_list_name[MAX_NAME_SIZE];
126  char tbl_slitpos_guess_name[MAX_NAME_SIZE];
127  char key_name[MAX_NAME_SIZE];
128  char col[MAX_NAME_SIZE];
129  int check = 0;
130  int lx = 0;
131  /* not used later
132  * int ly = 0;
133  */
134  int n_lines=0;
135  int i = 0;
136  int j = 0;
137  int n = 0;
138  int readsum=0;
139  int sum=0;
140  int fit=0;
141  int sw=0;
142 
143  int* status=NULL;
144  int* n_found_lines=NULL;
145  int* sum_pointer=NULL;
146  int** row_clean=NULL;
147 
148  float a=0;
149  float shift=0;
150  float val_x=0;
151  float val_y=0;
152 
153  float* wave=NULL;
154  float* intens=NULL;
155 
156  float** acoefs=NULL;
157  float** wavelength_clean=NULL;
158  float** sinfo_slit_pos=NULL;
159 
160  double fwhm_med=0;
161  double fwhm_avg=0;
162  double coef_med=0;
163  double coef_avg=0;
164 
165  cpl_image * im=NULL ;
166 
167  FitParams** par=NULL;
168 
169  cpl_table* tbl_wcal=NULL;
170  cpl_table* tbl_spos=NULL;
171  cpl_table* tbl_fitpar = NULL;
172  cpl_table* tbl_line_list = NULL;
173  cpl_table* tbl_slitpos_guess=NULL;
174  cpl_table * tbl_fp =NULL;
175  cpl_table* qclog_tbl=NULL;
176 
177  cpl_image* map_img=NULL;
178 
179  cpl_frameset* raw=NULL;
180  cpl_parameter* p=NULL;
181 
182  qc_wcal* qc=sinfo_qc_wcal_new();
183  int pdensity=0;
184 
185  /* -----------------------------------------------------------------
186  1) parse the file names and parameters to the ns_config data
187  structure cfg
188  -----------------------------------------------------------------
189  */
190 
191  check_nomsg(p=cpl_parameterlist_find(config,"sinfoni.product.density"));
192  check_nomsg(pdensity=cpl_parameter_get_int(p));
193 
194 
195  sinfo_msg("Parsing cpl input");
196  check_nomsg(raw=cpl_frameset_new());
197  cknull(cfg = sinfo_parse_cpl_input_wave(config,sof,&raw),
198  "could not parse cpl input!") ;
199 
200  check_nomsg(p = cpl_parameterlist_find(config,
201  "sinfoni.wavecal.slitpos_boostrap"));
202  check_nomsg(sw=cpl_parameter_get_bool(p));
203 
204  if(sw==1) {
205  cfg->nslitlets=32;
206  cfg->calibIndicator=1;
207  cfg->wavemapInd=0;
208  cfg->slitposIndicator=1;
209  sinfo_msg("***********************************");
210  sinfo_msg("parameter setting for %s",PRO_WAVE_SLITPOS_STACKED);
211  sinfo_msg("***********************************");
212  }
213 
214  if(sinfo_is_fits_file(cfg->inFrame) != 1) {
215  sinfo_msg_error("Input file cfg->inFrame %s is not FITS",cfg->inFrame);
216  goto cleanup;
217  }
218 
219 
220  if (cfg->slitposIndicator == 1 && cfg->estimateIndicator == 1) {
221  if (sinfo_is_fits_file(cfg->slitposGuessName) != 1) {
222  sinfo_msg_error("slitlet position guess list not given!");
223  goto cleanup;
224  }
225  }
226 
227  if (cfg->calibIndicator == 0 && cfg->wavemapInd == 1) {
228  if (sinfo_is_fits_file(cfg->coeffsName) != 1) {
229  sinfo_msg_error("coefficients list not given!");
230  goto cleanup;
231  }
232  }
233 
234  if (cfg->slitposIndicator == 1) {
235  if (cfg->calibIndicator != 1 && cfg->estimateIndicator != 1) {
236  if (sinfo_is_fits_file(cfg->paramsList) != 1) {
237  sinfo_msg_error("parameter list not given!");
238  goto cleanup;
239  }
240  }
241  }
242 
243  /*---load the emission line frame---*/
244  check(im = cpl_image_load(cfg->inFrame,CPL_TYPE_FLOAT,0,0)
245  ,"could not load image");
246  lx = cpl_image_get_size_x(im);
247  /*
248  * ly = cpl_image_get_size_y(im);
249  */
250 
251  if (cfg->calibIndicator == 1 || cfg->wavemapInd == 1) {
252  /*---open the line list and read the number of lines---*/
253  strcpy(tbl_line_list_name,cfg->lineList);
254  check_nomsg(tbl_line_list = cpl_table_load(tbl_line_list_name,1,0));
255  check_nomsg(n = cpl_table_get_nrow(tbl_line_list));
256  n_lines = n;
257 
258  check_nomsg(wave = cpl_table_get_data_float(tbl_line_list,"wave"));
259  cpl_type type=cpl_table_get_column_type(tbl_line_list,"int");
260  if(type==CPL_TYPE_INT) {
261  check_nomsg(cpl_table_cast_column(tbl_line_list,"int", "fint",CPL_TYPE_FLOAT));
262  check_nomsg(intens = cpl_table_get_data_float(tbl_line_list,"fint"));
263  } else {
264  check_nomsg(intens = cpl_table_get_data_float(tbl_line_list,"int"));
265  }
266  }
267 
268  /*
269  ----------------------------------------------------------------------
270  ---------------------------FINDLINES----------------------------------
271  ----------------------------------------------------------------------
272  */
273 
274  /*if not yet done:
275  do the wavelength calibration, that means:
276  find the dispersion relation and parameterize its coefficients
277  */
278  /*
279  sinfo_msg("guessBeginWave=%g",cfg->guessBeginWavelength);
280  sinfo_msg("guessDisp1=%g",cfg->guessDispersion1);
281  sinfo_msg("guessDisp2=%g",cfg->guessDispersion2);
282  */
283  if (cfg->calibIndicator == 1 && cfg->wavemapInd == 0) {
284  sinfo_msg("Findlines");
285  acoefs = sinfo_new_2Dfloatarray(cfg->nrDispCoefficients, lx);
286 
287  /*allocate memory*/
288  n_found_lines = sinfo_new_intarray(lx);
289  row_clean = sinfo_new_2Dintarray(lx, n_lines);
290  wavelength_clean = sinfo_new_2Dfloatarray(lx, n_lines);
291  sum_pointer = sinfo_new_intarray(1) ;
292 
293  /*find the emission lines in each image column*/
294  sinfo_new_intarray_set_value(sum_pointer, 0, 0);
295 
296  ck0( (check = sinfo_new_find_lines(im,
297  wave,
298  intens,
299  n_lines,
300  row_clean,
301  wavelength_clean,
302  cfg->guessBeginWavelength,
303  cfg->guessDispersion1,
304  cfg->guessDispersion2,
305  cfg->mindiff,
306  cfg->halfWidth,
307  n_found_lines,
308  cfg->sigma,
309  sum_pointer) ),
310  "sinfo_findLines failed!");
311 
312  /*---------------------------------------------------------------------
313  *-----------------------WAVE_CALIB-------------------------------------
314  *---------------------------------------------------------------------
315  */
316  sinfo_msg("Wave Calibration");
317  sum = sinfo_new_intarray_get_value(sum_pointer,0);
318  /* allocate memory for the fit parameters */
319  cknull(par = sinfo_new_fit_params( sum ),
320  "sinfo_newFitParams failed!");
321 
322  /*
323  fit each line, make a polynomial fit and fit the resulting fit
324  coefficients across the columns of the slitlet
325  */
326  cknull(map_img = sinfo_new_wave_cal(im,
327  par,
328  acoefs,
329  cfg->nslitlets,
330  row_clean,
331  wavelength_clean,
332  n_found_lines,
333  cfg->guessDispersion1,
334  cfg->halfWidth,
335  cfg->minAmplitude,
336  cfg->maxResidual,
337  cfg->fwhm,
338  cfg->nrDispCoefficients,
339  cfg->nrCoefCoefficients,
340  cfg->sigmaFactor,
341  cfg->pixeldist,
342  cfg->pixel_tolerance),
343  "sinfo_wave_cal failed!");
344 
345  sinfo_msg("Check line positions");
346 
347  shift=sinfo_new_check_line_positions(im,acoefs,cfg->nrDispCoefficients,
348  cfg->guessDispersion1,par);
349  if (FLAG == shift){
350  sinfo_msg_error("checkForLinePositions failed!\n");
351  }
352 
353 
354  sinfo_det_ncounts(raw, cfg->qc_thresh_max, qc);
355 
356  cknull_nomsg(qclog_tbl = sinfo_qclog_init());
357  ck0_nomsg(sinfo_qclog_add_int(qclog_tbl,"QC WAVE ALL",n_lines,
358  "Number of found lines","%d"));
359  ck0_nomsg(sinfo_qclog_add_int(qclog_tbl,"QC WAVE NPIXSAT",qc->nsat,
360  "Number of saturated pixels","%d"));
361  ck0_nomsg(sinfo_qclog_add_double(qclog_tbl,"QC WAVE MAXFLUX",qc->max_di,
362  "Max int off-lamp subtracted frm","%g"));
363  ck0_nomsg(sinfo_qclog_add_double(qclog_tbl,"QC WAVE POSERR",shift,
364  "Overall positioning error in mum","%g"));
365 
366  ck0(sinfo_pro_save_ima(map_img,ref_set,sof,cfg->outName,
367  PRO_WAVE_MAP,qclog_tbl,plugin_id,config),
368  "cannot save ima %s", cfg->outName);
369 
370  sinfo_free_table(&qclog_tbl);
371  sinfo_free_image(&map_img);
372 
373  /*
374  #store the resulting polynomial fit coefficients in an
375  ASCII file if wished
376  */
377 
378  if (cfg->writeCoeffsInd == 1) {
379  check_nomsg(tbl_wcal = cpl_table_new(lx));
380  for (i=0; i< cfg->nrDispCoefficients; i++) {
381  snprintf(col_name,MAX_NAME_SIZE-1,"%s%d","coeff",i);
382  check_nomsg(cpl_table_new_column(tbl_wcal,col_name, CPL_TYPE_DOUBLE));
383  }
384 
385  cknull_nomsg(qclog_tbl = sinfo_qclog_init());
386  ck0_nomsg(sinfo_qclog_add_int(qclog_tbl,"QC WAVE ALL",n_lines,
387  "Number found lines","%d"));
388 
389  for (j=0; j< lx; j++) {
390  for (i=0; i< cfg->nrDispCoefficients; i++) {
391  snprintf(col_name,MAX_NAME_SIZE-1,"%s%d","coeff",i);
392  a = sinfo_new_array2D_get_value(acoefs, i, j);
393  /* fprintf(acoefs_file, "%15.13g ", a) ; */
394  cpl_table_set_double(tbl_wcal,col_name,j,a);
395  }
396  }
397  for (i=0; i< cfg->nrDispCoefficients; i++) {
398  snprintf(col_name,MAX_NAME_SIZE-1,"%s%d","coeff",i);
399  check_nomsg(coef_avg=cpl_table_get_column_mean(tbl_wcal,col_name));
400  check_nomsg(coef_med=cpl_table_get_column_median(tbl_wcal,col_name));
401 
402  snprintf(key_name,MAX_NAME_SIZE-1,"%s%d%s","QC COEF",i," AVG");
403  ck0_nomsg(sinfo_qclog_add_double(qclog_tbl,key_name,coef_avg,
404  "Average wavecal Coef","%g"));
405 
406  snprintf(key_name,MAX_NAME_SIZE-1,"%s%d%s","QC COEF",i," MED");
407  ck0_nomsg(sinfo_qclog_add_double(qclog_tbl,key_name,coef_med,
408  "Median wavecal Coef","%g"));
409 
410  }
411  if(pdensity >1) {
412  strcpy(tbl_name,cfg->coeffsName);
413  ck0(sinfo_pro_save_tbl(tbl_wcal,ref_set,sof,tbl_name,
414  PRO_WAVE_COEF_SLIT,qclog_tbl,plugin_id,config),
415  "cannot save tbl %s", tbl_name);
416  sinfo_free_table(&tbl_wcal);
417  sinfo_free_table(&qclog_tbl);
418  }
419 
420  }
421 
422  /*
423  #store the resulting Gaussian fit parameters in an ASCII file if wished
424  */
425  if (cfg->writeParInd == 1) {
426  sinfo_new_dump_fit_params_to_ascii(par,WAVECAL_FIT_PARAMS_OUT_FILEASCII);
427 
428  cknull(par,"no fit parameters available!") ;
429  cknull(cfg->paramsList,"no filename available!") ;
430  check_nomsg(tbl_fp = cpl_table_new(par[0] -> n_params));
431  check_nomsg(cpl_table_new_column(tbl_fp,"n_params", CPL_TYPE_INT));
432  check_nomsg(cpl_table_new_column(tbl_fp,"column", CPL_TYPE_INT));
433  check_nomsg(cpl_table_new_column(tbl_fp,"line", CPL_TYPE_INT));
434 
435  for(j=0;j<4;j++) {
436  snprintf(col,MAX_NAME_SIZE-1,"%s%d","fpar",j);
437  cpl_table_new_column(tbl_fp,col, CPL_TYPE_DOUBLE);
438  snprintf(col,MAX_NAME_SIZE-1,"%s%d","dpar",j);
439  cpl_table_new_column(tbl_fp,col, CPL_TYPE_DOUBLE);
440  }
441 
442  cknull_nomsg(qclog_tbl = sinfo_qclog_init());
443  ck0_nomsg(sinfo_qclog_add_int(qclog_tbl,"QC NLINES",n_lines,
444  "Number of found lines","%d"));
445 
446  for ( i = 0 ; i < par[0] -> n_params ; i++ ) {
447 
448  check_nomsg(cpl_table_set_int(tbl_fp,"n_params",i,par[i]->n_params));
449  check_nomsg(cpl_table_set_int(tbl_fp,"column",i,par[i]->column));
450  check_nomsg(cpl_table_set_int(tbl_fp,"line",i,par[i]->line));
451 
452  for(j=0;j<4;j++) {
453  snprintf(col,MAX_NAME_SIZE-1,"%s%d","fpar",j);
454  if(isnan(par[i]->fit_par[j])) {
455  cpl_table_set_invalid(tbl_fp,col,i);
456  } else {
457  cpl_table_set_double(tbl_fp,col,i,par[i]->fit_par[j]);
458  }
459  snprintf(col,MAX_NAME_SIZE-1,"%s%d","dpar",j);
460  if(isnan(par[i]->derv_par[j])) {
461  cpl_table_set_invalid(tbl_fp,col,i);
462  } else {
463  cpl_table_set_double(tbl_fp,col,i,par[i]->derv_par[j]);
464  }
465  }
466  }
467 
468  check_nomsg(fwhm_avg = cpl_table_get_column_mean(tbl_fp,"fpar1"));
469  check_nomsg(fwhm_med = cpl_table_get_column_median(tbl_fp,"fpar1"));
470  ck0_nomsg(sinfo_qclog_add_double(qclog_tbl,"QC FWHM MED",fwhm_med,
471  "Median FWHM of found lines","%f"));
472  ck0_nomsg(sinfo_qclog_add_double(qclog_tbl,"QC FWHM AVG",fwhm_avg,
473  "Average FWHM of found lines","%f"));
474 
475  if(pdensity > 1) {
476  ck0(sinfo_pro_save_tbl(tbl_fp,ref_set,sof,cfg->paramsList,
477  PRO_WAVE_PAR_LIST,qclog_tbl,plugin_id,config),
478  "cannot save tbl %s", cfg->paramsList);
479  }
480 
481  sinfo_free_table(&qclog_tbl);
482  sinfo_free_table(&tbl_fp) ;
483 
484  }
485 
486  /* free memory */
487  sinfo_new_destroy_2Dfloatarray ( &wavelength_clean, lx );
488  sinfo_new_destroy_2Dintarray (&row_clean, lx);
489  sinfo_new_destroy_intarray(&n_found_lines );
490  sinfo_new_destroy_intarray(&sum_pointer );
491  sinfo_new_destroy_2Dfloatarray ( &acoefs, cfg->nrDispCoefficients );
492  sinfo_free_table(&tbl_line_list);
493 
494  /*----------------------------------------------------------------------
495  *-------------------WAVEMAP--------------------------------------------
496  *----------------------------------------------------------------------
497  */
498 
499  /*
500  #---now do the cross sinfo_correlation and produce a wavelength map---
501  */
502 
503  } else if (cfg->wavemapInd == 1 && cfg->calibIndicator == 0) {
504  sinfo_msg("Wavemap");
505  acoefs = sinfo_new_2Dfloatarray ( cfg->nrDispCoefficients, lx);
506  /* #read the parameterized dispersion relation */
507 
508  strcpy(tbl_name,cfg->coeffsName);
509  check_nomsg(tbl_wcal = cpl_table_load(tbl_name,1,0));
510 
511  for (i =0; i < lx; i++) {
512  for (j = 0; j< cfg->nrDispCoefficients; j++) {
513  snprintf(col_name,MAX_NAME_SIZE-1,"%s%d","coeff",j);
514  acoefs[j][i]=cpl_table_get_double(tbl_wcal,col_name,i,status);
515  }
516  }
517  sinfo_free_table(&tbl_wcal);
518 
519  cknull(map_img = sinfo_new_create_shifted_slit_wavemap2(im,
520  acoefs,
521  cfg->nrDispCoefficients,
522  wave,
523  intens,
524  n_lines,
525  cfg->magFactor,
526  cfg->guessDispersion1,
527  cfg->pixeldist ),
528  "sinfo_createShiftedSlitWavemap2 failed!");
529 
530  par = sinfo_new_fit_params(15*n_lines);
531  sinfo_msg("Check shifts");
532 
533  shift = sinfo_new_check_correlated_line_positions ( im, acoefs,
534  cfg->nrDispCoefficients,
535  wave,
536  intens,
537  n_lines,
538  cfg->fwhm,
539  cfg->halfWidth,
540  cfg->minAmplitude,
541  cfg->guessDispersion1,
542  par );
543 
544  if (FLAG == shift){
545  sinfo_msg_error("sinfo_checkCorrelatedLinePositions failed!\n");
546  }
547 
548  sinfo_det_ncounts(raw, cfg->qc_thresh_max,qc);
549  cknull_nomsg(qclog_tbl = sinfo_qclog_init());
550 
551  ck0_nomsg(sinfo_qclog_add_int(qclog_tbl,"QC NLINES",n_lines,
552  "Number of found lines","%d"));
553 
554  check_nomsg(fwhm_avg = cpl_table_get_column_mean(tbl_fp,"fpar1"));
555  check_nomsg(fwhm_med = cpl_table_get_column_median(tbl_fp,"fpar1"));
556 
557  ck0_nomsg(sinfo_qclog_add_double(qclog_tbl,"QC FWHM MED",fwhm_med,
558  "Median FWHM of found lines","%f"));
559  ck0_nomsg(sinfo_qclog_add_double(qclog_tbl,"QC FWHM AVG",fwhm_avg,
560  "Average FWHM of found lines","%f"));
561 
562  ck0(sinfo_pro_save_ima(map_img,ref_set,sof,cfg->outName,
563  PRO_WAVE_MAP,qclog_tbl,plugin_id,config),
564  "cannot save ima %s", cfg->outName);
565 
566  sinfo_free_table(&qclog_tbl);
567  sinfo_free_image(&map_img);
568 
569  /* # ---free memory--- */
570  sinfo_new_destroy_2Dfloatarray ( &acoefs, cfg->nrDispCoefficients );
571  /* To fix a memory bug we comment the following. But check! */
572  /* cpl_free ( wave ); */
573  /* cpl_free ( intens ); */
574 
575  } else if (cfg->wavemapInd == 1 && cfg->calibIndicator == 1) {
576  sinfo_msg_error("give either wavemapIndicator = yes and calibIndicator");
577  sinfo_msg_error("= no or wavemapIndicator = no and calibIndicator = yes") ;
578  goto cleanup;
579  }
580 
581  /*-------------------------------------------------------------------
582  *-------------------SLITFITS----------------------------------------
583  *-------------------------------------------------------------------
584  #--fit the slitlet sinfo_edge positions if desired--
585  */
586  if (cfg->slitposIndicator == 1) {
587  sinfo_msg("fit the slitlet sinfo_edge positions");
588  if (cfg->calibIndicator != 1 && cfg->estimateIndicator != 1) {
589 
590  /*
591  #read the first integer to determine the number of fit
592  parameters to allocate
593  */
594 
595  if(sinfo_is_fits_file(cfg->paramsList) !=1 ) {
596  sinfo_msg_error("cannot read FITS file %s ",cfg->paramsList);
597  goto cleanup;
598  }
599  strcpy(tbl_fitpar_name,cfg->paramsList);
600  check_nomsg(tbl_fitpar = cpl_table_load(tbl_fitpar_name,1,0));
601 
602  check_nomsg(readsum=cpl_table_get_int(tbl_fitpar,"n_params",1,status));
603  sinfo_free_table(&tbl_fitpar);
604 
605  cknull(sinfo_new_fit_params( readsum ),"sinfo_new_fit_params failed!");
606 
607  ck0(sinfo_dumpTblToFitParams(par, cfg->paramsList),
608  "reading tbl %s ", cfg->paramsList);
609  }
610 
611  /* #allocate memory for the slitlet position array */
612  sinfo_slit_pos = sinfo_new_2Dfloatarray(32,2);
613  /* #now decide which fit model function you want to use by
614  reading a given character
615  */
616  /*
617  sinfo_msg("cfg->fitBoltzIndicator=%d\n",cfg->fitBoltzIndicator);
618  sinfo_msg("cfg->estimateIndicator=%d\n",cfg->estimateIndicator);
619  sinfo_msg("cfg->fitEdgeIndicator=%d\n",cfg->fitEdgeIndicator);
620  */
621  if (cfg->fitBoltzIndicator == 1) {
622  if (cfg->estimateIndicator == 1) {
623  /* #open the ASCII list of the slitlet positions--- */
624  /*READ TFITS TABLE*/
625  strcpy(tbl_slitpos_guess_name,cfg->slitposGuessName);
626  check_nomsg(tbl_slitpos_guess=cpl_table_load(tbl_slitpos_guess_name,1,0));
627  check_nomsg(n = cpl_table_get_nrow(tbl_slitpos_guess));
628 
629  for (i =0 ; i< 32; i++){
630  val_x=cpl_table_get_double(tbl_slitpos_guess,"pos1",i,status);
631  val_y=cpl_table_get_double(tbl_slitpos_guess,"pos2",i,status);
632  sinfo_new_array2D_set_value(sinfo_slit_pos,val_x,i,0);
633  sinfo_new_array2D_set_value(sinfo_slit_pos,val_y,i,1);
634  }
635  sinfo_free_table(&tbl_slitpos_guess);
636 
637  sinfo_msg("sinfo_new_fit_slits_boltz_with_estimate");
638  fit = sinfo_new_fit_slits_boltz_with_estimate(im,
639  sinfo_slit_pos,
640  cfg->boxLength,
641  cfg->yBox,
642  cfg->diffTol,
643  cfg->loPos,
644  cfg->hiPos );
645  if (fit < 0){
646  sinfo_msg_error("sinfo_new_fit_slits_boltz_with_estimate failed" );
647  goto cleanup;
648  }
649  } else {
650  sinfo_msg("sinfo_new_fit_slits_boltz");
651  fit = sinfo_new_fit_slits_boltz(im,
652  par,
653  sinfo_slit_pos,
654  cfg->boxLength,
655  cfg->yBox,
656  cfg->diffTol );
657 
658  if (fit < 0) {
659  sinfo_msg_error ( "sinfo_new_fit_slits_boltz failed" );
660  goto cleanup;
661  }
662  }
663  } else if (cfg->fitEdgeIndicator == 1) {
664 
665  if (cfg->estimateIndicator == 1){
666  /*READ TFITS TABLE*/
667  strcpy(tbl_slitpos_guess_name,cfg->slitposGuessName);
668  check_nomsg(tbl_slitpos_guess=cpl_table_load(tbl_slitpos_guess_name,1,0));
669  check_nomsg(n = cpl_table_get_nrow(tbl_slitpos_guess));
670 
671  for (i =0 ; i< 32; i++){
672  val_x=cpl_table_get_double(tbl_slitpos_guess,"pos1",i,status);
673  val_y=cpl_table_get_double(tbl_slitpos_guess,"pos2",i,status);
674  sinfo_new_array2D_set_value(sinfo_slit_pos,val_x,i,0);
675  sinfo_new_array2D_set_value(sinfo_slit_pos,val_y,i,1);
676  }
677  cpl_table_delete(tbl_slitpos_guess);
678 
679  sinfo_msg("sinfo_new_fit_slits_edge_with_estimate");
680  fit = sinfo_new_fit_slits_edge_with_estimate(im,
681  sinfo_slit_pos,
682  cfg->boxLength,
683  cfg->yBox,
684  cfg->diffTol,
685  cfg->loPos,
686  cfg->hiPos );
687  if (fit < 0) {
688  sinfo_msg_error( "sinfo_new_fit_slits_boltz failed" );
689  goto cleanup;
690  }
691  } else {
692  sinfo_msg("sinfo_new_fit_slits_edge");
693  fit = sinfo_new_fit_slits_edge(im,
694  par,
695  sinfo_slit_pos,
696  cfg->boxLength,
697  cfg->yBox,
698  cfg->diffTol );
699  if (fit < 0) {
700  sinfo_msg_error("sinfo_new_fit_slits_edge failed" );
701  goto cleanup;
702  }
703  }
704  } else {
705  sinfo_msg_error("no indication of desired fit function given" );
706  goto cleanup;
707  }
708  sinfo_free_image(&im);
709 
710  /* #store the resulting sitlet positions in an TFITS table */
711  check_nomsg(tbl_spos = cpl_table_new(32));
712  check_nomsg(cpl_table_new_column(tbl_spos,"pos1", CPL_TYPE_DOUBLE));
713  check_nomsg(cpl_table_new_column(tbl_spos,"pos2", CPL_TYPE_DOUBLE));
714  check_nomsg(cpl_table_set_column_format(tbl_spos,"pos1", "15.9f"));
715  check_nomsg(cpl_table_set_column_format(tbl_spos,"pos2", "15.9f"));
716 
717  for (i =0; i< 32; i++) {
718  cpl_table_set_double(tbl_spos,"pos1",i,
719  sinfo_new_array2D_get_value(sinfo_slit_pos,i,0));
720  cpl_table_set_double(tbl_spos,"pos2",i,
721  sinfo_new_array2D_get_value(sinfo_slit_pos,i,1));
722 
723  }
724  if(sw == 1) {
725  strcpy(tbl_name,"out_slitpos_guess.fits");
726  ck0(sinfo_pro_save_tbl(tbl_spos,ref_set,sof,tbl_name,
727  PRO_SLIT_POS_GUESS,NULL,plugin_id,config),
728  "cannot save tbl %s", tbl_name);
729  } else {
730  strcpy(tbl_name,cfg->slitposName);
731  ck0(sinfo_pro_save_tbl(tbl_spos,ref_set,sof,tbl_name,
732  PRO_SLIT_POS,NULL,plugin_id,config),
733  "cannot save tbl %s", tbl_name);
734  }
735  sinfo_free_table(&tbl_spos);
736  sinfo_new_destroy_2Dfloatarray ( &sinfo_slit_pos, 32 );
737  }
738 
739  if ( (cfg->slitposIndicator == 1 && cfg->estimateIndicator != 1) ||
740  (cfg->calibIndicator == 1) || (cfg->wavemapInd == 1) ){
741  sinfo_new_destroy_fit_params(&par);
742  }
743 
744 
745 
746 
747 
748  if(pdensity > 1) {
749  check_nomsg(sinfo_image_resample(plugin_id,config,sof,ref_set));
750 
751 
807  }
808  sinfo_free_frameset(&raw);
809  sinfo_qc_wcal_delete(&qc);
810  sinfo_wavecal_free(&cfg);
811 
812  return 0;
813 
814  cleanup:
815  sinfo_free_image(&map_img);
816  //sinfo_free_image(&wstk_img);
817  sinfo_free_table(&tbl_spos);
818  sinfo_free_table(&tbl_fitpar);
819  sinfo_free_image(&map_img);
820  sinfo_free_table(&tbl_wcal);
821  sinfo_free_table(&tbl_fp) ;
822  sinfo_free_table(&tbl_line_list);
823  sinfo_free_table(&tbl_wcal);
824  sinfo_free_table(&qclog_tbl);
825  sinfo_free_image(&map_img);
826  sinfo_new_destroy_fit_params(&par);
827  sinfo_new_destroy_2Dfloatarray ( &sinfo_slit_pos, 32 );
828  sinfo_new_destroy_2Dfloatarray ( &wavelength_clean, lx );
829  sinfo_new_destroy_2Dintarray (&row_clean, lx);
830  sinfo_new_destroy_intarray(&n_found_lines );
831  sinfo_new_destroy_intarray(&sum_pointer );
832  if(acoefs!=NULL) {
833  sinfo_new_destroy_2Dfloatarray(&acoefs,cfg->nrDispCoefficients);
834  }
835  sinfo_free_table(&tbl_line_list);
836  sinfo_free_image(&im);
837  sinfo_wavecal_free(&cfg);
838  sinfo_free_frameset(&raw);
839  sinfo_qc_wcal_delete(&qc);
840  return -1 ;
841 
842 }
843 
844 
845 
846 
847 
848 
849 
850 
851 
852 
853 
854 
855 static cpl_error_code
856 sinfo_image_resample(const char* plugin_id,
857  cpl_parameterlist* config,
858  cpl_frameset* sof,
859  cpl_frameset* ref_set)
860 {
861  //RESAMPLE ThAr frame for QC
862  double dis=0;
863  float mi=0;
864  float ma=0;
865  double cwav=0;
866  int cpix=0;
867  const cpl_frame* frm=NULL;
868  char wstk_name[80];
869  char map_name[80];
870  cpl_image* res_ima=NULL;
871  int ncoeffs=3;
872  int nrows=SINFO_RESAMP_NROWS;
873  cpl_parameter* p=NULL;
874  cpl_image* wstk_img=NULL;
875  cpl_image* map_img=NULL;
876  int x_cen=0;
877 
878  check_nomsg(p = cpl_parameterlist_find(config,
879  "sinfoni.wavecal.n_coeffs"));
880 
881  check_nomsg(ncoeffs=cpl_parameter_get_int(p));
882 
883  check_nomsg(frm=cpl_frameset_find_const(sof,PRO_WAVE_LAMP_STACKED));
884  check_nomsg(strcpy(wstk_name,cpl_frame_get_filename(frm)));
885  check_nomsg(wstk_img=cpl_image_load(wstk_name,CPL_TYPE_FLOAT,0,0));
886 
887 
888 
889  check_nomsg(frm=cpl_frameset_find_const(sof,PRO_WAVE_MAP));
890  check_nomsg(strcpy(map_name,cpl_frame_get_filename(frm)));
891  check_nomsg(map_img=cpl_image_load(map_name,CPL_TYPE_FLOAT,0,0));
892 
893 
894 
895  cknull(res_ima = sinfo_new_defined_resampling(wstk_img,
896  map_img,
897  ncoeffs,
898  &nrows,
899  &dis,
900  &mi,
901  &ma,
902  &cwav,
903  &cpix),
904  " sinfo_definedResampling() failed" ) ;
905 
906 
907  x_cen=0.5*cpl_image_get_size_x(wstk_img);
908  ck0(sinfo_pro_save_ima(res_ima,ref_set,sof,WAVECAL_RESAMPLED_OUT_FILENAME,
909  PRO_RESAMPLED_WAVE,NULL,plugin_id,config),
910  "cannot save ima %s",WAVECAL_RESAMPLED_OUT_FILENAME);
911 
912  sinfo_set_wcs_cal_image(res_ima,WAVECAL_RESAMPLED_OUT_FILENAME,
913  (double)x_cen,(double)x_cen,1.,(double)cpix,cwav,dis);
914 
915  cleanup:
916 
917  sinfo_free_image(&map_img);
918  sinfo_free_image(&res_ima);
919  sinfo_free_image(&wstk_img);
920  return cpl_error_get_code();
921 
922 
923 }
#define sinfo_msg_error(...)
Print an error message.
Definition: sinfo_msg.h:69