IODA Bundle
LSTool.cc
Go to the documentation of this file.
1 /*
2  * (C) Copyright 1996-2012 ECMWF.
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  * In applying this licence, ECMWF does not waive the privileges and immunities
7  * granted to it by virtue of its status as an intergovernmental organisation nor
8  * does it submit to any jurisdiction.
9  */
10 
11 
12 #include <fstream>
13 
14 #include "odc/Reader.h"
15 #include "odc/tools/LSTool.h"
16 
17 using namespace eckit;
18 
19 namespace odc {
20 namespace tool {
21 
22 LSTool::LSTool (int argc, char *argv[]) : Tool(argc, argv)
23 {
24  registerOptionWithArgument("-o"); // Text Output
25 }
26 
27 const std::string LSTool::nullString;
28 
29 unsigned long long LSTool::printData(const std::string &db, std::ostream &out)
30 {
31  odc::Reader f(db);
32  odc::Reader::iterator it = f.begin();
33  odc::Reader::iterator end = f.end();
34 
35  core::MetaData md(0);
36  // Formatting of real values:
37  out << std::fixed;
38  unsigned long long n = 0;
39  for ( ; it != end; ++it, ++n)
40  {
41  if (md != it->columns())
42  {
43  md = it->columns();
44  const char* spacer = "";
45  for (size_t i = 0; i < md.size(); ++i) {
46  out << spacer << md[i]->name();
47  spacer = "\t";
48  }
49  out << std::endl;
50  }
51  const char* spacer = "";
52  for (size_t i = 0; i < md.size(); ++i)
53  {
54  out << spacer;
55  if (it->isMissing(i)) {
56  out << ".";
57  } else {
58  switch(md[i]->type())
59  {
60  case odc::api::INTEGER:
61  case odc::api::BITFIELD:
62  if (it->isMissing(i)) {
63  out << ".";
64  } else {
65  out << static_cast<int>((*it)[i]);
66  }
67  break;
68  case odc::api::REAL:
69  case odc::api::DOUBLE:
70  out << (*it)[i];
71  break;
72  case odc::api::STRING:
73  out << "'" << (*it).string(i) << "'";
74  break;
75  case odc::api::IGNORE:
76  default:
77  ASSERT("Unknown type" && false);
78  break;
79  }
80  }
81  spacer = "\t";
82  }
83  out << std::endl;
84  }
85  return n;
86 }
87 
89 {
90  if (parameters().size() < 2)
91  {
92  Log::error() << "Usage: ";
94  Log::error() << std::endl;
95  return;
96  }
97 
98  std::string db = parameters(1);
99 
100  std::unique_ptr<std::ofstream> foutPtr;
101  if (optionIsSet("-o"))
102  foutPtr.reset(new std::ofstream(optionArgument("-o", std::string("")).c_str()));
103  std::ostream& out = optionIsSet("-o") ? *foutPtr : std::cout;
104 
105  unsigned long long n = 0;
106  n = printData(db, out);
107  Log::info() << "Selected " << n << " row(s)." << std::endl;
108 }
109 
110 } // namespace tool
111 } // namespace odc
112 
const core::MetaData & columns() const
Definition: IteratorProxy.h:94
bool isMissing(size_t i)
Definition: IteratorProxy.h:98
bool optionIsSet(const std::string &)
T optionArgument(const std::string &, T defaultValue)
void registerOptionWithArgument(const std::string &)
const std::vector< std::string > parameters()
static void usage(const std::string &name, std::ostream &o)
Definition: LSTool.h:28
static const std::string nullString
Definition: LSTool.h:38
unsigned long long printData(const std::string &db, std::ostream &out)
Definition: LSTool.cc:29
@ BITFIELD
Definition: ColumnType.h:27
Definition: ColumnInfo.h:23