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

Generated on 3 Mar 2013 for SINFONI Pipeline Reference Manual by  doxygen 1.6.1