57int main(
int argc,
char **argv) {
59 string in_file, out_file, grid, fitgrid, comment, type, boundaries;
63 po::options_description desc(
"Allowed options");
65 desc.add_options()(
"help",
"produce this help message")(
66 "in", po::value<string>(&in_file),
"table to read")(
67 "out", po::value<string>(&out_file),
"table to write")(
68 "derivative", po::value<string>(),
"table to write")(
69 "grid", po::value<string>(&grid),
70 "new grid spacing (min:step:max). If 'grid' is specified only, "
71 "interpolation is performed.")(
72 "type", po::value<string>(&type)->default_value(
"akima"),
73 "[cubic|akima|linear]. If option is not specified, the default type "
74 "'akima' is assumed.")(
"fitgrid", po::value<string>(&fitgrid),
75 "specify fit grid (min:step:max). If 'grid' and "
76 "'fitgrid' are specified, a fit is performed.")(
78 "Option for fitgrid: Normally, values out of fitgrid boundaries are cut "
79 "off. If they shouldn't, choose --nocut.")(
80 "comment", po::value<string>(&comment),
81 "store a comment in the output table")(
82 "boundaries", po::value<string>(&boundaries),
83 "(natural|periodic|derivativezero) sets boundary conditions");
87 po::store(po::parse_command_line(argc, argv, desc), vm);
89 }
catch (po::error &err) {
90 cout <<
"error parsing command line: " << err.what() << endl;
96 if (vm.count(
"help")) {
105 if (!(vm.count(
"grid") || vm.count(
"fitgrid"))) {
106 cout <<
"Need grid for interpolation or fitgrid for fit.\n";
110 if ((!vm.count(
"grid")) && vm.count(
"fitgrid")) {
111 cout <<
"Need a grid for fitting as well.\n";
115 double min, max, step;
118 if (toks.size() != 3) {
119 cout <<
"wrong range format, use min:step:max\n";
123 step = stod(toks[1]);
128 std::unique_ptr<Spline> spline;
129 if (vm.count(
"type")) {
130 if (type ==
"cubic") {
131 spline = std::make_unique<CubicSpline>(
CubicSpline());
132 }
else if (type ==
"akima") {
133 spline = std::make_unique<AkimaSpline>(
AkimaSpline());
134 }
else if (type ==
"linear") {
135 spline = std::make_unique<LinSpline>(
LinSpline());
137 throw std::runtime_error(
"unknown spline type:" + type);
142 if (vm.count(
"boundaries")) {
143 if (boundaries ==
"periodic") {
146 if (boundaries ==
"derivativezero") {
153 if (vm.count(
"fitgrid")) {
155 if (toks.size() != 3) {
156 cout <<
"wrong range format in fitgrid, use min:step:max\n";
159 double sp_min, sp_max, sp_step;
160 sp_min = stod(toks[0]);
161 sp_step = stod(toks[1]);
162 sp_max = stod(toks[2]);
163 cout <<
"doing " << type <<
" fit " << sp_min <<
":" << sp_step <<
":"
168 Eigen::VectorXd x_copy;
169 Eigen::VectorXd y_copy;
170 if (!vm.count(
"nocut")) {
174 if (in.
x(i) < sp_min) {
177 if (in.
x(i) <= sp_max) {
183 x_copy = Eigen::VectorXd::Zero(maxindex - minindex + 1);
184 y_copy = Eigen::VectorXd::Zero(maxindex - minindex + 1);
186 x_copy(i - minindex) = in.
x(i);
187 y_copy(i - minindex) = in.
y(i);
192 spline->GenerateGrid(sp_min, sp_max, sp_step);
194 if (vm.count(
"nocut")) {
195 spline->Fit(in.
x(), in.
y());
197 spline->Fit(x_copy, y_copy);
199 }
catch (
const char *message) {
200 if (strcmp(
"qrsolve_zero_column_in_matrix", message)) {
201 throw std::runtime_error(
202 "error in Linalg::linalg_qrsolve : Not enough data for fit, "
203 "please adjust grid (zero row in fit matrix)");
204 }
else if (strcmp(
"constrained_qrsolve_zero_column_in_matrix",
206 throw std::runtime_error(
207 "error in Linalg::linalg_constrained_qrsolve : Not enough data "
208 "for fit, please adjust grid (zero row in fit matrix)");
210 throw std::runtime_error(
211 "Unknown error in csg_resample while fitting.");
217 spline->Interpolate(in.
x(), in.
y());
218 }
catch (
const char *message) {
219 if (strcmp(
"qrsolve_zero_column_in_matrix", message)) {
220 throw std::runtime_error(
221 "error in Linalg::linalg_qrsolve : Not enough data, please "
222 "adjust grid (zero row in fit matrix)");
223 }
else if (strcmp(
"constrained_qrsolve_zero_column_in_matrix",
225 throw std::runtime_error(
226 "error in Linalg::linalg_constrained_qrsolve : Not enough data, "
227 "please adjust grid (zero row in fit matrix)");
229 throw std::runtime_error(
230 "Unknown error in csg_resample while interpolating.");
236 out.
y() = spline->Calculate(out.
x());
239 if (vm.count(
"comment")) {
242 out.
flags() = std::vector<char>(out.
flags().size(),
'o');
245 der.
flags() = std::vector<char>(der.
flags().size(),
'o');
248 for (i = 0; out.
x(i) < in.
x(0) && i < out.
size(); ++i) {
253 for (; i < out.
size(); ++i) {
254 for (; j < in.
size(); ++j) {
255 if (in.
x(j) >= out.
x(i) ||
256 std::abs(in.
x(j) - out.
x(i)) < 1
e-12) {
260 if (in.
size() == j) {
269 if (vm.count(
"derivative")) {
270 der.
y() = spline->CalculateDerivative(der.
x());
272 der.
Save(vm[
"derivative"].as<string>());
275 }
catch (std::exception &error) {
276 cerr <<
"an error occurred:\n" << error.what() << endl;