File indexing completed on 2024-12-22 04:18:05

0001 /***************************************************************************
0002  *                                                                         *
0003  *   copyright : (C) 2007 The University of Toronto                        *
0004  *                   netterfield@astro.utoronto.ca                         *
0005  *   copyright : (C) 2005  University of British Columbia                  *
0006  *                   dscott@phas.ubc.ca                                    *
0007  *                                                                         *
0008  *   This program is free software; you can redistribute it and/or modify  *
0009  *   it under the terms of the GNU General Public License as published by  *
0010  *   the Free Software Foundation; either version 2 of the License, or     *
0011  *   (at your option) any later version.                                   *
0012  *                                                                         *
0013  ***************************************************************************/
0014 
0015 #ifndef INTERPOLATIONS_H
0016 #define INTERPOLATIONS_H
0017 
0018 #include <vector.h>
0019 
0020 bool interpolate(Kst::VectorPtr xVector,
0021                  Kst::VectorPtr yVector,
0022                  Kst::VectorPtr x1Vector,
0023                  Kst::VectorPtr yOutVector,
0024                  const gsl_interp_type* pType) {
0025 
0026   gsl_interp_accel *pAccel = NULL;
0027   gsl_interp *pInterp = NULL;
0028   gsl_spline *pSpline = NULL;
0029   int iLengthData;
0030   int iLengthInterp;
0031   bool bReturn = false;
0032   double* pResult[1];
0033 
0034   iLengthData = xVector->length();
0035   if (yVector->length() < iLengthData) {
0036     iLengthData = yVector->length();
0037   }
0038 
0039   iLengthInterp = x1Vector->length();
0040   if (iLengthInterp > 0) {
0041     if (yOutVector->length() != iLengthInterp) {
0042       yOutVector->resize(iLengthInterp, true);
0043       pResult[0] = (double*)realloc( yOutVector->raw_V_ptr(), iLengthInterp * sizeof( double ) );
0044     } else {
0045       pResult[0] = yOutVector->raw_V_ptr();
0046     }
0047 
0048     if (pResult[0] != NULL) {
0049 
0050       for (int i = 0; i < iLengthInterp; ++i) {
0051         yOutVector->raw_V_ptr()[i] = pResult[0][i];
0052       }
0053 
0054       pInterp = gsl_interp_alloc( pType, iLengthData );
0055       if (pInterp != NULL) {
0056         //
0057         // check that we have enough data points...
0058         //
0059         if ((unsigned int)iLengthData > gsl_interp_min_size( pInterp )) {
0060           pAccel  = gsl_interp_accel_alloc( );
0061           if (pAccel != NULL) {
0062             pSpline = gsl_spline_alloc( pType, iLengthData );
0063             if (pSpline != NULL) {
0064               if (!gsl_spline_init( pSpline, xVector->noNanValue(), yVector->noNanValue(), iLengthData )) {
0065                 double const *xV1 = x1Vector->noNanValue();
0066                 double *yVout = yOutVector->raw_V_ptr();
0067                 for( int i=0; i<iLengthInterp; i++) {
0068                   yVout[i] = gsl_spline_eval( pSpline, xV1[i], pAccel );
0069                 }
0070 
0071                 bReturn = true;
0072               }
0073               gsl_spline_free( pSpline );
0074             }
0075             gsl_interp_accel_free( pAccel );
0076           }
0077         }
0078         gsl_interp_free( pInterp );
0079       }
0080     }
0081   }
0082 
0083   return bReturn;
0084 }
0085 
0086 #endif