irplib_wlxcorr.c

00001 /* $Id: irplib_wlxcorr.c,v 1.58 2013/01/29 08:43:33 jtaylor Exp $
00002  *
00003  * This file is part of the IRPLIB package
00004  * Copyright (C) 2002,2003 European Southern Observatory
00005  *
00006  * This program is free software; you can redistribute it and/or modify
00007  * it under the terms of the GNU General Public License as published by
00008  * the Free Software Foundation; either version 2 of the License, or
00009  * (at your option) any later version.
00010  *
00011  * This program is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU General Public License
00017  * along with this program; if not, write to the Free Software
00018  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02111-1307  USA
00019  */
00020 
00021 /*
00022  * $Author: jtaylor $
00023  * $Date: 2013/01/29 08:43:33 $
00024  * $Revision: 1.58 $
00025  * $Name: HEAD $
00026  */
00027 
00028 #ifdef HAVE_CONFIG_H
00029 #include <config.h>
00030 #endif
00031 
00032 /*-----------------------------------------------------------------------------
00033                                 Includes
00034  -----------------------------------------------------------------------------*/
00035 
00036 #include <math.h>
00037 #include <string.h>
00038 
00039 #include <cpl.h>
00040 
00041 #include "irplib_wavecal_impl.h"
00042 
00043 #include "irplib_wlxcorr.h"
00044 
00045 /*----------------------------------------------------------------------------*/
00055 /*----------------------------------------------------------------------------*/
00056 
00057 /*-----------------------------------------------------------------------------
00058                            Defines
00059  -----------------------------------------------------------------------------*/
00060 
00061 /* TEMPORARY SUPPORT OF CPL 5.x */
00062 #ifndef CPL_SIZE_FORMAT
00063 #define CPL_SIZE_FORMAT "d"
00064 #define cpl_size int
00065 #endif
00066 /* END TEMPORARY SUPPORT OF CPL 5.x */
00067 
00068 #ifndef inline
00069 #define inline /* inline */
00070 #endif
00071 
00072 #define IRPLIB_MAX(A,B) ((A) > (B) ? (A) : (B))
00073 #define IRPLIB_MIN(A,B) ((A) < (B) ? (A) : (B))
00074 
00075 #define IRPLIB_PTR_SWAP(a,b)                                               \
00076     do { void * irplib_ptr_swap =(a);(a)=(b);(b)=irplib_ptr_swap; } while (0)
00077 
00078 /*-----------------------------------------------------------------------------
00079                            Private functions
00080  -----------------------------------------------------------------------------*/
00081 
00082 static void irplib_wlxcorr_estimate(cpl_vector *, cpl_vector *,
00083                                     const cpl_vector *,
00084                                     const cpl_bivector *,
00085                                     const cpl_vector *,
00086                                     const cpl_polynomial *,
00087                                     double, double);
00088 
00089 static int irplib_wlxcorr_signal_resample(cpl_vector *, const cpl_vector *, 
00090         const cpl_bivector *) ;
00091 static cpl_error_code cpl_vector_fill_lss_profile_symmetric(cpl_vector *,
00092                                                             double, double);
00093 static cpl_error_code irplib_wlcalib_fill_spectrum(cpl_vector *,
00094                                                 const cpl_bivector *,
00095                                                 const cpl_vector *,
00096                                                 const cpl_polynomial *, int);
00097 
00098 static cpl_boolean irplib_wlcalib_is_lines(const cpl_vector *,
00099                                         const cpl_polynomial *,
00100                                         int, double);
00101 
00105 /*----------------------------------------------------------------------------*/
00141 /*----------------------------------------------------------------------------*/
00142 cpl_polynomial * irplib_wlxcorr_best_poly(const cpl_vector     * spectrum,
00143                                           const cpl_bivector   * lines_catalog,
00144                                           int                    degree,
00145                                           const cpl_polynomial * guess_poly,
00146                                           const cpl_vector     * wl_error,
00147                                           int                    nsamples,
00148                                           double                 slitw,
00149                                           double                 fwhm,
00150                                           double               * xc,
00151                                           cpl_table           ** wlres,
00152                                           cpl_vector          ** xcorrs)
00153 {
00154     const int         spec_sz = cpl_vector_get_size(spectrum);
00155     const int         nfree   = cpl_vector_get_size(wl_error);
00156     int               ntests  = 1;
00157     cpl_vector      * model;
00158     cpl_vector      * vxc;
00159     cpl_vector      * init_pts_wl;
00160     cpl_matrix      * init_pts_x;
00161     cpl_vector      * pts_wl;
00162     cpl_vector      * vxcorrs;
00163     cpl_vector      * conv_kernel = NULL;    
00164     cpl_polynomial  * poly_sol;
00165     cpl_polynomial  * poly_candi;
00166     const double    * pwl_error = cpl_vector_get_data_const(wl_error); 
00167     const double    * dxc;
00168     cpl_size          degree_loc ;
00169     const cpl_boolean symsamp = CPL_TRUE; /* init_pts_x is symmetric */
00170     const cpl_boolean is_lines
00171         = irplib_wlcalib_is_lines(cpl_bivector_get_x_const(lines_catalog),
00172                                guess_poly, spec_sz, 1.0);
00173     int               i;
00174 
00175     /* FIXME: Need mode parameter for catalogue type (lines <=> profile) */
00176 
00177     /* In case of failure */
00178     if (wlres  != NULL) *wlres  = NULL;
00179     if (xcorrs != NULL) *xcorrs = NULL;
00180 
00181     /* Useful for knowing if resampling is used */
00182     cpl_msg_debug(cpl_func, "Checking %d^%d dispersion polynomials (slitw=%g, "
00183                   "fwhm=%g) against %d-point observed spectrum with%s "
00184                   "catalog resampling", nsamples, nfree, slitw, fwhm, spec_sz,
00185                   is_lines ? "out" : "");
00186 
00187     cpl_ensure(xc            != NULL, CPL_ERROR_NULL_INPUT,    NULL);
00188     *xc = -1.0;
00189     cpl_ensure(spectrum      != NULL,  CPL_ERROR_NULL_INPUT,    NULL);
00190     cpl_ensure(lines_catalog != NULL, CPL_ERROR_NULL_INPUT,    NULL);
00191     cpl_ensure(guess_poly    != NULL, CPL_ERROR_NULL_INPUT,    NULL);
00192     cpl_ensure(wl_error      != NULL, CPL_ERROR_NULL_INPUT,    NULL);
00193     cpl_ensure(nfree         >= 2,    CPL_ERROR_ILLEGAL_INPUT, NULL);
00194     cpl_ensure(nsamples      >  0,    CPL_ERROR_ILLEGAL_INPUT, NULL);
00195     /* FIXME: degree is redundant */
00196     cpl_ensure(1 + degree   == nfree, CPL_ERROR_ILLEGAL_INPUT, NULL);
00197 
00198     cpl_ensure(cpl_polynomial_get_dimension(guess_poly) == 1,
00199                CPL_ERROR_ILLEGAL_INPUT, NULL);
00200 
00201     if (nsamples > 1) {
00202         /* Search place must consist of more than one point */
00203         /* FIXME: The bounds should probably not be negative */
00204         for (i = 0; i < nfree; i++) {
00205             if (pwl_error[i] != 0.0) break;
00206         }
00207         cpl_ensure(i < nfree, CPL_ERROR_ILLEGAL_INPUT, NULL);
00208     }
00209  
00210     if (!is_lines) {
00211         /* Create the convolution kernel */
00212         conv_kernel = irplib_wlxcorr_convolve_create_kernel(slitw, fwhm);
00213         cpl_ensure(conv_kernel   != NULL, CPL_ERROR_ILLEGAL_INPUT, NULL);
00214     }
00215 
00216     /* Create initial test points */
00217     init_pts_x  = cpl_matrix_new(1, nfree);
00218     init_pts_wl = cpl_vector_new(nfree);
00219     pts_wl      = cpl_vector_new(nfree);
00220     for (i = 0; i < nfree; i++) {
00221         const double xpos  = spec_sz * i / (double)degree;
00222         const double wlpos = cpl_polynomial_eval_1d(guess_poly, xpos, NULL)
00223             - 0.5 * pwl_error[i];
00224 
00225         cpl_matrix_set(init_pts_x, 0, i, xpos);
00226         cpl_vector_set(init_pts_wl,   i, wlpos);
00227 
00228         ntests *= nsamples; /* Count number of tests */
00229 
00230     }
00231 
00232     vxcorrs = xcorrs != NULL ? cpl_vector_new(ntests) : NULL;
00233 
00234     poly_sol   = cpl_polynomial_new(1);
00235     poly_candi = cpl_polynomial_new(1);
00236     model = cpl_vector_new(spec_sz);
00237     vxc = cpl_vector_new(1);
00238     dxc = cpl_vector_get_data_const(vxc);
00239    
00240     /* Create the polynomial candidates and estimate them */
00241     for (i=0; i < ntests; i++) {
00242         int    idiv = i;
00243         int    deg;
00244 
00245         /* Update wavelength at one anchor point - and reset wavelengths
00246            to their default for any anchor point(s) at higher wavelengths */
00247         for (deg = degree; deg >= 0; deg--, idiv /= nsamples) {
00248             const int imod = idiv % nsamples;
00249             const double wlpos = cpl_vector_get(init_pts_wl, deg)
00250                                + imod * pwl_error[deg] / nsamples;
00251 
00252             /* FIXME: If wlpos causes pts_wl to be non-increasing, the
00253                solution will be non-physical with no need for evaluation.
00254                (*xc could be set to -1 in this case). */
00255             cpl_vector_set(pts_wl, deg, wlpos);
00256 
00257             if (imod > 0) break;
00258         }
00259 
00260         /* Generate */
00261         degree_loc = (cpl_size)degree ;
00262         cpl_polynomial_fit(poly_candi, init_pts_x, &symsamp, pts_wl,
00263                            NULL, CPL_FALSE, NULL, &degree_loc);
00264         /* *** Estimate *** */
00265         irplib_wlxcorr_estimate(vxc, model, spectrum, lines_catalog,
00266                                 conv_kernel, poly_candi, slitw, fwhm);
00267         if (vxcorrs != NULL) cpl_vector_set(vxcorrs, i, *dxc);
00268         if (*dxc > *xc) {
00269             /* Found a better solution */
00270             *xc = *dxc;
00271             IRPLIB_PTR_SWAP(poly_sol, poly_candi);
00272         }
00273     }
00274 
00275     cpl_vector_delete(model);
00276     cpl_vector_delete(vxc);
00277     cpl_vector_delete(conv_kernel);
00278     cpl_vector_delete(pts_wl);
00279     cpl_matrix_delete(init_pts_x);
00280     cpl_vector_delete(init_pts_wl);
00281     cpl_polynomial_delete(poly_candi);
00282 
00283 #ifdef CPL_WLCALIB_FAIL_ON_CONSTANT
00284     /* FIXME: */
00285     if (cpl_polynomial_get_degree(poly_sol) == 0) {
00286         cpl_polynomial_delete(poly_sol);
00287         cpl_vector_delete(vxcorrs);
00288         *xc = 0.0;
00289         cpl_error_set_message_macro(cpl_func, CPL_ERROR_ILLEGAL_OUTPUT,
00290                                     __FILE__, __LINE__, "Found a constant "
00291                                     "dispersion");
00292             cpl_errorstate_dump(prestate, CPL_FALSE, NULL);
00293         return NULL;
00294     }
00295 #endif
00296     
00297     if (wlres != NULL) {
00298         /* FIXME: A failure in the table creation is not considered a failure
00299            of the whole function call (although all outputs may be useless) */
00300 
00301         cpl_errorstate prestate = cpl_errorstate_get();
00302         /* Create the spc_table  */
00303         *wlres = irplib_wlxcorr_gen_spc_table(spectrum, lines_catalog, slitw,
00304                                               fwhm, guess_poly, poly_sol);
00305         if (*wlres == NULL) {
00306             cpl_polynomial_delete(poly_sol);
00307             cpl_vector_delete(vxcorrs);
00308             *xc = -1.0;
00309             cpl_error_set_message_macro(cpl_func, CPL_ERROR_ILLEGAL_OUTPUT,
00310                                         __FILE__, __LINE__, "Cannot generate "
00311                                         "infos table");
00312             /* cpl_errorstate_dump(prestate, CPL_FALSE, NULL); */
00313             cpl_errorstate_set(prestate);
00314             return NULL;
00315         }
00316     } 
00317     
00318     if (xcorrs != NULL) {
00319         *xcorrs = vxcorrs;
00320     } else {
00321         /* assert(vxcorrs == NULL); */
00322     }
00323 
00324     return poly_sol;
00325 }
00326 
00327 /*----------------------------------------------------------------------------*/
00345 /*----------------------------------------------------------------------------*/
00346 cpl_table * irplib_wlxcorr_gen_spc_table(
00347         const cpl_vector        *   spectrum,
00348         const cpl_bivector      *   lines_catalog,
00349         double                      slitw,
00350         double                      fwhm,
00351         const cpl_polynomial    *   guess_poly,
00352         const cpl_polynomial    *   corr_poly)
00353 {
00354 
00355     cpl_vector      *   conv_kernel = NULL;
00356     cpl_bivector    *   gen_init ;
00357     cpl_bivector    *   gen_corr ;
00358     cpl_table       *   spc_table ;
00359     const double    *   pgen ;
00360     const double        xtrunc = 0.5 * slitw + 5.0 * fwhm * CPL_MATH_SIG_FWHM;
00361     const int           spec_sz = cpl_vector_get_size(spectrum);
00362     const cpl_boolean   guess_resamp
00363         = !irplib_wlcalib_is_lines(cpl_bivector_get_x_const(lines_catalog),
00364                                 guess_poly, spec_sz, 1.0);
00365     const cpl_boolean   corr_resamp
00366         = !irplib_wlcalib_is_lines(cpl_bivector_get_x_const(lines_catalog),
00367                                 corr_poly, spec_sz, 1.0);
00368     cpl_error_code      error;
00369 
00370     cpl_msg_debug(cpl_func, "Tabel for guess dispersion polynomial (slitw=%g, "
00371                   "fwhm=%g) with %d-point observed spectrum with%s catalog re"
00372                   "sampling", slitw, fwhm, spec_sz, guess_resamp ? "out" : "");
00373     cpl_msg_debug(cpl_func, "Tabel for corr. dispersion polynomial (slitw=%g, "
00374                   "fwhm=%g) with %d-point observed spectrum with%s catalog re"
00375                   "sampling", slitw, fwhm, spec_sz, corr_resamp ? "out" : "");
00376 
00377     /* Test inputs */
00378     cpl_ensure(spectrum, CPL_ERROR_NULL_INPUT, NULL) ;
00379     cpl_ensure(lines_catalog, CPL_ERROR_NULL_INPUT, NULL) ;
00380     cpl_ensure(guess_poly, CPL_ERROR_NULL_INPUT, NULL) ;
00381     cpl_ensure(corr_poly, CPL_ERROR_NULL_INPUT, NULL) ;
00382 
00383     /* Create the convolution kernel */
00384     if (guess_resamp || corr_resamp) {
00385         conv_kernel = irplib_wlxcorr_convolve_create_kernel(slitw, fwhm);
00386 
00387         if (conv_kernel == NULL) {
00388             cpl_error_set_message_macro(cpl_func, CPL_ERROR_ILLEGAL_INPUT,
00389                                         __FILE__, __LINE__, "Cannot create "
00390                                         "convolution kernel") ;
00391             return NULL ;
00392         }
00393     }
00394 
00395     /* Get the emission at initial wavelengths */
00396     gen_init = cpl_bivector_new(spec_sz);
00397     if (guess_resamp) {
00398         error = irplib_wlcalib_fill_spectrum(cpl_bivector_get_y(gen_init),
00399                                           lines_catalog, conv_kernel,
00400                                           guess_poly, 0);
00401     } else {
00402         error = irplib_vector_fill_line_spectrum_model
00403             (cpl_bivector_get_y(gen_init), NULL, NULL,
00404              guess_poly, lines_catalog,
00405              slitw, fwhm, xtrunc, 0, CPL_FALSE, CPL_FALSE, NULL);
00406     }
00407 
00408     if (error || cpl_vector_fill_polynomial(cpl_bivector_get_x(gen_init),
00409                                             guess_poly, 1, 1)) {
00410         cpl_vector_delete(conv_kernel);
00411         cpl_bivector_delete(gen_init);
00412         cpl_error_set_message_macro(cpl_func, CPL_ERROR_ILLEGAL_INPUT,
00413                                     __FILE__, __LINE__, "Cannot get the "
00414                                     "emission spectrum");
00415         return NULL;
00416     }
00417  
00418     /* Get the emission at corrected wavelengths */
00419     gen_corr = cpl_bivector_new(spec_sz);
00420     if (corr_resamp) {
00421         error = irplib_wlcalib_fill_spectrum(cpl_bivector_get_y(gen_corr),
00422                                           lines_catalog, conv_kernel,
00423                                           corr_poly, 0);
00424     } else {
00425         error = irplib_vector_fill_line_spectrum_model
00426             (cpl_bivector_get_y(gen_corr), NULL, NULL,
00427              corr_poly, lines_catalog,
00428              slitw, fwhm, xtrunc, 0, CPL_FALSE, CPL_FALSE, NULL);
00429     }
00430 
00431     if (error || cpl_vector_fill_polynomial(cpl_bivector_get_x(gen_corr),
00432                                             corr_poly, 1, 1)) {
00433         cpl_vector_delete(conv_kernel);
00434         cpl_bivector_delete(gen_init);
00435         cpl_bivector_delete(gen_corr) ;
00436         cpl_error_set_message_macro(cpl_func, CPL_ERROR_ILLEGAL_INPUT,
00437                                     __FILE__, __LINE__, "Cannot get the "
00438                                     "emission spectrum");
00439         return NULL;
00440     }
00441     cpl_vector_delete(conv_kernel) ;
00442 
00443     /* Create the ouput table */
00444     spc_table = cpl_table_new(spec_sz);
00445     cpl_table_new_column(spc_table, IRPLIB_WLXCORR_COL_WAVELENGTH, 
00446             CPL_TYPE_DOUBLE);
00447     cpl_table_new_column(spc_table, IRPLIB_WLXCORR_COL_CAT_INIT, 
00448             CPL_TYPE_DOUBLE);
00449     cpl_table_new_column(spc_table, IRPLIB_WLXCORR_COL_CAT_FINAL, 
00450             CPL_TYPE_DOUBLE);
00451     cpl_table_new_column(spc_table, IRPLIB_WLXCORR_COL_OBS, CPL_TYPE_DOUBLE);
00452     
00453     /* Update table */
00454     pgen = cpl_bivector_get_x_data_const(gen_corr) ;
00455     cpl_table_copy_data_double(spc_table, IRPLIB_WLXCORR_COL_WAVELENGTH, pgen) ;
00456     pgen = cpl_bivector_get_y_data_const(gen_corr) ;
00457     cpl_table_copy_data_double(spc_table, IRPLIB_WLXCORR_COL_CAT_FINAL, pgen) ;
00458     pgen = cpl_vector_get_data_const(spectrum) ;
00459     cpl_table_copy_data_double(spc_table, IRPLIB_WLXCORR_COL_OBS, pgen) ;
00460     pgen = cpl_bivector_get_y_data_const(gen_init) ;
00461     cpl_table_copy_data_double(spc_table, IRPLIB_WLXCORR_COL_CAT_INIT, pgen);
00462     cpl_bivector_delete(gen_init);
00463     cpl_bivector_delete(gen_corr);
00464 
00465     return spc_table ;
00466 }
00467 
00468 /*----------------------------------------------------------------------------*/
00480 /*----------------------------------------------------------------------------*/
00481 cpl_bivector * irplib_wlxcorr_cat_extract(
00482         const cpl_bivector  *   lines_catalog,
00483         double                  wave_min,
00484         double                  wave_max)
00485 {
00486     const int           nlines = cpl_bivector_get_size(lines_catalog);
00487     int                 wave_min_id, wave_max_id ;
00488     cpl_vector       *  sub_cat_wl ;
00489     cpl_vector       *  sub_cat_int ;
00490     const cpl_vector *  xlines  = cpl_bivector_get_x_const(lines_catalog);
00491     const double     *  dxlines = cpl_vector_get_data_const(xlines);
00492 
00493     cpl_ensure(lines_catalog != NULL, CPL_ERROR_NULL_INPUT,    NULL);
00494 
00495     /* Find the 1st line */
00496     wave_min_id = (int)cpl_vector_find(xlines, wave_min);
00497     if (wave_min_id < 0) {
00498         cpl_error_set_message_macro(cpl_func, CPL_ERROR_ILLEGAL_INPUT,
00499                                     __FILE__, __LINE__, 
00500                                     "The starting wavelength cannot be found") ;
00501         return NULL ;
00502     }
00503 
00504     /* The first line must be greater than (at least?) wave_min */
00505     if (dxlines[wave_min_id] <= wave_min) wave_min_id++;
00506 
00507     /* Find the last line */
00508     wave_max_id = (int)cpl_vector_find(xlines, wave_max);
00509     if (wave_max_id < 0) {
00510         cpl_error_set_message_macro(cpl_func, CPL_ERROR_ILLEGAL_INPUT,
00511                                     __FILE__, __LINE__, 
00512                                     "The ending wavelength cannot be found") ;
00513         return NULL ;
00514     }
00515     /* The last line must be less than wave_max */
00516     if (dxlines[wave_max_id] >= wave_min) wave_max_id--;
00517 
00518     /* Checking the wavelength range at this point via the indices also
00519        verifies that they were not found using non-increasing wavelengths */
00520     cpl_ensure(wave_min_id <= wave_max_id, CPL_ERROR_ILLEGAL_INPUT, NULL);
00521 
00522     if (wave_min_id < 0 || wave_max_id == nlines) {
00523         cpl_error_set_message_macro(cpl_func, CPL_ERROR_ILLEGAL_INPUT,
00524                                     __FILE__, __LINE__, "The %d-line catalogue "
00525                                     "has no lines in the range %g -> %g",
00526                                     nlines, wave_min, wave_max);
00527         return NULL ;
00528     }
00529 
00530     sub_cat_wl = cpl_vector_extract(xlines, wave_min_id, wave_max_id, 1);
00531     sub_cat_int = cpl_vector_extract(cpl_bivector_get_y_const(lines_catalog), 
00532                                      wave_min_id, wave_max_id, 1);
00533  
00534     return cpl_bivector_wrap_vectors(sub_cat_wl, sub_cat_int);
00535 }
00536 
00537 /*----------------------------------------------------------------------------*/
00554 /*----------------------------------------------------------------------------*/
00555 cpl_vector * irplib_wlxcorr_convolve_create_kernel(double  slitw,
00556                                                    double  fwhm)
00557 {
00558     const double  sigma  = fwhm * CPL_MATH_SIG_FWHM;
00559     const int     size   = 1 + (int)(5.0 * sigma + 0.5*slitw);
00560     cpl_vector  * kernel = cpl_vector_new(size);
00561 
00562 
00563     if (cpl_vector_fill_lss_profile_symmetric(kernel, slitw, fwhm)) {
00564         cpl_vector_delete(kernel);
00565         cpl_ensure(0, cpl_error_get_code(), NULL);
00566     }
00567 
00568     return kernel;
00569 }
00570 
00571 /*----------------------------------------------------------------------------*/
00584 /*----------------------------------------------------------------------------*/
00585 int irplib_wlxcorr_convolve(
00586         cpl_vector          *   smoothed,
00587         const cpl_vector    *   conv_kernel)
00588 {
00589     int             nsamples ;
00590     int             ihwidth ;
00591     cpl_vector  *   raw ;
00592     double      *   psmoothe ;
00593     double      *   praw ;
00594     const double*   psymm ;
00595     int             i, j ;
00596 
00597     /* Test entries */
00598     cpl_ensure(smoothed, CPL_ERROR_NULL_INPUT, -1) ;
00599     cpl_ensure(conv_kernel, CPL_ERROR_NULL_INPUT, -1) ;
00600 
00601     /* Initialise */
00602     nsamples = cpl_vector_get_size(smoothed) ;
00603     ihwidth = cpl_vector_get_size(conv_kernel) - 1 ;
00604     cpl_ensure(ihwidth<nsamples, CPL_ERROR_ILLEGAL_INPUT, -1) ;
00605     psymm = cpl_vector_get_data_const(conv_kernel) ;
00606     psmoothe = cpl_vector_get_data(smoothed) ;
00607     
00608     /* Create raw vector */
00609     raw = cpl_vector_duplicate(smoothed) ;
00610     praw = cpl_vector_get_data(raw) ;
00611 
00612     /* Convolve with the symmetric function */
00613     for (i=0 ; i<ihwidth ; i++) {
00614         psmoothe[i] = praw[i] * psymm[0];
00615         for (j=1 ; j <= ihwidth ; j++) {
00616             const int k = i-j < 0 ? 0 : i-j;
00617             psmoothe[i] += (praw[k]+praw[i+j]) * psymm[j];
00618         }
00619     }
00620 
00621     for (i=ihwidth ; i<nsamples-ihwidth ; i++) {
00622         psmoothe[i] = praw[i] * psymm[0];
00623         for (j=1 ; j<=ihwidth ; j++)
00624             psmoothe[i] += (praw[i-j]+praw[i+j]) * psymm[j];
00625     }
00626     for (i=nsamples-ihwidth ; i<nsamples ; i++) {
00627         psmoothe[i] = praw[i] * psymm[0];
00628         for (j=1 ; j<=ihwidth ; j++) {
00629             const int k = i+j > nsamples-1 ? nsamples - 1 : i+j;
00630             psmoothe[i] += (praw[k]+praw[i-j]) * psymm[j];
00631         }
00632     }
00633     cpl_vector_delete(raw) ;
00634     return 0 ;
00635 }
00636 
00637 /*----------------------------------------------------------------------------*/
00647 /*----------------------------------------------------------------------------*/
00648 int irplib_wlxcorr_plot_solution(
00649         const cpl_polynomial    *   init,
00650         const cpl_polynomial    *   comp,
00651         const cpl_polynomial    *   sol,
00652         int                         pix_start,
00653         int                         pix_stop) 
00654 {
00655     int                 nsamples, nplots ;
00656     cpl_vector      **  vectors ;
00657     cpl_bivector    *   bivector ;
00658     double              diff ;
00659     int                 i ;
00660     
00661     /* Test entries */
00662     if (init == NULL || comp == NULL) return -1 ;
00663     
00664     /* Initialise */
00665     nsamples = pix_stop - pix_start + 1 ;
00666     if (sol != NULL)    nplots = 3 ;
00667     else                nplots = 2 ;
00668     
00669     /* Create vectors */
00670     vectors = cpl_malloc((nplots+1)*sizeof(cpl_vector*)) ;
00671     for (i=0 ; i<nplots+1 ; i++) vectors[i] = cpl_vector_new(nsamples) ;
00672 
00673     /* First plot with the lambda/pixel relation */
00674     /* Fill vectors */
00675     for (i=0 ; i<nsamples ; i++) {
00676         cpl_vector_set(vectors[0], i, pix_start+i) ;
00677         cpl_vector_set(vectors[1], i, 
00678                 cpl_polynomial_eval_1d(init, (double)(pix_start+i), NULL)) ;
00679         cpl_vector_set(vectors[2], i, 
00680                 cpl_polynomial_eval_1d(comp, (double)(pix_start+i), NULL)) ;
00681         if (sol != NULL) 
00682             cpl_vector_set(vectors[3], i, 
00683                     cpl_polynomial_eval_1d(sol, (double)(pix_start+i), NULL)) ;
00684     }
00685 
00686     /* Plot */
00687     cpl_plot_vectors("set grid;set xlabel 'Position (pixels)';", 
00688         "t '1-Initial / 2-Computed / 3-Solution' w lines", 
00689         "", (const cpl_vector **)vectors, nplots+1);
00690 
00691     /* Free vectors */
00692     for (i=0 ; i<nplots+1 ; i++) cpl_vector_delete(vectors[i]) ;
00693     cpl_free(vectors) ;
00694 
00695     /* Allocate vectors */
00696     nplots -- ;
00697     vectors = cpl_malloc((nplots+1)*sizeof(cpl_vector*)) ;
00698     for (i=0 ; i<nplots+1 ; i++) vectors[i] = cpl_vector_new(nsamples) ;
00699     
00700     /* Second plot with the delta-lambda/pixel relation */
00701     /* Fill vectors */
00702     for (i=0 ; i<nsamples ; i++) {
00703         cpl_vector_set(vectors[0], i, pix_start+i) ;
00704         diff = cpl_polynomial_eval_1d(comp, (double)(pix_start+i), NULL) -
00705             cpl_polynomial_eval_1d(init, (double)(pix_start+i), NULL) ;
00706         cpl_vector_set(vectors[1], i, diff) ;
00707         if (sol != NULL) {
00708             diff = cpl_polynomial_eval_1d(sol, (double)(pix_start+i), NULL) -
00709                 cpl_polynomial_eval_1d(init, (double)(pix_start+i), NULL) ;
00710             cpl_vector_set(vectors[2], i, diff) ;
00711         }
00712     }
00713 
00714     /* Plot */
00715     if (sol == NULL) {
00716         bivector = cpl_bivector_wrap_vectors(vectors[0], vectors[1]) ;
00717         cpl_plot_bivector(
00718 "set grid;set xlabel 'Position (pixels)';set ylabel 'Wavelength difference';", 
00719             "t 'Computed-Initial wavelenth' w lines", "", bivector);
00720         cpl_bivector_unwrap_vectors(bivector) ;
00721     } else {
00722         cpl_plot_vectors("set grid;set xlabel 'Position (pixels)';", 
00723             "t '1-Computed - Initial / 2--Solution - Initial' w lines", 
00724             "", (const cpl_vector **)vectors, nplots+1);
00725     }
00726     
00727     /* Free vectors */
00728     for (i=0 ; i<nplots+1 ; i++) cpl_vector_delete(vectors[i]) ;
00729     cpl_free(vectors) ;
00730 
00731     /* Return */
00732     return 0 ;
00733 }
00734 
00735 /*----------------------------------------------------------------------------*/
00746 /*----------------------------------------------------------------------------*/
00747 int irplib_wlxcorr_plot_spc_table(
00748         const cpl_table     *   spc_table, 
00749         const char          *   title,
00750         int                     first_plotted_line,
00751         int                     last_plotted_line) 
00752 {
00753     char                title_loc[1024] ;
00754     cpl_vector      **  vectors ;
00755     cpl_vector      **  sub_vectors ;
00756     cpl_vector      *   tmp_vec ;
00757     int                 nsamples ;
00758     double              max, mean1, mean3 ;
00759     int                 start_ind, stop_ind, hsize_pix ;
00760     int                 i, j ;
00761 
00762     /* Test entries */
00763     if (first_plotted_line > last_plotted_line) return -1 ;
00764     if (spc_table == NULL) return -1 ;
00765     
00766     /* Initialise */
00767     nsamples = cpl_table_get_nrow(spc_table) ;
00768     hsize_pix = 10 ;
00769     
00770     sprintf(title_loc, 
00771         "t '%s - 1-Initial catalog/2-Corrected catalog/3-Observed' w lines",
00772         title) ;
00773     title_loc[1023] = (char)0 ;
00774     
00775     vectors = cpl_malloc(4*sizeof(cpl_vector*)) ;
00776     vectors[0] = cpl_vector_wrap(nsamples, 
00777             cpl_table_get_data_double((cpl_table*)spc_table,
00778                 IRPLIB_WLXCORR_COL_WAVELENGTH));
00779     vectors[1] = cpl_vector_wrap(nsamples, 
00780             cpl_table_get_data_double((cpl_table*)spc_table, 
00781                 IRPLIB_WLXCORR_COL_CAT_INIT));
00782     vectors[2] = cpl_vector_wrap(nsamples, 
00783             cpl_table_get_data_double((cpl_table*)spc_table, 
00784                 IRPLIB_WLXCORR_COL_CAT_FINAL));
00785     vectors[3] = cpl_vector_wrap(nsamples, 
00786             cpl_table_get_data_double((cpl_table*)spc_table, 
00787                 IRPLIB_WLXCORR_COL_OBS)) ;
00788 
00789     /* Scale the signal for a bettre display */
00790     mean1 = cpl_vector_get_mean(vectors[1]) ;
00791     mean3 = cpl_vector_get_mean(vectors[3]) ;
00792     if (fabs(mean3) > 1)
00793         cpl_vector_multiply_scalar(vectors[3], fabs(mean1/mean3)) ;
00794 
00795     cpl_plot_vectors("set grid;set xlabel 'Wavelength (nm)';", title_loc,
00796         "", (const cpl_vector **)vectors, 4);
00797 
00798     /* Unscale the signal */
00799     if (fabs(mean3) > 1)
00800         cpl_vector_multiply_scalar(vectors[3], mean3/mean1) ;
00801 
00802     /* Loop on the brightest lines and zoom on them */
00803     sprintf(title_loc, 
00804 "t '%s - 1-Initial catalog/2-Corrected catalog/3-Observed (ZOOMED)' w lines",
00805         title) ;
00806     title_loc[1023] = (char)0 ;
00807     tmp_vec = cpl_vector_duplicate(vectors[2]) ;
00808     for (i=0 ; i<last_plotted_line ; i++) {
00809         /* Find the brightest line */
00810         if ((max = cpl_vector_get_max(tmp_vec)) <= 0.0) break ;
00811         for (j=0 ; j<nsamples ; j++) {
00812             if (cpl_vector_get(tmp_vec, j) == max) break ;
00813         }
00814         if (j-hsize_pix < 0) start_ind = 0 ;
00815         else start_ind = j-hsize_pix ;
00816         if (j+hsize_pix > nsamples-1) stop_ind = nsamples-1 ;
00817         else stop_ind = j+hsize_pix ;
00818         for (j=start_ind ; j<=stop_ind ; j++) cpl_vector_set(tmp_vec, j, 0.0) ;
00819 
00820         if (i+1 >= first_plotted_line) {
00821             sub_vectors = cpl_malloc(4*sizeof(cpl_vector*)) ;
00822             sub_vectors[0]=cpl_vector_extract(vectors[0],start_ind,stop_ind,1);
00823             sub_vectors[1]=cpl_vector_extract(vectors[1],start_ind,stop_ind,1);
00824             sub_vectors[2]=cpl_vector_extract(vectors[2],start_ind,stop_ind,1);
00825             sub_vectors[3]=cpl_vector_extract(vectors[3],start_ind,stop_ind,1);
00826 
00827             cpl_plot_vectors("set grid;set xlabel 'Wavelength (nm)';", 
00828                     title_loc, "", (const cpl_vector **)sub_vectors, 4);
00829 
00830             cpl_vector_delete(sub_vectors[0]) ;
00831             cpl_vector_delete(sub_vectors[1]) ;
00832             cpl_vector_delete(sub_vectors[2]) ;
00833             cpl_vector_delete(sub_vectors[3]) ;
00834             cpl_free(sub_vectors) ;
00835         }
00836     }
00837     cpl_vector_delete(tmp_vec) ;
00838     
00839     cpl_vector_unwrap(vectors[0]) ;
00840     cpl_vector_unwrap(vectors[1]) ;
00841     cpl_vector_unwrap(vectors[2]) ;
00842     cpl_vector_unwrap(vectors[3]) ;
00843     cpl_free(vectors) ;
00844 
00845     return 0 ;
00846 }
00847 
00848 /*----------------------------------------------------------------------------*/
00856 /*----------------------------------------------------------------------------*/
00857 int irplib_wlxcorr_catalog_plot(
00858         const cpl_bivector      *   cat,
00859         double                      wmin,
00860         double                      wmax) 
00861 {
00862     int                 start, stop ;
00863     cpl_bivector    *   subcat ;
00864     cpl_vector      *   subcat_x ;
00865     cpl_vector      *   subcat_y ;
00866     const double    *   pwave ;
00867     int                 nvals, nvals_tot ;
00868     int                 i ;
00869 
00870     /* Test entries */
00871     if (cat == NULL) return -1 ;
00872     if (wmax <= wmin) return -1 ;
00873 
00874     /* Initialise */
00875     nvals_tot = cpl_bivector_get_size(cat) ;
00876 
00877     /* Count the nb of values */
00878     pwave = cpl_bivector_get_x_data_const(cat) ;
00879     if (pwave[0] >= wmin) start = 0 ;
00880     else start = -1 ;
00881     if (pwave[nvals_tot-1] <= wmax) stop = nvals_tot-1 ;
00882     else stop = -1 ;
00883     i=0 ;
00884     while ((pwave[i] < wmin) && (i<nvals_tot-1)) i++ ;
00885     start = i ;
00886     i= nvals_tot-1 ;
00887     while ((pwave[i] > wmax) && (i>0)) i-- ;
00888     stop = i ;
00889 
00890     if (start>=stop) {
00891         cpl_msg_error(cpl_func, "Cannot plot the catalog") ;
00892         return -1 ;
00893     }
00894     nvals = start - stop + 1 ;
00895 
00896     /* Create the bivector to plot */
00897     subcat_x = cpl_vector_extract(cpl_bivector_get_x_const(cat),start,stop, 1) ;
00898     subcat_y = cpl_vector_extract(cpl_bivector_get_y_const(cat),start,stop, 1) ;
00899     subcat = cpl_bivector_wrap_vectors(subcat_x, subcat_y) ;
00900 
00901     /* Plot */
00902     if (nvals > 500) {
00903         cpl_plot_bivector(
00904                 "set grid;set xlabel 'Wavelength (nm)';set ylabel 'Emission';",
00905                 "t 'Catalog Spectrum' w lines", "", subcat);
00906     } else {
00907         cpl_plot_bivector(
00908                 "set grid;set xlabel 'Wavelength (nm)';set ylabel 'Emission';",
00909                 "t 'Catalog Spectrum' w impulses", "", subcat);
00910     }
00911     cpl_bivector_unwrap_vectors(subcat) ;
00912     cpl_vector_delete(subcat_x) ;
00913     cpl_vector_delete(subcat_y) ;
00914 
00915     return 0 ;
00916 }
00917    
00920 /*----------------------------------------------------------------------------*/
00935 /*----------------------------------------------------------------------------*/
00936 static void irplib_wlxcorr_estimate(cpl_vector           * vxc,
00937                                     cpl_vector           * model,
00938                                     const cpl_vector     * spectrum,
00939                                     const cpl_bivector   * lines_catalog,
00940                                     const cpl_vector     * conv_kernel,
00941                                     const cpl_polynomial * poly_candi,
00942                                     double                 slitw,
00943                                     double                 fwhm)
00944 {
00945     cpl_errorstate prestate = cpl_errorstate_get();
00946     const int hsize = cpl_vector_get_size(vxc) / 2;
00947 
00948     if (conv_kernel != NULL) {
00949         irplib_wlcalib_fill_spectrum(model, lines_catalog, conv_kernel,
00950                                   poly_candi, hsize);
00951     } else {
00952         const double xtrunc = 0.5 * slitw + 5.0 * fwhm * CPL_MATH_SIG_FWHM;
00953 
00954         irplib_vector_fill_line_spectrum_model(model, NULL, NULL, poly_candi,
00955                                                lines_catalog, slitw, fwhm,
00956                                                xtrunc, 0, CPL_FALSE, CPL_FALSE,
00957                                                NULL);
00958     }
00959 
00960     if (cpl_errorstate_is_equal(prestate))
00961         cpl_vector_correlate(vxc, model, spectrum);
00962 
00963     if (!cpl_errorstate_is_equal(prestate)) {
00964         cpl_vector_fill(vxc, 0.0);
00965 
00966         /* cpl_errorstate_dump(prestate, CPL_FALSE, NULL); */
00967         cpl_errorstate_set(prestate);
00968 
00969     }
00970 
00971     return;
00972 }
00973 
00974 
00975 /*----------------------------------------------------------------------------*/
00985 /*----------------------------------------------------------------------------*/
00986 static cpl_boolean irplib_wlcalib_is_lines(const cpl_vector * wavelengths,
00987                                         const cpl_polynomial * disp1d,
00988                                         int spec_sz,
00989                                         double tol)
00990 {
00991     const int nlines = cpl_vector_get_size(wavelengths);
00992     /* The dispersion on the detector center */
00993     const double dispersion = cpl_polynomial_eval_1d_diff(disp1d,
00994                                                           0.5 * spec_sz + 1.0,
00995                                                           0.5 * spec_sz,
00996                                                           NULL);
00997     const double range = cpl_vector_get(wavelengths, nlines-1)
00998         - cpl_vector_get(wavelengths, 0);
00999 
01000     cpl_ensure(wavelengths != NULL, CPL_ERROR_NULL_INPUT,    CPL_FALSE);
01001     cpl_ensure(disp1d      != NULL, CPL_ERROR_NULL_INPUT,    CPL_FALSE);
01002     cpl_ensure(cpl_polynomial_get_dimension(disp1d) == 1,
01003                CPL_ERROR_ILLEGAL_INPUT, CPL_FALSE);
01004     cpl_ensure(range > 0.0,      CPL_ERROR_ILLEGAL_INPUT, CPL_FALSE);
01005 
01006     return nlines * fabs(dispersion) <= tol * fabs(range) ? CPL_TRUE
01007         : CPL_FALSE;
01008 
01009 }
01010 
01011 /*----------------------------------------------------------------------------*/
01026 /*----------------------------------------------------------------------------*/
01027 static
01028 cpl_error_code irplib_wlcalib_fill_spectrum(cpl_vector           * self,
01029                                          const cpl_bivector   * lines_catalog,
01030                                          const cpl_vector     * conv_kernel,
01031                                          const cpl_polynomial * poly,
01032                                          int                    search_hs)
01033 {
01034 
01035 
01036     const int          size = cpl_vector_get_size(self);
01037     const int          nlines = cpl_bivector_get_size(lines_catalog);
01038     const cpl_vector * xlines  = cpl_bivector_get_x_const(lines_catalog);
01039     const double     * dxlines = cpl_vector_get_data_const(xlines);
01040     cpl_bivector     * sub_cat ;
01041     cpl_vector       * sub_cat_x;
01042     cpl_vector       * sub_cat_y;
01043     cpl_vector       * wl_limits;
01044     double             wave_min, wave_max;
01045     int                wave_min_id, wave_max_id;
01046     int                nsub;
01047     int                error;
01048 
01049     cpl_ensure_code(self          != NULL, CPL_ERROR_NULL_INPUT);
01050     cpl_ensure_code(lines_catalog != NULL, CPL_ERROR_NULL_INPUT);
01051     cpl_ensure_code(conv_kernel   != NULL, CPL_ERROR_NULL_INPUT);
01052     cpl_ensure_code(poly          != NULL, CPL_ERROR_NULL_INPUT);
01053     cpl_ensure_code(size > 0,              CPL_ERROR_ILLEGAL_INPUT);
01054 
01055 
01056     /* Resample the spectrum */
01057     wl_limits = cpl_vector_new(size + 1);
01058     cpl_vector_fill_polynomial(wl_limits, poly, 0.5 - search_hs, 1);
01059 
01060     /* The spectrum wavelength bounds */
01061     wave_min = cpl_vector_get(wl_limits, 0);
01062     wave_max = cpl_vector_get(wl_limits, size);
01063 
01064     /* Find the 1st line */
01065     wave_min_id = cpl_vector_find(xlines, wave_min);
01066     /* The first line must be less than or equal to wave_min */
01067     if (dxlines[wave_min_id] > wave_min) wave_min_id--;
01068 
01069     if (wave_min_id < 0) {
01070         cpl_vector_delete(wl_limits);
01071         return cpl_error_set_message_macro(cpl_func, CPL_ERROR_ILLEGAL_INPUT,
01072                                            __FILE__, __LINE__, "The %d-line "
01073                                            "catalogue only has lines above %g",
01074                                            nlines, wave_min);
01075     }
01076 
01077     /* Find the last line */
01078     wave_max_id = cpl_vector_find(xlines, wave_max);
01079     /* The last line must be greater than or equal to wave_max */
01080     if (dxlines[wave_max_id] < wave_max) wave_max_id++;
01081 
01082     if (wave_max_id == nlines) {
01083         cpl_vector_delete(wl_limits);
01084         return cpl_error_set_message_macro(cpl_func, CPL_ERROR_ILLEGAL_INPUT,
01085                                            __FILE__, __LINE__, "The %d-line "
01086                                            "catalogue only has lines below %g",
01087                                            nlines, wave_max);
01088     }
01089 
01090     /* Checking the wavelength range at this point via the indices also
01091        verifies that they were not found using non-increasing wavelengths */
01092     nsub = 1 + wave_max_id - wave_min_id;
01093     cpl_ensure_code(nsub > 1, CPL_ERROR_ILLEGAL_INPUT);
01094 
01095     /* Wrap a new bivector around the relevant part of the catalog */
01096     /* The data is _not_ modified */
01097     sub_cat_x = cpl_vector_wrap(nsub, wave_min_id + (double*)dxlines);
01098     sub_cat_y = cpl_vector_wrap(nsub, wave_min_id + (double*)
01099                                 cpl_bivector_get_y_data_const(lines_catalog));
01100     sub_cat = cpl_bivector_wrap_vectors(sub_cat_x, sub_cat_y);
01101 
01102     /* High resolution catalog */
01103     error = irplib_wlxcorr_signal_resample(self, wl_limits, sub_cat);
01104 
01105     cpl_vector_delete(wl_limits);
01106     cpl_bivector_unwrap_vectors(sub_cat);
01107     (void)cpl_vector_unwrap(sub_cat_x);
01108     (void)cpl_vector_unwrap(sub_cat_y);
01109 
01110     cpl_ensure_code(!error, CPL_ERROR_ILLEGAL_INPUT);
01111 
01112     /* Smooth the instrument resolution */
01113     cpl_ensure_code(!irplib_wlxcorr_convolve(self, conv_kernel),
01114                     cpl_error_get_code());
01115 
01116     return CPL_ERROR_NONE;
01117 }
01118 
01119 
01120 /*----------------------------------------------------------------------------*/
01130 /*----------------------------------------------------------------------------*/
01131 static int irplib_wlxcorr_signal_resample(
01132         cpl_vector          *   resampled, 
01133         const cpl_vector    *   xbounds,
01134         const cpl_bivector  *   hires)
01135 {
01136     const int           hrsize = cpl_bivector_get_size(hires);
01137     const cpl_vector*   xhires ;
01138     const cpl_vector*   yhires ;
01139     const double    *   pxhires ;
01140     const double    *   pyhires ;
01141     const double    *   pxbounds ;
01142     cpl_vector      *   ybounds ;
01143     cpl_bivector    *   boundary ;
01144     double          *   pybounds ;
01145     double          *   presampled ;
01146     int                 nsamples ;
01147     int                 i, itt ;
01148    
01149     /* Test entries */
01150     if ((!resampled) || (!xbounds) || (!hires)) return -1 ;
01151 
01152     /* Initialise */
01153     nsamples = cpl_vector_get_size(resampled) ;
01154 
01155     /* Initialise */
01156     presampled = cpl_vector_get_data(resampled) ;
01157     pxbounds = cpl_vector_get_data_const(xbounds) ;
01158     xhires = cpl_bivector_get_x_const(hires) ;
01159     yhires = cpl_bivector_get_y_const(hires) ;
01160     pxhires = cpl_vector_get_data_const(xhires) ;
01161     pyhires = cpl_vector_get_data_const(yhires) ;
01162     
01163     /* Create a new vector */
01164     ybounds = cpl_vector_new(cpl_vector_get_size(xbounds)) ;
01165     boundary = cpl_bivector_wrap_vectors((cpl_vector*)xbounds,ybounds) ;
01166     pybounds = cpl_vector_get_data(ybounds) ;
01167 
01168     /* Test entries */
01169     if (cpl_bivector_get_size(boundary) != nsamples + 1) {
01170         cpl_bivector_unwrap_vectors(boundary) ;
01171         cpl_vector_delete(ybounds) ;
01172         return -1 ;
01173     }
01174 
01175     /* Get the ind  */
01176     itt = cpl_vector_find(xhires, pxbounds[0]);
01177 
01178     /* Interpolate the signal */
01179     if (cpl_bivector_interpolate_linear(boundary, hires)) {
01180         cpl_bivector_unwrap_vectors(boundary) ;
01181         cpl_vector_delete(ybounds) ;
01182         return -1 ;
01183     }
01184 
01185     /* At this point itt most likely points to element just below
01186        pxbounds[0] */
01187     while (pxhires[itt] < pxbounds[0]) itt++;
01188 
01189     for (i=0; i < nsamples; i++) {
01190         /* The i'th signal is the weighted average of the two interpolated
01191            signals at the pixel boundaries and those table signals in
01192            between */
01193 
01194         double xlow  = pxbounds[i];
01195         double x     = pxhires[itt];
01196 
01197         if (x > pxbounds[i+1]) x = pxbounds[i+1];
01198         /* Contribution from interpolated value at wavelength at lower pixel
01199            boundary */
01200         presampled[i] = pybounds[i] * (x - xlow);
01201 
01202         /* Contribution from table values in between pixel boundaries */
01203         while ((pxhires[itt] < pxbounds[i+1]) && (itt < hrsize)) {
01204             const double xprev = x;
01205             x = pxhires[itt+1];
01206             if (x > pxbounds[i+1]) x = pxbounds[i+1];
01207             presampled[i] += pyhires[itt] * (x - xlow);
01208             xlow = xprev;
01209             itt++;
01210         }
01211 
01212         /* Contribution from interpolated value at wavelength at upper pixel
01213            boundary */
01214         presampled[i] += pybounds[i+1] * (pxbounds[i+1] - xlow);
01215 
01216         /* Compute average by dividing integral by length of pixel range
01217            (the factor 2 comes from the contributions) */
01218         presampled[i] /= 2 * (pxbounds[i+1] - pxbounds[i]);
01219     }
01220     cpl_bivector_unwrap_vectors(boundary) ;
01221     cpl_vector_delete(ybounds) ;
01222     return 0 ;
01223 }
01224 
01225 
01226 
01227 /*----------------------------------------------------------------------------*/
01248 /*----------------------------------------------------------------------------*/
01249 static cpl_error_code cpl_vector_fill_lss_profile_symmetric(cpl_vector * self,
01250                                                             double  slitw,
01251                                                             double  fwhm)
01252 {
01253 
01254     const double sigma = fwhm * CPL_MATH_SIG_FWHM;
01255     const int    n     = cpl_vector_get_size(self);
01256     int          i;
01257 
01258 
01259     cpl_ensure_code(self != NULL, CPL_ERROR_NULL_INPUT);
01260     cpl_ensure_code(slitw > 0.0,  CPL_ERROR_ILLEGAL_INPUT);
01261     cpl_ensure_code(fwhm  > 0.0,  CPL_ERROR_ILLEGAL_INPUT);
01262 
01263     /* Cannot fail now */
01264 
01265     /* Special case for i = 0 */
01266     (void)cpl_vector_set(self, 0,
01267                          (irplib_erf_antideriv(0.5*slitw + 0.5, sigma) -
01268                           irplib_erf_antideriv(0.5*slitw - 0.5, sigma)) / slitw);
01269 
01270     for (i = 1; i < n; i++) {
01271         /* FIXME: Reuse two irplib_erf_antideriv() calls from previous value */
01272         const double x1p = i + 0.5*slitw + 0.5;
01273         const double x1n = i - 0.5*slitw + 0.5;
01274         const double x0p = i + 0.5*slitw - 0.5;
01275         const double x0n = i - 0.5*slitw - 0.5;
01276         const double val = 0.5/slitw *
01277             (irplib_erf_antideriv(x1p, sigma) - irplib_erf_antideriv(x1n, sigma) -
01278              irplib_erf_antideriv(x0p, sigma) + irplib_erf_antideriv(x0n, sigma));
01279         (void)cpl_vector_set(self, i, val);
01280     }
01281 
01282     return CPL_ERROR_NONE;
01283 }

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