IODA Bundle
SetTool.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 #include <strings.h>
12 
13 #include "eckit/filesystem/PathName.h"
14 #include "eckit/log/Log.h"
15 #include "eckit/utils/Tokenizer.h"
16 
17 #include "odc/ConstantSetter.h"
18 #include "odc/Reader.h"
19 #include "odc/Writer.h"
20 
21 #include "SetTool.h"
22 
23 using namespace eckit;
24 using namespace odc::core;
25 
26 namespace odc {
27 namespace tool {
28 
29 SetTool::SetTool (int argc, char *parameters[]) : Tool(argc, parameters) { }
30 
32 {
33  if (parameters().size() != 4)
34  {
35  Log::error() << "Usage: ";
37  Log::error() << std::endl;
38  return;
39  }
40 
41  std::vector<std::string> columns;
42  std::vector<double> values;
43 
44  PathName inFile = parameters(2);
45  PathName outFile = parameters(3);
46 
47  Reader in(inFile);
48  Writer<> out(outFile);
49 
51 
52  Reader::iterator sourceIt = in.begin();
53  const Reader::iterator sourceEnd = in.end();
54 
55  parseUpdateList(parameters(1), columns, values);
56 
57  typedef ConstantSetter<Reader::iterator> Setter;
58  Setter setter(sourceIt, sourceEnd, columns, values);
59  Setter::iterator begin = setter.begin();
60  const Setter::iterator end = setter.end();
61  writer->pass1(begin, end);
62 }
63 
64 void SetTool::parseUpdateList(std::string s, std::vector<std::string>& columns, std::vector<double>& values)
65 {
66  Tokenizer splitAssignments(",");
67  std::vector<std::string> assignments;
68  splitAssignments(s, assignments);
69 
70  Tokenizer splitEq("=");
71 
72  for (size_t i = 0; i < assignments.size(); ++i)
73  {
74  std::vector<std::string> assignment;
75  splitEq(assignments[i], assignment);
76  ASSERT(assignment.size() == 2);
77 
78  std::string colName = assignment[0];
79  std::string value = assignment[1];
80 
81  Log::info() << "SetTool::parseUpdateList: " << colName << "='" << value << "'" << std::endl;
82 
83  double v = 0;
84 
85  if (value.find("0x") != 0)
86  v = translate(value);
87  else
88  {
89  value = value.substr(2);
90  ASSERT("Format of the hexadecimal value is not correct" && (value.size() % 2) == 0);
91  ASSERT("Hexadecimal literal is too long" && (value.size() / 2) <= sizeof(double));
92 
93  bzero(&v, sizeof(double));
94  for (size_t i = 0; i < value.size() / 2; ++i)
95  {
96  std::string byteInHex = value.substr(i * 2, 2);
97  char *p = 0;
98  unsigned char x;
99  reinterpret_cast<unsigned char*>(&v)[i] = x = static_cast<unsigned char>(strtoul(byteInHex.c_str(), &p, 16));
100  Log::debug() << "SetTool::parseUpdateList: '" << byteInHex << "' => " << x << std::endl;
101  }
102  }
103 
104  columns.push_back(colName);
105  values.push_back(v);
106  }
107 }
108 
109 } // namespace tool
110 } // namespace odc
111 
const iterator end() const
Definition: Reader.cc:81
iterator begin()
Definition: Reader.cc:74
static double translate(const std::string &v)
Definition: StringTool.cc:133
iterator begin(bool openDataHandle=true)
Definition: Writer.cc:92
const std::vector< std::string > parameters()
static void usage(const std::string &name, std::ostream &o)
Definition: SetTool.h:28
void parseUpdateList(std::string s, std::vector< std::string > &columns, std::vector< double > &values)
Definition: SetTool.cc:64
Definition: ColumnInfo.h:23