7 #include <odb_api/Reader.h>
8 #include <odb_api/Select.h>
10 #include "eckit/exception/Exceptions.h"
15 using namespace eckit;
27 : inputfile_(inputfile), outputfile_(outputfile)
35 Log::info() <<
"The following files will be opened and read: " << endl;
36 Log::info() <<
"ODB filename = " <<
inputfile << endl;
37 Log::info() <<
"Output NetCDF filename = " <<
outputfile << endl;
45 const odc::Column& column (*(
columns[
i]));
47 switch (column.type())
51 v = dataFile.add_var(
patchName(column.name()).c_str(), ncInt, xDim);
52 v->add_att(_FillValue,
int (column.missingValue()));
55 v = dataFile.add_var(
patchName(column.name()).c_str(), ncFloat, xDim);
56 v->add_att(_FillValue,
float (column.missingValue()));
59 v = dataFile.add_var(
patchName(column.name()).c_str(), ncDouble, xDim);
60 v->add_att(_FillValue,
double (column.missingValue()));
63 v = dataFile.add_var(
patchName(column.name()).c_str(), ncChar, xDim);
68 Log::error() <<
"Unknown column type: name=" << column.name() <<
", type=" << column.type() << endl;
69 ASSERT(
"Unknown type" &&
false);
80 NcFile dataFile(
outputfile().c_str(), NcFile::Replace);
84 if (! dataFile.is_valid())
85 throw UserError(
"Could not open file");
87 Log::info() <<
"Conversion to NetCDF 1D" << endl;
90 NcDim* xDim (dataFile.add_dim(
"hdrlen"));
91 dataFile.add_att(
"Conventions",
"CF-1.6");
98 union {
double n;
char b[
sizeof(double) + 1]; } buffer;
99 memset(&buffer, 0,
sizeof(buffer));
102 for(; it != odb.
end(); ++it, ++nrows)
105 buffer.n = ((*it)[
i]);
109 vars[
i]->put_rec(buffer.b, nrows);
112 vars[
i]->put_rec(&buffer.n, nrows);
116 Log::info() <<
"Converted " << nrows <<
" row(s)." << endl;
126 Log::info() <<
"The following files will be opened and read: " << endl;
127 Log::info() <<
"Header filename = " <<
fileNameHdr_ << endl;
129 Log::info() <<
"Output filename = " <<
outputfile << endl;
136 NcFile dataFile(
outputfile().c_str(), NcFile::Replace);
140 if (! dataFile.is_valid())
141 throw UserError (
"Couldn't open file!");
143 Log::info() <<
"Conversion to NetCDF 2D" << endl;
145 string sql =
"select distinct vertco_reference_1 from \"" +
fileNameBody_ +
"\" order by vertco_reference_1;";
150 Log::info() <<
" There are = " << nmaxchannel <<
" channels" << endl;
154 NcDim* xDim = dataFile.add_dim(
"hdrlen");
155 NcDim* yDim = dataFile.add_dim(
"maxbodylen", nmaxchannel);
159 int * channel =
new int [nmaxchannel];
166 colChannel = dataFile.add_var(its->columns()[0]->name().c_str(), ncInt, yDim);
167 channel[
i] = ((*its)[0]);
170 colChannel->put(channel, nmaxchannel);
177 colHdr =
new NcVar * [it_hdr->
columns().size()];
179 for (
int i=0;
i<it_hdr->
columns().size();++
i) {
180 switch(it_hdr->
columns()[
i]->type())
184 colHdr[
i] = dataFile.add_var(it_hdr->
columns()[
i]->name().c_str(), ncInt, xDim);
185 colHdr[
i]->add_att(_FillValue,(
int)it_hdr->
columns()[
i]->missingValue());
188 colHdr[
i] = dataFile.add_var(it_hdr->
columns()[
i]->name().c_str(), ncFloat, xDim);
189 colHdr[
i]->add_att(_FillValue, (
float)it_hdr->
columns()[
i]->missingValue());
197 ASSERT(
"Unknown type" &&
false);
204 for(; it_hdr != odb_hdr.
end(); ++it_hdr)
207 for (
int i=0;
i<it_hdr->
columns().size();++
i) {
209 colHdr[
i]->put(&nr, 1);
210 colHdr[
i]->set_cur(nrows);
220 colBody =
new NcVar * [it_body->
columns().size()];
222 int index_channel=-1;
224 for (
int i=0;
i<it_body->
columns().size();++
i) {
225 if (it_body->
columns()[
i]->name() ==
"vertco_reference_1" || it_body->
columns()[
i]->name() ==
"vertco_reference_1@body")
227 if (it_body->
columns()[
i]->name() ==
"seqno" || it_body->
columns()[
i]->name() ==
"seqno@hdr")
231 Log::info() <<
" index_vertco_reference_1 = " << index_channel << endl;
232 Log::info() <<
" index_seqno = " << index_seqno << endl;
234 if (index_channel != -1 && index_seqno != -1) {
235 for (
int i=0;
i<it_body->
columns().size();++
i) {
236 if ((it_body->
columns()[
i]->name() !=
"seqno" && it_body->
columns()[
i]->name() !=
"vertco_reference_1") &&
237 (it_body->
columns()[
i]->name() !=
"seqno@hdr" && it_body->
columns()[
i]->name() !=
"vertco_reference_1@body")) {
238 switch(it_body->
columns()[
i]->type())
242 colBody[
i] = dataFile.add_var(it_body->
columns()[
i]->name().c_str(), ncInt, xDim, yDim);
243 colBody[
i]->add_att(_FillValue,(
int)it_body->
columns()[
i]->missingValue());
246 colBody[
i] = dataFile.add_var(it_body->
columns()[
i]->name().c_str(), ncFloat, xDim, yDim);
247 colBody[
i]->add_att(_FillValue,(
float) it_body->
columns()[
i]->missingValue());
254 ASSERT(
"Unknown type" &&
false);
262 double nd[it_body->
columns().size()][nmaxchannel];
263 double lnd[nmaxchannel];
264 long icurrent_seqno=-1;
265 int icurrent_channel=-1;
266 for(; it_body != odb_body.
end(); ++it_body)
268 if (((*it_body)[index_seqno]) != icurrent_seqno) {
270 for (
int i=0;
i<it_body->
columns().size();++
i) {
271 if ((it_body->
columns()[
i]->name() !=
"seqno" && it_body->
columns()[
i]->name() !=
"vertco_reference_1") &&
272 (it_body->
columns()[
i]->name() !=
"seqno@hdr" && it_body->
columns()[
i]->name() !=
"vertco_reference_1@body")) {
274 for (
int j=0; j<nmaxchannel; ++j)
276 colBody[
i]->put_rec(&lnd[0], nrows-1);
281 icurrent_seqno = ((*it_body)[index_seqno]);
282 icurrent_channel = 0;
285 for (
int i=0;
i<it_body->
columns().size();++
i) {
286 for (
int j=0; j< nmaxchannel; ++j) {
287 nd[
i][j] = it_body->
columns()[
i]->missingValue();
291 while (icurrent_channel < nmaxchannel && channel[icurrent_channel] < ((*it_body)[index_channel])) {
296 for (
int i=0;
i<it_body->
columns().size();++
i) {
297 if ((it_body->
columns()[
i]->name() !=
"seqno" && it_body->
columns()[
i]->name() !=
"vertco_reference_1") &&
298 (it_body->
columns()[
i]->name() !=
"seqno@hdr" && it_body->
columns()[
i]->name() !=
"vertco_reference_1@body")) {
299 nd[
i][icurrent_channel] = ((*it_body)[
i]);
305 for (
int i=0;
i<it_body->
columns().size();++
i) {
306 if ((it_body->
columns()[
i]->name() !=
"seqno" && it_body->
columns()[
i]->name() !=
"vertco_reference_1") &&
307 (it_body->
columns()[
i]->name() !=
"seqno@hdr" && it_body->
columns()[
i]->name() !=
"vertco_reference_1@body")) {
308 for (
int j=0; j<nmaxchannel; ++j)
310 for (
int j=0; j<nmaxchannel; ++j)
311 Log::info() << nrows <<
" lnd[" << j <<
"]=" << lnd[j] <<
" " ;
313 colBody[
i]->put_rec(&lnd[0], nrows-1);
string patchName(const string &s)
Replaces all occurences of '@' with '_'.
std::vector< NcVar * > createVariables(NcFile &dataFile, const odc::MetaData &columns, NcDim *)
Odb2NetCDF_1D(const std::string &inputfile, const std::string &outputfile)
std::string fileNameBody_
Odb2NetCDF_2D(const std::string &inputfile, const std::string &outputfile)
std::string & outputfile()
Odb2NetCDF(const std::string &inputfile, const std::string &outputfile)
std::string & inputfile()
const iterator end() const
const core::MetaData & columns() const