IODA Bundle
Codec.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/Codec.h"
12 
13 #include "eckit/exception/Exceptions.h"
14 
15 #include "odc/core/CodecFactory.h"
16 
17 using namespace eckit;
18 
19 namespace odc {
20 namespace core {
21 
22 //----------------------------------------------------------------------------------------------------------------------
23 
24 Codec::Codec(const std::string& name, api::ColumnType type)
25 : name_(name),
26  hasMissing_(false),
27  missingValue_(odc::MDI::realMDI()),
28  min_(missingValue_),
29  max_(missingValue_),
30  type_(type) {}
31 
32 std::unique_ptr<Codec> Codec::clone()
33 {
35  c->hasMissing_ = hasMissing_;
36  c->missingValue_ = missingValue_;
37  c->min_ = min_;
38  c->max_ = max_;
39  return c;
40 }
41 
43 
45  if (ds.isOther()) {
46  setDataStream(ds.other());
47  } else {
49  }
50 }
51 
53  throw eckit::SeriousBug("Mismatched byte order between DataStream and Codec", Here());
54 }
55 
57  throw eckit::SeriousBug("Mismatched byte order between DataStream and Codec", Here());
58 }
59 
61  if (ds.isOther()) {
62  load(ds.other());
63  } else {
64  load(ds.same());
65  }
66 }
67 
69  throw eckit::SeriousBug("Mismatched byte order between DataStream and Codec", Here());
70 }
71 
73  throw eckit::SeriousBug("Mismatched byte order between DataStream and Codec", Here());
74 }
75 
77  if (ds.isOther()) {
78  save(ds.other());
79  } else {
80  save(ds.same());
81  }
82 }
83 
85  throw eckit::SeriousBug("Mismatched byte order between DataStream and Codec", Here());
86 }
87 
89  throw eckit::SeriousBug("Mismatched byte order between DataStream and Codec", Here());
90 }
91 
92 void Codec::missingValue(double v)
93 {
94  ASSERT("Cannot change missing value after encoding of column data started" && (min_ == missingValue_) && (max_ == missingValue_));
95  min_ = max_ = missingValue_ = v;
96 }
97 
98 void Codec::gatherStats(const double& v)
99 {
100  if(v == missingValue_)
101  hasMissing_ = 1;
102  else
103  {
104  if(v < min_ || min_ == missingValue_)
105  min_ = v;
106  if(v > max_ || max_ == missingValue_)
107  max_ = v;
108  }
109 }
110 
111 void Codec::print(std::ostream& s) const {
112  s << name_
113  << ", range=<" << std::fixed << min_ << "," << max_ << ">";
114 
115  if (hasMissing_) {
116  s << ", missingValue=" << missingValue_;
117  }
118 }
119 
120 //----------------------------------------------------------------------------------------------------------------------
121 
122 } // namespace core
123 } // namespace odc
124 
Definition: MDI.h:19
std::unique_ptr< Codec > build(const std::string &name, api::ColumnType type) const
Definition: CodecFactory.h:145
static CodecFactory & instance()
Definition: CodecFactory.cc:25
std::string name_
Definition: Codec.h:96
void setDataStream(GeneralDataStream &ds)
Definition: Codec.cc:44
virtual void print(std::ostream &s) const
Definition: Codec.cc:111
double missingValue() const
Definition: Codec.h:73
double missingValue_
Definition: Codec.h:99
void save(GeneralDataStream &ds)
Definition: Codec.cc:76
double max_
Definition: Codec.h:101
api::ColumnType type_
Definition: Codec.h:103
virtual void gatherStats(const double &v)
Definition: Codec.cc:98
double min_
Definition: Codec.h:100
void load(GeneralDataStream &ds)
Definition: Codec.cc:60
virtual std::unique_ptr< Codec > clone()
Definition: Codec.cc:32
int32_t hasMissing_
Definition: Codec.h:98
virtual ~Codec()
Definition: Codec.cc:42
DataStream< SameByteOrder > & same()
Definition: DataStream.h:129
DataStream< OtherByteOrder > & other()
Definition: DataStream.h:130
Definition: ColumnInfo.h:23