libwreport  3.5
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 
7 namespace wreport {
8 
57 typedef uint16_t Varcode;
58 
60 std::string varcode_format(Varcode code);
61 
65 #define WR_VAR(f, x, y) ((wreport::Varcode)( ((unsigned)(f)<<14) | ((unsigned)(x)<<8) | (unsigned)(y) ))
66 
73 #define WR_STRING_TO_VAR(str) ((wreport::Varcode)( \
74  (( ((str)[0] - '0')*10 + ((str)[1] - '0') ) << 8) | \
75  ( ((str)[2] - '0')*100 + ((str)[3] - '0')*10 + ((str)[4] - '0') ) \
76 ))
77 
79 #define WR_VAR_F(code) (((code) >> 14) & 0x3)
80 
82 #define WR_VAR_X(code) ((code) >> 8 & 0x3f)
83 
85 #define WR_VAR_Y(code) ((code) & 0xff)
86 
94 #define WR_VAR_FXY(code) WR_VAR_F(code), WR_VAR_X(code), WR_VAR_Y(code)
95 
106 Varcode varcode_parse(const char* desc);
107 
108 
110 enum class Vartype : unsigned
111 {
112  // Integer value
113  Integer,
114  // Floating point value
115  Decimal,
116  // String value
117  String,
118  // Opaque binary value
119  Binary,
120 };
121 
122 
124 const char* vartype_format(Vartype type);
125 
127 Vartype vartype_parse(const char* s);
128 
129 
135 struct _Varinfo
136 {
138  Varcode code;
139 
142 
144  char desc[64];
145 
148  char unit[24];
149 
156  int scale;
157 
159  unsigned len;
160 
167  int bit_ref;
168 
170  unsigned bit_len;
171 
173  int imin;
174 
176  int imax;
177 
179  double dmin;
180 
182  double dmax;
183 
193  int encode_decimal(double fval) const;
194 
198  double round_decimal(double val) const;
199 
209  uint32_t encode_binary(double fval) const;
210 
220  double decode_decimal(int val) const;
221 
231  double decode_binary(uint32_t val) const;
232 
234  void set_bufr(Varcode code,
235  const char* desc,
236  const char* unit,
237  int scale=0, unsigned len=0,
238  int bit_ref=0, int bit_len=0);
239 
241  void set_crex(Varcode code,
242  const char* desc,
243  const char* unit,
244  int scale=0, unsigned len=0);
245 
253  void set_string(Varcode code, const char* desc, unsigned len);
254 
262  void set_binary(Varcode code, const char* desc, unsigned bit_len);
263 
269  void compute_range();
270 };
271 
272 
281 typedef const _Varinfo* Varinfo;
282 
283 }
284 #endif
Vartype vartype_parse(const char *s)
Return a Vartype from its string description.
Varcode varcode_parse(const char *desc)
Convert a FXXYYY string descriptor code into its short integer representation.
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.
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:182
Vartype type
Type of the value stored in the variable.
Definition: varinfo.h:141
int scale
Scale of the variable, defining its decimal precision.
Definition: varinfo.h:156
Vartype
Variable type.
Definition: varinfo.h:110
Varcode code
Variable code, as in WMO BUFR/CREX table B.
Definition: varinfo.h:138
void set_binary(Varcode code, const char *desc, unsigned bit_len)
Set all the fields to represent an opaque binary variable.
double dmin
Minimum 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...
uint16_t Varcode
Holds the WMO variable code of a variable.
Definition: varinfo.h:57
void set_string(Varcode code, const char *desc, unsigned len)
Set all the fields to represent a string variable.
double round_decimal(double val) const
Round val so that it only fits the significant digits given in scale.
Information about a variable.
Definition: varinfo.h:135
char desc[64]
Freeform variable description.
Definition: varinfo.h:144
int bit_ref
Binary reference value for the variable.
Definition: varinfo.h:167
std::string varcode_format(Varcode code)
Format a varcode into a string.
uint32_t encode_binary(double fval) const
Encode a double value into a positive integer value using Varinfo binary encoding informations (bit_r...
unsigned bit_len
Length in bits of the variable when encoded as an unsigned binary value.
Definition: varinfo.h:170
String functions.
Definition: benchmark.h:13
int imax
Minimum unscaled decimal integer value the field can have.
Definition: varinfo.h:176
const char * vartype_format(Vartype type)
Return a string description of a Vartype.
unsigned len
Length in digits of the variable encoded as a decimal integer.
Definition: varinfo.h:159
double decode_decimal(int val) const
Decode a double value from a decimal integer value using Varinfo decimal encoding informations (scale...
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.
int encode_decimal(double fval) const
Encode a double value into a decimal integer value using Varinfo decimal encoding informations (scale...
const _Varinfo * Varinfo
Varinfo reference.
Definition: varinfo.h:281
int imin
Minimum unscaled decimal integer value the field can have.
Definition: varinfo.h:173
char unit[24]
Measurement unit of the variable, using the units defined in WMO BUFR/CREX table B.
Definition: varinfo.h:148