IODA Bundle
Real.h
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 #ifndef odc_core_codec_Real_H
12 #define odc_core_codec_Real_H
13 
14 #include "odc/core/Codec.h"
15 
16 
17 namespace odc {
18 namespace codec {
19 
20 //----------------------------------------------------------------------------------------------------------------------
21 
22 static constexpr uint32_t minFloatAsInt = 0x800000;
23 static constexpr uint32_t maxFloatAsInt = 0x7f7fffff;
24 static constexpr uint32_t real2MissingAsInt = 0xff7fffff; // = - reinterpret_cast<float&>(maxFloatAsInt)
25 
26 //----------------------------------------------------------------------------------------------------------------------
27 
28 template<typename ByteOrder>
29 class CodecLongReal : public core::DataStreamCodec<ByteOrder> {
30 
31 public: // definitions
32 
33  constexpr static const char* codec_name() { return "long_real"; }
34 
35 public: // methods
36 
38  core::DataStreamCodec<ByteOrder>(codec_name(), type),
41 
42  ~CodecLongReal() override {}
43 
46 
47 private: // methods
48 
49  unsigned char* encode(unsigned char* p, const double& d) override {
50  double e = d;
51  ByteOrder::swap(e);
52  memcpy(p, &e, sizeof(e));
53  return p + sizeof(e);
54  }
55 
56  void decode(double* out) override {
57  this->ds().read(*out);
58  }
59 
60  void skip() override {
61  this->ds().advance(sizeof(double));
62  }
63 
64  /// Keep track on internal missing value collisions, to help the CodecOptimizer.
65  void gatherStats(const double& v) override {
67 
68  float realInternalMissing = reinterpret_cast<const float&>(minFloatAsInt);
69  float realInternalMissing2 = reinterpret_cast<const float&>(maxFloatAsInt);
70  if (v == realInternalMissing) hasShortRealInternalMissing_ = true;
71  if (v == realInternalMissing2) hasShortReal2InternalMissing_ = true;
72  }
73 
74 private: // members
75 
78 };
79 
80 
81 //----------------------------------------------------------------------------------------------------------------------
82 
83 template <typename ByteOrder, uint32_t InternalMissing>
84 class ShortRealBase : public core::DataStreamCodec<ByteOrder> {
85 
86 public: // methods
87 
88  ShortRealBase(api::ColumnType type, const std::string& name) : core::DataStreamCodec<ByteOrder>(name, type) {}
89  ~ShortRealBase() override {}
90 
91 private: // methods
92 
93  unsigned char* encode(unsigned char* p, const double& d) override {
94 
95  const uint32_t internalMissingInt = InternalMissing;
96  const float internalMissing = reinterpret_cast<const float&>(internalMissingInt);
97 
98  float s;
99  if (d == this->missingValue_) {
100  s = internalMissing;
101  } else {
102  s = d;
103  ASSERT(s != internalMissing);
104  }
105 
106  ByteOrder::swap(s);
107  ::memcpy(p, &s, sizeof(s));
108  return p + sizeof(s);
109  }
110 
111  void decode(double* out) override {
112  float s;
113  this->ds().read(s);
114  const uint32_t internalMissingInt = InternalMissing;
115  const float internalMissing = reinterpret_cast<const float&>(internalMissingInt);
116  (*out) = (s == internalMissing ? this->missingValue_ : s);
117  }
118 
119  void skip() override {
120  this->ds().advance(sizeof(float));
121  }
122 };
123 
124 
125 template <typename ByteOrder>
126 struct CodecShortReal : public ShortRealBase<ByteOrder, minFloatAsInt> {
127  constexpr static const char* codec_name() { return "short_real"; }
130 };
131 
132 
133 template <typename ByteOrder>
134 struct CodecShortReal2 : public ShortRealBase<ByteOrder, real2MissingAsInt> {
135  constexpr static const char* codec_name() { return "short_real2"; }
138 };
139 
140 
141 //----------------------------------------------------------------------------------------------------------------------
142 
143 } // namespace codec
144 } // namespace odc
145 
146 #endif
147 
constexpr static const char * codec_name()
Definition: Real.h:33
void gatherStats(const double &v) override
Keep track on internal missing value collisions, to help the CodecOptimizer.
Definition: Real.h:65
bool hasShortReal2InternalMissing_
Definition: Real.h:77
bool hasShortRealInternalMissing_
Definition: Real.h:76
bool hasShortReal2InternalMissing() const
Definition: Real.h:45
CodecLongReal(api::ColumnType type)
Definition: Real.h:37
void skip() override
Definition: Real.h:60
void decode(double *out) override
Definition: Real.h:56
bool hasShortRealInternalMissing() const
Definition: Real.h:44
~CodecLongReal() override
Definition: Real.h:42
unsigned char * encode(unsigned char *p, const double &d) override
Definition: Real.h:49
void decode(double *out) override
Definition: Real.h:111
unsigned char * encode(unsigned char *p, const double &d) override
Definition: Real.h:93
~ShortRealBase() override
Definition: Real.h:89
ShortRealBase(api::ColumnType type, const std::string &name)
Definition: Real.h:88
void skip() override
Definition: Real.h:119
const std::string & name() const
Definition: Codec.h:40
double missingValue_
Definition: Codec.h:99
virtual void gatherStats(const double &v)
Definition: Codec.cc:98
DataStreamCodec(const std::string &name, api::ColumnType type)
Definition: Codec.h:125
DataStream< ByteOrder > & ds()
Definition: Codec.h:157
static constexpr uint32_t maxFloatAsInt
Definition: Real.h:23
static constexpr uint32_t real2MissingAsInt
Definition: Real.h:24
static constexpr uint32_t minFloatAsInt
Definition: Real.h:22
Definition: ColumnInfo.h:23
CodecShortReal2(api::ColumnType type)
Definition: Real.h:136
constexpr static const char * codec_name()
Definition: Real.h:135
CodecShortReal(api::ColumnType type)
Definition: Real.h:128
constexpr static const char * codec_name()
Definition: Real.h:127