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