icedb  version 0.5.1
Snow particle scattering database API
main.cpp
Go to the documentation of this file.
1 #include <icedb/defs.h>
5 #include <boost/lexical_cast.hpp>
6 #include <boost/program_options.hpp>
7 #include <exception>
8 #include <iostream>
10 #include <icedb/units/units.hpp>
11 #include <icedb/error.hpp>
12 
13 int main(int argc, char** argv) {
14  using namespace std;
15  int retval = 0;
16  try {
17  using namespace icedb;
18  namespace po = boost::program_options;
19 
20  po::options_description desc("Allowed options"), cmdline("Command-line options"),
21  config("Config options"), hidden("Hidden options"), oall("all options");
22 
23  cmdline.add_options()
24  ("help,h", "produce help message")
25  ("list-all", "List all refractive index providers")
26  ("list-subst", po::value<string>(), "List all refractive index providers for a given substance")
27  ("list-substs", "List all substances for which refractive indices can be determined")
28  ("list-provider", po::value<string>(), "List information about a refractive index provider (e.g. source paper, domain of validity)")
29  ("subst", po::value<string>(), "Substance of interest (ice, water)")
30  ("freq,f", po::value<double>(), "Frequency")
31  ("freq-units", po::value<string>()->default_value("GHz"), "Frequency units")
32  ("temp,T", po::value<double>(), "Temperature")
33  ("temp-units", po::value<string>()->default_value("K"), "Temperature units")
34  ;
35 
36  po::positional_options_description p;
37  p.add("subst", 1);
38  p.add("freq", 1);
39  p.add("freq-units", 1);
40  p.add("temp", 1);
41  p.add("temp-units", 1);
42 
43  desc.add(cmdline).add(config);
44  oall.add(cmdline).add(config).add(hidden);
45 
46  po::variables_map vm;
47  po::store(po::command_line_parser(argc, argv).
48  options(oall).positional(p).run(), vm);
49  po::notify(vm);
50 
51  auto doHelp = [&](const std::string &m) { cerr << desc << "\n" << m << endl; exit(1); };
52  if (vm.count("help") || argc < 2) doHelp("");
53 
54  if (vm.count("list-all")) {
57  return 0;
58  }
59  if (vm.count("list-substs")) {
61  return 0;
62  }
63  if (vm.count("list-subst")) {
64  string lsubst = vm["list-subst"].as<string>();
65  auto ps = icedb::refract::listAllProviders(lsubst);
66  if (!ps) ICEDB_throw(error::error_types::xNullPointer)
67  .add<std::string>("Reason", "Cannot find any refractive index formulas for this substance")
68  .add<std::string>("Substance", lsubst);
70  return 0;
71  }
72  if (vm.count("list-provider")) {
73  string lprov = vm["list-provider"].as<string>();
74  auto ps = icedb::refract::findProviderByName(lprov);
75  if (!ps) ICEDB_throw(error::error_types::xNullPointer)
76  .add<std::string>("Reason", "Cannot find this refractive index provider")
77  .add<std::string>("Provider", lprov);
79  return 0;
80  }
81  if (!vm.count("subst")) doHelp("Must specify a substance.");
82  string subst = vm["subst"].as<string>();
83  string freqUnits = vm["freq-units"].as<string>();
84  string tempUnits = vm["temp-units"].as<string>();
85  bool hasFreq = false, hasTemp = false;
86  double inFreq = 0, inTemp = 0;
87  if (vm.count("freq")) { hasFreq = true; inFreq = vm["freq"].as<double>(); }
88  if (vm.count("temp")) { hasTemp = true; inTemp = vm["temp"].as<double>(); }
89  complex<double> m(0, 0);
90 
91  // If an exact provider is specified by name, only a single entry is returned.
92  // Otherwise, all possible matches are returned.
93  auto provAll = icedb::refract::findProviders(subst, hasFreq, hasTemp);
94  // Iterate until a provider works.
95  bool found = false;
96  string prov;
97  for (const auto &p : *(provAll.get())) {
98  prov = p.second->name;
99  if (p.second->speciality_function_type == icedb::refract::provider_s::spt::FREQTEMP) {
101  icedb::refract::prepRefract(p.second, freqUnits, tempUnits, f);
102  if (f) {
103  try {
104  f(inFreq, inTemp, m);
105  cout << m << "\twas found using provider " << prov << "." << endl;
106  found = true;
107  //break;
108  }
109  catch (std::exception &e) { if (prov == subst) cerr << e.what(); } // Out of range
110  }
111  }
112  else if (p.second->speciality_function_type == icedb::refract::provider_s::spt::FREQ) {
114  icedb::refract::prepRefract(p.second, freqUnits, f);
115  if (f) {
116  try {
117  f(inFreq, m);
118  cout << m << "\twas found using provider " << prov << "." << endl;
119  found = true;
120  //break;
121  }
122  catch (std::exception &e) { if (prov == subst) cerr << e.what(); } // Out of range
123  }
124  }
125  else {
126  continue;
127  }
128  }
129  //if (found) {
130  // cout << m << "\twas found using provider " << prov << "." << endl;
131  //}
132  if (!found) {
133  cerr << "A refractive index provider that could handle the input cannot be found." << endl;
134  return 3;
135  }
136  //std::shared_ptr<scatdb::units::converter> cnv;
137  //cnv = std::shared_ptr<scatdb::units::converter>(new scatdb::units::converter(inUnits, outUnits));
138  }
139  catch (std::exception &e) {
140  cerr << "An exception has occurred: " << e.what() << endl;
141  retval = 2;
142  }
143  return retval;
144 }
int main(int argc, char **argv)
Definition: main.cpp:25
void enumSubstances(std::ostream &out)
Definition: refract.cpp:184
STL namespace.
void enumProviders(all_providers_p p, std::ostream &out)
Definition: refract.cpp:175
void enumProvider(provider_p p, std::ostream &out)
Definition: refract.cpp:158
all_providers_p listAllProviders()
Definition: refract.cpp:145
std::function< void(double, double, std::complex< double > &)> refractFunction_freq_temp_t
Definition: refract.hpp:60
#define ICEDB_throw(x)
Definition: error.hpp:88
void prepRefract(provider_p prov, const std::string &inFreqUnits, refractFunction_freqonly_t &res)
Definition: refract.cpp:268
all_providers_p findProviders(const std::string &subst, bool haveFreq, bool haveTemp)
Definition: refract.cpp:236
provider_p findProviderByName(const std::string &providerName)
Definition: refract.cpp:191
std::function< void(double, std::complex< double > &)> refractFunction_freqonly_t
Definition: refract.hpp:59