icedb  version 0.5.1
Snow particle scattering database API
Functions
main.cpp File Reference
#include <icedb/defs.h>
#include <boost/lexical_cast.hpp>
#include <boost/program_options.hpp>
#include <exception>
#include <iostream>
#include <icedb/units/units.hpp>
#include <icedb/error.hpp>
Include dependency graph for main.cpp:

Go to the source code of this file.

Functions

int main (int argc, char **argv)
 This is a program that converts units. More...
 

Function Documentation

◆ main()

int main ( int  argc,
char **  argv 
)

This is a program that converts units.

Definition at line 10 of file main.cpp.

References ICEDB_throw, icedb::units::converter::isValid(), and icedb::error::xBadInput.

10  {
11  using namespace std;
12  int retval = 0;
13  try {
14  using namespace icedb;
15  namespace po = boost::program_options;
16 
17  po::options_description desc("Allowed options"), cmdline("Command-line options"),
18  config("Config options"), hidden("Hidden options"), oall("all options");
19 
20  cmdline.add_options()
21  ("help,h", "produce help message")
22  ("input,i", po::value< double >(), "Input quantity")
23  ("input-units,u", po::value< string >(), "Input units")
24  ("output-units,o", po::value< string >(), "Output units")
25  ("spec", "Perform spectral interconversion.")
26  ;
27 
28  po::positional_options_description p;
29  p.add("input", 1);
30  p.add("input-units", 1);
31  p.add("output-units", 1);
32 
33  desc.add(cmdline).add(config);
34  oall.add(cmdline).add(config).add(hidden);
35 
36  po::variables_map vm;
37  po::store(po::command_line_parser(argc, argv).
38  options(oall).positional(p).run(), vm);
39  po::notify(vm);
40 
41  auto doHelp = [&desc](const std::string &msg) {
42  std::cerr << msg << std::endl << desc << std::endl;
43  exit(1);
44  };
45  if (vm.count("help")) doHelp("");
46 
47  double inVal, outVal;
48  string inUnits, outUnits;
49  bool isSpec = false;
50 
51  if (vm.count("spec")) isSpec = true;
52  if (vm.count("input")) inVal = vm["input"].as<double>();
53  else {
54  std::string temp;
55  cout << "Specify input number (without units): ";
56  std::getline(cin, temp);
57  try {
58  inVal = boost::lexical_cast<double>(temp);
59  }
60  catch (boost::bad_lexical_cast) {
62  .add<std::string>("Reason", "Cannot parse input number")
63  .add<string>("Input _Number_", temp);
64  }
65  }
66  if (vm.count("input-units")) inUnits = vm["input-units"].as<string>();
67  else {
68  cout << "Specify input units (terminate with 'enter'): ";
69  std::getline(cin, inUnits);
70  if (!inUnits.size()) doHelp("Need to specify input units.");
71  }
72  if (vm.count("output-units")) outUnits = vm["output-units"].as<string>();
73  else{
74  cout << "Specify output units (terminate with 'enter'): ";
75  std::getline(cin, outUnits);
76  if (!outUnits.size()) doHelp("Need to specify output units.");
77  }
78  bool hasLenUnits = false;
79  bool hasFreqUnits = false;
80  if (icedb::units::converter(inUnits, "m").isValid()) hasLenUnits = true;
81  if (icedb::units::converter(outUnits, "m").isValid()) hasLenUnits = true;
82  if (icedb::units::converter(inUnits, "Hz").isValid()) hasFreqUnits = true;
83  if (icedb::units::converter(outUnits, "Hz").isValid()) hasFreqUnits = true;
84 
85  if (!isSpec && hasLenUnits && hasFreqUnits) {
86  //if ((!vm.count("input") || !vm.count("input-units") || !vm.count("output-units")) && !isSpec) {
87  cout << "Is this an in-vacuo spectral unit conversion (i.e. GHz to mm) [yes]? ";
88  string temp;
89  std::getline(cin, temp);
90  if (temp.size()) {
91  char c = temp.at(0);
92  switch (c) {
93  case 'N': case 'n': case 'f': case 'F': case '0':
94  isSpec = false;
95  break;
96  case 'Y': case 'y': case 't': case 'T': case '1':
97  isSpec = true;
98  break;
99  default:
101  .add<std::string>("Reason", "Cannot parse boolean")
102  .add<string>("Input _Bool_", temp);
103  }
104  }
105  else isSpec = true;
106  }
107 
108  std::shared_ptr<icedb::units::converter> cnv;
109 
110  if (isSpec) {
111  cnv = std::shared_ptr<icedb::units::converter>(new icedb::units::conv_spec(inUnits, outUnits));
112  }
113  else {
114  cnv = std::shared_ptr<icedb::units::converter>(new icedb::units::converter(inUnits, outUnits));
115  }
116  if (cnv->isValid()) {
117  outVal = cnv->convert(inVal);
118  cout << outVal << endl;
119  }
120  else cerr << "Conversion is invalid or unhandled for \"" << inUnits << "\" to \"" << outUnits << "\"." << endl;
121  }
122  catch (std::exception &e) {
123  cerr << "An exception has occurred: " << e.what() << endl;
124  retval = 2;
125  }
126  return retval;
127 }
bool isValid() const
Is this conversion valid?
Definition: units.cpp:48
STL namespace.
#define ICEDB_throw(x)
Definition: error.hpp:88
Perform interconversions between frequency, wavelength and wavenumber (GHz, Hz, m, cm, um, cm^-1, m^-1)
Definition: units.hpp:56
Base conversion class.
Definition: units.hpp:26
Here is the call graph for this function: