13 #include "eckit/config/Resource.h"
14 #include "eckit/filesystem/PathName.h"
15 #include "eckit/log/Log.h"
16 #include "eckit/utils/StringTools.h"
17 #include "eckit/utils/Tokenizer.h"
18 #include "odc/FastODA2Request.h"
19 #include "odc/GribCodes.h"
24 using namespace eckit;
31 ODA2RequestTool::ODA2RequestTool(
int argc,
char **argv)
51 o <<
name <<
" [-c configFile] [-q] <input-file.odb> [<output-file>]";
56 eckit::PathName inputFile;
78 if (outputFile.size() == 0)
79 std::cout << request << std::endl;
82 ofstream out(outputFile.c_str());
83 out << request << std::endl;
92 return optionArgument(
"-c", std::string(
"~odc/codes/ODA2RequestTool.cfg"));
99 Log::debug() <<
"ODA2RequestTool::readConfig: reading file '" << fileName <<
"'" << std::endl;
104 Log::debug() <<
"ODA2RequestTool::readConfig: parsing '" << s <<
"'" << std::endl;
111 Log::debug() <<
"ODA2RequestTool::parseConfig: '" << s <<
"'" << std::endl;
113 vector<std::string> lines;
114 Tokenizer(
"\n")(s, lines);
116 Tokenizer tokenizer(
": \t");
117 for (
size_t i = 0;
i < lines.size(); ++
i)
119 vector<std::string> words;
120 tokenizer(lines[
i], words);
122 if (words.size() == 0)
125 ASSERT(
"Each line of config file should be like: 'MARS_KEYWORD : oda_column_name'" && words.size() == 2);
140 FastODA2Request<ODA2RequestClientTraits> o;
142 o.scanFile(inputFile);
143 return o.genRequest();
158 columnList += it->first;
161 const string select = std::string(
"select ") + columnList +
" from \"" + inputFile +
"\";";
162 Log::info() <<
"Executing '" << select <<
"'" << std::endl;
164 Translator<double, string> double2string;
168 for (
size_t i = 0;
i < n; ++
i)
173 : double2string((*row)[
i]);
180 stringstream request;
192 if (request.str().size()) request <<
",\n";
194 const std::string&
key = it->second;
195 const string k = StringTools::upper(
key);
199 for (Values::iterator vi = vs.begin(); vi != vs.end(); ++vi)
202 Log::debug() <<
"ODA2RequestTool::genRequest: v = '" << v <<
"', key = " <<
key << std::endl;
206 if (k ==
"CLASS" || k ==
"TYPE" || k ==
"STREAM")
208 Log::debug() <<
"ODA2RequestTool::genRequest: checking if '" << v <<
"' is numeric" << std::endl;
212 Log::debug() <<
"ODA2RequestTool::genRequest: replacing " << v <<
" with ";
213 v = GribCodes::alphanumeric(StringTools::lower(
key), v);
214 Log::debug() << v << std::endl;
216 v = StringTools::upper(v);
219 if (vi != vs.begin())
224 request <<
key <<
" = " << valuesList;
229 str <<
"ODB," << std::endl;
230 str << request.str();
std::string trim(const std::string &str)