irplib_wlxcorr.c

00001 /* $Id: irplib_wlxcorr.c,v 1.39 2008/02/07 12:37:16 llundin 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019  */
00020 
00021 /*
00022  * $Author: llundin $
00023  * $Date: 2008/02/07 12:37:16 $
00024  * $Revision: 1.39 $
00025  * $Name: uves-3_9_0 $
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 <cpl.h>
00038 
00039 #include "irplib_wlxcorr.h"
00040 #include "irplib_plot.h"
00041 
00042 /*----------------------------------------------------------------------------*/
00047 /*----------------------------------------------------------------------------*/
00048 
00052 /*----------------------------------------------------------------------------*/
00074 /*----------------------------------------------------------------------------*/
00075 cpl_error_code irplib_wlxcorr_find_1d(cpl_polynomial * self,
00076                                       const cpl_vector * observed,
00077                                       cpl_error_code (*pfmodel)(cpl_vector *,
00078                                                                 const cpl_vector *,
00079                                                                 const cpl_table *),
00080                                       double maxshift,
00081                                       const cpl_array * degshift,
00082                                       const cpl_array * subresol)
00083 
00084 {
00085 
00086     cpl_ensure_code(self     != NULL, CPL_ERROR_NULL_INPUT);
00087     cpl_ensure_code(observed != NULL, CPL_ERROR_NULL_INPUT);
00088     cpl_ensure_code(pfmodel  != NULL, CPL_ERROR_NULL_INPUT);
00089     cpl_ensure_code(degshift != NULL, CPL_ERROR_NULL_INPUT);
00090     cpl_ensure_code(subresol != NULL, CPL_ERROR_NULL_INPUT);
00091 
00092     cpl_ensure_code(cpl_polynomial_get_dimension(self) == 1,
00093                      CPL_ERROR_INVALID_TYPE);
00094 
00095     cpl_ensure_code(cpl_array_get_type(subresol) == CPL_TYPE_INT,
00096                     CPL_ERROR_INVALID_TYPE);
00097 
00098     cpl_ensure_code(cpl_array_get_type(degshift) == CPL_TYPE_INT ||
00099                     cpl_array_get_type(degshift) == CPL_TYPE_FLOAT ||
00100                     cpl_array_get_type(degshift) == CPL_TYPE_DOUBLE,
00101                     CPL_ERROR_INVALID_TYPE);
00102 
00103     cpl_ensure_code(cpl_array_get_size(subresol)
00104                     == cpl_array_get_size(degshift),
00105                     CPL_ERROR_INVALID_TYPE);
00106 
00107     cpl_ensure_code(maxshift > 0,     CPL_ERROR_ILLEGAL_INPUT);
00108 
00109     return CPL_ERROR_UNSUPPORTED_MODE;
00110 }
00111 
00112 
00113 /*----------------------------------------------------------------------------*/
00123 /*----------------------------------------------------------------------------*/
00124 int irplib_wlxcorr_plot_solution(
00125         const cpl_polynomial    *   init,
00126         const cpl_polynomial    *   comp,
00127         const cpl_polynomial    *   sol,
00128         int                         pix_start,
00129         int                         pix_stop) 
00130 {
00131     int                 nsamples, nplots ;
00132     cpl_vector      **  vectors ;
00133     cpl_bivector    *   bivector ;
00134     double              diff ;
00135     int                 i ;
00136     
00137     /* Test entries */
00138     if (init == NULL || comp == NULL) return -1 ;
00139     
00140     /* Initialise */
00141     nsamples = pix_stop - pix_start + 1 ;
00142     if (sol != NULL)    nplots = 3 ;
00143     else                nplots = 2 ;
00144     
00145     /* Create vectors */
00146     vectors = cpl_malloc((nplots+1)*sizeof(cpl_vector*)) ;
00147     for (i=0 ; i<nplots+1 ; i++) vectors[i] = cpl_vector_new(nsamples) ;
00148 
00149     /* First plot with the lambda/pixel relation */
00150     /* Fill vectors */
00151     for (i=0 ; i<nsamples ; i++) {
00152         cpl_vector_set(vectors[0], i, pix_start+i) ;
00153         cpl_vector_set(vectors[1], i, 
00154                 cpl_polynomial_eval_1d(init, (double)(pix_start+i), NULL)) ;
00155         cpl_vector_set(vectors[2], i, 
00156                 cpl_polynomial_eval_1d(comp, (double)(pix_start+i), NULL)) ;
00157         if (sol != NULL) 
00158             cpl_vector_set(vectors[3], i, 
00159                     cpl_polynomial_eval_1d(sol, (double)(pix_start+i), NULL)) ;
00160     }
00161 
00162     /* Plot */
00163     irplib_vectors_plot("set grid;set xlabel 'Position (pixels)';", 
00164         "t '1-Initial / 2-Computed / 3-Solution' w lines", 
00165         "", (const cpl_vector **)vectors, nplots+1);
00166 
00167     /* Free vectors */
00168     for (i=0 ; i<nplots+1 ; i++) cpl_vector_delete(vectors[i]) ;
00169     cpl_free(vectors) ;
00170 
00171     /* Allocate vectors */
00172     nplots -- ;
00173     vectors = cpl_malloc((nplots+1)*sizeof(cpl_vector*)) ;
00174     for (i=0 ; i<nplots+1 ; i++) vectors[i] = cpl_vector_new(nsamples) ;
00175     
00176     /* Second plot with the delta-lambda/pixel relation */
00177     /* Fill vectors */
00178     for (i=0 ; i<nsamples ; i++) {
00179         cpl_vector_set(vectors[0], i, pix_start+i) ;
00180         diff = cpl_polynomial_eval_1d(comp, (double)(pix_start+i), NULL) -
00181             cpl_polynomial_eval_1d(init, (double)(pix_start+i), NULL) ;
00182         cpl_vector_set(vectors[1], i, diff) ;
00183         if (sol != NULL) {
00184             diff = cpl_polynomial_eval_1d(sol, (double)(pix_start+i), NULL) -
00185                 cpl_polynomial_eval_1d(init, (double)(pix_start+i), NULL) ;
00186             cpl_vector_set(vectors[2], i, diff) ;
00187         }
00188     }
00189 
00190     /* Plot */
00191     if (sol == NULL) {
00192         bivector = cpl_bivector_wrap_vectors(vectors[0], vectors[1]) ;
00193         irplib_bivector_plot(
00194 "set grid;set xlabel 'Position (pixels)';set ylabel 'Wavelength difference';", 
00195             "t 'Computed-Initial wavelenth' w lines", "", bivector);
00196         cpl_bivector_unwrap_vectors(bivector) ;
00197     } else {
00198         irplib_vectors_plot("set grid;set xlabel 'Position (pixels)';", 
00199             "t '1-Computed - Initial / 2--Solution - Initial' w lines", 
00200             "", (const cpl_vector **)vectors, nplots+1);
00201     }
00202     
00203     /* Free vectors */
00204     for (i=0 ; i<nplots+1 ; i++) cpl_vector_delete(vectors[i]) ;
00205     cpl_free(vectors) ;
00206 
00207     /* Return */
00208     return 0 ;
00209 }
00210 
00211 /*----------------------------------------------------------------------------*/
00218 /*----------------------------------------------------------------------------*/
00219 int irplib_wlxcorr_plot_spc_table(
00220         const cpl_table     *   spc_table, 
00221         const char          *   title) 
00222 {
00223     char                title_loc[1024] ;
00224     cpl_vector      **  vectors ;
00225     cpl_vector      **  sub_vectors ;
00226     cpl_vector      *   tmp_vec ;
00227     int                 nsamples ;
00228     double              hsize_nm, max, mean1, mean3 ;
00229     int                 start_ind, stop_ind, nblines, hsize_pix ;
00230     int                 i, j ;
00231 
00232     /* Test entries */
00233     if (spc_table == NULL) return -1 ;
00234     
00235     /* Initialise */
00236     nsamples = cpl_table_get_nrow(spc_table) ;
00237     hsize_nm = 0.2 ;
00238     hsize_pix = 10 ;
00239     nblines = 0 ;
00240     sprintf(title_loc, 
00241         "t '%s - 1-Initial catalog/2-Corrected catalog/3-Observed' w lines",
00242         title) ;
00243     title_loc[1023] = (char)0 ;
00244     
00245     vectors = cpl_malloc(4*sizeof(cpl_vector*)) ;
00246     vectors[0] = cpl_vector_wrap(nsamples, 
00247             cpl_table_get_data_double((cpl_table*)spc_table,
00248                 CPL_WLCALIB_XC_COL_WAVELENGTH));
00249     vectors[1] = cpl_vector_wrap(nsamples, 
00250             cpl_table_get_data_double((cpl_table*)spc_table, 
00251                 CPL_WLCALIB_XC_COL_CAT_INIT));
00252     vectors[2] = cpl_vector_wrap(nsamples, 
00253             cpl_table_get_data_double((cpl_table*)spc_table, 
00254                 CPL_WLCALIB_XC_COL_CAT_FINAL));
00255     vectors[3] = cpl_vector_wrap(nsamples, 
00256             cpl_table_get_data_double((cpl_table*)spc_table, 
00257                 CPL_WLCALIB_XC_COL_OBS)) ;
00258 
00259     /* Scale the signal for a bettre display */
00260     mean1 = cpl_vector_get_mean(vectors[1]) ;
00261     mean3 = cpl_vector_get_mean(vectors[3]) ;
00262     if (fabs(mean3) > 1)
00263         cpl_vector_multiply_scalar(vectors[3], fabs(mean1/mean3)) ;
00264 
00265     irplib_vectors_plot("set grid;set xlabel 'Wavelength (nm)';", title_loc,
00266         "", (const cpl_vector **)vectors, 4);
00267 
00268     /* Unscale the signal */
00269     if (fabs(mean3) > 1)
00270         cpl_vector_multiply_scalar(vectors[3], mean3/mean1) ;
00271 
00272     /* Loop on the brightest lines and zoom on them */
00273     sprintf(title_loc, 
00274 "t '%s - 1-Initial catalog/2-Corrected catalog/3-Observed (ZOOMED)' w lines",
00275         title) ;
00276     title_loc[1023] = (char)0 ;
00277     tmp_vec = cpl_vector_duplicate(vectors[2]) ;
00278     for (i=0 ; i<nblines ; i++) {
00279         /* Find the brightest line */
00280         if ((max = cpl_vector_get_max(tmp_vec)) <= 0.0) break ;
00281         for (j=0 ; i<nsamples ; j++) {
00282             if (cpl_vector_get(tmp_vec, j) == max) break ;
00283         }
00284         if (j-hsize_pix < 0) start_ind = 0 ;
00285         else start_ind = j-hsize_pix ;
00286         if (j+hsize_pix > nsamples-1) stop_ind = nsamples-1 ;
00287         else stop_ind = j+hsize_pix ;
00288         for (j=start_ind ; j<=stop_ind ; j++) cpl_vector_set(tmp_vec, j, 0.0) ;
00289 
00290         sub_vectors = cpl_malloc(4*sizeof(cpl_vector*)) ;
00291         sub_vectors[0]=cpl_vector_extract(vectors[0],start_ind,stop_ind,1);
00292         sub_vectors[1]=cpl_vector_extract(vectors[1],start_ind,stop_ind,1);
00293         sub_vectors[2]=cpl_vector_extract(vectors[2],start_ind,stop_ind,1);
00294         sub_vectors[3]=cpl_vector_extract(vectors[3],start_ind,stop_ind,1);
00295 
00296         irplib_vectors_plot("set grid;set xlabel 'Wavelength (nm)';", title_loc,
00297             "", (const cpl_vector **)sub_vectors, 4);
00298 
00299         cpl_vector_delete(sub_vectors[0]) ;
00300         cpl_vector_delete(sub_vectors[1]) ;
00301         cpl_vector_delete(sub_vectors[2]) ;
00302         cpl_vector_delete(sub_vectors[3]) ;
00303         cpl_free(sub_vectors) ;
00304     }
00305     cpl_vector_delete(tmp_vec) ;
00306     
00307     cpl_vector_unwrap(vectors[0]) ;
00308     cpl_vector_unwrap(vectors[1]) ;
00309     cpl_vector_unwrap(vectors[2]) ;
00310     cpl_vector_unwrap(vectors[3]) ;
00311     cpl_free(vectors) ;
00312 
00313     return 0 ;
00314 }
00315 
00316 /*----------------------------------------------------------------------------*/
00324 /*----------------------------------------------------------------------------*/
00325 int irplib_wlxcorr_catalog_plot(
00326         const cpl_bivector      *   cat,
00327         double                      wmin,
00328         double                      wmax) 
00329 {
00330     int                 start, stop ;
00331     cpl_bivector    *   subcat ;
00332     cpl_vector      *   subcat_x ;
00333     cpl_vector      *   subcat_y ;
00334     const double    *   pwave ;
00335     int                 nvals, nvals_tot ;
00336     int                 i ;
00337 
00338     /* Test entries */
00339     if (cat == NULL) return -1 ;
00340     if (wmax <= wmin) return -1 ;
00341 
00342     /* Initialise */
00343     nvals_tot = cpl_bivector_get_size(cat) ;
00344 
00345     /* Count the nb of values */
00346     pwave = cpl_bivector_get_x_data_const(cat) ;
00347     if (pwave[0] >= wmin) start = 0 ;
00348     else start = -1 ;
00349     if (pwave[nvals_tot-1] <= wmax) stop = nvals_tot-1 ;
00350     else stop = -1 ;
00351     i=0 ;
00352     while ((pwave[i] < wmin) && (i<nvals_tot-1)) i++ ;
00353     start = i ;
00354     i= nvals_tot-1 ;
00355     while ((pwave[i] > wmax) && (i>0)) i-- ;
00356     stop = i ;
00357 
00358     if (start>=stop) {
00359         cpl_msg_error(cpl_func, "Cannot plot the catalog") ;
00360         return -1 ;
00361     }
00362     nvals = start - stop + 1 ;
00363 
00364     /* Create the bivector to plot */
00365     subcat_x = cpl_vector_extract(cpl_bivector_get_x_const(cat),start,stop, 1) ;
00366     subcat_y = cpl_vector_extract(cpl_bivector_get_y_const(cat),start,stop, 1) ;
00367     subcat = cpl_bivector_wrap_vectors(subcat_x, subcat_y) ;
00368 
00369     /* Plot */
00370     irplib_bivector_plot(
00371             "set grid;set xlabel 'Wavelength (nm)';set ylabel 'Emission';",
00372             "t 'Catalog Spectrum' w lines", "", subcat);
00373     cpl_bivector_unwrap_vectors(subcat) ;
00374     cpl_vector_delete(subcat_x) ;
00375     cpl_vector_delete(subcat_y) ;
00376 
00377     return 0 ;
00378 }
00379    

Generated on Fri Apr 18 14:11:41 2008 for UVES Pipeline Reference Manual by  doxygen 1.5.1