Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members  

real_interp.cc

Go to the documentation of this file.
00001 // real_interp.cc
00002 // SuperMix version 1.0  C++ source file
00003 //
00004 // Copyright (c) 1999 California Institute of Technology.
00005 // All rights reserved.
00006 //
00007 // Redistribution and use in source and binary forms for noncommercial
00008 // purposes are permitted provided that the above copyright notice and
00009 // this paragraph are duplicated in all such forms and that any
00010 // documentation and other materials related to such distribution and
00011 // use acknowledge that the software was developed by California
00012 // Institute of Technology. Redistribution and/or use in source or
00013 // binary forms is not permitted for any commercial purpose. Use of
00014 // this software does not include a permitted use of the Institute's
00015 // name or trademark for any purpose.
00016 //
00017 // DISCLAIMER:
00018 // THIS SOFTWARE AND/OR RELATED MATERIALS ARE PROVIDED "AS-IS" WITHOUT
00019 // WARRANTY OF ANY KIND INCLUDING ANY WARRANTIES OF PERFORMANCE OR
00020 // MERCHANTABILITY OR FITNESS FOR A PARTICULAR USE OR PURPOSE (AS SET
00021 // FORTH IN UCC 23212-2313) OR FOR ANY PURPOSE WHATSOEVER, FOR THE
00022 // LICENSED PRODUCT, HOWEVER USED.  IN NO EVENT SHALL CALTECH/JPL BE
00023 // LIABLE FOR ANY DAMAGES AND/OR COSTS, INCLUDING BUT NOT LIMITED TO
00024 // INCIDENTAL OR CONSEQUENTIAL DAMAGES OF ANY KIND, INCLUDING ECONOMIC
00025 // DAMAGE OR INJURY TO PROPERTY AND LOST PROFITS, REGARDLESS OF
00026 // WHETHER CALTECH/JPL SHALL BE ADVISED, HAVE REASON TO KNOW, OR IN
00027 // FACT SHALL KNOW OF THE POSSIBILITY.  THE USER BEARS ALL RISK
00028 // RELATING TO QUALITY AND PERFORMANCE OF THE SOFTWARE AND/OR RELATED
00029 // MATERIALS.
00030 //
00031 // ********************************************************************
00032 // 7/6/99:   Now derived from interpolator<double>; using real_matrix;
00033 //           added new constructor and table().
00034 // 4/20/99:  Modified to use interpolator<double>
00035 
00036 #include <math.h>
00037 #include "global.h"
00038 #include "error.h"
00039 #include "parameter/abstract_real_parameter.h"
00040 #include "datafile.h"
00041 #include "real_interp.h"
00042 #include "io.h"
00043 
00044 // ----------------------------------------------
00045 // Constructing the real_interp object
00046 
00047 // Constructors with no supplied data
00048 //
00049 real_interp::real_interp(const abstract_real_parameter &xp, 
00050                          real_interp::phase_type ptype) :
00051   interpolator<double>(), xparam(&xp), phase(ptype)
00052 { }
00053 
00054 real_interp::real_interp(real_interp::phase_type ptype) :
00055   interpolator<double>(), xparam(0), phase(ptype)
00056 { }
00057 
00058 
00059 // Constructors from input file or stream
00060 //
00061 
00062 real_interp::real_interp(const abstract_real_parameter &xp,
00063                          const char * const name, double x_unit,
00064                          double y_unit, int ycol, bool ignore_ws, 
00065                          real_interp::phase_type ptype ) :
00066   interpolator<double>(), xparam(&xp), phase(ptype)
00067 { file(name, x_unit, y_unit, ycol, ignore_ws, ptype); }
00068 
00069 real_interp::real_interp(const abstract_real_parameter &xp,
00070                          istream & s, double x_unit,
00071                          double y_unit, int ycol, bool ignore_ws, 
00072                          real_interp::phase_type ptype ) :
00073   interpolator<double>(), xparam(&xp), phase(ptype)
00074 { file(s, x_unit, y_unit, ycol, ignore_ws, ptype); }
00075 
00076 
00077 // Constructor from datafile
00078 //
00079 real_interp::real_interp(const datafile &df, int xcol, int ycol, 
00080                          const abstract_real_parameter &xp, 
00081                          real_interp::phase_type ptype) :
00082   interpolator<double>(), xparam(&xp), phase(ptype)
00083 {
00084   construct(*(df.table()), xcol, ycol) ;
00085 }
00086 
00087 
00088 // Constructor from real_table
00089 //
00090 real_interp::real_interp(const real_table &rt, int xrow, int yrow, 
00091                          const abstract_real_parameter &xp, 
00092                          real_interp::phase_type ptype) :
00093   interpolator<double>(), xparam(&xp), phase(ptype)
00094 {
00095   construct(rt, xrow, yrow) ;
00096 }
00097 
00098 
00099 // This does the real construction work
00100 //
00101 void real_interp::construct(const real_table &rt, int xrow, int yrow)
00102 {
00103   int npts = rt.Rmaxindex()-rt.Rminindex()+1 ; // how many data points ?
00104   int nrow = rt.Lmaxindex()-rt.Lminindex()+1 ; // how many rows ?
00105 
00106   // see if the arguments are reasonable
00107 
00108   if(npts < 1 || nrow < 1) {
00109     error::warning(
00110   "in real_interp constructor: table contains no data !") ;
00111     return ;
00112   }
00113   if(npts < 2) {
00114     error::warning(
00115 "in real_interp constructor: table contains insufficient data!") ;
00116     return ;
00117   }
00118   if(nrow < 2) {
00119     error::warning(
00120     "in real_interp constructor: table contains only one row!") ;
00121     return ;
00122   }
00123   if(xrow < rt.Lminindex() || xrow > rt.Lmaxindex()) {
00124     error::warning(
00125     "in real_interp constructor: x column number out of range !") ;
00126     return ;
00127   }
00128   if(yrow < rt.Lminindex() || yrow > rt.Lmaxindex()) {
00129     error::warning(
00130     "in real_interp constructor: y column number out of range !") ;
00131     return ;
00132   }
00133 
00134   // passed the tests, so...
00135   // add the data points to the interpolator
00136 
00137   double xval, yval ;
00138 
00139   for(int icol=rt.Rminindex(); icol<=rt.Rmaxindex(); icol++) {
00140     xval = rt[xrow][icol] ;
00141     yval = rt[yrow][icol] ;
00142     add(xval, yval) ;
00143   }
00144 
00145   // now adjust y values if they are phase data:
00146   if (phase == PHASE) {
00147     double yprev = 0.0 , ydelta ;
00148     for(data_iter i = data.begin(); i != data.end(); ++i) {
00149       ydelta = i->second - yprev ;
00150       ydelta = phase_adjust(ydelta) ;  // force delta(phase) to -Pi, Pi
00151       i->second = yprev += ydelta ;
00152     }
00153   }
00154 
00155   // finally build the interpolator:
00156   build() ;  
00157 }
00158 
00159 
00160 // ----------------------------------------------
00161 // read in data from an input stream or file
00162 
00163 real_interp & 
00164 real_interp::file(istream & s, double xu, double yu, int yc,
00165                   bool ws, real_interp::phase_type ptype)
00166 {
00167   phase = ptype;
00168 
00169   if(!s) {
00170     error::warning("real_interp: couldn't read from input.");
00171     return *this;
00172   }
00173 
00174   data_parser d(s);
00175   unsigned n = d.skip_parse();
00176   if(!n) {
00177     error::warning("real_interp: no data found in input.");
00178     return *this;
00179   }
00180 
00181   while(n) {
00182     double x;
00183     real_vector y;
00184     int m;
00185 
00186     m = d.convert(x, y, xu, yu);
00187     if(m < yc) {
00188       error::warning("real_interp: input stream doesn't have enough columns of data.");
00189     }
00190     else {
00191       add(x,y.read(yc));
00192     }
00193 
00194     n = (ws) ? d.skip_parse() : d.parse();
00195   }
00196 
00197   // now adjust y values if they are phase data:
00198   if (phase == PHASE) {
00199     double yprev = 0.0 , ydelta ;
00200     for(data_iter i = data.begin(); i != data.end(); ++i) {
00201       ydelta = i->second - yprev ;
00202       ydelta = phase_adjust(ydelta) ;  // force delta(phase) to -Pi, Pi
00203       i->second = yprev += ydelta ;
00204     }
00205   }
00206 
00207   // finally build the interpolator:
00208   build();
00209   return *this;
00210 }
00211 
00212 
00213 real_interp & 
00214 real_interp::file(const char * const name, double xu, double yu, int yc,
00215                   bool ws, real_interp::phase_type ptype)
00216 {
00217   phase = ptype;
00218 
00219   ifstream f(name);
00220   if(!f) {
00221     error::warning("real_interp: couldn't open file", name);
00222     return *this;
00223   }
00224   file(f,xu,yu,yc,ws,ptype);
00225   return *this;
00226 }
00227 
00228 
00229 // ----------------------------------------------
00230 // Return an interpolated value:
00231 // Define the get() virtual function for abstract_real_parameter
00232 //
00233 double real_interp::get() const
00234 {
00235   if(!(ready() && xparam)) {
00236     error::warning(
00237     "attempted use of a real_interp object that was not properly constructed");
00238     return(0.) ;
00239   }
00240 
00241   double xval = xparam->get() ;  // find out x value
00242   double yval = (*this)(xval) ;
00243 
00244   if (phase==PHASE) {
00245     yval = phase_adjust(yval) ;  // force range to -Pi, Pi
00246   }
00247 
00248   return(yval) ;
00249 }
00250 
00251 
00252 double real_interp::phase_adjust(double phase) const
00253 {
00254   return( phase - 2.*Pi*floor((phase+Pi)/(2.*Pi))) ;
00255 }
00256 

Please direct comments and corrections to supermix@submm.caltech.edu
Go to the supermix home page
Generated by doxygen1.2.7