libwreport  3.34
varinfo.h
Go to the documentation of this file.
1 #ifndef WREPORT_VARINFO_H
2 #define WREPORT_VARINFO_H
3 
4 #include <cstdint>
5 #include <string>
6 #include <iosfwd>
7 #include <wreport/fwd.h>
8 
9 namespace wreport {
10 
59 typedef uint16_t Varcode;
60 
62 std::string varcode_format(Varcode code);
63 
67 #define WR_VAR(f, x, y) ((wreport::Varcode)( ((unsigned)(f)<<14) | ((unsigned)(x)<<8) | (unsigned)(y) ))
68 
75 #define WR_STRING_TO_VAR(str) ((wreport::Varcode)( \
76  (( ((str)[0] - '0')*10 + ((str)[1] - '0') ) << 8) | \
77  ( ((str)[2] - '0')*100 + ((str)[3] - '0')*10 + ((str)[4] - '0') ) \
78 ))
79 
81 #define WR_VAR_F(code) (((code) >> 14) & 0x3)
82 
84 #define WR_VAR_X(code) ((code) >> 8 & 0x3f)
85 
87 #define WR_VAR_Y(code) ((code) & 0xff)
88 
96 #define WR_VAR_FXY(code) WR_VAR_F(code), WR_VAR_X(code), WR_VAR_Y(code)
97 
108 Varcode varcode_parse(const char* desc);
109 
110 
112 enum class Vartype : unsigned
113 {
114  // Integer value
115  Integer,
116  // Floating point value
117  Decimal,
118  // String value
119  String,
120  // Opaque binary value
121  Binary,
122 };
123 
124 
126 const char* vartype_format(Vartype type);
127 
129 Vartype vartype_parse(const char* s);
130 
131 std::ostream& operator<<(std::ostream& out, const Vartype& t);
132 
138 struct _Varinfo
139 {
142 
145 
147  char desc[64];
148 
151  char unit[24];
152 
159  int scale;
160 
162  unsigned len;
163 
170  int bit_ref;
171 
173  unsigned bit_len;
174 
176  int imin;
177 
179  int imax;
180 
182  double dmin;
183 
185  double dmax;
186 
196  int encode_decimal(double fval) const;
197 
201  double round_decimal(double val) const;
202 
212  uint32_t encode_binary(double fval) const;
213 
223  double decode_decimal(int val) const;
224 
234  double decode_binary(uint32_t val) const;
235 
238  const char* desc,
239  const char* unit,
240  int scale=0, unsigned len=0,
241  int bit_ref=0, int bit_len=0);
242 
245  const char* desc,
246  const char* unit,
247  int scale=0, unsigned len=0);
248 
256  void set_string(Varcode code, const char* desc, unsigned len);
257 
265  void set_binary(Varcode code, const char* desc, unsigned bit_len);
266 
273 };
274 
275 
284 typedef const _Varinfo* Varinfo;
285 
286 }
287 #endif
String functions.
Definition: benchmark.h:13
Vartype vartype_parse(const char *s)
Return a Vartype from its string description.
Vartype
Variable type.
Definition: varinfo.h:113
std::string varcode_format(Varcode code)
Format a varcode into a string.
const _Varinfo * Varinfo
Varinfo reference.
Definition: fwd.h:10
const char * vartype_format(Vartype type)
Return a string description of a Vartype.
Varcode varcode_parse(const char *desc)
Convert a FXXYYY string descriptor code into its short integer representation.
uint16_t Varcode
Holds the WMO variable code of a variable.
Definition: fwd.h:12
Information about a variable.
Definition: varinfo.h:139
double decode_decimal(int val) const
Decode a double value from a decimal integer value using Varinfo decimal encoding informations (scale...
double round_decimal(double val) const
Round val so that it only fits the significant digits given in scale.
int imin
Minimum unscaled decimal integer value the field can have.
Definition: varinfo.h:176
Varcode code
Variable code, as in WMO BUFR/CREX table B.
Definition: varinfo.h:141
char unit[24]
Measurement unit of the variable, using the units defined in WMO BUFR/CREX table B.
Definition: varinfo.h:151
unsigned len
Length in digits of the variable encoded as a decimal integer.
Definition: varinfo.h:162
double dmin
Minimum value the field can have.
Definition: varinfo.h:182
int bit_ref
Binary reference value for the variable.
Definition: varinfo.h:170
int scale
Scale of the variable, defining its decimal precision.
Definition: varinfo.h:159
unsigned bit_len
Length in bits of the variable when encoded as an unsigned binary value.
Definition: varinfo.h:173
int imax
Minimum unscaled decimal integer value the field can have.
Definition: varinfo.h:179
double decode_binary(uint32_t val) const
Decode a double value from a decimal integer value using Varinfo binary encoding informations (bit_re...
char desc[64]
Freeform variable description.
Definition: varinfo.h:147
void set_crex(Varcode code, const char *desc, const char *unit, int scale=0, unsigned len=0)
Set all the base Varinfo fields, then call compute_range.
void compute_range()
Compute the widest ranges for imin, imax, dmin and dmax that can fit any value that can be encoded bo...
double dmax
Maximum value the field can have.
Definition: varinfo.h:185
uint32_t encode_binary(double fval) const
Encode a double value into a positive integer value using Varinfo binary encoding informations (bit_r...
void set_string(Varcode code, const char *desc, unsigned len)
Set all the fields to represent a string variable.
int encode_decimal(double fval) const
Encode a double value into a decimal integer value using Varinfo decimal encoding informations (scale...
Vartype type
Type of the value stored in the variable.
Definition: varinfo.h:144
void set_binary(Varcode code, const char *desc, unsigned bit_len)
Set all the fields to represent an opaque binary variable.
void set_bufr(Varcode code, const char *desc, const char *unit, int scale=0, unsigned len=0, int bit_ref=0, int bit_len=0)
Set all the base Varinfo fields, then call compute_range.