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

Generated on Wed Jan 17 08:33:43 2007 for SINFONI Pipeline Reference Manual by  doxygen 1.4.4