sinfo_new_find_distortions.c

00001 /*
00002  * This file is part of the ESO SINFONI Pipeline
00003  * Copyright (C) 2004,2005 European Southern Observatory
00004  *
00005  * This program is free software; you can redistribute it and/or modify
00006  * it under the terms of the GNU General Public License as published by
00007  * the Free Software Foundation; either version 2 of the License, or
00008  * (at your option) any later version.
00009  *
00010  * This program is distributed in the hope that it will be useful,
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  * GNU General Public License for more details.
00014  *
00015  * You should have received a copy of the GNU General Public License
00016  * along with this program; if not, write to the Free Software
00017  * Foundation, 51 Franklin St, Fifth Floor, Boston, MA  02111-1307  USA
00018  */
00019 /*----------------------------------------------------------------------------
00020 
00021    File name    :       sinfo_find_distortions.c
00022    Author       :    A. Modigliani
00023    Created on   :    Aug 12, 2004
00024    Description  :
00025 
00026  ---------------------------------------------------------------------------*/
00027 #ifdef HAVE_CONFIG_H
00028 #  include <config.h>
00029 #endif
00030 
00031 /*----------------------------------------------------------------------------
00032                                 Includes
00033  ---------------------------------------------------------------------------*/
00034 
00035 #include "sinfo_new_find_distortions.h"
00036 #include "sinfo_pro_save.h"
00037 #include "sinfo_pro_types.h"
00038 #include "sinfo_finddist_ini_by_cpl.h"
00039 #include "sinfo_wave_calibration.h"
00040 #include "sinfo_cube_construct.h"
00041 #include "sinfo_absolute.h"
00042 #include "sinfo_distortion.h"
00043 #include "sinfo_utilities.h"
00044 #include "sinfo_recipes.h"
00045 #include "sinfo_utils_wrappers.h"
00046 #include "sinfo_error.h"
00047 #include "sinfo_globals.h"
00048 
00049 /*----------------------------------------------------------------------------
00050                                 Defines
00051  ---------------------------------------------------------------------------*/
00052 #define SINFO_ARC_SATURATION                 100000
00053 #define SINFO_ARC_MAX_WIDTH                  64
00054 
00055 
00064 /*----------------------------------------------------------------------------
00065                              Function Definitions
00066  ---------------------------------------------------------------------------*/
00067 
00068 /*----------------------------------------------------------------------------
00069    Function     :       sinfo_find_distortions()
00070    In           :
00071    Out          :
00072    Job          :
00073 
00074  ---------------------------------------------------------------------------*/
00075 static double
00076 new_compute_shift(double x,
00077           double y,
00078           double c00,
00079           double c10,
00080           double c01,
00081           double c11,
00082           double c20,
00083           double c02,
00084           double c21,
00085           double c12,
00086           double c30,
00087           double c03);
00088 
00089 int
00090 sinfo_new_find_distortions(const char* plugin_id,
00091                            cpl_parameterlist* config,
00092                            cpl_frameset* sof,
00093                cpl_frameset* set_fibre_ns)
00094 {
00095 
00096   finddist_config * cfg=NULL ;
00097   cpl_image * imonind=NULL ;
00098   cpl_image * impoly=NULL ;
00099   cpl_image * im=NULL ;
00100   cpl_image *  map=NULL ;
00101   cpl_image *  mask=NULL ;
00102 
00103   int*            degx=NULL;
00104   int*            degy=NULL;
00105   int* n_found_lines=NULL;
00106   int* sum_pointer=NULL;
00107   int** row_clean=NULL;
00108   int pdensity=0;
00109 
00110 
00111   int x_l=SIZEX/4*1;
00112   int x_u=SIZEX/4*3;
00113   int y_l=SIZEY/4*1;
00114   int y_u=SIZEY/4*3;
00115   int x_c=SIZEX/4*2;
00116   int y_c=SIZEY/4*2;
00117   const int pdx=4;
00118   const int pdy=4;
00119   int check = 0;
00120   int i = 0;
00121   int lx=0;
00122   int ly=0;
00123 
00124 
00125   int sum=0;
00126   int n_lines=0;
00127   int fit=0;
00128   int n=0;
00129   int j=0;
00130   int coef_pow[2];
00131   int arcs_window_size=0;
00132 
00133   float value=0;
00134   float newval=0;
00135   float total_dist=0;
00136   float shift=0;
00137 
00138   float* distances=NULL;
00139   float** wavelength_clean=NULL;
00140   float** slit_pos=NULL;
00141   float** acoefs=NULL;
00142   float* wave=NULL;
00143   float* intens=NULL;
00144   float* first =NULL;
00145 
00146   double*         coef=NULL;
00147 
00148   double xshift=0.;
00149   double arcs_thres_factor=0;
00150 
00151   double arcs_min_arclen_factor=0;
00152   double pcf[pdx][pdy];
00153 
00154 
00155   char tbl_name[FILE_NAME_SZ];
00156   char key_name[FILE_NAME_SZ];
00157 
00158   cpl_table* poly_tbl=NULL;
00159   cpl_table* tbl_spos=NULL;
00160   cpl_table* tbl_line_list=NULL;
00161 
00162   FitParams** par=NULL;
00163 
00164   cpl_frameset* stk=NULL;
00165   cpl_table* qclog_tbl=NULL;
00166 
00167   cpl_polynomial* distor_poly=NULL;
00168   cpl_apertures       *   arcs=NULL ;
00169   cpl_parameter* p=NULL;
00170   int smooth_rad=0;
00171 
00172 
00173   check_nomsg(p=cpl_parameterlist_find(config,"sinfoni.product.density"));
00174   check_nomsg(pdensity=cpl_parameter_get_int(p));
00175 
00176   /*
00177     parse the file names and parameters to the finddist_config
00178     data structure cfg
00179    */
00180   check_nomsg(stk=cpl_frameset_new());
00181   cknull(cfg = sinfo_parse_cpl_input_finddist(config,sof,&stk),
00182      "could not parse CPL input!");
00183 
00184   if(sinfo_is_fits_file (cfg->inFrame) != 1) {
00185     sinfo_msg_error("Input file %s is not FITS",cfg->inFrame);
00186     goto cleanup;
00187   }
00188 
00189   /* load the emission line frame--- */
00190   check(im = cpl_image_load(cfg->inFrame,CPL_TYPE_FLOAT,0,0),
00191     "could not load arc image");
00192 
00193   /* load the fake on - fake off frame--- */
00194   check(imonind = cpl_image_load(cfg->nsFrame,CPL_TYPE_FLOAT,0,0),
00195     "could not load on-off fake image");
00196 
00197   check(impoly  = cpl_image_load(cfg->nsFrame,CPL_TYPE_FLOAT,0,0),
00198     "could not load on-off fake image");
00199 
00200   /* load the fake on - fake off frame--- */
00201   check(mask = cpl_image_load(cfg->mask,CPL_TYPE_FLOAT,0,0),
00202     "could not load mask image");
00203 
00204   check(cpl_image_multiply (im,mask),
00205     "Failing to correct arc ima by bp mask");
00206 
00207   check(cpl_image_multiply (imonind,mask),
00208     "Failing to correct on-off fake ima by bp mask");
00209 
00210   check(cpl_image_multiply(impoly,mask),
00211     "Failing to correct on-off fake ima by bp mask");
00212 
00213   sinfo_free_image(&mask);
00214 
00215   check_nomsg(lx = cpl_image_get_size_x(im));
00216   check_nomsg(ly = cpl_image_get_size_y(im));
00217   /* TO BE CHENGED THE FOLLOWING INPUT */
00218   /* open the line list and read the number of lines */
00219   check(tbl_line_list = cpl_table_load(cfg->lineList,1,0),
00220     "problems loading table %s",cfg->lineList);
00221 
00222   check_nomsg(n_lines = cpl_table_get_nrow(tbl_line_list));
00223   check_nomsg(wave=cpl_table_get_data_float(tbl_line_list,"wave"));
00224   check_nomsg(intens = cpl_table_get_data_float(tbl_line_list,"int"));
00225 
00226   /*
00227        #---------------------------------------------------------------------
00228        #---------------------------FINDLINES---------------------------------
00229        #---------------------------------------------------------------------
00230   */
00231 
00232   sinfo_msg("Find Lines");
00233   /*
00234        do the wavelength calibration, that means:
00235        find the dispersion relation and parameterize its coefficients
00236   */
00237 
00238   acoefs  = sinfo_new_2Dfloatarray(cfg->nrDispCoefficients, lx);
00239   /* allocate memory */
00240   n_found_lines    = sinfo_new_intarray(lx);
00241   row_clean        = sinfo_new_2Dintarray(lx, n_lines);
00242   wavelength_clean = sinfo_new_2Dfloatarray(lx, n_lines);
00243   sum_pointer      = sinfo_new_intarray(1);
00244 
00245   /* find the emission lines in each image column */
00246   sinfo_new_intarray_set_value(sum_pointer, 0, 0);
00247 
00248   ck0(check = sinfo_new_find_lines(im,
00249                    wave,
00250                    intens,
00251                    n_lines,
00252                    row_clean,
00253                    wavelength_clean,
00254                    cfg->guessBeginWavelength,
00255                    cfg->guessDispersion1,
00256                    cfg->guessDispersion2,
00257                    cfg->mindiff,
00258                    cfg->halfWidth,
00259                    n_found_lines,
00260                    cfg->sigma,
00261                    sum_pointer ),
00262       "FindLines failed!");
00263 
00264   sinfo_free_table(&tbl_line_list);
00265 
00266   /*
00267    #-------------------------------------------------------------------------
00268    #---------------------------WAVECALIB-------------------------------------
00269    #-------------------------------------------------------------------------
00270     */
00271   sinfo_msg("Do wave calib");
00272   sum = sinfo_new_intarray_get_value(sum_pointer,0);
00273 
00274   /* #allocate memory for the fit parameters */
00275   cknull(par = sinfo_new_fit_params( sum ),
00276      "sinfo_new_fit_params failed!");
00277 
00278   /*
00279       fit each line, make a polynomial fit and fit the resulting fit
00280        coefficients across the columns of the slitlet
00281   */
00282   cknull(map = sinfo_new_wave_cal(im,
00283                              par,
00284                       acoefs,
00285                       cfg->nslitlets,
00286                       row_clean,
00287                       wavelength_clean,
00288                       n_found_lines,
00289                       cfg->guessDispersion1,
00290                       cfg->halfWidth,
00291                       cfg->minAmplitude,
00292                       cfg->maxResidual,
00293                       cfg->fwhm,
00294                       cfg->nrDispCoefficients,
00295                       cfg->nrCoefCoefficients,
00296                       cfg->sigmaFactor,
00297                       cfg->pixeldist,
00298                       cfg->pixel_tolerance),
00299      "sinfo_waveCal failed!");
00300   /* here mem leak */
00301 
00302   sinfo_msg("Check line positions");
00303   shift = sinfo_new_check_line_positions (im, acoefs,
00304                                           cfg->nrDispCoefficients,
00305                                           cfg->guessDispersion1,par);
00306 
00307   if (FLAG == shift) {
00308     sinfo_msg_error ( "checkForLinePositions failed!");
00309     goto cleanup;
00310   }
00311   sinfo_free_image(&map);
00312 
00313 
00314  /* free memory */
00315   sinfo_new_destroy_2Dfloatarray ( &wavelength_clean, lx );
00316   sinfo_new_destroy_2Dintarray (&row_clean, lx);
00317   sinfo_new_destroy_intarray(&n_found_lines );
00318   sinfo_new_destroy_intarray(&sum_pointer );
00319   sinfo_new_destroy_2Dfloatarray ( &acoefs, cfg->nrDispCoefficients );
00320   /*
00321   sinfo_new_destroy_array (& wave );
00322   sinfo_new_destroy_array (& intens );
00323   */
00324   /*
00325   #----------------------------------------------------------------------------
00326   #-------------------------SLITFITS-------------------------------------------
00327   #----------------------------------------------------------------------------
00328   #--fit the slitlet sinfo_edge positions if desired--
00329   */
00330 
00331   sinfo_msg("Find slit pos");
00332   /*allocate memory for the slitlet position array */
00333   slit_pos = sinfo_new_2Dfloatarray(32,2);
00334 
00335   fit = sinfo_new_fit_slits_boltz( im, par, slit_pos, cfg->boxLength,
00336                       cfg->yBox, cfg->diffTol );
00337 
00338 
00339   if (fit < 0) {
00340     sinfo_msg_error( "sinfo_fitSlitsBoltz failed" );
00341     goto cleanup;
00342   }
00343   sinfo_new_destroy_fit_params(&par);
00344 
00345   check_nomsg(tbl_spos=cpl_table_new(32));
00346   check_nomsg(cpl_table_new_column(tbl_spos,"pos1", CPL_TYPE_DOUBLE));
00347   check_nomsg(cpl_table_new_column(tbl_spos,"pos2", CPL_TYPE_DOUBLE));
00348   check_nomsg(cpl_table_set_column_format(tbl_spos,"pos1", "15.9f"));
00349   check_nomsg(cpl_table_set_column_format(tbl_spos,"pos2", "15.9f"));
00350 
00351   for (i =0; i< 32; i++) {
00352       check_nomsg(cpl_table_set_double(tbl_spos,"pos1",i,
00353                                 sinfo_new_array2D_get_value(slit_pos,i,0)));
00354       check_nomsg(cpl_table_set_double(tbl_spos,"pos2",i,
00355                                 sinfo_new_array2D_get_value(slit_pos,i,1)));
00356 
00357   }
00358 
00359   if(pdensity > 2) {
00360     strcpy(tbl_name,"out_slitlets_pos_predist.fits");
00361     ck0(sinfo_pro_save_tbl(tbl_spos,set_fibre_ns,sof,tbl_name,
00362                PRO_SLITLETS_POS_PREDIST,NULL,plugin_id,config),
00363     "cannot save tbl %s", tbl_name);
00364   }
00365   sinfo_free_table(&tbl_spos);
00366 
00367   /*
00368   #---------------------------------------------------------
00369   # do the north - south - test
00370   #---------------------------------------------------------
00371   */
00372   sinfo_msg("Do north south test");
00373   /*
00374   sinfo_msg("cfg->nslits =%d\n", cfg->nslits);
00375   sinfo_msg("cfg->nshalfWidth =%d\n", cfg->nshalfWidth);
00376   sinfo_msg("cfg->nsfwhm=%f\n",cfg->nsfwhm );
00377   sinfo_msg("cfg->minDiff=%f\n", cfg->minDiff);
00378   sinfo_msg("cfg->estimated_dist=%f\n", cfg->estimated_dist);
00379   sinfo_msg("cfg->devtol=%f\n", cfg->devtol);
00380   sinfo_msg("cfg->loPos=%d\n", cfg->loPos);
00381   sinfo_msg("cfg->hiPos=%d\n",cfg->hiPos);
00382   */
00383   cknull(distances = sinfo_north_south_test(imonind,
00384                       cfg->nslits,
00385                       cfg->nshalfWidth,
00386                       cfg->nsfwhm ,
00387                       cfg->minDiff,
00388                       cfg->estimated_dist,
00389                       cfg->devtol,
00390                       cfg->loPos,
00391                       cfg->hiPos),
00392      "north_south_test failed");
00393 
00394   sinfo_free_image(&imonind);
00395 
00396   /*
00397   #---------------------------------------------------------
00398   # get firstcol
00399   #---------------------------------------------------------
00400   */
00401   sinfo_msg("get first col");
00402   first = sinfo_new_floatarray(61);
00403   total_dist=0;
00404   n=0;
00405 
00406   for (i=0; i< 31; i++) {
00407     total_dist=total_dist+sinfo_new_array_get_value(distances,i);
00408 
00409     for (j=0; j< 2; j++) {
00410       if (j == 0) {
00411         if (i != 30) {
00412       newval = sinfo_new_array2D_get_value(slit_pos,i+1,j) - total_dist;
00413       sinfo_new_array_set_value(first, newval, n);
00414       n = n+1;
00415         }
00416       }
00417       if (j == 1) {
00418         newval = sinfo_new_array2D_get_value(slit_pos,i,j) - total_dist;
00419         sinfo_new_array_set_value(first, newval, n);
00420         n = n+1;
00421       }
00422     }
00423   }
00424   value = sinfo_new_f_median(first,61);
00425   sinfo_msg("Firstcol =%f", value);
00426   sinfo_new_destroy_array(&first);
00427   sinfo_new_destroy_array(&distances);
00428   sinfo_new_destroy_2Dfloatarray ( &slit_pos, 32 );
00429 
00430   /*
00431   #---------------------------------------------------------
00432   # Determine distortions
00433   #---------------------------------------------------------
00434   */
00435   sinfo_msg("Determine distortions");
00436   degx=cpl_calloc(8,sizeof(int));
00437   degy=cpl_calloc(8,sizeof(int));
00438   coef=cpl_calloc(8,sizeof(double));
00439 
00440   check_nomsg(p=cpl_parameterlist_find(config,
00441                 "sinfoni.distortion.arcs_thresh_factor"));
00442   check_nomsg(arcs_thres_factor=cpl_parameter_get_double(p));
00443 
00444   check_nomsg(p=cpl_parameterlist_find(config,
00445                 "sinfoni.distortion.arcs_min_arclen_factor"));
00446   check_nomsg(arcs_min_arclen_factor=cpl_parameter_get_double(p));
00447 
00448   check_nomsg(p=cpl_parameterlist_find(config,
00449                 "sinfoni.distortion.arcs_window_size"));
00450   check_nomsg(arcs_window_size=cpl_parameter_get_int(p));
00451 
00452   check_nomsg(p=cpl_parameterlist_find(config,
00453                 "sinfoni.distortion.smooth_rad"));
00454   check_nomsg(smooth_rad=cpl_parameter_get_int(p));
00455 
00456 
00457   cknull(distor_poly=sinfo_distortion_estimate_new(impoly,
00458                            0,
00459                            0,
00460                                                  cpl_image_get_size_x(impoly),
00461                                                  cpl_image_get_size_y(impoly),
00462                                                    FALSE,
00463                                                    SINFO_ARC_SATURATION,
00464                                                    SINFO_ARC_MAX_WIDTH,
00465                                                    arcs_thres_factor,
00466                                                    arcs_min_arclen_factor,
00467                                                    arcs_window_size,
00468                                                    smooth_rad,
00469                                                    3,
00470                                                    (double)value,
00471                                                    &arcs),
00472      "cannot estimage distortion") ;
00473   /*AMo: additional mem leaks */
00474 
00475   sinfo_free_apertures(&arcs);
00476   coef_pow[0]=0;
00477   coef_pow[1]=0;
00478   check_nomsg(pcf[0][0]=cpl_polynomial_get_coeff(distor_poly,coef_pow));
00479   sinfo_msg("Polynomial fit results: coeff[%d][%d]=%g",0,0,pcf[0][0]);
00480   /*
00481   pcf[0][0]+=value;
00482   */
00483   sinfo_msg("Polynomial fit results: coeff[%d][%d]=%g",0,0,pcf[0][0]);
00484   check_nomsg(cpl_polynomial_set_coeff(distor_poly,coef_pow,pcf[0][0]));
00485 
00486   /* up to here ok */
00487   /* To check mem leaks */
00488   sinfo_free_image(&impoly);
00489 
00490   //sinfo_msg("Polynomial fit results: deg=%d",
00491   //      cpl_polynomial_get_degree(distor_poly));
00492 
00493 
00494   coef_pow[0]=0;
00495   coef_pow[1]=0;
00496   check_nomsg(pcf[0][0]=cpl_polynomial_get_coeff(distor_poly,coef_pow));
00497   //sinfo_msg("Polynomial fit results: coeff[%d][%d]=%g",0,0,pcf[0][0]);
00498 
00499   coef_pow[0]=1;
00500   coef_pow[1]=0;
00501   check_nomsg(pcf[1][0]=cpl_polynomial_get_coeff(distor_poly,coef_pow));
00502   //sinfo_msg("Polynomial fit results: coeff[%d][%d]=%g",1,0,pcf[1][0]);
00503 
00504   coef_pow[0]=0;
00505   coef_pow[1]=1;
00506   check_nomsg(pcf[0][1]=cpl_polynomial_get_coeff(distor_poly,coef_pow));
00507   //sinfo_msg("Polynomial fit results: coeff[%d][%d]=%g",0,1,pcf[0][1]);
00508 
00509   coef_pow[0]=1;
00510   coef_pow[1]=1;
00511   check_nomsg(pcf[1][1]=cpl_polynomial_get_coeff(distor_poly,coef_pow));
00512   //sinfo_msg("Polynomial fit results: coeff[%d][%d]=%g",1,1,pcf[1][1]);
00513 
00514   coef_pow[0]=2;
00515   coef_pow[1]=0;
00516   check_nomsg(pcf[2][0]=cpl_polynomial_get_coeff(distor_poly,coef_pow));
00517   //sinfo_msg("Polynomial fit results: coeff[%d][%d]=%g",2,0,pcf[2][0]);
00518 
00519   coef_pow[0]=0;
00520   coef_pow[1]=2;
00521   check_nomsg(pcf[0][2]=cpl_polynomial_get_coeff(distor_poly,coef_pow));
00522   //sinfo_msg("Polynomial fit results: coeff[%d][%d]=%g",0,2,pcf[0][2]);
00523 
00524   coef_pow[0]=2;
00525   coef_pow[1]=1;
00526   check_nomsg(pcf[2][1]=cpl_polynomial_get_coeff(distor_poly,coef_pow));
00527   //sinfo_msg("Polynomial fit results: coeff[%d][%d]=%g",2,1,pcf[2][1]);
00528 
00529   coef_pow[0]=1;
00530   coef_pow[1]=2;
00531   check_nomsg(pcf[1][2]=cpl_polynomial_get_coeff(distor_poly,coef_pow));
00532   //sinfo_msg("Polynomial fit results: coeff[%d][%d]=%g",1,2,pcf[1][2]);
00533 
00534   coef_pow[0]=3;
00535   coef_pow[1]=0;
00536   check_nomsg(pcf[3][0]=cpl_polynomial_get_coeff(distor_poly,coef_pow));
00537   //sinfo_msg("Polynomial fit results: coeff[%d][%d]=%g",3,0,pcf[3][0]);
00538 
00539   coef_pow[0]=0;
00540   coef_pow[1]=3;
00541   check_nomsg(pcf[0][3]=cpl_polynomial_get_coeff(distor_poly,coef_pow));
00542   //sinfo_msg("Polynomial fit results: coeff[%d][%d]=%g",0,3,pcf[0][3]);
00543 
00544   sinfo_msg("Save results");
00545 
00546   check_nomsg(poly_tbl=cpl_table_new(10));
00547   check_nomsg(cpl_table_new_column(poly_tbl,"degx", CPL_TYPE_INT));
00548   check_nomsg(cpl_table_new_column(poly_tbl,"degy", CPL_TYPE_INT));
00549   check_nomsg(cpl_table_new_column(poly_tbl,"coeff", CPL_TYPE_DOUBLE));
00550   check_nomsg(cpl_table_set_int(poly_tbl,"degx",0,0));
00551   check_nomsg(cpl_table_set_int(poly_tbl,"degx",1,1));
00552   check_nomsg(cpl_table_set_int(poly_tbl,"degx",2,0));
00553   check_nomsg(cpl_table_set_int(poly_tbl,"degx",3,1));
00554   check_nomsg(cpl_table_set_int(poly_tbl,"degx",4,2));
00555   check_nomsg(cpl_table_set_int(poly_tbl,"degx",5,0));
00556   check_nomsg(cpl_table_set_int(poly_tbl,"degx",6,2));
00557   check_nomsg(cpl_table_set_int(poly_tbl,"degx",7,1));
00558   check_nomsg(cpl_table_set_int(poly_tbl,"degx",8,3));
00559   check_nomsg(cpl_table_set_int(poly_tbl,"degx",9,0));
00560 
00561   check_nomsg(cpl_table_set_int(poly_tbl,"degy",0,0));
00562   check_nomsg(cpl_table_set_int(poly_tbl,"degy",1,0));
00563   check_nomsg(cpl_table_set_int(poly_tbl,"degy",2,1));
00564   check_nomsg(cpl_table_set_int(poly_tbl,"degy",3,1));
00565   check_nomsg(cpl_table_set_int(poly_tbl,"degy",4,0));
00566   check_nomsg(cpl_table_set_int(poly_tbl,"degy",5,2));
00567   check_nomsg(cpl_table_set_int(poly_tbl,"degy",6,1));
00568   check_nomsg(cpl_table_set_int(poly_tbl,"degy",7,2));
00569   check_nomsg(cpl_table_set_int(poly_tbl,"degy",8,0));
00570   check_nomsg(cpl_table_set_int(poly_tbl,"degy",9,3));
00571 
00572   check_nomsg(cpl_table_set_double(poly_tbl,"coeff",0,pcf[0][0]));
00573   check_nomsg(cpl_table_set_double(poly_tbl,"coeff",1,pcf[1][0]));
00574   check_nomsg(cpl_table_set_double(poly_tbl,"coeff",2,pcf[0][1]));
00575   check_nomsg(cpl_table_set_double(poly_tbl,"coeff",3,pcf[1][1]));
00576   check_nomsg(cpl_table_set_double(poly_tbl,"coeff",4,pcf[2][0]));
00577   check_nomsg(cpl_table_set_double(poly_tbl,"coeff",5,pcf[0][2]));
00578   check_nomsg(cpl_table_set_double(poly_tbl,"coeff",6,pcf[2][1]));
00579   check_nomsg(cpl_table_set_double(poly_tbl,"coeff",7,pcf[1][2]));
00580   check_nomsg(cpl_table_set_double(poly_tbl,"coeff",8,pcf[3][0]));
00581   check_nomsg(cpl_table_set_double(poly_tbl,"coeff",9,pcf[0][3]));
00582 
00583   //sinfo_msg("Polynomial distortion coefficients \n");
00584   //sinfo_msg("c= %g   %g  %g  %g  %g  %g  %g  %g %g %g\n",
00585   //       pcf[0][0],pcf[1][0],pcf[0][1],pcf[1][1],
00586   //           pcf[2][0],pcf[0][2],pcf[2][1],pcf[1][2],pcf[3][0],pcf[0][3]);
00587 
00588   /* QC LOG */
00589   check_nomsg(qclog_tbl = sinfo_qclog_init());
00590   snprintf(key_name,MAX_NAME_SIZE-1,"%s%d%d","QC COEFF",0,0);
00591   ck0_nomsg(sinfo_qclog_add_double(qclog_tbl,key_name,pcf[0][0],
00592              "Polynomial distortion coefficient","%g"));
00593 
00594   snprintf(key_name,MAX_NAME_SIZE-1,"%s%d%d","QC COEFF",1,0);
00595   ck0_nomsg(sinfo_qclog_add_double(qclog_tbl,key_name,pcf[1][0],
00596              "Polynomial distortion coefficient","%g"));
00597 
00598   snprintf(key_name,MAX_NAME_SIZE-1,"%s%d%d","QC COEFF",0,1);
00599   ck0_nomsg(sinfo_qclog_add_double(qclog_tbl,key_name,pcf[0][1],
00600              "Polynomial distortion coefficient","%g"));
00601 
00602   snprintf(key_name,MAX_NAME_SIZE-1,"%s%d%d","QC COEFF",1,1);
00603   ck0_nomsg(sinfo_qclog_add_double(qclog_tbl,key_name,pcf[1][1],
00604              "Polynomial distortion coefficient","%g"));
00605 
00606   snprintf(key_name,MAX_NAME_SIZE-1,"%s%d%d","QC COEFF",2,0);
00607   ck0_nomsg(sinfo_qclog_add_double(qclog_tbl,key_name,pcf[2][0],
00608              "Polynomial distortion coefficient","%g"));
00609 
00610   snprintf(key_name,MAX_NAME_SIZE-1,"%s%d%d","QC COEFF",0,2);
00611   ck0_nomsg(sinfo_qclog_add_double(qclog_tbl,key_name,pcf[0][2],
00612              "Polynomial distortion coefficient","%g"));
00613 
00614   snprintf(key_name,MAX_NAME_SIZE-1,"%s%d%d","QC COEFF",2,1);
00615   ck0_nomsg(sinfo_qclog_add_double(qclog_tbl,key_name,pcf[2][1],
00616              "Polynomial distortion coefficient","%g"));
00617 
00618   snprintf(key_name,MAX_NAME_SIZE-1,"%s%d%d","QC COEFF",1,2);
00619   ck0_nomsg(sinfo_qclog_add_double(qclog_tbl,key_name,pcf[1][2],
00620              "Polynomial distortion coefficient","%g"));
00621 
00622   snprintf(key_name,MAX_NAME_SIZE-1,"%s%d%d","QC COEFF",3,0);
00623   ck0_nomsg(sinfo_qclog_add_double(qclog_tbl,key_name,pcf[3][0],
00624              "Polynomial distortion coefficient","%g"));
00625 
00626   snprintf(key_name,MAX_NAME_SIZE-1,"%s%d%d","QC COEFF",0,3);
00627   ck0_nomsg(sinfo_qclog_add_double(qclog_tbl,key_name,pcf[0][3],
00628              "Polynomial distortion coefficient","%g"));
00629 
00630   snprintf(key_name,MAX_NAME_SIZE-1,"%s","QC OFFSET");
00631   ck0_nomsg(sinfo_qclog_add_double(qclog_tbl,key_name,value,
00632              "Polynomial distortion coefficient","%g"));
00633 
00634   xshift=new_compute_shift(x_c,y_c,pcf[0][0],pcf[1][0],pcf[0][1],
00635                                    pcf[1][1],pcf[2][0],pcf[0][2],
00636                                    pcf[2][1],pcf[1][2],pcf[3][0],pcf[0][3]);
00637 
00638   ck0_nomsg(sinfo_qclog_add_double(qclog_tbl,"QC XSHIFT CC",xshift,
00639                                    "X shift in x_c,y_c","%g"));
00640 
00641 
00642   xshift=new_compute_shift(x_l,y_l,pcf[0][0],pcf[1][0],pcf[0][1],
00643                                    pcf[1][1],pcf[2][0],pcf[0][2],
00644                                    pcf[2][1],pcf[1][2],pcf[3][0],pcf[0][3]);
00645 
00646   ck0_nomsg(sinfo_qclog_add_double(qclog_tbl,"QC XSHIFT LL",xshift,
00647                                    "X shift in x_l,y_l","%g"));
00648 
00649   xshift=new_compute_shift(x_l,y_u,pcf[0][0],pcf[1][0],pcf[0][1],
00650                                    pcf[1][1],pcf[2][0],pcf[0][2],
00651                                    pcf[2][1],pcf[1][2],pcf[3][0],pcf[0][3]);
00652 
00653   ck0_nomsg(sinfo_qclog_add_double(qclog_tbl,"QC XSHIFT UL",xshift,
00654                                    "X shift in x_l,y_u","%g"));
00655 
00656   xshift=new_compute_shift(x_u,y_u,pcf[0][0],pcf[1][0],pcf[0][1],
00657                                    pcf[1][1],pcf[2][0],pcf[0][2],
00658                                    pcf[2][1],pcf[1][2],pcf[3][0],pcf[0][3]);
00659 
00660   ck0_nomsg(sinfo_qclog_add_double(qclog_tbl,"QC XSHIFT UR",xshift,
00661                                    "X shift in x_u,y_u","%g"));
00662 
00663 
00664   xshift=new_compute_shift(x_u,y_l,pcf[0][0],pcf[1][0],pcf[0][1],
00665                                    pcf[1][1],pcf[2][0],pcf[0][2],
00666                                    pcf[2][1],pcf[1][2],pcf[3][0],pcf[0][3]);
00667 
00668   ck0_nomsg(sinfo_qclog_add_double(qclog_tbl,"QC XSHIFT LR",xshift,
00669                                    "X shift in x_u,y_l","%g"));
00670 
00671 
00672   ck0(sinfo_pro_save_tbl(poly_tbl,set_fibre_ns,sof,cfg->outName,
00673              PRO_DISTORTION,qclog_tbl,plugin_id,config),
00674       "cannot dump tbl %s", cfg->outName);
00675 
00676   sinfo_free_table(&poly_tbl);
00677   sinfo_free_table(&qclog_tbl);
00678   sinfo_free_polynomial(&distor_poly);
00679   sinfo_free_int(&degx);
00680   sinfo_free_int(&degy);
00681   sinfo_free_double(&coef);
00682   sinfo_free_image(&im);
00683   sinfo_free_frameset(&stk);
00684   sinfo_finddist_free (&cfg);
00685 
00686   return 0;
00687 
00688  cleanup:
00689   sinfo_free_table(&poly_tbl);
00690   sinfo_free_table(&qclog_tbl);
00691   sinfo_free_polynomial(&distor_poly);
00692   sinfo_free_int(&degx);
00693   sinfo_free_int(&degy);
00694   sinfo_free_double(&coef);
00695   sinfo_free_apertures(&arcs);
00696 
00697 
00698 
00699   /*if(wave != NULL) sinfo_new_destroy_array (& wave );*/
00700   /*if(intens != NULL) sinfo_new_destroy_array (& intens );*/
00701   if(first != NULL) sinfo_new_destroy_array(&first);
00702   sinfo_free_table(&tbl_spos);
00703   if(slit_pos != NULL) sinfo_new_destroy_2Dfloatarray (&slit_pos,32);
00704   if(distances != NULL) sinfo_new_destroy_array(&distances);
00705   if(par!=NULL) sinfo_new_destroy_fit_params(&par);
00706   if(n_found_lines != NULL) sinfo_new_destroy_intarray(&n_found_lines );
00707   if(row_clean != NULL) sinfo_new_destroy_2Dintarray(&row_clean, lx);
00708   if(wavelength_clean != NULL) {
00709       sinfo_new_destroy_2Dfloatarray(&wavelength_clean,lx);
00710   }
00711   if(sum_pointer != NULL) sinfo_new_destroy_intarray(&sum_pointer);
00712   if(acoefs != NULL) {
00713       sinfo_new_destroy_2Dfloatarray(&acoefs, cfg->nrDispCoefficients );
00714   }
00715   sinfo_free_table(&tbl_line_list);
00716   sinfo_free_image(&mask);
00717   sinfo_free_image(&impoly);
00718   sinfo_free_image(&imonind);
00719   sinfo_free_image(&map);
00720   sinfo_free_image(&im);
00721   sinfo_finddist_free (&cfg);
00722   sinfo_free_frameset(&stk);
00723   return -1;
00724 
00725 }
00726 
00727 
00728 static double
00729 new_compute_shift(double x,
00730               double y,
00731               double c00,
00732               double c10,
00733               double c01,
00734               double c11,
00735               double c20,
00736               double c02,
00737               double c21,
00738               double c12,
00739               double c30,
00740               double c03)
00741 {
00742 
00743   double x_shift=0;
00744   double shift=0;
00745   //sinfo_msg("c= %g   %g  %g  %g  %g  %g  %g  %g %g %g\n",
00746   //       c00,c10,c01,c11,
00747   //       c20,c02,c21,c12,c30,c03);
00748 
00749   shift=c00+c10*x+c01*y+
00750         c11*x*y+c20*x*x+c02*y*y+
00751         c21*x*x*y+c12*x*y*y+c30*x*x*x+c03*y*y*y;
00752   x_shift=x-shift;
00753   return x_shift;
00754 
00755 
00756 
00757 }

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