irplib_wlxcorr.c

00001 /* $Id: irplib_wlxcorr.c,v 1.37 2007/07/24 09:15:31 yjung 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: yjung $
00023  * $Date: 2007/07/24 09:15:31 $
00024  * $Revision: 1.37 $
00025  * $Name: uves-3_4_5 $
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 
00051 /*----------------------------------------------------------------------------*/
00061 /*----------------------------------------------------------------------------*/
00062 int irplib_wlxcorr_plot_solution(
00063         const cpl_polynomial    *   init,
00064         const cpl_polynomial    *   comp,
00065         const cpl_polynomial    *   sol,
00066         int                         pix_start,
00067         int                         pix_stop) 
00068 {
00069     int                 nsamples, nplots ;
00070     cpl_vector      **  vectors ;
00071     cpl_bivector    *   bivector ;
00072     double              diff ;
00073     int                 i ;
00074     
00075     /* Test entries */
00076     if (init == NULL || comp == NULL) return -1 ;
00077     
00078     /* Initialise */
00079     nsamples = pix_stop - pix_start + 1 ;
00080     if (sol != NULL)    nplots = 3 ;
00081     else                nplots = 2 ;
00082     
00083     /* Create vectors */
00084     vectors = cpl_malloc((nplots+1)*sizeof(cpl_vector*)) ;
00085     for (i=0 ; i<nplots+1 ; i++) vectors[i] = cpl_vector_new(nsamples) ;
00086 
00087     /* First plot with the lambda/pixel relation */
00088     /* Fill vectors */
00089     for (i=0 ; i<nsamples ; i++) {
00090         cpl_vector_set(vectors[0], i, pix_start+i) ;
00091         cpl_vector_set(vectors[1], i, 
00092                 cpl_polynomial_eval_1d(init, (double)(pix_start+i), NULL)) ;
00093         cpl_vector_set(vectors[2], i, 
00094                 cpl_polynomial_eval_1d(comp, (double)(pix_start+i), NULL)) ;
00095         if (sol != NULL) 
00096             cpl_vector_set(vectors[3], i, 
00097                     cpl_polynomial_eval_1d(sol, (double)(pix_start+i), NULL)) ;
00098     }
00099 
00100     /* Plot */
00101     irplib_vectors_plot("set grid;set xlabel 'Position (pixels)';", 
00102         "t '1-Initial / 2-Computed / 3-Solution' w lines", 
00103         "", (const cpl_vector **)vectors, nplots+1);
00104 
00105     /* Free vectors */
00106     for (i=0 ; i<nplots+1 ; i++) cpl_vector_delete(vectors[i]) ;
00107     cpl_free(vectors) ;
00108 
00109     /* Allocate vectors */
00110     nplots -- ;
00111     vectors = cpl_malloc((nplots+1)*sizeof(cpl_vector*)) ;
00112     for (i=0 ; i<nplots+1 ; i++) vectors[i] = cpl_vector_new(nsamples) ;
00113     
00114     /* Second plot with the delta-lambda/pixel relation */
00115     /* Fill vectors */
00116     for (i=0 ; i<nsamples ; i++) {
00117         cpl_vector_set(vectors[0], i, pix_start+i) ;
00118         diff = cpl_polynomial_eval_1d(comp, (double)(pix_start+i), NULL) -
00119             cpl_polynomial_eval_1d(init, (double)(pix_start+i), NULL) ;
00120         cpl_vector_set(vectors[1], i, diff) ;
00121         if (sol != NULL) {
00122             diff = cpl_polynomial_eval_1d(sol, (double)(pix_start+i), NULL) -
00123                 cpl_polynomial_eval_1d(init, (double)(pix_start+i), NULL) ;
00124             cpl_vector_set(vectors[2], i, diff) ;
00125         }
00126     }
00127 
00128     /* Plot */
00129     if (sol == NULL) {
00130         bivector = cpl_bivector_wrap_vectors(vectors[0], vectors[1]) ;
00131         irplib_bivector_plot(
00132 "set grid;set xlabel 'Position (pixels)';set ylabel 'Wavelength difference';", 
00133             "t 'Computed-Initial wavelenth' w lines", "", bivector);
00134         cpl_bivector_unwrap_vectors(bivector) ;
00135     } else {
00136         irplib_vectors_plot("set grid;set xlabel 'Position (pixels)';", 
00137             "t '1-Computed - Initial / 2--Solution - Initial' w lines", 
00138             "", (const cpl_vector **)vectors, nplots+1);
00139     }
00140     
00141     /* Free vectors */
00142     for (i=0 ; i<nplots+1 ; i++) cpl_vector_delete(vectors[i]) ;
00143     cpl_free(vectors) ;
00144 
00145     /* Return */
00146     return 0 ;
00147 }
00148 
00149 /*----------------------------------------------------------------------------*/
00156 /*----------------------------------------------------------------------------*/
00157 int irplib_wlxcorr_plot_spc_table(
00158         const cpl_table     *   spc_table, 
00159         const char          *   title) 
00160 {
00161     char                title_loc[1024] ;
00162     cpl_vector      **  vectors ;
00163     cpl_vector      **  sub_vectors ;
00164     cpl_vector      *   tmp_vec ;
00165     int                 nsamples ;
00166     double              hsize_nm, max, mean1, mean3 ;
00167     int                 start_ind, stop_ind, nblines, hsize_pix ;
00168     int                 i, j ;
00169 
00170     /* Test entries */
00171     if (spc_table == NULL) return -1 ;
00172     
00173     /* Initialise */
00174     nsamples = cpl_table_get_nrow(spc_table) ;
00175     hsize_nm = 0.2 ;
00176     hsize_pix = 10 ;
00177     nblines = 0 ;
00178     sprintf(title_loc, 
00179         "t '%s - 1-Initial catalog/2-Corrected catalog/3-Observed' w lines",
00180         title) ;
00181     title_loc[1023] = (char)0 ;
00182     
00183     vectors = cpl_malloc(4*sizeof(cpl_vector*)) ;
00184     vectors[0] = cpl_vector_wrap(nsamples, 
00185             cpl_table_get_data_double((cpl_table*)spc_table,
00186                 CPL_WLCALIB_XC_COL_WAVELENGTH));
00187     vectors[1] = cpl_vector_wrap(nsamples, 
00188             cpl_table_get_data_double((cpl_table*)spc_table, 
00189                 CPL_WLCALIB_XC_COL_CAT_INIT));
00190     vectors[2] = cpl_vector_wrap(nsamples, 
00191             cpl_table_get_data_double((cpl_table*)spc_table, 
00192                 CPL_WLCALIB_XC_COL_CAT_FINAL));
00193     vectors[3] = cpl_vector_wrap(nsamples, 
00194             cpl_table_get_data_double((cpl_table*)spc_table, 
00195                 CPL_WLCALIB_XC_COL_OBS)) ;
00196 
00197     /* Scale the signal for a bettre display */
00198     mean1 = cpl_vector_get_mean(vectors[1]) ;
00199     mean3 = cpl_vector_get_mean(vectors[3]) ;
00200     if (fabs(mean3) > 1)
00201         cpl_vector_multiply_scalar(vectors[3], fabs(mean1/mean3)) ;
00202 
00203     irplib_vectors_plot("set grid;set xlabel 'Wavelength (nm)';", title_loc,
00204         "", (const cpl_vector **)vectors, 4);
00205 
00206     /* Unscale the signal */
00207     if (fabs(mean3) > 1)
00208         cpl_vector_multiply_scalar(vectors[3], mean3/mean1) ;
00209 
00210     /* Loop on the brightest lines and zoom on them */
00211     sprintf(title_loc, 
00212 "t '%s - 1-Initial catalog/2-Corrected catalog/3-Observed (ZOOMED)' w lines",
00213         title) ;
00214     title_loc[1023] = (char)0 ;
00215     tmp_vec = cpl_vector_duplicate(vectors[2]) ;
00216     for (i=0 ; i<nblines ; i++) {
00217         /* Find the brightest line */
00218         if ((max = cpl_vector_get_max(tmp_vec)) <= 0.0) break ;
00219         for (j=0 ; i<nsamples ; j++) {
00220             if (cpl_vector_get(tmp_vec, j) == max) break ;
00221         }
00222         if (j-hsize_pix < 0) start_ind = 0 ;
00223         else start_ind = j-hsize_pix ;
00224         if (j+hsize_pix > nsamples-1) stop_ind = nsamples-1 ;
00225         else stop_ind = j+hsize_pix ;
00226         for (j=start_ind ; j<=stop_ind ; j++) cpl_vector_set(tmp_vec, j, 0.0) ;
00227 
00228         sub_vectors = cpl_malloc(4*sizeof(cpl_vector*)) ;
00229         sub_vectors[0]=cpl_vector_extract(vectors[0],start_ind,stop_ind,1);
00230         sub_vectors[1]=cpl_vector_extract(vectors[1],start_ind,stop_ind,1);
00231         sub_vectors[2]=cpl_vector_extract(vectors[2],start_ind,stop_ind,1);
00232         sub_vectors[3]=cpl_vector_extract(vectors[3],start_ind,stop_ind,1);
00233 
00234         irplib_vectors_plot("set grid;set xlabel 'Wavelength (nm)';", title_loc,
00235             "", (const cpl_vector **)sub_vectors, 4);
00236 
00237         cpl_vector_delete(sub_vectors[0]) ;
00238         cpl_vector_delete(sub_vectors[1]) ;
00239         cpl_vector_delete(sub_vectors[2]) ;
00240         cpl_vector_delete(sub_vectors[3]) ;
00241         cpl_free(sub_vectors) ;
00242     }
00243     cpl_vector_delete(tmp_vec) ;
00244     
00245     cpl_vector_unwrap(vectors[0]) ;
00246     cpl_vector_unwrap(vectors[1]) ;
00247     cpl_vector_unwrap(vectors[2]) ;
00248     cpl_vector_unwrap(vectors[3]) ;
00249     cpl_free(vectors) ;
00250 
00251     return 0 ;
00252 }
00253 
00254 /*----------------------------------------------------------------------------*/
00262 /*----------------------------------------------------------------------------*/
00263 int irplib_wlxcorr_catalog_plot(
00264         const cpl_bivector      *   cat,
00265         double                      wmin,
00266         double                      wmax) 
00267 {
00268     int                 start, stop ;
00269     cpl_bivector    *   subcat ;
00270     cpl_vector      *   subcat_x ;
00271     cpl_vector      *   subcat_y ;
00272     const double    *   pwave ;
00273     int                 nvals, nvals_tot ;
00274     int                 i ;
00275 
00276     /* Test entries */
00277     if (cat == NULL) return -1 ;
00278     if (wmax <= wmin) return -1 ;
00279 
00280     /* Initialise */
00281     nvals_tot = cpl_bivector_get_size(cat) ;
00282 
00283     /* Count the nb of values */
00284     pwave = cpl_bivector_get_x_data_const(cat) ;
00285     if (pwave[0] >= wmin) start = 0 ;
00286     else start = -1 ;
00287     if (pwave[nvals_tot-1] <= wmax) stop = nvals_tot-1 ;
00288     else stop = -1 ;
00289     i=0 ;
00290     while ((pwave[i] < wmin) && (i<nvals_tot-1)) i++ ;
00291     start = i ;
00292     i= nvals_tot-1 ;
00293     while ((pwave[i] > wmax) && (i>0)) i-- ;
00294     stop = i ;
00295 
00296     if (start>=stop) {
00297         cpl_msg_error(cpl_func, "Cannot plot the catalog") ;
00298         return -1 ;
00299     }
00300     nvals = start - stop + 1 ;
00301 
00302     /* Create the bivector to plot */
00303     subcat_x = cpl_vector_extract(cpl_bivector_get_x_const(cat),start,stop, 1) ;
00304     subcat_y = cpl_vector_extract(cpl_bivector_get_y_const(cat),start,stop, 1) ;
00305     subcat = cpl_bivector_wrap_vectors(subcat_x, subcat_y) ;
00306 
00307     /* Plot */
00308     irplib_bivector_plot(
00309             "set grid;set xlabel 'Wavelength (nm)';set ylabel 'Emission';",
00310             "t 'Catalog Spectrum' w lines", "", subcat);
00311     cpl_bivector_unwrap_vectors(subcat) ;
00312     cpl_vector_delete(subcat_x) ;
00313     cpl_vector_delete(subcat_y) ;
00314 
00315     return 0 ;
00316 }
00317    

Generated on Thu Nov 15 14:32:26 2007 for UVES Pipeline Reference Manual by  doxygen 1.5.1