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

0001 /***************************************************************************
0002  *                                                                         *
0003  *   copyright : (C) 2007 The University of Toronto                        *
0004  *                   netterfield@astro.utoronto.ca                         *
0005   *                                                                         *
0006  *   This program is free software; you can redistribute it and/or modify  *
0007  *   it under the terms of the GNU General Public License as published by  *
0008  *   the Free Software Foundation; either version 2 of the License, or     *
0009  *   (at your option) any later version.                                   *
0010  *                                                                         *
0011  ***************************************************************************/
0012 
0013 #ifndef FITS_COMMON_H
0014 #define FITS_COMMON_H
0015 
0016 #include <stdlib.h>
0017 #include <string.h>
0018 #include <math.h>
0019 #include <vector.h>
0020 
0021 #define XVALUES 0
0022 #define YVALUES 1
0023 #define WEIGHTS 2
0024 
0025 double interpolate(int iIndex, int iLengthDesired, const double* pArray, int iLengthActual) {
0026   double value;
0027   double fj;
0028   double fdj;
0029   int j;
0030 
0031   if (iLengthDesired == iLengthActual) {
0032     value =  pArray[iIndex];
0033   } else {
0034     fj    = (double)(iIndex * (iLengthActual-1)) / (double)(iLengthDesired-1);
0035     j     = (int)floor(fj);
0036     fdj   = fj - (double)j;
0037 
0038     //Don't read an invalid index from pArray... found by valgrind
0039     double A = j+1 < iLengthActual ? pArray[j+1] : 0;
0040     double B = j < iLengthActual ? pArray[j] : 0;
0041 
0042     value = A * fdj + B * (1.0 - fdj);
0043   }
0044 
0045   return value;
0046 }
0047 
0048 
0049 void assign( Kst::VectorPtr targetVector, double* pResult[], int iIndex, int iLength ) {
0050   for (int i = 0; i < iLength; i++) {
0051     targetVector->raw_V_ptr()[i] = pResult[iIndex][i];
0052   }
0053 }
0054 
0055 
0056 bool precursor( Kst::VectorPtr xVector, Kst::VectorPtr yVector, Kst::VectorPtr weightsVector,
0057                 int *piLength, bool bWeighted, bool bLowHigh, int iNumParams,
0058                 double* pInputs[], Kst::VectorPtr vectorOutYFitted, Kst::VectorPtr vectorOutYResiduals,
0059                 Kst::VectorPtr vectorOutYParameters, Kst::VectorPtr vectorOutYCovariance, Kst::VectorPtr vectorOutYLo,
0060                 Kst::VectorPtr vectorOutYHi) {
0061 
0062   bool bRetVal = false;
0063   int  iNumCovar = ( iNumParams * ( iNumParams + 1 ) ) / 2;
0064   int  i;
0065 
0066   pInputs[XVALUES] = 0L;
0067   pInputs[YVALUES] = 0L;
0068   if (bWeighted) {
0069     pInputs[WEIGHTS] = 0L;
0070   }
0071 
0072   if( xVector->length() >= 2 &&
0073       yVector->length() >= 2 &&
0074       (!bWeighted || weightsVector->length() >= 2) ) {
0075         *piLength = xVector->length();
0076         if(  yVector->length() > *piLength ) {
0077           *piLength =  yVector->length();
0078         }
0079 
0080     //
0081     // do any necessary interpolations...
0082     //
0083         pInputs[XVALUES] = (double*)malloc(*piLength * sizeof( double ));
0084 
0085         double const *v_x = xVector->noNanValue();
0086         double const *v_y = yVector->noNanValue();
0087 
0088         if (xVector->length() == *piLength) {
0089           for( i=0; i<*piLength; i++) {
0090             pInputs[XVALUES][i] = v_x[i];
0091           }
0092         } else {
0093           for( i=0; i<*piLength; i++) {
0094             pInputs[XVALUES][i] = interpolate( i, *piLength, v_x, xVector->length() );
0095           }
0096         }
0097 
0098         pInputs[YVALUES] = (double*)malloc(*piLength * sizeof( double ));
0099         if (yVector->length() == *piLength) {
0100           for( i=0; i<*piLength; i++) {
0101             pInputs[YVALUES][i] = v_y[i];
0102           }
0103         } else {
0104           for( i=0; i<*piLength; i++) {
0105             pInputs[YVALUES][i] = interpolate( i, *piLength, v_y, yVector->length() );
0106           }
0107         }
0108 
0109         if (bWeighted) {
0110           pInputs[WEIGHTS] = (double*)malloc(*piLength * sizeof( double ));
0111           if (weightsVector->length() == *piLength) {
0112             for( i=0; i<*piLength; i++) {
0113               pInputs[WEIGHTS][i] = weightsVector->value()[i];
0114             }
0115           } else {
0116             for( i=0; i<*piLength; i++) {
0117               pInputs[WEIGHTS][i] = interpolate( i, *piLength, weightsVector->value(), weightsVector->length() );
0118             }
0119           }
0120         }
0121 
0122     if( *piLength > iNumParams + 1 ) {
0123       vectorOutYFitted->resize(*piLength);
0124       vectorOutYResiduals->resize(*piLength);
0125       vectorOutYParameters->resize(iNumParams);
0126       vectorOutYCovariance->resize(iNumCovar);
0127       if( bLowHigh ) {
0128         vectorOutYLo->resize(*piLength);
0129         vectorOutYHi->resize(*piLength);
0130       }
0131 
0132       bRetVal = true;
0133     }
0134   }
0135   return bRetVal;
0136 }
0137 
0138 
0139 void postcursor( bool bWeighted, double* pInputs[]) {
0140   if (pInputs[XVALUES] != 0L ) {
0141     free( pInputs[XVALUES] );
0142   }
0143 
0144   if (pInputs[YVALUES] != 0L ) {
0145     free( pInputs[YVALUES] );
0146   }
0147 
0148   if( bWeighted ) {
0149     if (pInputs[WEIGHTS] != 0L ) {
0150       free( pInputs[WEIGHTS] );
0151     }
0152   }
0153 }
0154 
0155 #endif