UFO
PiecewiseLinearInterpolation.cc
Go to the documentation of this file.
1 /*
2  * (C) Copyright 2020 Met Office UK
3  *
4  * This software is licensed under the terms of the Apache Licence Version 2.0
5  * which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
6  */
7 
8 #include <stdexcept>
9 #include <utility>
10 
13 
14 namespace ufo {
15 
17  std::vector<double> sortedAbscissas, std::vector<double> ordinates) {
18  if (sortedAbscissas.empty())
19  throw std::invalid_argument("At least one interpolation point must be provided");
20 
21  if (sortedAbscissas.size() != ordinates.size())
22  throw std::invalid_argument("The number of abscissas must be the same as that of ordinates");
23 
24  abscissas_ = std::move(sortedAbscissas);
25  ordinates_ = std::move(ordinates);
26 }
27 
28 double PiecewiseLinearInterpolation::operator()(double abscissa) const {
29  return interpolate(abscissas_, ordinates_, abscissa);
30 }
31 
32 double PiecewiseLinearInterpolation::interpolate(const std::vector<double> &sortedAbscissas,
33  const std::vector<double> &ordinates,
34  double abscissa) {
35  if (sortedAbscissas.size() == 1) {
36  // The Fortran functions don't handle this case correctly.
37  return ordinates[0];
38  }
39 
40  int wi = 0;
41  double wf = 0.0;
42  vert_interp_weights_f90(sortedAbscissas.size(), abscissa, sortedAbscissas.data(), wi, wf);
43 
44  double f = 0.0;
45  vert_interp_apply_f90(ordinates.size(), ordinates.data(), f, wi, wf);
46 
47  return f;
48 }
49 
50 } // namespace ufo
static double interpolate(const std::vector< double > &sortedAbscissas, const std::vector< double > &ordinates, double abscissa)
Convenience function interpolating the data points (sortedAbscissas[i], ordinates[i]) at abscissa wit...
double operator()(double abscissa) const
Evaluate the interpolated function at abscissa.
PiecewiseLinearInterpolation(std::vector< double > sortedAbscissas, std::vector< double > ordinates)
Create an object representing a piecewise linear interpolation of the data points (sortedAbscissas[i]...
logical, parameter f
Definition: RunCRTM.h:27
void vert_interp_apply_f90(const int &nlev, const double *fvec, double &f, const int &wi, const double &wf)
void vert_interp_weights_f90(const int &nlev, const double &obl, const double *vec, int &wi, double &wf)
Interface to Fortran vertical interpolation routines.