IODA Bundle
Examples.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 /// This file contains examples of usage of public APIs.
12 
13 #include <string>
14 #include <iostream>
15 #include <vector>
16 
17 #include "eckit/filesystem/PathName.h"
18 #include "eckit/io/FileHandle.h"
19 
20 #include "odc/api/Odb.h"
21 
22 #include "odc/Select.h"
23 #include "odc/Reader.h"
24 #include "odc/Writer.h"
25 
26 #include "TestCase.h"
27 
28 using namespace eckit;
29 
30 namespace {
31 
32 TEST(example_select_data_read_results)
33 {
34  // Prepare input data
35  const std::string data =
36  R"(x:INTEGER,y:INTEGER,v:DOUBLE
37  1,1,0.3
38  1,1,0.2
39  2,2,0.4
40  2,2,0.1)";
41 
42  FileHandle out("example_select_data_read_results.odb");
43  out.openForWrite(0);
44  AutoClose close(out);
45  odc::api::odbFromCSV(data, out);
46 
47  odc::Select select("select x,min(v),max(v);", "example_select_data_read_results.odb");
48 
49  for (odc::Select::iterator it (select.begin()),
50  end (select.end());
51  it != end;
52  ++it)
53  {
54  double r0 = (*it)[0],
55  r1 = (*it)[1],
56  r2 = (*it)[2];
57 
58  std::cout << r0 << ", " << r1 << ", " << r2 << std::endl;
59  }
60 }
61 
62 
63 TEST(example_read_data)
64 {
65  // Prepare input data
66  const std::string data = "x:INTEGER,y:INTEGER,v:DOUBLE\n" "1,1,0.3\n" "1,1,0.2\n" "2,2,0.4\n" "2,2,0.1\n";
67  FileHandle out("example_read_data.odb");
68  out.openForWrite(0);
69  AutoClose close(out);
70  odc::api::odbFromCSV(data, out);
71 
72  odc::Reader o("example_read_data.odb");
73  for (odc::Reader::iterator it (o.begin()),
74  end (o.end());
75  it != end;
76  ++it)
77  {
78  double r0 = (*it)[0],
79  r1 = (*it)[1],
80  r2 = (*it)[2];
81 
82  std::cout << r0 << ", " << r1 << ", " << r2 << std::endl;
83  }
84 }
85 
86 TEST(example_write_data)
87 {
88  odc::core::MetaData metaData;
89  metaData
90  .addColumn("x", "INTEGER")
91  .addColumn("y", "INTEGER")
92  .addColumn("v", "DOUBLE");
93 
94  odc::Writer<> writer("example_write_data.odb");
95  odc::Writer<>::iterator it (writer.begin());
96  it->columns(metaData);
97  it->writeHeader();
98 
99  for (size_t i (1); i <= 1000; ++i)
100  {
101  (*it)[0] = i;
102  (*it)[1] = i*2;
103  (*it)[2] = i*3;
104 
105  // Incrementing iterator moves coursor to the next row.
106  ++it;
107  }
108 }
109 
110 } // namespace
111 
const iterator end() const
Definition: Reader.cc:81
iterator begin()
Definition: Reader.cc:74
void writeHeader()
const core::MetaData & columns() const
Definition: IteratorProxy.h:94
const iterator end()
Definition: Select.cc:77
iterator begin()
Definition: Select.cc:81
MetaData & addColumn(const std::string &name, const std::string &type)
Definition: MetaData.cc:279
TEST(example_write_data)
Definition: Examples.cc:82
size_t odbFromCSV(DataHandle &dh_in, DataHandle &dh_out, const std::string &delimiter)
odbFromCSV returns number of lines imported
Definition: Odb.cc:375