IODA Bundle
Column.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 "odc/core/Column.h"
12 
13 #include "eckit/utils/StringTools.h"
14 
15 
16 using namespace eckit;
17 using namespace odc::api;
18 
19 namespace odc {
20 namespace core {
21 
22 //----------------------------------------------------------------------------------------------------------------------
23 
24 Column::Column(MetaData &owner)
25 : owner_(owner),
26  name_(),
27  type_(IGNORE),
28  bitfieldDef_()
29 {}
30 
32 : owner_(o.owner_),
33  name_(o.name_),
34  type_(o.type_),
35  bitfieldDef_(o.bitfieldDef_)
36 {
37  *this = o;
38 }
39 
41 
43 {
44  name(other.name());
45 
46 // type<DataStream<SameByteOrder, DataHandle> >(other.type(), false);
47  type_ = other.type_;
48  if (type_ == BITFIELD)
49  bitfieldDef(other.bitfieldDef());
50 
51  if (other.coder_) {
52  coder_ = other.coder_->clone();
53  } else {
54  coder_.reset();
55  }
56 // hasMissing(other.hasMissing());
57 // missingValue(other.missingValue());
58  return *this;
59 }
60 
62 {
63  switch(type) {
64  case IGNORE: return "IGNORE";
65  case INTEGER: return "INTEGER";
66  case REAL: return "REAL";
67  case DOUBLE: return "DOUBLE";
68  case STRING: return "STRING";
69  case BITFIELD: return "BITFIELD";
70  default: return "UNKNOWN_TYPE";
71  }
72 }
73 
74 ColumnType Column::type(const std::string& t)
75 {
76  std::string ut(StringTools::upper(t));
77  if (ut == "IGNORE") return IGNORE;
78  if (ut == "INTEGER") return INTEGER;
79  if (ut == "REAL") return REAL;
80  if (ut == "DOUBLE") return DOUBLE;
81  if (ut == "STRING") return STRING;
82  if (ut == "BITFIELD") return BITFIELD;
83 
84  Log::error() << "Unknown type: '" << t << "'" << std::endl;
85  ASSERT(0 && "Unknown type");
86  return IGNORE;
87 }
88 
90 {
91  // FIXME
92  return coder().name() == "constant"
93  || coder().name() == "constant_string";
94 }
95 
96 //Column::Column(DataHandle *dataHandle) : dataHandle_(dataHandle), name_(), type_(IGNORE/*?*/), coder_(0) {}
97 
98 
99 /// return true if names and types are the same; do not compare codecs.
100 bool Column::operator==(const Column& other) const {
101  return equals(other);
102 }
103 
104 bool Column::equals(const Column& other, bool compareDataSizes) const {
105  if (name() == other.name() && type() == other.type()) {
106  if (compareDataSizes) {
107  return (dataSizeDoubles() == other.dataSizeDoubles());
108  } else {
109  return true;
110  }
111  }
112  return false;
113 }
114 
115 void Column::print(std::ostream& s) const
116 {
117  s << "name: " << name_ << ", ";
118  s << "type: " << columnTypeName(ColumnType(type_));
119 
120  if (type_ == BITFIELD)
121  {
122  eckit::sql::FieldNames names = bitfieldDef_.first;
123  eckit::sql::Sizes sizes = bitfieldDef_.second;
124  ASSERT(names.size() == sizes.size());
125  s << " [";
126  for (size_t i = 0; i < names.size(); ++i)
127  s << names[i] << ":" << sizes[i]
128  << (i != names.size() - 1 ? ";" : "");
129  s << "] ";
130  }
131 
132  s << ", ";
133 
134  s << "codec: ";
135  if (coder_) s << *coder_;
136  else s << "NONE";
137 }
138 
139 //----------------------------------------------------------------------------------------------------------------------
140 
141 } // namespace core
142 } // namespace odc
143 
const std::string & name() const
Definition: Codec.h:40
void bitfieldDef(const eckit::sql::BitfieldDef &b)
Definition: Column.h:85
std::unique_ptr< Codec > coder_
Definition: Column.h:110
Codec & coder() const
Definition: Column.h:51
std::string name_
Definition: Column.h:107
virtual ~Column()
Definition: Column.cc:40
bool equals(const Column &other, bool compareDataSizes=true) const
Definition: Column.cc:104
size_t dataSizeDoubles() const
Definition: Column.h:54
virtual void print(std::ostream &s) const
Definition: Column.cc:115
const eckit::sql::BitfieldDef & bitfieldDef() const
Definition: Column.h:86
Column(MetaData &)
Definition: Column.cc:24
static const char * columnTypeName(api::ColumnType type)
Definition: Column.cc:61
api::ColumnType type() const
Definition: Column.h:64
void name(const std::string name)
Definition: Column.h:57
Column & operator=(const Column &)
Definition: Column.cc:42
static api::ColumnType type(const std::string &)
Definition: Column.cc:74
bool isConstant()
Definition: Column.cc:89
bool operator==(const Column &) const
return true if names and types are the same; do not compare codecs.
Definition: Column.cc:100
const std::string & name() const
Definition: Column.h:58
eckit::sql::BitfieldDef bitfieldDef_
bitfieldDef_ is not empty if type_ == BITFIELD.
Definition: Column.h:112
int32_t type_
Note: type_ should be ColumnType, but it is saved on file so must be of a fixed size type.
Definition: Column.h:109
@ BITFIELD
Definition: ColumnType.h:27
Definition: ColumnInfo.h:23