bufr2synop 0.24.0
Functions | Variables
bufrdeco_wmo.c File Reference

This file has the code to read bufr files from WMO csv files. More...

#include "bufrdeco.h"
Include dependency graph for bufrdeco_wmo.c:

Go to the source code of this file.

Functions

int get_wmo_tablenames (struct bufrdeco *b)
 Get the complete pathnames for WMO csv table files needed by a bufr message. More...
 
int bufr_read_tables (struct bufrdeco *b)
 Read the tables according with bufr file data from a bufr table directory. More...
 
int bufrdeco_store_tables (struct bufr_tables **t, struct bufr_tables_cache *c, uint8_t ver)
 Init an element of array c->tab[] if still not allocated. If allocated clean it and set *t pointing to this element. More...
 
int bufrdeco_cache_tables_search (struct bufr_tables_cache *c, uint8_t ver)
 Search a struct bufr_tables in bufr_tables_cache. More...
 
int bufrdeco_free_cache_tables (struct bufr_tables_cache *c)
 deallocate and clean a bufr_tables_cache More...
 

Variables

const char DEFAULT_BUFRTABLES_WMO_CSV_DIR1 [] = "/usr/local/share/bufr2synop/"
 
const char DEFAULT_BUFRTABLES_WMO_CSV_DIR2 [] = "/usr/share/bufr2synop/"
 

Detailed Description

This file has the code to read bufr files from WMO csv files.

Definition in file bufrdeco_wmo.c.

Function Documentation

◆ bufr_read_tables()

int bufr_read_tables ( struct bufrdeco b)

Read the tables according with bufr file data from a bufr table directory.

Parameters
bbasic struct with needed data
Returns
if success return 0, otherwise 1

The default directories where to search bufr tables are stored in DEFAULT_BUFRTABLES_WMO_CSV_DIR1 and DEFAULT_BUFRTABLES_WMO_CSV_DIR2

Definition at line 162 of file bufrdeco_wmo.c.

163{
164 int index;
165 buf_t i;
166 bufrdeco_assert ( b != NULL );
167 struct bufr_tableB *tb;
168
170 {
171 // When using cache, member b->tables is actually a pointer in array b->cache.tab[]
172
173 if ( ( index = bufrdeco_cache_tables_search ( & ( b->cache ), b->sec1.master_version ) ) >= 0 )
174 {
175#ifdef __DEBUG
176 printf ( "# Found tables in cache for version %u index %d\n", b->sec1.master_version, index );
177#endif
178 // hit cache, then the only task is to change member b->tables, and restore original values from item im tableB
179 b->tables = b->cache.tab[index];
180 tb = & ( b->tables->b );
181
182 for ( i = 0; i < tb->nlines ; i++ )
183 {
184 tb->item[i].scale = tb->item[i].scale_ori;
185 tb->item[i].reference = tb->item[i].reference_ori;
186 tb->item[i].nbits = tb->item[i].nbits_ori;
187 tb->item[i].changed = 0;
188 }
189
190 // all done
191 return 0;
192 }
193 else
194 {
195#ifdef __DEBUG
196 printf ( "# Tables for version %u not found in cache. Stored in index %d\n", b->sec1.master_version, b->cache.next );
197#endif
198 // If not in cache, the new master version tables has to be stored. This implies that
199 bufrdeco_store_tables ( & ( b->tables ), & ( b->cache ), b->sec1.master_version );
200
201 // get tablenames
202 if ( get_wmo_tablenames ( b ) )
203 {
204 snprintf ( b->error, sizeof ( b->error ),"%s(): Cannot find bufr tables\n", __func__ );
205 return 1;
206 }
207
208 // Missed cache
209 if ( bufr_read_tableB ( b ) )
210 {
211 return 1;
212 }
213
214 if ( bufr_read_tableC ( b ) )
215 {
216 return 1;
217 }
218
219 if ( bufr_read_tableD ( b ) )
220 {
221 return 1;
222 }
223
224 }
225 }
226 else
227 {
228 // If tables still not initialized then do it
229 if ( b->tables == NULL && bufrdeco_init_tables ( & ( b->tables ) ) )
230 {
231 snprintf ( b->error, sizeof ( b->error ), "%s(): Cannot allocate memory for tables\n", __func__ );
232 return 1;
233 }
234 // get tablenames
235 if ( get_wmo_tablenames ( b ) )
236 {
237 snprintf ( b->error, sizeof ( b->error ),"%s(): Cannot find bufr tables\n", __func__ );
238 return 1;
239 }
240
241 // And now read tables
242 if ( bufr_read_tableB ( b ) )
243 {
244 return 1;
245 }
246
247 if ( bufr_read_tableC ( b ) )
248 {
249 return 1;
250 }
251
252 if ( bufr_read_tableD ( b ) )
253 {
254 return 1;
255 }
256 }
257 return 0;
258}
int bufrdeco_init_tables(struct bufr_tables **t)
Init a struct bufr_tables allocating space.
uint32_t buf_t
Type to set offsets and dimension of arrays or counters used in bufrdeco.
Definition: bufrdeco.h:346
#define bufrdeco_assert(__my_expr__)
Check a expression and exit if it fails.
Definition: bufrdeco.h:374
int bufr_read_tableC(struct bufrdeco *b)
Reads a file with table C content (Code table and bit flags) according with csv WMO format.
int bufr_read_tableD(struct bufrdeco *b)
Reads a file with table D content according with WMO csv format.
#define BUFRDECO_USE_TABLES_CACHE
Bit mask to the member mask of struct bufrdeco to mark the use of bufr_tables cache.
Definition: bufrdeco.h:268
int bufr_read_tableB(struct bufrdeco *b)
int get_wmo_tablenames(struct bufrdeco *b)
Get the complete pathnames for WMO csv table files needed by a bufr message.
Definition: bufrdeco_wmo.c:47
int bufrdeco_store_tables(struct bufr_tables **t, struct bufr_tables_cache *c, uint8_t ver)
Init an element of array c->tab[] if still not allocated. If allocated clean it and set *t pointing t...
Definition: bufrdeco_wmo.c:269
int bufrdeco_cache_tables_search(struct bufr_tables_cache *c, uint8_t ver)
Search a struct bufr_tables in bufr_tables_cache.
Definition: bufrdeco_wmo.c:304
uint8_t master_version
Definition: bufrdeco.h:782
Store a table B readed from a file formated and named as ECMWF bufrdc package.
Definition: bufrdeco.h:859
struct bufr_tableB_decoded_item item[BUFR_MAXLINES_TABLEB]
Definition: bufrdeco.h:867
buf_t nlines
Definition: bufrdeco.h:863
struct bufr_tables * tab[BUFRDECO_TABLES_CACHE_SIZE]
Definition: bufrdeco.h:955
struct bufr_tableB b
Definition: bufrdeco.h:939
uint32_t mask
Definition: bufrdeco.h:966
struct bufr_tables_cache cache
Definition: bufrdeco.h:974
struct bufr_tables * tables
Definition: bufrdeco.h:973
char error[1024]
Definition: bufrdeco.h:983
struct bufr_sec1 sec1
Definition: bufrdeco.h:969

References bufr_tables::b, bufr_read_tableB(), bufr_read_tableC(), bufr_read_tableD(), bufrdeco_assert, bufrdeco_cache_tables_search(), bufrdeco_init_tables(), bufrdeco_store_tables(), BUFRDECO_USE_TABLES_CACHE, bufrdeco::cache, bufr_tableB_decoded_item::changed, bufrdeco::error, get_wmo_tablenames(), bufr_tableB::item, bufrdeco::mask, bufr_sec1::master_version, bufr_tableB_decoded_item::nbits, bufr_tableB_decoded_item::nbits_ori, bufr_tables_cache::next, bufr_tableB::nlines, bufr_tableB_decoded_item::reference, bufr_tableB_decoded_item::reference_ori, bufr_tableB_decoded_item::scale, bufr_tableB_decoded_item::scale_ori, bufrdeco::sec1, bufr_tables_cache::tab, and bufrdeco::tables.

Referenced by bufrdeco_read_buffer().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ bufrdeco_cache_tables_search()

int bufrdeco_cache_tables_search ( struct bufr_tables_cache c,
uint8_t  ver 
)

Search a struct bufr_tables in bufr_tables_cache.

Parameters
cpointer to the struct bufr_tables_cache where to search
versmall int with the master version acting as a key in the seardch
Returns
The index of found struct. If no struct found returns -1

Definition at line 304 of file bufrdeco_wmo.c.

305{
306 buf_t i = 0;
307
308 for ( i = 0; i < BUFRDECO_TABLES_CACHE_SIZE ; i++ )
309 {
310 if ( c->ver[i] == ver )
311 return i; // found
312 }
313 return -1; // Not found
314}
#define BUFRDECO_TABLES_CACHE_SIZE
Max number of structs bufr_tables in a bufr_tables_cache.
Definition: bufrdeco.h:341
int8_t ver[BUFRDECO_TABLES_CACHE_SIZE]
Definition: bufrdeco.h:954

References BUFRDECO_TABLES_CACHE_SIZE, and bufr_tables_cache::ver.

Referenced by bufr_read_tables().

Here is the caller graph for this function:

◆ bufrdeco_free_cache_tables()

int bufrdeco_free_cache_tables ( struct bufr_tables_cache c)

deallocate and clean a bufr_tables_cache

Parameters
cpointer to the struct to clean
Returns
0 if success

Definition at line 322 of file bufrdeco_wmo.c.

323{
324 buf_t i;
325
326 for ( i = 0; i < BUFRDECO_TABLES_CACHE_SIZE; i++ )
327 {
328 if ( c->tab[i] )
329 {
330 free ( ( void * ) c->tab[i] );
331 }
332 }
333 // then clean
334 memset ( c, 0, sizeof ( struct bufr_tables_cache ) );
335 return 0;
336}
Struct to store the cache of structs bufr_tables.
Definition: bufrdeco.h:951

References BUFRDECO_TABLES_CACHE_SIZE, and bufr_tables_cache::tab.

Referenced by bufrdeco_close().

Here is the caller graph for this function:

◆ bufrdeco_store_tables()

int bufrdeco_store_tables ( struct bufr_tables **  t,
struct bufr_tables_cache c,
uint8_t  ver 
)

Init an element of array c->tab[] if still not allocated. If allocated clean it and set *t pointing to this element.

Parameters
tpointer to array of struct bufr_tables
cpointer ti struct bufr_tables_cache
verversion of tables acting as key
Returns
if success return 0

Definition at line 269 of file bufrdeco_wmo.c.

270{
271 if ( c->tab[c->next] == NULL )
272 {
273 // Init the array element
274 bufrdeco_init_tables ( & ( c->tab[c->next] ) );
275
276 // increase the counter of allocated elements
277 ( c->nt )++;
278 }
279 else
280 {
281 // Clean the element in array with zeroes
282 memset ( c->tab[c->next], 0, sizeof ( struct bufr_tables ) );
283
284 // sets the proper version as a key of element
285 c->ver[c->next] = ver;
286 }
287
288 // t will point to array element
289 *t = c->tab[c->next];
290
291 // Set the member 'next'. It marks the next element in array it will be modified when failing a search in future
292 c->next = ( c->next + 1 ) % BUFRDECO_TABLES_CACHE_SIZE;
293 return 0;
294}
Contains all tables needed to parse a bufr file.
Definition: bufrdeco.h:938

References bufrdeco_init_tables(), BUFRDECO_TABLES_CACHE_SIZE, bufr_tables_cache::next, bufr_tables_cache::nt, bufr_tables_cache::tab, and bufr_tables_cache::ver.

Referenced by bufr_read_tables().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ get_wmo_tablenames()

int get_wmo_tablenames ( struct bufrdeco b)

Get the complete pathnames for WMO csv table files needed by a bufr message.

Parameters
bpointer for a struct bufrdeco
Returns
if success return 0, otherwise 1

For WMO files this format is adopted BUFR_XX_Y_Z_TableB_en for table B BUFR_XX_Y_Z_CodeFlag for Code table and Flag table. This is equivalent to table C in ECMWF package BUFR_XX_Y_Z_TableD_en for table D

XX is the Version number of master table used Y is the revision (currently ignored) Z is minor revision (currently ignored)

Definition at line 47 of file bufrdeco_wmo.c.

48{
49 struct stat st;
50 char aux[BUFRDECO_PATH_LENGTH - 32];
51
52 bufrdeco_assert ( b != NULL );
53
54 if ( b->bufrtables_dir[0] == '\0' )
55 {
56 // try to guess directory
57 if ( stat ( DEFAULT_BUFRTABLES_WMO_CSV_DIR1, &st ) )
58 {
59 if ( stat ( DEFAULT_BUFRTABLES_WMO_CSV_DIR2, &st ) )
60 {
61 return 1;
62 }
63 else
64 {
65 if ( S_ISDIR ( st.st_mode ) )
66 {
67 strcpy ( aux, DEFAULT_BUFRTABLES_WMO_CSV_DIR2 );
68 }
69 }
70 }
71 else
72 {
73 if ( S_ISDIR ( st.st_mode ) )
74 {
75 strcpy ( aux, DEFAULT_BUFRTABLES_WMO_CSV_DIR1 );
76 }
77 }
78 }
79 else
80 {
81 strcpy ( aux, b->bufrtables_dir );
82 }
83
84 switch ( b->sec1.master_version )
85 {
86 case 4:
87 case 5:
88 case 6:
89 case 7:
90 case 8:
91 case 9:
92 case 10:
93 case 11:
94 case 12:
95 case 13:
96 snprintf ( b->tables->b.path, sizeof ( b->tables->b.path ),"%sBUFR_13_0_0_TableB_en.csv", aux );
97 snprintf ( b->tables->c.path, sizeof ( b->tables->c.path ),"%sBUFR_13_0_0_TableC_en.csv", aux );
98 snprintf ( b->tables->d.path, sizeof ( b->tables->d.path ),"%sBUFR_13_0_0_TableD_en.csv", aux );
99 break;
100 case 18:
101 snprintf ( b->tables->b.path, sizeof ( b->tables->b.path ),"%sBUFR_18_1_0_TableB_en.csv", aux );
102 snprintf ( b->tables->c.path, sizeof ( b->tables->c.path ),"%sBUFR_18_1_0_TableC_en.csv", aux );
103 snprintf ( b->tables->d.path, sizeof ( b->tables->d.path ),"%sBUFR_18_1_0_TableD_en.csv", aux );
104 break;
105 case 19:
106 snprintf ( b->tables->b.path, sizeof ( b->tables->b.path ),"%sBUFR_19_1_1_TableB_en.csv", aux );
107 snprintf ( b->tables->c.path, sizeof ( b->tables->c.path ),"%sBUFR_19_1_1_TableC_en.csv", aux );
108 snprintf ( b->tables->d.path, sizeof ( b->tables->d.path ),"%sBUFR_19_1_1_TableD_en.csv", aux );
109 break;
110 case 22:
111 snprintf ( b->tables->b.path, sizeof ( b->tables->b.path ),"%sBUFR_22_0_1_TableB_en.csv", aux );
112 snprintf ( b->tables->c.path, sizeof ( b->tables->c.path ),"%sBUFR_22_0_1_TableC_en.csv", aux );
113 snprintf ( b->tables->d.path, sizeof ( b->tables->d.path ),"%sBUFR_22_0_1_TableD_en.csv", aux );
114 break;
115 case 14:
116 case 15:
117 case 16:
118 case 17:
119 case 20:
120 case 21:
121 case 23:
122 case 24:
123 case 25:
124 case 26:
125 case 27:
126 case 28:
127 case 29:
128 case 30:
129 case 31:
130 case 32:
131 case 33:
132 case 34:
133 case 35:
134 case 36:
135 case 37:
136 snprintf ( b->tables->b.path, sizeof ( b->tables->b.path ),"%sBUFR_%d_0_0_TableB_en.csv", aux, b->sec1.master_version );
137 snprintf ( b->tables->c.path, sizeof ( b->tables->c.path ),"%sBUFR_%d_0_0_TableC_en.csv", aux, b->sec1.master_version );
138 snprintf ( b->tables->d.path, sizeof ( b->tables->d.path ),"%sBUFR_%d_0_0_TableD_en.csv", aux, b->sec1.master_version );
139 break;
140 case 38:
141 snprintf ( b->tables->b.path, sizeof ( b->tables->b.path ),"%sBUFR_%d_1_0_TableB_en.csv", aux, b->sec1.master_version );
142 snprintf ( b->tables->c.path, sizeof ( b->tables->c.path ),"%sBUFR_%d_1_0_TableC_en.csv", aux, b->sec1.master_version );
143 snprintf ( b->tables->d.path, sizeof ( b->tables->d.path ),"%sBUFR_%d_1_0_TableD_en.csv", aux, b->sec1.master_version );
144 break;
145 default:
146 snprintf ( b->tables->b.path, sizeof ( b->tables->b.path ),"%sBUFR_38_1_0_TableB_en.csv", aux );
147 snprintf ( b->tables->c.path, sizeof ( b->tables->c.path ),"%sBUFR_38_1_0_TableC_en.csv", aux );
148 snprintf ( b->tables->d.path, sizeof ( b->tables->d.path ),"%sBUFR_38_1_0_TableD_en.csv", aux );
149 break;
150 }
151 return 0;
152}
#define BUFRDECO_PATH_LENGTH
Length for files/directory path strings.
Definition: bufrdeco.h:335
const char DEFAULT_BUFRTABLES_WMO_CSV_DIR2[]
Definition: bufrdeco_wmo.c:30
const char DEFAULT_BUFRTABLES_WMO_CSV_DIR1[]
Definition: bufrdeco_wmo.c:29
char path[BUFRDECO_PATH_LENGTH]
Definition: bufrdeco.h:861
char path[BUFRDECO_PATH_LENGTH]
Definition: bufrdeco.h:893
char path[BUFRDECO_PATH_LENGTH]
Definition: bufrdeco.h:922
struct bufr_tableD d
Definition: bufrdeco.h:941
struct bufr_tableC c
Definition: bufrdeco.h:940
char bufrtables_dir[BUFRDECO_PATH_LENGTH]
Definition: bufrdeco.h:982

References bufr_tables::b, bufrdeco_assert, BUFRDECO_PATH_LENGTH, bufrdeco::bufrtables_dir, bufr_tables::c, bufr_tables::d, DEFAULT_BUFRTABLES_WMO_CSV_DIR1, DEFAULT_BUFRTABLES_WMO_CSV_DIR2, bufr_sec1::master_version, bufr_tableB::path, bufr_tableC::path, bufr_tableD::path, bufrdeco::sec1, and bufrdeco::tables.

Referenced by bufr_read_tables().

Here is the caller graph for this function:

Variable Documentation

◆ DEFAULT_BUFRTABLES_WMO_CSV_DIR1

const char DEFAULT_BUFRTABLES_WMO_CSV_DIR1[] = "/usr/local/share/bufr2synop/"

And these are the default directories when using WMO csv table files

Definition at line 29 of file bufrdeco_wmo.c.

Referenced by get_wmo_tablenames().

◆ DEFAULT_BUFRTABLES_WMO_CSV_DIR2

const char DEFAULT_BUFRTABLES_WMO_CSV_DIR2[] = "/usr/share/bufr2synop/"

Definition at line 30 of file bufrdeco_wmo.c.

Referenced by get_wmo_tablenames().