libdballe  5.18
common.h
1 /*
2  * DB-ALLe - Archive for punctual meteorological data
3  *
4  * Copyright (C) 2005--2010 ARPA-SIM <urpsim@smr.arpa.emr.it>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18  *
19  * Author: Enrico Zini <enrico@enricozini.com>
20  */
21 
22 #ifndef DBA_AOF_IMPORTERS_COMMON_H
23 #define DBA_AOF_IMPORTERS_COMMON_H
24 
25 /*
26  * Common functions for all AOF decoders.
27  */
28 
29 #include <dballe/msg/msg.h>
30 #include <dballe/msg/aof_codec.h>
31 
32 #include <stdio.h>
33 #include <stdint.h> /* uint32_t */
34 #include <math.h>
35 
36 // #define TRACE_DECODER
37 
38 #ifdef TRACE_DECODER
39 #define TRACE(...) fprintf(stderr, __VA_ARGS__)
40 #define IFTRACE if (1)
41 #else
42 #define TRACE(...) do { } while (0)
43 #define IFTRACE if (0)
44 #endif
45 
46 #define AOF_UNDEF 0x7fffffff
47 
48 #define OBS(n) (obs[n-1])
49 
50 namespace dballe {
51 namespace msg {
52 
53 /* Parse a 2 bit confidence interval into a percent confidence interval */
54 static inline int get_conf2(uint32_t conf)
55 {
56  switch (conf & 3)
57  {
58  case 0: return 76; break;
59  case 1: return 51; break;
60  case 2: return 26; break;
61  case 3: return 0; break;
62  }
63  return 0;
64 }
65 
66 /* Parse a 6 bit confidence interval into a percent confidence interval */
67 static inline int get_conf6(uint32_t conf)
68 {
69  return get_conf2(conf >> 3);
70 }
71 
72 /* Count the number of bits present in a word */
73 static inline int count_bits(uint32_t v)
74 {
75  /* Algorithm based on Magic Binary Numbers
76  * http://graphics.stanford.edu/~seander/bithacks.html
77  const int S[] = {1, 2, 4, 8, 16}; // Magic Binary Numbers
78  const int B[] = {0x55555555, 0x33333333, 0x0F0F0F0F, 0x00FF00FF, 0x0000FFFF};
79 
80  v = ((v >> S[0]) & B[0]) + (v & B[0]);
81  v = ((v >> S[1]) & B[1]) + (v & B[1]);
82  v = ((v >> S[2]) & B[2]) + (v & B[2]);
83  v = ((v >> S[3]) & B[3]) + (v & B[3]);
84  v = ((v >> S[4]) & B[4]) + (v & B[4]);
85 
86  return c;
87  */
88 
89  // Kernigan algorithm (1 iteration per bit set to 1): better because where
90  // this function is used the bits set to 1 are usually few
91 
92  unsigned int c; // c accumulates the total bits set in v
93  for (c = 0; v; c++)
94  v &= v - 1; // clear the least significant bit set
95 
96  return c;
97 }
98 
99 static inline uint32_t get_extra_conf(const uint32_t* obs, int idx)
100 {
101  int count = count_bits(OBS(32));
102  uint32_t w = OBS(33+count+idx/4);
103  return (w >> ((idx % 4) * 6)) & 0x3f;
104 
105 }
106 
107 /* Convert Kelvin from AOF to Kelvins */
108 static inline double totemp(double k) { return k / 10.0; }
109 
110 #if 0
111 /* Dump a word */
112 void dba_aof_dump_word(const char* prefix, uint32_t x);
113 
114 
115 uint32_t dba_aof_get_extra_conf(const uint32_t* obs, int idx);
116 
117 #endif
118 
119 } // namespace msg
120 } // namespace dballe
121 
122 /* vim:set ts=4 sw=4: */
123 #endif