sinfo_new_slit_pos.c

00001 /*----------------------------------------------------------------------------
00002  
00003    File name    :       sinfo_new_slit_pos.c
00004    Author       :    A. Modigliani
00005    Created on   :    Sep 17, 2003
00006    Description  : 
00007 
00008  ---------------------------------------------------------------------------*/
00009 /*
00010  * This file is part of the ESO SINFONI Pipeline
00011  * Copyright (C) 2004,2005 European Southern Observatory
00012  *
00013  * This program is free software; you can redistribute it and/or modify
00014  * it under the terms of the GNU General Public License as published by
00015  * the Free Software Foundation; either version 2 of the License, or
00016  * (at your option) any later version.
00017  *
00018  * This program is distributed in the hope that it will be useful,
00019  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00020  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00021  * GNU General Public License for more details.
00022  *
00023  * You should have received a copy of the GNU General Public License
00024  * along with this program; if not, write to the Free Software
00025  * Foundation, 51 Franklin St, Fifth Floor, Boston, MA  02111-1307  USA
00026  */
00027 #ifdef HAVE_CONFIG_H
00028 #  include <config.h>
00029 #endif
00030 
00031 /*----------------------------------------------------------------------------
00032                                 Includes
00033  ---------------------------------------------------------------------------*/
00034 #include "sinfo_new_slit_pos.h"
00035 #include "sinfo_pro_save.h"
00036 #include "sinfo_pro_types.h"
00037 #include "sinfo_wavecal_ini_by_cpl.h"
00038 #include "sinfo_wcal_functions.h"
00039 #include "sinfo_wave_calibration.h"
00040 #include "sinfo_utilities.h"
00041 #include "sinfo_utils_wrappers.h"
00042 #include "sinfo_hidden.h"
00043 #include "sinfo_error.h"
00044 #include "sinfo_globals.h"
00045 
00046 /*----------------------------------------------------------------------------
00047                                 Defines
00048  ---------------------------------------------------------------------------*/
00049 
00050 /*----------------------------------------------------------------------------
00051                              Function Definitions
00052  ---------------------------------------------------------------------------*/
00060 /*----------------------------------------------------------------------------
00061    Function     :       sinfo_new_slit_pos()
00062    In           :       ini_file: file name of according .ini file
00063    Out          :       integer (0 if it worked, -1 if it doesn't) 
00064    Job          :
00065 
00066 
00067   Normal method:
00068 
00069   does the wavelength calibration and the fitting of the slitlet sinfo_edge 
00070   positions (ASCII file 32 x 2 values) if wished
00071   produces an array of the bcoefs and of the fit parameters if wished and a 
00072   wavelength calibration map input is an emission line frame and a line list
00073 
00074 
00075   o searching for lines by cross sinfo_correlation with a line list
00076   o Gaussian fitting of emission lines in each column->positions of the lines->
00077     resulting fit parameters can be stored in an ASCII file
00078   o Fitting of a polynomial to the line positions for each column
00079   o Smoothing: fitting of each polynomial coefficient by another polynomial
00080     across the whole frame -> resulting polynomial coefficients can be stored 
00081     in an ASCII file.
00082   o Wavelength calibration map (micron value for each frame pixel) can be
00083     produced by using these coefficients and a cross sinfo_correlation to the
00084     original frame
00085 
00086   o The slitlet sinfo_edge positions can be fitted:
00087     1) Automatically (not really stable) or by using guess sinfo_edge positions
00088     2) By using a Boltzmann or a linear slope function
00089 
00090   Slit method:
00091 
00092   does the wavelength calibration and the fitting of the slitlet sinfo_edge 
00093   positions (ASCII file 32 x 2 values) if wished produces a list of the fit 
00094   parameters and of the smoothed coefficients if wished and a wavelength 
00095   calibration map input is an emission line frame and a line list
00096 
00097   o Does the same as other method but smoothes the found polynomial 
00098     coefficients within each slitlet and not over the whole frame.
00099 
00100   o Produces always a wavelength calibration map and does not crosscorrelate.
00101 
00102  ---------------------------------------------------------------------------*/
00103 
00104 
00105 
00106 int sinfo_new_slit_pos (cpl_parameterlist* config, cpl_frameset* sof)
00107 {
00108     wave_config * cfg =NULL;
00109     int check = 0;
00110     int lx = 0;
00111     int ly = 0;
00112     int n_lines=0;
00113     int i = 0;
00114     int j = 0;
00115     int n = 0;
00116 
00117     int sum=0;
00118 
00119     int* n_found_lines=NULL;
00120     int* sum_pointer=NULL;
00121     int** row_clean=NULL;
00122 
00123     float a=0;
00124     float shift=0;
00125     float* wave=NULL;
00126     float* intens=NULL;
00127 
00128     float** acoefs=NULL;
00129     float** wavelength_clean=NULL;
00130 
00131     float** sinfo_slit_pos=NULL;
00132  
00133     cpl_image *  map=NULL ;
00134     cpl_image * im=NULL ;
00135 
00136     FitParams** par=NULL;
00137 
00138     cpl_table* tbl_wcal=NULL;
00139     cpl_table* tbl_spos=NULL;
00140 
00141     char* col_name=NULL;
00142     char* tbl_name=NULL;
00143 
00144     char* tbl_line_list_name=NULL;
00145     cpl_table* tbl_line_list = NULL;
00146     int* status=NULL;
00147  
00148     cpl_frameset* raw=NULL;
00149 
00150 
00151     cpl_table * tbl_fp =NULL;
00152     char* col=NULL;
00153     cpl_table* qclog_tbl=NULL;
00154     char* key_name=NULL;
00155     double fwhm_med=0;
00156     double fwhm_avg=0;
00157     double coef_med=0;
00158     double coef_avg=0;
00159     int trow=0;
00160     qc_wcal* qc=sinfo_qc_wcal_new();
00161     /*        -----------------------------------------------------------------
00162        1) parse the file names and parameters to the ns_config data 
00163           structure cfg
00164        -----------------------------------------------------------------
00165      */
00166 
00167     sinfo_msg("Parsing cpl input");
00168     cfg = sinfo_parse_cpl_input_wave(config,sof,&raw) ;
00169     
00170 
00171     cfg->nslitlets=32;
00172     cfg->calibIndicator=1;
00173     cfg->wavemapInd=0;
00174     cfg->slitposIndicator=1;
00175 
00176     if(cpl_error_get_code() != CPL_ERROR_NONE) {
00177       sinfo_msg_error((char* ) cpl_error_get_message());
00178       sinfo_qc_wcal_delete(&qc);
00179       return -1;
00180     }
00181    
00182     if (cfg == NULL)
00183     {
00184         sinfo_msg_error("could not parse cpl input!\n") ;
00185         sinfo_qc_wcal_delete(&qc);
00186         return -1 ;
00187     }
00188     if(sinfo_is_fits_file(cfg->inFrame) != 1) {
00189       sinfo_msg_error("Input file %s is not FITS",cfg->inFrame);
00190       sinfo_qc_wcal_delete(&qc);
00191       return -1;
00192     }
00193 
00194 
00195     if (cfg->slitposIndicator == 1 && cfg->estimateIndicator == 1) {
00196       if (sinfo_is_fits_file(cfg->slitposGuessName) != 1) {
00197         sinfo_msg_error("slitlet position guess list not given!");
00198         sinfo_qc_wcal_delete(&qc);
00199         return -1;
00200       }
00201     }
00202 
00203     if (cfg->calibIndicator == 0 && cfg->wavemapInd == 1) {
00204       if (sinfo_is_fits_file(cfg->coeffsName) != 1) {
00205         sinfo_msg_error("coefficients list not given!");
00206         sinfo_qc_wcal_delete(&qc);
00207         return -1;
00208       }
00209     }
00210 
00211 
00212     if (cfg->slitposIndicator == 1) {
00213       if (cfg->calibIndicator != 1 && cfg->estimateIndicator != 1) {
00214        
00215         if (sinfo_is_fits_file(cfg->paramsList) != 1) {
00216       sinfo_msg_error("parameter list not given!");
00217           sinfo_qc_wcal_delete(&qc);
00218       return -1;
00219     }
00220     
00221       }
00222     }
00223 
00224 /*---load the emission line frame---*/
00225     im = cpl_image_load(cfg->inFrame,CPL_TYPE_FLOAT,0,0);
00226     if (im == NULL) {
00227       sinfo_msg_error("could not load image\n");
00228       sinfo_qc_wcal_delete(&qc);
00229       return -1;
00230     }
00231 
00232 
00233     lx = cpl_image_get_size_x(im);
00234     ly = cpl_image_get_size_y(im);
00235 
00236 
00237 
00238 if (cfg->calibIndicator == 1 || cfg->wavemapInd == 1) {
00239     /*---open the line list and read the number of lines---*/
00240 
00241     if(cpl_error_get_code() != CPL_ERROR_NONE) {
00242       sinfo_msg_error((char* ) cpl_error_get_message());
00243       sinfo_qc_wcal_delete(&qc);
00244       return -1;
00245     }
00246 
00247     tbl_line_list_name=cfg->lineList;
00248     tbl_line_list = cpl_table_load(tbl_line_list_name,1,0);
00249     if(cpl_error_get_code() != CPL_ERROR_NONE) {
00250       sinfo_msg_error((char* ) cpl_error_get_message());
00251       sinfo_qc_wcal_delete(&qc);
00252       return -1;
00253     }
00254     n = cpl_table_get_nrow(tbl_line_list);
00255     n_lines = n;
00256     if(cpl_error_get_code() != CPL_ERROR_NONE) {
00257       sinfo_msg_error((char* ) cpl_error_get_message());
00258       sinfo_qc_wcal_delete(&qc);
00259       return -1;
00260     }
00261 
00262     /* THIS ORIGINATES A MEMORY LEAK 
00263     wave   = sinfo_new_floatarray (n);
00264     intens = sinfo_new_floatarray (n);
00265     if (wave == NULL || intens == NULL) {
00266       sinfo_msg_error("could not allocate memory for the line list values\n" );
00267       sinfo_qc_wcal_delete(&qc);
00268       return -1;
00269     }
00270     */
00271 
00272     wave   = cpl_table_get_data_float(tbl_line_list,"wave");
00273     if(cpl_error_get_code() != CPL_ERROR_NONE) {
00274       sinfo_msg_error((char* ) cpl_error_get_message());
00275       sinfo_qc_wcal_delete(&qc);
00276       return -1;
00277     }
00278 
00279     intens = cpl_table_get_data_float(tbl_line_list,"int");
00280     if(cpl_error_get_code() != CPL_ERROR_NONE) {
00281       sinfo_msg_error((char* ) cpl_error_get_message());
00282       sinfo_qc_wcal_delete(&qc);
00283       return -1;
00284     }
00285     
00286 }
00287 
00288 
00289 
00290 /*
00291  ----------------------------------------------------------------------
00292  ---------------------------FINDLINES----------------------------------
00293  ----------------------------------------------------------------------
00294  */
00295 
00296 
00297 /*if not yet done:
00298   do the wavelength calibration, that means: 
00299   find the dispersion relation and parameterize its coefficients
00300  */
00301 /*
00302  sinfo_msg("guessBeginWave=%g",cfg->guessBeginWavelength);
00303  sinfo_msg("guessDisp1=%g",cfg->guessDispersion1);
00304  sinfo_msg("guessDisp2=%g",cfg->guessDispersion2);
00305 */
00306 if (cfg->calibIndicator == 1 && cfg->wavemapInd == 0) {
00307    sinfo_msg("Findlines");
00308    acoefs  = sinfo_new_2Dfloatarray(cfg->nrDispCoefficients, lx);
00309    /*allocate memory*/
00310    n_found_lines    = sinfo_new_intarray(lx); 
00311    row_clean        = sinfo_new_2Dintarray(lx, n_lines);
00312    wavelength_clean = sinfo_new_2Dfloatarray(lx, n_lines);
00313    sum_pointer      = sinfo_new_intarray(1) ;
00314    /*find the emission lines in each image column*/
00315    sinfo_new_intarray_set_value(sum_pointer, 0, 0);
00316    check = sinfo_new_find_lines(im, wave, intens, n_lines, row_clean, 
00317                       wavelength_clean, cfg->guessBeginWavelength, 
00318              cfg->guessDispersion1, cfg->guessDispersion2,
00319                       cfg->mindiff, cfg->halfWidth, 
00320                       n_found_lines, cfg->sigma, sum_pointer );
00321    if (-1 == check) {
00322            sinfo_msg_error("sinfo_findLines failed!\n");
00323            sinfo_qc_wcal_delete(&qc);
00324            return -1;
00325    }
00326       
00327 
00328 /*---------------------------------------------------------------------
00329  *-----------------------WAVE_CALIB-------------------------------------
00330  *---------------------------------------------------------------------
00331  */
00332 
00333      
00334    sinfo_msg("Wave Calibration");
00335    sum = sinfo_new_intarray_get_value(sum_pointer,0);
00336    /* allocate memory for the fit parameters */
00337    par = sinfo_new_fit_params( sum );
00338    if (par == NULL) {
00339         sinfo_msg_error("sinfo_newFitParams failed!\n");
00340         sinfo_qc_wcal_delete(&qc);
00341         return -1;
00342    }
00343 
00344   /*
00345    fit each line, make a polynomial fit and fit the resulting fit 
00346    coefficients across the columns of the slitlet
00347    */
00348    sinfo_slit_pos = sinfo_new_2Dfloatarray(32,2);
00349 
00350    map = sinfo_new_spred_wave_cal(im, 
00351                  par, 
00352                  acoefs, 
00353                  cfg->nslitlets, 
00354                  row_clean, 
00355                  wavelength_clean, 
00356                  n_found_lines, 
00357                  cfg->guessDispersion1, 
00358                  cfg->halfWidth, 
00359                  cfg->minAmplitude, 
00360                  cfg->maxResidual, 
00361                  cfg->fwhm, 
00362                  cfg->nrDispCoefficients, 
00363                  cfg->nrCoefCoefficients, 
00364                  cfg->sigmaFactor, 
00365                  cfg->pixeldist, 
00366                  cfg->pixel_tolerance,
00367                  sinfo_slit_pos);
00368 
00369  
00370    if (map == NULL ) { 
00371           sinfo_msg_error("sinfo_wave_cal failed!\n");
00372           sinfo_qc_wcal_delete(&qc);
00373       return -1;
00374    }
00375    sinfo_msg("Check line positions");
00376   
00377    shift = sinfo_new_check_line_positions (im, acoefs, 
00378                                           cfg->nrDispCoefficients,
00379                                           cfg->guessDispersion1, par);
00380    if (FLAG == shift){ 
00381       sinfo_msg_error("checkForLinePositions failed!\n");
00382    }
00383 
00384 
00385     sinfo_det_ncounts(raw, cfg->qc_thresh_max,qc);
00386     qclog_tbl = sinfo_qclog_init();
00387     ck0_nomsg(sinfo_qclog_add_int(qclog_tbl,"QC WAVE ALL",
00388                   n_lines,"Number of found lines","%d"));
00389 
00390     ck0_nomsg(sinfo_qclog_add_int(qclog_tbl,"QC WAVE NPIXSAT",
00391                   qc->nsat,"Number of saturated pixels","%d"));
00392 
00393     ck0_nomsg(sinfo_qclog_add_int(qclog_tbl,"QC WAVE MAXFLUX",
00394                 qc->max_di,"Max int off-lamp subracted frm","%g"));
00395 
00396    if(-1 == sinfo_pro_save_ima(map,raw,sof,cfg->outName,
00397          PRO_WAVE_MAP,qclog_tbl,cpl_func,config)) {
00398          sinfo_msg_error("cannot save ima %s", cfg->outName);
00399     }
00400    sinfo_free_table(&qclog_tbl);
00401  
00402  
00403    /*
00404     #store the resulting polynomial fit coefficients in an 
00405      ASCII file if wished
00406     */
00407 
00408    if (cfg->writeCoeffsInd == 1) {
00409          col_name = (char*) cpl_calloc(MAX_NAME_SIZE,sizeof(char*));
00410          tbl_name = (char*) cpl_calloc(MAX_NAME_SIZE,sizeof(char*));
00411          tbl_wcal = cpl_table_new(lx);
00412          for (i=0; i< cfg->nrDispCoefficients; i++) {
00413              snprintf(col_name,MAX_NAME_SIZE-1,"%s%d","coeff",i);
00414              cpl_table_new_column(tbl_wcal,col_name, CPL_TYPE_DOUBLE);
00415      }
00416 
00417 
00418          qclog_tbl = sinfo_qclog_init();
00419          key_name  = cpl_calloc(FILE_NAME_SZ,sizeof(char));
00420          ck0_nomsg(sinfo_qclog_add_int(qclog_tbl,"QC WAVE ALL",
00421                n_lines,"Number of found lines","%d"));
00422 
00423 
00424 
00425          for (j=0; j< lx; j++) { 
00426         for (i=0; i< cfg->nrDispCoefficients; i++) {
00427                 snprintf(col_name,MAX_NAME_SIZE-1,"%s%d","coeff",i);
00428             a = sinfo_new_array2D_get_value(acoefs, i, j);
00429         /* fprintf(acoefs_file, "%15.13g ", a) ; */
00430                 cpl_table_set_double(tbl_wcal,col_name,j,a);
00431         }
00432 
00433 
00434      }
00435 
00436 
00437      for (i=0; i< cfg->nrDispCoefficients; i++) {
00438             snprintf(col_name,MAX_NAME_SIZE-1,"%s%d","coeff",i);
00439             coef_avg=cpl_table_get_column_mean(tbl_wcal,col_name);
00440             coef_med=cpl_table_get_column_median(tbl_wcal,col_name);
00441 
00442             trow=1+i;
00443             snprintf(key_name,MAX_NAME_SIZE-1,"%s%d%s","QC COEF",i," AVG");
00444             ck0_nomsg(sinfo_qclog_add_double(qclog_tbl,key_name,coef_avg,
00445                          "Average wavecal Coef","%g"));
00446 
00447             trow=1+i+cfg->nrDispCoefficients;
00448             snprintf(key_name,MAX_NAME_SIZE-1,"%s%d%s","QC COEF",i," MED");
00449             ck0_nomsg(sinfo_qclog_add_double(qclog_tbl,key_name,coef_med,
00450                          "Median wavecal Coef","%g"));
00451 
00452      }
00453 
00454      /*
00455          fclose(acoefs_file);
00456      */
00457          strcpy(tbl_name,cfg->coeffsName);
00458 
00459          if(-1 == sinfo_pro_save_tbl(tbl_wcal,raw,sof,tbl_name,
00460              PRO_WAVE_COEF_SLIT,qclog_tbl,cpl_func,config)) {
00461          sinfo_msg_error("cannot save tbl %s", tbl_name);
00462          }
00463          sinfo_free_table(&tbl_wcal);
00464          sinfo_free_table(&qclog_tbl);
00465          cpl_free(key_name);
00466 
00467          cpl_free(col_name);
00468          cpl_free(tbl_name);
00469 
00470    }
00471 
00472 
00473    /*
00474     #store the resulting Gaussian fit parameters in an ASCII file if wished
00475    */
00476    if (cfg->writeParInd == 1) {
00477 
00478 
00479       sinfo_new_dump_fit_params_to_ascii(par,WAVECAL_FIT_PARAMS_OUT_FILEASCII);
00480  
00481       if ( NULL == par )
00482       {
00483          sinfo_msg_error ("no fit parameters available!") ;
00484          sinfo_qc_wcal_delete(&qc);
00485          return -1;
00486       }
00487 
00488       if ( NULL == cfg->paramsList )
00489       {
00490          sinfo_msg_error ("no filename available!") ;
00491          sinfo_qc_wcal_delete(&qc);
00492          return -1;
00493       }
00494 
00495       tbl_fp = cpl_table_new(par[0] -> n_params);
00496       cpl_table_new_column(tbl_fp,"n_params", CPL_TYPE_INT);
00497       cpl_table_new_column(tbl_fp,"column", CPL_TYPE_INT);
00498       cpl_table_new_column(tbl_fp,"line", CPL_TYPE_INT);
00499       col = (char*) cpl_calloc(MAX_NAME_SIZE,sizeof(char*));
00500 
00501       for(j=0;j<4;j++) {
00502          snprintf(col,MAX_NAME_SIZE-1,"%s%d","fpar",j);
00503          cpl_table_new_column(tbl_fp,col, CPL_TYPE_DOUBLE);
00504          snprintf(col,MAX_NAME_SIZE-1,"%s%d","dpar",j);
00505          cpl_table_new_column(tbl_fp,col, CPL_TYPE_DOUBLE);
00506       }
00507 
00508 
00509 
00510     qclog_tbl = sinfo_qclog_init();
00511     ck0_nomsg(sinfo_qclog_add_int(qclog_tbl,"QC NLINES",n_lines,
00512                   "Number of Found lines","%d"));
00513 
00514       for ( i = 0 ; i < par[0] -> n_params ; i++ )
00515       {
00516          cpl_table_set_int(tbl_fp,"n_params",i,par[i]->n_params);
00517          cpl_table_set_int(tbl_fp,"column",i,par[i]->column);
00518          cpl_table_set_int(tbl_fp,"line",i,par[i]->line);
00519 
00520 
00521          for(j=0;j<4;j++) {
00522         snprintf(col,MAX_NAME_SIZE-1,"%s%d","fpar",j);
00523             cpl_table_set_double(tbl_fp,col,i,par[i]->fit_par[j]);
00524         snprintf(col,MAX_NAME_SIZE-1,"%s%d","dpar",j);
00525             cpl_table_set_double(tbl_fp,col,i,par[i]->derv_par[j]);
00526      }
00527       }
00528 
00529       fwhm_avg = cpl_table_get_column_mean(tbl_fp,"fpar1");
00530       fwhm_med = cpl_table_get_column_median(tbl_fp,"fpar1");
00531       ck0_nomsg(sinfo_qclog_add_double(qclog_tbl,"QC FWHM MED",fwhm_med,
00532                        "Median FWHM of found lines","%f"));
00533 
00534       ck0_nomsg(sinfo_qclog_add_double(qclog_tbl,"QC FWHM AVG",fwhm_avg,
00535                        "Average FWHM of found lines","%f"));
00536 
00537 
00538       if(-1 == sinfo_pro_save_tbl(tbl_fp,raw,sof,cfg->paramsList,
00539          PRO_WAVE_PAR_LIST,qclog_tbl,cpl_func,config)) {
00540          sinfo_msg_error("cannot save tbl %s", cfg->paramsList);
00541       }
00542       sinfo_free_table(&qclog_tbl);
00543 
00544       sinfo_free_table(&tbl_fp) ;
00545       cpl_free(col);
00546 
00547    }
00548    /* free memory */
00549    sinfo_new_destroy_2Dfloatarray ( &wavelength_clean, lx );
00550    sinfo_new_destroy_2Dintarray (&row_clean, lx);
00551    sinfo_new_destroy_intarray(&n_found_lines );
00552    sinfo_new_destroy_intarray(&sum_pointer );
00553    sinfo_new_destroy_2Dfloatarray ( &acoefs, cfg->nrDispCoefficients );
00554 
00555 /*----------------------------------------------------------------------
00556  *-------------------WAVEMAP--------------------------------------------
00557  *----------------------------------------------------------------------
00558  */
00559 
00560 /*
00561 #---now do the cross sinfo_correlation and produce a wavelength map---
00562  */
00563 } else if (cfg->wavemapInd == 1 && cfg->calibIndicator == 0) { 
00564   sinfo_msg("Wavemap");
00565   acoefs = sinfo_new_2Dfloatarray ( cfg->nrDispCoefficients, lx);
00566    /* #read the parameterized dispersion relation */
00567 
00568    tbl_name = (char*) cpl_calloc(MAX_NAME_SIZE,sizeof(char*));
00569    col_name = (char*) cpl_calloc(MAX_NAME_SIZE,sizeof(char*));
00570    strcpy(tbl_name,cfg->coeffsName);
00571    tbl_wcal = cpl_table_load(tbl_name,1,0);
00572     if(cpl_error_get_code() != CPL_ERROR_NONE) {
00573       sinfo_msg("cannot load table %s",tbl_name);
00574       sinfo_msg_error((char* ) cpl_error_get_message());
00575       sinfo_qc_wcal_delete(&qc);
00576       return -1;
00577     }
00578    for (i =0; i < lx; i++) {
00579       for (j = 0; j< cfg->nrDispCoefficients; j++) {
00580             snprintf(col_name,MAX_NAME_SIZE-1,"%s%d","coeff",j);
00581             acoefs[j][i]=cpl_table_get_double(tbl_wcal,col_name,i,status);
00582       }
00583    }
00584     if(cpl_error_get_code() != CPL_ERROR_NONE) {
00585       sinfo_msg("cannot read table %s",tbl_name);
00586       sinfo_msg_error((char* ) cpl_error_get_message());
00587       sinfo_qc_wcal_delete(&qc);
00588       return -1;
00589     }
00590     cpl_free(col_name);
00591     cpl_free(tbl_name);
00592     sinfo_free_table(&tbl_wcal);
00593 
00594     map = sinfo_new_create_shifted_slit_wavemap2 ( im, 
00595                                           acoefs, 
00596                                           cfg->nrDispCoefficients,
00597                                           wave, 
00598                                           intens, 
00599                                           n_lines, 
00600                                           cfg->magFactor, 
00601                       cfg->guessDispersion1, 
00602                                           cfg->pixeldist );
00603    if (map == NULL) {
00604            sinfo_msg_error("sinfo_createShiftedSlitWavemap2 failed!\n");
00605            sinfo_qc_wcal_delete(&qc);
00606            return -1;
00607    }
00608 
00609    par = sinfo_new_fit_params(15*n_lines);
00610    sinfo_msg("Check shifts");
00611 
00612    shift = sinfo_new_check_correlated_line_positions ( im, acoefs, 
00613                                            cfg->nrDispCoefficients, 
00614                                            wave, 
00615                                            intens, 
00616                                            n_lines, 
00617                                            cfg->fwhm, 
00618                                            cfg->halfWidth, 
00619                                            cfg->minAmplitude, 
00620                                            cfg->guessDispersion1, 
00621                                            par );
00622 
00623 
00624    if (FLAG == shift){
00625       sinfo_msg_error("sinfo_checkCorrelatedLinePositions failed!\n");
00626    }
00627 
00628 
00629     sinfo_det_ncounts(raw, cfg->qc_thresh_max,qc);
00630     qclog_tbl = sinfo_qclog_init();
00631     ck0_nomsg(sinfo_qclog_add_int(qclog_tbl,"QC NLINES",n_lines,
00632                   "Number of found lines","%d"));
00633 
00634 
00635 
00636     fwhm_avg = cpl_table_get_column_mean(tbl_fp,"fpar1");
00637     fwhm_med = cpl_table_get_column_median(tbl_fp,"fpar1");
00638 
00639     ck0_nomsg(sinfo_qclog_add_double(qclog_tbl,"QC FWHM MED",fwhm_med,
00640                      "Median FWHM of found lines","%f"));
00641     ck0_nomsg(sinfo_qclog_add_double(qclog_tbl,"QC FWHM AVG",fwhm_avg,
00642                      "Average FWHM of found lines","%f"));
00643 
00644 
00645     if(-1 == sinfo_pro_save_ima(map,raw,sof,cfg->outName,
00646          PRO_WAVE_MAP,qclog_tbl,cpl_func,config)) {
00647          sinfo_msg_error("cannot save ima %s", cfg->outName);
00648     }
00649     sinfo_free_table(&qclog_tbl);
00650 
00651 
00652    /* # ---free memory--- */
00653    sinfo_new_destroy_2Dfloatarray ( &acoefs, cfg->nrDispCoefficients );
00654    /* To fix a memory bug we comment the following. But check! */
00655 
00656 } else if (cfg->wavemapInd == 1 && cfg->calibIndicator == 1) {
00657    sinfo_msg_error("give either wavemapIndicator = yes and calibIndicator = no \
00658                 or wavemapIndicator = no and calibIndicator = yes") ;
00659    sinfo_qc_wcal_delete(&qc);
00660    return -1;
00661 }
00662 
00663 
00664 /*-------------------------------------------------------------------
00665  *-------------------SLITFITS----------------------------------------
00666  *-------------------------------------------------------------------
00667  #--fit the slitlet sinfo_edge positions if desired--
00668  */
00669 if (cfg->slitposIndicator == 1) {
00670   sinfo_msg("fit the slitlet sinfo_edge positions");
00671 
00672 
00673     /* #store the resulting sitlet positions in an TFITS table */
00674    tbl_name = (char*) cpl_calloc(MAX_NAME_SIZE,sizeof(char*));
00675    tbl_spos = cpl_table_new(32);
00676    cpl_table_new_column(tbl_spos,"pos1", CPL_TYPE_DOUBLE);
00677    cpl_table_new_column(tbl_spos,"pos2", CPL_TYPE_DOUBLE);
00678    cpl_table_set_column_format(tbl_spos,"pos1", "15.9f");
00679    cpl_table_set_column_format(tbl_spos,"pos2", "15.9f");
00680 
00681     for (i =0; i< 32; i++) {
00682      /*
00683      fprintf( slitpos_file, "%15.9f %15.9f \n",
00684       sinfo_new_array2D_get_value(sinfo_slit_pos,i,0),
00685                         sinfo_new_array2D_get_value(sinfo_slit_pos,i,1));
00686      */
00687       cpl_table_set_double(tbl_spos,"pos1",i,
00688                            sinfo_new_array2D_get_value(sinfo_slit_pos,i,0));
00689       cpl_table_set_double(tbl_spos,"pos2",i,
00690                            sinfo_new_array2D_get_value(sinfo_slit_pos,i,1));
00691      
00692    }
00693     /* strcpy(tbl_name,cfg->slitposName); */
00694     strcpy(tbl_name,"out_guess_slit_pos.fits");
00695     if(-1 == sinfo_pro_save_tbl(tbl_spos,raw,sof,tbl_name,
00696          PRO_SLIT_POS,NULL,cpl_func,config)) {
00697          sinfo_msg_error("cannot save tbl %s", tbl_name);
00698     }
00699     sinfo_free_table(&tbl_spos);
00700     cpl_free(tbl_name);
00701    /*# free memory*/
00702    sinfo_new_destroy_2Dfloatarray ( &sinfo_slit_pos, 32 );
00703    
00704    
00705 }
00706 
00707 /* #-----free the rest memory--*/
00708 if ( (cfg->slitposIndicator == 1 && cfg->estimateIndicator != 1) || 
00709      (cfg->calibIndicator == 1)  || (cfg->wavemapInd == 1) ){
00710      sinfo_new_destroy_fit_params(&par);
00711 }
00712 sinfo_free_image( &im );
00713 sinfo_free_image( &map );
00714 sinfo_wavecal_free(&cfg);
00715 sinfo_qc_wcal_delete(&qc);
00716 
00717  return 0;
00718 
00719  cleanup:
00720  return -1;
00721 
00722 
00723 
00724 }
00725 

Generated on 8 Mar 2011 for SINFONI Pipeline Reference Manual by  doxygen 1.6.1