IODA Bundle
CAPIExamples.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 /// \file CAPIExamples.cc
12 ///
13 /// This file contains examples of usage of public APIs.
14 ///
15 /// @author Piotr Kuchta, ECMWF, September 2015
16 
17 #include <cstring>
18 #include <iostream>
19 #include <math.h>
20 #include <string>
21 #include <vector>
22 
23 #include "eckit/exception/Exceptions.h"
24 #include "eckit/io/FileHandle.h"
25 
26 #include "odc/api/odc.h"
27 #include "odc/odccapi.h"
28 
29 #include "TestCase.h"
30 
31 namespace {
32 
33 // TODO: Migrate this test over!!!
34 
35 #if 0 // DISABLED
36 
37 TEST(c_api_example_select_data_read_results)
38 {
39  const int maxCols = 4;
40  int err;
41  double buffer[maxCols];
42  double* data = buffer;
43  int newDataset = 0;
44  int nRows = 0;
45  oda_ptr oh;
47  size_t i;
48 
49  // Prepare input data
50 
51  long size = 0;
52  void* buf = 0;
53 
54  ASSERT(false); //odc_import_encode_text(
55 // "x:INTEGER,y:INTEGER,v:DOUBLE,f:BITFIELD[a:1;b:2]]\n"
56 // "1,1,0.3,0\n"
57 // "1,1,0.2,1\n"
58 // "2,2,0.4,2\n"
59 // "2,2,0.1,3\n", ",", 0, &size);
60 
61  eckit::FileHandle dh("c_example_select_data_read_results.odb");
62  dh.openForWrite(size);
63  ASSERT(dh.write(buf, size) == size);
64  dh.close();
65  free(buf);
66 
67  oh = odb_select_create("", &err);
68  ASSERT(0 == err);
69  it = odb_create_select_iterator(oh, "select x, min(v), max(v)"
70  " from \"c_example_select_data_read_results.odb\""
71  " order by x;", &err);
72  ASSERT(0 == err);
73  for (i = 0; 0 == odb_select_iterator_get_next_row(it, 3, data, &newDataset); ++i)
74  {
75  switch (i) {
76  case 0: ASSERT(data[0] == 1 && fabs(data[1] - 0.2) < 0.0000001); break;
77  case 1: ASSERT(data[0] == 2 && fabs(data[1] - 0.1) < 0.0000001); break;
78  }
79  }
80  ASSERT(i == 2);
81  ASSERT(0 == odb_select_iterator_destroy(it));
82 
83  // Select some bitfields
84  it = odb_create_select_iterator(oh, "select f, f.a, f.b from \"c_example_select_data_read_results.odb\";", &err);
85  ASSERT(0 == err);
86 
87  for (i = 0; 0 == odb_select_iterator_get_next_row(it, 3, data, &newDataset); ++i)
88  {
89  double f = data[0], a = data[1], b = data[2];
90  switch (i) {
91  case 0: ASSERT(f == 0 && a == 0 && b == 0); break;
92  case 1: ASSERT(f == 1 && a == 1 && b == 0); break;
93  case 2: ASSERT(f == 2 && a == 0 && b == 1); break;
94  case 3: ASSERT(f == 3 && a == 1 && b == 1); break;
95  }
96  }
97  ASSERT(i == 4);
98 
99  ASSERT(0 == odb_select_iterator_destroy(it));
100  ASSERT(0 == odb_select_destroy(oh));
101 }
102 
103 
104 TEST(c_api_example_read_data)
105 {
106  const int numberOfColumns = 4;
107  int err, nCols, type, nameLength;
108  char *name;
109  oda_ptr oh;
110  oda_read_iterator* it;
111  double buffer[numberOfColumns];
112  double* data;
113  int newDataset;
114  int nRows;
115 
116  // Prepare input data
117  long size = 0;
118  void* buf = 0; ASSERT(false); //odc_import_encode_text(
119 // "x:INTEGER,y:REAL,v:DOUBLE,f:BITFIELD[a:1;b:2]\n"
120 // "1,1,0.3,0\n"
121 // "2,1,0.2,1\n"
122 // "3,2,0.4,2\n"
123 // "4,2,0.1,3\n", ",", 0, &size);
124 
125  eckit::FileHandle dh("c_api_example_read_data.odb");
126  dh.openForWrite(size);
127  ASSERT(dh.write(buf, size) == size);
128  dh.close();
129  free(buf);
130 
131 
132  oh = odb_read_create("", &err);
133  it = odb_create_read_iterator(oh, "c_api_example_read_data.odb", &err);
134  ASSERT(0 == err);
135  ASSERT(0 != it);
136 
138  ASSERT(nCols == numberOfColumns);
139 
140  ASSERT(0 == odb_read_iterator_get_column_type(it, 0, &type));
141  ASSERT(type == 1 /*INTEGER*/);
142 
143  ASSERT(0 == odb_read_iterator_get_column_type(it, 1, &type));
144  ASSERT(type == 2 /*REAL*/);
145 
146  ASSERT(0 == odb_read_iterator_get_column_type(it, 2, &type));
147  ASSERT(type == 5 /*DOUBLE*/);
148 
149  ASSERT(0 == odb_read_iterator_get_column_type(it, 3, &type));
150  ASSERT(type == 4 /*BITFIELD*/);
151 
152  ASSERT(0 == odb_read_iterator_get_column_name(it, 0, &name, &nameLength));
153  ASSERT(0 == strncmp("x", name, strlen("x")));
154  ASSERT(0 == odb_read_iterator_get_column_name(it, 1, &name, &nameLength));
155  ASSERT(0 == strncmp("y", name, strlen("y")));
156  ASSERT(0 == odb_read_iterator_get_column_name(it, 2, &name, &nameLength));
157  ASSERT(0 == strncmp("v", name, strlen("v")));
158  ASSERT(0 == odb_read_iterator_get_column_name(it, 3, &name, &nameLength));
159  ASSERT(0 == strncmp("f", name, strlen("f")));
160 
161  data = buffer;
162  newDataset = 0;
163  for (nRows = 0; 0 == odb_read_iterator_get_next_row(it, numberOfColumns, data, &newDataset); ++nRows)
164  {
165  int x = int(data[0]);
166  int f = int(data[3]);
167 
168  std::cout << "Read row " << nRows << ", x=" << x << std::endl;
169 
170  ASSERT(x == nRows + 1 && f == nRows);
171  }
172 
173  ASSERT(nRows == 4);
174 
175  ASSERT(0 == odb_read_iterator_destroy(it));
176  ASSERT(0 == odb_read_destroy(oh));
177 }
178 
179 #endif
180 
181 TEST(c_api_example_write_data)
182 {
183  int err;
185  oda_write_iterator* wi = odb_create_write_iterator(writer, "c_api_example_write_data.odb", &err);
186  ASSERT(0 == odb_write_iterator_set_no_of_columns(wi, 4));
187  ASSERT(0 == odb_write_iterator_set_column(wi, 0, odc::api::INTEGER, "x"));
188  ASSERT(0 == odb_write_iterator_set_column(wi, 1, odc::api::REAL, "y"));
189  ASSERT(0 == odb_write_iterator_set_column(wi, 2, odc::api::DOUBLE, "v"));
190  // Define three fields: a (1 bit only), b (2 bits), c (1 bit)
191  ASSERT(0 == odb_write_iterator_set_bitfield(wi, 3, odc::api::BITFIELD, "bf", "a:b:c", "1:2:1"));
192  ASSERT(0 == odb_write_iterator_write_header(wi));
193 
194  double data[4];
195  for (int i = 1; i <= 10; ++i)
196  {
197  data[0] = i;
198  data[1] = i * 10;
199  data[2] = i * 100;
200  data[3] = i;
201 
202  ASSERT(0 == odb_write_iterator_set_next_row(wi, data, 4));
203  }
204 
205  ASSERT(0 == odb_write_iterator_destroy(wi));
206  ASSERT(0 == odb_writer_destroy(writer));
207 }
208 
209 } // namespace
210 
TEST(c_api_example_write_data)
@ BITFIELD
Definition: ColumnType.h:27
oda_read_iterator_ptr odb_create_read_iterator(oda_ptr co, const char *filename, int *err)
Definition: odccapi.cc:198
int odb_write_iterator_write_header(oda_write_iterator_ptr wi)
Definition: odccapi.cc:514
int odb_read_iterator_destroy(oda_read_iterator_ptr it)
Definition: odccapi.cc:252
int odb_read_iterator_get_no_of_columns(oda_read_iterator_ptr it, int *numberOfColumns)
Definition: odccapi.cc:264
int odb_select_destroy(oda_ptr o)
Definition: odccapi.cc:186
int odb_select_iterator_destroy(oda_select_iterator_ptr it)
Definition: odccapi.cc:258
int odb_write_iterator_set_column(oda_write_iterator_ptr wi, int index, int type, const char *name)
Definition: odccapi.cc:412
oda_ptr odb_read_create(const char *config, int *err)
Definition: odccapi.cc:144
oda_ptr odb_select_create(const char *config, int *err)
Definition: odccapi.cc:157
int odb_select_iterator_get_next_row(oda_select_iterator_ptr it, int count, double *data, int *new_dataset)
Definition: odccapi.cc:358
int odb_write_iterator_set_no_of_columns(oda_write_iterator_ptr wi, int n)
Definition: odccapi.cc:405
oda_writer_ptr odb_writer_create(const char *config, int *err)
Definition: odccapi.cc:167
int odb_writer_destroy(oda_writer_ptr o)
Definition: odccapi.cc:192
int odb_read_iterator_get_column_type(oda_select_iterator_ptr it, int n, int *type)
Definition: odccapi.cc:306
int odb_write_iterator_set_next_row(oda_write_iterator_ptr wi, double *data, int count)
Definition: odccapi.cc:521
oda_write_iterator_ptr odb_create_write_iterator(oda_ptr co, const char *filename, int *err)
Definition: odccapi.cc:387
oda_select_iterator_ptr odb_create_select_iterator(oda_ptr co, const char *sql, int *err)
Definition: odccapi.cc:215
int odb_read_iterator_get_column_name(oda_read_iterator_ptr it, int n, char **name, int *size_name)
Definition: odccapi.cc:320
int odb_write_iterator_destroy(oda_write_iterator_ptr wi)
Definition: odccapi.cc:399
int odb_read_destroy(oda_ptr o)
Definition: odccapi.cc:175
int odb_write_iterator_set_bitfield(oda_write_iterator_ptr wi, int index, int type, const char *name, const char *bitfieldNames, const char *bitfieldSizes)
Definition: odccapi.cc:418
int odb_read_iterator_get_next_row(oda_read_iterator_ptr it, int count, double *data, int *new_dataset)
Definition: odccapi.cc:336
void oda_writer
Definition: odccapi.h:41
void oda_read_iterator
Definition: odccapi.h:38
void oda_select_iterator
Definition: odccapi.h:39
void * oda_ptr
Definition: odccapi.h:30
void oda_write_iterator
Definition: odccapi.h:42